targets/zephyr: Update to new Jerry API and improve REPL interaction.
Various calls updated to use API from jerry-api.h (to-be JerryScript 1.0
API).
Use jerry_eval() instead of jerry_run_simple(), as it allow to store (and
then access) variables in a global environment. E.g. following now works
(entered and executed as two separate lines):
a = 1
print(a)
JerryScript-DCO-1.0-Signed-off-by: Paul Sokolovsky paul.sokolovsky@linaro.org
This commit is contained in:
@@ -21,7 +21,7 @@
|
|||||||
#include <misc/printk.h>
|
#include <misc/printk.h>
|
||||||
#include <misc/shell.h>
|
#include <misc/shell.h>
|
||||||
|
|
||||||
#include "jerry.h"
|
#include "jerry-api.h"
|
||||||
|
|
||||||
#if defined (CONFIG_STDOUT_CONSOLE)
|
#if defined (CONFIG_STDOUT_CONSOLE)
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -41,19 +41,19 @@ static unsigned char flags = 0;
|
|||||||
*/
|
*/
|
||||||
int jerryscript_test ()
|
int jerryscript_test ()
|
||||||
{
|
{
|
||||||
jerry_completion_code_t ret_code;
|
jerry_value_t ret_val;
|
||||||
|
|
||||||
const char script[] =
|
const char script[] =
|
||||||
"var test=0; " \
|
"var test=0; " \
|
||||||
"for (var t=100; t<1000; t++) test+=t; " \
|
"for (var t=100; t<1000; t++) test+=t; " \
|
||||||
"print ('Hi JS World! '+test);";
|
"print ('Hi JS World! '+test);";
|
||||||
|
|
||||||
printf ("Script [%s]\n",script);
|
printf ("Script [%s]\n", script);
|
||||||
ret_code = jerry_run_simple ((jerry_char_t *) script,
|
ret_val = jerry_eval ((jerry_char_t *) script,
|
||||||
strlen (script),
|
strlen (script),
|
||||||
JERRY_FLAG_EMPTY);
|
false);
|
||||||
|
|
||||||
return ret_code;
|
return jerry_value_has_error_flag (ret_val) ? -1 : 0;
|
||||||
} /* jerryscript_test */
|
} /* jerryscript_test */
|
||||||
|
|
||||||
|
|
||||||
@@ -76,9 +76,7 @@ static int shell_cmd_version (int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
uint32_t version = sys_kernel_version_get ();
|
uint32_t version = sys_kernel_version_get ();
|
||||||
|
|
||||||
printf ("Jerryscript %s %s %s\n", jerry_branch_name,
|
printf ("Jerryscript API %d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION);
|
||||||
jerry_build_date,
|
|
||||||
jerry_commit_hash);
|
|
||||||
|
|
||||||
printk ("Zephyr version %d.%d.%d\n", SYS_KERNEL_VER_MAJOR (version),
|
printk ("Zephyr version %d.%d.%d\n", SYS_KERNEL_VER_MAJOR (version),
|
||||||
SYS_KERNEL_VER_MINOR (version),
|
SYS_KERNEL_VER_MINOR (version),
|
||||||
@@ -127,19 +125,21 @@ static int shell_cmd_handler (int argc, char *argv[])
|
|||||||
printf ("[%s] %lu\n", source_buffer, strlen (source_buffer));
|
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),
|
strlen (source_buffer),
|
||||||
JERRY_FLAG_EMPTY);
|
false);
|
||||||
|
|
||||||
free (source_buffer);
|
free (source_buffer);
|
||||||
|
|
||||||
if (ret_code != JERRY_COMPLETION_CODE_OK)
|
if (jerry_value_has_error_flag (ret_val))
|
||||||
{
|
{
|
||||||
printf ("Failed to run JS\n");
|
printf ("Failed to run JS\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
jerry_release_value (ret_val);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
} /* shell_cmd_handler */
|
} /* shell_cmd_handler */
|
||||||
|
|
||||||
@@ -158,7 +158,12 @@ const struct shell_cmd commands[] =
|
|||||||
void main (void)
|
void main (void)
|
||||||
{
|
{
|
||||||
printf ("Jerry Compilation " __DATE__ " " __TIME__ "\n");
|
printf ("Jerry Compilation " __DATE__ " " __TIME__ "\n");
|
||||||
|
jerry_init (JERRY_INIT_EMPTY);
|
||||||
shell_register_app_cmd_handler (shell_cmd_handler);
|
shell_register_app_cmd_handler (shell_cmd_handler);
|
||||||
shell_init ("js> ", commands);
|
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 */
|
} /* main */
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user