diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler.js | 2 | ||||
-rw-r--r-- | src/jsifier.js | 8 | ||||
-rw-r--r-- | src/modules.js | 20 | ||||
-rw-r--r-- | src/preamble.js | 4 | ||||
-rw-r--r-- | src/runtime.js | 8 | ||||
-rw-r--r-- | src/settings.js | 3 |
6 files changed, 31 insertions, 14 deletions
diff --git a/src/compiler.js b/src/compiler.js index 1a48fad2..c0bd64d3 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -32,6 +32,8 @@ if (CORRECT_ROUNDINGS === 2) { CORRECT_ROUNDINGS_LINES = set(CORRECT_ROUNDINGS_LINES); // for fast checking } +EXPORTED_FUNCTIONS = set(EXPORTED_FUNCTIONS); + // Load compiler code load('framework.js'); diff --git a/src/jsifier.js b/src/jsifier.js index 2c33eba1..7a7bc47f 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -117,7 +117,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { function indexizeFunctions(value) { // TODO: Also check for other functions (externals, library, etc.) if (value in FUNCTIONS) { - value = value + '.__index__'; // Store integer value + value = Functions.getIndex(value); // Store integer value } return value; } @@ -537,7 +537,10 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { func.JS += ' return' + (func.returnType !== 'void' ? ' null' : '') + ';\n'; } func.JS += '}\n'; - func.JS += func.ident + '.__index__ = Runtime.getFunctionIndex(' + func.ident + ', "' + func.ident + '");\n'; + if (func.ident in EXPORTED_FUNCTIONS) { + func.JS += 'Module["' + func.ident + '"] = ' + func.ident + ';'; + } + return func; } }); @@ -1170,6 +1173,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { var pre = processMacros(preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()), CONSTANTS)); print(pre); generated.forEach(function(item) { print(indentify(item.JS || '', 2)); }); + print(Functions.generateIndexing()); var postParts = processMacros(preprocess(read('postamble.js'), CONSTANTS)).split('{{GLOBAL_VARS}}'); print(postParts[0]); diff --git a/src/modules.js b/src/modules.js index 4d225057..804de24a 100644 --- a/src/modules.js +++ b/src/modules.js @@ -114,3 +114,23 @@ var Types = { needAnalysis: {} // Types noticed during parsing, that need analysis }; +var Functions = { + indexedFunctions: [0, 0], // Start at a non-0 (even, see below) value + + // Mark a function as needing indexing, and returns the index + getIndex: function(ident) { + var key = this.indexedFunctions.indexOf(ident); + if (key < 0) { + key = this.indexedFunctions.length; + this.indexedFunctions[key] = ident; + this.indexedFunctions[key+1] = 0; // Need to have keys be even numbers, see |polymorph| test + } + return key.toString(); + }, + + // Generate code for function indexing + generateIndexing: function() { + return 'var FUNCTION_TABLE = [' + this.indexedFunctions.toString().replace('"', '') + '];'; + } +}; + diff --git a/src/preamble.js b/src/preamble.js index 9f457a5c..a2a66bd6 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -157,10 +157,6 @@ var START_TIME = Date.now(); function __globalConstructor__() { } -// Maps ints ==> functions. This lets us pass around ints, which are -// actually pointers to functions, and we convert at call()time -var FUNCTION_TABLE = []; - var __THREW__ = false; // Used in checking for thrown exceptions. var __ATEXIT__ = []; diff --git a/src/runtime.js b/src/runtime.js index 6ab78611..acd5ed8d 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -77,14 +77,6 @@ Runtime = { staticAlloc: unInline('staticAlloc', ['size']), alignMemory: unInline('alignMemory', ['size', 'quantum']), - getFunctionIndex: function getFunctionIndex(func, ident) { - var key = FUNCTION_TABLE.length; - FUNCTION_TABLE[key] = func; - FUNCTION_TABLE[key+1] = null; // Need to have keys be even numbers, see |polymorph| test - Module[ident] = func; // Export using full name, for Closure Compiler - return key; - }, - // TODO: cleanup isNumberType: function(type) { return type in Runtime.INT_TYPES || type in Runtime.FLOAT_TYPES; diff --git a/src/settings.js b/src/settings.js index ceda7ec8..a93d9183 100644 --- a/src/settings.js +++ b/src/settings.js @@ -68,6 +68,9 @@ CORRECT_ROUNDINGS = 1; // C rounds to 0 (-5.5 to -5, +5.5 to 5), while JS has no // Math.floor is to negative, ceil to positive. With CORRECT_ROUNDINGS, // we will do slow but correct C rounding operations. +EXPORTED_FUNCTIONS = ['_main']; // Functions that are explicitly exported, so they are guaranteed to + // be accessible outside of the generated code. + SHOW_LABELS = 0; // Show labels in the generated code // Compiler debugging options |