Skip to content

Commit 1f1bcd6

Browse files
committed
Added lua vm and execution
Updated tstl to 0.10.0
1 parent 1c0791d commit 1f1bcd6

5 files changed

Lines changed: 83 additions & 6 deletions

File tree

package-lock.json

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
},
3131
"dependencies": {
3232
"@types/url-parse": "^1.4.1",
33-
"fengari": "^0.1.2",
33+
"fengari-web": "^0.1.2",
3434
"monaco-editor": "^0.14.3",
35-
"typescript-to-lua": "file:../typescript-to-lua-0.9.0.tgz"
35+
"typescript-to-lua": "^0.10.0"
3636
}
3737
}

src/custom.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ interface FSDummy {
1313
interface Window {
1414
fs: FSDummy
1515
}
16+
17+
declare module 'fengari-web';

src/fengariWorker.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
1-
//import {lauxlib, lua, lualib, to_jsstring, to_luastring } from "fengari";
1+
import {lauxlib, lua, lualib, to_jsstring, to_luastring, interop } from "fengari-web";
2+
3+
declare var self: any;
4+
5+
function executeLua(luaStr: string): any {
6+
// clear print buffer and patch lua print
7+
self.printStream = "";
8+
const printToGlobal = `
9+
local js = require "js"
10+
local oldPrint = print
11+
_G.print = function(...)
12+
local elements = table.pack(...)
13+
for i = 1, elements.n do
14+
js.global.printStream = js.global.printStream .. tostring(elements[i]) .. "\\t"
15+
end
16+
js.global.printStream = js.global.printStream .. "\\n"
17+
end
18+
`;
19+
20+
luaStr = printToGlobal + luaStr;
21+
22+
const L = lauxlib.luaL_newstate();
23+
lualib.luaL_openlibs(L);
24+
lauxlib.luaL_requiref(L, to_luastring("js"), interop.luaopen_js, 1);
25+
lua.lua_pop(L, 1);
26+
const status = lauxlib.luaL_dostring(L, to_luastring(luaStr));
27+
28+
if (status === lua.LUA_OK) {
29+
// Read the return value from stack depending on its type.
30+
if (lua.lua_isboolean(L, -1)) {
31+
return lua.lua_toboolean(L, -1);
32+
} else if (lua.lua_isnil(L, -1)) {
33+
return null;
34+
} else if (lua.lua_isnumber(L, -1)) {
35+
return lua.lua_tonumber(L, -1);
36+
} else if (lua.lua_isstring(L, -1)) {
37+
return lua.lua_tojsstring(L, -1);
38+
} else {
39+
throw new Error("Unsupported lua return type: " + to_jsstring(lua.lua_typename(L, lua.lua_type(L, -1))));
40+
}
41+
} else {
42+
// If the lua VM did not terminate with status code LUA_OK an error occurred.
43+
// Throw a JS error with the message, retrieved by reading a string from the stack.
44+
45+
// Filter control characters out of string which are in there because ????
46+
throw new Error("LUA ERROR: " + to_jsstring(lua.lua_tostring(L, -1).filter((c: number) => c >= 20)));
47+
}
48+
}
249

350
onmessage = (event: MessageEvent) => {
4-
//postMessage({luaStr: transpileString(event.data.tsStr)});
51+
executeLua(event.data.luaStr);
52+
postMessage({luaPrint: self.printStream});
553
};

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import {editor} from 'monaco-editor/esm/vs/editor/editor.api';
44

55
import TSTLWorker = require('worker-loader!./tstlWorker');
66

7+
import FengariWorker = require('worker-loader!./fengariWorker');
8+
79
document.addEventListener('DOMContentLoaded', () => {
810
const container = document.getElementById('example-ts');
11+
const output = document.getElementById('example-output');
912
const exampleLua = document.getElementById('example-lua');
1013

1114
let example = `// Declare exposed API
@@ -87,8 +90,17 @@ document.addEventListener('DOMContentLoaded', () => {
8790
}
8891
}
8992

93+
const fengariWorker = new (FengariWorker as any)();
94+
9095
tstlWorker.onmessage = (event: MessageEvent) => {
9196
luaEditor.setValue(event.data.luaStr);
97+
fengariWorker.postMessage({luaStr: event.data.luaStr});
98+
}
99+
100+
fengariWorker.onmessage = (event: MessageEvent) => {
101+
if (output) {
102+
output.innerText = event.data.luaPrint;
103+
}
92104
}
93105
}
94106
});

0 commit comments

Comments
 (0)