Skip to content

Commit e426b86

Browse files
committed
Update jerry API
* Fix error handling (related issue: #1141) * Move output paramters to the end of the arguments lists * Inline small functions JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent a816ab8 commit e426b86

File tree

4 files changed

+133
-126
lines changed

4 files changed

+133
-126
lines changed

jerry-core/jerry-api.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ typedef uint32_t jerry_value_t;
9696
*/
9797
typedef bool (*jerry_external_handler_t) (const jerry_object_t *function_obj_p,
9898
const jerry_value_t this_val,
99-
jerry_value_t *ret_val_p,
10099
const jerry_value_t args_p[],
101-
const jerry_length_t args_count);
100+
const jerry_length_t args_count,
101+
jerry_value_t *ret_val_p);
102102

103103
/**
104104
* Native free callback of an object
@@ -197,15 +197,15 @@ bool jerry_is_constructor (const jerry_object_t *);
197197
bool jerry_is_function (const jerry_object_t *);
198198
bool jerry_add_object_field (jerry_object_t *, const jerry_char_t *, jerry_size_t, const jerry_value_t, bool);
199199
bool jerry_delete_object_field (jerry_object_t *, const jerry_char_t *, jerry_size_t);
200-
bool jerry_get_object_field_value (jerry_object_t *, const jerry_char_t *, jerry_value_t *);
201-
bool jerry_get_object_field_value_sz (jerry_object_t *, const jerry_char_t *, jerry_size_t, jerry_value_t *);
200+
jerry_value_t jerry_get_object_field_value (jerry_object_t *, const jerry_char_t *);
201+
jerry_value_t jerry_get_object_field_value_sz (jerry_object_t *, const jerry_char_t *, jerry_size_t);
202202
bool jerry_set_object_field_value (jerry_object_t *, const jerry_char_t *, const jerry_value_t);
203203
bool jerry_set_object_field_value_sz (jerry_object_t *, const jerry_char_t *, jerry_size_t, const jerry_value_t);
204204
bool jerry_foreach_object_field (jerry_object_t *, jerry_object_field_foreach_t, void *);
205205
bool jerry_get_object_native_handle (jerry_object_t *, uintptr_t *);
206206
void jerry_set_object_native_handle (jerry_object_t *, uintptr_t, jerry_object_free_callback_t);
207-
bool jerry_construct_object (jerry_object_t *, jerry_value_t *, const jerry_value_t[], uint16_t);
208-
bool jerry_call_function (jerry_object_t *, jerry_object_t *, jerry_value_t *, const jerry_value_t[], uint16_t);
207+
bool jerry_construct_object (jerry_object_t *, const jerry_value_t[], uint16_t, jerry_value_t *);
208+
bool jerry_call_function (jerry_object_t *, jerry_object_t *, const jerry_value_t[], uint16_t, jerry_value_t *);
209209

210210
/**
211211
* @}

jerry-core/jerry.c

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,19 @@ jerry_make_api_unavailable (void)
124124
jerry_api_available = false;
125125
} /* jerry_make_api_unavailable */
126126

127+
/**
128+
* Returns whether the given jerry_value_t is error.
129+
*/
130+
inline bool __attr_always_inline___
131+
jerry_value_is_error (const jerry_value_t value) /**< api value */
132+
{
133+
return ECMA_IS_VALUE_ERROR (value);
134+
} /* jerry_value_is_error */
135+
127136
/**
128137
* Returns whether the given jerry_value_t is null.
129138
*/
130-
bool
139+
inline bool __attr_always_inline___
131140
jerry_value_is_null (const jerry_value_t value) /**< api value */
132141
{
133142
return ecma_is_value_null (value);
@@ -136,7 +145,7 @@ jerry_value_is_null (const jerry_value_t value) /**< api value */
136145
/**
137146
* Returns whether the given jerry_value_t is undefined.
138147
*/
139-
bool
148+
inline bool __attr_always_inline___
140149
jerry_value_is_undefined (const jerry_value_t value) /**< api value */
141150
{
142151
return ecma_is_value_undefined (value);
@@ -145,7 +154,7 @@ jerry_value_is_undefined (const jerry_value_t value) /**< api value */
145154
/**
146155
* Returns whether the given jerry_value_t has boolean type.
147156
*/
148-
bool
157+
inline bool __attr_always_inline___
149158
jerry_value_is_boolean (const jerry_value_t value) /**< api value */
150159
{
151160
return ecma_is_value_boolean (value);
@@ -157,7 +166,7 @@ jerry_value_is_boolean (const jerry_value_t value) /**< api value */
157166
* More specifically, returns true if the type is JERRY_DATA_TYPE_FLOAT32,
158167
* JERRY_DATA_TYPE_FLOAT64 or JERRY_DATA_TYPE_UINT32, false otherwise.
159168
*/
160-
bool
169+
inline bool __attr_always_inline___
161170
jerry_value_is_number (const jerry_value_t value) /**< api value */
162171
{
163172
return ecma_is_value_number (value);
@@ -166,7 +175,7 @@ jerry_value_is_number (const jerry_value_t value) /**< api value */
166175
/**
167176
* Returns whether the given jerry_value_t is string.
168177
*/
169-
bool
178+
inline bool __attr_always_inline___
170179
jerry_value_is_string (const jerry_value_t value) /**< api value */
171180
{
172181
return ecma_is_value_string (value);
@@ -175,7 +184,7 @@ jerry_value_is_string (const jerry_value_t value) /**< api value */
175184
/**
176185
* Returns whether the given jerry_value_t is object.
177186
*/
178-
bool
187+
inline bool __attr_always_inline___
179188
jerry_value_is_object (const jerry_value_t value) /**< api value */
180189
{
181190
return ecma_is_value_object (value);
@@ -184,7 +193,7 @@ jerry_value_is_object (const jerry_value_t value) /**< api value */
184193
/**
185194
* Returns whether the given jerry_value_t is a function object.
186195
*/
187-
bool
196+
inline bool __attr_always_inline___
188197
jerry_value_is_function (const jerry_value_t value) /**< api value */
189198
{
190199
return ecma_op_is_callable (value);
@@ -310,10 +319,10 @@ jerry_create_string_value (jerry_string_t *str_p) /**< jerry_string_t from which
310319
*
311320
* @return completion code
312321
*/
313-
inline static jerry_completion_code_t __attr_always_inline___
314-
jerry_convert_eval_completion_to_retval (jerry_value_t *retval_p, /**< [out] api value */
315-
ecma_value_t completion) /**< completion of 'eval'-mode
322+
static inline jerry_completion_code_t __attr_always_inline___
323+
jerry_convert_eval_completion_to_retval (ecma_value_t completion, /**< completion of 'eval'-mode
316324
* code execution */
325+
jerry_value_t *retval_p) /**< [out] api value */
317326
{
318327
*retval_p = completion;
319328

@@ -715,9 +724,9 @@ jerry_dispatch_external_function (ecma_object_t *function_object_p, /**< externa
715724

716725
bool is_successful = ((jerry_external_handler_t) handler_p) (function_object_p,
717726
this_arg_value,
718-
&ret_value,
719727
arguments_list_p,
720-
arguments_list_len);
728+
arguments_list_len,
729+
&ret_value);
721730

722731
if (!is_successful)
723732
{
@@ -877,14 +886,13 @@ jerry_delete_object_field (jerry_object_t *object_p, /**< object to delete field
877886
* - there is field with specified name in the object;
878887
* false - otherwise.
879888
*/
880-
bool jerry_get_object_field_value (jerry_object_t *object_p, /**< object */
881-
const jerry_char_t *field_name_p, /**< field name */
882-
jerry_value_t *field_value_p) /**< [out] field value */
889+
jerry_value_t
890+
jerry_get_object_field_value (jerry_object_t *object_p, /**< object */
891+
const jerry_char_t *field_name_p) /**< field name */
883892
{
884893
return jerry_get_object_field_value_sz (object_p,
885894
field_name_p,
886-
lit_zt_utf8_string_size (field_name_p),
887-
field_value_p);
895+
lit_zt_utf8_string_size (field_name_p));
888896
} /* jerry_get_object_field_value */
889897

890898
/**
@@ -946,35 +954,29 @@ jerry_foreach_object_field (jerry_object_t *object_p, /**< object */
946954
* if value was retrieved successfully, it should be freed
947955
* with jerry_release_value just when it becomes unnecessary.
948956
*
949-
* @return true, if field value was retrieved successfully, i.e. upon the call:
950-
* - there is field with specified name in the object;
951-
* false - otherwise.
957+
* @return ecma value
952958
*/
953-
bool
959+
jerry_value_t
954960
jerry_get_object_field_value_sz (jerry_object_t *object_p, /**< object */
955961
const jerry_char_t *field_name_p, /**< name of the field */
956-
jerry_size_t field_name_size, /**< size of field name in bytes */
957-
jerry_value_t *field_value_p) /**< [out] field value, if retrieved successfully */
962+
jerry_size_t field_name_size) /**< size of field name in bytes */
958963
{
959964
jerry_assert_api_available ();
960965

961966
ecma_string_t *field_name_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) field_name_p,
962967
(lit_utf8_size_t) field_name_size);
963968

964-
*field_value_p = ecma_op_object_get (object_p, field_name_str_p);
969+
ecma_value_t field_value = ecma_op_object_get (object_p, field_name_str_p);
965970

966971
ecma_deref_ecma_string (field_name_str_p);
967972

968-
return (!ECMA_IS_VALUE_ERROR (*field_value_p)
969-
&& !ecma_is_value_undefined (*field_value_p));
973+
return field_value;
970974
} /* jerry_get_object_field_value_sz */
971975

972976
/**
973977
* Set value of field in the specified object
974978
*
975-
* @return true, if field value was set successfully, i.e. upon the call:
976-
* - field value is writable;
977-
* false - otherwise.
979+
* @return ecma value
978980
*/
979981
bool
980982
jerry_set_object_field_value (jerry_object_t *object_p, /**< object */
@@ -1112,12 +1114,12 @@ jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke functio
11121114
* if function is invoked as constructor;
11131115
* in case of simple function call set 'this'
11141116
* binding to the global object) */
1115-
jerry_value_t *retval_p, /**< pointer to place for function's
1116-
* return value / thrown exception value
1117-
* or NULL (to ignore the values) */
11181117
const jerry_value_t args_p[], /**< function's call arguments
11191118
* (NULL if arguments number is zero) */
1120-
jerry_length_t args_count) /**< number of the arguments */
1119+
jerry_length_t args_count, /**< number of the arguments */
1120+
jerry_value_t *retval_p) /**< pointer to place for function's
1121+
* return value / thrown exception value
1122+
* or NULL (to ignore the values) */
11211123
{
11221124
JERRY_ASSERT (args_count == 0 || args_p != NULL);
11231125
JERRY_STATIC_ASSERT (sizeof (args_count) == sizeof (ecma_length_t),
@@ -1173,13 +1175,14 @@ jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke functio
11731175

11741176
/**
11751177
* Construct new TypeError object
1178+
*
1179+
* @return TypeError object value
11761180
*/
1177-
static void
1178-
jerry_construct_type_error (jerry_value_t *retval_p) /**< [out] value with constructed
1179-
* TypeError object */
1181+
static inline jerry_value_t __attr_always_inline___
1182+
jerry_construct_type_error (void)
11801183
{
11811184
ecma_object_t *type_error_obj_p = ecma_new_standard_error (ECMA_ERROR_TYPE);
1182-
*retval_p = ecma_make_object_value (type_error_obj_p);
1185+
return ecma_make_object_value (type_error_obj_p);
11831186
} /* jerry_construct_type_error */
11841187

11851188
/**
@@ -1200,24 +1203,24 @@ bool
12001203
jerry_call_function (jerry_object_t *function_object_p, /**< function object to call */
12011204
jerry_object_t *this_arg_p, /**< object for 'this' binding
12021205
* or NULL (set 'this' binding to the global object) */
1203-
jerry_value_t *retval_p, /**< pointer to place for function's
1204-
* return value / thrown exception value
1205-
* or NULL (to ignore the values) */
12061206
const jerry_value_t args_p[], /**< function's call arguments
12071207
* (NULL if arguments number is zero) */
1208-
uint16_t args_count) /**< number of the arguments */
1208+
uint16_t args_count, /**< number of the arguments */
1209+
jerry_value_t *retval_p) /**< [out] pointer to place for function's
1210+
* return value / thrown exception value
1211+
* or NULL (to ignore the values) */
12091212
{
12101213
jerry_assert_api_available ();
12111214

12121215
if (jerry_is_function (function_object_p))
12131216
{
1214-
return jerry_invoke_function (false, function_object_p, this_arg_p, retval_p, args_p, args_count);
1217+
return jerry_invoke_function (false, function_object_p, this_arg_p, args_p, args_count, retval_p);
12151218
}
12161219
else
12171220
{
12181221
if (retval_p != NULL)
12191222
{
1220-
jerry_construct_type_error (retval_p);
1223+
*retval_p = jerry_construct_type_error ();
12211224
}
12221225

12231226
return false;
@@ -1240,24 +1243,24 @@ jerry_call_function (jerry_object_t *function_object_p, /**< function object to
12401243
*/
12411244
bool
12421245
jerry_construct_object (jerry_object_t *function_object_p, /**< function object to call */
1243-
jerry_value_t *retval_p, /**< pointer to place for function's
1244-
* return value / thrown exception value
1245-
* or NULL (to ignore the values) */
12461246
const jerry_value_t args_p[], /**< function's call arguments
12471247
* (NULL if arguments number is zero) */
1248-
uint16_t args_count) /**< number of the arguments */
1248+
uint16_t args_count, /**< number of the arguments */
1249+
jerry_value_t *retval_p) /**< [out] pointer to place for function's
1250+
* return value / thrown exception value
1251+
* or NULL (to ignore the values) */
12491252
{
12501253
jerry_assert_api_available ();
12511254

12521255
if (jerry_is_constructor (function_object_p))
12531256
{
1254-
return jerry_invoke_function (true, function_object_p, NULL, retval_p, args_p, args_count);
1257+
return jerry_invoke_function (true, function_object_p, NULL, args_p, args_count, retval_p);
12551258
}
12561259
else
12571260
{
12581261
if (retval_p != NULL)
12591262
{
1260-
jerry_construct_type_error (retval_p);
1263+
*retval_p = jerry_construct_type_error ();
12611264
}
12621265

12631266
return false;
@@ -1306,7 +1309,7 @@ jerry_eval (const jerry_char_t *source_p, /**< source code */
13061309
is_direct,
13071310
is_strict);
13081311

1309-
status = jerry_convert_eval_completion_to_retval (retval_p, completion);
1312+
status = jerry_convert_eval_completion_to_retval (completion, retval_p);
13101313

13111314
return status;
13121315
} /* jerry_eval */
@@ -2086,7 +2089,7 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
20862089
/* vm should be already initialized */
20872090
ecma_value_t completion = vm_run_eval (bytecode_p, false);
20882091

2089-
ret_code = jerry_convert_eval_completion_to_retval (retval_p, completion);
2092+
ret_code = jerry_convert_eval_completion_to_retval (completion, retval_p);
20902093

20912094
ecma_free_value (completion);
20922095
}

main-unix.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ read_file (const char *file_name,
7474
static bool
7575
assert_handler (const jerry_object_t *function_obj_p __attribute__((unused)), /**< function object */
7676
const jerry_value_t this_p __attribute__((unused)), /**< this arg */
77-
jerry_value_t *ret_val_p __attribute__((unused)), /**< return argument */
7877
const jerry_value_t args_p[], /**< function arguments */
79-
const jerry_length_t args_cnt) /**< number of function arguments */
78+
const jerry_length_t args_cnt, /**< number of function arguments */
79+
jerry_value_t *ret_val_p __attribute__((unused))) /**< return argument */
8080
{
8181
if (args_cnt == 1
8282
&& jerry_value_is_boolean (args_p[0])
@@ -417,9 +417,10 @@ main (int argc,
417417
bool is_done = false;
418418

419419
jerry_object_t *global_obj_p = jerry_get_global ();
420-
jerry_value_t print_function;
420+
jerry_value_t print_function = jerry_get_object_field_value (global_obj_p,
421+
(jerry_char_t *) "print");
421422

422-
if (!jerry_get_object_field_value (global_obj_p, (jerry_char_t *) "print", &print_function))
423+
if (jerry_value_is_error (print_function))
423424
{
424425
return JERRY_STANDALONE_EXIT_CODE_FAIL;
425426
}
@@ -464,9 +465,9 @@ main (int argc,
464465
jerry_value_t ret_val_print;
465466
if (jerry_call_function (jerry_get_object_value (print_function),
466467
NULL,
467-
&ret_val_print,
468468
args,
469-
1))
469+
1,
470+
&ret_val_print))
470471
{
471472
jerry_release_value (ret_val_print);
472473
}

0 commit comments

Comments
 (0)