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 <stdlib.h>
16 #include <stdio.h>
17
18 #include "jerryscript-mbed-library-registry/wrap_tools.h"
19
jsmbed_wrap_register_global_function(const char * name,jerry_external_handler_t handler)20 bool jsmbed_wrap_register_global_function(const char* name, jerry_external_handler_t handler) {
21 jerry_value_t global_object_val = jerry_get_global_object();
22 jerry_value_t reg_function = jerry_create_external_function(handler);
23
24 bool is_ok = true;
25
26 if (!(jerry_value_is_function(reg_function)
27 && jerry_value_is_constructor(reg_function))) {
28 is_ok = false;
29 LOG_PRINT_ALWAYS("Error: jerry_create_external_function failed!\r\n");
30 jerry_release_value(global_object_val);
31 jerry_release_value(reg_function);
32 return is_ok;
33 }
34
35 if (jerry_value_is_error(reg_function)) {
36 is_ok = false;
37 LOG_PRINT_ALWAYS("Error: jerry_create_external_function has error flag! \r\n");
38 jerry_release_value(global_object_val);
39 jerry_release_value(reg_function);
40 return is_ok;
41 }
42
43 jerry_value_t jerry_name = jerry_create_string((jerry_char_t *) name);
44
45 jerry_value_t set_result = jerry_set_property(global_object_val, jerry_name, reg_function);
46
47
48 if (jerry_value_is_error(set_result)) {
49 is_ok = false;
50 LOG_PRINT_ALWAYS("Error: jerry_create_external_function failed: [%s]\r\n", name);
51 }
52
53 jerry_release_value(jerry_name);
54 jerry_release_value(global_object_val);
55 jerry_release_value(reg_function);
56 jerry_release_value(set_result);
57
58 return is_ok;
59 }
60
jsmbed_wrap_register_class_constructor(const char * name,jerry_external_handler_t handler)61 bool jsmbed_wrap_register_class_constructor(const char* name, jerry_external_handler_t handler) {
62 // Register class constructor as a global function
63 return jsmbed_wrap_register_global_function(name, handler);
64 }
65
jsmbed_wrap_register_class_function(jerry_value_t this_obj,const char * name,jerry_external_handler_t handler)66 bool jsmbed_wrap_register_class_function(jerry_value_t this_obj, const char* name, jerry_external_handler_t handler) {
67 jerry_value_t property_name = jerry_create_string(reinterpret_cast<const jerry_char_t *>(name));
68 jerry_value_t handler_obj = jerry_create_external_function(handler);
69
70 jerry_release_value(jerry_set_property(this_obj, property_name, handler_obj));
71
72 jerry_release_value(handler_obj);
73 jerry_release_value(property_name);
74
75 // TODO: check for errors, and return false in the case of errors
76 return true;
77 }
78