program titles are properly push/popped even when the program was errored

This commit is contained in:
minjaesong
2021-04-24 11:06:35 +09:00
parent 2fa8400150
commit 62e115a32e
2 changed files with 78 additions and 53 deletions

View File

@@ -393,7 +393,7 @@ con.getch = function() {
return sys.readKey(); return sys.readKey();
}; };
con.move = function(y, x) { con.move = function(y, x) {
print(String.fromCharCode(27,91)+(y|0)+";"+(x|0)+"H"); print("\x1B["+(y|0)+";"+(x|0)+"H");
}; };
con.addch = function(c) { con.addch = function(c) {
graphics.putSymbol(c|0); graphics.putSymbol(c|0);
@@ -437,33 +437,33 @@ con.resetkeybuf = function() {
sys.poke(-45, 0); sys.poke(-46, 0); sys.poke(-47, 0); sys.poke(-48, 0); sys.poke(-45, 0); sys.poke(-46, 0); sys.poke(-47, 0); sys.poke(-48, 0);
}; };
con.video_reverse = function() { con.video_reverse = function() {
print(String.fromCharCode(27,91)+"7m"); print("\x1B[7m");
}; };
con.color_fore = function(n) { // 0..7; -1 for transparent con.color_fore = function(n) { // 0..7; -1 for transparent
if (n < 0) if (n < 0)
print(String.fromCharCode(27,91)+"38;5;255m"); print("\x1B[38;5;255m");
else else
print(String.fromCharCode(27,91)+(((n|0) % 8)+30)+"m"); print("\x1B["+(((n|0) % 8)+30)+"m");
}; };
con.color_back = function(n) { // 0..7; -1 for transparent con.color_back = function(n) { // 0..7; -1 for transparent
if (n < 0) if (n < 0)
print(String.fromCharCode(27,91)+"48;5;255m"); print("\x1B[48;5;255m");
else else
print(String.fromCharCode(27,91)+(((n|0) % 8)+40)+"m"); print("\x1B["+(((n|0) % 8)+40)+"m");
}; };
con.color_pair = function(fore, back) { // 0..255 con.color_pair = function(fore, back) { // 0..255
print(String.fromCharCode(27,91)+"38;5;"+fore+"m"); print("\x1B[38;5;"+fore+"m");
print(String.fromCharCode(27,91)+"48;5;"+back+"m"); print("\x1B[48;5;"+back+"m");
}; };
con.clear = function() { con.clear = function() {
print(String.fromCharCode(27,91)+"2J"); print("\x1B[2J");
}; };
// @params arg 0 to hide, nonzero to show // @params arg 0 to hide, nonzero to show
con.curs_set = function(arg) { con.curs_set = function(arg) {
print(String.fromCharCode(27,91)+"?25"+(((arg|0) == 0) ? "l" : "h")); print("\x1B[?25"+(((arg|0) == 0) ? "l" : "h"));
}; };
con.reset_graphics = function() { con.reset_graphics = function() {
print(String.fromCharCode(27,91,109)); print("\x1B[m");
}; };
// returns current key-down status // returns current key-down status
con.poll_keys = function() { con.poll_keys = function() {

View File

@@ -66,10 +66,32 @@ function greet() {
println(welcome_text); println(welcome_text);
} }
function sendLcdMsg(s) { /*uninterruptible*/ function sendLcdMsg(s) {
for (let i = 1024; i < 1048; i++) { // making it uninterruptible
sys.poke(-i-1, (s === undefined) ? 0 : s.charCodeAt(i - 1024)|0); sys.poke(-1025, (s === undefined) ? 0 : s.charCodeAt(0)|0);
} sys.poke(-1026, (s === undefined) ? 0 : s.charCodeAt(1)|0);
sys.poke(-1027, (s === undefined) ? 0 : s.charCodeAt(2)|0);
sys.poke(-1028, (s === undefined) ? 0 : s.charCodeAt(3)|0);
sys.poke(-1029, (s === undefined) ? 0 : s.charCodeAt(4)|0);
sys.poke(-1030, (s === undefined) ? 0 : s.charCodeAt(5)|0);
sys.poke(-1031, (s === undefined) ? 0 : s.charCodeAt(6)|0);
sys.poke(-1032, (s === undefined) ? 0 : s.charCodeAt(7)|0);
sys.poke(-1033, (s === undefined) ? 0 : s.charCodeAt(8)|0);
sys.poke(-1034, (s === undefined) ? 0 : s.charCodeAt(9)|0);
sys.poke(-1035, (s === undefined) ? 0 : s.charCodeAt(10)|0);
sys.poke(-1036, (s === undefined) ? 0 : s.charCodeAt(11)|0);
sys.poke(-1037, (s === undefined) ? 0 : s.charCodeAt(12)|0);
sys.poke(-1038, (s === undefined) ? 0 : s.charCodeAt(13)|0);
sys.poke(-1039, (s === undefined) ? 0 : s.charCodeAt(14)|0);
sys.poke(-1040, (s === undefined) ? 0 : s.charCodeAt(15)|0);
sys.poke(-1041, (s === undefined) ? 0 : s.charCodeAt(16)|0);
sys.poke(-1042, (s === undefined) ? 0 : s.charCodeAt(17)|0);
sys.poke(-1043, (s === undefined) ? 0 : s.charCodeAt(18)|0);
sys.poke(-1044, (s === undefined) ? 0 : s.charCodeAt(19)|0);
sys.poke(-1045, (s === undefined) ? 0 : s.charCodeAt(20)|0);
sys.poke(-1046, (s === undefined) ? 0 : s.charCodeAt(21)|0);
sys.poke(-1047, (s === undefined) ? 0 : s.charCodeAt(22)|0);
sys.poke(-1048, (s === undefined) ? 0 : s.charCodeAt(23)|0);
} }
function trimStartRevSlash(s) { function trimStartRevSlash(s) {
@@ -384,18 +406,44 @@ shell.execute = function(line) {
}); });
} }
else { else {
if (_G.shellProgramTitles === undefined) _G.shellProgramTitles = []; let gotError = false;
_G.shellProgramTitles.push(cmd.toUpperCase())
sendLcdMsg(_G.shellProgramTitles[_G.shellProgramTitles.length - 1]);
//serial.println(_G.shellProgramTitles);
let ret = execApp(programCode, tokens); // return value of undefined will cast into 0 try {
errorlevel = 0; // reset the number
_G.shellProgramTitles.pop(); if (_G.shellProgramTitles === undefined) _G.shellProgramTitles = [];
sendLcdMsg(_G.shellProgramTitles[_G.shellProgramTitles.length - 1]); _G.shellProgramTitles.push(cmd.toUpperCase())
//serial.println(_G.shellProgramTitles); sendLcdMsg(_G.shellProgramTitles[_G.shellProgramTitles.length - 1]);
//serial.println(_G.shellProgramTitles);
return ret; errorlevel = execApp(programCode, tokens); // return value of undefined will cast into 0
}
catch (e) {
gotError = true;
serial.printerr(`[command.js] program quit with ${e}:\n${e.stack || '(stack trace unavailable)'}`);
if (`${e}`.startsWith("InterruptedException"))
errorlevel = SIGTERM.name;
else if (e instanceof IllegalAccessException || `${e}`.startsWith("net.torvald.tsvm.ErrorIllegalAccess"))
errorlevel = SIGSEGV.name;
// exception catched means something went wrong, so if errorlevel is found to be zero, force set to 1.
else
errorlevel = 1;
}
finally {
// sometimes no-error program may return nothing as the errorlevel; force set to 0 then.
if (!gotError && (errorlevel == undefined || (typeof errorlevel.trim == "function" && errorlevel.trim().length == 0) || isNaN(errorlevel)))
errorlevel = 0;
serial.printerr(`errorlevel: ${errorlevel}`);
_G.shellProgramTitles.pop();
sendLcdMsg(_G.shellProgramTitles[_G.shellProgramTitles.length - 1]);
//serial.println(_G.shellProgramTitles);
return errorlevel;
}
} }
} }
} }
@@ -448,7 +496,6 @@ if (goInteractive) {
while (true) { while (true) {
let key = con.getch(); let key = con.getch();
let gotError = false;
// printable chars // printable chars
if (key >= 32 && key <= 126) { if (key >= 32 && key <= 126) {
@@ -464,38 +511,16 @@ if (goInteractive) {
// enter // enter
else if (key === 10 || key === con.KEY_RETURN) { else if (key === 10 || key === con.KEY_RETURN) {
println(); println();
try {
errorlevel = 0; // reset the number
errorlevel = shell.execute(cmdbuf);
}
catch (e) {
gotError = true;
serial.printerr(`[command.js] program quit with ${e}:\n${e.stack || '(stack trace unavailable)'}`); shell.execute(cmdbuf);
if (`${e}`.startsWith("InterruptedException")) if (cmdbuf.trim().length > 0)
errorlevel = SIGTERM.name; cmdHistory.push(cmdbuf);
else if (e instanceof IllegalAccessException || `${e}`.startsWith("net.torvald.tsvm.ErrorIllegalAccess"))
errorlevel = SIGSEGV.name;
// exception catched means something went wrong, so if errorlevel is found to be zero, force set to 1.
else
errorlevel = 1;
}
finally {
// sometimes no-error program may return nothing as the errorlevel; force set to 0 then.
if (!gotError && (errorlevel == undefined || (typeof errorlevel.trim == "function" && errorlevel.trim().length == 0) || isNaN(errorlevel)))
errorlevel = 0;
serial.printerr(`errorlevel: ${errorlevel}`); cmdHistoryScroll = 0;
con.curs_set(1);
if (cmdbuf.trim().length > 0) break;
cmdHistory.push(cmdbuf);
cmdHistoryScroll = 0;
con.curs_set(1);
break;
}
} }
// up arrow // up arrow
else if (key === con.KEY_UP && cmdHistory.length > 0 && cmdHistoryScroll < cmdHistory.length) { else if (key === con.KEY_UP && cmdHistory.length > 0 && cmdHistoryScroll < cmdHistory.length) {