From 99564913987698cdfc2528a93b5157030e637ced Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 23 Mar 2017 08:33:41 +0100 Subject: dofile --- README.md | 12 +++--------- src/lbaselib.js | 14 ++++++++++++++ src/ldebug.js | 9 ++++++++- tests/ldebug.js | 14 +++++++------- tests/load.js | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 0cc0b09..38f0abe 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,8 @@ - [ ] weak table - [x] function - [x] string (8-bit clean) - - [ ] number (32-bit ?) - - [ ] integer - - [ ] float - - [ ] userdata + - [x] number (32-bit) + - [x] userdata - [x] Tag Methods - [ ] C API - [x] ... @@ -82,11 +80,7 @@ - [ ] luaL_traceback - [ ] luaL_unref - [ ] Standard library - - [ ] Base lib - - [x] ... - - [x] load - - [x] loadfile - - [ ] dofile + - [x] Base lib - [x] Coroutine - [x] Table - [x] Math diff --git a/src/lbaselib.js b/src/lbaselib.js index 604a005..83b3c33 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -358,7 +358,21 @@ if (typeof window === "undefined") { return load_aux(L, status, env); }; + const dofilecont = function(L, d1, d2) { + return lapi.lua_gettop(L) - 1; + }; + + const luaB_dofile = function(L) { + let fname = lauxlib.luaL_optstring(L, 1, null); + lapi.lua_settop(L, 1); + if (lauxlib.luaL_loadfile(L, fname) !== TS.LUA_OK) + return lapi.lua_error(L); + lapi.lua_callk(L, 0, lua.LUA_MULTRET, 0, dofilecont); + return dofilecont(L, 0, 0); + }; + base_funcs.loadfile = luaB_loadfile; + base_funcs.dofile = luaB_dofile; } diff --git a/src/ldebug.js b/src/ldebug.js index 529cb0c..3b01263 100644 --- a/src/ldebug.js +++ b/src/ldebug.js @@ -431,7 +431,14 @@ const varinfo = function(L, o) { kind = getobjname(ci.func.p, ci.pcOff, stkid - ci.u.l.base); } - return kind ? ` (${kind.funcname} '${kind.name.jsstring()}')` : ``; + if (kind) { + let funcname = kind.funcname instanceof TValue ? kind.funcname.jsstring() : kind.funcname; + let name = kind.name instanceof TValue ? kind.name.jsstring() : kind.name; + + return ` (${funcname} '${name}')`; + } + + return ``; }; const luaG_typeerror = function(L, o, op) { diff --git a/tests/ldebug.js b/tests/ldebug.js index 472c66b..0261305 100644 --- a/tests/ldebug.js +++ b/tests/ldebug.js @@ -40,7 +40,7 @@ test('luaG_typeerror', function (t) { t.ok( lapi.lua_tostring(L, -1).endsWith("attempt to get length of a boolean value (local 'a')"), "Correct error was thrown" - ) + ); }); @@ -69,7 +69,7 @@ test('luaG_typeerror', function (t) { t.ok( lapi.lua_tostring(L, -1).endsWith("attempt to index a boolean value (local 'a')"), "Correct error was thrown" - ) + ); }); @@ -98,7 +98,7 @@ test('luaG_typeerror', function (t) { t.ok( lapi.lua_tostring(L, -1).endsWith("attempt to index a boolean value (local 'a')"), "Correct error was thrown" - ) + ); }); @@ -127,7 +127,7 @@ test('luaG_typeerror', function (t) { t.ok( lapi.lua_tostring(L, -1).endsWith("attempt to index a boolean value (local 'a')"), "Correct error was thrown" - ) + ); }); @@ -155,7 +155,7 @@ test('luaG_concaterror', function (t) { t.ok( lapi.lua_tostring(L, -1).endsWith("attempt to concatenate a table value"), "Correct error was thrown" - ) + ); }); @@ -183,7 +183,7 @@ test('luaG_opinterror', function (t) { t.ok( lapi.lua_tostring(L, -1).endsWith("attempt to perform arithmetic on a string value"), "Correct error was thrown" - ) + ); }); @@ -211,5 +211,5 @@ test('luaG_tointerror', function (t) { t.ok( lapi.lua_tostring(L, -1).endsWith("number has no integer representation"), "Correct error was thrown" - ) + ); }); \ No newline at end of file diff --git a/tests/load.js b/tests/load.js index c7ea3f9..ef37309 100644 --- a/tests/load.js +++ b/tests/load.js @@ -147,4 +147,36 @@ test('loadfile', function (t) { "Correct element(s) on the stack" ); +}); + + +test('dofile', function (t) { + let luaCode = ` + return dofile("tests/loadfile-test.lua") + `, L; + + t.plan(3); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, luaCode); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.strictEqual( + lapi.lua_tostring(L, -1), + "hello world", + "Correct element(s) on the stack" + ); + }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf