forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsifier.js
564 lines (495 loc) · 25.5 KB
/
jsifier.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
// Copyright 2010 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.
//"use strict";
// Convert analyzed data to javascript. Everything has already been calculated
// before this stage, which just does the final conversion to JavaScript.
// Handy sets
var STRUCT_LIST = set('struct', 'list');
var addedLibraryItems = {};
var asmLibraryFunctions = [];
var allExternPrimitives = ['Math_floor', 'Math_abs', 'Math_sqrt', 'Math_pow', 'Math_cos', 'Math_sin', 'Math_tan', 'Math_acos', 'Math_asin', 'Math_atan', 'Math_atan2', 'Math_exp', 'Math_log', 'Math_ceil', 'Math_imul', 'Math_min', 'Math_max', 'Math_clz32', 'Math_fround',
'Int8Array', 'Uint8Array', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array'];
// Specifies the set of referenced built-in primitives such as Math.max etc.
var usedExternPrimitives = {};
var SETJMP_LABEL = -1;
var INDENTATION = ' ';
var functionStubSigs = {};
// Some JS-implemented library functions are proxied to be called on the main browser thread, if the Emscripten runtime is executing in a Web Worker.
// Each such proxied function is identified via an ordinal number (this is not the same namespace as function pointers in general).
var proxiedFunctionTable = ["null" /* Reserve index 0 for an undefined function*/];
// proxiedFunctionInvokers contains bodies of the functions that will perform the proxying. These
// are generated in a map to keep track which ones have already been emitted, to avoid outputting duplicates.
// map: pair(sig, syncOrAsync) -> function body
var proxiedFunctionInvokers = {};
// We include asm2wasm imports if the trap mode is 'js' (to call out to JS to do some math stuff).
// However, we always need some of them (like the frem import because % is in asm.js but not in wasm).
// But we can avoid emitting all the others in many cases.
var NEED_ALL_ASM2WASM_IMPORTS = BINARYEN_TRAP_MODE == 'js';
// Used internally. set when there is a main() function.
// Also set when in a linkable module, as the main() function might
// arrive from a dynamically-linked library, and not necessarily
// the current compilation unit.
// Also set for STANDALONE_WASM since the _start function is needed to call
// static ctors, even if there is no user main.
var HAS_MAIN = ('_main' in IMPLEMENTED_FUNCTIONS) || MAIN_MODULE || SIDE_MODULE || STANDALONE_WASM;
// Mangles the given C/JS side function name to assembly level function name (adds an underscore)
function mangleCSymbolName(f) {
return f[0] == '$' ? f.substr(1) : '_' + f;
}
// Reverses C/JS name mangling: _foo -> foo, and foo -> $foo.
function demangleCSymbolName(f) {
return f[0] == '_' ? f.substr(1) : '$' + f;
}
// JSifier
function JSify(data, functionsOnly) {
var mainPass = !functionsOnly;
var itemsDict = { type: [], GlobalVariableStub: [], functionStub: [], function: [], GlobalVariable: [], GlobalVariablePostSet: [] };
if (mainPass) {
// Add additional necessary items for the main pass. We can now do this since types are parsed (types can be used through
// generateStructInfo in library.js)
LibraryManager.load();
var libFuncsToInclude;
if (INCLUDE_FULL_LIBRARY) {
assert(!SIDE_MODULE, 'Cannot have both INCLUDE_FULL_LIBRARY and SIDE_MODULE set.')
libFuncsToInclude = (MAIN_MODULE || SIDE_MODULE) ? DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.slice(0) : [];
for (var key in LibraryManager.library) {
if (!key.match(/__(deps|postset|inline|asm|sig)$/)) {
libFuncsToInclude.push(key);
}
}
} else {
libFuncsToInclude = DEFAULT_LIBRARY_FUNCS_TO_INCLUDE;
}
libFuncsToInclude.forEach(function(ident) {
data.functionStubs.push({
ident: mangleCSymbolName(ident)
});
});
}
function processLibraryFunction(snippet, ident, finalName) {
// It is possible that when printing the function as a string on Windows, the js interpreter we are in returns the string with Windows
// line endings \r\n. This is undesirable, since line endings are managed in the form \n in the output for binary file writes, so
// make sure the endings are uniform.
snippet = snippet.toString().replace(/\r\n/gm,"\n");
assert(snippet.indexOf('XXX missing C define') == -1,
'Trying to include a library function with missing C defines: ' + finalName + ' | ' + snippet);
// name the function; overwrite if it's already named
snippet = snippet.replace(/function(?:\s+([^(]+))?\s*\(/, 'function ' + finalName + '(');
// Apply special js library debug modes
if (!LibraryManager.library[ident + '__asm']) {
// apply LIBRARY_DEBUG if relevant
if (LIBRARY_DEBUG) {
snippet = modifyFunction(snippet, function(name, args, body) {
return 'function ' + name + '(' + args + ') {\n' +
'var ret = (function() { if (runtimeDebug) err("[library call:' + finalName + ': " + Array.prototype.slice.call(arguments).map(prettyPrint) + "]");\n' +
body +
'}).apply(this, arguments); if (runtimeDebug && typeof ret !== "undefined") err(" [ return:" + prettyPrint(ret)); return ret; \n}\n';
});
}
}
return snippet;
}
// functionStub
function functionStubHandler(item) {
// In LLVM, exceptions generate a set of functions of form __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc.
// where the number specifies the number of arguments. In Emscripten, route all these to a single function '__cxa_find_matching_catch'
// that variadically processes all of these functions using JS 'arguments' object.
if (item.ident.startsWith('___cxa_find_matching_catch_')) {
if (DISABLE_EXCEPTION_THROWING) {
error('DISABLE_EXCEPTION_THROWING was set (likely due to -fno-exceptions), which means no C++ exception throwing support code is linked in, but exception catching code appears. Either do not set DISABLE_EXCEPTION_THROWING (if you do want exception throwing) or compile all source files with -fno-except (so that no exceptions support code is required); also make sure DISABLE_EXCEPTION_CATCHING is set to the right value - if you want exceptions, it should be off, and vice versa.');
return;
}
var num = +item.ident.split('_').slice(-1)[0];
addCxaCatch(num);
// Continue, with the code below emitting the proper JavaScript based on
// what we just added to the library.
}
function addFromLibrary(ident) {
if (ident in addedLibraryItems) return '';
addedLibraryItems[ident] = true;
// dependencies can be JS functions, which we just run
if (typeof ident == 'function') return ident();
// don't process any special identifiers. These are looked up when processing the base name of the identifier.
if (ident.endsWith('__sig') || ident.endsWith('__proxy') || ident.endsWith('__asm') || ident.endsWith('__inline') || ident.endsWith('__deps') || ident.endsWith('__postset')) {
return '';
}
var finalName = mangleCSymbolName(ident);
// if the function was implemented in compiled code, we just need to export it so we can reach it from JS
if (finalName in IMPLEMENTED_FUNCTIONS) {
EXPORTED_FUNCTIONS[finalName] = 1;
// stop here: we don't need to add anything from our js libraries, not even deps, compiled code is on it
return '';
}
// Don't replace implemented functions with library ones (which can happen when we add dependencies).
// Note: We don't return the dependencies here. Be careful not to end up where this matters
if (finalName in Functions.implementedFunctions) return '';
var noExport = false;
if (allExternPrimitives.indexOf(ident) != -1) {
usedExternPrimitives[ident] = 1;
return;
} else if ((!LibraryManager.library.hasOwnProperty(ident) && !LibraryManager.library.hasOwnProperty(ident + '__inline')) || SIDE_MODULE) {
if (!(finalName in IMPLEMENTED_FUNCTIONS) && !LINKABLE) {
if (ERROR_ON_UNDEFINED_SYMBOLS) {
error('undefined symbol: ' + ident);
warnOnce('To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`')
} else if (VERBOSE || WARN_ON_UNDEFINED_SYMBOLS) {
warn('undefined symbol: ' + ident);
}
}
if (!RELOCATABLE) {
// emit a stub that will fail at runtime
LibraryManager.library[ident] = new Function("err('missing function: " + ident + "'); abort(-1);");
} else {
var isGlobalAccessor = ident.startsWith('g$');
var realIdent = ident;
if (isGlobalAccessor) {
realIdent = realIdent.substr(2);
}
var target = (SIDE_MODULE ? 'parent' : '') + "Module['" + mangleCSymbolName(realIdent) + "']";
var assertion = '';
if (ASSERTIONS) {
var what = 'function';
if (isGlobalAccessor) {
what = 'global';
}
assertion += 'if (!' + target + ') abort("external ' + what + ' \'' + realIdent + '\' is missing. perhaps a side module was not linked in? if this function was expected to arrive from a system library, try to build the MAIN_MODULE with EMCC_FORCE_STDLIBS=1 in the environment");\n';
}
var functionBody;
if (isGlobalAccessor) {
functionBody = assertion + "return " + target + ";"
} else {
functionBody = assertion + "return " + target + ".apply(null, arguments);";
}
LibraryManager.library[ident] = new Function(functionBody);
if (SIDE_MODULE) {
// no dependencies, just emit the thunk
Functions.libraryFunctions[finalName] = 1;
return processLibraryFunction(LibraryManager.library[ident], ident, finalName);
}
noExport = true;
}
}
var original = LibraryManager.library[ident];
var snippet = original;
var redirectedIdent = null;
var deps = LibraryManager.library[ident + '__deps'] || [];
deps.forEach(function(dep) {
if (typeof snippet === 'string' && !(dep in LibraryManager.library)) warn('missing library dependency ' + dep + ', make sure you are compiling with the right options (see #ifdefs in src/library*.js)');
});
var isFunction = false;
if (typeof snippet === 'string') {
if (snippet[0] != '=') {
var target = LibraryManager.library[snippet];
if (target) {
// Redirection for aliases. We include the parent, and at runtime make ourselves equal to it.
// This avoid having duplicate functions with identical content.
redirectedIdent = snippet;
deps.push(snippet);
snippet = mangleCSymbolName(snippet);
}
// In asm, we need to know about library functions. If there is a target, though, then no
// need to consider this a library function - we will call directly to it anyhow
if (!redirectedIdent && (typeof target == 'function' || /Math_\w+/.exec(snippet))) {
Functions.libraryFunctions[finalName] = 1;
}
}
} else if (typeof snippet === 'object') {
snippet = stringifyWithFunctions(snippet);
} else if (typeof snippet === 'function') {
isFunction = true;
snippet = processLibraryFunction(snippet, ident, finalName);
Functions.libraryFunctions[finalName] = 1;
}
var postsetId = ident + '__postset';
var postset = LibraryManager.library[postsetId];
if (postset) {
// A postset is either code to run right now, or some text we should emit.
// If it's code, it may return some text to emit as well.
if (typeof postset === 'function') {
postset = postset();
}
if (postset && !addedLibraryItems[postsetId] && !SIDE_MODULE) {
addedLibraryItems[postsetId] = true;
itemsDict.GlobalVariablePostSet.push({
JS: postset + ';'
});
}
}
if (redirectedIdent) {
deps = deps.concat(LibraryManager.library[redirectedIdent + '__deps'] || []);
}
// In asm, dependencies implemented in C might be needed by JS library functions.
// We don't know yet if they are implemented in C or not. To be safe, export such
// special cases.
[LIBRARY_DEPS_TO_AUTOEXPORT].forEach(function(special) {
deps.forEach(function(dep) {
if (dep == special && !EXPORTED_FUNCTIONS[dep]) {
EXPORTED_FUNCTIONS[dep] = 1;
}
});
});
if (VERBOSE) printErr('adding ' + finalName + ' and deps ' + deps + ' : ' + (snippet + '').substr(0, 40));
var depsText = (deps ? '\n' + deps.map(addFromLibrary).filter(function(x) { return x != '' }).join('\n') : '');
var contentText;
if (isFunction) {
// Emit the body of a JS library function.
var proxyingMode = LibraryManager.library[ident + '__proxy'];
if (USE_PTHREADS && proxyingMode) {
if (proxyingMode !== 'sync' && proxyingMode !== 'async') {
throw 'Invalid proxyingMode ' + ident + '__proxy: \'' + proxyingMode + '\' specified!';
}
var sync = proxyingMode === 'sync';
assert(typeof original === 'function');
contentText = modifyFunction(snippet, function(name, args, body) {
return 'function ' + name + '(' + args + ') {\n' +
'if (ENVIRONMENT_IS_PTHREAD) return _emscripten_proxy_to_main_thread_js(' + proxiedFunctionTable.length + ', ' + (+sync) + (args ? ', ' : '') + args + ');\n' + body + '}\n';
});
proxiedFunctionTable.push(finalName);
} else {
contentText = snippet; // Regular JS function that will be executed in the context of the calling thread.
}
} else if (typeof snippet === 'string' && snippet.indexOf(';') == 0) {
// In JS libraries
// foo: ';[code here verbatim]'
// emits
// 'var foo;[code here verbatim];'
contentText = 'var ' + finalName + snippet;
if (snippet[snippet.length-1] != ';' && snippet[snippet.length-1] != '}') contentText += ';';
} else {
// In JS libraries
// foo: '=[value]'
// emits
// 'var foo = [value];'
if (typeof snippet === 'string' && snippet[0] == '=') snippet = snippet.substr(1);
contentText = 'var ' + finalName + '=' + snippet + ';';
}
var sig = LibraryManager.library[ident + '__sig'];
if (isFunction && sig && LibraryManager.library[ident + '__asm']) {
// asm library function, add it as generated code alongside the generated code
Functions.implementedFunctions[finalName] = sig;
asmLibraryFunctions.push(contentText);
contentText = ' ';
Functions.libraryFunctions[finalName] = 2;
noExport = true; // if it needs to be exported, that will happen in emscripten.py
}
// asm module exports are done in emscripten.py, after the asm module is ready. Here
// we also export library methods as necessary.
if ((EXPORT_ALL || (finalName in EXPORTED_FUNCTIONS)) && !noExport) {
contentText += '\nModule["' + finalName + '"] = ' + finalName + ';';
}
if (!LibraryManager.library[ident + '__asm']) {
// If we are not an asm library func, and we have a dep that is, then we need to call
// into the asm module to reach that dep. so it must be exported from the asm module.
// We set EXPORTED_FUNCTIONS here to tell emscripten.py to do that.
deps.forEach(function(dep) {
if (LibraryManager.library[dep + '__asm']) {
EXPORTED_FUNCTIONS[mangleCSymbolName(dep)] = 0;
}
});
}
return depsText + contentText;
}
itemsDict.functionStub.push(item);
var shortident = demangleCSymbolName(item.ident);
// If this is not linkable, anything not in the library is definitely missing
if (item.ident in DEAD_FUNCTIONS) {
if (LibraryManager.library[shortident + '__asm']) {
warn('cannot kill asm library function ' + item.ident);
} else {
LibraryManager.library[shortident] = new Function("err('dead function: " + shortident + "'); abort(-1);");
delete LibraryManager.library[shortident + '__inline'];
delete LibraryManager.library[shortident + '__deps'];
}
}
item.JS = addFromLibrary(shortident);
}
// Final combiner
function finalCombiner() {
var splitPostSets = splitter(itemsDict.GlobalVariablePostSet, function(x) { return x.ident && x.dependencies });
itemsDict.GlobalVariablePostSet = splitPostSets.leftIn;
var orderedPostSets = splitPostSets.splitOut;
var limit = orderedPostSets.length * orderedPostSets.length;
for (var i = 0; i < orderedPostSets.length; i++) {
for (var j = i+1; j < orderedPostSets.length; j++) {
if (orderedPostSets[j].ident in orderedPostSets[i].dependencies) {
var temp = orderedPostSets[i];
orderedPostSets[i] = orderedPostSets[j];
orderedPostSets[j] = temp;
i--;
limit--;
assert(limit > 0, 'Could not sort postsets!');
break;
}
}
}
itemsDict.GlobalVariablePostSet = itemsDict.GlobalVariablePostSet.concat(orderedPostSets);
//
if (!mainPass) {
if (!Variables.generatedGlobalBase) {
Variables.generatedGlobalBase = true;
// Globals are done, here is the rest of static memory
if (SIDE_MODULE) {
print('gb = alignMemory(getMemory({{{ STATIC_BUMP }}} + ' + MAX_GLOBAL_ALIGN + '), ' + MAX_GLOBAL_ALIGN + ' || 1);\n');
// The static area consists of explicitly initialized data, followed by zero-initialized data.
// The latter may need zeroing out if the MAIN_MODULE has already used this memory area before
// dlopen'ing the SIDE_MODULE. Since we don't know the size of the explicitly initialized data
// here, we just zero the whole thing, which is suboptimal, but should at least resolve bugs
// from uninitialized memory.
print('for (var i = gb; i < gb + {{{ STATIC_BUMP }}}; ++i) HEAP8[i] = 0;\n');
}
// emit "metadata" in a comment. FIXME make this nicer
print('// STATICTOP = STATIC_BASE + ' + Runtime.alignMemory(Variables.nextIndexedOffset) + ';\n');
}
var generated = itemsDict.function.concat(itemsDict.type).concat(itemsDict.GlobalVariableStub).concat(itemsDict.GlobalVariable);
print(generated.map(function(item) { return item.JS; }).join('\n'));
if (memoryInitialization.length > 0) {
// apply postsets directly into the big memory initialization
itemsDict.GlobalVariablePostSet = itemsDict.GlobalVariablePostSet.filter(function(item) {
var m;
if (m = /^HEAP([\dFU]+)\[([()>\d]+)\] *= *([()|\d{}\w_' ]+);?$/.exec(item.JS)) {
var type = getTypeFromHeap(m[1]);
var bytes = Runtime.getNativeTypeSize(type);
var target = eval(m[2]) << log2(bytes);
var value = m[3];
try {
value = eval(value);
} catch(e) {
// possibly function table {{{ FT_* }}} etc.
if (value.indexOf('{{ ') < 0) return true;
}
writeInt8s(memoryInitialization, target - Runtime.GLOBAL_BASE, value, type);
return false;
}
return true;
});
// write out the singleton big memory initialization value
if (USE_PTHREADS) {
print('if (!ENVIRONMENT_IS_PTHREAD) {') // Pthreads should not initialize memory again, since it's shared with the main thread.
}
print('/* memory initializer */ ' + makePointer(memoryInitialization, null, 'ALLOC_NONE', 'i8', 'GLOBAL_BASE' + (SIDE_MODULE ? '+H_BASE' : ''), true));
if (USE_PTHREADS) {
print('}')
}
} else {
print('/* no memory initializer */'); // test purposes
}
if (!SIDE_MODULE && !WASM_BACKEND) {
if (USE_PTHREADS) {
print('var tempDoublePtr;');
print('if (!ENVIRONMENT_IS_PTHREAD) tempDoublePtr = ' + makeStaticAlloc(12) + ';');
} else {
print('var tempDoublePtr = ' + makeStaticAlloc(8) + '');
}
if (ASSERTIONS) print('assert(tempDoublePtr % 8 == 0);');
print('\nfunction copyTempFloat(ptr) { // functions, because inlining this code increases code size too much');
print(' HEAP8[tempDoublePtr] = HEAP8[ptr];');
print(' HEAP8[tempDoublePtr+1] = HEAP8[ptr+1];');
print(' HEAP8[tempDoublePtr+2] = HEAP8[ptr+2];');
print(' HEAP8[tempDoublePtr+3] = HEAP8[ptr+3];');
print('}\n');
print('function copyTempDouble(ptr) {');
print(' HEAP8[tempDoublePtr] = HEAP8[ptr];');
print(' HEAP8[tempDoublePtr+1] = HEAP8[ptr+1];');
print(' HEAP8[tempDoublePtr+2] = HEAP8[ptr+2];');
print(' HEAP8[tempDoublePtr+3] = HEAP8[ptr+3];');
print(' HEAP8[tempDoublePtr+4] = HEAP8[ptr+4];');
print(' HEAP8[tempDoublePtr+5] = HEAP8[ptr+5];');
print(' HEAP8[tempDoublePtr+6] = HEAP8[ptr+6];');
print(' HEAP8[tempDoublePtr+7] = HEAP8[ptr+7];');
print('}\n');
}
print('// {{PRE_LIBRARY}}\n'); // safe to put stuff here that statically allocates
return;
}
var shellFile = SHELL_FILE ? SHELL_FILE : (SIDE_MODULE ? 'shell_sharedlib.js' : (MINIMAL_RUNTIME ? 'shell_minimal.js' : 'shell.js'));
var shellParts = read(shellFile).split('{{BODY}}');
print(processMacros(preprocess(shellParts[0], shellFile)));
var pre;
if (SIDE_MODULE) {
pre = processMacros(preprocess(read('preamble_sharedlib.js'), 'preamble_sharedlib.js'));
} else if (MINIMAL_RUNTIME) {
pre = processMacros(preprocess(read('preamble_minimal.js'), 'preamble_minimal.js'));
} else {
pre = processMacros(preprocess(read('support.js'), 'support.js')) +
processMacros(preprocess(read('preamble.js'), 'preamble.js'));
}
print(pre);
// Print out global variables and postsets TODO: batching
var legalizedI64sDefault = legalizedI64s;
legalizedI64s = false;
var globalsData = {functionStubs: []}
JSify(globalsData, true);
globalsData = null;
var generated = itemsDict.functionStub.concat(itemsDict.GlobalVariablePostSet);
generated.forEach(function(item) { print(indentify(item.JS || '', 2)); });
legalizedI64s = legalizedI64sDefault;
if (!SIDE_MODULE) {
if (USE_PTHREADS) {
print('\n // proxiedFunctionTable specifies the list of functions that can be called either synchronously or asynchronously from other threads in postMessage()d or internally queued events. This way a pthread in a Worker can synchronously access e.g. the DOM on the main thread.')
print('\nvar proxiedFunctionTable = [' + proxiedFunctionTable.join() + '];\n');
}
}
if (!MINIMAL_RUNTIME) {
print('var ASSERTIONS = ' + !!ASSERTIONS + ';\n');
print(preprocess(read('arrayUtils.js')));
}
if (SUPPORT_BASE64_EMBEDDING) {
print(preprocess(read('base64Utils.js')));
}
var usedExternPrimitiveNames = Object.keys(usedExternPrimitives);
if (usedExternPrimitiveNames.length > 0) {
print('// ASM_LIBRARY EXTERN PRIMITIVES: ' + usedExternPrimitiveNames.join(',') + '\n');
}
if (asmLibraryFunctions.length > 0) {
print('// ASM_LIBRARY FUNCTIONS');
function fix(f) { // fix indenting to not confuse js optimizer
f = f.substr(f.indexOf('f')); // remove initial spaces before 'function'
f = f.substr(0, f.lastIndexOf('\n')+1); // remove spaces and last } XXX assumes function has multiple lines
return f + '}'; // add unindented } to match function
}
print(asmLibraryFunctions.map(fix).join('\n'));
}
if (abortExecution) throw Error('Aborting compilation due to previous errors');
// This is the main 'post' pass. Print out the generated code that we have here, together with the
// rest of the output that we started to print out earlier (see comment on the
// "Final shape that will be created").
print('// EMSCRIPTEN_END_FUNCS\n');
if (HEADLESS) {
print('if (!ENVIRONMENT_IS_WEB) {');
print(read('headlessCanvas.js'));
print('\n');
print(read('headless.js').replace("'%s'", "'http://emscripten.org'").replace("'?%s'", "''").replace("'?%s'", "'/'").replace('%s,', 'null,').replace('%d', '0'));
print('}');
}
if (PROXY_TO_WORKER) {
print('if (ENVIRONMENT_IS_WORKER) {\n');
print(read('webGLWorker.js'));
print(processMacros(preprocess(read('proxyWorker.js'), 'proxyWorker.js')));
print('}');
}
if (DETERMINISTIC) {
print(read('deterministic.js'));
}
var postFile = SIDE_MODULE ? 'postamble_sharedlib.js' : (MINIMAL_RUNTIME ? 'postamble_minimal.js' : 'postamble.js');
var postParts = processMacros(preprocess(read(postFile), postFile)).split('{{GLOBAL_VARS}}');
print(postParts[0]);
print(postParts[1]);
var shellParts = read(shellFile).split('{{BODY}}');
print(processMacros(preprocess(shellParts[1], shellFile)));
// Print out some useful metadata
if (RUNNING_JS_OPTS) {
var generatedFunctions = JSON.stringify(keys(Functions.implementedFunctions));
if (RUNNING_JS_OPTS) {
print('// EMSCRIPTEN_GENERATED_FUNCTIONS: ' + generatedFunctions + '\n');
}
}
PassManager.serialize();
}
// Data
if (mainPass) {
data.functionStubs.forEach(functionStubHandler);
}
finalCombiner();
}