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 <string.h>
18 #include <stdlib.h>
19
20 #include <zephyr.h>
21 #include <misc/printk.h>
22 #include "getline-zephyr.h"
23
24 #include "jerryscript.h"
25 #include "jerryscript-port.h"
26 #include "jerryscript-ext/handler.h"
27
28 static jerry_value_t print_function;
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 jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Warning: failed to register '%s' method.", name_p);
42 }
43
44 jerry_release_value (result_val);
45 } /* register_js_function */
46
shell_cmd_handler(char * source_buffer)47 static int shell_cmd_handler (char *source_buffer)
48 {
49 jerry_value_t ret_val;
50
51 ret_val = jerry_eval ((jerry_char_t *) source_buffer,
52 strlen (source_buffer),
53 JERRY_PARSE_NO_OPTS);
54
55 if (jerry_value_is_error (ret_val))
56 {
57 /* User-friendly error messages require at least "cp" JerryScript
58 profile. Include a message prefix in case "cp_minimal" profile
59 is used. */
60 printf ("Error executing statement: ");
61 /* Clear error flag, otherwise print call below won't produce any
62 output. */
63 ret_val = jerry_get_value_from_error (ret_val, true);
64 }
65
66 if (!jerry_value_is_error (print_function))
67 {
68 jerry_value_t ret_val_print = jerry_call_function (print_function,
69 jerry_create_undefined (),
70 &ret_val,
71 1);
72 jerry_release_value (ret_val_print);
73 }
74
75 jerry_release_value (ret_val);
76
77 return 0;
78 } /* shell_cmd_handler */
79
main(void)80 void main (void)
81 {
82 union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
83 srand (now.u);
84 uint32_t zephyr_ver = sys_kernel_version_get ();
85 printf ("JerryScript build: " __DATE__ " " __TIME__ "\n");
86 printf ("JerryScript API %d.%d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION, JERRY_API_PATCH_VERSION);
87 printf ("Zephyr version %d.%d.%d\n", (int)SYS_KERNEL_VER_MAJOR (zephyr_ver),
88 (int)SYS_KERNEL_VER_MINOR (zephyr_ver),
89 (int)SYS_KERNEL_VER_PATCHLEVEL (zephyr_ver));
90
91 zephyr_getline_init ();
92 jerry_init (JERRY_INIT_EMPTY);
93 register_js_function ("print", jerryx_handler_print);
94 jerry_value_t global_obj_val = jerry_get_global_object ();
95
96 jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print");
97 print_function = jerry_get_property (global_obj_val, print_func_name_val);
98 jerry_release_value (print_func_name_val);
99 jerry_release_value (global_obj_val);
100 if (jerry_value_is_error (print_function))
101 {
102 printf ("Error: could not look up print function, expression results won't be printed\n");
103 }
104
105 while (1)
106 {
107 char *s;
108 printf("js> ");
109 fflush(stdout);
110 s = zephyr_getline ();
111 if (*s)
112 {
113 shell_cmd_handler (s);
114 }
115 }
116
117 /* As we never retturn from REPL above, don't call jerry_cleanup() here. */
118 } /* main */
119