1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#include <stdio.h> 17425bb815Sopenharmony_ci#include <string.h> 18425bb815Sopenharmony_ci#include <stdlib.h> 19425bb815Sopenharmony_ci 20425bb815Sopenharmony_ci#include <zephyr.h> 21425bb815Sopenharmony_ci#include <misc/printk.h> 22425bb815Sopenharmony_ci#include "getline-zephyr.h" 23425bb815Sopenharmony_ci 24425bb815Sopenharmony_ci#include "jerryscript.h" 25425bb815Sopenharmony_ci#include "jerryscript-port.h" 26425bb815Sopenharmony_ci#include "jerryscript-ext/handler.h" 27425bb815Sopenharmony_ci 28425bb815Sopenharmony_cistatic jerry_value_t print_function; 29425bb815Sopenharmony_ci 30425bb815Sopenharmony_ci/** 31425bb815Sopenharmony_ci * Register a JavaScript function in the global object. 32425bb815Sopenharmony_ci */ 33425bb815Sopenharmony_cistatic void 34425bb815Sopenharmony_ciregister_js_function (const char *name_p, /**< name of the function */ 35425bb815Sopenharmony_ci jerry_external_handler_t handler_p) /**< function callback */ 36425bb815Sopenharmony_ci{ 37425bb815Sopenharmony_ci jerry_value_t result_val = jerryx_handler_register_global ((const jerry_char_t *) name_p, handler_p); 38425bb815Sopenharmony_ci 39425bb815Sopenharmony_ci if (jerry_value_is_error (result_val)) 40425bb815Sopenharmony_ci { 41425bb815Sopenharmony_ci jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Warning: failed to register '%s' method.", name_p); 42425bb815Sopenharmony_ci } 43425bb815Sopenharmony_ci 44425bb815Sopenharmony_ci jerry_release_value (result_val); 45425bb815Sopenharmony_ci} /* register_js_function */ 46425bb815Sopenharmony_ci 47425bb815Sopenharmony_cistatic int shell_cmd_handler (char *source_buffer) 48425bb815Sopenharmony_ci{ 49425bb815Sopenharmony_ci jerry_value_t ret_val; 50425bb815Sopenharmony_ci 51425bb815Sopenharmony_ci ret_val = jerry_eval ((jerry_char_t *) source_buffer, 52425bb815Sopenharmony_ci strlen (source_buffer), 53425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 54425bb815Sopenharmony_ci 55425bb815Sopenharmony_ci if (jerry_value_is_error (ret_val)) 56425bb815Sopenharmony_ci { 57425bb815Sopenharmony_ci /* User-friendly error messages require at least "cp" JerryScript 58425bb815Sopenharmony_ci profile. Include a message prefix in case "cp_minimal" profile 59425bb815Sopenharmony_ci is used. */ 60425bb815Sopenharmony_ci printf ("Error executing statement: "); 61425bb815Sopenharmony_ci /* Clear error flag, otherwise print call below won't produce any 62425bb815Sopenharmony_ci output. */ 63425bb815Sopenharmony_ci ret_val = jerry_get_value_from_error (ret_val, true); 64425bb815Sopenharmony_ci } 65425bb815Sopenharmony_ci 66425bb815Sopenharmony_ci if (!jerry_value_is_error (print_function)) 67425bb815Sopenharmony_ci { 68425bb815Sopenharmony_ci jerry_value_t ret_val_print = jerry_call_function (print_function, 69425bb815Sopenharmony_ci jerry_create_undefined (), 70425bb815Sopenharmony_ci &ret_val, 71425bb815Sopenharmony_ci 1); 72425bb815Sopenharmony_ci jerry_release_value (ret_val_print); 73425bb815Sopenharmony_ci } 74425bb815Sopenharmony_ci 75425bb815Sopenharmony_ci jerry_release_value (ret_val); 76425bb815Sopenharmony_ci 77425bb815Sopenharmony_ci return 0; 78425bb815Sopenharmony_ci} /* shell_cmd_handler */ 79425bb815Sopenharmony_ci 80425bb815Sopenharmony_civoid main (void) 81425bb815Sopenharmony_ci{ 82425bb815Sopenharmony_ci union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () }; 83425bb815Sopenharmony_ci srand (now.u); 84425bb815Sopenharmony_ci uint32_t zephyr_ver = sys_kernel_version_get (); 85425bb815Sopenharmony_ci printf ("JerryScript build: " __DATE__ " " __TIME__ "\n"); 86425bb815Sopenharmony_ci printf ("JerryScript API %d.%d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION, JERRY_API_PATCH_VERSION); 87425bb815Sopenharmony_ci printf ("Zephyr version %d.%d.%d\n", (int)SYS_KERNEL_VER_MAJOR (zephyr_ver), 88425bb815Sopenharmony_ci (int)SYS_KERNEL_VER_MINOR (zephyr_ver), 89425bb815Sopenharmony_ci (int)SYS_KERNEL_VER_PATCHLEVEL (zephyr_ver)); 90425bb815Sopenharmony_ci 91425bb815Sopenharmony_ci zephyr_getline_init (); 92425bb815Sopenharmony_ci jerry_init (JERRY_INIT_EMPTY); 93425bb815Sopenharmony_ci register_js_function ("print", jerryx_handler_print); 94425bb815Sopenharmony_ci jerry_value_t global_obj_val = jerry_get_global_object (); 95425bb815Sopenharmony_ci 96425bb815Sopenharmony_ci jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print"); 97425bb815Sopenharmony_ci print_function = jerry_get_property (global_obj_val, print_func_name_val); 98425bb815Sopenharmony_ci jerry_release_value (print_func_name_val); 99425bb815Sopenharmony_ci jerry_release_value (global_obj_val); 100425bb815Sopenharmony_ci if (jerry_value_is_error (print_function)) 101425bb815Sopenharmony_ci { 102425bb815Sopenharmony_ci printf ("Error: could not look up print function, expression results won't be printed\n"); 103425bb815Sopenharmony_ci } 104425bb815Sopenharmony_ci 105425bb815Sopenharmony_ci while (1) 106425bb815Sopenharmony_ci { 107425bb815Sopenharmony_ci char *s; 108425bb815Sopenharmony_ci printf("js> "); 109425bb815Sopenharmony_ci fflush(stdout); 110425bb815Sopenharmony_ci s = zephyr_getline (); 111425bb815Sopenharmony_ci if (*s) 112425bb815Sopenharmony_ci { 113425bb815Sopenharmony_ci shell_cmd_handler (s); 114425bb815Sopenharmony_ci } 115425bb815Sopenharmony_ci } 116425bb815Sopenharmony_ci 117425bb815Sopenharmony_ci /* As we never retturn from REPL above, don't call jerry_cleanup() here. */ 118425bb815Sopenharmony_ci} /* main */ 119