Skip to content

Commit 80dc523

Browse files
committed
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 [email protected]
1 parent 1cdc660 commit 80dc523

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

targets/zephyr/Makefile.zephyr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ ifndef ZEPHYR_BASE
4444
$(error Missing Zephyr base, did you source zephyr-env.sh? )
4545
endif
4646

47+
# -cp is required for user-friendly error messages in REPL
4748
VARIETY ?= -cp_minimal
4849

4950
INTERM = build/$(BOARD)/obj-$(BOARD)

targets/zephyr/src/main-zephyr.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
static char *source_buffer = NULL;
3535
static unsigned char flags = 0;
36+
static jerry_value_t print_function;
3637

3738
#define VERBOSE 0x01
3839

@@ -135,7 +136,22 @@ static int shell_cmd_handler (int argc, char *argv[])
135136

136137
if (jerry_value_has_error_flag (ret_val))
137138
{
138-
printf ("Failed to run JS\n");
139+
/* User-friendly error messages require at least "cp" JerryScript
140+
profile. Include a message prefix in case "cp_minimal" profile
141+
is used. */
142+
printf ("Error executing statement: ");
143+
/* Clear error flag, otherwise print call below won't produce any
144+
output. */
145+
jerry_value_clear_error_flag (&ret_val);
146+
}
147+
148+
if (!jerry_value_has_error_flag (print_function))
149+
{
150+
jerry_value_t ret_val_print = jerry_call_function (print_function,
151+
jerry_create_undefined (),
152+
&ret_val,
153+
1);
154+
jerry_release_value (ret_val_print);
139155
}
140156

141157
jerry_release_value (ret_val);
@@ -159,6 +175,17 @@ void main (void)
159175
{
160176
printf ("Jerry Compilation " __DATE__ " " __TIME__ "\n");
161177
jerry_init (JERRY_INIT_EMPTY);
178+
jerry_value_t global_obj_val = jerry_get_global_object ();
179+
180+
jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print");
181+
print_function = jerry_get_property (global_obj_val, print_func_name_val);
182+
jerry_release_value (print_func_name_val);
183+
jerry_release_value (global_obj_val);
184+
if (jerry_value_has_error_flag (print_function))
185+
{
186+
printf ("Error: could not look up print function, expression results won't be printed\n");
187+
}
188+
162189
shell_register_app_cmd_handler (shell_cmd_handler);
163190
shell_init ("js> ", commands);
164191
/* Don't call jerry_cleanup() here, as shell_init() returns after setting

0 commit comments

Comments
 (0)