Skip to content

targets/zephyr: Update to new Jerry API and improve REPL interaction. #1190

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 12, 2016
Merged
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
31 changes: 18 additions & 13 deletions targets/zephyr/src/main-zephyr.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <misc/printk.h>
#include <misc/shell.h>

#include "jerry.h"
#include "jerry-api.h"

#if defined (CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
Expand All @@ -41,19 +41,19 @@ static unsigned char flags = 0;
*/
int jerryscript_test ()
{
jerry_completion_code_t ret_code;
jerry_value_t ret_val;

const char script[] =
"var test=0; " \
"for (var t=100; t<1000; t++) test+=t; " \
"print ('Hi JS World! '+test);";

printf ("Script [%s]\n",script);
ret_code = jerry_run_simple ((jerry_char_t *) script,
printf ("Script [%s]\n", script);
ret_val = jerry_eval ((jerry_char_t *) script,
strlen (script),
JERRY_FLAG_EMPTY);
false);

return ret_code;
return jerry_value_has_error_flag (ret_val) ? -1 : 0;
} /* jerryscript_test */


Expand All @@ -76,9 +76,7 @@ static int shell_cmd_version (int argc, char *argv[])
{
uint32_t version = sys_kernel_version_get ();

printf ("Jerryscript %s %s %s\n", jerry_branch_name,
jerry_build_date,
jerry_commit_hash);
printf ("Jerryscript API %d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION);

printk ("Zephyr version %d.%d.%d\n", SYS_KERNEL_VER_MAJOR (version),
SYS_KERNEL_VER_MINOR (version),
Expand Down Expand Up @@ -127,19 +125,21 @@ static int shell_cmd_handler (int argc, char *argv[])
printf ("[%s] %lu\n", source_buffer, strlen (source_buffer));
}

jerry_completion_code_t ret_code;
jerry_value_t ret_val;

ret_code = jerry_run_simple ((jerry_char_t *) source_buffer,
ret_val = jerry_eval ((jerry_char_t *) source_buffer,
strlen (source_buffer),
JERRY_FLAG_EMPTY);
false);

free (source_buffer);

if (ret_code != JERRY_COMPLETION_CODE_OK)
if (jerry_value_has_error_flag (ret_val))
{
printf ("Failed to run JS\n");
}

jerry_release_value (ret_val);

return 0;
} /* shell_cmd_handler */

Expand All @@ -158,7 +158,12 @@ const struct shell_cmd commands[] =
void main (void)
{
printf ("Jerry Compilation " __DATE__ " " __TIME__ "\n");
jerry_init (JERRY_INIT_EMPTY);
shell_register_app_cmd_handler (shell_cmd_handler);
shell_init ("js> ", commands);
/* Don't call jerry_cleanup() here, as shell_init() returns after setting
up background task to process shell input, and that task calls
shell_cmd_handler(), etc. as callbacks. This processing happens in
the infinite loop, so JerryScript doesn't need to be de-initialized. */
} /* main */
Copy link
Contributor

Choose a reason for hiding this comment

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

jerry_cleanup should be called before the end of main

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@LaszloLango : That's not the case with Zephyr. Generally, a typical embedded app would work as an infinite loop (it "exits" via hardware reset), and specifically with Zephyr, shell_init() just sets up background task and exit, all actual input passed to callbacks at later time. So, it's not ok to deinitialize Jerry after call to shell_init(), and per above, no need to deinitialize at all. Hope that makes sense. I'm adding a comment to the patch with this info.

Copy link
Contributor

Choose a reason for hiding this comment

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

@LaszloLango

shell_init will spawn a fiber that performs the shell task.
task_fiber_start(stack, STACKSIZE, shell, 0, 0, 7, 0);

If you clean zephyr there it will just crash.

With the change of running code on eval i think pfalcon is right on not cleaning up after execution.

Copy link
Contributor

Choose a reason for hiding this comment

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

@pfalcon, @sergioamr, OK, thanks for the explanation.