diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-08-01 11:49:50 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-08-01 11:49:50 -0700 |
commit | ae08d741658d4c94e7e8bb4f8bfd429fe88784a2 (patch) | |
tree | c4602d506799e2a2a64e4dd99ee5efdf6ae70173 | |
parent | f1ee7feb8addf5319cb9f2e0cc64426d0b4518d9 (diff) | |
parent | c1e78765a6380a38f6e69cb4cd808edc1d21a81b (diff) |
Merge pull request #1452 from inolen/onprerun
added addOnPreRun and addOnPostRun
-rw-r--r-- | src/postamble.js | 26 | ||||
-rw-r--r-- | src/preamble.js | 67 |
2 files changed, 57 insertions, 36 deletions
diff --git a/src/postamble.js b/src/postamble.js index 8fe3b65d..25a50bfc 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -6,7 +6,7 @@ var inMain; Module['callMain'] = Module.callMain = function callMain(args) { assert(runDependencies == 0, 'cannot call main when async dependencies remain! (listen on __ATMAIN__)'); - assert(!Module['preRun'] || Module['preRun'].length == 0, 'cannot call main when preRun functions remain to be called'); + assert(__ATPRERUN__.length == 0, 'cannot call main when preRun functions remain to be called'); args = args || []; @@ -74,17 +74,11 @@ function run(args) { return; } - if (Module['preRun']) { - if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']]; - var toRun = Module['preRun']; - Module['preRun'] = []; - for (var i = toRun.length-1; i >= 0; i--) { - toRun[i](); - } - if (runDependencies > 0) { - // a preRun added a dependency, run will be called later - return; - } + preRun(); + + if (runDependencies > 0) { + // a preRun added a dependency, run will be called later + return; } function doRun() { @@ -96,12 +90,8 @@ function run(args) { if (Module['_main'] && shouldRunNow) { Module['callMain'](args); } - if (Module['postRun']) { - if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']]; - while (Module['postRun'].length > 0) { - Module['postRun'].pop()(); - } - } + + postRun(); } if (Module['setStatus']) { diff --git a/src/preamble.js b/src/preamble.js index 57376ad3..2955c885 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -705,37 +705,74 @@ function callRuntimeCallbacks(callbacks) { } } -var __ATINIT__ = []; // functions called during startup -var __ATMAIN__ = []; // functions called when main() is to be run -var __ATEXIT__ = []; // functions called during shutdown +var __ATPRERUN__ = []; // functions called before the runtime is initialized +var __ATINIT__ = []; // functions called during startup +var __ATMAIN__ = []; // functions called when main() is to be run +var __ATEXIT__ = []; // functions called during shutdown +var __ATPOSTRUN__ = []; // functions called after the runtime has exited var runtimeInitialized = false; +function preRun() { + // compatibility - merge in anything from Module['preRun'] at this time + if (Module['preRun']) { + if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']]; + while (Module['preRun'].length) { + addOnPreRun(Module['preRun'].shift()); + } + } + callRuntimeCallbacks(__ATPRERUN__); +} + function ensureInitRuntime() { if (runtimeInitialized) return; runtimeInitialized = true; callRuntimeCallbacks(__ATINIT__); } + function preMain() { callRuntimeCallbacks(__ATMAIN__); } + function exitRuntime() { callRuntimeCallbacks(__ATEXIT__); } -Module['addOnInit'] = Module.addOnInit = function addOnInit(cb) { +function postRun() { + // compatibility - merge in anything from Module['postRun'] at this time + if (Module['postRun']) { + if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']]; + while (Module['postRun'].length) { + addOnPostRun(Module['postRun'].shift()); + } + } + callRuntimeCallbacks(__ATPOSTRUN__); +} + +function addOnPreRun(cb) { + __ATPRERUN__.unshift(cb); +} +Module['addOnPreRun'] = Module.addOnPreRun = addOnPreRun; + +function addOnInit(cb) { __ATINIT__.unshift(cb); -}; +} +Module['addOnInit'] = Module.addOnInit = addOnInit; -Module['addOnPreMain'] = Module.addOnPreMain = function addOnPreMain(cb) { +function addOnPreMain(cb) { __ATMAIN__.unshift(cb); -}; +} +Module['addOnPreMain'] = Module.addOnPreMain = addOnPreMain; -Module['addOnExit'] = Module.addOnExit = function addOnExit(cb) { +function addOnExit(cb) { __ATEXIT__.unshift(cb); -}; +} +Module['addOnExit'] = Module.addOnExit = addOnExit; -// TODO add onprerun, onpostrun +function addOnPostRun(cb) { + __ATPOSTRUN__.unshift(cb); +} +Module['addOnPostRun'] = Module.addOnPostRun = addOnPostRun; // Tools @@ -873,12 +910,6 @@ Module['removeRunDependency'] = removeRunDependency; Module["preloadedImages"] = {}; // maps url to image data Module["preloadedAudios"] = {}; // maps url to audio data -function addPreRun(func) { - if (!Module['preRun']) Module['preRun'] = []; - else if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']]; - Module['preRun'].push(func); -} - #if PGO var PGOMonitor = { called: {}, @@ -893,7 +924,7 @@ var PGOMonitor = { }; Module['PGOMonitor'] = PGOMonitor; __ATEXIT__.push({ func: function() { PGOMonitor.dump() } }); -addPreRun(function() { addRunDependency('pgo') }); +addOnPreRun(function() { addRunDependency('pgo') }); #endif function loadMemoryInitializer(filename) { @@ -906,7 +937,7 @@ function loadMemoryInitializer(filename) { } // always do this asynchronously, to keep shell and web as similar as possible - addPreRun(function() { + addOnPreRun(function() { if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) { applyData(Module['readBinary'](filename)); } else { |