forked from daurnimator/lua.vm.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua.js
More file actions
60 lines (54 loc) · 1.49 KB
/
lua.js
File metadata and controls
60 lines (54 loc) · 1.49 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
var Lua = {
// public
init: function() {
// Load Lua-side glue TODO: embed the file here, avoid the xhr
var xhr = new XMLHttpRequest();
xhr.open('get', 'js.lua', false);
xhr.overrideMimeType('text/plain');
xhr.send(null);
var jsLua = xhr.response;
Lua.execute(jsLua);
// Run script tags on page
var onload = window.onload;
window.onload = function() {
if (onload) onload();
Lua.executeScripts();
};
},
execute: function(code) {
Module.ccall('lua_execute', null, ['string'], [code]);
},
// internal
executeScripts: function() {
Array.prototype.forEach.call(document.querySelectorAll('script[type=\"text\/lua\"]'), function(tag) {
Lua.execute(tag.innerHTML)
});
},
// internal glue layer
theGlobal: this,
wrappers: {},
reverseWrappers: {},
last: null,
test: function(what, idx) {
Lua.last = eval(what);
if (this.reverseWrappers[Lua.last]) {
return -this.reverseWrappers[Lua.last];
}
this.reverseWrappers[Lua.last] = idx;
switch (typeof Lua.last) {
case 'number': return 1;
case 'string': return 2;
case 'object': return 3;
case 'function': return 4;
default: return 0;
}
},
funcWrapper: function(i) {
var realthis = this;
return function() {
if (realthis.reverseWrappers[this]) Lua.execute('js.lua_table[' + i + '](js.storeGet('+realthis.reverseWrappers[this]+'))');
else Lua.execute('js.lua_table[' + i + ']()');
};
}
};
Lua.init();