Skip to content

Commit 0c05874

Browse files
committed
Implemented jerry_api_create_error()
JerryScript-DCO-1.0-Signed-off-by: Ilyong Cho [email protected]
1 parent 7dfbc88 commit 0c05874

File tree

3 files changed

+155
-6
lines changed

3 files changed

+155
-6
lines changed

jerry-core/jerry-api.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ typedef enum
5757
JERRY_API_DATA_TYPE_OBJECT /**< object */
5858
} jerry_api_data_type_t;
5959

60+
/**
61+
* Jerry API Error object types
62+
*/
63+
typedef enum
64+
{
65+
JERRY_API_ERROR_COMMON, /**< Error */
66+
JERRY_API_ERROR_EVAL, /**< EvalError */
67+
JERRY_API_ERROR_RANGE, /**< RangeError */
68+
JERRY_API_ERROR_REFERENCE, /**< ReferenceError */
69+
JERRY_API_ERROR_SYNTAX, /**< SyntaxError */
70+
JERRY_API_ERROR_TYPE, /**< TypeError */
71+
JERRY_API_ERROR_URI /**< URIError */
72+
} jerry_api_error_t;
73+
6074
/**
6175
* Jerry's string value
6276
*/
@@ -127,6 +141,8 @@ jerry_api_string_t* jerry_api_create_string (const char *v);
127141
extern EXTERN_C
128142
jerry_api_object_t* jerry_api_create_object (void);
129143
extern EXTERN_C
144+
jerry_api_object_t* jerry_api_create_error (jerry_api_error_t error_type, const char *message_p);
145+
extern EXTERN_C
130146
jerry_api_object_t* jerry_api_create_external_function (jerry_external_handler_t handler_p);
131147

132148
extern EXTERN_C

jerry-core/jerry.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "ecma-alloc.h"
1919
#include "ecma-builtins.h"
20+
#include "ecma-exceptions.h"
2021
#include "ecma-extension.h"
2122
#include "ecma-eval.h"
2223
#include "ecma-function-object.h"
@@ -415,6 +416,80 @@ jerry_api_create_object (void)
415416
return ecma_op_create_object_object_noarg ();
416417
} /* jerry_api_create_object */
417418

419+
/**
420+
* Create an error object
421+
*
422+
* Note:
423+
* caller should release the object with jerry_api_release_object, just when the value becomes unnecessary.
424+
*
425+
* @return pointer to created error object
426+
*/
427+
jerry_api_object_t*
428+
jerry_api_create_error (jerry_api_error_t error_type, const char *message_p)
429+
{
430+
jerry_assert_api_available ();
431+
432+
ecma_standard_error_t standard_error_type = ECMA_ERROR_COMMON;
433+
434+
switch (error_type)
435+
{
436+
case JERRY_API_ERROR_COMMON:
437+
{
438+
standard_error_type = ECMA_ERROR_COMMON;
439+
break;
440+
}
441+
case JERRY_API_ERROR_EVAL:
442+
{
443+
standard_error_type = ECMA_ERROR_EVAL;
444+
break;
445+
}
446+
case JERRY_API_ERROR_RANGE:
447+
{
448+
standard_error_type = ECMA_ERROR_RANGE;
449+
break;
450+
}
451+
case JERRY_API_ERROR_REFERENCE:
452+
{
453+
standard_error_type = ECMA_ERROR_REFERENCE;
454+
break;
455+
}
456+
case JERRY_API_ERROR_SYNTAX:
457+
{
458+
standard_error_type = ECMA_ERROR_SYNTAX;
459+
break;
460+
}
461+
case JERRY_API_ERROR_TYPE:
462+
{
463+
standard_error_type = ECMA_ERROR_TYPE;
464+
break;
465+
}
466+
case JERRY_API_ERROR_URI:
467+
{
468+
standard_error_type = ECMA_ERROR_URI;
469+
break;
470+
}
471+
default:
472+
{
473+
JERRY_UNREACHABLE ();
474+
}
475+
}
476+
477+
if (message_p == NULL)
478+
{
479+
return ecma_new_standard_error (standard_error_type);
480+
}
481+
else
482+
{
483+
ecma_string_t* message_string_p = ecma_new_ecma_string ((const ecma_char_t*) message_p);
484+
485+
ecma_object_t* error_object_p = ecma_new_standard_error_with_message (standard_error_type, message_string_p);
486+
487+
ecma_deref_ecma_string (message_string_p);
488+
489+
return error_object_p;
490+
}
491+
}
492+
418493
/**
419494
* Create an external function object
420495
*

tests/unit/test_api.cpp

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,21 @@ const char *test_source = (
3434
"this.t = 12; "
3535
"} "
3636
"this.A = A; "
37-
"this.a = new A ();"
38-
"function call_external () {"
39-
" return this.external ('1', true);"
40-
"}"
37+
"this.a = new A (); "
38+
"function call_external () { "
39+
" return this.external ('1', true); "
40+
"} "
41+
"function call_throw_test() { "
42+
" bool catched = false "
43+
" try { "
44+
" this.throw_test(); "
45+
" } catch (e) { "
46+
" catched = true; "
47+
" assert(e.name == 'TypeError'); "
48+
" assert(e.message == 'error'); "
49+
" } "
50+
" assert(catched); "
51+
"} "
4152
);
4253

4354
bool test_api_is_free_callback_was_called = false;
@@ -117,6 +128,24 @@ handler (const jerry_api_object_t *function_obj_p,
117128
return true;
118129
} /* handler */
119130

131+
static bool
132+
handler_throw_test (const jerry_api_object_t *function_obj_p,
133+
const jerry_api_value_t *this_p,
134+
jerry_api_value_t *ret_val_p,
135+
const jerry_api_value_t args_p[],
136+
const uint16_t args_cnt)
137+
{
138+
printf ("ok %p %p %p %d %p\n", function_obj_p, this_p, args_p, args_cnt, ret_val_p);
139+
140+
jerry_api_object_t* error_p = jerry_api_create_error (JERRY_API_ERROR_TYPE, "error");
141+
142+
test_api_init_api_value_object (ret_val_p, error_p);
143+
144+
jerry_api_release_object (error_p);
145+
146+
return false;
147+
}
148+
120149
static void
121150
handler_construct_freecb (uintptr_t native_p)
122151
{
@@ -162,6 +191,7 @@ main (void)
162191
jerry_api_value_t val_external, val_external_construct, val_call_external;
163192
jerry_api_object_t* global_obj_p;
164193
jerry_api_object_t* external_func_p, *external_construct_p;
194+
jerry_api_object_t* throw_test_handler_p;
165195
jerry_api_value_t res, args[2];
166196
char buffer[32];
167197

@@ -308,8 +338,6 @@ main (void)
308338
jerry_api_release_value (&res);
309339
assert (!strcmp (buffer, "string from handler"));
310340

311-
jerry_api_release_object (global_obj_p);
312-
313341
// Create native handler bound function object and set it to 'external_construct' variable
314342
external_construct_p = jerry_api_create_external_function (handler_construct);
315343
assert (external_construct_p != NULL
@@ -346,6 +374,36 @@ main (void)
346374

347375
jerry_api_release_value (&res);
348376

377+
378+
// Test: Throwing exception from native handler.
379+
throw_test_handler_p = jerry_api_create_external_function (handler_throw_test);
380+
assert (throw_test_handler_p != NULL
381+
&& jerry_api_is_function (throw_test_handler_p));
382+
383+
test_api_init_api_value_object (&val_t, throw_test_handler_p);
384+
is_ok = jerry_api_set_object_field_value (global_obj_p,
385+
"throw_test",
386+
&val_t);
387+
assert (is_ok);
388+
jerry_api_release_value (&val_t);
389+
jerry_api_release_object (throw_test_handler_p);
390+
391+
is_ok = jerry_api_get_object_field_value (global_obj_p, "call_throw_test", &val_t);
392+
assert (is_ok
393+
&& val_t.type == JERRY_API_DATA_TYPE_OBJECT);
394+
395+
is_ok = jerry_api_call_function (val_t.v_object,
396+
global_obj_p,
397+
&res,
398+
NULL, 0);
399+
assert (is_ok);
400+
jerry_api_release_value (&val_t);
401+
jerry_api_release_value (&res);
402+
403+
404+
// cleanup.
405+
jerry_api_release_object (global_obj_p);
406+
349407
jerry_cleanup ();
350408

351409
assert (test_api_is_free_callback_was_called);

0 commit comments

Comments
 (0)