Skip to content

Preprocessor: Use eval() for good #7651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand Down
3 changes: 0 additions & 3 deletions src/jsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/library_browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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
};
Expand Down
10 changes: 5 additions & 5 deletions src/library_gl.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var LibraryGL = {
syncs: [],
#endif

#if USES_GL_EMULATION
#if FULL_ES2 || LEGACY_GL_EMULATION
currArrayBuffer: 0,
currElementArrayBuffer: 0,
#endif
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/library_vr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 9 additions & 7 deletions src/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
44 changes: 3 additions & 41 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions src/support.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also yank out the WASM_BACKEND_WITH_RESERVED_FUNCTION_POINTERS definition from jsifier.js, because this should be all that uses it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed

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) {
Expand All @@ -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;
Expand Down