aboutsummaryrefslogtreecommitdiff
path: root/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher
diff options
context:
space:
mode:
Diffstat (limited to 'targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher')
-rw-r--r--targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/launcher.h21
-rw-r--r--targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/setup.h22
-rw-r--r--targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp95
-rw-r--r--targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/setup.cpp54
4 files changed, 192 insertions, 0 deletions
diff --git a/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/launcher.h b/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/launcher.h
new file mode 100644
index 00000000..ea49bae5
--- /dev/null
+++ b/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/launcher.h
@@ -0,0 +1,21 @@
+/* Copyright JS Foundation and other contributors, http://js.foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _JERRYSCRIPT_MBED_LAUNCHER_LAUNCHER_H
+#define _JERRYSCRIPT_MBED_LAUNCHER_LAUNCHER_H
+
+void jsmbed_js_launch(void);
+void jsmbed_js_exit(void);
+
+#endif // _JERRYSCRIPT_MBED_LAUNCHER_LAUNCHER_H
diff --git a/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/setup.h b/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/setup.h
new file mode 100644
index 00000000..f87a628c
--- /dev/null
+++ b/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/setup.h
@@ -0,0 +1,22 @@
+/* Copyright JS Foundation and other contributors, http://js.foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef _JERRYSCRIPT_MBED_LAUNCHER_SETUP_H
+#define _JERRYSCRIPT_MBED_LAUNCHER_SETUP_H
+
+#include "jerry-core/include/jerryscript.h"
+
+void jsmbed_js_load_magic_strings(void);
+
+#endif // _JERRYSCRIPT_MBED_LAUNCHER_SETUP_H
diff --git a/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp b/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp
new file mode 100644
index 00000000..e8623795
--- /dev/null
+++ b/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/launcher.cpp
@@ -0,0 +1,95 @@
+/* Copyright JS Foundation and other contributors, http://js.foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "mbed.h"
+#include "rtos.h"
+
+#include "jerry-core/include/jerryscript.h"
+#include "jerry-core/include/jerryscript-port.h"
+
+#include "jerryscript-mbed-event-loop/EventLoop.h"
+
+#include "jerryscript-mbed-util/js_source.h"
+#include "jerryscript-mbed-library-registry/registry.h"
+
+#include "jerryscript-mbed-launcher/launcher.h"
+#include "jerryscript-mbed-launcher/setup.h"
+
+#include "jerry-targetjs.h"
+
+DECLARE_JS_CODES;
+
+/**
+ * load_javascript
+ *
+ * Parse and run javascript files specified in jerry-targetjs.h
+ */
+static int load_javascript() {
+ for (int src = 0; js_codes[src].source; src++) {
+ LOG_PRINT("running js file %s\r\n", js_codes[src].name);
+
+ const jerry_char_t* code = reinterpret_cast<const jerry_char_t*>(js_codes[src].source);
+ const size_t length = js_codes[src].length;
+
+ jerry_value_t parsed_code = jerry_parse(code, length, NULL);
+
+ if (jerry_value_is_exception(parsed_code)) {
+ LOG_PRINT_ALWAYS("jerry_parse failed [%s]\r\n", js_codes[src].name);
+ jerry_value_free(parsed_code);
+ jsmbed_js_exit();
+ return -1;
+ }
+
+ jerry_value_t returned_value = jerry_run(parsed_code);
+ jerry_value_free(parsed_code);
+
+ if (jerry_value_is_exception(returned_value)) {
+ LOG_PRINT_ALWAYS("jerry_run failed [%s]\r\n", js_codes[src].name);
+ jerry_value_free(returned_value);
+ jsmbed_js_exit();
+ return -1;
+ }
+
+ jerry_value_free(returned_value);
+ }
+
+ return 0;
+}
+
+int jsmbed_js_init() {
+ union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
+ srand (now.u);
+ jerry_init_flag_t flags = JERRY_INIT_EMPTY;
+ jerry_init(flags);
+
+ jsmbed_js_load_magic_strings();
+ mbed::js::LibraryRegistry::getInstance().register_all();
+
+ return 0;
+}
+
+void jsmbed_js_exit() {
+ jerry_cleanup();
+}
+
+void jsmbed_js_launch() {
+ jsmbed_js_init();
+
+ puts(" JerryScript in mbed\r\n");
+ puts(" build date: " __DATE__ " \r\n");
+
+ if (load_javascript() == 0) {
+ mbed::js::event_loop();
+ }
+}
diff --git a/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/setup.cpp b/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/setup.cpp
new file mode 100644
index 00000000..fd19b713
--- /dev/null
+++ b/targets/os/mbedos5/jerryscript-mbed/jerryscript-mbed-launcher/source/setup.cpp
@@ -0,0 +1,54 @@
+/* Copyright JS Foundation and other contributors, http://js.foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "jerryscript-mbed-launcher/setup.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "jerryscript-mbed-util/logging.h"
+
+extern uint32_t jsmbed_js_magic_string_count;
+extern uint32_t jsmbed_js_magic_string_values[];
+
+extern const char *jsmbed_js_magic_strings[];
+extern const jerry_length_t jsmbed_js_magic_string_lengths[];
+
+void
+jsmbed_js_load_magic_strings ()
+{
+ if (jsmbed_js_magic_string_count == 0)
+ {
+ return;
+ }
+
+ jerry_register_magic_strings ((const jerry_char_t **) jsmbed_js_magic_strings,
+ jsmbed_js_magic_string_count,
+ jsmbed_js_magic_string_lengths);
+
+ jerry_value_t global = jerry_current_realm ();
+
+ for (unsigned int idx = 0; idx < jsmbed_js_magic_string_count; idx++)
+ {
+ jerry_value_t constant_value = jerry_number (jsmbed_js_magic_string_values[idx]);
+ jerry_value_t magic_string = jerry_string_sz (jsmbed_js_magic_strings[idx]);
+
+ jerry_value_free (jerry_object_set (global, magic_string, constant_value));
+
+ jerry_value_free (constant_value);
+ jerry_value_free (magic_string);
+ }
+
+ jerry_value_free (global);
+}