From a40719dc74b8dbb0d6c8fc0272c0d79ad8a7a9ea Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 22 Feb 2017 09:00:12 +0100 Subject: select --- src/lapi.js | 17 +++++++++++++++++ src/lauxlib.js | 2 +- src/lbaselib.js | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) (limited to 'src') 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, -- cgit v1.2.3-54-g00ecf