diff options
author | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-02-22 09:00:12 +0100 |
---|---|---|
committer | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-02-22 09:00:12 +0100 |
commit | a40719dc74b8dbb0d6c8fc0272c0d79ad8a7a9ea (patch) | |
tree | a87cbfa7d3e7620b64681f3f738320c58be0f469 /src | |
parent | dbe2a3bf2ef8c053e6c596c99e29eb27b6118f1b (diff) | |
download | fengari-a40719dc74b8dbb0d6c8fc0272c0d79ad8a7a9ea.tar.gz fengari-a40719dc74b8dbb0d6c8fc0272c0d79ad8a7a9ea.tar.bz2 fengari-a40719dc74b8dbb0d6c8fc0272c0d79ad8a7a9ea.zip |
select
Diffstat (limited to 'src')
-rw-r--r-- | src/lapi.js | 17 | ||||
-rw-r--r-- | src/lauxlib.js | 2 | ||||
-rw-r--r-- | src/lbaselib.js | 15 |
3 files changed, 33 insertions, 1 deletions
diff --git a/src/lapi.js b/src/lapi.js index e149619..bc4dd54 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -472,6 +472,22 @@ const lua_tonumber = function(L, idx) { return lvm.tonumber(index2addr(L, idx)) }; +const lua_topointer = function(L, idx) { + let o = index2addr(L, idx); + switch (o.ttype()) { + case CT.LUA_TTABLE: + case CT.LUA_TLCL: + case CT.LUA_TCCL: + case CT.LUA_TLCF: + case CT.LUA_TTHREAD: + case CT.LUA_TUSERDATA: + case CT.LUA_TLIGHTUSERDATA: + return o.value; + default: + return null; + } +}; + const f_call = function(L, ud) { ldo.luaD_callnoyield(L, ud.funcOff, ud.nresults); }; @@ -653,6 +669,7 @@ module.exports.lua_tointeger = lua_tointeger; module.exports.lua_toboolean = lua_toboolean; module.exports.lua_tolstring = lua_tolstring; module.exports.lua_tostring = lua_tostring; +module.exports.lua_topointer = lua_topointer; module.exports.lua_load = lua_load; module.exports.lua_callk = lua_callk; module.exports.lua_call = lua_call; diff --git a/src/lauxlib.js b/src/lauxlib.js index 3ebcc7d..c44780e 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -92,7 +92,7 @@ const luaL_typename = function(L, i) { }; const luaL_argcheck = function(L, cond, arg, extramsg) { - if (!cond) throw new Error("bad argument"); // TODO: luaL_argerror + if (!cond) throw new Error(extramsg); // TODO: luaL_argerror }; const luaL_checkany = function(L, arg) { diff --git a/src/lbaselib.js b/src/lbaselib.js index d0e094f..19a990e 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -138,6 +138,20 @@ const luaB_error = function(L) { return lapi.lua_error(L); }; +const luaB_select = function(L) { + let n = lapi.lua_gettop(L); + if (lapi.lua_type(L, 1) === CT.LUA_TSTRING && lapi.lua_tostring(L, 1) === "#") { + lapi.lua_pushinteger(L, n - 1); + return 1; + } else { + let i = lauxlib.luaL_checkinteger(L, 1); + if (i < 0) i = n + i; + else if (i > n) i = n; + lauxlib.luaL_argcheck(L, 1 <= i, 1, "index out of range"); + return n - i; + } +}; + /* ** Continuation function for 'pcall' and 'xpcall'. Both functions ** already pushed a 'true' before doing the call, so in case of success @@ -183,6 +197,7 @@ const base_funcs = { "tostring": luaB_tostring, "getmetatable": luaB_getmetatable, "ipairs": luaB_ipairs, + "select": luaB_select, "setmetatable": luaB_setmetatable, "rawequal": luaB_rawequal, "rawset": luaB_rawset, |