diff options
author | David Claughton <dave@eclecticdave.com> | 2011-11-08 21:13:59 +0000 |
---|---|---|
committer | David Claughton <dave@eclecticdave.com> | 2011-12-03 01:29:44 +0000 |
commit | ca12d620e84fc5284976fb3ecfb8ef30ee7cfe4a (patch) | |
tree | aec53cc648157cf6eab5469a472ba872c735da9e /src | |
parent | 62000632bb844fd0b6473ad0387ce3b82bc62909 (diff) |
Modify exit() to throw an object and catch it.
* Changed exit from throwing an text string to throwing
an Error-derived object encapsulating the exit status.
Then catch it in 'callMain' and return the status.
Enable this functionality by setting CATCH_EXIT_CODE in settings.js
Diffstat (limited to 'src')
-rw-r--r-- | src/library.js | 16 | ||||
-rw-r--r-- | src/postamble.js | 12 | ||||
-rw-r--r-- | src/settings.js | 4 |
3 files changed, 31 insertions, 1 deletions
diff --git a/src/library.js b/src/library.js index 113c955c..1f63e6d0 100644 --- a/src/library.js +++ b/src/library.js @@ -1752,9 +1752,25 @@ LibraryManager.library = { _exit: function(status) { // void _exit(int status); // http://pubs.opengroup.org/onlinepubs/000095399/functions/exit.html + +#if CATCH_EXIT_CODE + function ExitStatus() { + this.name = "ExitStatus"; + this.message = "Program terminated with exit(" + status + ")"; + this.status = status; + }; + ExitStatus.prototype = new Error(); + ExitStatus.prototype.constructor = ExitStatus; +#endif + __shutdownRuntime__(); ABORT = true; + +#if CATCH_EXIT_CODE + throw new ExitStatus(); +#else throw 'exit(' + status + ') called, at ' + new Error().stack; +#endif }, fork__deps: ['__setErrNo', '$ERRNO_CODES'], fork: function() { diff --git a/src/postamble.js b/src/postamble.js index c8fd44bd..51ffc6ee 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -17,7 +17,14 @@ Module.callMain = function callMain(args) { argv.push(0); argv = allocate(argv, 'i32', ALLOC_STATIC); +#if CATCH_EXIT_CODE + try { + return _main(argc, argv, 0); + } + catch(e) { if (e.name == "ExitStatus") return e.status; throw e; } +#else return _main(argc, argv, 0); +#endif } {{GLOBAL_VARS}} @@ -49,7 +56,10 @@ Module['noInitialRun'] = true; #endif if (!Module['noInitialRun']) { - run(); + var ret = run(); +#if CATCH_EXIT_CODE + print('Exit Status: ' + ret); +#endif } // {{POST_RUN_ADDITIONS}} diff --git a/src/settings.js b/src/settings.js index bcda4757..66762298 100644 --- a/src/settings.js +++ b/src/settings.js @@ -77,6 +77,10 @@ var CLOSURE_INLINE_PREVENTION_LINES = 50; // Functions of this number of lines o // code generated that tells the closure compiler not to // inline them. This is useful to prevent the generation of // overly large functions. +var CATCH_EXIT_CODE = 0; // If set, causes exit() to throw an exception object which is caught + // in a try..catch block and results in the exit status being + // returned from run(). If zero (the default), the program is just + // terminated with an error message. // Generated code debugging options var SAFE_HEAP = 0; // Check each write to the heap against a list of blocked addresses |