From e8a12210e0a5a0d86b5bacfd10e673b36d56fb38 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 23 Feb 2017 15:14:11 +0100 Subject: coroutine.wrap --- README.md | 34 +++------------------------------- src/lcorolib.js | 2 +- tests/lcorolib.js | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 50c4d7e..714c482 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,7 @@ - [ ] float - [ ] userdata - [ ] thread -- [ ] Tag Methods - - [x] ... - - [ ] `__tostring` - - [ ] `__pairs` +- [x] Tag Methods - [ ] C API - [x] lua_absindex - [x] lua_atpanic @@ -201,36 +198,11 @@ - [ ] luaL_unref - [ ] Standard library - [ ] Base lib - - [x] assert - - [x] collectgarbage (unavailable) - - [x] error - - [x] getmetatable - - [x] ipairs - - [x] next - - [x] pairs - - [x] pcall - - [x] print - - [x] rawequal - - [x] rawget - - [x] rawlen - - [x] rawset - - [x] select - - [x] setmetatable - - [x] tonumber - - [x] tostring - - [x] type - - [x] xpcall + - [x] ... - [ ] dofile - [ ] loadfile - [ ] load - - [ ] Coroutine - - [x] coroutine.create - - [x] coroutine.isyieldable - - [x] coroutine.resume - - [x] coroutine.running - - [x] coroutine.status - - [x] coroutine.yield - - [ ] coroutine.wrap + - [x] Coroutine - [ ] Debug (errors) - [ ] DOM API binding - [ ] Parse Lua diff --git a/src/lcorolib.js b/src/lcorolib.js index 32532a6..05f3b00 100644 --- a/src/lcorolib.js +++ b/src/lcorolib.js @@ -62,7 +62,7 @@ const luaB_coresume = function(L) { }; const luaB_auxwrap = function(L) { - let co = lapi.lua_tothread(L, lapi.lua_upvalueindex(1)); + let co = lapi.lua_tothread(L, lua.lua_upvalueindex(1)); let r = auxresume(L, co, lapi.lua_gettop(L)); if (r < 0) { if (lapi.lua_type(L, -1) === CT.LUA_TSTRING) { /* error object is a string? */ diff --git a/tests/lcorolib.js b/tests/lcorolib.js index dfecd71..78024dd 100644 --- a/tests/lcorolib.js +++ b/tests/lcorolib.js @@ -181,4 +181,41 @@ test('coroutine.running', function (t) { lapi.lua_toboolean(L, -1), "Correct element(s) on the stack" ); +}); + + +test('coroutine.wrap', function (t) { + let luaCode = ` + local co = coroutine.wrap(function (start) + local b = coroutine.yield(start * start); + coroutine.yield(b * b) + end) + + pow = co(5) + pow = co(pow) + + return pow + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + let bc = toByteCode(luaCode).dataView; + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lapi.lua_load(L, bc, "test-coroutine.wrap"); + + lapi.lua_call(L, 0, -1); + + }, "JS Lua program ran without error"); + + t.strictEqual( + lapi.lua_tonumber(L, -1), + 625, + "Correct element(s) on the stack" + ); }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf