From 93f5f802165c3b8ab01a0393409cbd1c352596c8 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 28 Apr 2019 22:16:27 +1000 Subject: [PATCH] javascript: Pass (error) exit value out from script to process caller. --- ports/javascript/main.c | 19 +++++++++++++++---- ports/javascript/wrapper.js | 6 +++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/ports/javascript/main.c b/ports/javascript/main.c index 65bae98b44..f8ef0e7c55 100644 --- a/ports/javascript/main.c +++ b/ports/javascript/main.c @@ -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) { diff --git a/ports/javascript/wrapper.js b/ports/javascript/wrapper.js index 84ad2ae3a7..ae0f24e7e4 100644 --- a/ports/javascript/wrapper.js +++ b/ports/javascript/wrapper.js @@ -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; \ No newline at end of file +Module["onRuntimeInitialized"] = mainProgram;