diff options
author | Anthony Pesch <inolen@gmail.com> | 2013-08-09 00:36:35 -0700 |
---|---|---|
committer | Anthony Pesch <inolen@gmail.com> | 2013-08-09 11:11:12 -0700 |
commit | a9bb2f0261fd88048d7e499df33808a23884cb02 (patch) | |
tree | 9fd6f9aebf7ca97f8220d914b2eecc20501f56f9 /src/postamble.js | |
parent | bd1d02e02cf0f39c8d7f672520910714d1c2d4e5 (diff) |
- always throw an exception in exit
- remove default exit status prints
- added EXITSTATUS global to enable exit callbacks to determine the status
Diffstat (limited to 'src/postamble.js')
-rw-r--r-- | src/postamble.js | 60 |
1 files changed, 28 insertions, 32 deletions
diff --git a/src/postamble.js b/src/postamble.js index 25a50bfc..ad265b32 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -2,7 +2,6 @@ // === Auto-generated postamble setup entry stuff === var initialStackTop; -var inMain; Module['callMain'] = Module.callMain = function callMain(args) { assert(runDependencies == 0, 'cannot call main when async dependencies remain! (listen on __ATMAIN__)'); @@ -27,40 +26,36 @@ Module['callMain'] = Module.callMain = function callMain(args) { argv.push(0); argv = allocate(argv, 'i32', ALLOC_NORMAL); + initialStackTop = STACKTOP; + + try { #if BENCHMARK - var start = Date.now(); + var start = Date.now(); #endif - initialStackTop = STACKTOP; - inMain = true; + var ret = Module['_main'](argc, argv, 0); - var ret; - try { - ret = Module['_main'](argc, argv, 0); +#if BENCHMARK + Module.realPrint('main() took ' + (Date.now() - start) + ' milliseconds'); +#endif + + // if we're not running an evented main loop, it's time to exit + if (!Module['noExitRuntime']) { + exit(ret); + } } catch(e) { - if (e && typeof e == 'object' && e.type == 'ExitStatus') { + if (e.name == 'ExitStatus') { // exit() throws this once it's done to make sure execution // has been stopped completely - Module.print('Exit Status: ' + e.value); - return e.value; + return; } else if (e == 'SimulateInfiniteLoop') { // running an evented main loop, don't immediately exit Module['noExitRuntime'] = true; + return; } else { throw e; } - } finally { - inMain = false; - } - -#if BENCHMARK - Module.realPrint('main() took ' + (Date.now() - start) + ' milliseconds'); -#endif - - // if we're not running an evented main loop, it's time to exit - if (!Module['noExitRuntime']) { - exit(ret); } } @@ -110,21 +105,21 @@ Module['run'] = Module.run = run; function exit(status) { ABORT = true; + EXITSTATUS = status; STACKTOP = initialStackTop; - // TODO call externally added 'exit' callbacks with the status code. - // It'd be nice to provide the same interface for all Module events (e.g. - // prerun, premain, postmain). Perhaps an EventEmitter so we can do: - // Module.on('exit', function (status) {}); - // exit the runtime exitRuntime(); - - if (inMain) { - // if we're still inside the callMain's try/catch, we need to throw an - // exception in order to immediately terminate execution. - throw { type: 'ExitStatus', value: status }; - } + + // throw an exception to halt the current execution + function ExitStatus() { + this.name = "ExitStatus"; + this.message = "Program terminated with exit(" + status + ")"; + this.status = status; + }; + ExitStatus.prototype = new Error(); + ExitStatus.prototype.constructor = ExitStatus; + throw new ExitStatus(); } Module['exit'] = Module.exit = exit; @@ -134,6 +129,7 @@ function abort(text) { } ABORT = true; + EXITSTATUS = 1; throw 'abort() at ' + (new Error().stack); } |