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 /src/ldblib.js | |
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
Diffstat (limited to 'src/ldblib.js')
-rw-r--r-- | src/ldblib.js | 55 |
1 files changed, 55 insertions, 0 deletions
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; |