Skip to content

Commit 2e9016b

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

File tree

4 files changed

+142
-198
lines changed

4 files changed

+142
-198
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+
jerry_value_t jerry_construct_object (jerry_object_t *, const jerry_value_t[], uint16_t);
208+
jerry_value_t jerry_call_function (jerry_object_t *, jerry_object_t *, const jerry_value_t[], uint16_t);
209209

210210
/**
211211
* @}

jerry-core/jerry.c

Lines changed: 42 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ 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+
bool
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
*/
@@ -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 */
@@ -1098,11 +1100,9 @@ jerry_set_object_native_handle (jerry_object_t *object_p, /**< object to set han
10981100
* If function is invoked as constructor, it should support [[Construct]] method,
10991101
* otherwise, if function is simply called - it should support [[Call]] method.
11001102
*
1101-
* @return true, if invocation was performed successfully, i.e.:
1102-
* - no unhandled exceptions were thrown in connection with the call;
1103-
* false - otherwise.
1103+
* @return TODO
11041104
*/
1105-
static bool
1105+
static jerry_value_t
11061106
jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke function as constructor
11071107
* (this_arg_p should be NULL, as it is ignored),
11081108
* false - perform function call */
@@ -1112,9 +1112,6 @@ jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke functio
11121112
* if function is invoked as constructor;
11131113
* in case of simple function call set 'this'
11141114
* 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) */
11181115
const jerry_value_t args_p[], /**< function's call arguments
11191116
* (NULL if arguments number is zero) */
11201117
jerry_length_t args_count) /**< number of the arguments */
@@ -1123,8 +1120,6 @@ jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke functio
11231120
JERRY_STATIC_ASSERT (sizeof (args_count) == sizeof (ecma_length_t),
11241121
size_of_args_count_must_be_equal_to_size_of_ecma_length_t);
11251122

1126-
bool is_successful = true;
1127-
11281123
ecma_value_t call_completion;
11291124

11301125
if (is_invoke_as_constructor)
@@ -1157,29 +1152,19 @@ jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke functio
11571152
args_count);
11581153
}
11591154

1160-
if (ECMA_IS_VALUE_ERROR (call_completion))
1161-
{
1162-
/* unhandled exception during the function call */
1163-
is_successful = false;
1164-
}
1165-
1166-
if (retval_p != NULL)
1167-
{
1168-
*retval_p = call_completion;
1169-
}
1170-
1171-
return is_successful;
1155+
return call_completion;
11721156
} /* jerry_invoke_function */
11731157

11741158
/**
11751159
* Construct new TypeError object
1160+
*
1161+
* @return TypeError object value
11761162
*/
1177-
static void
1178-
jerry_construct_type_error (jerry_value_t *retval_p) /**< [out] value with constructed
1179-
* TypeError object */
1163+
static inline jerry_value_t __attr_always_inline___
1164+
jerry_construct_type_error (void)
11801165
{
11811166
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);
1167+
return ecma_make_error_obj_value (type_error_obj_p);
11831168
} /* jerry_construct_type_error */
11841169

11851170
/**
@@ -1189,20 +1174,12 @@ jerry_construct_type_error (jerry_value_t *retval_p) /**< [out] value with const
11891174
* returned value should be freed with jerry_release_value
11901175
* just when the value becomes unnecessary.
11911176
*
1192-
* @return true, if call was performed successfully, i.e.:
1193-
* - specified object is a function object (see also jerry_is_function);
1194-
* - no unhandled exceptions were thrown in connection with the call;
1195-
* false - otherwise, 'retval_p' contains thrown exception:
1196-
* if called object is not function object - a TypeError instance;
1197-
* else - exception, thrown during the function call.
1177+
* @return TODO
11981178
*/
1199-
bool
1179+
jerry_value_t
12001180
jerry_call_function (jerry_object_t *function_object_p, /**< function object to call */
12011181
jerry_object_t *this_arg_p, /**< object for 'this' binding
12021182
* 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) */
12061183
const jerry_value_t args_p[], /**< function's call arguments
12071184
* (NULL if arguments number is zero) */
12081185
uint16_t args_count) /**< number of the arguments */
@@ -1211,17 +1188,10 @@ jerry_call_function (jerry_object_t *function_object_p, /**< function object to
12111188

12121189
if (jerry_is_function (function_object_p))
12131190
{
1214-
return jerry_invoke_function (false, function_object_p, this_arg_p, retval_p, args_p, args_count);
1191+
return jerry_invoke_function (false, function_object_p, this_arg_p, args_p, args_count);
12151192
}
1216-
else
1217-
{
1218-
if (retval_p != NULL)
1219-
{
1220-
jerry_construct_type_error (retval_p);
1221-
}
12221193

1223-
return false;
1224-
}
1194+
return jerry_construct_type_error ();
12251195
} /* jerry_call_function */
12261196

12271197
/**
@@ -1231,18 +1201,10 @@ jerry_call_function (jerry_object_t *function_object_p, /**< function object to
12311201
* returned value should be freed with jerry_release_value
12321202
* just when the value becomes unnecessary.
12331203
*
1234-
* @return true, if construction was performed successfully, i.e.:
1235-
* - specified object is a constructor function object (see also jerry_is_constructor);
1236-
* - no unhandled exceptions were thrown in connection with the invocation;
1237-
* false - otherwise, 'retval_p' contains thrown exception:
1238-
* if specified object is not a constructor function object - a TypeError instance;
1239-
* else - exception, thrown during the invocation.
1204+
* @return TODO
12401205
*/
1241-
bool
1206+
jerry_value_t
12421207
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) */
12461208
const jerry_value_t args_p[], /**< function's call arguments
12471209
* (NULL if arguments number is zero) */
12481210
uint16_t args_count) /**< number of the arguments */
@@ -1251,17 +1213,10 @@ jerry_construct_object (jerry_object_t *function_object_p, /**< function object
12511213

12521214
if (jerry_is_constructor (function_object_p))
12531215
{
1254-
return jerry_invoke_function (true, function_object_p, NULL, retval_p, args_p, args_count);
1216+
return jerry_invoke_function (true, function_object_p, NULL, args_p, args_count);
12551217
}
1256-
else
1257-
{
1258-
if (retval_p != NULL)
1259-
{
1260-
jerry_construct_type_error (retval_p);
1261-
}
12621218

1263-
return false;
1264-
}
1219+
return jerry_construct_type_error ();
12651220
} /* jerry_construct_object */
12661221

12671222
/**
@@ -1306,7 +1261,7 @@ jerry_eval (const jerry_char_t *source_p, /**< source code */
13061261
is_direct,
13071262
is_strict);
13081263

1309-
status = jerry_convert_eval_completion_to_retval (retval_p, completion);
1264+
status = jerry_convert_eval_completion_to_retval (completion, retval_p);
13101265

13111266
return status;
13121267
} /* jerry_eval */
@@ -2086,7 +2041,7 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
20862041
/* vm should be already initialized */
20872042
ecma_value_t completion = vm_run_eval (bytecode_p, false);
20882043

2089-
ret_code = jerry_convert_eval_completion_to_retval (retval_p, completion);
2044+
ret_code = jerry_convert_eval_completion_to_retval (completion, retval_p);
20902045

20912046
ecma_free_value (completion);
20922047
}

main-unix.c

Lines changed: 10 additions & 10 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
}
@@ -461,12 +462,11 @@ main (int argc,
461462

462463
/* Print return value */
463464
const jerry_value_t args[] = { ret_val };
464-
jerry_value_t ret_val_print;
465-
if (jerry_call_function (jerry_get_object_value (print_function),
466-
NULL,
467-
&ret_val_print,
468-
args,
469-
1))
465+
jerry_value_t ret_val_print = jerry_call_function (jerry_get_object_value (print_function),
466+
NULL,
467+
args,
468+
1);
469+
if (!jerry_value_is_error (ret_val_print))
470470
{
471471
jerry_release_value (ret_val_print);
472472
}

0 commit comments

Comments
 (0)