From 66826a03cd3215d1d25856a3d07d9d37be7fe602 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 21 Feb 2017 09:31:15 +0100 Subject: luaG_typeerror --- src/ldebug.js | 7 +++-- tests/ldebug.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/ldebug.js b/src/ldebug.js index ecd612b..3180e40 100644 --- a/src/ldebug.js +++ b/src/ldebug.js @@ -12,6 +12,7 @@ const OC = require('./lopcodes.js'); const lvm = require('./lvm.js'); const ltm = require('./ltm.js'); const lfunc = require('./lfunc.js'); +const lapi = require('./lapi.js'); const TMS = ltm.TMS; const TValue = lobject.TValue; const Table = lobject.Table; @@ -428,7 +429,7 @@ const varinfo = function(L, o) { kind = getupvalname(L, ci, o); /* check whether 'o' is an upvalue */ let stkid = isinstack(L, ci, o); if (!kind && stkid) /* no? try a register */ - kind = getobjname(ci.func.p, ci.pcOff, stkid); + kind = getobjname(ci.func.p, ci.pcOff, stkid - ci.u.l.base); } return kind ? ` (${kind.funcname} '${kind.name}')` : ``; @@ -469,7 +470,9 @@ const luaG_addinfo = function(L, msg, src, line) { if (src) buff = lobject.luaO_chunkid(src, luaconf.LUA_IDSIZE); - return `${buff}:${line}: ${msg}`; + L.stack[L.top++] = new TValue(CT.LUA_TLNGSTR, `${buff}:${line}: ${msg}`); // We don't need to check for overflow here + + return L.stack[L.top - 1]; }; const luaG_runerror = function(L, msg) { diff --git a/tests/ldebug.js b/tests/ldebug.js index a6fa55f..b627cf7 100644 --- a/tests/ldebug.js +++ b/tests/ldebug.js @@ -21,7 +21,7 @@ test('luaG_typeerror', function (t) { return #a `, L; - t.plan(1); + t.plan(2); t.doesNotThrow(function () { @@ -38,5 +38,95 @@ test('luaG_typeerror', function (t) { }, "JS Lua program ran without error"); - console.log(lapi.lua_tostring(L, -1)); + t.ok( + lapi.lua_tostring(L, -1).endsWith("attempt to get length of a boolean value (local 'a')"), + "Correct error was thrown" + ) +}); + + +test('luaG_typeerror', function (t) { + let luaCode = ` + local a = true + return a.yo + `, 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-typeerror"); + + lapi.lua_pcall(L, 0, -1, 0); + + }, "JS Lua program ran without error"); + + t.ok( + lapi.lua_tostring(L, -1).endsWith("attempt to index a boolean value (local 'a')"), + "Correct error was thrown" + ) +}); + + +test('luaG_typeerror', function (t) { + let luaCode = ` + local a = true + return a.yo + `, 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-typeerror"); + + lapi.lua_pcall(L, 0, -1, 0); + + }, "JS Lua program ran without error"); + + t.ok( + lapi.lua_tostring(L, -1).endsWith("attempt to index a boolean value (local 'a')"), + "Correct error was thrown" + ) +}); + + +test('luaG_typeerror', function (t) { + let luaCode = ` + local a = true + a.yo = 1 + `, 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-typeerror"); + + lapi.lua_pcall(L, 0, -1, 0); + + }, "JS Lua program ran without error"); + + t.ok( + lapi.lua_tostring(L, -1).endsWith("attempt to index a boolean value (local 'a')"), + "Correct error was thrown" + ) }); \ No newline at end of file -- cgit v1.2.3-54-g00ecf