diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2015-01-16 17:07:02 +0100 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2015-01-16 17:07:02 +0100 |
commit | 6124b7b5e687eff120507dccfe98b72b7ddb702d (patch) | |
tree | 8f84aecb8afcc4116bda5fff0c44cb3c7fa9daee | |
parent | 1fd73399caa9a1ea24bd626e607980ac94debd64 (diff) | |
download | lua-compat-5.3-6124b7b5e687eff120507dccfe98b72b7ddb702d.tar.gz lua-compat-5.3-6124b7b5e687eff120507dccfe98b72b7ddb702d.tar.bz2 lua-compat-5.3-6124b7b5e687eff120507dccfe98b72b7ddb702d.zip |
add new fields to math library
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | compat53.lua | 77 | ||||
-rwxr-xr-x | tests/test.lua | 116 |
3 files changed, 165 insertions, 37 deletions
@@ -63,6 +63,8 @@ your project: | |||
63 | * `string.pack`, `string.packsize`, and `string.unpack` if the | 63 | * `string.pack`, `string.packsize`, and `string.unpack` if the |
64 | `struct` module is available. (`struct` is not 100% compatible | 64 | `struct` module is available. (`struct` is not 100% compatible |
65 | to Lua 5.3's string packing!) | 65 | to Lua 5.3's string packing!) |
66 | * `math.maxinteger` and `math.mininteger`, `math.tointeger`, `math.type`, | ||
67 | and `math.ult` | ||
66 | 68 | ||
67 | ### C | 69 | ### C |
68 | 70 | ||
@@ -107,8 +109,11 @@ For Lua 5.1 additionally: | |||
107 | 109 | ||
108 | ## What's not implemented | 110 | ## What's not implemented |
109 | 111 | ||
110 | * the new Lua functions of Lua 5.3 | 112 | * bit operators |
111 | * the table library doesn't respect metamethods yet | 113 | * integer division operator |
114 | * `table.move` | ||
115 | * `coroutine.isyieldable` | ||
116 | * `ipairs` and the `table` library don't respect metamethods yet | ||
112 | * Lua 5.1: `_ENV`, `goto`, labels, ephemeron tables, etc. See | 117 | * Lua 5.1: `_ENV`, `goto`, labels, ephemeron tables, etc. See |
113 | [`lua-compat-5.2`][2] for a detailed list. | 118 | [`lua-compat-5.2`][2] for a detailed list. |
114 | * the following C API functions/macros: | 119 | * the following C API functions/macros: |
diff --git a/compat53.lua b/compat53.lua index 98e1a3d..0346cb7 100644 --- a/compat53.lua +++ b/compat53.lua | |||
@@ -3,10 +3,13 @@ local lua_version = _VERSION:sub(-3) | |||
3 | if lua_version ~= "5.3" then | 3 | if lua_version ~= "5.3" then |
4 | 4 | ||
5 | -- load utf8 library | 5 | -- load utf8 library |
6 | utf8 = require("compat53.utf8") | 6 | local ok, utf8lib = pcall(require, "compat53.utf8") |
7 | package.loaded["utf8"] = utf8 | 7 | if ok then |
8 | if lua_version == "5.1" then | 8 | utf8 = utf8lib |
9 | utf8.charpattern = "[%z\1-\127\194-\244][\128-\191]*" | 9 | package.loaded["utf8"] = utf8lib |
10 | if lua_version == "5.1" then | ||
11 | utf8lib.charpattern = "[%z\1-\127\194-\244][\128-\191]*" | ||
12 | end | ||
10 | end | 13 | end |
11 | 14 | ||
12 | -- use Roberto's struct module for string packing/unpacking for now | 15 | -- use Roberto's struct module for string packing/unpacking for now |
@@ -20,5 +23,71 @@ if lua_version ~= "5.3" then | |||
20 | string.unpack = struct.unpack | 23 | string.unpack = struct.unpack |
21 | end | 24 | end |
22 | 25 | ||
26 | |||
27 | -- update math library | ||
28 | do | ||
29 | local maxint, minint = 1, 0 | ||
30 | |||
31 | while maxint+1 > maxint and 2*maxint > maxint do | ||
32 | maxint = maxint * 2 | ||
33 | end | ||
34 | if 2*maxint <= maxint then | ||
35 | maxint = 2*maxint-1 | ||
36 | minint = -maxint-1 | ||
37 | else | ||
38 | maxint = maxint | ||
39 | minint = -maxint | ||
40 | end | ||
41 | math.maxinteger = maxint | ||
42 | math.mininteger = minint | ||
43 | |||
44 | local _type = type | ||
45 | function math.tointeger(n) | ||
46 | if _type(n) == "number" and n <= maxint and n >= minint and n % 1 == 0 then | ||
47 | return n | ||
48 | end | ||
49 | return nil | ||
50 | end | ||
51 | |||
52 | function math.type(n) | ||
53 | if _type(n) == "number" then | ||
54 | if n <= maxint and n >= minint and n % 1 == 0 then | ||
55 | return "integer" | ||
56 | else | ||
57 | return "float" | ||
58 | end | ||
59 | else | ||
60 | return nil | ||
61 | end | ||
62 | end | ||
63 | |||
64 | local _error = error | ||
65 | local function checkinteger(x, i, f) | ||
66 | local t = _type(x) | ||
67 | if t ~= "number" then | ||
68 | _error("bad argument #"..i.." to '"..f.. | ||
69 | "' (number expected, got "..t..")", 0) | ||
70 | elseif x > maxint or x < minint or x % 1 ~= 0 then | ||
71 | _error("bad argument #"..i.." to '"..f.. | ||
72 | "' (number has no integer representation)", 0) | ||
73 | else | ||
74 | return x | ||
75 | end | ||
76 | end | ||
77 | |||
78 | function math.ult(m, n) | ||
79 | m = checkinteger(m, "1", "math.ult") | ||
80 | n = checkinteger(n, "2", "math.ult") | ||
81 | if m >= 0 and n < 0 then | ||
82 | return true | ||
83 | elseif m < 0 and n >= 0 then | ||
84 | return false | ||
85 | else | ||
86 | return m < n | ||
87 | end | ||
88 | end | ||
89 | end | ||
90 | |||
23 | end | 91 | end |
24 | 92 | ||
93 | -- vi: set expandtab softtabstop=3 shiftwidth=3 : | ||
diff --git a/tests/test.lua b/tests/test.lua index cc07ee8..ac415bc 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -1,63 +1,106 @@ | |||
1 | #!/usr/bin/env lua | 1 | #!/usr/bin/env lua |
2 | 2 | ||
3 | local mod = require( "testmod" ) | 3 | local F, ___ |
4 | local F | ||
5 | do | 4 | do |
6 | local type, unpack = type, table.unpack or unpack | 5 | local type, unpack = type, table.unpack or unpack |
7 | function F( ... ) | 6 | function F(...) |
8 | local args, n = { ... }, select( '#', ... ) | 7 | local args, n = { ... }, select('#', ...) |
9 | for i = 1, n do | 8 | for i = 1, n do |
10 | local t = type( args[ i ] ) | 9 | local t = type(args[i]) |
11 | if t ~= "string" and t ~= "number" and t ~= "boolean" then | 10 | if t ~= "string" and t ~= "number" and t ~= "boolean" then |
12 | args[ i ] = t | 11 | args[i] = t |
13 | end | 12 | end |
14 | end | 13 | end |
15 | return unpack( args, 1, n ) | 14 | return unpack(args, 1, n) |
15 | end | ||
16 | local sep = ("="):rep(70) | ||
17 | function ___() | ||
18 | print(sep) | ||
16 | end | 19 | end |
17 | end | 20 | end |
18 | 21 | ||
22 | print( "testing Lua API ..." ) | ||
23 | package.path = "../?.lua;"..package.path | ||
24 | require("compat53") | ||
25 | ___'' | ||
26 | print("math.maxinteger", math.maxinteger+1 > math.maxinteger) | ||
27 | print("math.mininteger", math.mininteger-1 < math.mininteger) | ||
28 | |||
29 | |||
30 | ___'' | ||
31 | print("math.tointeger", math.tointeger(0)) | ||
32 | print("math.tointeger", math.tointeger(math.pi)) | ||
33 | print("math.tointeger", math.tointeger("hello")) | ||
34 | print("math.tointeger", math.tointeger(math.maxinteger+2.0)) | ||
35 | print("math.tointeger", math.tointeger(math.mininteger*2.0)) | ||
36 | |||
37 | |||
38 | ___'' | ||
39 | print("math.type", math.type(0)) | ||
40 | print("math.type", math.type(math.pi)) | ||
41 | print("math.type", math.type("hello")) | ||
42 | |||
43 | |||
44 | ___'' | ||
45 | print("math.ult", math.ult(1, 2), math.ult(2, 1)) | ||
46 | print("math.ult", math.ult(-1, 2), math.ult(2, -1)) | ||
47 | print("math.ult", math.ult(-1, -2), math.ult(-2, -1)) | ||
48 | print("math.ult", pcall(math.ult, "x", 2)) | ||
49 | print("math.ult", pcall(math.ult, 1, 2.1)) | ||
50 | ___'' | ||
51 | |||
52 | |||
19 | 53 | ||
20 | print( mod.isinteger( 1 ) ) | 54 | print("testing C API ...") |
21 | print( mod.isinteger( 0 ) ) | 55 | local mod = require("testmod") |
22 | print( mod.isinteger( 1234567 ) ) | 56 | ___'' |
23 | print( mod.isinteger( 12.3 ) ) | 57 | print(mod.isinteger(1)) |
24 | print( mod.isinteger( math.huge ) ) | 58 | print(mod.isinteger(0)) |
25 | print( mod.isinteger( math.sqrt( -1 ) ) ) | 59 | print(mod.isinteger(1234567)) |
60 | print(mod.isinteger(12.3)) | ||
61 | print(mod.isinteger(math.huge)) | ||
62 | print(mod.isinteger(math.sqrt(-1))) | ||
26 | 63 | ||
27 | 64 | ||
28 | print( mod.rotate( 1, 1, 2, 3, 4, 5, 6 ) ) | 65 | ___'' |
29 | print( mod.rotate(-1, 1, 2, 3, 4, 5, 6 ) ) | 66 | print(mod.rotate(1, 1, 2, 3, 4, 5, 6)) |
30 | print( mod.rotate( 4, 1, 2, 3, 4, 5, 6 ) ) | 67 | print(mod.rotate(-1, 1, 2, 3, 4, 5, 6)) |
31 | print( mod.rotate( -4, 1, 2, 3, 4, 5, 6 ) ) | 68 | print(mod.rotate(4, 1, 2, 3, 4, 5, 6)) |
69 | print(mod.rotate(-4, 1, 2, 3, 4, 5, 6)) | ||
32 | 70 | ||
33 | 71 | ||
34 | print( mod.strtonum( "+123" ) ) | 72 | ___'' |
35 | print( mod.strtonum( " 123 " ) ) | 73 | print(mod.strtonum("+123")) |
36 | print( mod.strtonum( "-1.23" ) ) | 74 | print(mod.strtonum(" 123 ")) |
37 | print( mod.strtonum( " 123 abc" ) ) | 75 | print(mod.strtonum("-1.23")) |
38 | print( mod.strtonum( "jkl" ) ) | 76 | print(mod.strtonum(" 123 abc")) |
77 | print(mod.strtonum("jkl")) | ||
39 | 78 | ||
40 | 79 | ||
80 | ___'' | ||
41 | local a, b, c = mod.requiref() | 81 | local a, b, c = mod.requiref() |
42 | print( type( a ), type( b ), type( c ), | 82 | print( type(a), type(b), type(c), |
43 | a.boolean, b.boolean, c.boolean, | 83 | a.boolean, b.boolean, c.boolean, |
44 | type( requiref1 ), type( requiref2 ), type( requiref3 ) ) | 84 | type(requiref1), type(requiref2), type(requiref3)) |
45 | 85 | ||
86 | ___'' | ||
46 | local proxy, backend = {}, {} | 87 | local proxy, backend = {}, {} |
47 | setmetatable( proxy, { __index = backend, __newindex = backend } ) | 88 | setmetatable(proxy, { __index = backend, __newindex = backend }) |
48 | print( rawget( proxy, 1 ), rawget( backend, 1 ) ) | 89 | print(rawget(proxy, 1), rawget(backend, 1)) |
49 | print( mod.getseti( proxy, 1 ) ) | 90 | print(mod.getseti(proxy, 1)) |
50 | print( rawget( proxy, 1 ), rawget( backend, 1 ) ) | 91 | print(rawget(proxy, 1), rawget(backend, 1)) |
51 | print( mod.getseti( proxy, 1 ) ) | 92 | print(mod.getseti(proxy, 1)) |
52 | print( rawget( proxy, 1 ), rawget( backend, 1 ) ) | 93 | print(rawget(proxy, 1), rawget(backend, 1)) |
53 | 94 | ||
54 | -- tests for Lua 5.1 | 95 | -- tests for Lua 5.1 |
96 | ___'' | ||
55 | print(mod.tonumber(12)) | 97 | print(mod.tonumber(12)) |
56 | print(mod.tonumber("12")) | 98 | print(mod.tonumber("12")) |
57 | print(mod.tonumber("0")) | 99 | print(mod.tonumber("0")) |
58 | print(mod.tonumber(false)) | 100 | print(mod.tonumber(false)) |
59 | print(mod.tonumber("error")) | 101 | print(mod.tonumber("error")) |
60 | 102 | ||
103 | ___'' | ||
61 | print(mod.tointeger(12)) | 104 | print(mod.tointeger(12)) |
62 | print(mod.tointeger("12")) | 105 | print(mod.tointeger("12")) |
63 | print(mod.tointeger("0")) | 106 | print(mod.tointeger("0")) |
@@ -67,6 +110,7 @@ print( "bbb" ) | |||
67 | print(mod.tointeger(false)) | 110 | print(mod.tointeger(false)) |
68 | print(mod.tointeger("error")) | 111 | print(mod.tointeger("error")) |
69 | 112 | ||
113 | ___'' | ||
70 | print(mod.len("123")) | 114 | print(mod.len("123")) |
71 | print(mod.len({ 1, 2, 3})) | 115 | print(mod.len({ 1, 2, 3})) |
72 | print(pcall(mod.len, true)) | 116 | print(pcall(mod.len, true)) |
@@ -76,34 +120,44 @@ print(mod.len(ud)) | |||
76 | meta.__len = function() return true end | 120 | meta.__len = function() return true end |
77 | print(pcall(mod.len, ud)) | 121 | print(pcall(mod.len, ud)) |
78 | 122 | ||
123 | ___'' | ||
79 | print(mod.copy(true, "string", {}, 1)) | 124 | print(mod.copy(true, "string", {}, 1)) |
80 | 125 | ||
126 | ___'' | ||
81 | print(mod.rawxetp()) | 127 | print(mod.rawxetp()) |
82 | print(mod.rawxetp("I'm back")) | 128 | print(mod.rawxetp("I'm back")) |
83 | 129 | ||
130 | ___'' | ||
84 | print(F(mod.globals()), mod.globals() == _G) | 131 | print(F(mod.globals()), mod.globals() == _G) |
85 | 132 | ||
133 | ___'' | ||
86 | local t = {} | 134 | local t = {} |
87 | print(F(mod.subtable(t))) | 135 | print(F(mod.subtable(t))) |
88 | local x, msg = mod.subtable(t) | 136 | local x, msg = mod.subtable(t) |
89 | print(F(x, msg, x == t.xxx)) | 137 | print(F(x, msg, x == t.xxx)) |
90 | 138 | ||
139 | ___'' | ||
91 | print(F(mod.udata())) | 140 | print(F(mod.udata())) |
92 | print(mod.udata("nosuchtype")) | 141 | print(mod.udata("nosuchtype")) |
93 | 142 | ||
143 | ___'' | ||
94 | print(F(mod.uservalue())) | 144 | print(F(mod.uservalue())) |
95 | 145 | ||
146 | ___'' | ||
96 | print(mod.getupvalues()) | 147 | print(mod.getupvalues()) |
97 | 148 | ||
149 | ___'' | ||
98 | print(mod.absindex("hi", true)) | 150 | print(mod.absindex("hi", true)) |
99 | 151 | ||
152 | ___'' | ||
100 | print(mod.tolstring("string")) | 153 | print(mod.tolstring("string")) |
101 | local t = setmetatable({}, { | 154 | local t = setmetatable({}, { |
102 | __tostring = function(v) return "mytable" end | 155 | __tostring = function(v) return "mytable" end |
103 | }) | 156 | }) |
104 | print(mod.tolstring( t ) ) | 157 | print(mod.tolstring(t)) |
105 | local t = setmetatable({}, { | 158 | local t = setmetatable({}, { |
106 | __tostring = function(v) return nil end | 159 | __tostring = function(v) return nil end |
107 | }) | 160 | }) |
108 | print(pcall(mod.tolstring, t)) | 161 | print(pcall(mod.tolstring, t)) |
162 | ___'' | ||
109 | 163 | ||