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#ifndef _JERRYSCRIPT_MBED_UTIL_WRAPPERS_H 16#define _JERRYSCRIPT_MBED_UTIL_WRAPPERS_H 17 18/* 19 * Used in header/source files for wrappers, to declare the signature of the 20 * registration function. 21 */ 22#define DECLARE_JS_WRAPPER_REGISTRATION(NAME) \ 23 void jsmbed_wrap_registry_entry__ ## NAME (void) 24 25// 26// 2. Wrapper function declaration/use macros 27// 28 29// Global functions 30#define DECLARE_GLOBAL_FUNCTION(NAME) \ 31jerry_value_t \ 32NAME_FOR_GLOBAL_FUNCTION(NAME) (const jerry_value_t function_obj_p, \ 33 const jerry_value_t this_obj, \ 34 const jerry_value_t args[], \ 35 const jerry_length_t args_count) 36 37#define REGISTER_GLOBAL_FUNCTION(NAME) \ 38 jsmbed_wrap_register_global_function ( # NAME, NAME_FOR_GLOBAL_FUNCTION(NAME) ) 39 40#define REGISTER_GLOBAL_FUNCTION_WITH_HANDLER(NAME, HANDLER) \ 41 jsmbed_wrap_register_global_function ( # NAME, HANDLER ) 42 43// Class constructors 44#define DECLARE_CLASS_CONSTRUCTOR(CLASS) \ 45jerry_value_t \ 46NAME_FOR_CLASS_CONSTRUCTOR(CLASS) (const jerry_value_t function_obj, \ 47 const jerry_value_t this_obj, \ 48 const jerry_value_t args[], \ 49 const jerry_length_t args_count) 50 51#define REGISTER_CLASS_CONSTRUCTOR(CLASS) \ 52 jsmbed_wrap_register_class_constructor ( # CLASS, NAME_FOR_CLASS_CONSTRUCTOR(CLASS) ) 53 54// Class functions 55#define DECLARE_CLASS_FUNCTION(CLASS, NAME) \ 56jerry_value_t \ 57NAME_FOR_CLASS_FUNCTION(CLASS, NAME) (const jerry_value_t function_obj, \ 58 const jerry_value_t this_obj, \ 59 const jerry_value_t args[], \ 60 const jerry_length_t args_count) 61 62#define ATTACH_CLASS_FUNCTION(OBJECT, CLASS, NAME) \ 63 jsmbed_wrap_register_class_function (OBJECT, # NAME, NAME_FOR_CLASS_FUNCTION(CLASS, NAME) ) 64 65// 66// 3. Argument checking macros 67// 68#define CHECK_ARGUMENT_COUNT(CLASS, NAME, EXPR) \ 69 if (!(EXPR)) { \ 70 const char* error_msg = "ERROR: wrong argument count for " # CLASS "." # NAME ", expected " # EXPR "."; \ 71 return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg)); \ 72 } 73 74#define CHECK_ARGUMENT_TYPE_ALWAYS(CLASS, NAME, INDEX, TYPE) \ 75 if (!jerry_value_is_ ## TYPE (args[INDEX])) { \ 76 const char* error_msg = "ERROR: wrong argument type for " # CLASS "." # NAME ", expected argument " # INDEX " to be a " # TYPE ".\n"; \ 77 return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg)); \ 78 } 79 80#define CHECK_ARGUMENT_TYPE_ON_CONDITION(CLASS, NAME, INDEX, TYPE, EXPR) \ 81 if ((EXPR)) { \ 82 if (!jerry_value_is_ ## TYPE (args[INDEX])) { \ 83 const char* error_msg = "ERROR: wrong argument type for " # CLASS "." # NAME ", expected argument " # INDEX " to be a " # TYPE ".\n"; \ 84 return jerry_create_error(JERRY_ERROR_TYPE, reinterpret_cast<const jerry_char_t*>(error_msg)); \ 85 } \ 86 } 87 88#define NAME_FOR_GLOBAL_FUNCTION(NAME) __gen_jsmbed_global_func_ ## NAME 89#define NAME_FOR_CLASS_CONSTRUCTOR(CLASS) __gen_jsmbed_class_constructor_ ## CLASS 90#define NAME_FOR_CLASS_FUNCTION(CLASS, NAME) __gen_jsmbed_func_c_ ## CLASS ## _f_ ## NAME 91 92#define NAME_FOR_CLASS_NATIVE_CONSTRUCTOR(CLASS, TYPELIST) __gen_native_jsmbed_ ## CLASS ## __Special_create_ ## TYPELIST 93#define NAME_FOR_CLASS_NATIVE_DESTRUCTOR(CLASS) __gen_native_jsmbed_ ## CLASS ## __Special_destroy 94#define NAME_FOR_CLASS_NATIVE_FUNCTION(CLASS, NAME) __gen_native_jsmbed_ ## CLASS ## _ ## NAME 95 96#endif // _JERRYSCRIPT_MBED_UTIL_WRAPPERS_H 97