1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#!/usr/bin/env node
"use strict";
const lua = require('../../src/lua.js');
const lapi = require('../../src/lapi.js');
const lauxlib = require('../../src/lauxlib.js');
const linit = require('../../src/linit.js');
const fs = require('fs');
const readlineSync = require('readline-sync');
readlineSync.setDefaultOptions({
prompt: '> '
});
const stdin = lua.to_luastring("=stdin");
const L = lauxlib.luaL_newstate();
linit.luaL_openlibs(L);
console.log(lua.FENGARI_COPYRIGHT);
for (;;) {
let input = readlineSync.prompt();
if (input.length === 0)
continue;
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;
}
}
while (status === lua.thread_status.LUA_ERRSYNTAX && lapi.lua_tojsstring(L, -1).endsWith("<eof>")) {
/* continuation */
lapi.lua_pop(L, 1);
input += "\n" + readlineSync.prompt({
prompt: '>> '
});
let buffer = lua.to_luastring(input);
status = lauxlib.luaL_loadbuffer(L, buffer, buffer.length, stdin);
}
if (status !== lua.thread_status.LUA_OK) {
lauxlib.lua_writestringerror(`${lapi.lua_tojsstring(L, -1)}\n`);
lapi.lua_settop(L, 0);
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 */
}
|