diff options
| author | Philipp Janda <siffiejoe@gmx.net> | 2015-01-17 02:17:25 +0100 |
|---|---|---|
| committer | Philipp Janda <siffiejoe@gmx.net> | 2015-01-17 02:17:25 +0100 |
| commit | 64783408a4a108812f22268c12f71c75f5399d81 (patch) | |
| tree | 2134d0dbc1287538220d677c682d43264cb29d85 | |
| parent | 6124b7b5e687eff120507dccfe98b72b7ddb702d (diff) | |
| download | lua-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.md | 3 | ||||
| -rw-r--r-- | compat53.lua | 28 | ||||
| -rwxr-xr-x | tests/test.lua | 10 |
3 files changed, 38 insertions, 3 deletions
| @@ -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 @@ | |||
| 1 | local lua_version = _VERSION:sub(-3) | 1 | local lua_version = _VERSION:sub(-3) |
| 2 | 2 | ||
| 3 | if lua_version ~= "5.3" then | 3 | if 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 | |||
| 91 | end | 115 | end |
| 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 | |||
| 22 | print( "testing Lua API ..." ) | 22 | print( "testing Lua API ..." ) |
| 23 | package.path = "../?.lua;"..package.path | 23 | package.path = "../?.lua;"..package.path |
| 24 | require("compat53") | 24 | require("compat53") |
| 25 | |||
| 26 | ___'' | ||
| 27 | do | ||
| 28 | local t = setmetatable( {}, { __index = { 1, false, "three" } } ) | ||
| 29 | for i,v in ipairs(t) do | ||
| 30 | print("ipairs", i, v) | ||
| 31 | end | ||
| 32 | end | ||
| 33 | |||
| 34 | |||
| 25 | ___'' | 35 | ___'' |
| 26 | print("math.maxinteger", math.maxinteger+1 > math.maxinteger) | 36 | print("math.maxinteger", math.maxinteger+1 > math.maxinteger) |
| 27 | print("math.mininteger", math.mininteger-1 < math.mininteger) | 37 | print("math.mininteger", math.mininteger-1 < math.mininteger) |
