summaryrefslogtreecommitdiff
path: root/src/lbaselib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-22 08:23:37 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-22 08:23:37 +0100
commitdbe2a3bf2ef8c053e6c596c99e29eb27b6118f1b (patch)
tree4920ab827f35d0305a951fde5d627cc8c767b4fe /src/lbaselib.js
parentc1824a99035a231172f3c10ae3ee24a3e6330260 (diff)
downloadfengari-dbe2a3bf2ef8c053e6c596c99e29eb27b6118f1b.tar.gz
fengari-dbe2a3bf2ef8c053e6c596c99e29eb27b6118f1b.tar.bz2
fengari-dbe2a3bf2ef8c053e6c596c99e29eb27b6118f1b.zip
ipairs
Diffstat (limited to 'src/lbaselib.js')
-rw-r--r--src/lbaselib.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/lbaselib.js b/src/lbaselib.js
index b3dcd12..d0e094f 100644
--- a/src/lbaselib.js
+++ b/src/lbaselib.js
@@ -89,6 +89,44 @@ const luaB_type = function(L) {
return 1;
};
+const pairsmeta = function(L, method, iszero, iter) {
+ lauxlib.luaL_checkany(L, 1);
+ if (lauxlib.luaL_getmetafield(L, 1, method).ttisnil()) { /* no metamethod? */
+ lapi.lua_pushcfunction(L, iter); /* will return generator, */
+ lapi.lua_pushvalue(L, 1); /* state, */
+ if (iszero) lapi.lua_pushinteger(L, 0); /* and initial value */
+ else lapi.lua_pushnil(L);
+ } else {
+ lapi.lua_pushvalue(L, 1); /* argument 'self' to metamethod */
+ lapi.lua_call(L, 1, 3); /* get 3 values from metamethod */
+ }
+ return 3;
+};
+
+/*
+** Traversal function for 'ipairs'
+*/
+const ipairsaux = function(L) {
+ let i = lauxlib.luaL_checkinteger(L, 2) + 1;
+ lapi.lua_pushinteger(L, i);
+ return lapi.lua_geti(L, 1, i) === CT.LUA_TNIL ? 1 : 2;
+};
+
+/*
+** 'ipairs' function. Returns 'ipairsaux', given "table", 0.
+** (The given "table" may not be a table.)
+*/
+const luaB_ipairs = function(L) {
+ // Lua 5.2
+ // return pairsmeta(L, "__ipairs", 1, ipairsaux);
+
+ lauxlib.luaL_checkany(L, 1);
+ lapi.lua_pushcfunction(L, ipairsaux); /* iteration function */
+ lapi.lua_pushvalue(L, 1); /* state */
+ lapi.lua_pushinteger(L, 0); /* initial value */
+ return 3;
+};
+
const luaB_error = function(L) {
let level = lauxlib.luaL_optinteger(L, 2, 1);
lapi.lua_settop(L, 1);
@@ -144,6 +182,7 @@ const base_funcs = {
"print": luaB_print,
"tostring": luaB_tostring,
"getmetatable": luaB_getmetatable,
+ "ipairs": luaB_ipairs,
"setmetatable": luaB_setmetatable,
"rawequal": luaB_rawequal,
"rawset": luaB_rawset,