Skip to content

Commit 8e85042

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ó llango.u-szeged@partner.samsung.com
1 parent 8453a9a commit 8e85042

File tree

4 files changed

+146
-205
lines changed

4 files changed

+146
-205
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: 46 additions & 91 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

@@ -698,8 +707,8 @@ jerry_create_external_function (jerry_external_handler_t handler_p) /**< pointer
698707
* Dispatch call to specified external function using the native handler
699708
*
700709
* Note:
701-
* if called native handler returns true, then dispatcher just returns value received
702-
* through 'return value' output argument, otherwise - throws the value as an exception.
710+
* if called native handler returns true, then dispatcher just returns value received
711+
* through 'return value' output argument, otherwise - throws the value as an exception.
703712
*
704713
* @return ecma value
705714
* Returned value must be freed with ecma_free_value
@@ -717,9 +726,9 @@ jerry_dispatch_external_function (ecma_object_t *function_object_p, /**< externa
717726

718727
bool is_successful = ((jerry_external_handler_t) handler_p) (function_object_p,
719728
this_arg_value,
720-
&ret_value,
721729
arguments_list_p,
722-
arguments_list_len);
730+
arguments_list_len,
731+
&ret_value);
723732

724733
if (!is_successful)
725734
{
@@ -879,14 +888,13 @@ jerry_delete_object_field (jerry_object_t *object_p, /**< object to delete field
879888
* - there is field with specified name in the object;
880889
* false - otherwise.
881890
*/
882-
bool jerry_get_object_field_value (jerry_object_t *object_p, /**< object */
883-
const jerry_char_t *field_name_p, /**< field name */
884-
jerry_value_t *field_value_p) /**< [out] field value */
891+
jerry_value_t
892+
jerry_get_object_field_value (jerry_object_t *object_p, /**< object */
893+
const jerry_char_t *field_name_p) /**< field name */
885894
{
886895
return jerry_get_object_field_value_sz (object_p,
887896
field_name_p,
888-
lit_zt_utf8_string_size (field_name_p),
889-
field_value_p);
897+
lit_zt_utf8_string_size (field_name_p));
890898
} /* jerry_get_object_field_value */
891899

892900
/**
@@ -945,38 +953,32 @@ jerry_foreach_object_field (jerry_object_t *object_p, /**< object */
945953
* Get value of field in the specified object
946954
*
947955
* Note:
948-
* if value was retrieved successfully, it should be freed
949-
* with jerry_release_value just when it becomes unnecessary.
956+
* returned value should be freed with jerry_release_value.
950957
*
951-
* @return true, if field value was retrieved successfully, i.e. upon the call:
952-
* - there is field with specified name in the object;
953-
* false - otherwise.
958+
* @return jerry value of the given field
954959
*/
955-
bool
960+
jerry_value_t
956961
jerry_get_object_field_value_sz (jerry_object_t *object_p, /**< object */
957962
const jerry_char_t *field_name_p, /**< name of the field */
958-
jerry_size_t field_name_size, /**< size of field name in bytes */
959-
jerry_value_t *field_value_p) /**< [out] field value, if retrieved successfully */
963+
jerry_size_t field_name_size) /**< size of field name in bytes */
960964
{
961965
jerry_assert_api_available ();
962966

963967
ecma_string_t *field_name_str_p = ecma_new_ecma_string_from_utf8 ((lit_utf8_byte_t *) field_name_p,
964968
(lit_utf8_size_t) field_name_size);
965969

966-
*field_value_p = ecma_op_object_get (object_p, field_name_str_p);
970+
ecma_value_t field_value = ecma_op_object_get (object_p, field_name_str_p);
967971

968972
ecma_deref_ecma_string (field_name_str_p);
969973

970-
return (!ECMA_IS_VALUE_ERROR (*field_value_p)
971-
&& !ecma_is_value_undefined (*field_value_p));
974+
return field_value;
972975
} /* jerry_get_object_field_value_sz */
973976

974977
/**
975978
* Set value of field in the specified object
976979
*
977-
* @return true, if field value was set successfully, i.e. upon the call:
978-
* - field value is writable;
979-
* false - otherwise.
980+
* @return true - if field value was set successfully
981+
* false - otherwise
980982
*/
981983
bool
982984
jerry_set_object_field_value (jerry_object_t *object_p, /**< object */
@@ -1100,11 +1102,9 @@ jerry_set_object_native_handle (jerry_object_t *object_p, /**< object to set han
11001102
* If function is invoked as constructor, it should support [[Construct]] method,
11011103
* otherwise, if function is simply called - it should support [[Call]] method.
11021104
*
1103-
* @return true, if invocation was performed successfully, i.e.:
1104-
* - no unhandled exceptions were thrown in connection with the call;
1105-
* false - otherwise.
1105+
* @return returned jerry value of the given function object
11061106
*/
1107-
static bool
1107+
static jerry_value_t
11081108
jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke function as constructor
11091109
* (this_arg_p should be NULL, as it is ignored),
11101110
* false - perform function call */
@@ -1114,9 +1114,6 @@ jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke functio
11141114
* if function is invoked as constructor;
11151115
* in case of simple function call set 'this'
11161116
* binding to the global object) */
1117-
jerry_value_t *retval_p, /**< pointer to place for function's
1118-
* return value / thrown exception value
1119-
* or NULL (to ignore the values) */
11201117
const jerry_value_t args_p[], /**< function's call arguments
11211118
* (NULL if arguments number is zero) */
11221119
jerry_length_t args_count) /**< number of the arguments */
@@ -1125,8 +1122,6 @@ jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke functio
11251122
JERRY_STATIC_ASSERT (sizeof (args_count) == sizeof (ecma_length_t),
11261123
size_of_args_count_must_be_equal_to_size_of_ecma_length_t);
11271124

1128-
bool is_successful = true;
1129-
11301125
ecma_value_t call_completion;
11311126

11321127
if (is_invoke_as_constructor)
@@ -1159,29 +1154,19 @@ jerry_invoke_function (bool is_invoke_as_constructor, /**< true - invoke functio
11591154
args_count);
11601155
}
11611156

1162-
if (ECMA_IS_VALUE_ERROR (call_completion))
1163-
{
1164-
/* unhandled exception during the function call */
1165-
is_successful = false;
1166-
}
1167-
1168-
if (retval_p != NULL)
1169-
{
1170-
*retval_p = call_completion;
1171-
}
1172-
1173-
return is_successful;
1157+
return call_completion;
11741158
} /* jerry_invoke_function */
11751159

11761160
/**
11771161
* Construct new TypeError object
1162+
*
1163+
* @return TypeError object value
11781164
*/
1179-
static void
1180-
jerry_construct_type_error (jerry_value_t *retval_p) /**< [out] value with constructed
1181-
* TypeError object */
1165+
static jerry_value_t __attr_always_inline___
1166+
jerry_construct_type_error (void)
11821167
{
11831168
ecma_object_t *type_error_obj_p = ecma_new_standard_error (ECMA_ERROR_TYPE);
1184-
*retval_p = ecma_make_object_value (type_error_obj_p);
1169+
return ecma_make_error_obj_value (type_error_obj_p);
11851170
} /* jerry_construct_type_error */
11861171

11871172
/**
@@ -1191,20 +1176,12 @@ jerry_construct_type_error (jerry_value_t *retval_p) /**< [out] value with const
11911176
* returned value should be freed with jerry_release_value
11921177
* just when the value becomes unnecessary.
11931178
*
1194-
* @return true, if call was performed successfully, i.e.:
1195-
* - specified object is a function object (see also jerry_is_function);
1196-
* - no unhandled exceptions were thrown in connection with the call;
1197-
* false - otherwise, 'retval_p' contains thrown exception:
1198-
* if called object is not function object - a TypeError instance;
1199-
* else - exception, thrown during the function call.
1179+
* @return returned jerry value of the given function object
12001180
*/
1201-
bool
1181+
jerry_value_t
12021182
jerry_call_function (jerry_object_t *function_object_p, /**< function object to call */
12031183
jerry_object_t *this_arg_p, /**< object for 'this' binding
12041184
* or NULL (set 'this' binding to the global object) */
1205-
jerry_value_t *retval_p, /**< pointer to place for function's
1206-
* return value / thrown exception value
1207-
* or NULL (to ignore the values) */
12081185
const jerry_value_t args_p[], /**< function's call arguments
12091186
* (NULL if arguments number is zero) */
12101187
uint16_t args_count) /**< number of the arguments */
@@ -1213,17 +1190,10 @@ jerry_call_function (jerry_object_t *function_object_p, /**< function object to
12131190

12141191
if (jerry_is_function (function_object_p))
12151192
{
1216-
return jerry_invoke_function (false, function_object_p, this_arg_p, retval_p, args_p, args_count);
1193+
return jerry_invoke_function (false, function_object_p, this_arg_p, args_p, args_count);
12171194
}
1218-
else
1219-
{
1220-
if (retval_p != NULL)
1221-
{
1222-
jerry_construct_type_error (retval_p);
1223-
}
12241195

1225-
return false;
1226-
}
1196+
return jerry_construct_type_error ();
12271197
} /* jerry_call_function */
12281198

12291199
/**
@@ -1233,18 +1203,10 @@ jerry_call_function (jerry_object_t *function_object_p, /**< function object to
12331203
* returned value should be freed with jerry_release_value
12341204
* just when the value becomes unnecessary.
12351205
*
1236-
* @return true, if construction was performed successfully, i.e.:
1237-
* - specified object is a constructor function object (see also jerry_is_constructor);
1238-
* - no unhandled exceptions were thrown in connection with the invocation;
1239-
* false - otherwise, 'retval_p' contains thrown exception:
1240-
* if specified object is not a constructor function object - a TypeError instance;
1241-
* else - exception, thrown during the invocation.
1206+
* @return returned jerry value of the given constructor
12421207
*/
1243-
bool
1208+
jerry_value_t
12441209
jerry_construct_object (jerry_object_t *function_object_p, /**< function object to call */
1245-
jerry_value_t *retval_p, /**< pointer to place for function's
1246-
* return value / thrown exception value
1247-
* or NULL (to ignore the values) */
12481210
const jerry_value_t args_p[], /**< function's call arguments
12491211
* (NULL if arguments number is zero) */
12501212
uint16_t args_count) /**< number of the arguments */
@@ -1253,17 +1215,10 @@ jerry_construct_object (jerry_object_t *function_object_p, /**< function object
12531215

12541216
if (jerry_is_constructor (function_object_p))
12551217
{
1256-
return jerry_invoke_function (true, function_object_p, NULL, retval_p, args_p, args_count);
1218+
return jerry_invoke_function (true, function_object_p, NULL, args_p, args_count);
12571219
}
1258-
else
1259-
{
1260-
if (retval_p != NULL)
1261-
{
1262-
jerry_construct_type_error (retval_p);
1263-
}
12641220

1265-
return false;
1266-
}
1221+
return jerry_construct_type_error ();
12671222
} /* jerry_construct_object */
12681223

12691224
/**
@@ -1308,7 +1263,7 @@ jerry_eval (const jerry_char_t *source_p, /**< source code */
13081263
is_direct,
13091264
is_strict);
13101265

1311-
status = jerry_convert_eval_completion_to_retval (retval_p, completion);
1266+
status = jerry_convert_eval_completion_to_retval (completion, retval_p);
13121267

13131268
return status;
13141269
} /* jerry_eval */
@@ -2088,7 +2043,7 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */
20882043
/* vm should be already initialized */
20892044
ecma_value_t completion = vm_run_eval (bytecode_p, false);
20902045

2091-
ret_code = jerry_convert_eval_completion_to_retval (retval_p, completion);
2046+
ret_code = jerry_convert_eval_completion_to_retval (completion, retval_p);
20922047

20932048
ecma_free_value (completion);
20942049
}

main-unix.c

Lines changed: 10 additions & 13 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,16 +462,12 @@ 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))
470-
{
471-
jerry_release_value (ret_val_print);
472-
}
465+
jerry_value_t ret_val_print = jerry_call_function (jerry_get_object_value (print_function),
466+
NULL,
467+
args,
468+
1);
473469

470+
jerry_release_value (ret_val_print);
474471
jerry_release_value (ret_val);
475472
}
476473
}

0 commit comments

Comments
 (0)