aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2015-01-16 17:07:02 +0100
committerPhilipp Janda <siffiejoe@gmx.net>2015-01-16 17:07:02 +0100
commit6124b7b5e687eff120507dccfe98b72b7ddb702d (patch)
tree8f84aecb8afcc4116bda5fff0c44cb3c7fa9daee
parent1fd73399caa9a1ea24bd626e607980ac94debd64 (diff)
downloadlua-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.md9
-rw-r--r--compat53.lua77
-rwxr-xr-xtests/test.lua116
3 files changed, 165 insertions, 37 deletions
diff --git a/README.md b/README.md
index d870827..7099702 100644
--- a/README.md
+++ b/README.md
@@ -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)
3if lua_version ~= "5.3" then 3if 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
23end 91end
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
3local mod = require( "testmod" ) 3local F, ___
4local F
5do 4do
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
17end 20end
18 21
22print( "testing Lua API ..." )
23package.path = "../?.lua;"..package.path
24require("compat53")
25___''
26print("math.maxinteger", math.maxinteger+1 > math.maxinteger)
27print("math.mininteger", math.mininteger-1 < math.mininteger)
28
29
30___''
31print("math.tointeger", math.tointeger(0))
32print("math.tointeger", math.tointeger(math.pi))
33print("math.tointeger", math.tointeger("hello"))
34print("math.tointeger", math.tointeger(math.maxinteger+2.0))
35print("math.tointeger", math.tointeger(math.mininteger*2.0))
36
37
38___''
39print("math.type", math.type(0))
40print("math.type", math.type(math.pi))
41print("math.type", math.type("hello"))
42
43
44___''
45print("math.ult", math.ult(1, 2), math.ult(2, 1))
46print("math.ult", math.ult(-1, 2), math.ult(2, -1))
47print("math.ult", math.ult(-1, -2), math.ult(-2, -1))
48print("math.ult", pcall(math.ult, "x", 2))
49print("math.ult", pcall(math.ult, 1, 2.1))
50___''
51
52
19 53
20print( mod.isinteger( 1 ) ) 54print("testing C API ...")
21print( mod.isinteger( 0 ) ) 55local mod = require("testmod")
22print( mod.isinteger( 1234567 ) ) 56___''
23print( mod.isinteger( 12.3 ) ) 57print(mod.isinteger(1))
24print( mod.isinteger( math.huge ) ) 58print(mod.isinteger(0))
25print( mod.isinteger( math.sqrt( -1 ) ) ) 59print(mod.isinteger(1234567))
60print(mod.isinteger(12.3))
61print(mod.isinteger(math.huge))
62print(mod.isinteger(math.sqrt(-1)))
26 63
27 64
28print( mod.rotate( 1, 1, 2, 3, 4, 5, 6 ) ) 65___''
29print( mod.rotate(-1, 1, 2, 3, 4, 5, 6 ) ) 66print(mod.rotate(1, 1, 2, 3, 4, 5, 6))
30print( mod.rotate( 4, 1, 2, 3, 4, 5, 6 ) ) 67print(mod.rotate(-1, 1, 2, 3, 4, 5, 6))
31print( mod.rotate( -4, 1, 2, 3, 4, 5, 6 ) ) 68print(mod.rotate(4, 1, 2, 3, 4, 5, 6))
69print(mod.rotate(-4, 1, 2, 3, 4, 5, 6))
32 70
33 71
34print( mod.strtonum( "+123" ) ) 72___''
35print( mod.strtonum( " 123 " ) ) 73print(mod.strtonum("+123"))
36print( mod.strtonum( "-1.23" ) ) 74print(mod.strtonum(" 123 "))
37print( mod.strtonum( " 123 abc" ) ) 75print(mod.strtonum("-1.23"))
38print( mod.strtonum( "jkl" ) ) 76print(mod.strtonum(" 123 abc"))
77print(mod.strtonum("jkl"))
39 78
40 79
80___''
41local a, b, c = mod.requiref() 81local a, b, c = mod.requiref()
42print( type( a ), type( b ), type( c ), 82print( 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___''
46local proxy, backend = {}, {} 87local proxy, backend = {}, {}
47setmetatable( proxy, { __index = backend, __newindex = backend } ) 88setmetatable(proxy, { __index = backend, __newindex = backend })
48print( rawget( proxy, 1 ), rawget( backend, 1 ) ) 89print(rawget(proxy, 1), rawget(backend, 1))
49print( mod.getseti( proxy, 1 ) ) 90print(mod.getseti(proxy, 1))
50print( rawget( proxy, 1 ), rawget( backend, 1 ) ) 91print(rawget(proxy, 1), rawget(backend, 1))
51print( mod.getseti( proxy, 1 ) ) 92print(mod.getseti(proxy, 1))
52print( rawget( proxy, 1 ), rawget( backend, 1 ) ) 93print(rawget(proxy, 1), rawget(backend, 1))
53 94
54-- tests for Lua 5.1 95-- tests for Lua 5.1
96___''
55print(mod.tonumber(12)) 97print(mod.tonumber(12))
56print(mod.tonumber("12")) 98print(mod.tonumber("12"))
57print(mod.tonumber("0")) 99print(mod.tonumber("0"))
58print(mod.tonumber(false)) 100print(mod.tonumber(false))
59print(mod.tonumber("error")) 101print(mod.tonumber("error"))
60 102
103___''
61print(mod.tointeger(12)) 104print(mod.tointeger(12))
62print(mod.tointeger("12")) 105print(mod.tointeger("12"))
63print(mod.tointeger("0")) 106print(mod.tointeger("0"))
@@ -67,6 +110,7 @@ print( "bbb" )
67print(mod.tointeger(false)) 110print(mod.tointeger(false))
68print(mod.tointeger("error")) 111print(mod.tointeger("error"))
69 112
113___''
70print(mod.len("123")) 114print(mod.len("123"))
71print(mod.len({ 1, 2, 3})) 115print(mod.len({ 1, 2, 3}))
72print(pcall(mod.len, true)) 116print(pcall(mod.len, true))
@@ -76,34 +120,44 @@ print(mod.len(ud))
76meta.__len = function() return true end 120meta.__len = function() return true end
77print(pcall(mod.len, ud)) 121print(pcall(mod.len, ud))
78 122
123___''
79print(mod.copy(true, "string", {}, 1)) 124print(mod.copy(true, "string", {}, 1))
80 125
126___''
81print(mod.rawxetp()) 127print(mod.rawxetp())
82print(mod.rawxetp("I'm back")) 128print(mod.rawxetp("I'm back"))
83 129
130___''
84print(F(mod.globals()), mod.globals() == _G) 131print(F(mod.globals()), mod.globals() == _G)
85 132
133___''
86local t = {} 134local t = {}
87print(F(mod.subtable(t))) 135print(F(mod.subtable(t)))
88local x, msg = mod.subtable(t) 136local x, msg = mod.subtable(t)
89print(F(x, msg, x == t.xxx)) 137print(F(x, msg, x == t.xxx))
90 138
139___''
91print(F(mod.udata())) 140print(F(mod.udata()))
92print(mod.udata("nosuchtype")) 141print(mod.udata("nosuchtype"))
93 142
143___''
94print(F(mod.uservalue())) 144print(F(mod.uservalue()))
95 145
146___''
96print(mod.getupvalues()) 147print(mod.getupvalues())
97 148
149___''
98print(mod.absindex("hi", true)) 150print(mod.absindex("hi", true))
99 151
152___''
100print(mod.tolstring("string")) 153print(mod.tolstring("string"))
101local t = setmetatable({}, { 154local t = setmetatable({}, {
102 __tostring = function(v) return "mytable" end 155 __tostring = function(v) return "mytable" end
103}) 156})
104print(mod.tolstring( t ) ) 157print(mod.tolstring(t))
105local t = setmetatable({}, { 158local t = setmetatable({}, {
106 __tostring = function(v) return nil end 159 __tostring = function(v) return nil end
107}) 160})
108print(pcall(mod.tolstring, t)) 161print(pcall(mod.tolstring, t))
162___''
109 163