diff --git a/assets/disk0/returnany.js b/assets/disk0/returnany.js new file mode 100644 index 0000000..f14272e --- /dev/null +++ b/assets/disk0/returnany.js @@ -0,0 +1,3 @@ +println("This program attemps to read a string and returns it verbatim. If the underlying shell is not insane, errorlevel must be 0.\nNow, let's put this to the test..."); +print("Your word? "); +return read(); diff --git a/assets/disk0/tvdos/TVDOS.SYS b/assets/disk0/tvdos/TVDOS.SYS index 164e096..bbaf8fe 100644 --- a/assets/disk0/tvdos/TVDOS.SYS +++ b/assets/disk0/tvdos/TVDOS.SYS @@ -1,3 +1,19 @@ +// define exceptions +function InterruptedException(m) { + this.message = m; + this.stack = (new Error()).stack; +}; +InterruptedException.prototype = Object.create(Error.prototype); +InterruptedException.prototype.name = 'InterruptedException'; +InterruptedException.prototype.constructor = InterruptedException; +class SIG { + constructor(name, number) { + this.name = "SIG" + name; + this.number = number|0; + } +} +const SIGTERM = new SIG("TERM",15); +const SIGSEGV = new SIG("SEGV",11) // define TVDOS var _TVDOS = {}; _TVDOS.VERSION = "1.0"; @@ -127,20 +143,24 @@ var GL = eval(filesystem.readAll("A")); // @param cmdsrc JS source code // @param args arguments for the program, must be Array, and args[0] is always the name of the program, e.g. // for command line 'echo foo bar', args[0] must be 'echo' +// @return status returned by the program var execApp = (cmdsrc, args) => { var execAppPrg = eval( -`function InterruptedException(m){this.message=m;this.stack=(new Error()).stack;};` + -`InterruptedException.prototype=Object.create(Error.prototype);` + -`InterruptedException.prototype.name='InterruptedException';` + -`InterruptedException.prototype.constructor=InterruptedException;` + -`var _appStub=function(exec_args){${cmdsrc} -};try {_appStub;} -catch (e){ - if (e instanceof InterruptedException) 'SIGTERM'; - else 1; -}`); // making 'exec_args' a app-level global +`var _appStub=function(exec_args){${cmdsrc}\n};_appStub`); // making 'exec_args' a app-level global - return execAppPrg(args); + var status = 0; + + try { + status = execAppPrg(args); + return status; + } + catch (e) { + serial.printerr(`app execution interrupted -- ${e}\n${e.stack}`); + if (e instanceof InterruptedException) + return SIGTERM; + else + return (undefined == status) ? 0 : status; + } } /////////////////////////////////////////////////////////////////////////////// diff --git a/assets/disk0/tvdos/bin/command.js b/assets/disk0/tvdos/bin/command.js index 6941892..181f7a6 100644 --- a/assets/disk0/tvdos/bin/command.js +++ b/assets/disk0/tvdos/bin/command.js @@ -386,7 +386,7 @@ shell.execute = function(line) { sendLcdMsg(_G.shellProgramTitles[_G.shellProgramTitles.length - 1]); //serial.println(_G.shellProgramTitles); - return (ret == undefined) ? 0 : ret; + return ret; } } } @@ -457,8 +457,14 @@ if (goInteractive) { try { errorlevel = 0; // reset the number errorlevel = shell.execute(cmdbuf); - //if (isNaN(errorlevel)) errorlevel = 2; - serial.printerr(`errorlevel: ${errorlevel}`); + let rawE = errorlevel; + + if (errorlevel instanceof SIG) + errorlevel = errorlevel.name; + else if (errorlevel == undefined || (typeof errorlevel.trim == "function" && errorlevel.trim().length == 0) || isNaN(errorlevel)) + errorlevel = 0; + + serial.printerr(`errorlevel: ${errorlevel} (raw: ${rawE})`); } catch (e) { printerrln("\n"+(e.stack || e));