From e0d4ffcc75a04b3ecc2cc08aea372d9621e5b6ac Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 20 Feb 2017 11:26:28 +0100 Subject: rawequal --- README.md | 7 ++++--- src/lapi.js | 11 +++++++++-- src/lbaselib.js | 22 +++++++++++++--------- tests/lbaselib.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 19306fe..1a4a4e3 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ - [x] lua_setfield - [x] lua_settop - [x] lua_tostring + - [x] lua_rawequal - [ ] lua_arith - [ ] lua_close - [ ] lua_compare @@ -110,7 +111,6 @@ - [ ] lua_pushlightuserdata - [ ] lua_pushthread - [ ] lua_pushvfstring - - [ ] lua_rawequal - [ ] lua_rawgetp - [ ] lua_rawlen - [ ] lua_rawset @@ -149,8 +149,8 @@ - [x] luaL_checkstack - [x] luaL_tolstring - [x] luaL_openlibs - - [ ] luaL_getsubtable - - [ ] luaL_requiref + - [x] luaL_getsubtable + - [x] luaL_requiref - [ ] luaL_Buffer - [ ] luaL_Reg - [ ] luaL_Stream @@ -206,6 +206,7 @@ - [x] print - [x] getmetatable - [x] setmetatable + - [x] rawequal - [ ] ... - [ ] Debug (errors) - [ ] DOM API binding diff --git a/src/lapi.js b/src/lapi.js index e1e548d..95eb6f7 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -443,7 +443,13 @@ const lua_istable = function(L, idx) { const lua_isstring = function(L, idx) { let o = index2addr(L, idx); return o.ttisstring() || o.ttisnumber(); -} +}; + +const lua_rawequal = function(L, index1, index2) { + let o1 = index2addr(L, index1); + let o2 = index2addr(L, index2); + return lvm.luaV_equalobj(null, o1, o2); // TODO: isvalid ? +}; /* ** 'load' and 'call' functions (run Lua code) @@ -591,4 +597,5 @@ module.exports.lua_getfield = lua_getfield; module.exports.lua_getglobal = lua_getglobal; module.exports.lua_getmetatable = lua_getmetatable; module.exports.lua_setmetatable = lua_setmetatable; -module.exports.lua_settop = lua_settop; \ No newline at end of file +module.exports.lua_settop = lua_settop; +module.exports.lua_rawequal = lua_rawequal; \ No newline at end of file diff --git a/src/lbaselib.js b/src/lbaselib.js index 12c28fe..62d25df 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -33,17 +33,17 @@ const luaB_tostring = function(L) { lauxlib.luaL_checkany(L, 1); lauxlib.luaL_tolstring(L, 1, null); - return true; + return 1; }; const luaB_getmetatable = function(L) { lauxlib.luaL_checkany(L, 1); if (!lapi.lua_getmetatable(L, 1)) { lapi.lua_pushnil(L); - return true; /* no metatable */ + return 1; /* no metatable */ } lauxlib.luaL_getmetafield(L, 1, "__metatable"); - return true; /* returns either __metatable field (if present) or metatable */ + return 1; /* returns either __metatable field (if present) or metatable */ }; const luaB_setmetatable = function(L) { @@ -54,7 +54,14 @@ const luaB_setmetatable = function(L) { throw new Error("cannot change a protected metatable"); lapi.lua_settop(L, 2); lapi.lua_setmetatable(L, 1); - return true; + return 1; +}; + +const luaB_rawequal = function(L) { + lauxlib.luaL_checkany(L, 1); + lauxlib.luaL_checkany(L, 2); + lapi.lua_pushboolean(L, lapi.lua_rawequal(L, 1, 2)); + return 1; }; const base_funcs = { @@ -62,6 +69,7 @@ const base_funcs = { "tostring": luaB_tostring, "getmetatable": luaB_getmetatable, "setmetatable": luaB_setmetatable, + "rawequal": luaB_rawequal, }; const luaopen_base = function(L) { @@ -74,11 +82,7 @@ const luaopen_base = function(L) { /* set global _VERSION */ lapi.lua_pushliteral(L, lua.LUA_VERSION); lapi.lua_setfield(L, -2, "_VERSION"); - return true; + return 1; }; -module.exports.luaB_tostring = luaB_tostring; -module.exports.luaB_print = luaB_print; -module.exports.luaB_getmetatable = luaB_getmetatable; -module.exports.luaB_setmetatable = luaB_setmetatable; module.exports.luaopen_base = luaopen_base; diff --git a/tests/lbaselib.js b/tests/lbaselib.js index 3ddec2d..ab68a5f 100644 --- a/tests/lbaselib.js +++ b/tests/lbaselib.js @@ -82,4 +82,48 @@ test('setmetatable, getmetatable', function (t) { lapi.lua_istable(L, -1), "Correct element(s) on the stack" ); +}); + + +test('rawequal', function (t) { + let luaCode = ` + local mt = { + __eq = function () + return true + end + } + + local t1 = {} + local t2 = {} + + setmetatable(t1, mt); + + return rawequal(t1, t2), t1 == t2 + `, L; + + t.plan(3); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-rawequal"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + t.notOk( + lapi.lua_toboolean(L, -2), + "Correct element(s) on the stack" + ); + + t.ok( + lapi.lua_toboolean(L, -1), + "Correct element(s) on the stack" + ); }); \ No newline at end of file -- cgit v1.2.3-70-g09d2