forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.js
521 lines (477 loc) · 16.7 KB
/
modules.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
// Copyright 2011 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";
// Various namespace-like modules
var STACK_ALIGN = 16;
var Variables = {
globals: {},
indexedGlobals: {}, // for indexed globals, ident ==> index
// Used in calculation of indexed globals
nextIndexedOffset: 0,
resolveAliasToIdent: function(ident) {
while (1) {
var varData = Variables.globals[ident];
if (!(varData && varData.targetIdent)) break;
ident = varData.targetIdent; // might need to eval to turn (6) into 6
}
return ident;
},
};
var Types = {
types: {},
};
// Constructs an array ['a0', 'a1', 'a2', ..., 'a(n-1)']
function genArgSequence(n) {
var args = [];
for(var i = 0; i < n; ++i) {
args.push('a'+i);
}
return args;
}
var Functions = {
// All functions that will be implemented in this file. Maps id to signature
implementedFunctions: {},
// functions added from the library. value 2 means asmLibraryFunction
libraryFunctions: {},
};
var LibraryManager = {
library: null,
structs: {},
loaded: false,
libraries: [],
has: function(name) {
return this.libraries.indexOf(name) >= 0;
},
load: function() {
if (this.library) return;
// Core system libraries (always linked against)
var libraries = [
'library.js',
'library_formatString.js',
'library_path.js',
'library_signals.js',
'library_syscall.js',
'library_html5.js',
'library_stack_trace.js',
'library_wasi.js'
];
if (!DISABLE_EXCEPTION_THROWING) {
libraries.push('library_exceptions.js');
} else {
libraries.push('library_exceptions_stub.js');
}
if (!MINIMAL_RUNTIME) {
libraries.push('library_browser.js');
}
if (FILESYSTEM) {
// Core filesystem libraries (always linked against, unless -s FILESYSTEM=0 is specified)
libraries = libraries.concat([
'library_fs.js',
'library_memfs.js',
'library_tty.js',
'library_pipefs.js', // ok to include it by default since it's only used if the syscall is used
'library_sockfs.js', // ok to include it by default since it's only used if the syscall is used
]);
if (NODERAWFS) {
// NODERAWFS requires NODEFS
if (SYSTEM_JS_LIBRARIES.indexOf('library_nodefs.js') < 0) {
libraries.push('library_nodefs.js');
}
libraries.push('library_noderawfs.js');
}
}
// Additional JS libraries (without AUTO_JS_LIBRARIES, link to these explicitly via -lxxx.js)
if (AUTO_JS_LIBRARIES) {
libraries = libraries.concat([
'library_webgl.js',
'library_openal.js',
'library_vr.js',
'library_sdl.js',
'library_glut.js',
'library_xlib.js',
'library_egl.js',
'library_glfw.js',
'library_uuid.js',
'library_glew.js',
'library_idbstore.js',
'library_async.js'
]);
} else {
if (EMTERPRETIFY_ASYNC || ASYNCIFY) {
libraries.push('library_async.js');
}
if (USE_SDL == 1) {
libraries.push('library_sdl.js');
}
if (USE_SDL == 2) {
libraries.push('library_egl.js', 'library_webgl.js');
}
}
// Add any explicitly specified system JS libraries to link to, add those to link.
libraries = libraries.concat(SYSTEM_JS_LIBRARIES)
if (LZ4) {
libraries.push('library_lz4.js');
}
if (USE_WEBGL2) {
libraries.push('library_webgl2.js');
}
if (LEGACY_GL_EMULATION) {
libraries.push('library_glemu.js');
}
if (BOOTSTRAPPING_STRUCT_INFO) libraries = ['library_bootstrap_structInfo.js', 'library_formatString.js'];
// TODO: deduplicate libraries (not needed for correctness, but avoids unnecessary work)
// Save the list for has() queries later.
this.libraries = libraries;
for (var i = 0; i < libraries.length; i++) {
var filename = libraries[i];
var src = read(filename);
var processed = undefined;
try {
processed = processMacros(preprocess(src, filename));
eval(processed);
} catch(e) {
var details = [e, e.lineNumber ? 'line number: ' + e.lineNumber : ''];
if (VERBOSE) {
details.push((e.stack || "").toString().replace('Object.<anonymous>', filename));
}
if (processed) {
error('failure to execute js library "' + filename + '": ' + details);
if (VERBOSE) {
error('preprocessed source (you can run a js engine on this to get a clearer error message sometimes):\n=============\n' + processed + '\n=============');
} else {
error('use -s VERBOSE to see more details')
}
} else {
error('failure to process js library "' + filename + '": ' + details);
if (VERBOSE) {
error('original source:\n=============\n' + src + '\n=============');
} else {
error('use -s VERBOSE to see more details')
}
}
throw e;
}
}
// apply synonyms. these are typically not speed-sensitive, and doing it this way makes it possible to not include hacks in the compiler
// (and makes it simpler to switch between SDL versions, fastcomp and non-fastcomp, etc.).
var lib = LibraryManager.library;
libloop: for (var x in lib) {
if (x.lastIndexOf('__') > 0) continue; // ignore __deps, __*
if (lib[x + '__asm']) continue; // ignore asm library functions, those need to be fully optimized
if (typeof lib[x] === 'string') {
var target = x;
while (typeof lib[target] === 'string') {
// ignore code and variable assignments, aliases are just simple names
if (lib[target].search(/[=({; ]/) >= 0) continue libloop;
// ignore trivial pass-throughs to Math.*
if (lib[target].indexOf('Math_') == 0) continue libloop;
target = lib[target];
}
if (lib[target + '__asm']) continue; // This is an alias of an asm library function. Also needs to be fully optimized.
if (!isNaN(target)) continue; // This is a number, and so cannot be an alias target.
if (typeof lib[target] === 'undefined' || typeof lib[target] === 'function') {
// If the alias provides a signature, then construct a specific 'function foo(a0, a1, a2) { [return] _target(a0, a1, a2); }' form of forwarding.
// Otherwise construct a generic 'function foo() { return _target.apply(null, arguments); }' forwarding.
// The benefit of the first form is that Closure is able to fully inline and reason about the function.
// Note that the signature is checked on the alias function, not on the target function. That allows aliases to choose individually which form
// to use.
if (lib[x + '__sig']) {
var argCount = lib[x + '__sig'].length - 1;
var ret = lib[x + '__sig'] == 'v' ? '' : 'return ';
var args = genArgSequence(argCount).join(',');
lib[x] = new Function(args, ret + '_' + target + '(' + args + ');');
} else {
lib[x] = new Function('return _' + target + '.apply(null, arguments)');
}
if (!lib[x + '__deps']) lib[x + '__deps'] = [];
lib[x + '__deps'].push(target);
}
}
}
if (WASM_BACKEND) {
// all asm.js methods should just be run in JS. We should optimize them eventually into wasm. TODO
for (var x in lib) {
if (lib[x + '__asm']) {
lib[x + '__asm'] = undefined;
}
}
}
/*
// export code for CallHandlers.h
printErr('============================');
for (var x in this.library) {
var y = this.library[x];
if (typeof y === 'string' && x.indexOf('__sig') < 0 && x.indexOf('__postset') < 0 && y.indexOf(' ') < 0) {
printErr('DEF_REDIRECT_HANDLER(' + x + ', ' + y + ');');
}
}
printErr('============================');
for (var x in this.library) {
var y = this.library[x];
if (typeof y === 'string' && x.indexOf('__sig') < 0 && x.indexOf('__postset') < 0 && y.indexOf(' ') < 0) {
printErr(' SETUP_CALL_HANDLER(' + x + ');');
}
}
printErr('============================');
// end export code for CallHandlers.h
*/
this.loaded = true;
},
// Given an ident, see if it is an alias for something, and so forth, returning
// the earliest ancestor (the root)
getRootIdent: function(ident) {
if (!this.library) return null;
var ret = LibraryManager.library[ident];
if (!ret) return null;
var last = ident;
while (typeof ret === 'string') {
last = ret;
ret = LibraryManager.library[ret];
}
return last;
},
isStubFunction: function(ident) {
if (SIDE_MODULE == 1) return false; // cannot eliminate these, as may be implement in the main module and imported by us
var libCall = LibraryManager.library[ident.substr(1)];
return typeof libCall === 'function' && libCall.toString().replace(/\s/g, '') === 'function(){}'
&& !(ident in Functions.implementedFunctions);
}
};
if (!BOOTSTRAPPING_STRUCT_INFO) {
// Load struct and define information.
var temp = JSON.parse(read(STRUCT_INFO));
C_STRUCTS = temp.structs;
C_DEFINES = temp.defines;
} else {
C_STRUCTS = {};
C_DEFINES = {};
}
// Safe way to access a C define. We check that we don't add library functions with missing defines.
function cDefine(key) {
if (key in C_DEFINES) return C_DEFINES[key];
throw 'Missing C define ' + key + '! If you just added it to struct_info.json, you need to ./emcc --clear-cache';
}
var EXPORTED_RUNTIME_METHODS_SET = set(EXPORTED_RUNTIME_METHODS.concat(EXTRA_EXPORTED_RUNTIME_METHODS));
EXPORTED_RUNTIME_METHODS = unset(EXPORTED_RUNTIME_METHODS_SET);
EXTRA_EXPORTED_RUNTIME_METHODS = [];
function isFSPrefixed(name) {
return name.length > 3 && name[0] === 'F' && name[1] === 'S' && name[2] === '_';
}
// forcing the filesystem exports a few things by default
function isExportedByForceFilesystem(name) {
return name === 'FS_createFolder' ||
name === 'FS_createPath' ||
name === 'FS_createDataFile' ||
name === 'FS_createPreloadedFile' ||
name === 'FS_createLazyFile' ||
name === 'FS_createLink' ||
name === 'FS_createDevice' ||
name === 'FS_unlink' ||
name === 'getMemory' ||
name === 'addRunDependency' ||
name === 'removeRunDependency' ||
name === 'calledRun';
}
// export parts of the JS runtime that the user asked for
function exportRuntime() {
// optionally export something.
// in ASSERTIONS mode we show a useful error if it is used without
// being exported. how we show the message depends on whether it's
// a function (almost all of them) or a number.
function maybeExport(name, isNumber) {
// if requested to be exported, export it
if (name in EXPORTED_RUNTIME_METHODS_SET) {
var exported = name;
// the exported name may differ from the internal name
if (isFSPrefixed(exported)) {
// this is a filesystem value, FS.x exported as FS_x
exported = 'FS.' + exported.substr(3);
} else if (exported === 'print') {
exported = 'out';
} else if (exported === 'printErr') {
exported = 'err';
}
return 'Module["' + name + '"] = ' + exported + ';';
}
// do not export it. but if ASSERTIONS, emit a
// stub with an error, so the user gets a message
// if it is used, that they should export it
if (ASSERTIONS) {
// check if it already exists, to support EXPORT_ALL and other cases
// (we could optimize this, but in ASSERTIONS mode code size doesn't
// matter anyhow)
var extra = '';
if (isExportedByForceFilesystem(name)) {
extra = '. Alternatively, forcing filesystem support (-s FORCE_FILESYSTEM=1) can export this for you';
}
if (!isNumber) {
return 'if (!Object.getOwnPropertyDescriptor(Module, "' + name + '")) Module["' + name + '"] = function() { abort("\'' + name + '\' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS (see the FAQ)' + extra + '") };';
} else {
return 'if (!Object.getOwnPropertyDescriptor(Module, "' + name + '")) Object.defineProperty(Module, "' + name + '", { configurable: true, get: function() { abort("\'' + name + '\' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS (see the FAQ)' + extra + '") } });';
}
}
return '';
}
function maybeExportNumber(name) {
return maybeExport(name, true);
}
// All possible runtime elements to export
var runtimeElements = [
'intArrayFromString',
'intArrayToString',
'ccall',
'cwrap',
'setValue',
'getValue',
'allocate',
'getMemory',
'AsciiToString',
'stringToAscii',
'UTF8ArrayToString',
'UTF8ToString',
'stringToUTF8Array',
'stringToUTF8',
'lengthBytesUTF8',
'UTF16ToString',
'stringToUTF16',
'lengthBytesUTF16',
'UTF32ToString',
'stringToUTF32',
'lengthBytesUTF32',
'allocateUTF8',
'stackTrace',
'addOnPreRun',
'addOnInit',
'addOnPreMain',
'addOnExit',
'addOnPostRun',
'writeStringToMemory',
'writeArrayToMemory',
'writeAsciiToMemory',
'addRunDependency',
'removeRunDependency',
'ENV',
'FS',
'FS_createFolder',
'FS_createPath',
'FS_createDataFile',
'FS_createPreloadedFile',
'FS_createLazyFile',
'FS_createLink',
'FS_createDevice',
'FS_unlink',
'GL',
'dynamicAlloc',
'loadDynamicLibrary',
'loadWebAssemblyModule',
'getLEB',
'getFunctionTables',
'alignFunctionTables',
'registerFunctions',
'addFunction',
'removeFunction',
'getFuncWrapper',
'prettyPrint',
'makeBigInt',
'dynCall',
'getCompilerSetting',
'stackSave',
'stackRestore',
'stackAlloc',
'establishStackSpace',
'print',
'printErr',
'getTempRet0',
'setTempRet0',
'callMain',
'abort',
];
if (!MINIMAL_RUNTIME) {
runtimeElements.push('Pointer_stringify');
runtimeElements.push('warnOnce');
}
if (STACK_OVERFLOW_CHECK) {
runtimeElements.push('writeStackCookie');
runtimeElements.push('checkStackCookie');
runtimeElements.push('abortStackOverflow');
}
if (USE_PTHREADS) {
// In pthreads mode, the following functions always need to be exported to
// Module for closure compiler, and also for MODULARIZE (so worker.js can
// access them).
['PThread', 'ExitStatus', 'tempDoublePtr', 'wasmMemory', '_pthread_self',
'ExitStatus', 'tempDoublePtr'].forEach(function(x) {
EXPORTED_RUNTIME_METHODS_SET[x] = 1;
runtimeElements.push(x);
});
}
if (SUPPORT_BASE64_EMBEDDING) {
runtimeElements.push('intArrayFromBase64');
runtimeElements.push('tryParseAsDataURI');
}
// dynCall_* methods are not hardcoded here, as they
// depend on the file being compiled. check for them
// and add them.
for (var name in EXPORTED_RUNTIME_METHODS_SET) {
if (/^dynCall_/.test(name)) {
// a specific dynCall; add to the list
runtimeElements.push(name);
}
}
var runtimeNumbers = [
'ALLOC_NORMAL',
'ALLOC_STACK',
'ALLOC_DYNAMIC',
'ALLOC_NONE',
'calledRun',
];
if (ASSERTIONS) {
// check all exported things exist, warn about typos
for (var name in EXPORTED_RUNTIME_METHODS_SET) {
if (runtimeElements.indexOf(name) < 0 &&
runtimeNumbers.indexOf(name) < 0) {
printErr('warning: invalid item (maybe a typo?) in EXPORTED_RUNTIME_METHODS: ' + name);
}
}
}
return runtimeElements.map(function(name) {
return maybeExport(name);
}).join('\n') + runtimeNumbers.map(function(name) {
return maybeExportNumber(name);
}).join('\n');
}
var PassManager = {
serialize: function() {
print('\n//FORWARDED_DATA:' + JSON.stringify({
Functions: Functions,
EXPORTED_FUNCTIONS: EXPORTED_FUNCTIONS,
STATIC_BUMP: STATIC_BUMP, // updated with info from JS
DYNAMICTOP_PTR: DYNAMICTOP_PTR,
ATINITS: ATINITS.join('\n'),
ATMAINS: ATMAINS.join('\n'),
ATEXITS: ATEXITS.join('\n'),
}));
},
load: function(json) {
var data = JSON.parse(json);
for (var i in data.Types) {
Types[i] = data.Types[i];
}
for (var i in data.Variables) {
Variables[i] = data.Variables[i];
}
for (var i in data.Functions) {
Functions[i] = data.Functions[i];
}
EXPORTED_FUNCTIONS = data.EXPORTED_FUNCTIONS;
/*
print('\n//LOADED_DATA:' + JSON.stringify({
Types: Types,
Variables: Variables,
Functions: Functions
}));
*/
}
};