From a40719dc74b8dbb0d6c8fc0272c0d79ad8a7a9ea Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 22 Feb 2017 09:00:12 +0100 Subject: select --- src/lbaselib.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/lbaselib.js') 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