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 /compat53.lua | |
| 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
Diffstat (limited to 'compat53.lua')
| -rw-r--r-- | compat53.lua | 28 |
1 files changed, 26 insertions, 2 deletions
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 : |
