aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2015-01-17 02:17:25 +0100
committerPhilipp Janda <siffiejoe@gmx.net>2015-01-17 02:17:25 +0100
commit64783408a4a108812f22268c12f71c75f5399d81 (patch)
tree2134d0dbc1287538220d677c682d43264cb29d85
parent6124b7b5e687eff120507dccfe98b72b7ddb702d (diff)
downloadlua-compat-5.3-64783408a4a108812f22268c12f71c75f5399d81.tar.gz
lua-compat-5.3-64783408a4a108812f22268c12f71c75f5399d81.tar.bz2
lua-compat-5.3-64783408a4a108812f22268c12f71c75f5399d81.zip
add ipairs function that respects __index
-rw-r--r--README.md3
-rw-r--r--compat53.lua28
-rwxr-xr-xtests/test.lua10
3 files changed, 38 insertions, 3 deletions
diff --git a/README.md b/README.md
index 7099702..5adaa38 100644
--- a/README.md
+++ b/README.md
@@ -65,6 +65,7 @@ your project:
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`, 66* `math.maxinteger` and `math.mininteger`, `math.tointeger`, `math.type`,
67 and `math.ult` 67 and `math.ult`
68* `ipairs` respects `__index` metamethod
68 69
69### C 70### C
70 71
@@ -113,7 +114,7 @@ For Lua 5.1 additionally:
113* integer division operator 114* integer division operator
114* `table.move` 115* `table.move`
115* `coroutine.isyieldable` 116* `coroutine.isyieldable`
116* `ipairs` and the `table` library don't respect metamethods yet 117* `table` library doesn't respect metamethods yet
117* Lua 5.1: `_ENV`, `goto`, labels, ephemeron tables, etc. See 118* Lua 5.1: `_ENV`, `goto`, labels, ephemeron tables, etc. See
118 [`lua-compat-5.2`][2] for a detailed list. 119 [`lua-compat-5.2`][2] for a detailed list.
119* the following C API functions/macros: 120* the following C API functions/macros:
diff --git a/compat53.lua b/compat53.lua
index 0346cb7..8bf6bd8 100644
--- a/compat53.lua
+++ b/compat53.lua
@@ -1,6 +1,11 @@
1local lua_version = _VERSION:sub(-3) 1local lua_version = _VERSION:sub(-3)
2 2
3if lua_version ~= "5.3" then 3if lua_version ~= "5.3" then
4 local _type = type
5 -- select the most powerful getmetatable function available
6 local gmt = _type(debug) == "table" and debug.getmetatable or
7 getmetatable or function() return false end
8 local checkinteger -- forward declararation
4 9
5 -- load utf8 library 10 -- load utf8 library
6 local ok, utf8lib = pcall(require, "compat53.utf8") 11 local ok, utf8lib = pcall(require, "compat53.utf8")
@@ -41,7 +46,6 @@ if lua_version ~= "5.3" then
41 math.maxinteger = maxint 46 math.maxinteger = maxint
42 math.mininteger = minint 47 math.mininteger = minint
43 48
44 local _type = type
45 function math.tointeger(n) 49 function math.tointeger(n)
46 if _type(n) == "number" and n <= maxint and n >= minint and n % 1 == 0 then 50 if _type(n) == "number" and n <= maxint and n >= minint and n % 1 == 0 then
47 return n 51 return n
@@ -62,7 +66,7 @@ if lua_version ~= "5.3" then
62 end 66 end
63 67
64 local _error = error 68 local _error = error
65 local function checkinteger(x, i, f) 69 function checkinteger(x, i, f)
66 local t = _type(x) 70 local t = _type(x)
67 if t ~= "number" then 71 if t ~= "number" then
68 _error("bad argument #"..i.." to '"..f.. 72 _error("bad argument #"..i.." to '"..f..
@@ -88,6 +92,26 @@ if lua_version ~= "5.3" then
88 end 92 end
89 end 93 end
90 94
95
96 -- ipairs should respect __index metamethod
97 do
98 local _ipairs = ipairs
99 local function ipairs_iterator(st, var)
100 var = var + 1
101 local val = st[var]
102 if val ~= nil then
103 return var, st[var]
104 end
105 end
106 function ipairs(t)
107 if gmt(t) ~= nil then -- t has metatable
108 return ipairs_iterator, t, 0
109 else
110 return _ipairs(t)
111 end
112 end
113 end
114
91end 115end
92 116
93-- vi: set expandtab softtabstop=3 shiftwidth=3 : 117-- vi: set expandtab softtabstop=3 shiftwidth=3 :
diff --git a/tests/test.lua b/tests/test.lua
index ac415bc..ab25820 100755
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -22,6 +22,16 @@ end
22print( "testing Lua API ..." ) 22print( "testing Lua API ..." )
23package.path = "../?.lua;"..package.path 23package.path = "../?.lua;"..package.path
24require("compat53") 24require("compat53")
25
26___''
27do
28 local t = setmetatable( {}, { __index = { 1, false, "three" } } )
29 for i,v in ipairs(t) do
30 print("ipairs", i, v)
31 end
32end
33
34
25___'' 35___''
26print("math.maxinteger", math.maxinteger+1 > math.maxinteger) 36print("math.maxinteger", math.maxinteger+1 > math.maxinteger)
27print("math.mininteger", math.mininteger-1 < math.mininteger) 37print("math.mininteger", math.mininteger-1 < math.mininteger)