diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-04-12 11:47:22 +0200 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-04-12 11:47:22 +0200 |
commit | a6dc1dbf5a439a854581927fb36fdad05d0dff4b (patch) | |
tree | 89f83f41ad52bb791f27279c65ba0dd9e081bd62 /tests | |
parent | eeb3d8ac1de70703ce7db4788b7fb43fcda80aa8 (diff) | |
parent | d2c29700b140c6656bc9cc4c8870f9f340b98945 (diff) | |
download | fengari-a6dc1dbf5a439a854581927fb36fdad05d0dff4b.tar.gz fengari-a6dc1dbf5a439a854581927fb36fdad05d0dff4b.tar.bz2 fengari-a6dc1dbf5a439a854581927fb36fdad05d0dff4b.zip |
Merge remote-tracking branch 'daurnimator/cli'
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/manual-tests/lua-cli.js | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/tests/manual-tests/lua-cli.js b/tests/manual-tests/lua-cli.js index 6c7801c..de64359 100755 --- a/tests/manual-tests/lua-cli.js +++ b/tests/manual-tests/lua-cli.js @@ -12,6 +12,8 @@ readlineSync.setDefaultOptions({ prompt: '> ' }); +const stdin = lua.to_luastring("=stdin"); + const L = lauxlib.luaL_newstate(); linit.luaL_openlibs(L); @@ -24,10 +26,36 @@ for (;;) { if (input.length === 0) continue; - let buffer = lua.to_luastring(input); - if (lauxlib.luaL_loadbuffer(L, buffer, buffer.length, lua.to_luastring("=stdin")) - || lapi.lua_pcall(L, 0, 0, 0)) { + let status; + { + let buffer = lua.to_luastring("return " + input); + status = lauxlib.luaL_loadbuffer(L, buffer, buffer.length, stdin); + } + if (status !== lua.thread_status.LUA_OK) { + lapi.lua_pop(L, 1); + let buffer = lua.to_luastring(input); + if (lauxlib.luaL_loadbuffer(L, buffer, buffer.length, stdin) === lua.thread_status.LUA_OK) { + status = lua.thread_status.LUA_OK; + } + } + if (status !== lua.thread_status.LUA_OK) { + lauxlib.lua_writestringerror(`${lapi.lua_tojsstring(L, -1)}\n`); + continue; + } + if (lapi.lua_pcall(L, 0, lua.LUA_MULTRET, 0) !== lua.thread_status.LUA_OK) { lauxlib.lua_writestringerror(`${lapi.lua_tojsstring(L, -1)}\n`); + lapi.lua_settop(L, 0); + continue; + } + let n = lapi.lua_gettop(L); + if (n > 0) { /* any result to be printed? */ + lapi.lua_getglobal(L, lua.to_luastring("print")); + lapi.lua_insert(L, 1); + if (lapi.lua_pcall(L, n, 0, 0) != lua.thread_status.LUA_OK) { + lauxlib.lua_writestringerror(`error calling 'print' (${lapi.lua_tojsstring(L, -1)})\n`); + lapi.lua_settop(L, 0); + continue; + } } lapi.lua_settop(L, 0); /* remove eventual returns */ } |