This repository has been archived by the owner on May 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
luna.js
5414 lines (5409 loc) · 148 KB
/
luna.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function integrateWasmJS(Module) {
var method = Module["wasmJSMethod"] || Module["wasmJSMethod"] || "native-wasm,asmjs" || "native-wasm";
Module["wasmJSMethod"] = method;
var wasmTextFile = Module["wasmTextFile"] || "luna.wast";
var wasmBinaryFile = Module["wasmBinaryFile"] || "luna.wasm";
var asmjsCodeFile = Module["asmjsCodeFile"] || "luna.asm.js";
var wasmPageSize = 64 * 1024;
var asm2wasmImports = {
"f64-rem": (function(x, y) {
return x % y;
}),
"f64-to-int": (function(x) {
return x | 0;
}),
"i32s-div": (function(x, y) {
return (x | 0) / (y | 0) | 0;
}),
"i32u-div": (function(x, y) {
return (x >>> 0) / (y >>> 0) >>> 0;
}),
"i32s-rem": (function(x, y) {
return (x | 0) % (y | 0) | 0;
}),
"i32u-rem": (function(x, y) {
return (x >>> 0) % (y >>> 0) >>> 0;
}),
"debugger": (function() {
debugger;
})
};
var info = {
"global": null,
"env": null,
"asm2wasm": asm2wasmImports,
"parent": Module
};
var exports = null;
function lookupImport(mod, base) {
var lookup = info;
if (mod.indexOf(".") < 0) {
lookup = (lookup || {})[mod];
} else {
var parts = mod.split(".");
lookup = (lookup || {})[parts[0]];
lookup = (lookup || {})[parts[1]];
}
if (base) {
lookup = (lookup || {})[base];
}
if (lookup === undefined) {
abort("bad lookupImport to (" + mod + ")." + base);
}
return lookup;
}
function mergeMemory(newBuffer) {
var oldBuffer = Module["buffer"];
if (newBuffer.byteLength < oldBuffer.byteLength) {
Module["printErr"]("the new buffer in mergeMemory is smaller than the previous one. in native wasm, we should grow memory here");
}
var oldView = new Int8Array(oldBuffer);
var newView = new Int8Array(newBuffer);
if (!memoryInitializer) {
oldView.set(newView.subarray(STATIC_BASE, STATIC_BASE + STATIC_BUMP), STATIC_BASE);
}
newView.set(oldView);
updateGlobalBuffer(newBuffer);
updateGlobalBufferViews();
}
var WasmTypes = {
none: 0,
i32: 1,
i64: 2,
f32: 3,
f64: 4
};
function fixImports(imports) {
if (!0) return imports;
var ret = {};
for (var i in imports) {
var fixed = i;
if (fixed[0] == "_") fixed = fixed.substr(1);
ret[fixed] = imports[i];
}
return ret;
}
function getBinary() {
var binary;
if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
binary = Module["wasmBinary"];
assert(binary, "on the web, we need the wasm binary to be preloaded and set on Module['wasmBinary']. emcc.py will do that for you when generating HTML (but not JS)");
binary = new Uint8Array(binary);
} else {
binary = Module["readBinary"](wasmBinaryFile);
}
return binary;
}
function doJustAsm(global, env, providedBuffer) {
if (typeof Module["asm"] !== "function" || Module["asm"] === methodHandler) {
if (!Module["asmPreload"]) {
eval(Module["read"](asmjsCodeFile));
} else {
Module["asm"] = Module["asmPreload"];
}
}
if (typeof Module["asm"] !== "function") {
Module["printErr"]("asm evalling did not set the module properly");
return false;
}
return Module["asm"](global, env, providedBuffer);
}
function doNativeWasm(global, env, providedBuffer) {
if (typeof WebAssembly !== "object") {
Module["printErr"]("no native wasm support detected");
return false;
}
if (!(Module["wasmMemory"] instanceof WebAssembly.Memory)) {
Module["printErr"]("no native wasm Memory in use");
return false;
}
env["memory"] = Module["wasmMemory"];
info["global"] = {
"NaN": NaN,
"Infinity": Infinity
};
info["global.Math"] = global.Math;
info["env"] = env;
var instance;
try {
instance = new WebAssembly.Instance(new WebAssembly.Module(getBinary()), info);
} catch (e) {
Module["printErr"]("failed to compile wasm module: " + e);
if (e.toString().indexOf("imported Memory with incompatible size") >= 0) {
Module["printErr"]("Memory size incompatibility issues may be due to changing TOTAL_MEMORY at runtime to something too large. Use ALLOW_MEMORY_GROWTH to allow any size memory (and also make sure not to set TOTAL_MEMORY at runtime to something smaller than it was at compile time).");
}
return false;
}
exports = instance.exports;
if (exports.memory) mergeMemory(exports.memory);
Module["usingWasm"] = true;
return exports;
}
function doWasmPolyfill(global, env, providedBuffer, method) {
if (typeof WasmJS !== "function") {
Module["printErr"]("WasmJS not detected - polyfill not bundled?");
return false;
}
var wasmJS = WasmJS({});
wasmJS["outside"] = Module;
wasmJS["info"] = info;
wasmJS["lookupImport"] = lookupImport;
assert(providedBuffer === Module["buffer"]);
info.global = global;
info.env = env;
assert(providedBuffer === Module["buffer"]);
env["memory"] = providedBuffer;
assert(env["memory"] instanceof ArrayBuffer);
wasmJS["providedTotalMemory"] = Module["buffer"].byteLength;
var code;
if (method === "interpret-binary") {
code = getBinary();
} else {
code = Module["read"](method == "interpret-asm2wasm" ? asmjsCodeFile : wasmTextFile);
}
var temp;
if (method == "interpret-asm2wasm") {
temp = wasmJS["_malloc"](code.length + 1);
wasmJS["writeAsciiToMemory"](code, temp);
wasmJS["_load_asm2wasm"](temp);
} else if (method === "interpret-s-expr") {
temp = wasmJS["_malloc"](code.length + 1);
wasmJS["writeAsciiToMemory"](code, temp);
wasmJS["_load_s_expr2wasm"](temp);
} else if (method === "interpret-binary") {
temp = wasmJS["_malloc"](code.length);
wasmJS["HEAPU8"].set(code, temp);
wasmJS["_load_binary2wasm"](temp, code.length);
} else {
throw "what? " + method;
}
wasmJS["_free"](temp);
wasmJS["_instantiate"](temp);
if (Module["newBuffer"]) {
mergeMemory(Module["newBuffer"]);
Module["newBuffer"] = null;
}
exports = wasmJS["asmExports"];
return exports;
}
Module["asmPreload"] = Module["asm"];
Module["reallocBuffer"] = (function(size) {
size = Math.ceil(size / wasmPageSize) * wasmPageSize;
var old = Module["buffer"];
var result = exports["__growWasmMemory"](size / wasmPageSize);
if (Module["usingWasm"]) {
if (result !== (-1 | 0)) {
return Module["buffer"] = Module["wasmMemory"].buffer;
} else {
return null;
}
} else {
return Module["buffer"] !== old ? Module["buffer"] : null;
}
});
Module["asm"] = (function(global, env, providedBuffer) {
global = fixImports(global);
env = fixImports(env);
if (!env["table"]) {
var TABLE_SIZE = Module["wasmTableSize"];
if (TABLE_SIZE === undefined) TABLE_SIZE = 1024;
if (typeof WebAssembly === "object" && typeof WebAssembly.Table === "function") {
env["table"] = new WebAssembly.Table({
initial: TABLE_SIZE,
maximum: TABLE_SIZE,
element: "anyfunc"
});
} else {
env["table"] = new Array(TABLE_SIZE);
}
}
if (!env["memoryBase"]) {
env["memoryBase"] = STATIC_BASE;
}
if (!env["tableBase"]) {
env["tableBase"] = 0;
}
var exports;
var methods = method.split(",");
for (var i = 0; i < methods.length; i++) {
var curr = methods[i];
Module["printErr"]("trying binaryen method: " + curr);
if (curr === "native-wasm") {
if (exports = doNativeWasm(global, env, providedBuffer)) break;
} else if (curr === "asmjs") {
if (exports = doJustAsm(global, env, providedBuffer)) break;
} else if (curr === "interpret-asm2wasm" || curr === "interpret-s-expr" || curr === "interpret-binary") {
if (exports = doWasmPolyfill(global, env, providedBuffer, curr)) break;
} else {
throw "bad method: " + curr;
}
}
if (!exports) throw "no binaryen method succeeded. consider enabling more options, like interpreting, if you want that: https://github.com/kripken/emscripten/wiki/WebAssembly#binaryen-methods";
Module["printErr"]("binaryen method succeeded.");
return exports;
});
var methodHandler = Module["asm"];
}
var Module;
if (!Module) Module = (typeof Module !== "undefined" ? Module : null) || {};
Module["preRun"] = (function() {
callLuna = (function(inputFileName, tnsFileName) {
return function MyCallMain(args) {
args = args || [];
ensureInitRuntime();
var argc = args.length + 1;
function pad() {
for (var i = 0; i < 4 - 1; i++) {
argv.push(0);
}
}
var argv = [ allocate(intArrayFromString(Module["thisProgram"]), "i8", ALLOC_NORMAL) ];
pad();
for (var i = 0; i < argc - 1; i = i + 1) {
argv.push(allocate(intArrayFromString(args[i]), "i8", ALLOC_NORMAL));
pad();
}
argv.push(0);
argv = allocate(argv, "i32", ALLOC_NORMAL);
return Module["_main"](argc, argv, 0);
}([ inputFileName, tnsFileName ]);
});
makeAndDownloadTNS = (function(luaScript) {
const luaFileName = "myScript.lua";
const tnsFileName = "myScript.tns";
FS.writeFile(luaFileName, luaScript);
try {
FS.unlink(tnsFileName);
} catch (e) {}
const retVal = callLuna(luaFileName, tnsFileName);
if (retVal !== 0) {
alert("Oops, something went wrong generating the .tns file (see console)");
return;
}
const file = FS.readFile(tnsFileName, {
encoding: "binary"
});
if (file) {
const blob = new Blob([ file ], {
type: "application/octet-stream"
});
window["saveAs"](blob, tnsFileName);
} else {
alert("Oops, something went wrong retrieving the generated .tns file :(");
}
});
fileLoaded = (function(event, filename) {
if (event.target.readyState == FileReader.DONE) {
var tnsName = filename.replace(/\.(lua|xml)$/, ".tns");
FS.writeFile(filename, new Uint8Array(event.target.result), {
encoding: "binary"
});
try {
FS.unlink(tnsName);
} catch (e) {}
var retVal = callLuna(filename, tnsName);
if (retVal !== 0) {
alert("Oops, something went wrong generating the .tns file (see console)");
return;
}
var file = FS.readFile(tnsName, {
encoding: "binary"
});
if (file) {
var blob = new Blob([ file ], {
type: "application/octet-stream"
});
window["saveAs"](blob, tnsName);
} else {
alert("Oops, something went wrong retrieving the generated .tns file :(");
}
}
});
fileLoad = (function(event) {
var file = event.target.files[0];
if (!file || file.name.substr(-4) !== ".lua" && file.name.substr(-4) !== ".xml") {
alert("Error: a .lua script or .xml file is needed");
return;
}
var reader = new FileReader;
reader.onloadend = (function(event) {
fileLoaded(event, file.name);
});
reader.readAsArrayBuffer(file);
});
});
var Module;
if (!Module) Module = (typeof Module !== "undefined" ? Module : null) || {};
var moduleOverrides = {};
for (var key in Module) {
if (Module.hasOwnProperty(key)) {
moduleOverrides[key] = Module[key];
}
}
var ENVIRONMENT_IS_WEB = false;
var ENVIRONMENT_IS_WORKER = false;
var ENVIRONMENT_IS_NODE = false;
var ENVIRONMENT_IS_SHELL = false;
if (Module["ENVIRONMENT"]) {
if (Module["ENVIRONMENT"] === "WEB") {
ENVIRONMENT_IS_WEB = true;
} else if (Module["ENVIRONMENT"] === "WORKER") {
ENVIRONMENT_IS_WORKER = true;
} else if (Module["ENVIRONMENT"] === "NODE") {
ENVIRONMENT_IS_NODE = true;
} else if (Module["ENVIRONMENT"] === "SHELL") {
ENVIRONMENT_IS_SHELL = true;
} else {
throw new Error("The provided Module['ENVIRONMENT'] value is not valid. It must be one of: WEB|WORKER|NODE|SHELL.");
}
} else {
ENVIRONMENT_IS_WEB = typeof window === "object";
ENVIRONMENT_IS_WORKER = typeof importScripts === "function";
ENVIRONMENT_IS_NODE = typeof process === "object" && typeof require === "function" && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;
ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
}
if (ENVIRONMENT_IS_NODE) {
if (!Module["print"]) Module["print"] = console.log;
if (!Module["printErr"]) Module["printErr"] = console.warn;
var nodeFS;
var nodePath;
Module["read"] = function read(filename, binary) {
if (!nodeFS) nodeFS = require("fs");
if (!nodePath) nodePath = require("path");
filename = nodePath["normalize"](filename);
var ret = nodeFS["readFileSync"](filename);
return binary ? ret : ret.toString();
};
Module["readBinary"] = function readBinary(filename) {
var ret = Module["read"](filename, true);
if (!ret.buffer) {
ret = new Uint8Array(ret);
}
assert(ret.buffer);
return ret;
};
Module["load"] = function load(f) {
globalEval(read(f));
};
if (!Module["thisProgram"]) {
if (process["argv"].length > 1) {
Module["thisProgram"] = process["argv"][1].replace(/\\/g, "/");
} else {
Module["thisProgram"] = "unknown-program";
}
}
Module["arguments"] = process["argv"].slice(2);
if (typeof module !== "undefined") {
module["exports"] = Module;
}
process["on"]("uncaughtException", (function(ex) {
if (!(ex instanceof ExitStatus)) {
throw ex;
}
}));
Module["inspect"] = (function() {
return "[Emscripten Module object]";
});
} else if (ENVIRONMENT_IS_SHELL) {
if (!Module["print"]) Module["print"] = print;
if (typeof printErr != "undefined") Module["printErr"] = printErr;
if (typeof read != "undefined") {
Module["read"] = read;
} else {
Module["read"] = function read() {
throw "no read() available";
};
}
Module["readBinary"] = function readBinary(f) {
if (typeof readbuffer === "function") {
return new Uint8Array(readbuffer(f));
}
var data = read(f, "binary");
assert(typeof data === "object");
return data;
};
if (typeof scriptArgs != "undefined") {
Module["arguments"] = scriptArgs;
} else if (typeof arguments != "undefined") {
Module["arguments"] = arguments;
}
} else if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
Module["read"] = function read(url) {
var xhr = new XMLHttpRequest;
xhr.open("GET", url, false);
xhr.send(null);
return xhr.responseText;
};
Module["readAsync"] = function readAsync(url, onload, onerror) {
var xhr = new XMLHttpRequest;
xhr.open("GET", url, true);
xhr.responseType = "arraybuffer";
xhr.onload = function xhr_onload() {
if (xhr.status == 200 || xhr.status == 0 && xhr.response) {
onload(xhr.response);
} else {
onerror();
}
};
xhr.onerror = onerror;
xhr.send(null);
};
if (typeof arguments != "undefined") {
Module["arguments"] = arguments;
}
if (typeof console !== "undefined") {
if (!Module["print"]) Module["print"] = function print(x) {
console.log(x);
};
if (!Module["printErr"]) Module["printErr"] = function printErr(x) {
console.warn(x);
};
} else {
var TRY_USE_DUMP = false;
if (!Module["print"]) Module["print"] = TRY_USE_DUMP && typeof dump !== "undefined" ? (function(x) {
dump(x);
}) : (function(x) {});
}
if (ENVIRONMENT_IS_WORKER) {
Module["load"] = importScripts;
}
if (typeof Module["setWindowTitle"] === "undefined") {
Module["setWindowTitle"] = (function(title) {
document.title = title;
});
}
} else {
throw "Unknown runtime environment. Where are we?";
}
function globalEval(x) {
eval.call(null, x);
}
if (!Module["load"] && Module["read"]) {
Module["load"] = function load(f) {
globalEval(Module["read"](f));
};
}
if (!Module["print"]) {
Module["print"] = (function() {});
}
if (!Module["printErr"]) {
Module["printErr"] = Module["print"];
}
if (!Module["arguments"]) {
Module["arguments"] = [];
}
if (!Module["thisProgram"]) {
Module["thisProgram"] = "./this.program";
}
Module.print = Module["print"];
Module.printErr = Module["printErr"];
Module["preRun"] = [];
Module["postRun"] = [];
for (var key in moduleOverrides) {
if (moduleOverrides.hasOwnProperty(key)) {
Module[key] = moduleOverrides[key];
}
}
moduleOverrides = undefined;
integrateWasmJS(Module);
var Runtime = {
setTempRet0: (function(value) {
tempRet0 = value;
}),
getTempRet0: (function() {
return tempRet0;
}),
stackSave: (function() {
return STACKTOP;
}),
stackRestore: (function(stackTop) {
STACKTOP = stackTop;
}),
getNativeTypeSize: (function(type) {
switch (type) {
case "i1":
case "i8":
return 1;
case "i16":
return 2;
case "i32":
return 4;
case "i64":
return 8;
case "float":
return 4;
case "double":
return 8;
default:
{
if (type[type.length - 1] === "*") {
return Runtime.QUANTUM_SIZE;
} else if (type[0] === "i") {
var bits = parseInt(type.substr(1));
assert(bits % 8 === 0);
return bits / 8;
} else {
return 0;
}
}
}
}),
getNativeFieldSize: (function(type) {
return Math.max(Runtime.getNativeTypeSize(type), Runtime.QUANTUM_SIZE);
}),
STACK_ALIGN: 16,
prepVararg: (function(ptr, type) {
if (type === "double" || type === "i64") {
if (ptr & 7) {
assert((ptr & 7) === 4);
ptr += 4;
}
} else {
assert((ptr & 3) === 0);
}
return ptr;
}),
getAlignSize: (function(type, size, vararg) {
if (!vararg && (type == "i64" || type == "double")) return 8;
if (!type) return Math.min(size, 8);
return Math.min(size || (type ? Runtime.getNativeFieldSize(type) : 0), Runtime.QUANTUM_SIZE);
}),
dynCall: (function(sig, ptr, args) {
if (args && args.length) {
return Module["dynCall_" + sig].apply(null, [ ptr ].concat(args));
} else {
return Module["dynCall_" + sig].call(null, ptr);
}
}),
functionPointers: [],
addFunction: (function(func) {
for (var i = 0; i < Runtime.functionPointers.length; i++) {
if (!Runtime.functionPointers[i]) {
Runtime.functionPointers[i] = func;
return 2 * (1 + i);
}
}
throw "Finished up all reserved function pointers. Use a higher value for RESERVED_FUNCTION_POINTERS.";
}),
removeFunction: (function(index) {
Runtime.functionPointers[(index - 2) / 2] = null;
}),
warnOnce: (function(text) {
if (!Runtime.warnOnce.shown) Runtime.warnOnce.shown = {};
if (!Runtime.warnOnce.shown[text]) {
Runtime.warnOnce.shown[text] = 1;
Module.printErr(text);
}
}),
funcWrappers: {},
getFuncWrapper: (function(func, sig) {
assert(sig);
if (!Runtime.funcWrappers[sig]) {
Runtime.funcWrappers[sig] = {};
}
var sigCache = Runtime.funcWrappers[sig];
if (!sigCache[func]) {
if (sig.length === 1) {
sigCache[func] = function dynCall_wrapper() {
return Runtime.dynCall(sig, func);
};
} else if (sig.length === 2) {
sigCache[func] = function dynCall_wrapper(arg) {
return Runtime.dynCall(sig, func, [ arg ]);
};
} else {
sigCache[func] = function dynCall_wrapper() {
return Runtime.dynCall(sig, func, Array.prototype.slice.call(arguments));
};
}
}
return sigCache[func];
}),
getCompilerSetting: (function(name) {
throw "You must build with -s RETAIN_COMPILER_SETTINGS=1 for Runtime.getCompilerSetting or emscripten_get_compiler_setting to work";
}),
stackAlloc: (function(size) {
var ret = STACKTOP;
STACKTOP = STACKTOP + size | 0;
STACKTOP = STACKTOP + 15 & -16;
return ret;
}),
staticAlloc: (function(size) {
var ret = STATICTOP;
STATICTOP = STATICTOP + size | 0;
STATICTOP = STATICTOP + 15 & -16;
return ret;
}),
dynamicAlloc: (function(size) {
var ret = HEAP32[DYNAMICTOP_PTR >> 2];
var end = (ret + size + 15 | 0) & -16;
HEAP32[DYNAMICTOP_PTR >> 2] = end;
if (end >= TOTAL_MEMORY) {
var success = enlargeMemory();
if (!success) {
HEAP32[DYNAMICTOP_PTR >> 2] = ret;
return 0;
}
}
return ret;
}),
alignMemory: (function(size, quantum) {
var ret = size = Math.ceil(size / (quantum ? quantum : 16)) * (quantum ? quantum : 16);
return ret;
}),
makeBigInt: (function(low, high, unsigned) {
var ret = unsigned ? +(low >>> 0) + +(high >>> 0) * +4294967296 : +(low >>> 0) + +(high | 0) * +4294967296;
return ret;
}),
GLOBAL_BASE: 1024,
QUANTUM_SIZE: 4,
__dummy__: 0
};
Module["Runtime"] = Runtime;
var ABORT = 0;
var EXITSTATUS = 0;
function assert(condition, text) {
if (!condition) {
abort("Assertion failed: " + text);
}
}
function getCFunc(ident) {
var func = Module["_" + ident];
if (!func) {
try {
func = eval("_" + ident);
} catch (e) {}
}
assert(func, "Cannot call unknown function " + ident + " (perhaps LLVM optimizations or closure removed it?)");
return func;
}
var cwrap, ccall;
((function() {
var JSfuncs = {
"stackSave": (function() {
Runtime.stackSave();
}),
"stackRestore": (function() {
Runtime.stackRestore();
}),
"arrayToC": (function(arr) {
var ret = Runtime.stackAlloc(arr.length);
writeArrayToMemory(arr, ret);
return ret;
}),
"stringToC": (function(str) {
var ret = 0;
if (str !== null && str !== undefined && str !== 0) {
var len = (str.length << 2) + 1;
ret = Runtime.stackAlloc(len);
stringToUTF8(str, ret, len);
}
return ret;
})
};
var toC = {
"string": JSfuncs["stringToC"],
"array": JSfuncs["arrayToC"]
};
ccall = function ccallFunc(ident, returnType, argTypes, args, opts) {
var func = getCFunc(ident);
var cArgs = [];
var stack = 0;
if (args) {
for (var i = 0; i < args.length; i++) {
var converter = toC[argTypes[i]];
if (converter) {
if (stack === 0) stack = Runtime.stackSave();
cArgs[i] = converter(args[i]);
} else {
cArgs[i] = args[i];
}
}
}
var ret = func.apply(null, cArgs);
if (returnType === "string") ret = Pointer_stringify(ret);
if (stack !== 0) {
if (opts && opts.async) {
EmterpreterAsync.asyncFinalizers.push((function() {
Runtime.stackRestore(stack);
}));
return;
}
Runtime.stackRestore(stack);
}
return ret;
};
var sourceRegex = /^function\s*[a-zA-Z$_0-9]*\s*\(([^)]*)\)\s*{\s*([^*]*?)[\s;]*(?:return\s*(.*?)[;\s]*)?}$/;
function parseJSFunc(jsfunc) {
var parsed = jsfunc.toString().match(sourceRegex).slice(1);
return {
arguments: parsed[0],
body: parsed[1],
returnValue: parsed[2]
};
}
var JSsource = null;
function ensureJSsource() {
if (!JSsource) {
JSsource = {};
for (var fun in JSfuncs) {
if (JSfuncs.hasOwnProperty(fun)) {
JSsource[fun] = parseJSFunc(JSfuncs[fun]);
}
}
}
}
cwrap = function cwrap(ident, returnType, argTypes) {
argTypes = argTypes || [];
var cfunc = getCFunc(ident);
var numericArgs = argTypes.every((function(type) {
return type === "number";
}));
var numericRet = returnType !== "string";
if (numericRet && numericArgs) {
return cfunc;
}
var argNames = argTypes.map((function(x, i) {
return "$" + i;
}));
var funcstr = "(function(" + argNames.join(",") + ") {";
var nargs = argTypes.length;
if (!numericArgs) {
ensureJSsource();
funcstr += "var stack = " + JSsource["stackSave"].body + ";";
for (var i = 0; i < nargs; i++) {
var arg = argNames[i], type = argTypes[i];
if (type === "number") continue;
var convertCode = JSsource[type + "ToC"];
funcstr += "var " + convertCode.arguments + " = " + arg + ";";
funcstr += convertCode.body + ";";
funcstr += arg + "=(" + convertCode.returnValue + ");";
}
}
var cfuncname = parseJSFunc((function() {
return cfunc;
})).returnValue;
funcstr += "var ret = " + cfuncname + "(" + argNames.join(",") + ");";
if (!numericRet) {
var strgfy = parseJSFunc((function() {
return Pointer_stringify;
})).returnValue;
funcstr += "ret = " + strgfy + "(ret);";
}
if (!numericArgs) {
ensureJSsource();
funcstr += JSsource["stackRestore"].body.replace("()", "(stack)") + ";";
}
funcstr += "return ret})";
return eval(funcstr);
};
}))();
Module["ccall"] = ccall;
Module["cwrap"] = cwrap;
function setValue(ptr, value, type, noSafe) {
type = type || "i8";
if (type.charAt(type.length - 1) === "*") type = "i32";
switch (type) {
case "i1":
HEAP8[ptr >> 0] = value;
break;
case "i8":
HEAP8[ptr >> 0] = value;
break;
case "i16":
HEAP16[ptr >> 1] = value;
break;
case "i32":
HEAP32[ptr >> 2] = value;
break;
case "i64":
tempI64 = [ value >>> 0, (tempDouble = value, +Math_abs(tempDouble) >= +1 ? tempDouble > +0 ? (Math_min(+Math_floor(tempDouble / +4294967296), +4294967295) | 0) >>> 0 : ~~+Math_ceil((tempDouble - +(~~tempDouble >>> 0)) / +4294967296) >>> 0 : 0) ], HEAP32[ptr >> 2] = tempI64[0], HEAP32[ptr + 4 >> 2] = tempI64[1];
break;
case "float":
HEAPF32[ptr >> 2] = value;
break;
case "double":
HEAPF64[ptr >> 3] = value;
break;
default:
abort("invalid type for setValue: " + type);
}
}
Module["setValue"] = setValue;
function getValue(ptr, type, noSafe) {
type = type || "i8";
if (type.charAt(type.length - 1) === "*") type = "i32";
switch (type) {
case "i1":
return HEAP8[ptr >> 0];
case "i8":
return HEAP8[ptr >> 0];
case "i16":
return HEAP16[ptr >> 1];
case "i32":
return HEAP32[ptr >> 2];
case "i64":
return HEAP32[ptr >> 2];
case "float":
return HEAPF32[ptr >> 2];
case "double":
return HEAPF64[ptr >> 3];
default:
abort("invalid type for setValue: " + type);
}
return null;
}
Module["getValue"] = getValue;
var ALLOC_NORMAL = 0;
var ALLOC_STACK = 1;
var ALLOC_STATIC = 2;
var ALLOC_DYNAMIC = 3;
var ALLOC_NONE = 4;
Module["ALLOC_NORMAL"] = ALLOC_NORMAL;
Module["ALLOC_STACK"] = ALLOC_STACK;
Module["ALLOC_STATIC"] = ALLOC_STATIC;
Module["ALLOC_DYNAMIC"] = ALLOC_DYNAMIC;
Module["ALLOC_NONE"] = ALLOC_NONE;
function allocate(slab, types, allocator, ptr) {
var zeroinit, size;
if (typeof slab === "number") {
zeroinit = true;
size = slab;
} else {
zeroinit = false;
size = slab.length;
}
var singleType = typeof types === "string" ? types : null;
var ret;
if (allocator == ALLOC_NONE) {
ret = ptr;
} else {
ret = [ typeof _malloc === "function" ? _malloc : Runtime.staticAlloc, Runtime.stackAlloc, Runtime.staticAlloc, Runtime.dynamicAlloc ][allocator === undefined ? ALLOC_STATIC : allocator](Math.max(size, singleType ? 1 : types.length));
}
if (zeroinit) {
var ptr = ret, stop;
assert((ret & 3) == 0);
stop = ret + (size & ~3);
for (; ptr < stop; ptr += 4) {
HEAP32[ptr >> 2] = 0;
}
stop = ret + size;
while (ptr < stop) {
HEAP8[ptr++ >> 0] = 0;
}
return ret;
}
if (singleType === "i8") {
if (slab.subarray || slab.slice) {
HEAPU8.set(slab, ret);
} else {
HEAPU8.set(new Uint8Array(slab), ret);
}
return ret;
}
var i = 0, type, typeSize, previousType;
while (i < size) {
var curr = slab[i];
if (typeof curr === "function") {
curr = Runtime.getFunctionIndex(curr);
}
type = singleType || types[i];
if (type === 0) {
i++;
continue;
}
if (type == "i64") type = "i32";
setValue(ret + i, curr, type);
if (previousType !== type) {
typeSize = Runtime.getNativeTypeSize(type);
previousType = type;
}
i += typeSize;
}
return ret;
}
Module["allocate"] = allocate;
function getMemory(size) {
if (!staticSealed) return Runtime.staticAlloc(size);
if (!runtimeInitialized) return Runtime.dynamicAlloc(size);
return _malloc(size);
}
Module["getMemory"] = getMemory;
function Pointer_stringify(ptr, length) {
if (length === 0 || !ptr) return "";
var hasUtf = 0;
var t;
var i = 0;
while (1) {
t = HEAPU8[ptr + i >> 0];
hasUtf |= t;
if (t == 0 && !length) break;
i++;
if (length && i == length) break;
}
if (!length) length = i;
var ret = "";
if (hasUtf < 128) {
var MAX_CHUNK = 1024;
var curr;
while (length > 0) {
curr = String.fromCharCode.apply(String, HEAPU8.subarray(ptr, ptr + Math.min(length, MAX_CHUNK)));
ret = ret ? ret + curr : curr;
ptr += MAX_CHUNK;
length -= MAX_CHUNK;
}
return ret;
}
return Module["UTF8ToString"](ptr);
}
Module["Pointer_stringify"] = Pointer_stringify;
function AsciiToString(ptr) {
var str = "";
while (1) {
var ch = HEAP8[ptr++ >> 0];
if (!ch) return str;
str += String.fromCharCode(ch);
}
}
Module["AsciiToString"] = AsciiToString;
function stringToAscii(str, outPtr) {
return writeAsciiToMemory(str, outPtr, false);
}
Module["stringToAscii"] = stringToAscii;
var UTF8Decoder = typeof TextDecoder !== "undefined" ? new TextDecoder("utf8") : undefined;
function UTF8ArrayToString(u8Array, idx) {
var endPtr = idx;
while (u8Array[endPtr]) ++endPtr;
if (endPtr - idx > 16 && u8Array.subarray && UTF8Decoder) {
return UTF8Decoder.decode(u8Array.subarray(idx, endPtr));
} else {
var u0, u1, u2, u3, u4, u5;
var str = "";
while (1) {
u0 = u8Array[idx++];
if (!u0) return str;
if (!(u0 & 128)) {
str += String.fromCharCode(u0);
continue;
}
u1 = u8Array[idx++] & 63;
if ((u0 & 224) == 192) {
str += String.fromCharCode((u0 & 31) << 6 | u1);
continue;
}
u2 = u8Array[idx++] & 63;
if ((u0 & 240) == 224) {
u0 = (u0 & 15) << 12 | u1 << 6 | u2;
} else {
u3 = u8Array[idx++] & 63;
if ((u0 & 248) == 240) {
u0 = (u0 & 7) << 18 | u1 << 12 | u2 << 6 | u3;