Skip to content

Commit 685d6b9

Browse files
committed
update 0419
* tests/unit/util/... * each util has its own cmakelist * uintptr_t extra_info instead of union info JerryScript-DCO-1.0-Signed-off-by: Zidong Jiang [email protected]
1 parent 2c55f8f commit 685d6b9

File tree

8 files changed

+119
-92
lines changed

8 files changed

+119
-92
lines changed

jerry-util/CMakeLists.txt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,4 @@ cmake_minimum_required (VERSION 2.8.12)
1616
set(JERRY_UTIL_NAME jerry-util)
1717
project (${JERRY_UTIL_NAME} C)
1818

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)
19+
add_subdirectory(arg-validator)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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_VALIDATOR_NAME jerry-util-validator)
17+
project (${JERRY_UTIL_VALIDATOR_NAME} C)
18+
19+
# Include directories
20+
set(INCLUDE_UTIL_VALIDATOR "${CMAKE_CURRENT_SOURCE_DIR}")
21+
22+
# Source directories
23+
file(GLOB SOURCE_UTIL_VALIDATOR *.c)
24+
25+
add_library(${JERRY_UTIL_VALIDATOR_NAME} STATIC ${SOURCE_UTIL_VALIDATOR})
26+
27+
target_include_directories(${JERRY_UTIL_VALIDATOR_NAME} SYSTEM PUBLIC ${INCLUDE_UTIL_VALIDATOR})
28+
target_include_directories(${JERRY_UTIL_VALIDATOR_NAME} SYSTEM PRIVATE "${CMAKE_SOURCE_DIR}/jerry-core")
29+
target_link_libraries(${JERRY_NAME} jerry-core)
30+
31+
install(TARGETS ${JERRY_UTIL_VALIDATOR_NAME} DESTINATION lib)
32+
install(FILES arg-validator.h DESTINATION include/jerry-util)

jerry-util/arg-validator/arg-init.inline.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ jerry_value_t jerry_arg_validator_ignore (jerry_arg_iterator_t *iterator_p);
4444
*/
4545
static inline jerry_arg_t
4646
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 */
47+
jerry_arg_conversion_t conv_flag, /**< whether the conversion is allowed */
48+
jerry_arg_optional_t opt_flag) /**< whether it is optional argument */
4949
{
5050
jerry_arg_func_t func;
5151

52-
if (is_strict)
52+
if (conv_flag == JERRY_ARG_CONVERSION_NO)
5353
{
54-
if (is_optional)
54+
if (opt_flag == JERRY_ARG_OPTIONAL)
5555
{
5656
func = jerry_arg_validator_number_strict_optional;
5757
}
@@ -62,7 +62,7 @@ jerry_arg_number (double *dest, /**< points to the native number */
6262
}
6363
else
6464
{
65-
if (is_optional)
65+
if (opt_flag == JERRY_ARG_OPTIONAL)
6666
{
6767
func = jerry_arg_validator_number_optional;
6868
}
@@ -86,14 +86,14 @@ jerry_arg_number (double *dest, /**< points to the native number */
8686
*/
8787
static inline jerry_arg_t
8888
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 */
89+
jerry_arg_conversion_t conv_flag, /**< whether the conversion is allowed */
90+
jerry_arg_optional_t opt_flag) /**< whether it is optional argument */
9191
{
9292
jerry_arg_func_t func;
9393

94-
if (is_strict)
94+
if (conv_flag == JERRY_ARG_CONVERSION_NO)
9595
{
96-
if (is_optional)
96+
if (opt_flag == JERRY_ARG_OPTIONAL)
9797
{
9898
func = jerry_arg_validator_boolean_strict_optional;
9999
}
@@ -104,7 +104,7 @@ jerry_arg_boolean (bool *dest, /**< points to the native bool */
104104
}
105105
else
106106
{
107-
if (is_optional)
107+
if (opt_flag == JERRY_ARG_OPTIONAL)
108108
{
109109
func = jerry_arg_validator_boolean_optional;
110110
}
@@ -129,14 +129,14 @@ jerry_arg_boolean (bool *dest, /**< points to the native bool */
129129
static inline jerry_arg_t
130130
jerry_arg_string (char *dest, /**< points to the native char array */
131131
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 */
132+
jerry_arg_conversion_t conv_flag, /**< whether the conversion is allowed */
133+
jerry_arg_optional_t opt_flag) /**< whether it is optional argument */
134134
{
135135
jerry_arg_func_t func;
136136

137-
if (is_strict)
137+
if (conv_flag == JERRY_ARG_CONVERSION_NO)
138138
{
139-
if (is_optional)
139+
if (opt_flag == JERRY_ARG_OPTIONAL)
140140
{
141141
func = jerry_arg_validator_string_strict_optional;
142142
}
@@ -147,7 +147,7 @@ jerry_arg_string (char *dest, /**< points to the native char array */
147147
}
148148
else
149149
{
150-
if (is_optional)
150+
if (opt_flag == JERRY_ARG_OPTIONAL)
151151
{
152152
func = jerry_arg_validator_string_optional;
153153
}
@@ -161,7 +161,7 @@ jerry_arg_string (char *dest, /**< points to the native char array */
161161
{
162162
.func = func,
163163
.dest = (void *) dest,
164-
.info.length = size
164+
.extra_info = (uintptr_t) size
165165
};
166166
} /* jerry_arg_string */
167167

@@ -172,11 +172,11 @@ jerry_arg_string (char *dest, /**< points to the native char array */
172172
*/
173173
static inline jerry_arg_t
174174
jerry_arg_function (jerry_value_t *dest, /**< points to the js function value */
175-
bool is_optional) /**< whether it is optional */
175+
jerry_arg_optional_t opt_flag) /**< whether it is optional argument */
176176
{
177177
jerry_arg_func_t func;
178178

179-
if (is_optional)
179+
if (opt_flag == JERRY_ARG_OPTIONAL)
180180
{
181181
func = jerry_arg_validator_function_optional;
182182
}
@@ -200,11 +200,11 @@ jerry_arg_function (jerry_value_t *dest, /**< points to the js function value */
200200
static inline jerry_arg_t
201201
jerry_arg_native_pointer (void **dest, /**< points to the native pointer */
202202
const jerry_object_native_info_t *info_p, /**< expected the type info */
203-
bool is_optional) /**< whether it is optional */
203+
jerry_arg_optional_t opt_flag) /**< whether it is optional argument */
204204
{
205205
jerry_arg_func_t func;
206206

207-
if (is_optional)
207+
if (opt_flag == JERRY_ARG_OPTIONAL)
208208
{
209209
func = jerry_arg_validator_native_pointer_optional;
210210
}
@@ -217,7 +217,7 @@ jerry_arg_native_pointer (void **dest, /**< points to the native pointer */
217217
{
218218
.func = func,
219219
.dest = (void *) dest,
220-
.info.obj_p = (void *) info_p
220+
.extra_info = (uintptr_t) info_p
221221
};
222222
} /* jerry_arg_native_pointer */
223223

@@ -236,20 +236,20 @@ jerry_arg_ignore (void)
236236
} /* jerry_arg_ignore */
237237

238238
/**
239-
* Create a jerry_arg_t instance with custome validator.
239+
* Create a jerry_arg_t instance with custom validator.
240240
*
241241
* @return a jerry_arg_t instance.
242242
*/
243243
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 */
244+
jerry_arg_custom (void *dest, /**< points to the native argument */
245+
uintptr_t extra_info, /**< the extra infomation of the jerry_arg_t */
246+
jerry_arg_func_t func) /**< the custom validator function */
247247
{
248248
return (jerry_arg_t)
249249
{
250250
.func = func,
251251
.dest = dest,
252-
.info.obj_p = extra_info
252+
.extra_info = extra_info
253253
};
254254
} /* jerry_arg_custome */
255255

jerry-util/arg-validator/arg-validator-functions.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ jerry_arg_validator_string_strict (jerry_arg_iterator_t *iterator_p)
208208
}
209209

210210

211-
jerry_length_t dest_len = iterator_p->c_arg_p->info.length;
211+
jerry_length_t dest_len = (jerry_length_t) iterator_p->c_arg_p->extra_info;
212212
char *dest = jerry_arg_iterator_get_dest (iterator_p);
213213

214214
jerry_size_t size = jerry_string_to_char_buffer (js_arg,
@@ -241,7 +241,7 @@ jerry_arg_validator_string_strict_optional (jerry_arg_iterator_t *iterator_p)
241241
}
242242

243243

244-
jerry_length_t dest_len = iterator_p->c_arg_p->info.length;
244+
jerry_length_t dest_len = (jerry_length_t) iterator_p->c_arg_p->extra_info;
245245
char *dest = jerry_arg_iterator_get_dest (iterator_p);
246246

247247
jerry_size_t size = jerry_string_to_char_buffer (js_arg,
@@ -278,7 +278,7 @@ jerry_arg_validator_string_optional (jerry_arg_iterator_t *iterator_p)
278278
}
279279

280280

281-
jerry_length_t dest_len = iterator_p->c_arg_p->info.length;
281+
jerry_length_t dest_len = (jerry_length_t) iterator_p->c_arg_p->extra_info;
282282
char *dest = jerry_arg_iterator_get_dest (iterator_p);
283283

284284
jerry_size_t size = jerry_string_to_char_buffer (to_string,
@@ -314,7 +314,7 @@ jerry_arg_validator_string (jerry_arg_iterator_t *iterator_p)
314314
}
315315

316316

317-
jerry_length_t dest_len = iterator_p->c_arg_p->info.length;
317+
jerry_length_t dest_len = (jerry_length_t) iterator_p->c_arg_p->extra_info;
318318
char *dest = jerry_arg_iterator_get_dest (iterator_p);
319319

320320
jerry_size_t size = jerry_string_to_char_buffer (to_string,
@@ -390,7 +390,7 @@ jerry_arg_validator_native_pointer (jerry_arg_iterator_t *iterator_p)
390390

391391
const jerry_object_native_info_t *expected_info_p;
392392
const jerry_object_native_info_t *out_info_p;
393-
expected_info_p = (const jerry_object_native_info_t *) iterator_p->c_arg_p->info.obj_p;
393+
expected_info_p = (const jerry_object_native_info_t *) iterator_p->c_arg_p->extra_info;
394394
void **ptr_p = (void **) jerry_arg_iterator_get_dest (iterator_p);
395395
bool is_ok = jerry_get_object_native_pointer (js_arg, ptr_p, &out_info_p);
396396

@@ -421,7 +421,7 @@ jerry_arg_validator_native_pointer_optional (jerry_arg_iterator_t *iterator_p)
421421

422422
const jerry_object_native_info_t *expected_info_p;
423423
const jerry_object_native_info_t *out_info_p;
424-
expected_info_p = (const jerry_object_native_info_t *) iterator_p->c_arg_p->info.obj_p;
424+
expected_info_p = (const jerry_object_native_info_t *) iterator_p->c_arg_p->extra_info;
425425
void **ptr_p = (void **) jerry_arg_iterator_get_dest (iterator_p);
426426
bool is_ok = jerry_get_object_native_pointer (js_arg, ptr_p, &out_info_p);
427427

jerry-util/arg-validator/arg-validator.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ jerry_validate_and_assign_args (const jerry_value_t *js_arg_p, /**< points to th
4848

4949
while (c_arg_index < c_arg_cnt)
5050
{
51-
iterator.c_arg_p = &c_arg_p[c_arg_index];
51+
iterator.c_arg_p = c_arg_p;
5252

53-
ret = c_arg_p[c_arg_index].func (&iterator);
53+
ret = iterator.c_arg_p->func (&iterator);
5454

5555
if (jerry_value_has_error_flag (ret))
5656
{
5757
break;
5858
}
5959

6060
c_arg_index ++;
61+
c_arg_p ++;
6162
}
6263

6364
return ret;
@@ -77,18 +78,16 @@ jerry_validate_and_assign_all (const jerry_value_t this_val, /**< the this_val f
7778
const jerry_arg_t *c_arg_p, /**< points to the jerry_arg_t */
7879
const jerry_length_t c_arg_cnt) /**< the count of the native argument */
7980
{
80-
jerry_value_t ret = jerry_create_undefined ();
81-
8281
if (c_arg_cnt == 0)
8382
{
84-
return ret;
83+
return jerry_create_undefined ();
8584
}
8685

87-
ret = jerry_validate_and_assign_args (&this_val, 1, &c_arg_p[0], 1);
86+
jerry_value_t is_ok = jerry_validate_and_assign_args (&this_val, 1, &c_arg_p[0], 1);
8887

89-
if (jerry_value_has_error_flag (ret))
88+
if (jerry_value_has_error_flag (is_ok))
9089
{
91-
jerry_release_value (ret);
90+
jerry_release_value (is_ok);
9291

9392
return jerry_create_error (JERRY_ERROR_TYPE, (jerry_char_t *) "'this' validation failed");
9493
}

jerry-util/arg-validator/arg-validator.h

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,20 @@ typedef jerry_value_t (*jerry_arg_func_t) (jerry_arg_iterator_t *iterator_p);
3636
*/
3737
struct jerry_arg_t
3838
{
39-
jerry_arg_func_t func; /**< the validator function for the native argument */
40-
void *dest; /**< point to the native argument */
41-
/** The extra infomation for validator */
42-
union
43-
{
44-
void *obj_p; /**< the pointer to native handle's expected type info */
45-
jerry_length_t length; /**< the length of the string */
46-
} info;
39+
jerry_arg_func_t func; /**< the validator function for the native argument */
40+
void *dest; /**< point to the native argument */
41+
uintptr_t extra_info; /**< extra infomation for validator */
4742
};
4843

4944
/**
5045
* The argument iteration used for validator funtions.
5146
*/
5247
struct jerry_arg_iterator_t
5348
{
54-
const jerry_arg_t *c_arg_p; /**< points to the current jerry_arg_t */
55-
const jerry_value_t *js_arg_p; /**< points to the current js argument */
56-
const jerry_length_t js_arg_cnt; /**< the total count of the js arguments */
57-
jerry_length_t js_arg_index; /**< the current index of the js argument */
49+
const jerry_arg_t *c_arg_p; /**< points to the current jerry_arg_t */
50+
const jerry_value_t *js_arg_p; /**< points to the current js argument */
51+
const jerry_length_t js_arg_cnt; /**< the total count of the js arguments */
52+
jerry_length_t js_arg_index; /**< the current index of the js argument */
5853
};
5954

6055
jerry_value_t jerry_validate_and_assign_all (const jerry_value_t this_val,
@@ -72,22 +67,32 @@ jerry_value_t jerry_validate_and_assign_args (const jerry_value_t *js_arg_p,
7267
jerry_value_t jerry_arg_iterator_pop (jerry_arg_iterator_t *iterator_p);
7368
void *jerry_arg_iterator_get_dest (jerry_arg_iterator_t *iterator_p);
7469

70+
/**
71+
* Indicates whether a argument can be converted into the target type.
72+
*/
73+
typedef enum
74+
{
75+
JERRY_ARG_CONVERSION_YES, /**< the validator inside will invoke toNumber, toBoolean or toString */
76+
JERRY_ARG_CONVERSION_NO /**< the conversion is not allowed. */
77+
} jerry_arg_conversion_t;
78+
79+
/**
80+
* Indicates whether a argument is optional or required.
81+
*/
82+
typedef enum
83+
{
84+
JERRY_ARG_OPTIONAL, /**< the argument is optional */
85+
JERRY_ARG_REQUIRED /**< the argument is required. */
86+
} jerry_arg_optional_t;
87+
7588
/* Inline functions for initializing jerry_arg_t */
76-
/* The argument's type must be the primitive, e.g. number, string, boolean. */
77-
#define JERRY_ARG_STRICT true
78-
/* The validator inside will invoke toNumber, toBoolean or toString */
79-
#define JERRY_ARG_RELAX false
80-
/* The argument is optional */
81-
#define JERRY_ARG_OPTIOANL true
82-
/* The argument is required */
83-
#define JERRY_ARG_REQUIRED false
84-
static inline jerry_arg_t jerry_arg_number (double *dest, bool is_strict, bool is_optional);
85-
static inline jerry_arg_t jerry_arg_boolean (bool *dest, bool is_strict, bool is_optional);
86-
static inline jerry_arg_t jerry_arg_string (char *dest, uint32_t size, bool is_strict, bool is_optional);
87-
static inline jerry_arg_t jerry_arg_function (jerry_value_t *dest, bool is_optional);
88-
static inline jerry_arg_t jerry_arg_native_pointer (void **dest, const jerry_object_native_info_t *info_p, bool is_optional);
89+
static inline jerry_arg_t jerry_arg_number (double *dest, jerry_arg_conversion_t conv_flag, jerry_arg_optional_t opt_flag);
90+
static inline jerry_arg_t jerry_arg_boolean (bool *dest, jerry_arg_conversion_t conv_flag, jerry_arg_optional_t opt_flag);
91+
static inline jerry_arg_t jerry_arg_string (char *dest, uint32_t size, jerry_arg_conversion_t conv_flag, jerry_arg_optional_t opt_flag);
92+
static inline jerry_arg_t jerry_arg_function (jerry_value_t *dest, jerry_arg_optional_t opt_flag);
93+
static inline jerry_arg_t jerry_arg_native_pointer (void **dest, const jerry_object_native_info_t *info_p, jerry_arg_optional_t opt_flag);
8994
static inline jerry_arg_t jerry_arg_ignore (void);
90-
static inline jerry_arg_t jerry_arg_custome (void *dest, void *extra_info, jerry_arg_func_t func);
95+
static inline jerry_arg_t jerry_arg_custom (void *dest, uintptr_t extra_info, jerry_arg_func_t func);
9196

9297
#include "arg-init.inline.h"
9398

0 commit comments

Comments
 (0)