From 214d745949e33f869751214c0997bf02f63c88b4 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 23 May 2017 11:53:18 +0200 Subject: ltests.js: fixed getstring --- tests/test-suite/inprogress/coroutine.js | 156 ++++++++++++++++++++----------- tests/test-suite/ltests.js | 4 +- 2 files changed, 103 insertions(+), 57 deletions(-) (limited to 'tests/test-suite') diff --git a/tests/test-suite/inprogress/coroutine.js b/tests/test-suite/inprogress/coroutine.js index 602e050..b026bb6 100644 --- a/tests/test-suite/inprogress/coroutine.js +++ b/tests/test-suite/inprogress/coroutine.js @@ -657,61 +657,6 @@ test("[test-suite] coroutine: access to locals of erroneous coroutines", functio }); -const jsprefix = ` - T = require('T') - - function fact (t, x) - assert(turn == t) - if x == 0 then return 1 - else return x*fact(t, x-1) - end - end -`; - -test("[test-suite] coroutine: testing yields inside hooks", function (t) { - let luaCode = ` - local A, B = 0, 0 - - local x = coroutine.create(function () - T.sethook("yield 0", "", 2) - A = fact("A", 6) - end) - - local y = coroutine.create(function () - T.sethook("yield 0", "", 3) - B = fact("B", 7) - end) - - while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y') - if A==0 then turn = "A"; assert(T.resume(x)) end - if B==0 then turn = "B"; assert(T.resume(y)) end - end - - assert(B // A == 7) -- fact(7) // fact(6) - `, L; - - t.plan(2); - - t.doesNotThrow(function () { - - L = lauxlib.luaL_newstate(); - - lualib.luaL_openlibs(L); - - ltests.luaopen_tests(L); - - lauxlib.luaL_loadstring(L, lua.to_luastring(jsprefix + luaCode)); - - }, "Lua program loaded without error"); - - t.doesNotThrow(function () { - - lua.lua_call(L, 0, -1); - - }, "Lua program ran without error"); -}); - - test("[test-suite] coroutine: leaving a pending coroutine open", function (t) { let luaCode = ` _X = coroutine.wrap(function () @@ -975,6 +920,107 @@ test("[test-suite] coroutine: testing yields inside 'for' iterators", function ( }); +const jsprefix = ` + T = require('T') + + function fact (t, x) + assert(turn == t) + if x == 0 then return 1 + else return x*fact(t, x-1) + end + end +`; + +test("[test-suite] coroutine: testing yields inside hooks", function (t) { + let luaCode = ` + local A, B = 0, 0 + + local x = coroutine.create(function () + T.sethook("yield 0", "", 2) + A = fact("A", 6) + end) + + local y = coroutine.create(function () + T.sethook("yield 0", "", 3) + B = fact("B", 7) + end) + + while A==0 or B==0 do -- A ~= 0 when 'x' finishes (similar for 'B','y') + if A==0 then turn = "A"; assert(T.resume(x)) end + if B==0 then turn = "B"; assert(T.resume(y)) end + end + + assert(B // A == 7) -- fact(7) // fact(6) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lualib.luaL_openlibs(L); + + ltests.luaopen_tests(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(jsprefix + luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); +}); + + +test("[test-suite] coroutine: testing yields inside line hook", function (t) { + let luaCode = ` + local line = debug.getinfo(1, "l").currentline + 2 -- get line number + local function foo () + local x = 10 --<< this line is 'line' + x = x + 10 + _G.XX = x + end + + -- testing yields in line hook + local co = coroutine.wrap(function () + T.sethook("setglobal X; yield 0", "l", 0) + foo() + return 10 + end) + + _G.XX = nil; + _G.X = nil; co(); assert(_G.X == line) + _G.X = nil; co(); assert(_G.X == line + 1) + _G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil) + _G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20) + assert(co() == 10) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lualib.luaL_openlibs(L); + + ltests.luaopen_tests(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(jsprefix + luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); +}); + + test("[test-suite] coroutine: tests for coroutine API", { skip: true }, function (t) { t.comment("TODO"); }); diff --git a/tests/test-suite/ltests.js b/tests/test-suite/ltests.js index dcc8cec..685f530 100644 --- a/tests/test-suite/ltests.js +++ b/tests/test-suite/ltests.js @@ -58,7 +58,7 @@ const getstring = function(L, buff, pc) { while (pc.script[pc.offset] !== 0 && pc.offset < pc.script.length && delimits.indexOf(pc.script[pc.offset]) < 0) buff[i++] = pc.script[pc.offset++]; } - buff[i] = 0; + buff.length = i; return buff; }; @@ -102,7 +102,7 @@ const runJS = function(L, L1, pc) { let status = 0; if (!pc || pc.length === 0) return lauxlib.luaL_error(L, "attempt to runJS empty script"); for (;;) { - let inst = lua.to_jsstring(getstring(L, buff, pc).slice(0, -1)); + let inst = lua.to_jsstring(getstring(L, buff, pc)); if (inst.length === 0) return 0; else if (inst === "absindex") { lua.lua_pushnumber(1, lua.lua_absindex(1, getindex(L, L1, pc))); -- cgit v1.2.3-54-g00ecf