diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-04-11 10:06:09 +0200 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-04-11 10:45:32 +0200 |
commit | 4d374d77766b6f621f2194a9546521295aa528af (patch) | |
tree | 0391532304b34c87e36a85a5ea13b24d1d42fe11 | |
parent | bb51e9d6e41efe08f6f053599bf31245ab116b77 (diff) | |
download | fengari-4d374d77766b6f621f2194a9546521295aa528af.tar.gz fengari-4d374d77766b6f621f2194a9546521295aa528af.tar.bz2 fengari-4d374d77766b6f621f2194a9546521295aa528af.zip |
debug.debug
Use readline-sync to read from stdin interactively
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | src/lauxlib.js | 82 | ||||
-rw-r--r-- | src/lbaselib.js | 4 | ||||
-rw-r--r-- | src/ldblib.js | 55 | ||||
-rw-r--r-- | src/linit.js | 2 | ||||
-rw-r--r-- | src/lualib.js | 2 | ||||
-rw-r--r-- | tests/debug-cli.js | 19 |
7 files changed, 125 insertions, 40 deletions
diff --git a/package.json b/package.json index d9527ab..6fc5ffe 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "tmp": "0.0.31" }, "dependencies": { + "readline-sync": "^1.4.7", "seedrandom": "^2.4.2", "sprintf-js": "giann/sprintf.js" } diff --git a/src/lauxlib.js b/src/lauxlib.js index 5a13379..03ca210 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -545,41 +545,47 @@ if (typeof require === "function") { } } -module.exports.LUA_LOADED_TABLE = LUA_LOADED_TABLE; -module.exports.luaL_Buffer = luaL_Buffer; -module.exports.luaL_addchar = luaL_addchar; -module.exports.luaL_addlstring = luaL_addlstring; -module.exports.luaL_addstring = luaL_addstring; -module.exports.luaL_addvalue = luaL_addvalue; -module.exports.luaL_argcheck = luaL_argcheck; -module.exports.luaL_argerror = luaL_argerror; -module.exports.luaL_buffinit = luaL_buffinit; -module.exports.luaL_buffinitsize = luaL_buffinitsize; -module.exports.luaL_callmeta = luaL_callmeta; -module.exports.luaL_checkany = luaL_checkany; -module.exports.luaL_checkinteger = luaL_checkinteger; -module.exports.luaL_checklstring = luaL_checklstring; -module.exports.luaL_checknumber = luaL_checknumber; -module.exports.luaL_checkstack = luaL_checkstack; -module.exports.luaL_checkstring = luaL_checkstring; -module.exports.luaL_checktype = luaL_checktype; -module.exports.luaL_error = luaL_error; -module.exports.luaL_getmetafield = luaL_getmetafield; -module.exports.luaL_getsubtable = luaL_getsubtable; -module.exports.luaL_len = luaL_len; -module.exports.luaL_loadbuffer = luaL_loadbuffer; -module.exports.luaL_loadbufferx = luaL_loadbufferx; -module.exports.luaL_loadstring = luaL_loadstring; -module.exports.luaL_newlib = luaL_newlib; -module.exports.luaL_newstate = luaL_newstate; -module.exports.luaL_opt = luaL_opt; -module.exports.luaL_optinteger = luaL_optinteger; -module.exports.luaL_optlstring = luaL_optlstring; -module.exports.luaL_optstring = luaL_optstring; -module.exports.luaL_prepbuffsize = luaL_prepbuffsize; -module.exports.luaL_pushresult = luaL_pushresult; -module.exports.luaL_requiref = luaL_requiref; -module.exports.luaL_setfuncs = luaL_setfuncs; -module.exports.luaL_tolstring = luaL_tolstring; -module.exports.luaL_typename = luaL_typename; -module.exports.luaL_where = luaL_where; +const lua_writestringerror = function(s) { + if (process.stderr) process.stderr.write(s); + else console.error(s); +}; + +module.exports.LUA_LOADED_TABLE = LUA_LOADED_TABLE; +module.exports.luaL_Buffer = luaL_Buffer; +module.exports.luaL_addchar = luaL_addchar; +module.exports.luaL_addlstring = luaL_addlstring; +module.exports.luaL_addstring = luaL_addstring; +module.exports.luaL_addvalue = luaL_addvalue; +module.exports.luaL_argcheck = luaL_argcheck; +module.exports.luaL_argerror = luaL_argerror; +module.exports.luaL_buffinit = luaL_buffinit; +module.exports.luaL_buffinitsize = luaL_buffinitsize; +module.exports.luaL_callmeta = luaL_callmeta; +module.exports.luaL_checkany = luaL_checkany; +module.exports.luaL_checkinteger = luaL_checkinteger; +module.exports.luaL_checklstring = luaL_checklstring; +module.exports.luaL_checknumber = luaL_checknumber; +module.exports.luaL_checkstack = luaL_checkstack; +module.exports.luaL_checkstring = luaL_checkstring; +module.exports.luaL_checktype = luaL_checktype; +module.exports.luaL_error = luaL_error; +module.exports.luaL_getmetafield = luaL_getmetafield; +module.exports.luaL_getsubtable = luaL_getsubtable; +module.exports.luaL_len = luaL_len; +module.exports.luaL_loadbuffer = luaL_loadbuffer; +module.exports.luaL_loadbufferx = luaL_loadbufferx; +module.exports.luaL_loadstring = luaL_loadstring; +module.exports.luaL_newlib = luaL_newlib; +module.exports.luaL_newstate = luaL_newstate; +module.exports.luaL_opt = luaL_opt; +module.exports.luaL_optinteger = luaL_optinteger; +module.exports.luaL_optlstring = luaL_optlstring; +module.exports.luaL_optstring = luaL_optstring; +module.exports.luaL_prepbuffsize = luaL_prepbuffsize; +module.exports.luaL_pushresult = luaL_pushresult; +module.exports.luaL_requiref = luaL_requiref; +module.exports.luaL_setfuncs = luaL_setfuncs; +module.exports.luaL_tolstring = luaL_tolstring; +module.exports.luaL_typename = luaL_typename; +module.exports.luaL_where = luaL_where; +module.exports.lua_writestringerror = lua_writestringerror; diff --git a/src/lbaselib.js b/src/lbaselib.js index 4d0fb9c..931a285 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -26,7 +26,9 @@ const luaB_print = function(L) { lapi.lua_pop(L, 1); } - console.log(lobject.jsstring(str)); + // Don't use console.log if Node + if (process.stdout) process.stdout.write(lobject.jsstring(str) + "\n"); + else console.log(lobject.jsstring(str)); return 0; }; diff --git a/src/ldblib.js b/src/ldblib.js new file mode 100644 index 0000000..39b9574 --- /dev/null +++ b/src/ldblib.js @@ -0,0 +1,55 @@ +"use strict"; + +const assert = require('assert'); + +const lua = require('./lua.js'); +const lapi = require('./lapi.js'); +const lauxlib = require('./lauxlib.js'); + + +const dblib = { +}; + +// Only with Node +if (typeof require === "function") { + let fs = false; + try { + fs = require('fs'); + } catch (e) {} + + if (fs) { + const readlineSync = require('readline-sync'); + readlineSync.setDefaultOptions({ + prompt: 'lua_debug> ' + }); + + // TODO: if in browser, use a designated input in the DOM ? + const db_debug = function(L) { + for (;;) { + let input = readlineSync.prompt(); + + if (input === "cont") + return 0; + + if (input.length === 0) + continue; + + let buffer = lua.to_luastring(input); + if (lauxlib.luaL_loadbuffer(L, buffer, buffer.length, lua.to_luastring("=(debug command)")) + || lapi.lua_pcall(L, 0, 0, 0)) { + lauxlib.lua_writestringerror(`${lapi.lua_tojsstring(L, -1)}\n`); + } + lapi.lua_settop(L, 0); /* remove eventual returns */ + } + }; + + dblib.debug = db_debug; + } +} + +const luaopen_debug = function(L) { + lauxlib.luaL_newlib(L, dblib); + return 1; +}; + +module.exports.luaopen_debug = luaopen_debug; diff --git a/src/linit.js b/src/linit.js index 3e9b1af..9425ae7 100644 --- a/src/linit.js +++ b/src/linit.js @@ -11,6 +11,7 @@ const lmathlib = require('./lmathlib.js'); const lstrlib = require('./lstrlib.js'); const ltablib = require('./ltablib.js'); const lutf8lib = require('./lutf8lib.js'); +const ldblib = require('./ldblib.js'); const lualib = require('./lualib.js'); const loadedlibs = { @@ -19,6 +20,7 @@ const loadedlibs = { [lualib.LUA_STRLIBNAME]: lstrlib.luaopen_string, [lualib.LUA_TABLIBNAME]: ltablib.luaopen_table, [lualib.LUA_UTF8LIBNAME]: lutf8lib.luaopen_utf8, + [lualib.LUA_DBLIBNAME]: ldblib.luaopen_debug, "_G": lbaselib.luaopen_base }; diff --git a/src/lualib.js b/src/lualib.js index 83b1d0d..9751302 100644 --- a/src/lualib.js +++ b/src/lualib.js @@ -40,7 +40,7 @@ module.exports[LUA_MATHLIBNAME] = require("./lmathlib.js").luaopen_math; const LUA_DBLIBNAME = "debug"; module.exports.LUA_DBLIBNAME = LUA_DBLIBNAME; -module.exports[LUA_DBLIBNAME] = require("./ldebug.js").luaopen_debug; +module.exports[LUA_DBLIBNAME] = require("./ldblib.js").luaopen_debug; const LUA_LOADLIBNAME = "package"; module.exports.LUA_LOADLIBNAME = LUA_LOADLIBNAME; diff --git a/tests/debug-cli.js b/tests/debug-cli.js new file mode 100644 index 0000000..aa6847f --- /dev/null +++ b/tests/debug-cli.js @@ -0,0 +1,19 @@ +"use strict"; + +const lapi = require("../src/lapi.js"); +const lauxlib = require("../src/lauxlib.js"); +const lua = require('../src/lua.js'); +const linit = require('../src/linit.js'); + +let luaCode = ` + a = "debug me" + debug.debug() +`, L; + +L = lauxlib.luaL_newstate(); + +linit.luaL_openlibs(L); + +lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + +lapi.lua_call(L, 0, -1); |