-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
69 lines (63 loc) · 1.41 KB
/
main.js
File metadata and controls
69 lines (63 loc) · 1.41 KB
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
"use strict";
const path = require("path");
const {
FENGARI_COPYRIGHT,
to_luastring,
lua: {
LUA_ERRSYNTAX,
LUA_OK,
LUA_REGISTRYINDEX,
lua_pcall,
lua_pop,
lua_pushstring,
lua_setfield,
lua_setglobal,
lua_tojsstring
},
lauxlib: {
LUA_LOADED_TABLE,
luaL_checkstack,
luaL_getsubtable,
luaL_loadfile,
luaL_newstate,
luaL_requiref
},
lualib: {
luaL_openlibs
}
} = require('fengari');
const {
luaopen_js,
push,
tojs
} = require('fengari-interop');
const L = luaL_newstate();
/* open standard libraries */
luaL_openlibs(L);
/* js lib */
luaL_requiref(L, to_luastring("js"), luaopen_js, 0);
lua_pop(L, 1); /* remove lib */
/* register electron lib */
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
push(L, require("electron"));
lua_setfield(L, -2, to_luastring("electron")); /* LOADED[modname] = module */
lua_pop(L, 1); /* remove LOADED */
lua_pushstring(L, to_luastring(FENGARI_COPYRIGHT));
lua_setglobal(L, to_luastring("_COPYRIGHT"));
let ok = luaL_loadfile(L, to_luastring(path.join(__dirname, "main.lua")));
if (ok === LUA_ERRSYNTAX) {
throw new SyntaxError(lua_tojsstring(L, -1));
}
if (ok === LUA_OK) {
/* Push process.argv[2:] */
luaL_checkstack(L, process.argv.length-2, null);
for (let i=2; i<process.argv.length; i++) {
push(L, process.argv[i]);
}
ok = lua_pcall(L, process.argv.length-2, 0, 0);
}
if (ok !== LUA_OK) {
let e = tojs(L, -1);
lua_pop(L, 1);
throw e;
}