Skip to content

targets/zephyr: REPL: Print expression result, or exception value. #1199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions targets/zephyr/Makefile.zephyr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 28 additions & 1 deletion targets/zephyr/src/main-zephyr.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

static char *source_buffer = NULL;
static unsigned char flags = 0;
static jerry_value_t print_function;

#define VERBOSE 0x01

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd check here that the get_property call was successful or not.

if (jerry_value_has_error_flag (print_function))
{
  // something went wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original idea was along the lines I mentioned previously: "We know that call is not expected to fail, and this is an embedded port, so it's not exactly clear how to deal with failure". But I guess you're right, something may happen with a later refactor (e.g. "print" replaced with "console.log"), so I'll try to add some no-nonsense error handling for that case.

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
Expand Down