From 3d667b50989e31d18af0c235e28b68055adf62ff Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 14 Apr 2017 11:48:23 +0200 Subject: debug.gethook --- README.md | 91 +++++++++++++++++++++++++++------------------------------ src/ldblib.js | 25 +++++++++++++++- src/ldebug.js | 17 +++++++++++ tests/ldblib.js | 56 +++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 01fcbde..73708f7 100644 --- a/README.md +++ b/README.md @@ -5,40 +5,33 @@

# fengari -🐺 φεγγάρι - A Lua VM written in JS ES6 targeting the browser +🐺 φεγγάρι - The Lua VM written in JS ES6 targeting the browser ## So far - [x] Lexing/Parsing - [x] Parse bytecode - [x] Opcodes -- [ ] Basic types representation: - - [x] nil - - [x] boolean - - [x] table - - [ ] weak table - - [x] function - - [x] string (8-bit clean) - - [x] number (32-bit) - - [x] userdata +- [x] Basic types representation: - [x] Tag Methods +- [ ] Standard library + - [x] Base lib + - [x] Coroutine + - [x] Debug + - [x] Math + - [x] String + - [x] Table + - [x] utf8 + - [ ] Package + - [ ] os + - [ ] io - [ ] C API - [x] ... - [ ] lua_arith - [ ] lua_close - - [ ] lua_gethook - - [ ] lua_gethookcount - - [ ] lua_gethookmask - - [ ] lua_geti - - [ ] lua_getinfo - - [ ] lua_getstack - - [ ] lua_getupvalue - [ ] lua_isboolean - [ ] lua_islightuserdata - - [ ] lua_isuserdata - - [ ] lua_pcallk - [ ] lua_pushfstring - - [ ] lua_pushlightuserdata - [ ] lua_pushvfstring - [ ] lua_rawseti - [ ] lua_register @@ -59,41 +52,43 @@ - [ ] luaL_newlibtable - [ ] luaL_newmetatable - [ ] luaL_optnumber - - [ ] luaL_optstring - [ ] luaL_prepbuffer - [ ] luaL_pushresultsize - [ ] luaL_ref - [ ] luaL_setmetatable - [ ] luaL_testudata - [ ] luaL_unref -- [ ] Standard library - - [x] Base lib - - [x] Coroutine - - [x] Table - - [x] Math - - [x] utf8 - - [x] String - - [ ] Package - - [ ] os - - [ ] io - - [ ] Debug - - [x] debug.debug - - [x] debug.getinfo - - [x] debug.getlocal - - [x] debug.getmetatable - - [x] debug.getregistry - - [x] debug.getupvalue - - [x] debug.getuservalue - - [x] debug.sethook - - [x] debug.setlocal - - [x] debug.setmetatable - - [x] debug.setupvalue - - [x] debug.setuservalue - - [x] debug.traceback - - [x] debug.upvalueid - - [x] debug.upvaluejoin - - [ ] debug.gethook - [ ] Run [Lua test suite](https://github.com/lua/tests) + - [x] strings.lua + - [ ] all.lua + - [ ] big.lua + - [ ] checktable.lua + - [ ] constructs.lua + - [ ] errors.lua + - [ ] gc.lua + - [ ] literals.lua + - [ ] math.lua + - [ ] sort.lua + - [ ] utf8.lua + - [ ] api.lua + - [ ] bitwise.lua + - [ ] closure.lua + - [ ] coroutine.lua + - [ ] events.lua + - [ ] goto.lua + - [ ] locals.lua + - [ ] nextvar.lua + - [ ] vararg.lua + - [ ] attrib.lua + - [ ] calls.lua + - [ ] code.lua + - [ ] db.lua + - [ ] files.lua + - [ ] heavy.lua + - [ ] main.lua + - [ ] pm.lua + - [ ] tpack.lua + - [ ] verybig.lua - [ ] DOM API binding ## References diff --git a/src/ldblib.js b/src/ldblib.js index 81f27fb..5c78724 100644 --- a/src/ldblib.js +++ b/src/ldblib.js @@ -308,7 +308,6 @@ const unmakemask = function(mask, smask) { if (mask & lua.LUA_MASKCALL) smask[i++] = char["c"]; if (mask & lua.LUA_MASKRET) smask[i++] = char["r"]; if (mask & lua.LUA_MASKLINE) smask[i++] = char["l"]; - smask[i] = 0; return smask; }; @@ -344,6 +343,29 @@ const db_sethook = function(L) { return 0; }; +const db_gethook = function(L) { + let thread = getthread(L); + let L1 = thread.thread; + let arg = thread.arg; + let buff = []; + let mask = ldebug.lua_gethookmask(L1); + let hook = ldebug.lua_gethook(L1); + if (hook === null) /* no hook? */ + lapi.lua_pushnil(L); + else if (hook !== hookf) /* external hook? */ + lapi.lua_pushliteral(L, "external hook"); + else { /* hook table must exist */ + lapi.lua_rawgetp(L, lua.LUA_REGISTRYINDEX, HOOKKEY); + checkstack(L, L1, 1); + lapi.lua_pushthread(L1); lapi.lua_xmove(L1, L, 1); + lapi.lua_rawget(L, -2); /* 1st result = hooktable[L1] */ + lapi.lua_remove(L, -2); /* remove hook table */ + } + lapi.lua_pushstring(L, unmakemask(mask, buff)); /* 2nd result = mask */ + lapi.lua_pushinteger(L, ldebug.lua_gethookcount(L1)); /* 3rd result = count */ + return 3; +}; + const db_traceback = function(L) { let thread = getthread(L); let L1 = thread.thread; @@ -359,6 +381,7 @@ const db_traceback = function(L) { }; const dblib = { + "gethook": db_gethook, "getinfo": db_getinfo, "getlocal": db_getlocal, "getmetatable": db_getmetatable, diff --git a/src/ldebug.js b/src/ldebug.js index a73b7f7..0a0e22c 100644 --- a/src/ldebug.js +++ b/src/ldebug.js @@ -51,6 +51,20 @@ const lua_sethook = function(L, func, mask, count) { L.hookmask = mask; }; +const lua_gethook = function(L) { + return L.hook; +}; + + +const lua_gethookmask = function(L) { + return L.hookmask; +}; + + +const lua_gethookcount = function(L) { + return L.basehookcount; +}; + const lua_getstack = function(L, level, ar) { let ci; let status; @@ -633,6 +647,9 @@ module.exports.luaG_runerror = luaG_runerror; module.exports.luaG_tointerror = luaG_tointerror; module.exports.luaG_traceexec = luaG_traceexec; module.exports.luaG_typeerror = luaG_typeerror; +module.exports.lua_gethook = lua_gethook; +module.exports.lua_gethookcount = lua_gethookcount; +module.exports.lua_gethookmask = lua_gethookmask; module.exports.lua_getinfo = lua_getinfo; module.exports.lua_getlocal = lua_getlocal; module.exports.lua_getstack = lua_getstack; diff --git a/tests/ldblib.js b/tests/ldblib.js index 8c3ad7f..6587228 100644 --- a/tests/ldblib.js +++ b/tests/ldblib.js @@ -51,6 +51,62 @@ test('debug.sethook', function (t) { }); +test('debug.gethook', function (t) { + let luaCode = ` + local result = "" + + debug.sethook(function (event) + result = result .. event .. " " + end, "crl", 1) + + local l = function() end + + l() + l() + l() + + return debug.gethook() + `, L; + + t.plan(5); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + t.deepEqual( + lapi.lua_typename(L, lapi.lua_type(L, -3)), + lua.to_luastring("function"), + "Correct element(s) on the stack" + ); + + t.deepEqual( + lapi.lua_tojsstring(L, -2), + "crl", + "Correct element(s) on the stack" + ); + + t.deepEqual( + lapi.lua_tointeger(L, -1), + 1, + "Correct element(s) on the stack" + ); + +}); + + test('debug.getlocal', function (t) { let luaCode = ` local alocal = "alocal" -- cgit v1.2.3-54-g00ecf