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#ifndef JERRYX_HANDLER_H
17#define JERRYX_HANDLER_H
18
19#include "jerryscript.h"
20
21#ifdef __cplusplus
22extern "C"
23{
24#endif /* __cplusplus */
25
26/*
27 * Handler registration helper
28 */
29
30jerry_value_t jerryx_handler_register_global (const jerry_char_t *name_p,
31 jerry_external_handler_t handler_p);
32
33/*
34 * Common external function handlers
35 */
36
37jerry_value_t jerryx_handler_assert_fatal (const jerry_value_t func_obj_val, const jerry_value_t this_p,
38 const jerry_value_t args_p[], const jerry_length_t args_cnt);
39jerry_value_t jerryx_handler_assert_throw (const jerry_value_t func_obj_val, const jerry_value_t this_p,
40 const jerry_value_t args_p[], const jerry_length_t args_cnt);
41jerry_value_t jerryx_handler_assert (const jerry_value_t func_obj_val, const jerry_value_t this_p,
42 const jerry_value_t args_p[], const jerry_length_t args_cnt);
43jerry_value_t jerryx_handler_gc (const jerry_value_t func_obj_val, const jerry_value_t this_p,
44 const jerry_value_t args_p[], const jerry_length_t args_cnt);
45jerry_value_t jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this_p,
46 const jerry_value_t args_p[], const jerry_length_t args_cnt);
47jerry_value_t jerryx_handler_resource_name (const jerry_value_t func_obj_val, const jerry_value_t this_p,
48 const jerry_value_t args_p[], const jerry_length_t args_cnt);
49
50/**
51 * Struct used by the `jerryx_set_functions` method to
52 * register multiple methods for a given object.
53 */
54typedef struct
55{
56 const char *name; /**< name of the property to add */
57 jerry_value_t value; /**< value of the property */
58} jerryx_property_entry;
59
60#define JERRYX_PROPERTY_NUMBER(NAME, NUMBER) (jerryx_property_entry) { NAME, jerry_create_number (NUMBER) }
61#define JERRYX_PROPERTY_STRING(NAME, STR) \
62 (jerryx_property_entry) { NAME, jerry_create_string_from_utf8 ((const jerry_char_t *) STR) }
63#define JERRYX_PROPERTY_STRING_SZ(NAME, STR, SIZE) \
64 (jerryx_property_entry) { NAME, jerry_create_string_sz_from_utf8 ((const jerry_char_t *) STR, SIZE) }
65#define JERRYX_PROPERTY_BOOLEAN(NAME, VALUE) (jerryx_property_entry) { NAME, jerry_create_boolean (VALUE) }
66#define JERRYX_PROPERTY_FUNCTION(NAME, FUNC) (jerryx_property_entry) { NAME, jerry_create_external_function (FUNC) }
67#define JERRYX_PROPERTY_UNDEFINED(NAME) (jerryx_property_entry) { NAME, jerry_create_undefined() }
68#define JERRYX_PROPERTY_LIST_END() (jerryx_property_entry) { NULL, 0 }
69
70/**
71 * Stores the result of property register operation.
72 */
73typedef struct
74{
75 jerry_value_t result; /**< result of property registraion (undefined or error object) */
76 uint32_t registered; /**< number of successfully registered methods */
77} jerryx_register_result;
78
79jerryx_register_result
80jerryx_set_properties (const jerry_value_t target_object,
81 const jerryx_property_entry entries[]);
82
83void
84jerryx_release_property_entry (const jerryx_property_entry entries[],
85 const jerryx_register_result register_result);
86
87jerry_value_t
88jerryx_set_property_str (const jerry_value_t target_object,
89 const char *name,
90 const jerry_value_t value);
91
92jerry_value_t
93jerryx_get_property_str (const jerry_value_t target_object,
94 const char *name);
95
96bool
97jerryx_has_property_str (const jerry_value_t target_object,
98 const char *name);
99
100#ifdef __cplusplus
101}
102#endif /* __cplusplus */
103#endif /* !JERRYX_HANDLER_H */
104