-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathjsifier.js
559 lines (497 loc) · 21.1 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
/**
* @license
* Copyright 2010 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
// "use strict";
// Convert analyzed data to javascript. Everything has already been calculated
// before this stage, which just does the final conversion to JavaScript.
global.addedLibraryItems = {};
// 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).
global.proxiedFunctionTable = ['null'/* Reserve index 0 for an undefined function*/];
// Mangles the given C/JS side function name to assembly level function name (adds an underscore)
function mangleCSymbolName(f) {
if (f === '__main_argc_argv') {
f = 'main';
}
return f[0] == '$' ? f.substr(1) : '_' + f;
}
// Splits out items that pass filter. Returns also the original sans the filtered
function splitter(array, filter) {
const splitOut = array.filter(filter);
const leftIn = array.filter((x) => !filter(x));
return {leftIn: leftIn, splitOut: splitOut};
}
// Functions that start with '$' should not be exported to the wasm module.
// They are intended to be exclusive to JS code only.
function isJsOnlySymbol(symbol) {
return symbol[0] == '$';
}
function escapeJSONKey(x) {
if (/^[\d\w_]+$/.exec(x) || x[0] === '"' || x[0] === "'") return x;
assert(!x.includes("'"), 'cannot have internal single quotes in keys: ' + x);
return "'" + x + "'";
}
function stringifyWithFunctions(obj) {
if (typeof obj == 'function') return obj.toString();
if (obj === null || typeof obj != 'object') return JSON.stringify(obj);
if (Array.isArray(obj)) {
return '[' + obj.map(stringifyWithFunctions).join(',') + ']';
}
return '{' + Object.keys(obj).map((key) => escapeJSONKey(key) + ':' + stringifyWithFunctions(obj[key])).join(',') + '}';
}
function isDefined(symName) {
if (WASM_EXPORTS.has(symName) || SIDE_MODULE_EXPORTS.has(symName)) {
return true;
}
if (symName == '__main_argc_argv' && SIDE_MODULE_EXPORTS.has('main')) {
return true;
}
// 'invoke_' symbols are created at runtime in libary_dylink.py so can
// always be considered as defined.
if (RELOCATABLE && symName.startsWith('invoke_')) {
return true;
}
return false;
}
function runJSify(symbolsOnly = false) {
const libraryItems = [];
const symbolDeps = {};
let postSets = [];
LibraryManager.load();
const symbolsNeeded = DEFAULT_LIBRARY_FUNCS_TO_INCLUDE;
for (const sym of EXPORTED_RUNTIME_METHODS) {
if ('$' + sym in LibraryManager.library) {
symbolsNeeded.push('$' + sym);
}
}
if (INCLUDE_FULL_LIBRARY) {
for (const key of Object.keys(LibraryManager.library)) {
if (!isJsLibraryConfigIdentifier(key)) {
symbolsNeeded.push(key);
}
}
}
function convertPointerParams(snippet, sig) {
// Automatically convert any incoming pointer arguments from BigInt
// to double (this limits the range to int53).
// And convert the return value if the function returns a pointer.
return modifyFunction(snippet, (name, args, body) => {
let argLines = args.split('\n');
argLines = argLines.map((line) => line.split('//')[0]);
const argNames = argLines.join(' ').split(',').map((name) => name.trim());
let newArgs = [];
let argConvertions = '';
for (let i = 1; i < sig.length; i++) {
const name = argNames[i - 1];
if (sig[i] == 'p') {
argConvertions += ` ${name} = Number(${name});\n`;
newArgs.push(`Number(${name})`);
} else {
newArgs.push(name);
}
}
if (sig[0] == 'p') {
// For functions that return a pointer we need to convert
// the return value too, which means we need to wrap the
// body in an inner function.
newArgs = newArgs.join(',');
return `\
function ${name}(${args}) {
var ret = ((${args}) => { ${body} })(${newArgs});
return BigInt(ret);
}`;
}
// Otherwise no inner function is needed and we covert the arguments
// before executing the function body.
return `\
function ${name}(${args}) {
${argConvertions}
${body};
}`;
});
}
function processLibraryFunction(snippet, symbol, mangled, deps) {
// 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');
// name the function; overwrite if it's already named
snippet = snippet.replace(/function(?:\s+([^(]+))?\s*\(/, 'function ' + mangled + '(');
// apply LIBRARY_DEBUG if relevant
if (LIBRARY_DEBUG && !isJsOnlySymbol(symbol)) {
snippet = modifyFunction(snippet, (name, args, body) => `\
function ${name}(${args}) {
var ret = (function() { if (runtimeDebug) err("[library call:${mangled}: " + Array.prototype.slice.call(arguments).map(prettyPrint) + "]");
${body}
}).apply(this, arguments);
if (runtimeDebug && typeof ret != "undefined") err(" [ return:" + prettyPrint(ret));
return ret;
}`);
}
if (SHARED_MEMORY) {
const proxyingMode = LibraryManager.library[symbol + '__proxy'];
if (proxyingMode) {
if (proxyingMode !== 'sync' && proxyingMode !== 'async') {
throw new Error(`Invalid proxyingMode ${symbol}__proxy: '${proxyingMode}' specified!`);
}
const sync = proxyingMode === 'sync';
if (PTHREADS) {
snippet = modifyFunction(snippet, (name, args, body) => `
function ${name}(${args}) {
if (ENVIRONMENT_IS_PTHREAD)
return _emscripten_proxy_to_main_thread_js(${proxiedFunctionTable.length}, ${+sync}${args ? ', ' : ''}${args});
${body}
}\n`);
} else if (WASM_WORKERS && ASSERTIONS) {
// In ASSERTIONS builds add runtime checks that proxied functions are not attempted to be called in Wasm Workers
// (since there is no automatic proxying architecture available)
snippet = modifyFunction(snippet, (name, args, body) => `
function ${name}(${args}) {
assert(!ENVIRONMENT_IS_WASM_WORKER, "Attempted to call proxied function '${name}' in a Wasm Worker, but in Wasm Worker enabled builds, proxied function architecture is not available!");
${body}
}\n`);
}
proxiedFunctionTable.push(mangled);
}
}
if (MEMORY64) {
const sig = LibraryManager.library[symbol + '__sig'];
if (sig && sig.includes('p')) {
snippet = convertPointerParams(snippet, sig);
}
}
return snippet;
}
function addImplicitDeps(snippet, deps) {
// There are some common dependencies that we inject automatically by
// conservatively scanning the input functions for their usage.
// Specifically, these are dependencies that are automatically generated by
// the {{{ makeDynCall }}}, and {{{ runtimeKeepalivePush/Pop }}} macros.
const autoDeps = [
'getDynCaller',
'getWasmTableEntry',
'runtimeKeepalivePush',
'runtimeKeepalivePop',
];
for (const dep of autoDeps) {
if (snippet.includes(dep + '(')) {
deps.push('$' + dep);
}
}
}
function itemHandler(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.mangled.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;
}
const num = +item.mangled.split('_').slice(-1)[0];
addCxaCatch(num);
// Continue, with the code below emitting the proper JavaScript based on
// what we just added to the library.
}
const TOP_LEVEL = 'top-level compiled C/C++ code';
function addFromLibrary(item, dependent) {
// dependencies can be JS functions, which we just run
if (typeof item == 'function') {
return item();
}
const symbol = item.symbol;
const mangled = item.mangled;
if (symbol in addedLibraryItems) {
return;
}
addedLibraryItems[symbol] = true;
// don't process any special identifiers. These are looked up when
// processing the base name of the identifier.
if (isJsLibraryConfigIdentifier(symbol)) {
return;
}
const deps = LibraryManager.library[symbol + '__deps'] || [];
if (!Array.isArray(deps)) {
error(`JS library directive ${symbol}__deps=${deps.toString()} is of type ${typeof deps}, but it should be an array!`);
return;
}
if (symbolsOnly) {
if (!isJsOnlySymbol(symbol) && LibraryManager.library.hasOwnProperty(symbol)) {
externalDeps = deps.filter((d) => !isJsOnlySymbol(d) && !(d in LibraryManager.library) && typeof d === 'string');
symbolDeps[symbol] = externalDeps;
}
return;
}
// if the function was implemented in compiled code, there is no need to
// include the js version
if (WASM_EXPORTS.has(symbol)) {
return;
}
// This gets set to true in the case of dynamic linking for symbols that
// are undefined in the main module. In this case we create a stub that
// will resolve the correct symbol at runtime, or assert if its missing.
let isStub = false;
if (!LibraryManager.library.hasOwnProperty(symbol)) {
const isWeakImport = WEAK_IMPORTS.has(symbol);
if (!isDefined(symbol) && !isWeakImport) {
if (PROXY_TO_PTHREAD && !MAIN_MODULE && symbol == '__main_argc_argv') {
error('PROXY_TO_PTHREAD proxies main() for you, but no main exists');
return;
}
let undefinedSym = symbol;
if (symbol === '__main_argc_argv') {
undefinedSym = 'main/__main_argc_argv';
}
let msg = 'undefined symbol: ' + undefinedSym;
if (dependent) msg += ` (referenced by ${dependent})`;
if (ERROR_ON_UNDEFINED_SYMBOLS) {
error(msg);
warnOnce('To disable errors for undefined symbols use `-sERROR_ON_UNDEFINED_SYMBOLS=0`');
warnOnce(mangled + ' may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library');
} else if (VERBOSE || WARN_ON_UNDEFINED_SYMBOLS) {
warn(msg);
}
if (symbol === '__main_argc_argv' && STANDALONE_WASM) {
warn('To build in STANDALONE_WASM mode without a main(), use emcc --no-entry');
}
}
if (!RELOCATABLE) {
// emit a stub that will fail at runtime
LibraryManager.library[symbol] = new Function(`err('missing function: ${symbol}'); abort(-1);`);
// We have already warned/errored about this function, so for the purposes of Closure use, mute all type checks
// regarding this function, marking ot a variadic function that can take in anything and return anything.
// (not useful to warn/error multiple times)
LibraryManager.library[symbol + '__docs'] = '/** @type {function(...*):?} */';
} else {
// Create a stub for this symbol which can later be replaced by the
// dynamic linker. If this stub is called before the symbol is
// resolved assert in debug builds or trap in release builds.
if (ASYNCIFY) {
// See the definition of asyncifyStubs in preamble.js for why this
// is needed.
target = `asyncifyStubs['${symbol}']`;
} else {
target = `wasmImports['${symbol}']`;
}
let assertion = '';
if (ASSERTIONS) {
assertion += `if (!${target} || ${target}.stub) abort("external symbol '${symbol}' 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`;
}
const functionBody = assertion + `return ${target}.apply(null, arguments);`;
LibraryManager.library[symbol] = new Function(functionBody);
isStub = true;
}
}
librarySymbols.push(mangled);
const original = LibraryManager.library[symbol];
let snippet = original;
const isUserSymbol = LibraryManager.library[symbol + '__user'];
deps.forEach((dep) => {
if (typeof snippet == 'string' && !(dep in LibraryManager.library)) {
warn(`missing library dependency ${dep}, make sure you are compiling with the right options (see #if in src/library*.js)`);
}
if (isUserSymbol && LibraryManager.library[dep + '__internal']) {
warn(`user library symbol '${symbol}' depends on internal symbol '${dep}'`);
}
});
let isFunction = false;
if (typeof snippet == 'string') {
if (snippet[0] != '=') {
const 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.
const redirectedTarget = snippet;
deps.push(redirectedTarget);
snippet = mangleCSymbolName(redirectedTarget);
}
}
} else if (typeof snippet == 'object') {
snippet = stringifyWithFunctions(snippet);
addImplicitDeps(snippet, deps);
} else if (typeof snippet == 'function') {
isFunction = true;
snippet = processLibraryFunction(snippet, symbol, mangled, deps);
addImplicitDeps(snippet, deps);
}
const postsetId = symbol + '__postset';
let 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]) {
addedLibraryItems[postsetId] = true;
postSets.push({
JS: postset + ';',
});
}
}
if (VERBOSE) {
printErr(`adding ${mangled} and deps ${deps} : ` + (snippet + '').substr(0, 40));
}
const deps_list = deps.join("','");
const identDependents = symbol + `__deps: ['${deps_list}']`;
function addDependency(dep) {
if (typeof dep != 'function') {
dep = {symbol: dep, mangled: mangleCSymbolName(dep)};
}
return addFromLibrary(dep, `${identDependents}, referenced by ${dependent}`);
}
let contentText;
if (isFunction) {
// Emit the body of a JS library function.
if ((USE_ASAN || USE_LSAN || UBSAN_RUNTIME) && LibraryManager.library[symbol + '__noleakcheck']) {
contentText = modifyFunction(snippet, (name, args, body) => `
function ${name}(${args}) {
return withBuiltinMalloc(function() {
${body}
});
}\n`);
deps.push('$withBuiltinMalloc');
} else {
contentText = snippet; // Regular JS function that will be executed in the context of the calling thread.
}
} else if (typeof snippet == 'string' && snippet.startsWith(';')) {
// In JS libraries
// foo: ';[code here verbatim]'
// emits
// 'var foo;[code here verbatim];'
contentText = 'var ' + mangled + 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 ${mangled} = ${snippet};`;
}
const sig = LibraryManager.library[symbol + '__sig'];
// 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 || EXPORTED_FUNCTIONS.has(mangled)) && !isStub) {
contentText += `\nModule["${mangled}"] = ${mangled};`;
}
// Relocatable code needs signatures to create proper wrappers. Stack
// switching needs signatures so we can create a proper
// WebAssembly.Function with the signature for the Promise API.
// TODO: For asyncify we could only add the signatures we actually need,
// of async imports/exports.
if (sig && (RELOCATABLE || ASYNCIFY == 2)) {
contentText += `\n${mangled}.sig = '${sig}';`;
}
if (isStub) {
contentText += `\n${mangled}.stub = true;`;
if (ASYNCIFY) {
contentText += `\nasyncifyStubs['${symbol}'] = undefined;`;
}
}
let commentText = '';
if (LibraryManager.library[symbol + '__docs']) {
commentText = LibraryManager.library[symbol + '__docs'] + '\n';
}
const depsText = (deps ? deps.map(addDependency).filter((x) => x != '').join('\n') + '\n' : '');
return depsText + commentText + contentText;
}
libraryItems.push(item);
item.JS = addFromLibrary(item, TOP_LEVEL);
}
function includeFile(fileName) {
print(`// include: ${fileName}`);
print(processMacros(preprocess(fileName)));
print(`// end include: ${fileName}`);
}
function includeFileRaw(fileName) {
print(`// include: ${fileName}`);
print(read(fileName));
print(`// end include: ${fileName}`);
}
function finalCombiner() {
const splitPostSets = splitter(postSets, (x) => x.symbol && x.dependencies);
postSets = splitPostSets.leftIn;
const orderedPostSets = splitPostSets.splitOut;
let limit = orderedPostSets.length * orderedPostSets.length;
for (let i = 0; i < orderedPostSets.length; i++) {
for (let j = i + 1; j < orderedPostSets.length; j++) {
if (orderedPostSets[j].symbol in orderedPostSets[i].dependencies) {
const temp = orderedPostSets[i];
orderedPostSets[i] = orderedPostSets[j];
orderedPostSets[j] = temp;
i--;
limit--;
assert(limit > 0, 'Could not sort postsets!');
break;
}
}
}
postSets = postSets.concat(orderedPostSets);
const shellFile = MINIMAL_RUNTIME ? 'shell_minimal.js' : 'shell.js';
includeFile(shellFile);
const preFile = MINIMAL_RUNTIME ? 'preamble_minimal.js' : 'preamble.js';
includeFile(preFile);
for (const item of libraryItems.concat(postSets)) {
print(indentify(item.JS || '', 2));
}
if (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 ((SUPPORT_BASE64_EMBEDDING || FORCE_FILESYSTEM) && !MINIMAL_RUNTIME) {
includeFile('base64Utils.js');
}
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) {');
includeFile('headlessCanvas.js');
includeFile('headless.js')
print('}');
}
if (PROXY_TO_WORKER) {
print('if (ENVIRONMENT_IS_WORKER) {\n');
includeFile('webGLWorker.js');
includeFile('proxyWorker.js');
print('}');
}
if (DETERMINISTIC) {
includeFile('deterministic.js');
}
const postFile = MINIMAL_RUNTIME ? 'postamble_minimal.js' : 'postamble.js';
includeFile(postFile);
for (const fileName of POST_JS_FILES) {
includeFileRaw(fileName);
}
print('//FORWARDED_DATA:' + JSON.stringify({
librarySymbols: librarySymbols,
warnings: warnings,
ATINITS: ATINITS.join('\n'),
ATMAINS: ATMAINS.join('\n'),
ATEXITS: ATEXITS.join('\n'),
}));
}
for (const sym of symbolsNeeded) {
itemHandler({
symbol: sym,
mangled: mangleCSymbolName(sym),
});
}
if (symbolsOnly) {
print(JSON.stringify(symbolDeps));
return;
}
finalCombiner();
}