diff options
author | daurnimator <quae@daurnimator.com> | 2017-08-17 00:31:52 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-08-17 01:18:07 +1000 |
commit | 0186ad3e5be4cbf7738a4cc2f5bb7679f16e5800 (patch) | |
tree | 0d449011fa41fc9532e1f72c15a8e3cc32bebba4 | |
parent | 7b8f0563309b866a4e36a20e17d9a01bb4236b79 (diff) | |
download | fengari-0186ad3e5be4cbf7738a4cc2f5bb7679f16e5800.tar.gz fengari-0186ad3e5be4cbf7738a4cc2f5bb7679f16e5800.tar.bz2 fengari-0186ad3e5be4cbf7738a4cc2f5bb7679f16e5800.zip |
src/lbaselib.js: Print immediately to console in node
-rw-r--r-- | src/lbaselib.js | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/src/lbaselib.js b/src/lbaselib.js index 6947f94..eb12280 100644 --- a/src/lbaselib.js +++ b/src/lbaselib.js @@ -3,10 +3,27 @@ const lua = require('./lua.js'); const lauxlib = require('./lauxlib.js'); +let lua_writestring; +let lua_writeline; +if (WEB) { + let buff = []; + lua_writestring = function(s) { + buff = buff.concat(s); + }; + lua_writeline = function() { + console.log(lua.to_jsstring(buff)); + buff = []; + }; +} else { + lua_writestring = function(s) { + process.stdout.write(Buffer.from(s)); + }; + lua_writeline = function() { + process.stdout.write("\n"); + }; +} const luaB_print = function(L) { let n = lua.lua_gettop(L); /* number of arguments */ - let str = []; - lua.lua_getglobal(L, lua.to_luastring("tostring", true)); for (let i = 1; i <= n; i++) { lua.lua_pushvalue(L, -1); /* function to be called */ @@ -15,16 +32,11 @@ const luaB_print = function(L) { let s = lua.lua_tolstring(L, -1); if (s === null) return lauxlib.luaL_error(L, lua.to_luastring("'tostring' must return a string to 'print'", true)); - if (i > 1) str.push("\t".charCodeAt(0)); - str = str.concat(s); + if (i > 1) lua_writestring(["\t".charCodeAt(0)]); + lua_writestring(s); lua.lua_pop(L, 1); } - - // Don't use console.log if Node - if (process.stdout) { - str.push("\n".charCodeAt(0)); - process.stdout.write(Buffer.from(str)); - } else console.log(lua.to_jsstring(str)); + lua_writeline(); return 0; }; |