Extension description syntax; extension instantiation, field values and calls with arguments (except strings); example of a simple extension.
String arguments support is supposed to be added in a subsequent commit.
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
/* Copyright 2015 Samsung Electronics Co., Ltd.
|
||||
*
|
||||
* 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 "jerry.h"
|
||||
|
||||
/* Counting fields */
|
||||
enum
|
||||
{
|
||||
#define EXTENSION_FIELD(_field_name, _type, _value) \
|
||||
JERRY_EXTENSION_ ## EXTENSION_NAME ## _ ## _field_name,
|
||||
# include EXTENSION_DESCRIPTION_HEADER
|
||||
#undef EXTENSION_FIELD
|
||||
JERRY_EXTENSION_FIELDS_NUMBER
|
||||
};
|
||||
|
||||
/* Counting functions */
|
||||
enum
|
||||
{
|
||||
#define EXTENSION_FUNCTION(_function_name, _function_wrapper, _args_number, ... /* args */) \
|
||||
JERRY_EXTENSION_ ## EXTENSION_NAME ## _ ## _function_name,
|
||||
# include EXTENSION_DESCRIPTION_HEADER
|
||||
#undef EXTENSION_FUNCTION
|
||||
JERRY_EXTENSION_FUNCTIONS_NUMBER
|
||||
};
|
||||
|
||||
/* Fields description */
|
||||
static const jerry_extension_field_t jerry_extension_fields [JERRY_EXTENSION_FIELDS_NUMBER + 1] =
|
||||
{
|
||||
#define EXTENSION_FIELD(_field_name, _type, _value) \
|
||||
{ # _field_name, JERRY_EXTENSION_FIELD_TYPE_ ## _type, _value },
|
||||
# include EXTENSION_DESCRIPTION_HEADER
|
||||
#undef EXTENSION_FIELD
|
||||
};
|
||||
|
||||
/* Functions wrapper definitions */
|
||||
#define EXTENSION_ARG_PASS_BOOL(_arg_index) \
|
||||
args_p [_arg_index].v_bool
|
||||
#define EXTENSION_ARG_PASS_FLOAT32(_arg_index) \
|
||||
args_p [_arg_index].v_float32
|
||||
#define EXTENSION_ARG_PASS_FLOAT64(_arg_index) \
|
||||
args_p [_arg_index].v_float64
|
||||
#define EXTENSION_ARG_PASS_UINT32(_arg_index) \
|
||||
args_p [_arg_index].v_uint32
|
||||
#define EXTENSION_ARG_PASS_STRING(_arg_index) \
|
||||
(const char*) args_p [_arg_index].v_string.chars_p, \
|
||||
args_p [_arg_index].v_string.length
|
||||
#define EXTENSION_ARG(_arg_index, _type) EXTENSION_ARG_PASS_ ## _type(_arg_index)
|
||||
#define EXTENSION_FUNCTION(_function_name, _function_to_call, _args_number, ...) \
|
||||
static void jerry_extension_ ## _function_name ## _wrapper (const jerry_extension_function_t *function_block_p) \
|
||||
{ \
|
||||
const jerry_extension_function_arg_t *args_p = function_block_p->args_p; \
|
||||
_function_to_call (__VA_ARGS__); \
|
||||
}
|
||||
# include EXTENSION_DESCRIPTION_HEADER
|
||||
#undef EXTENSION_FUNCTION
|
||||
#undef EXTENSION_ARG
|
||||
#undef EXTENSION_ARG_PASS_STRING
|
||||
#undef EXTENSION_ARG_PASS_UINT32
|
||||
#undef EXTENSION_ARG_PASS_FLOAT64
|
||||
#undef EXTENSION_ARG_PASS_FLOAT32
|
||||
#undef EXTENSION_ARG_PASS_BOOL
|
||||
|
||||
/* Functions' arguments description */
|
||||
#define EXTENSION_ARG(_arg_index, _type) [_arg_index] = { \
|
||||
(JERRY_EXTENSION_FIELD_TYPE_ ## _type), \
|
||||
false /* just for initialization, should be overwritten upon call */ \
|
||||
}
|
||||
#define EXTENSION_FUNCTION(_function_name, _function_to_call, _args_number, ...) \
|
||||
static jerry_extension_function_arg_t jerry_extension_function_ ## _function_name ## _args [_args_number] = { \
|
||||
__VA_ARGS__ \
|
||||
};
|
||||
# include EXTENSION_DESCRIPTION_HEADER
|
||||
#undef EXTENSION_FUNCTION
|
||||
#undef EXTENSION_ARG
|
||||
|
||||
/* Functions description */
|
||||
static const jerry_extension_function_t jerry_extension_functions [JERRY_EXTENSION_FUNCTIONS_NUMBER + 1] =
|
||||
{
|
||||
#define EXTENSION_FUNCTION(_function_name, _function_wrapper, _args_number, ...) \
|
||||
{ \
|
||||
# _function_name, jerry_extension_ ## _function_name ## _wrapper, \
|
||||
jerry_extension_function_ ## _function_name ## _args, \
|
||||
_args_number \
|
||||
},
|
||||
# include EXTENSION_DESCRIPTION_HEADER
|
||||
#undef EXTENSION_FUNCTION
|
||||
};
|
||||
|
||||
static jerry_extension_descriptor_t jerry_extension
|
||||
{
|
||||
JERRY_EXTENSION_FIELDS_NUMBER,
|
||||
JERRY_EXTENSION_FUNCTIONS_NUMBER,
|
||||
jerry_extension_fields,
|
||||
jerry_extension_functions,
|
||||
EXTENSION_NAME,
|
||||
NULL,
|
||||
0 /* just for initialization, should be overwritten upon registration */
|
||||
};
|
||||
Reference in New Issue
Block a user