Skip to content

Commit 2c55f8f

Browse files
committed
A tool in jerry-util: arg validator for binding
It provides some APIs for binding developers, so that they can validate the type of the js argument and convert/assign them to the native argument. Related Issue: #1716 JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]
1 parent 5481fca commit 2c55f8f

File tree

9 files changed

+1132
-0
lines changed

9 files changed

+1132
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,7 @@ endif()
236236

237237
# Unittests
238238
if(UNITTESTS)
239+
# Add jerry's util
240+
add_subdirectory(jerry-util)
239241
add_subdirectory(tests/unit)
240242
endif()

jerry-util/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
cmake_minimum_required (VERSION 2.8.12)
16+
set(JERRY_UTIL_NAME jerry-util)
17+
project (${JERRY_UTIL_NAME} C)
18+
19+
# Include directories
20+
set(INCLUDE_UTIL "${CMAKE_CURRENT_SOURCE_DIR}/arg-validator")
21+
22+
# Source directories
23+
file(GLOB SOURCE_UTIL arg-validator/*.c)
24+
25+
add_library(${JERRY_UTIL_NAME} STATIC ${SOURCE_UTIL})
26+
27+
28+
target_include_directories(${JERRY_UTIL_NAME} SYSTEM PUBLIC ${INCLUDE_UTIL})
29+
target_include_directories(${JERRY_UTIL_NAME} SYSTEM PRIVATE "${CMAKE_SOURCE_DIR}/jerry-core")
30+
target_link_libraries(${JERRY_NAME} jerry-core)
31+
32+
install(TARGETS ${JERRY_UTIL_NAME} DESTINATION lib)
33+
install(DIRECTORY ${INCLUDE_UTIL}/ DESTINATION include/jerry-util)
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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 JERRY_ARG_INIT_INLINE_H
17+
#define JERRY_ARG_INIT_INLINE_H
18+
19+
/* validator functions for each type. */
20+
jerry_value_t jerry_arg_validator_number_strict (jerry_arg_iterator_t *iterator_p);
21+
jerry_value_t jerry_arg_validator_number_strict_optional (jerry_arg_iterator_t *iterator_p);
22+
jerry_value_t jerry_arg_validator_number_optional (jerry_arg_iterator_t *iterator_p);
23+
jerry_value_t jerry_arg_validator_number (jerry_arg_iterator_t *iterator_p);
24+
jerry_value_t jerry_arg_validator_boolean_strict (jerry_arg_iterator_t *iterator_p);
25+
jerry_value_t jerry_arg_validator_boolean_strict_optional (jerry_arg_iterator_t *iterator_p);
26+
jerry_value_t jerry_arg_validator_boolean_optional (jerry_arg_iterator_t *iterator_p);
27+
jerry_value_t jerry_arg_validator_boolean (jerry_arg_iterator_t *iterator_p);
28+
jerry_value_t jerry_arg_validator_string_strict (jerry_arg_iterator_t *iterator_p);
29+
jerry_value_t jerry_arg_validator_string_strict_optional (jerry_arg_iterator_t *iterator_p);
30+
jerry_value_t jerry_arg_validator_string_optional (jerry_arg_iterator_t *iterator_p);
31+
jerry_value_t jerry_arg_validator_string (jerry_arg_iterator_t *iterator_p);
32+
jerry_value_t jerry_arg_validator_function (jerry_arg_iterator_t *iterator_p);
33+
jerry_value_t jerry_arg_validator_function_optional (jerry_arg_iterator_t *iterator_p);
34+
jerry_value_t jerry_arg_validator_constructor (jerry_arg_iterator_t *iterator_p);
35+
jerry_value_t jerry_arg_validator_constructor_optional (jerry_arg_iterator_t *iterator_p);
36+
jerry_value_t jerry_arg_validator_native_pointer (jerry_arg_iterator_t *iterator_p);
37+
jerry_value_t jerry_arg_validator_native_pointer_optional (jerry_arg_iterator_t *iterator_p);
38+
jerry_value_t jerry_arg_validator_ignore (jerry_arg_iterator_t *iterator_p);
39+
40+
/**
41+
* Create a jerry_arg_t instance for number argument.
42+
*
43+
* @return a jerry_arg_t instance.
44+
*/
45+
static inline jerry_arg_t
46+
jerry_arg_number (double *dest, /**< points to the native number */
47+
bool is_strict, /**< whether it is strict type (do not automatically convert inside) */
48+
bool is_optional) /**< whether it is optional */
49+
{
50+
jerry_arg_func_t func;
51+
52+
if (is_strict)
53+
{
54+
if (is_optional)
55+
{
56+
func = jerry_arg_validator_number_strict_optional;
57+
}
58+
else
59+
{
60+
func = jerry_arg_validator_number_strict;
61+
}
62+
}
63+
else
64+
{
65+
if (is_optional)
66+
{
67+
func = jerry_arg_validator_number_optional;
68+
}
69+
else
70+
{
71+
func = jerry_arg_validator_number;
72+
}
73+
}
74+
75+
return (jerry_arg_t)
76+
{
77+
.func = func,
78+
.dest = (void *) dest
79+
};
80+
} /* jerry_arg_number */
81+
82+
/**
83+
* Create a jerry_arg_t instance for boolean argument.
84+
*
85+
* @return a jerry_arg_t instance.
86+
*/
87+
static inline jerry_arg_t
88+
jerry_arg_boolean (bool *dest, /**< points to the native bool */
89+
bool is_strict, /**< whether it is strict type (do not automatically convert inside) */
90+
bool is_optional) /**< whether it is optional */
91+
{
92+
jerry_arg_func_t func;
93+
94+
if (is_strict)
95+
{
96+
if (is_optional)
97+
{
98+
func = jerry_arg_validator_boolean_strict_optional;
99+
}
100+
else
101+
{
102+
func = jerry_arg_validator_boolean_strict;
103+
}
104+
}
105+
else
106+
{
107+
if (is_optional)
108+
{
109+
func = jerry_arg_validator_boolean_optional;
110+
}
111+
else
112+
{
113+
func = jerry_arg_validator_boolean;
114+
}
115+
}
116+
117+
return (jerry_arg_t)
118+
{
119+
.func = func,
120+
.dest = (void *) dest
121+
};
122+
} /* jerry_arg_boolean */
123+
124+
/**
125+
* Create a jerry_arg_t instance for string argument.
126+
*
127+
* @return a jerry_arg_t instance.
128+
*/
129+
static inline jerry_arg_t
130+
jerry_arg_string (char *dest, /**< points to the native char array */
131+
uint32_t size, /**< the size of native char array */
132+
bool is_strict, /**< whether it is strict type (do not automatically convert inside) */
133+
bool is_optional) /**< whether it is optional */
134+
{
135+
jerry_arg_func_t func;
136+
137+
if (is_strict)
138+
{
139+
if (is_optional)
140+
{
141+
func = jerry_arg_validator_string_strict_optional;
142+
}
143+
else
144+
{
145+
func = jerry_arg_validator_string_strict;
146+
}
147+
}
148+
else
149+
{
150+
if (is_optional)
151+
{
152+
func = jerry_arg_validator_string_optional;
153+
}
154+
else
155+
{
156+
func = jerry_arg_validator_string;
157+
}
158+
}
159+
160+
return (jerry_arg_t)
161+
{
162+
.func = func,
163+
.dest = (void *) dest,
164+
.info.length = size
165+
};
166+
} /* jerry_arg_string */
167+
168+
/**
169+
* Create a jerry_arg_t instance for function argument.
170+
*
171+
* @return a jerry_arg_t instance.
172+
*/
173+
static inline jerry_arg_t
174+
jerry_arg_function (jerry_value_t *dest, /**< points to the js function value */
175+
bool is_optional) /**< whether it is optional */
176+
{
177+
jerry_arg_func_t func;
178+
179+
if (is_optional)
180+
{
181+
func = jerry_arg_validator_function_optional;
182+
}
183+
else
184+
{
185+
func = jerry_arg_validator_function;
186+
}
187+
188+
return (jerry_arg_t)
189+
{
190+
.func = func,
191+
.dest = (void *) dest
192+
};
193+
} /* jerry_arg_function */
194+
195+
/**
196+
* Create a jerry_arg_t instance for native pointer argument.
197+
*
198+
* @return a jerry_arg_t instance.
199+
*/
200+
static inline jerry_arg_t
201+
jerry_arg_native_pointer (void **dest, /**< points to the native pointer */
202+
const jerry_object_native_info_t *info_p, /**< expected the type info */
203+
bool is_optional) /**< whether it is optional */
204+
{
205+
jerry_arg_func_t func;
206+
207+
if (is_optional)
208+
{
209+
func = jerry_arg_validator_native_pointer_optional;
210+
}
211+
else
212+
{
213+
func = jerry_arg_validator_native_pointer;
214+
}
215+
216+
return (jerry_arg_t)
217+
{
218+
.func = func,
219+
.dest = (void *) dest,
220+
.info.obj_p = (void *) info_p
221+
};
222+
} /* jerry_arg_native_pointer */
223+
224+
/**
225+
* Create a jerry_arg_t instance for ignored argument.
226+
*
227+
* @return a jerry_arg_t instance.
228+
*/
229+
static inline jerry_arg_t
230+
jerry_arg_ignore (void)
231+
{
232+
return (jerry_arg_t)
233+
{
234+
.func = jerry_arg_validator_ignore
235+
};
236+
} /* jerry_arg_ignore */
237+
238+
/**
239+
* Create a jerry_arg_t instance with custome validator.
240+
*
241+
* @return a jerry_arg_t instance.
242+
*/
243+
static inline jerry_arg_t
244+
jerry_arg_custome (void *dest, /**< points to the native argument */
245+
void *extra_info, /**< the extra infomation of the jerry_arg_t */
246+
jerry_arg_func_t func) /**< the custome validator function */
247+
{
248+
return (jerry_arg_t)
249+
{
250+
.func = func,
251+
.dest = dest,
252+
.info.obj_p = extra_info
253+
};
254+
} /* jerry_arg_custome */
255+
256+
#endif /* !JERRY_ARG_INIT_INLINE_H */
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
#include "arg-validator.h"
17+
#include "jerryscript.h"
18+
19+
/**
20+
* Pop the current JS argument.
21+
*
22+
* @return the current JS argument.
23+
*/
24+
jerry_value_t
25+
jerry_arg_iterator_pop (jerry_arg_iterator_t *iterator_p) /**< points to the argument iterator */
26+
{
27+
if (iterator_p->js_arg_index >= iterator_p->js_arg_cnt)
28+
{
29+
return jerry_create_undefined ();
30+
}
31+
32+
jerry_value_t ret = *iterator_p->js_arg_p;
33+
iterator_p->js_arg_index ++;
34+
iterator_p->js_arg_p ++;
35+
36+
return ret;
37+
} /* jerry_arg_iterator_pop */
38+
39+
/**
40+
* Get the pointer to the current native argument.
41+
*
42+
* @return a pointer
43+
*/
44+
inline void *
45+
jerry_arg_iterator_get_dest (jerry_arg_iterator_t *iterator_p) /**< points to the argument iterator */
46+
{
47+
return iterator_p->c_arg_p->dest;
48+
} /* jerry_arg_iterator_get_dest */

0 commit comments

Comments
 (0)