From 4d374d77766b6f621f2194a9546521295aa528af Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 11 Apr 2017 10:06:09 +0200 Subject: debug.debug Use readline-sync to read from stdin interactively --- src/ldblib.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/ldblib.js (limited to 'src/ldblib.js') 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; -- cgit v1.2.3-54-g00ecf