From 3f6f88a1948454562b4416b2f4d398dedac725cc Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 23 May 2017 11:30:45 +0200 Subject: ltests.js: resume, coroutine.js: yields inside hooks --- tests/test-suite/inprogress/coroutine.js | 55 ++++++++++++++++++++++++++++++-- tests/test-suite/ltests.js | 20 +++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) (limited to 'tests/test-suite') diff --git a/tests/test-suite/inprogress/coroutine.js b/tests/test-suite/inprogress/coroutine.js index 3161bf0..602e050 100644 --- a/tests/test-suite/inprogress/coroutine.js +++ b/tests/test-suite/inprogress/coroutine.js @@ -8,6 +8,7 @@ const lua = require('../../../src/lua.js'); const lauxlib = require('../../../src/lauxlib.js'); const lualib = require('../../../src/lualib.js'); +const ltests = require('../ltests.js'); const prefix = ` mt = { @@ -656,8 +657,58 @@ test("[test-suite] coroutine: access to locals of erroneous coroutines", functio }); -test("[test-suite] coroutine: JS Tests", { skip: true }, function (t) { - t.comment("TODO"); +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"); }); diff --git a/tests/test-suite/ltests.js b/tests/test-suite/ltests.js index fc81131..dcc8cec 100644 --- a/tests/test-suite/ltests.js +++ b/tests/test-suite/ltests.js @@ -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 = getstring(L, buff, pc); + let inst = lua.to_jsstring(getstring(L, buff, pc).slice(0, -1)); if (inst.length === 0) return 0; else if (inst === "absindex") { lua.lua_pushnumber(1, lua.lua_absindex(1, getindex(L, L1, pc))); @@ -385,6 +385,7 @@ const sethook = function(L) { if (count > 0) mask |= lua.LUA_MASKCOUNT; sethookaux(L, mask, count, scpt); } + return 0; }; const Cfunck = function(L, status, ctx) { @@ -395,8 +396,25 @@ const Cfunck = function(L, status, ctx) { return runJS(L, L, lua.lua_tostring(L, ctx)); }; +const coresume = function(L) { + let status; + let co = lua.lua_tothread(L, 1); + lauxlib.luaL_argcheck(L, co, 1, lua.to_luastring("coroutine expected", true)); + status = lua.lua_resume(co, L, 0); + if (status != lua.LUA_OK && status !== lua.LUA_YIELD) { + lua.lua_pushboolean(L, 0); + lua.lua_insert(L, -2); + return 2; /* return false + error message */ + } + else { + lua.lua_pushboolean(L, 1); + return 1; + } +}; + const tests_funcs = { "newuserdata": newuserdata, + "resume": coresume, "sethook": sethook }; -- cgit v1.2.3-54-g00ecf