diff --git a/src/compiler.js b/src/compiler.js index 3b0e8ecf3a697..424e9b3205885 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -169,10 +169,6 @@ EXPORTED_FUNCTIONS = set(EXPORTED_FUNCTIONS); EXCEPTION_CATCHING_WHITELIST = set(EXCEPTION_CATCHING_WHITELIST); IMPLEMENTED_FUNCTIONS = set(IMPLEMENTED_FUNCTIONS); -// TODO: Implement support for proper preprocessing, e.g. "#if A || B" and "#if defined(A) || defined(B)" to -// avoid needing this here. -USES_GL_EMULATION = FULL_ES2 || LEGACY_GL_EMULATION; - DEAD_FUNCTIONS.forEach(function(dead) { DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.push(dead.substr(1)); }); diff --git a/src/jsifier.js b/src/jsifier.js index 9605cb063ff97..c11c8f3aa380d 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -42,9 +42,6 @@ var NEED_ALL_ASM2WASM_IMPORTS = BINARYEN_METHOD != 'native-wasm' || BINARYEN_TRA // the current compilation unit. var HAS_MAIN = ('_main' in IMPLEMENTED_FUNCTIONS) || MAIN_MODULE || SIDE_MODULE; -var WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS = - WASM_BACKEND && RESERVED_FUNCTION_POINTERS; - // JSifier function JSify(data, functionsOnly) { var mainPass = !functionsOnly; diff --git a/src/library_browser.js b/src/library_browser.js index 1dd1fcfc104c3..4c2176763f3f5 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -1188,7 +1188,7 @@ var LibraryBrowser = { // Signal GL rendering layer that processing of a new frame is about to start. This helps it optimize // VBO double-buffering and reduce GPU stalls. -#if USES_GL_EMULATION +#if FULL_ES2 || LEGACY_GL_EMULATION GL.newRenderingFrameStarted(); #endif diff --git a/src/library_fs.js b/src/library_fs.js index abcf326efb629..98b6d26a323ec 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -5,16 +5,16 @@ mergeInto(LibraryManager.library, { $FS__deps: ['$ERRNO_CODES', '$ERRNO_MESSAGES', '__setErrNo', '$PATH', '$TTY', '$MEMFS', -#if __EMSCRIPTEN_HAS_idbfs_js__ +#if LibraryManager.has('library_idbfs.js') '$IDBFS', #endif -#if __EMSCRIPTEN_HAS_nodefs_js__ +#if LibraryManager.has('library_nodefs.js') '$NODEFS', #endif -#if __EMSCRIPTEN_HAS_workerfs_js__ +#if LibraryManager.has('library_workerfs.js') '$WORKERFS', #endif -#if __EMSCRIPTEN_HAS_noderawfs_js__ +#if LibraryManager.has('library_noderawfs.js') '$NODERAWFS', #endif 'stdin', 'stdout', 'stderr'], @@ -1418,13 +1418,13 @@ mergeInto(LibraryManager.library, { FS.filesystems = { 'MEMFS': MEMFS, -#if __EMSCRIPTEN_HAS_idbfs_js__ +#if LibraryManager.has('library_idbfs.js') 'IDBFS': IDBFS, #endif -#if __EMSCRIPTEN_HAS_nodefs_js__ +#if LibraryManager.has('library_nodefs.js') 'NODEFS': NODEFS, #endif -#if __EMSCRIPTEN_HAS_workerfs_js__ +#if LibraryManager.has('library_workerfs.js') 'WORKERFS': WORKERFS, #endif }; diff --git a/src/library_gl.js b/src/library_gl.js index dac225d941508..528f96db8ba19 100644 --- a/src/library_gl.js +++ b/src/library_gl.js @@ -40,7 +40,7 @@ var LibraryGL = { syncs: [], #endif -#if USES_GL_EMULATION +#if FULL_ES2 || LEGACY_GL_EMULATION currArrayBuffer: 0, currElementArrayBuffer: 0, #endif @@ -77,7 +77,7 @@ var LibraryGL = { unpackAlignment: 4, // default alignment is 4 bytes init: function() { -#if USES_GL_EMULATION +#if FULL_ES2 || LEGACY_GL_EMULATION GL.createLog2ceilLookup(GL.MAX_TEMP_BUFFER_SIZE); #endif GL.miniTempBuffer = new Float32Array(GL.MINI_TEMP_BUFFER_SIZE); @@ -113,7 +113,7 @@ var LibraryGL = { miniTempBuffer: null, miniTempBufferViews: [0], // index i has the view of size i+1 -#if USES_GL_EMULATION +#if FULL_ES2 || LEGACY_GL_EMULATION // When user GL code wants to render from client-side memory, we need to upload the vertex data to a temp VBO // for rendering. Maintain a set of temp VBOs that are created-on-demand to appropriate sizes, and never destroyed. // 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 = { #endif var bufferObj = buffer ? GL.buffers[buffer] : null; -#if USES_GL_EMULATION +#if FULL_ES2 || LEGACY_GL_EMULATION if (target == GLctx.ARRAY_BUFFER) { GL.currArrayBuffer = buffer; #if LEGACY_GL_EMULATION @@ -4282,7 +4282,7 @@ var LibraryGL = { #endif GLctx['bindVertexArray'](GL.vaos[vao]); #endif -#if USES_GL_EMULATION +#if FULL_ES2 || LEGACY_GL_EMULATION var ibo = GLctx.getParameter(GLctx.ELEMENT_ARRAY_BUFFER_BINDING); GL.currElementArrayBuffer = ibo ? (ibo.name | 0) : 0; #endif diff --git a/src/library_vr.js b/src/library_vr.js index b1811ae651cab..1dd29cadbc2f9 100644 --- a/src/library_vr.js +++ b/src/library_vr.js @@ -195,7 +195,7 @@ var LibraryWebVR = { /* Prevent scheduler being called twice when loop is changed */ display.mainLoop.running = true; -#if USES_GL_EMULATION +#if FULL_ES2 || LEGACY_GL_EMULATION GL.newRenderingFrameStarted(); #endif diff --git a/src/modules.js b/src/modules.js index e8100efa42040..18c7bf9b5ce07 100644 --- a/src/modules.js +++ b/src/modules.js @@ -97,6 +97,11 @@ var LibraryManager = { library: null, structs: {}, loaded: false, + libraries: [], + + has: function(name) { + return this.libraries.indexOf(name) >= 0; + }, load: function() { if (this.library) return; @@ -163,18 +168,15 @@ var LibraryManager = { libraries = libraries.concat(additionalLibraries); - // For each JS library library_xxx.js, add a preprocessor token __EMSCRIPTEN_HAS_xxx_js__ so that code can conditionally dead code eliminate out - // if a particular feature is not being linked in. - for (var i = 0; i < libraries.length; ++i) { - global['__EMSCRIPTEN_HAS_' + libraries[i].replace('.', '_').replace('library_', '') + '__'] = 1 - } - if (BOOTSTRAPPING_STRUCT_INFO) libraries = ['library_bootstrap_structInfo.js', 'library_formatString.js']; if (ONLY_MY_CODE) { - libraries = []; + libraries.length = 0; LibraryManager.library = {}; } + // 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); diff --git a/src/parseTools.js b/src/parseTools.js index 5f5fd08e6e2a9..8700db430c3cd 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -42,47 +42,9 @@ function preprocess(text, filenameHint) { if (line[1] == 'i') { if (line[2] == 'f') { // if var parts = line.split(' '); - var ident = parts[1]; - var op = parts[2]; - var value = parts[3]; - if (typeof value === 'string') { - // when writing - // #if option == 'stringValue' - // we need to get rid of the quotes - if (value[0] === '"' || value[0] === "'") { - assert(value[value.length - 1] == '"' || value[value.length - 1] == "'"); - value = value.substring(1, value.length - 1); - } - } - if (op) { - if (op === '==') { - showStack.push(ident in this && this[ident] == value); - } else if (op === '!=') { - showStack.push(!(ident in this && this[ident] == value)); - } else if (op === '<') { - showStack.push(ident in this && this[ident] < value); - } else if (op === '<=') { - showStack.push(ident in this && this[ident] <= value); - } else if (op === '>') { - showStack.push(ident in this && this[ident] > value); - } else if (op === '>=') { - showStack.push(ident in this && this[ident] >= value); - } else { - error('unsupported preprocessor op ' + op); - } - } else { - // Check if a value is truthy. - var short = ident[0] === '!' ? ident.substr(1) : ident; - var truthy = short in this; - if (truthy) { - truthy = !!this[short]; - } - if (ident[0] === '!') { - showStack.push(!truthy); - } else { - showStack.push(truthy); - } - } + var after = parts.slice(1).join(' '); + var truthy = !!eval(after); + showStack.push(truthy); } else if (line[2] == 'n') { // include var filename = line.substr(line.indexOf(' ')+1); if (filename.indexOf('"') === 0) { diff --git a/src/support.js b/src/support.js index 0c857d0d11db7..532e2e1da24b2 100644 --- a/src/support.js +++ b/src/support.js @@ -603,15 +603,15 @@ Module['registerFunctions'] = registerFunctions; #endif // RELOCATABLE #endif // EMULATED_FUNCTION_POINTERS -#if WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS +#if WASM_BACKEND && RESERVED_FUNCTION_POINTERS var jsCallStartIndex = {{{ JSCALL_START_INDEX }}}; var jsCallSigOrder = {{{ JSON.stringify(JSCALL_SIG_ORDER) }}}; var jsCallNumSigs = Object.keys(jsCallSigOrder).length; var functionPointers = new Array(jsCallNumSigs * {{{ RESERVED_FUNCTION_POINTERS }}}); -#else // WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS == 0 +#else // WASM_BACKEND && RESERVED_FUNCTION_POINTERS == 0 var jsCallStartIndex = 1; var functionPointers = new Array({{{ RESERVED_FUNCTION_POINTERS }}}); -#endif // WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS +#endif // WASM_BACKEND && RESERVED_FUNCTION_POINTERS // 'sig' parameter is only used on LLVM wasm backend function addFunction(func, sig) { @@ -626,11 +626,11 @@ function addFunction(func, sig) { } #endif // ASSERTIONS #if EMULATED_FUNCTION_POINTERS == 0 -#if WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS +#if WASM_BACKEND && RESERVED_FUNCTION_POINTERS var base = jsCallSigOrder[sig] * {{{ RESERVED_FUNCTION_POINTERS }}}; -#else // WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS == 0 +#else // WASM_BACKEND && RESERVED_FUNCTION_POINTERS == 0 var base = 0; -#endif // WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS +#endif // WASM_BACKEND && RESERVED_FUNCTION_POINTERS for (var i = base; i < base + {{{ RESERVED_FUNCTION_POINTERS }}}; i++) { if (!functionPointers[i]) { functionPointers[i] = func;