Skip to content

Commit 5247935

Browse files
authored
Preprocessor: Use eval() for good (#7651)
This replaces our custom preprocessor code with eval(). This allows arbitrary conditions in our #ifs, e.g. the __EMSCRIPTEN_HAS_nodefs__ things are now #if LibraryManager.has('library_workerfs.js') where that function is a new utility that checks if a library was included. This patch also switches to things we've wanted to do, like #if FULL_ES2 || LEGACY_GL_EMULATION where in the past we had to create a new global for them. We now error if we do if we ifdef on something nonexistent (the eval call throws), which noticed that we had a bunch of NO_DYNAMIC_EXECUTION code that could be removed (we removed that option a while ago).
1 parent edfbab3 commit 5247935

File tree

9 files changed

+32
-75
lines changed

9 files changed

+32
-75
lines changed

src/compiler.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ EXPORTED_FUNCTIONS = set(EXPORTED_FUNCTIONS);
169169
EXCEPTION_CATCHING_WHITELIST = set(EXCEPTION_CATCHING_WHITELIST);
170170
IMPLEMENTED_FUNCTIONS = set(IMPLEMENTED_FUNCTIONS);
171171

172-
// TODO: Implement support for proper preprocessing, e.g. "#if A || B" and "#if defined(A) || defined(B)" to
173-
// avoid needing this here.
174-
USES_GL_EMULATION = FULL_ES2 || LEGACY_GL_EMULATION;
175-
176172
DEAD_FUNCTIONS.forEach(function(dead) {
177173
DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.push(dead.substr(1));
178174
});

src/jsifier.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ var NEED_ALL_ASM2WASM_IMPORTS = BINARYEN_METHOD != 'native-wasm' || BINARYEN_TRA
4242
// the current compilation unit.
4343
var HAS_MAIN = ('_main' in IMPLEMENTED_FUNCTIONS) || MAIN_MODULE || SIDE_MODULE;
4444

45-
var WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS =
46-
WASM_BACKEND && RESERVED_FUNCTION_POINTERS;
47-
4845
// JSifier
4946
function JSify(data, functionsOnly) {
5047
var mainPass = !functionsOnly;

src/library_browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ var LibraryBrowser = {
11881188

11891189
// Signal GL rendering layer that processing of a new frame is about to start. This helps it optimize
11901190
// VBO double-buffering and reduce GPU stalls.
1191-
#if USES_GL_EMULATION
1191+
#if FULL_ES2 || LEGACY_GL_EMULATION
11921192
GL.newRenderingFrameStarted();
11931193
#endif
11941194

src/library_fs.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
mergeInto(LibraryManager.library, {
77
$FS__deps: ['$ERRNO_CODES', '$ERRNO_MESSAGES', '__setErrNo', '$PATH', '$TTY', '$MEMFS',
8-
#if __EMSCRIPTEN_HAS_idbfs_js__
8+
#if LibraryManager.has('library_idbfs.js')
99
'$IDBFS',
1010
#endif
11-
#if __EMSCRIPTEN_HAS_nodefs_js__
11+
#if LibraryManager.has('library_nodefs.js')
1212
'$NODEFS',
1313
#endif
14-
#if __EMSCRIPTEN_HAS_workerfs_js__
14+
#if LibraryManager.has('library_workerfs.js')
1515
'$WORKERFS',
1616
#endif
17-
#if __EMSCRIPTEN_HAS_noderawfs_js__
17+
#if LibraryManager.has('library_noderawfs.js')
1818
'$NODERAWFS',
1919
#endif
2020
'stdin', 'stdout', 'stderr'],
@@ -1418,13 +1418,13 @@ mergeInto(LibraryManager.library, {
14181418

14191419
FS.filesystems = {
14201420
'MEMFS': MEMFS,
1421-
#if __EMSCRIPTEN_HAS_idbfs_js__
1421+
#if LibraryManager.has('library_idbfs.js')
14221422
'IDBFS': IDBFS,
14231423
#endif
1424-
#if __EMSCRIPTEN_HAS_nodefs_js__
1424+
#if LibraryManager.has('library_nodefs.js')
14251425
'NODEFS': NODEFS,
14261426
#endif
1427-
#if __EMSCRIPTEN_HAS_workerfs_js__
1427+
#if LibraryManager.has('library_workerfs.js')
14281428
'WORKERFS': WORKERFS,
14291429
#endif
14301430
};

src/library_gl.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var LibraryGL = {
4040
syncs: [],
4141
#endif
4242

43-
#if USES_GL_EMULATION
43+
#if FULL_ES2 || LEGACY_GL_EMULATION
4444
currArrayBuffer: 0,
4545
currElementArrayBuffer: 0,
4646
#endif
@@ -77,7 +77,7 @@ var LibraryGL = {
7777
unpackAlignment: 4, // default alignment is 4 bytes
7878

7979
init: function() {
80-
#if USES_GL_EMULATION
80+
#if FULL_ES2 || LEGACY_GL_EMULATION
8181
GL.createLog2ceilLookup(GL.MAX_TEMP_BUFFER_SIZE);
8282
#endif
8383
GL.miniTempBuffer = new Float32Array(GL.MINI_TEMP_BUFFER_SIZE);
@@ -113,7 +113,7 @@ var LibraryGL = {
113113
miniTempBuffer: null,
114114
miniTempBufferViews: [0], // index i has the view of size i+1
115115

116-
#if USES_GL_EMULATION
116+
#if FULL_ES2 || LEGACY_GL_EMULATION
117117
// When user GL code wants to render from client-side memory, we need to upload the vertex data to a temp VBO
118118
// for rendering. Maintain a set of temp VBOs that are created-on-demand to appropriate sizes, and never destroyed.
119119
// Also, for best performance the VBOs are double-buffered, i.e. every second frame we switch the set of VBOs we
@@ -3642,7 +3642,7 @@ var LibraryGL = {
36423642
#endif
36433643
var bufferObj = buffer ? GL.buffers[buffer] : null;
36443644

3645-
#if USES_GL_EMULATION
3645+
#if FULL_ES2 || LEGACY_GL_EMULATION
36463646
if (target == GLctx.ARRAY_BUFFER) {
36473647
GL.currArrayBuffer = buffer;
36483648
#if LEGACY_GL_EMULATION
@@ -4282,7 +4282,7 @@ var LibraryGL = {
42824282
#endif
42834283
GLctx['bindVertexArray'](GL.vaos[vao]);
42844284
#endif
4285-
#if USES_GL_EMULATION
4285+
#if FULL_ES2 || LEGACY_GL_EMULATION
42864286
var ibo = GLctx.getParameter(GLctx.ELEMENT_ARRAY_BUFFER_BINDING);
42874287
GL.currElementArrayBuffer = ibo ? (ibo.name | 0) : 0;
42884288
#endif

src/library_vr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ var LibraryWebVR = {
195195
/* Prevent scheduler being called twice when loop is changed */
196196
display.mainLoop.running = true;
197197

198-
#if USES_GL_EMULATION
198+
#if FULL_ES2 || LEGACY_GL_EMULATION
199199
GL.newRenderingFrameStarted();
200200
#endif
201201

src/modules.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ var LibraryManager = {
9797
library: null,
9898
structs: {},
9999
loaded: false,
100+
libraries: [],
101+
102+
has: function(name) {
103+
return this.libraries.indexOf(name) >= 0;
104+
},
100105

101106
load: function() {
102107
if (this.library) return;
@@ -163,18 +168,15 @@ var LibraryManager = {
163168

164169
libraries = libraries.concat(additionalLibraries);
165170

166-
// For each JS library library_xxx.js, add a preprocessor token __EMSCRIPTEN_HAS_xxx_js__ so that code can conditionally dead code eliminate out
167-
// if a particular feature is not being linked in.
168-
for (var i = 0; i < libraries.length; ++i) {
169-
global['__EMSCRIPTEN_HAS_' + libraries[i].replace('.', '_').replace('library_', '') + '__'] = 1
170-
}
171-
172171
if (BOOTSTRAPPING_STRUCT_INFO) libraries = ['library_bootstrap_structInfo.js', 'library_formatString.js'];
173172
if (ONLY_MY_CODE) {
174-
libraries = [];
173+
libraries.length = 0;
175174
LibraryManager.library = {};
176175
}
177176

177+
// Save the list for has() queries later.
178+
this.libraries = libraries;
179+
178180
for (var i = 0; i < libraries.length; i++) {
179181
var filename = libraries[i];
180182
var src = read(filename);

src/parseTools.js

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,47 +42,9 @@ function preprocess(text, filenameHint) {
4242
if (line[1] == 'i') {
4343
if (line[2] == 'f') { // if
4444
var parts = line.split(' ');
45-
var ident = parts[1];
46-
var op = parts[2];
47-
var value = parts[3];
48-
if (typeof value === 'string') {
49-
// when writing
50-
// #if option == 'stringValue'
51-
// we need to get rid of the quotes
52-
if (value[0] === '"' || value[0] === "'") {
53-
assert(value[value.length - 1] == '"' || value[value.length - 1] == "'");
54-
value = value.substring(1, value.length - 1);
55-
}
56-
}
57-
if (op) {
58-
if (op === '==') {
59-
showStack.push(ident in this && this[ident] == value);
60-
} else if (op === '!=') {
61-
showStack.push(!(ident in this && this[ident] == value));
62-
} else if (op === '<') {
63-
showStack.push(ident in this && this[ident] < value);
64-
} else if (op === '<=') {
65-
showStack.push(ident in this && this[ident] <= value);
66-
} else if (op === '>') {
67-
showStack.push(ident in this && this[ident] > value);
68-
} else if (op === '>=') {
69-
showStack.push(ident in this && this[ident] >= value);
70-
} else {
71-
error('unsupported preprocessor op ' + op);
72-
}
73-
} else {
74-
// Check if a value is truthy.
75-
var short = ident[0] === '!' ? ident.substr(1) : ident;
76-
var truthy = short in this;
77-
if (truthy) {
78-
truthy = !!this[short];
79-
}
80-
if (ident[0] === '!') {
81-
showStack.push(!truthy);
82-
} else {
83-
showStack.push(truthy);
84-
}
85-
}
45+
var after = parts.slice(1).join(' ');
46+
var truthy = !!eval(after);
47+
showStack.push(truthy);
8648
} else if (line[2] == 'n') { // include
8749
var filename = line.substr(line.indexOf(' ')+1);
8850
if (filename.indexOf('"') === 0) {

src/support.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -603,15 +603,15 @@ Module['registerFunctions'] = registerFunctions;
603603
#endif // RELOCATABLE
604604
#endif // EMULATED_FUNCTION_POINTERS
605605

606-
#if WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS
606+
#if WASM_BACKEND && RESERVED_FUNCTION_POINTERS
607607
var jsCallStartIndex = {{{ JSCALL_START_INDEX }}};
608608
var jsCallSigOrder = {{{ JSON.stringify(JSCALL_SIG_ORDER) }}};
609609
var jsCallNumSigs = Object.keys(jsCallSigOrder).length;
610610
var functionPointers = new Array(jsCallNumSigs * {{{ RESERVED_FUNCTION_POINTERS }}});
611-
#else // WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS == 0
611+
#else // WASM_BACKEND && RESERVED_FUNCTION_POINTERS == 0
612612
var jsCallStartIndex = 1;
613613
var functionPointers = new Array({{{ RESERVED_FUNCTION_POINTERS }}});
614-
#endif // WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS
614+
#endif // WASM_BACKEND && RESERVED_FUNCTION_POINTERS
615615

616616
// 'sig' parameter is only used on LLVM wasm backend
617617
function addFunction(func, sig) {
@@ -626,11 +626,11 @@ function addFunction(func, sig) {
626626
}
627627
#endif // ASSERTIONS
628628
#if EMULATED_FUNCTION_POINTERS == 0
629-
#if WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS
629+
#if WASM_BACKEND && RESERVED_FUNCTION_POINTERS
630630
var base = jsCallSigOrder[sig] * {{{ RESERVED_FUNCTION_POINTERS }}};
631-
#else // WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS == 0
631+
#else // WASM_BACKEND && RESERVED_FUNCTION_POINTERS == 0
632632
var base = 0;
633-
#endif // WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS
633+
#endif // WASM_BACKEND && RESERVED_FUNCTION_POINTERS
634634
for (var i = base; i < base + {{{ RESERVED_FUNCTION_POINTERS }}}; i++) {
635635
if (!functionPointers[i]) {
636636
functionPointers[i] = func;

0 commit comments

Comments
 (0)