From 80dc523ad325d25bf766cacb9720593553bef31a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 13 Jul 2016 14:04:56 +0300 Subject: [PATCH] targets/zephyr: REPL: Print expression result, or exception value. With this change, REPL works almost the same as a standard JS REPL does (browser console, Node.js, etc.). A result of each expression is printed, e.g. "2+2" will print "4". Result is printed even if its value is "undefined", e.g. "print(1)" will print "1", and on the next line "undefined" (which is again how "normal" JS console work, except a normal JS way to print to console is console.log()). If an exception occured, a message "Error executing statement:", followed by external representation of exception object, is printed. Note that distinctive exception objects are supported only in "cp" (compact profile), not in "cp_minimal". In the latter case, there's only exception hierarchy top-level prototype, so any error message will look like: Error executing statement: [object Function] (That's the reason why there's error message prefix - so it weren't too confusing even if used in cp_minimal config. Unfortunately, compile-time JerryScript configuration isn't available to Zephyr's main.c, so there's no easy way to implement cp vs cp_minimal distinction). JerryScript-DCO-1.0-Signed-off-by: Paul Sokolovsky paul.sokolovsky@linaro.org --- targets/zephyr/Makefile.zephyr | 1 + targets/zephyr/src/main-zephyr.c | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/targets/zephyr/Makefile.zephyr b/targets/zephyr/Makefile.zephyr index 4762f6a594..cc68c0dd87 100755 --- a/targets/zephyr/Makefile.zephyr +++ b/targets/zephyr/Makefile.zephyr @@ -44,6 +44,7 @@ ifndef ZEPHYR_BASE $(error Missing Zephyr base, did you source zephyr-env.sh? ) endif +# -cp is required for user-friendly error messages in REPL VARIETY ?= -cp_minimal INTERM = build/$(BOARD)/obj-$(BOARD) diff --git a/targets/zephyr/src/main-zephyr.c b/targets/zephyr/src/main-zephyr.c index 3628de292e..3401862f91 100644 --- a/targets/zephyr/src/main-zephyr.c +++ b/targets/zephyr/src/main-zephyr.c @@ -33,6 +33,7 @@ static char *source_buffer = NULL; static unsigned char flags = 0; +static jerry_value_t print_function; #define VERBOSE 0x01 @@ -135,7 +136,22 @@ static int shell_cmd_handler (int argc, char *argv[]) if (jerry_value_has_error_flag (ret_val)) { - printf ("Failed to run JS\n"); + /* User-friendly error messages require at least "cp" JerryScript + profile. Include a message prefix in case "cp_minimal" profile + is used. */ + printf ("Error executing statement: "); + /* Clear error flag, otherwise print call below won't produce any + output. */ + jerry_value_clear_error_flag (&ret_val); + } + + if (!jerry_value_has_error_flag (print_function)) + { + jerry_value_t ret_val_print = jerry_call_function (print_function, + jerry_create_undefined (), + &ret_val, + 1); + jerry_release_value (ret_val_print); } jerry_release_value (ret_val); @@ -159,6 +175,17 @@ void main (void) { printf ("Jerry Compilation " __DATE__ " " __TIME__ "\n"); jerry_init (JERRY_INIT_EMPTY); + jerry_value_t global_obj_val = jerry_get_global_object (); + + jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print"); + print_function = jerry_get_property (global_obj_val, print_func_name_val); + jerry_release_value (print_func_name_val); + jerry_release_value (global_obj_val); + if (jerry_value_has_error_flag (print_function)) + { + printf ("Error: could not look up print function, expression results won't be printed\n"); + } + shell_register_app_cmd_handler (shell_cmd_handler); shell_init ("js> ", commands); /* Don't call jerry_cleanup() here, as shell_init() returns after setting