diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-01-16 11:18:06 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-01-16 11:18:06 -0800 |
commit | 5fe3bcc54b6c7ee04d0b09cc3e44e0cd06b95405 (patch) | |
tree | 2cf6d3a26b321328941346235d0c4f7db919ab3b | |
parent | 58132ad51111bcb024f7a9f2bdce970324c05e00 (diff) |
refactor runtime init and exit code
-rw-r--r-- | src/jsifier.js | 6 | ||||
-rw-r--r-- | src/library.js | 2 | ||||
-rw-r--r-- | src/postamble.js | 4 | ||||
-rw-r--r-- | src/postamble_sharedlib.js | 2 | ||||
-rw-r--r-- | src/preamble.js | 26 | ||||
-rw-r--r-- | src/preamble_sharedlib.js | 16 |
6 files changed, 37 insertions, 19 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index b14c213f..db6936f6 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -254,9 +254,9 @@ function JSify(data, functionsOnly, givenFunctions) { assert(!item.lines); // FIXME remove this, after we are sure it isn't needed var ret = [item]; if (item.ident == '_llvm_global_ctors') { - item.JS = '\n__globalConstructor__ = function() {\n' + - item.ctors.map(function(ctor) { return ' ' + toNiceIdent(ctor) + '();' }).join('\n') + - '\n}\n'; + item.JS = '\n__ATINIT__ = __ATINIT__.concat([\n' + + item.ctors.map(function(ctor) { return ' { func: ' + toNiceIdent(ctor) + ' }' }).join(',\n') + + '\n]);\n'; return ret; } else { if (item.external && BUILD_AS_SHARED_LIB) { diff --git a/src/library.js b/src/library.js index 3ede01af..3c596ea1 100644 --- a/src/library.js +++ b/src/library.js @@ -1763,7 +1763,7 @@ LibraryManager.library = { ExitStatus.prototype.constructor = ExitStatus; #endif - __shutdownRuntime__(); + exitRuntime(); ABORT = true; #if CATCH_EXIT_CODE diff --git a/src/postamble.js b/src/postamble.js index 0e295171..27242363 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -32,12 +32,12 @@ Module.callMain = function callMain(args) { function run(args) { args = args || Module['arguments']; - __globalConstructor__(); + initRuntime(); var ret = null; if (Module['_main']) { ret = Module.callMain(args); - __shutdownRuntime__(); + exitRuntime(); } return ret; } diff --git a/src/postamble_sharedlib.js b/src/postamble_sharedlib.js index c7a03597..fa179253 100644 --- a/src/postamble_sharedlib.js +++ b/src/postamble_sharedlib.js @@ -4,7 +4,7 @@ {{GLOBAL_VARS}} function run(args) { - __globalConstructor__(); + initRuntime(); } Module['run'] = run; diff --git a/src/preamble.js b/src/preamble.js index 5bb904d5..0b9288c4 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -333,13 +333,8 @@ Module['printXULProfiling'] = printXULProfiling; // Runtime essentials //======================================== -var __globalConstructor__ = function globalConstructor() { -}; - var __THREW__ = false; // Used in checking for thrown exceptions. -var __ATEXIT__ = []; - var ABORT = false; var undef = 0; @@ -648,17 +643,26 @@ STACK_MAX = STACK_ROOT + TOTAL_STACK; STATICTOP = alignMemoryPage(STACK_MAX); -function __shutdownRuntime__() { - while(__ATEXIT__.length > 0) { - var atexit = __ATEXIT__.pop(); - var func = atexit.func; +function callRuntimeCallbacks(callbacks) { + while(callbacks.length > 0) { + var callback = callbacks.pop(); + var func = callback.func; if (typeof func === 'number') { func = FUNCTION_TABLE[func]; } - func(atexit.arg === undefined ? null : atexit.arg); + func(callback.arg === undefined ? null : callback.arg); } +} + +var __ATINIT__ = []; // functions called during startup +var __ATEXIT__ = []; // functions called during shutdown + +function initRuntime() { + callRuntimeCallbacks(__ATINIT__); +} - // allow browser to GC, set heaps to null? +function exitRuntime() { + callRuntimeCallbacks(__ATEXIT__); // Print summary of correction activity CorrectionsMonitor.print(); diff --git a/src/preamble_sharedlib.js b/src/preamble_sharedlib.js index 4b91c44c..2a071f6b 100644 --- a/src/preamble_sharedlib.js +++ b/src/preamble_sharedlib.js @@ -4,7 +4,21 @@ // Runtime essentials //======================================== -var __globalConstructor__ = function globalConstructor() { +function callRuntimeCallbacks(callbacks) { + while(callbacks.length > 0) { + var callback = callbacks.pop(); + var func = callback.func; + if (typeof func === 'number') { + func = FUNCTION_TABLE[func]; + } + func(callback.arg === undefined ? null : callback.arg); + } +} + +var __ATINIT__ = []; // functions called during startup + +function initRuntime() { + callRuntimeCallbacks(__ATINIT__); } // === Body === |