-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
preamble.js
1191 lines (1083 loc) · 38 KB
/
preamble.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
if (typeof log === 'function') log('premalbe');
// === Preamble library stuff ===
// Documentation for the public APIs defined in this file must be updated in:
// site/source/docs/api_reference/preamble.js.rst
// A prebuilt local version of the documentation is available at:
// site/build/text/docs/api_reference/preamble.js.txt
// You can also build docs locally as HTML or other formats in site/
// An online HTML version (which may be of a different version of Emscripten)
// is up at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html
#if BENCHMARK
Module.realPrint = out;
out = err = function(){};
#endif
#if WASM2JS
#include "wasm2js.js"
#endif
#if WASM
if (typeof WebAssembly !== 'object') {
#if ASSERTIONS
abort('No WebAssembly support found. Build with -s WASM=0 to target JavaScript instead.');
#else
err('no native wasm support detected');
#endif
}
#endif
#include "runtime_safe_heap.js"
// Wasm globals
var wasmMemory;
// Potentially used for direct table calls.
var wasmTable;
#if USE_PTHREADS
// For sending to workers.
var wasmModule;
#endif // USE_PTHREADS
//========================================
// Runtime essentials
//========================================
// whether we are quitting the application. no code should run after this.
// set in exit() and abort()
var ABORT = false;
// set by exit() and abort(). Passed to 'onExit' handler.
// NOTE: This is also used as the process return code code in shell environments
// but only when noExitRuntime is false.
var EXITSTATUS = 0;
/** @type {function(*, string=)} */
function assert(condition, text) {
if (!condition) {
abort('Assertion failed: ' + text);
}
}
// Returns the C function with a specified identifier (for C++, you need to do manual name mangling)
function getCFunc(ident) {
var func = Module['_' + ident]; // closure exported function
assert(func, 'Cannot call unknown function ' + ident + ', make sure it is exported');
return func;
}
// C calling interface.
function ccall(ident, returnType, argTypes, args, opts) {
// For fast lookup of conversion functions
var toC = {
'string': function(str) {
var ret = 0;
if (str !== null && str !== undefined && str !== 0) { // null string
// at most 4 bytes per UTF-8 code point, +1 for the trailing '\0'
var len = (str.length << 2) + 1;
ret = stackAlloc(len);
stringToUTF8(str, ret, len);
}
return ret;
},
'array': function(arr) {
var ret = stackAlloc(arr.length);
writeArrayToMemory(arr, ret);
return ret;
}
};
function convertReturnValue(ret) {
if (returnType === 'string') return UTF8ToString(ret);
if (returnType === 'boolean') return Boolean(ret);
return ret;
}
var func = getCFunc(ident);
var cArgs = [];
var stack = 0;
#if ASSERTIONS
assert(returnType !== 'array', 'Return type should not be "array".');
#endif
if (args) {
for (var i = 0; i < args.length; i++) {
var converter = toC[argTypes[i]];
if (converter) {
if (stack === 0) stack = stackSave();
cArgs[i] = converter(args[i]);
} else {
cArgs[i] = args[i];
}
}
}
var ret = func.apply(null, cArgs);
#if EMTERPRETIFY_ASYNC
if (typeof EmterpreterAsync === 'object' && EmterpreterAsync.state) {
#if ASSERTIONS
assert(opts && opts.async, 'The call to ' + ident + ' is running asynchronously. If this was intended, add the async option to the ccall/cwrap call.');
assert(!EmterpreterAsync.restartFunc, 'Cannot have multiple async ccalls in flight at once');
#endif
return new Promise(function(resolve) {
EmterpreterAsync.restartFunc = func;
EmterpreterAsync.asyncFinalizers.push(function(ret) {
if (stack !== 0) stackRestore(stack);
resolve(convertReturnValue(ret));
});
});
}
#endif
ret = convertReturnValue(ret);
if (stack !== 0) stackRestore(stack);
#if EMTERPRETIFY_ASYNC
// If this is an async ccall, ensure we return a promise
if (opts && opts.async) return Promise.resolve(ret);
#endif
return ret;
}
function cwrap(ident, returnType, argTypes, opts) {
#if !ASSERTIONS
argTypes = argTypes || [];
// When the function takes numbers and returns a number, we can just return
// the original function
var numericArgs = argTypes.every(function(type){ return type === 'number'});
var numericRet = returnType !== 'string';
if (numericRet && numericArgs && !opts) {
return getCFunc(ident);
}
#endif
return function() {
return ccall(ident, returnType, argTypes, arguments, opts);
}
}
var ALLOC_NORMAL = 0; // Tries to use _malloc()
var ALLOC_STACK = 1; // Lives for the duration of the current function call
var ALLOC_DYNAMIC = 2; // Cannot be freed except through sbrk
var ALLOC_NONE = 3; // Do not allocate
// allocate(): This is for internal use. You can use it yourself as well, but the interface
// is a little tricky (see docs right below). The reason is that it is optimized
// for multiple syntaxes to save space in generated code. So you should
// normally not use allocate(), and instead allocate memory using _malloc(),
// initialize it with setValue(), and so forth.
// @slab: An array of data, or a number. If a number, then the size of the block to allocate,
// in *bytes* (note that this is sometimes confusing: the next parameter does not
// affect this!)
// @types: Either an array of types, one for each byte (or 0 if no type at that position),
// or a single type which is used for the entire block. This only matters if there
// is initial data - if @slab is a number, then this does not matter at all and is
// ignored.
// @allocator: How to allocate memory, see ALLOC_*
/** @type {function((TypedArray|Array<number>|number), string, number, number=)} */
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 = [_malloc,
#if DECLARE_ASM_MODULE_EXPORTS
stackAlloc,
#else
typeof stackAlloc !== 'undefined' ? stackAlloc : null,
#endif
dynamicAlloc][allocator](Math.max(size, singleType ? 1 : types.length));
}
if (zeroinit) {
var stop;
ptr = ret;
assert((ret & 3) == 0);
stop = ret + (size & ~3);
for (; ptr < stop; ptr += 4) {
{{{ makeSetValue('ptr', '0', '0', 'i32', null, true) }}};
}
stop = ret + size;
while (ptr < stop) {
{{{ makeSetValue('ptr++', '0', '0', 'i8', null, true) }}};
}
return ret;
}
if (singleType === 'i8') {
if (slab.subarray || slab.slice) {
HEAPU8.set(/** @type {!Uint8Array} */ (slab), ret);
} else {
HEAPU8.set(new Uint8Array(slab), ret);
}
return ret;
}
var i = 0, type, typeSize, previousType;
while (i < size) {
var curr = slab[i];
type = singleType || types[i];
if (type === 0) {
i++;
continue;
}
#if ASSERTIONS
assert(type, 'Must know what type to store in allocate!');
#endif
if (type == 'i64') type = 'i32'; // special case: we have one i32 here, and one i32 later
setValue(ret+i, curr, type);
// no need to look up size unless type changes, so cache it
if (previousType !== type) {
typeSize = getNativeTypeSize(type);
previousType = type;
}
i += typeSize;
}
return ret;
}
// Allocate memory during any stage of startup - static memory early on, dynamic memory later, malloc when ready
function getMemory(size) {
if (!runtimeInitialized) return dynamicAlloc(size);
return _malloc(size);
}
#include "runtime_strings.js"
#include "runtime_stack_trace.js"
// Memory management
var PAGE_SIZE = 16384;
var WASM_PAGE_SIZE = {{{ WASM_PAGE_SIZE }}};
var ASMJS_PAGE_SIZE = {{{ ASMJS_PAGE_SIZE }}};
function alignUp(x, multiple) {
if (x % multiple > 0) {
x += multiple - (x % multiple);
}
return x;
}
var HEAP,
/** @type {ArrayBuffer} */
buffer,
/** @type {Int8Array} */
HEAP8,
/** @type {Uint8Array} */
HEAPU8,
/** @type {Int16Array} */
HEAP16,
/** @type {Uint16Array} */
HEAPU16,
/** @type {Int32Array} */
HEAP32,
/** @type {Uint32Array} */
HEAPU32,
/** @type {Float32Array} */
HEAPF32,
/** @type {Float64Array} */
HEAPF64;
function updateGlobalBufferViews() {
Module['HEAP8'] = HEAP8 = new Int8Array(buffer);
Module['HEAP16'] = HEAP16 = new Int16Array(buffer);
Module['HEAP32'] = HEAP32 = new Int32Array(buffer);
Module['HEAPU8'] = HEAPU8 = new Uint8Array(buffer);
Module['HEAPU16'] = HEAPU16 = new Uint16Array(buffer);
Module['HEAPU32'] = HEAPU32 = new Uint32Array(buffer);
Module['HEAPF32'] = HEAPF32 = new Float32Array(buffer);
Module['HEAPF64'] = HEAPF64 = new Float64Array(buffer);
}
#if USE_PTHREADS
if (!ENVIRONMENT_IS_PTHREAD) { // Pthreads have already initialized these variables in src/worker.js, where they were passed to the thread worker at startup time
#endif
var STATIC_BASE = {{{ GLOBAL_BASE }}},
STACK_BASE = {{{ getQuoted('STACK_BASE') }}},
STACKTOP = STACK_BASE,
STACK_MAX = {{{ getQuoted('STACK_MAX') }}},
DYNAMIC_BASE = {{{ getQuoted('DYNAMIC_BASE') }}},
DYNAMICTOP_PTR = {{{ makeStaticAlloc(4) }}};
#if ASSERTIONS
assert(STACK_BASE % 16 === 0, 'stack must start aligned');
assert(DYNAMIC_BASE % 16 === 0, 'heap must start aligned');
#endif
#if USE_PTHREADS
}
#endif
#if EMTERPRETIFY
function abortStackOverflowEmterpreter() {
abort("Emterpreter stack overflow! Decrease the recursion level or increase EMT_STACK_MAX in tools/emterpretify.py (current value " + EMT_STACK_MAX + ").");
}
#endif
var TOTAL_STACK = {{{ TOTAL_STACK }}};
#if ASSERTIONS
if (Module['TOTAL_STACK']) assert(TOTAL_STACK === Module['TOTAL_STACK'], 'the stack size can no longer be determined at runtime')
#endif
var INITIAL_TOTAL_MEMORY = Module['TOTAL_MEMORY'] || {{{ TOTAL_MEMORY }}};
if (INITIAL_TOTAL_MEMORY < TOTAL_STACK) err('TOTAL_MEMORY should be larger than TOTAL_STACK, was ' + INITIAL_TOTAL_MEMORY + '! (TOTAL_STACK=' + TOTAL_STACK + ')');
// Initialize the runtime's memory
#if ASSERTIONS
// check for full engine support (use string 'subarray' to avoid closure compiler confusion)
assert(typeof Int32Array !== 'undefined' && typeof Float64Array !== 'undefined' && Int32Array.prototype.subarray !== undefined && Int32Array.prototype.set !== undefined,
'JS engine does not provide full typed array support');
#endif
#if IN_TEST_HARNESS
// Test runs in browsers should always be free from uncaught exceptions. If an uncaught exception is thrown, we fail browser test execution in the REPORT_RESULT() macro to output an error value.
if (ENVIRONMENT_IS_WEB) {
window.addEventListener('error', function(e) {
if (e.message.indexOf('SimulateInfiniteLoop') != -1) return;
console.error('Page threw an exception ' + e);
Module['pageThrewException'] = true;
});
}
#if USE_PTHREADS
if (typeof SharedArrayBuffer === 'undefined' || typeof Atomics === 'undefined') {
xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:8888/report_result?skipped:%20SharedArrayBuffer%20is%20not%20supported!');
xhr.send();
setTimeout(function() { window.close() }, 2000);
}
#endif
#endif
#include "runtime_sab_polyfill.js"
#if USE_PTHREADS
if (ENVIRONMENT_IS_PTHREAD) {
#if MODULARIZE && WASM
// In pthreads mode the wasmMemory and others are received in an onmessage, and that
// onmessage then loadScripts us, sending wasmMemory etc. on Module. Here we recapture
// it to a local so it can be used normally.
wasmMemory = Module['wasmMemory'];
#endif
} else {
#endif // USE_PTHREADS
#if WASM
if (Module['wasmMemory']) {
wasmMemory = Module['wasmMemory'];
} else {
wasmMemory = new WebAssembly.Memory({
'initial': INITIAL_TOTAL_MEMORY / WASM_PAGE_SIZE
#if ALLOW_MEMORY_GROWTH
#if WASM_MEM_MAX != -1
,
'maximum': {{{ WASM_MEM_MAX }}} / WASM_PAGE_SIZE
#endif
#else
,
'maximum': INITIAL_TOTAL_MEMORY / WASM_PAGE_SIZE
#endif // ALLOW_MEMORY_GROWTH
#if USE_PTHREADS
,
'shared': true
#endif
});
#if USE_PTHREADS
assert(wasmMemory.buffer instanceof SharedArrayBuffer, 'requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag');
#endif
}
#else // WASM
if (Module['buffer']) {
buffer = Module['buffer'];
}
#ifdef USE_PTHREADS
else if (typeof SharedArrayBuffer !== 'undefined') {
buffer = new SharedArrayBuffer(INITIAL_TOTAL_MEMORY);
}
#endif
else {
buffer = new ArrayBuffer(INITIAL_TOTAL_MEMORY);
}
#endif // WASM
#if USE_PTHREADS
}
#endif
#if WASM
if (wasmMemory) {
buffer = wasmMemory.buffer;
}
#endif
// If the user provides an incorrect length, just use that length instead rather than providing the user to
// specifically provide the memory length with Module['TOTAL_MEMORY'].
INITIAL_TOTAL_MEMORY = buffer.byteLength;
#ifdef ASSERTIONS && WASM
assert(INITIAL_TOTAL_MEMORY % WASM_PAGE_SIZE === 0);
#ifdef ALLOW_MEMORY_GROWTH && WASM_MEM_MAX != -1
assert({{{ WASM_PAGE_SIZE }}} % WASM_PAGE_SIZE === 0);
#endif
#endif
updateGlobalBufferViews();
#if USE_PTHREADS
if (!ENVIRONMENT_IS_PTHREAD) { // Pthreads have already initialized these variables in src/worker.js, where they were passed to the thread worker at startup time
#endif
HEAP32[DYNAMICTOP_PTR>>2] = DYNAMIC_BASE;
#if USE_PTHREADS
}
#endif
#include "runtime_stack_check.js"
// Endianness check (note: assumes compiler arch was little-endian)
#if ASSERTIONS
HEAP16[1] = 0x6373;
if (HEAPU8[2] !== 0x73 || HEAPU8[3] !== 0x63) throw 'Runtime error: expected the system to be little-endian!';
#endif // ASSERTIONS
function callRuntimeCallbacks(callbacks) {
while(callbacks.length > 0) {
var callback = callbacks.shift();
if (typeof callback == 'function') {
callback();
continue;
}
var func = callback.func;
if (typeof func === 'number') {
if (callback.arg === undefined) {
Module['dynCall_v'](func);
} else {
Module['dynCall_vi'](func, callback.arg);
}
} else {
func(callback.arg === undefined ? null : callback.arg);
}
}
}
var __ATPRERUN__ = []; // functions called before the runtime is initialized
var __ATINIT__ = []; // functions called during startup
var __ATMAIN__ = []; // functions called when main() is to be run
var __ATEXIT__ = []; // functions called during shutdown
var __ATPOSTRUN__ = []; // functions called after the main() is called
var runtimeInitialized = false;
var runtimeExited = false;
#if USE_PTHREADS
if (ENVIRONMENT_IS_PTHREAD) runtimeInitialized = true; // The runtime is hosted in the main thread, and bits shared to pthreads via SharedArrayBuffer. No need to init again in pthread.
#endif
function preRun() {
#if USE_PTHREADS
if (ENVIRONMENT_IS_PTHREAD) return; // PThreads reuse the runtime from the main thread.
#endif
// compatibility - merge in anything from Module['preRun'] at this time
if (Module['preRun']) {
if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
while (Module['preRun'].length) {
addOnPreRun(Module['preRun'].shift());
}
}
callRuntimeCallbacks(__ATPRERUN__);
}
function initRuntime() {
#if STACK_OVERFLOW_CHECK
checkStackCookie();
#endif
#if ASSERTIONS
assert(!runtimeInitialized);
#endif
runtimeInitialized = true;
#if USE_PTHREADS
// Pass the thread address inside the asm.js scope to store it for fast access that avoids the need for a FFI out.
__register_pthread_ptr(PThread.mainThreadBlock, /*isMainBrowserThread=*/!ENVIRONMENT_IS_WORKER, /*isMainRuntimeThread=*/1);
_emscripten_register_main_browser_thread_id(PThread.mainThreadBlock);
#endif
{{{ getQuoted('ATINITS') }}}
callRuntimeCallbacks(__ATINIT__);
}
function preMain() {
#if STACK_OVERFLOW_CHECK
checkStackCookie();
#endif
#if USE_PTHREADS
if (ENVIRONMENT_IS_PTHREAD) return; // PThreads reuse the runtime from the main thread.
#endif
{{{ getQuoted('ATMAINS') }}}
callRuntimeCallbacks(__ATMAIN__);
}
function exitRuntime() {
#if STACK_OVERFLOW_CHECK
checkStackCookie();
#endif
#if USE_PTHREADS
if (ENVIRONMENT_IS_PTHREAD) return; // PThreads reuse the runtime from the main thread.
#endif
#if EXIT_RUNTIME
callRuntimeCallbacks(__ATEXIT__);
{{{ getQuoted('ATEXITS') }}}
#endif
runtimeExited = true;
}
function postRun() {
#if STACK_OVERFLOW_CHECK
checkStackCookie();
#endif
#if USE_PTHREADS
if (ENVIRONMENT_IS_PTHREAD) return; // PThreads reuse the runtime from the main thread.
#endif
// compatibility - merge in anything from Module['postRun'] at this time
if (Module['postRun']) {
if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']];
while (Module['postRun'].length) {
addOnPostRun(Module['postRun'].shift());
}
}
callRuntimeCallbacks(__ATPOSTRUN__);
}
function addOnPreRun(cb) {
__ATPRERUN__.unshift(cb);
}
function addOnInit(cb) {
__ATINIT__.unshift(cb);
}
function addOnPreMain(cb) {
__ATMAIN__.unshift(cb);
}
function addOnExit(cb) {
#if EXIT_RUNTIME
__ATEXIT__.unshift(cb);
#endif
}
function addOnPostRun(cb) {
__ATPOSTRUN__.unshift(cb);
}
{{{ unSign }}}
{{{ reSign }}}
#include "runtime_math.js"
// A counter of dependencies for calling run(). If we need to
// do asynchronous work before running, increment this and
// decrement it. Incrementing must happen in a place like
// Module.preRun (used by emcc to add file preloading).
// Note that you can add dependencies in preRun, even though
// it happens right before run - run will be postponed until
// the dependencies are met.
var runDependencies = 0;
var runDependencyWatcher = null;
var dependenciesFulfilled = null; // overridden to take different actions when all run dependencies are fulfilled
#if ASSERTIONS
var runDependencyTracking = {};
#endif
function getUniqueRunDependency(id) {
#if ASSERTIONS
var orig = id;
while (1) {
if (!runDependencyTracking[id]) return id;
id = orig + Math.random();
}
#endif
return id;
}
function addRunDependency(id) {
#if USE_PTHREADS
// We should never get here in pthreads (could no-op this out if called in pthreads, but that might indicate a bug in caller side,
// so good to be very explicit)
assert(!ENVIRONMENT_IS_PTHREAD, "addRunDependency cannot be used in a pthread worker");
#endif
runDependencies++;
if (Module['monitorRunDependencies']) {
Module['monitorRunDependencies'](runDependencies);
}
#if ASSERTIONS
if (id) {
assert(!runDependencyTracking[id]);
runDependencyTracking[id] = 1;
if (runDependencyWatcher === null && typeof setInterval !== 'undefined') {
// Check for missing dependencies every few seconds
runDependencyWatcher = setInterval(function() {
if (ABORT) {
clearInterval(runDependencyWatcher);
runDependencyWatcher = null;
return;
}
var shown = false;
for (var dep in runDependencyTracking) {
if (!shown) {
shown = true;
err('still waiting on run dependencies:');
}
err('dependency: ' + dep);
}
if (shown) {
err('(end of list)');
}
}, 10000);
}
} else {
err('warning: run dependency added without ID');
}
#endif
}
function removeRunDependency(id) {
runDependencies--;
if (Module['monitorRunDependencies']) {
Module['monitorRunDependencies'](runDependencies);
}
#if ASSERTIONS
if (id) {
assert(runDependencyTracking[id]);
delete runDependencyTracking[id];
} else {
err('warning: run dependency removed without ID');
}
#endif
if (runDependencies == 0) {
if (runDependencyWatcher !== null) {
clearInterval(runDependencyWatcher);
runDependencyWatcher = null;
}
if (dependenciesFulfilled) {
var callback = dependenciesFulfilled;
dependenciesFulfilled = null;
callback(); // can add another dependenciesFulfilled
}
}
}
Module["preloadedImages"] = {}; // maps url to image data
Module["preloadedAudios"] = {}; // maps url to audio data
#if WASM && MAIN_MODULE
Module["preloadedWasm"] = {}; // maps url to wasm instance exports
#endif
#if RELOCATABLE
{{{
(function() {
// add in RUNTIME_LINKED_LIBS, if provided
if (RUNTIME_LINKED_LIBS.length > 0) {
return "if (!Module['dynamicLibraries']) Module['dynamicLibraries'] = [];\n" +
"Module['dynamicLibraries'] = " + JSON.stringify(RUNTIME_LINKED_LIBS) + ".concat(Module['dynamicLibraries']);\n";
}
return '';
})()
}}}
addOnPreRun(function() {
function loadDynamicLibraries(libs) {
if (libs) {
libs.forEach(function(lib) {
// libraries linked to main never go away
loadDynamicLibrary(lib, {global: true, nodelete: true});
});
}
}
// if we can load dynamic libraries synchronously, do so, otherwise, preload
#if WASM
if (Module['dynamicLibraries'] && Module['dynamicLibraries'].length > 0 && !Module['readBinary']) {
// we can't read binary data synchronously, so preload
addRunDependency('preload_dynamicLibraries');
Promise.all(Module['dynamicLibraries'].map(function(lib) {
return loadDynamicLibrary(lib, {loadAsync: true, global: true, nodelete: true});
})).then(function() {
// we got them all, wonderful
removeRunDependency('preload_dynamicLibraries');
});
return;
}
#endif
loadDynamicLibraries(Module['dynamicLibraries']);
});
#if ASSERTIONS
function lookupSymbol(ptr) { // for a pointer, print out all symbols that resolve to it
var ret = [];
for (var i in Module) {
if (Module[i] === ptr) ret.push(i);
}
print(ptr + ' is ' + ret);
}
#endif
#endif
var memoryInitializer = null;
#if USE_PTHREADS && PTHREAD_HINT_NUM_CORES < 0
if (!ENVIRONMENT_IS_PTHREAD) addOnPreRun(function() {
addRunDependency('pthreads_querycores');
var bg = document.createElement('div');
bg.style = "position: absolute; top: 0%; left: 0%; width: 100%; height: 100%; background-color: black; z-index:1001; -moz-opacity: 0.8; opacity:.80; filter: alpha(opacity=80);";
var div = document.createElement('div');
var default_num_cores = navigator.hardwareConcurrency || 4;
var hwConcurrency = navigator.hardwareConcurrency ? ("says " + navigator.hardwareConcurrency) : "is not available";
var html = '<div style="width: 100%; text-align:center;"> Thread setup</div> <br /> Number of logical cores: <input type="number" style="width: 50px;" value="'
+ default_num_cores + '" min="1" max="32" id="thread_setup_num_logical_cores"></input> <br /><span style="font-size: 75%;">(<span style="font-family: monospace;">navigator.hardwareConcurrency</span> '
+ hwConcurrency + ')</span> <br />';
#if PTHREAD_POOL_SIZE < 0
html += 'PThread pool size: <input type="number" style="width: 50px;" value="'
+ default_num_cores + '" min="1" max="32" id="thread_setup_pthread_pool_size"></input> <br />';
#endif
html += ' <br /> <input type="button" id="thread_setup_button_go" value="Go"></input>';
div.innerHTML = html;
div.style = 'position: absolute; top: 35%; left: 35%; width: 30%; height: 150px; padding: 16px; border: 16px solid gray; background-color: white; z-index:1002; overflow: auto;';
document.body.appendChild(bg);
document.body.appendChild(div);
var goButton = document.getElementById('thread_setup_button_go');
goButton.onclick = function() {
var num_logical_cores = parseInt(document.getElementById('thread_setup_num_logical_cores').value);
_emscripten_force_num_logical_cores(num_logical_cores);
#if PTHREAD_POOL_SIZE < 0
var pthread_pool_size = parseInt(document.getElementById('thread_setup_pthread_pool_size').value);
PThread.allocateUnusedWorkers(pthread_pool_size, function() { removeRunDependency('pthreads_querycores'); });
#else
removeRunDependency('pthreads_querycores');
#endif
document.body.removeChild(bg);
document.body.removeChild(div);
}
});
#endif
#if PTHREAD_POOL_SIZE > 0
// To work around https://bugzilla.mozilla.org/show_bug.cgi?id=1049079, warm up a worker pool before starting up the application.
if (!ENVIRONMENT_IS_PTHREAD) addOnPreRun(function() { if (typeof SharedArrayBuffer !== 'undefined') { addRunDependency('pthreads'); PThread.allocateUnusedWorkers({{{PTHREAD_POOL_SIZE}}}, function() { removeRunDependency('pthreads'); }); }});
#endif
#if ASSERTIONS && !('$FS' in addedLibraryItems) && !ASMFS
// show errors on likely calls to FS when it was not included
var FS = {
error: function() {
abort('Filesystem support (FS) was not included. The problem is that you are using files from JS, but files were not used from C/C++, so filesystem support was not auto-included. You can force-include filesystem support with -s FORCE_FILESYSTEM=1');
},
init: function() { FS.error() },
createDataFile: function() { FS.error() },
createPreloadedFile: function() { FS.error() },
createLazyFile: function() { FS.error() },
open: function() { FS.error() },
mkdev: function() { FS.error() },
registerDevice: function() { FS.error() },
analyzePath: function() { FS.error() },
loadFilesFromDB: function() { FS.error() },
ErrnoError: function ErrnoError() { FS.error() },
};
Module['FS_createDataFile'] = FS.createDataFile;
Module['FS_createPreloadedFile'] = FS.createPreloadedFile;
#endif
#if CYBERDWARF
var cyberDWARFFile = '{{{ BUNDLED_CD_DEBUG_FILE }}}';
#endif
#include "URIUtils.js"
#if WASM
var wasmBinaryFile = '{{{ WASM_BINARY_FILE }}}';
if (!isDataURI(wasmBinaryFile)) {
wasmBinaryFile = locateFile(wasmBinaryFile);
}
function getBinary() {
try {
if (Module['wasmBinary']) {
return new Uint8Array(Module['wasmBinary']);
}
#if SUPPORT_BASE64_EMBEDDING
var binary = tryParseAsDataURI(wasmBinaryFile);
if (binary) {
return binary;
}
#endif
if (Module['readBinary']) {
return Module['readBinary'](wasmBinaryFile);
} else {
#if WASM_ASYNC_COMPILATION
throw "both async and sync fetching of the wasm failed";
#else
throw "sync fetching of the wasm failed: you can preload it to Module['wasmBinary'] manually, or emcc.py will do that for you when generating HTML (but not JS)";
#endif
}
}
catch (err) {
abort(err);
}
}
function getBinaryPromise() {
// if we don't have the binary yet, and have the Fetch api, use that
// in some environments, like Electron's render process, Fetch api may be present, but have a different context than expected, let's only use it on the Web
if (!Module['wasmBinary'] && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) && typeof fetch === 'function') {
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function(response) {
if (!response['ok']) {
throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";
}
return response['arrayBuffer']();
}).catch(function () {
return getBinary();
});
}
// Otherwise, getBinary should be able to get it synchronously
return new Promise(function(resolve, reject) {
resolve(getBinary());
});
}
#if LOAD_SOURCE_MAP
var wasmSourceMap;
#include "source_map_support.js"
#endif
#if 'emscripten_generate_pc' in addedLibraryItems
var wasmOffsetConverter;
#include "wasm_offset_converter.js"
#endif
// Create the wasm instance.
// Receives the wasm imports, returns the exports.
function createWasm(env) {
#if WASM2JS || AUTODEBUG
// wasm2js legalization of i64 support code may require these
// autodebug may also need them
env['setTempRet0'] = setTempRet0;
env['getTempRet0'] = getTempRet0;
#endif
#if AUTODEBUG
env['log_execution'] = function(loc) {
console.log('log_execution ' + loc);
};
env['get_i32'] = function(loc, index, value) {
console.log('get_i32 ' + [loc, index, value]);
return value;
};
env['get_i64'] = function(loc, index, low, high) {
console.log('get_i64 ' + [loc, index, low, high]);
env['setTempRet0'](high);
return low;
};
env['get_f32'] = function(loc, index, value) {
console.log('get_f32 ' + [loc, index, value]);
return value;
};
env['get_f64'] = function(loc, index, value) {
console.log('get_f64 ' + [loc, index, value]);
return value;
};
env['set_i32'] = function(loc, index, value) {
console.log('set_i32 ' + [loc, index, value]);
return value;
};
env['set_i64'] = function(loc, index, low, high) {
console.log('set_i64 ' + [loc, index, low, high]);
env['setTempRet0'](high);
return low;
};
env['set_f32'] = function(loc, index, value) {
console.log('set_f32 ' + [loc, index, value]);
return value;
};
env['set_f64'] = function(loc, index, value) {
console.log('set_f64 ' + [loc, index, value]);
return value;
};
env['load_ptr'] = function(loc, bytes, offset, ptr) {
console.log('load_ptr ' + [loc, bytes, offset, ptr]);
return ptr;
};
env['load_val_i32'] = function(loc, value) {
console.log('load_val_i32 ' + [loc, value]);
return value;
};
env['load_val_i64'] = function(loc, low, high) {
console.log('load_val_i64 ' + [loc, low, high]);
env['setTempRet0'](high);
return low;
};
env['load_val_f32'] = function(loc, value) {
console.log('loaload_val_i32d_ptr ' + [loc, value]);
return value;
};
env['load_val_f64'] = function(loc, value) {
console.log('load_val_f64 ' + [loc, value]);
return value;
};
env['store_ptr'] = function(loc, bytes, offset, ptr) {
console.log('store_ptr ' + [loc, bytes, offset, ptr]);
return ptr;
};
env['store_val_i32'] = function(loc, value) {
console.log('store_val_i32 ' + [loc, value]);
return value;
};
env['store_val_i64'] = function(loc, low, high) {
console.log('store_val_i64 ' + [loc, low, high]);
env['setTempRet0'](high);
return low;
};
env['store_val_f32'] = function(loc, value) {
console.log('loastore_val_i32d_ptr ' + [loc, value]);
return value;
};
env['store_val_f64'] = function(loc, value) {
console.log('store_val_f64 ' + [loc, value]);
return value;
};
#endif
// prepare imports
var info = {
'env': env
#if WASM_BACKEND == 0
,
'global': {
'NaN': NaN,
'Infinity': Infinity
},
'global.Math': Math,
'asm2wasm': asm2wasmImports
#endif
};
// Load the wasm module and create an instance of using native support in the JS engine.
// handle a generated wasm instance, receiving its exports and
// performing other necessary setup
function receiveInstance(instance, module) {
var exports = instance.exports;
// We want to modify the exports in some cases, however, in ES6 environments that
// may be illegal as the wasm exports are an ES6 export object, which is read-only.
// To work around that, we simply make a copy of them.
#if EXPORT_ES6 && (ASSERTIONS || BYSYNCIFY)
var originalExports = exports;
exports = {};
for (var x in originalExports) exports[x] = originalExports[x];
#endif
#if BYSYNCIFY
exports = Bysyncify.instrumentWasmExports(exports);
#endif
Module['asm'] = exports;
#if USE_PTHREADS
// Keep a reference to the compiled module so we can post it to the workers.
wasmModule = module;
// Instantiation is synchronous in pthreads and we assert on run dependencies.
if (!ENVIRONMENT_IS_PTHREAD) removeRunDependency('wasm-instantiate');
#else
removeRunDependency('wasm-instantiate');
#endif
}
#if USE_PTHREADS