javascript: Pass (error) exit value out from script to process caller.

pull/1/head
Damien George 2019-04-28 22:16:27 +10:00
parent bd0bacb637
commit 93f5f80216
2 changed files with 18 additions and 7 deletions

View File

@ -39,7 +39,8 @@
#include "library.h"
#if MICROPY_ENABLE_COMPILER
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
int do_str(const char *src, mp_parse_input_kind_t input_kind) {
int ret = 0;
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
@ -51,18 +52,28 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) {
} else {
// uncaught exception
if (mp_obj_is_subclass_fast(mp_obj_get_type((mp_obj_t)nlr.ret_val), &mp_type_SystemExit)) {
// at the moment, the value of SystemExit is unused
mp_obj_t exit_val = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(nlr.ret_val));
if (exit_val != mp_const_none) {
mp_int_t int_val;
if (mp_obj_get_int_maybe(exit_val, &int_val)) {
ret = int_val & 255;
} else {
ret = 1;
}
}
} else {
mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
ret = 1;
}
}
return ret;
}
#endif
static char *stack_top;
void mp_js_do_str(const char *code) {
do_str(code, MP_PARSE_FILE_INPUT);
int mp_js_do_str(const char *code) {
return do_str(code, MP_PARSE_FILE_INPUT);
}
int mp_js_process_char(int c) {

View File

@ -29,7 +29,7 @@ var Module = {};
var mainProgram = function()
{
mp_js_init = Module.cwrap('mp_js_init', 'null', ['number']);
mp_js_do_str = Module.cwrap('mp_js_do_str', 'null', ['string']);
mp_js_do_str = Module.cwrap('mp_js_do_str', 'number', ['string']);
mp_js_init_repl = Module.cwrap('mp_js_init_repl', 'null', ['null']);
mp_js_process_char = Module.cwrap('mp_js_process_char', 'number', ['number']);
@ -69,9 +69,9 @@ var mainProgram = function()
}
});
} else {
mp_js_do_str(contents);
process.exitCode = mp_js_do_str(contents);
}
}
}
Module["onRuntimeInitialized"] = mainProgram;
Module["onRuntimeInitialized"] = mainProgram;