From be3f9810115cbaded69be37ebc3d088ee252a225 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 10 May 2017 15:30:33 +0200 Subject: Fixed bad order when trying __lt TM in luaV_lessequal --- src/lvm.js | 4 +- tests/test-suite/events.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/lvm.js b/src/lvm.js index 8ca7b98..0d30484 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -737,9 +737,9 @@ const luaV_lessequal = function(L, l, r) { if (res >= 0) return res ? 1 : 0; } - + /* try 'lt': */ L.ci.callstatus |= lstate.CIST_LEQ; /* mark it is doing 'lt' for 'le' */ - res = ltm.luaT_callorderTM(L, l, r, ltm.TMS.TM_LT); + res = ltm.luaT_callorderTM(L, r, l, ltm.TMS.TM_LT); L.ci.callstatus ^= lstate.CIST_LEQ; /* clear mark */ if (res < 0) ldebug.luaG_ordererror(L, l, r); diff --git a/tests/test-suite/events.js b/tests/test-suite/events.js index 49576ef..3c9a590 100644 --- a/tests/test-suite/events.js +++ b/tests/test-suite/events.js @@ -201,3 +201,98 @@ test("[test-suite] events: testing metatable", function (t) { }, "Lua program ran without error"); }); + + +test("[test-suite] events: test for rawlen", function (t) { + let luaCode = ` + t = setmetatable({1,2,3}, {__len = function () return 10 end}) + assert(#t == 10 and rawlen(t) == 3) + assert(rawlen"abc" == 3) + assert(not pcall(rawlen, io.stdin)) + assert(not pcall(rawlen, 34)) + assert(not pcall(rawlen)) + + -- rawlen for long strings + assert(rawlen(string.rep('a', 1000)) == 1000) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] events: test comparison", function (t) { + let luaCode = ` + t = {} + t.__lt = function (a,b,c) + collectgarbage() + assert(c == nil) + if type(a) == 'table' then a = a.x end + if type(b) == 'table' then b = b.x end + return aOp(1)) and not(Op(1)>Op(2)) and (Op(2)>Op(1))) + assert(not(Op('a')>Op('a')) and not(Op('a')>Op('b')) and (Op('b')>Op('a'))) + assert((Op(1)>=Op(1)) and not(Op(1)>=Op(2)) and (Op(2)>=Op(1))) + assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1)) + assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a'))) + assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a'))) + end + + test() + + t.__le = function (a,b,c) + assert(c == nil) + if type(a) == 'table' then a = a.x end + if type(b) == 'table' then b = b.x end + return a<=b, "dummy" + end + + test() -- retest comparisons, now using both 'lt' and 'le' + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); -- cgit v1.2.3-54-g00ecf