• 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 #include "mbed.h"
16 #include "rtos.h"
17 
18 #include "jerry-core/include/jerryscript.h"
19 #include "jerry-core/include/jerryscript-port.h"
20 
21 #include "jerryscript-mbed-event-loop/EventLoop.h"
22 
23 #include "jerryscript-mbed-util/js_source.h"
24 #include "jerryscript-mbed-library-registry/registry.h"
25 
26 #include "jerryscript-mbed-launcher/launcher.h"
27 #include "jerryscript-mbed-launcher/setup.h"
28 
29 #include "jerry-targetjs.h"
30 
31 DECLARE_JS_CODES;
32 
33 /**
34  * load_javascript
35  *
36  * Parse and run javascript files specified in jerry-targetjs.h
37  */
load_javascript()38 static int load_javascript() {
39     for (int src = 0; js_codes[src].source; src++) {
40         LOG_PRINT("running js file %s\r\n", js_codes[src].name);
41 
42         const jerry_char_t* code = reinterpret_cast<const jerry_char_t*>(js_codes[src].source);
43         const size_t length = js_codes[src].length;
44 
45         jerry_value_t parsed_code = jerry_parse(NULL, 0, code, length, JERRY_PARSE_NO_OPTS);
46 
47         if (jerry_value_is_error(parsed_code)) {
48             LOG_PRINT_ALWAYS("jerry_parse failed [%s]\r\n", js_codes[src].name);
49             jerry_release_value(parsed_code);
50             jsmbed_js_exit();
51             return -1;
52         }
53 
54         jerry_value_t returned_value = jerry_run(parsed_code);
55         jerry_release_value(parsed_code);
56 
57         if (jerry_value_is_error(returned_value)) {
58             LOG_PRINT_ALWAYS("jerry_run failed [%s]\r\n", js_codes[src].name);
59             jerry_release_value(returned_value);
60             jsmbed_js_exit();
61             return -1;
62         }
63 
64         jerry_release_value(returned_value);
65     }
66 
67     return 0;
68 }
69 
jsmbed_js_init()70 int jsmbed_js_init() {
71     union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
72     srand (now.u);
73     jerry_init_flag_t flags = JERRY_INIT_EMPTY;
74     jerry_init(flags);
75 
76     jsmbed_js_load_magic_strings();
77     mbed::js::LibraryRegistry::getInstance().register_all();
78 
79     return 0;
80 }
81 
jsmbed_js_exit()82 void jsmbed_js_exit() {
83     jerry_cleanup();
84 }
85 
jsmbed_js_launch()86 void jsmbed_js_launch() {
87     jsmbed_js_init();
88 
89     puts("   JerryScript in mbed\r\n");
90     puts("   build date:  " __DATE__ " \r\n");
91 
92     if (load_javascript() == 0) {
93         mbed::js::event_loop();
94     }
95 }
96