• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright JS Foundation and other contributors, http://js.foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include "shell.h"
20 #include "jerryscript.h"
21 #include "jerryscript-ext/handler.h"
22 #include "jerryscript-port.h"
23 
24 /**
25  * Standalone Jerry exit codes
26  */
27 #define JERRY_STANDALONE_EXIT_CODE_OK   (0)
28 #define JERRY_STANDALONE_EXIT_CODE_FAIL (1)
29 
30 /**
31  * Register a JavaScript function in the global object.
32  */
33 static void
register_js_function(const char * name_p,jerry_external_handler_t handler_p)34 register_js_function (const char *name_p, /**< name of the function */
35                       jerry_external_handler_t handler_p) /**< function callback */
36 {
37   jerry_value_t result_val = jerryx_handler_register_global ((const jerry_char_t *) name_p, handler_p);
38 
39   if (jerry_value_is_error (result_val))
40   {
41     printf ("Warning: failed to register '%s' method.", name_p);
42   }
43 
44   jerry_release_value (result_val);
45 } /* register_js_function */
46 
47 /**
48  * Jerryscript simple test
49  */
test_jerry(int argc,char ** argv)50 int test_jerry (int argc, char **argv)
51 {
52   /* Suppress compiler errors */
53   (void) argc;
54   (void) argv;
55 
56   jerry_value_t ret_value = jerry_create_undefined ();
57 
58   const jerry_char_t script[] = "print ('Hello, World!');";
59   printf ("This test run the following script code: [%s]\n\n", script);
60 
61   /* Initialize engine */
62   jerry_init (JERRY_INIT_EMPTY);
63 
64   /* Register the print function in the global object. */
65   register_js_function ("print", jerryx_handler_print);
66 
67   /* Setup Global scope code */
68   ret_value = jerry_parse (NULL, 0, script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
69 
70   if (!jerry_value_is_error (ret_value))
71   {
72     /* Execute the parsed source code in the Global scope */
73     ret_value = jerry_run (ret_value);
74   }
75 
76   int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
77 
78   if (jerry_value_is_error (ret_value))
79   {
80     printf ("Script Error!");
81 
82     ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
83   }
84 
85   jerry_release_value (ret_value);
86 
87   /* Cleanup engine */
88   jerry_cleanup ();
89 
90   return ret_code;
91 
92 } /* test_jerry */
93 
94 const shell_command_t shell_commands[] = {
95   { "test", "Jerryscript Hello World test", test_jerry },
96   { NULL, NULL, NULL }
97 };
98 
main(void)99 int main (void)
100 {
101   union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
102   srand (now.u);
103   printf ("You are running RIOT on a(n) %s board.\n", RIOT_BOARD);
104   printf ("This board features a(n) %s MCU.\n", RIOT_MCU);
105 
106   /* start the shell */
107   char line_buf[SHELL_DEFAULT_BUFSIZE];
108   shell_run (shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
109 
110   return 0;
111 }
112