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#ifndef _JERRYSCRIPT_MBED_UTIL_WRAPPERS_H
16425bb815Sopenharmony_ci#define _JERRYSCRIPT_MBED_UTIL_WRAPPERS_H
17425bb815Sopenharmony_ci
18425bb815Sopenharmony_ci/*
19425bb815Sopenharmony_ci * Used in header/source files for wrappers, to declare the signature of the
20425bb815Sopenharmony_ci * registration function.
21425bb815Sopenharmony_ci */
22425bb815Sopenharmony_ci#define DECLARE_JS_WRAPPER_REGISTRATION(NAME) \
23425bb815Sopenharmony_ci  void jsmbed_wrap_registry_entry__ ## NAME (void)
24425bb815Sopenharmony_ci
25425bb815Sopenharmony_ci//
26425bb815Sopenharmony_ci// 2. Wrapper function declaration/use macros
27425bb815Sopenharmony_ci//
28425bb815Sopenharmony_ci
29425bb815Sopenharmony_ci// Global functions
30425bb815Sopenharmony_ci#define DECLARE_GLOBAL_FUNCTION(NAME) \
31425bb815Sopenharmony_cijerry_value_t \
32425bb815Sopenharmony_ciNAME_FOR_GLOBAL_FUNCTION(NAME) (const jerry_value_t function_obj_p, \
33425bb815Sopenharmony_ci                  const jerry_value_t  this_obj, \
34425bb815Sopenharmony_ci                  const jerry_value_t    args[], \
35425bb815Sopenharmony_ci                  const jerry_length_t   args_count)
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci#define REGISTER_GLOBAL_FUNCTION(NAME) \
38425bb815Sopenharmony_ci  jsmbed_wrap_register_global_function ( # NAME, NAME_FOR_GLOBAL_FUNCTION(NAME) )
39425bb815Sopenharmony_ci
40425bb815Sopenharmony_ci#define REGISTER_GLOBAL_FUNCTION_WITH_HANDLER(NAME, HANDLER) \
41425bb815Sopenharmony_ci  jsmbed_wrap_register_global_function ( # NAME, HANDLER )
42425bb815Sopenharmony_ci
43425bb815Sopenharmony_ci// Class constructors
44425bb815Sopenharmony_ci#define DECLARE_CLASS_CONSTRUCTOR(CLASS) \
45425bb815Sopenharmony_cijerry_value_t \
46425bb815Sopenharmony_ciNAME_FOR_CLASS_CONSTRUCTOR(CLASS) (const jerry_value_t function_obj, \
47425bb815Sopenharmony_ci                  const jerry_value_t    this_obj, \
48425bb815Sopenharmony_ci                  const jerry_value_t    args[], \
49425bb815Sopenharmony_ci                  const jerry_length_t   args_count)
50425bb815Sopenharmony_ci
51425bb815Sopenharmony_ci#define REGISTER_CLASS_CONSTRUCTOR(CLASS) \
52425bb815Sopenharmony_ci  jsmbed_wrap_register_class_constructor ( # CLASS, NAME_FOR_CLASS_CONSTRUCTOR(CLASS) )
53425bb815Sopenharmony_ci
54425bb815Sopenharmony_ci// Class functions
55425bb815Sopenharmony_ci#define DECLARE_CLASS_FUNCTION(CLASS, NAME) \
56425bb815Sopenharmony_cijerry_value_t \
57425bb815Sopenharmony_ciNAME_FOR_CLASS_FUNCTION(CLASS, NAME) (const jerry_value_t function_obj, \
58425bb815Sopenharmony_ci                  const jerry_value_t  this_obj, \
59425bb815Sopenharmony_ci                  const jerry_value_t    args[], \
60425bb815Sopenharmony_ci                  const jerry_length_t   args_count)
61425bb815Sopenharmony_ci
62425bb815Sopenharmony_ci#define ATTACH_CLASS_FUNCTION(OBJECT, CLASS, NAME) \
63425bb815Sopenharmony_ci  jsmbed_wrap_register_class_function (OBJECT, # NAME, NAME_FOR_CLASS_FUNCTION(CLASS, NAME) )
64425bb815Sopenharmony_ci
65425bb815Sopenharmony_ci//
66425bb815Sopenharmony_ci// 3. Argument checking macros
67425bb815Sopenharmony_ci//
68425bb815Sopenharmony_ci#define CHECK_ARGUMENT_COUNT(CLASS, NAME, EXPR) \
69425bb815Sopenharmony_ci    if (!(EXPR)) { \
70425bb815Sopenharmony_ci        const char* error_msg = "ERROR: wrong argument count for " # CLASS "." # NAME ", expected " # EXPR "."; \
71425bb815Sopenharmony_ci        return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg)); \
72425bb815Sopenharmony_ci    }
73425bb815Sopenharmony_ci
74425bb815Sopenharmony_ci#define CHECK_ARGUMENT_TYPE_ALWAYS(CLASS, NAME, INDEX, TYPE) \
75425bb815Sopenharmony_ci    if (!jerry_value_is_ ## TYPE (args[INDEX])) { \
76425bb815Sopenharmony_ci        const char* error_msg = "ERROR: wrong argument type for " # CLASS "." # NAME ", expected argument " # INDEX " to be a " # TYPE ".\n"; \
77425bb815Sopenharmony_ci        return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg)); \
78425bb815Sopenharmony_ci    }
79425bb815Sopenharmony_ci
80425bb815Sopenharmony_ci#define CHECK_ARGUMENT_TYPE_ON_CONDITION(CLASS, NAME, INDEX, TYPE, EXPR) \
81425bb815Sopenharmony_ci    if ((EXPR)) { \
82425bb815Sopenharmony_ci        if (!jerry_value_is_ ## TYPE (args[INDEX])) { \
83425bb815Sopenharmony_ci            const char* error_msg = "ERROR: wrong argument type for " # CLASS "." # NAME ", expected argument " # INDEX " to be a " # TYPE ".\n"; \
84425bb815Sopenharmony_ci            return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg)); \
85425bb815Sopenharmony_ci        } \
86425bb815Sopenharmony_ci    }
87425bb815Sopenharmony_ci
88425bb815Sopenharmony_ci#define NAME_FOR_GLOBAL_FUNCTION(NAME) __gen_jsmbed_global_func_ ## NAME
89425bb815Sopenharmony_ci#define NAME_FOR_CLASS_CONSTRUCTOR(CLASS) __gen_jsmbed_class_constructor_ ## CLASS
90425bb815Sopenharmony_ci#define NAME_FOR_CLASS_FUNCTION(CLASS, NAME) __gen_jsmbed_func_c_ ## CLASS ## _f_ ## NAME
91425bb815Sopenharmony_ci
92425bb815Sopenharmony_ci#define NAME_FOR_CLASS_NATIVE_CONSTRUCTOR(CLASS, TYPELIST) __gen_native_jsmbed_ ## CLASS ## __Special_create_ ## TYPELIST
93425bb815Sopenharmony_ci#define NAME_FOR_CLASS_NATIVE_DESTRUCTOR(CLASS) __gen_native_jsmbed_ ## CLASS ## __Special_destroy
94425bb815Sopenharmony_ci#define NAME_FOR_CLASS_NATIVE_FUNCTION(CLASS, NAME) __gen_native_jsmbed_ ## CLASS ## _ ## NAME
95425bb815Sopenharmony_ci
96425bb815Sopenharmony_ci#endif  // _JERRYSCRIPT_MBED_UTIL_WRAPPERS_H
97