Skip to content

Commit c696bc9

Browse files
authored
Implement vm throw capture status management for API errors (#4783)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
1 parent 7c21fb8 commit c696bc9

File tree

7 files changed

+392
-117
lines changed

7 files changed

+392
-117
lines changed

docs/02.API-REFERENCE.md

Lines changed: 222 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3862,6 +3862,228 @@ void main(void)
38623862

38633863
- [jerry_error_object_created_callback_t](#jerry_error_object_created_callback_t)
38643864

3865+
## jerry_set_vm_throw_callback
3866+
3867+
**Summary**
3868+
3869+
The callback passed to this function is called when an error is thrown
3870+
in ECMAScript code. The callback is not called again until the value is
3871+
caught. See: [jerry_vm_throw_callback_t](#jerry_vm_throw_callback_t).
3872+
3873+
*Notes*:
3874+
- This API depends on a build option (`JERRY_VM_THROW`) and can be checked
3875+
in runtime with the `JERRY_FEATURE_VM_THROW` feature enum value,
3876+
see: [jerry_is_feature_enabled](#jerry_is_feature_enabled).
3877+
3878+
**Prototype**
3879+
3880+
```c
3881+
void
3882+
jerry_set_vm_throw_callback (jerry_vm_throw_callback_t throw_cb,
3883+
void *user_p);
3884+
```
3885+
3886+
- `throw_cb` - callback which is called on throws (passing NULL disables this feature)
3887+
- `user_p` - user pointer passed to the `throw_cb` function
3888+
3889+
*New in version [[NEXT_RELEASE]]*.
3890+
3891+
**Example**
3892+
3893+
[doctest]: # (test="compile")
3894+
3895+
```c
3896+
#include "jerryscript.h"
3897+
3898+
static void
3899+
vm_throw_callback (const jerry_value_t error_value, /**< captured error */
3900+
void *user_p) /**< user pointer */
3901+
{
3902+
(void) error_value;
3903+
3904+
/* Counts the number of throws. */
3905+
int *counter_p = (int *) user_p;
3906+
(*counter_p)++;
3907+
}
3908+
3909+
int
3910+
main (void)
3911+
{
3912+
jerry_init (JERRY_INIT_EMPTY);
3913+
3914+
int counter = 0;
3915+
jerry_set_vm_throw_callback (vm_throw_callback, &counter);
3916+
3917+
const jerry_char_t script[] = "try { throw new Error('1') } catch (e) { throw new Error('2') }";
3918+
3919+
jerry_release_value (jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS));
3920+
3921+
/* The counter contains 2. */
3922+
3923+
jerry_cleanup ();
3924+
return 0;
3925+
}
3926+
```
3927+
3928+
**See also**
3929+
3930+
- [jerry_vm_throw_callback_t](#jerry_vm_throw_callback_t)
3931+
- [jerry_error_is_throw_captured](#jerry_error_is_throw_captured)
3932+
- [jerry_error_set_throw_capture](#jerry_error_set_throw_capture)
3933+
3934+
## jerry_error_is_throw_captured
3935+
3936+
**Summary**
3937+
3938+
Checks whether the callback set by [jerry_set_vm_throw_callback](#jerry_set_vm_throw_callback)
3939+
captured the error.
3940+
3941+
*Notes*:
3942+
- This API depends on a build option (`JERRY_VM_THROW`) and can be checked
3943+
in runtime with the `JERRY_FEATURE_VM_THROW` feature enum value,
3944+
see: [jerry_is_feature_enabled](#jerry_is_feature_enabled).
3945+
3946+
**Prototype**
3947+
3948+
```c
3949+
bool jerry_error_is_throw_captured (jerry_value_t value);
3950+
```
3951+
3952+
- `value` - api value (should be an error reference)
3953+
- return value
3954+
- true, if the vm throw callback captured the error
3955+
- false, otherwise
3956+
3957+
*New in version [[NEXT_RELEASE]]*.
3958+
3959+
**Example**
3960+
3961+
[doctest]: # (test="compile")
3962+
3963+
```c
3964+
#include "jerryscript.h"
3965+
3966+
static void
3967+
vm_throw_callback (const jerry_value_t error_value, /**< captured error */
3968+
void *user_p) /**< user pointer */
3969+
{
3970+
(void) error_value;
3971+
(void) user_p;
3972+
}
3973+
3974+
int
3975+
main (void)
3976+
{
3977+
jerry_init (JERRY_INIT_EMPTY);
3978+
3979+
int counter = 0;
3980+
jerry_set_vm_throw_callback (vm_throw_callback, &counter);
3981+
3982+
const jerry_char_t script[] = "throw new Error()";
3983+
jerry_value_t result_value = jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS);
3984+
3985+
if (jerry_error_is_throw_captured (result_value))
3986+
{
3987+
/* Code enters here, because the vm_throw_callback function is called. */
3988+
}
3989+
3990+
jerry_release_value (result_value);
3991+
3992+
jerry_cleanup ();
3993+
return 0;
3994+
}
3995+
```
3996+
3997+
**See also**
3998+
3999+
- [jerry_set_vm_throw_callback](#jerry_set_vm_throw_callback)
4000+
- [jerry_error_set_throw_capture](#jerry_error_set_throw_capture)
4001+
4002+
## jerry_error_set_throw_capture
4003+
4004+
**Summary**
4005+
4006+
Sets whether the callback set by [jerry_set_vm_throw_callback](#jerry_set_vm_throw_callback)
4007+
should capture the error or not.
4008+
4009+
*Notes*:
4010+
- This API depends on a build option (`JERRY_VM_THROW`) and can be checked
4011+
in runtime with the `JERRY_FEATURE_VM_THROW` feature enum value,
4012+
see: [jerry_is_feature_enabled](#jerry_is_feature_enabled).
4013+
4014+
**Prototype**
4015+
4016+
```c
4017+
void jerry_error_set_throw_capture (jerry_value_t value, bool should_capture);
4018+
```
4019+
4020+
- `value` - api value (should be an error reference)
4021+
- `should_capture` - callback should capture this error
4022+
4023+
*New in version [[NEXT_RELEASE]]*.
4024+
4025+
**Example**
4026+
4027+
[doctest]: # (test="compile")
4028+
4029+
```c
4030+
#include "jerryscript.h"
4031+
4032+
static void
4033+
vm_throw_callback (const jerry_value_t error_value, /**< captured error */
4034+
void *user_p) /**< user pointer */
4035+
{
4036+
(void) error_value;
4037+
(void) user_p;
4038+
}
4039+
4040+
static jerry_value_t
4041+
throw_exception (const jerry_call_info_t *call_info_p, /**< call info */
4042+
const jerry_value_t argv[], /**< argument list */
4043+
const jerry_length_t argc) /**< argument count */
4044+
{
4045+
(void) call_info_p;
4046+
(void) argv;
4047+
(void) argc;
4048+
4049+
jerry_value_t result_value = jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Error!");
4050+
4051+
/* Ignore calling the vm_throw_callback function. */
4052+
jerry_error_set_throw_capture (result_value, false);
4053+
return result_value;
4054+
}
4055+
4056+
int
4057+
main (void)
4058+
{
4059+
jerry_init (JERRY_INIT_EMPTY);
4060+
4061+
int counter = 0;
4062+
jerry_set_vm_throw_callback (vm_throw_callback, &counter);
4063+
4064+
jerry_value_t global_object_value = jerry_get_global_object ();
4065+
jerry_value_t function_value = jerry_create_external_function (throw_exception);
4066+
jerry_value_t function_name_value = jerry_create_string ((const jerry_char_t *) "throw_exception");
4067+
4068+
jerry_release_value (jerry_set_property (global_object_value, function_name_value, function_value));
4069+
jerry_release_value (function_name_value);
4070+
jerry_release_value (function_value);
4071+
jerry_release_value (global_object_value);
4072+
4073+
const jerry_char_t script[] = "throw_exception()";
4074+
jerry_release_value (jerry_eval (script, sizeof (script) - 1, JERRY_PARSE_NO_OPTS));
4075+
4076+
jerry_cleanup ();
4077+
return 0;
4078+
}
4079+
```
4080+
4081+
**See also**
4082+
4083+
- [jerry_set_vm_throw_callback](#jerry_set_vm_throw_callback)
4084+
- [jerry_error_is_throw_captured](#jerry_error_is_throw_captured)
4085+
4086+
38654087
# Getter functions of 'jerry_value_t'
38664088

38674089
Get raw data from API values.
@@ -11680,75 +11902,6 @@ main (void)
1168011902

1168111903
- [jerry_vm_exec_stop_callback_t](#jerry_vm_exec_stop_callback_t)
1168211904

11683-
## jerry_set_vm_throw_callback
11684-
11685-
**Summary**
11686-
11687-
The callback passed to this function is called when an error is thrown
11688-
in ECMAScript code. The callback is not called again until the value is
11689-
caught. See: [jerry_vm_throw_callback_t](#jerry_vm_throw_callback_t).
11690-
11691-
*Notes*:
11692-
- This API depends on a build option (`JERRY_VM_THROW`) and can be checked
11693-
in runtime with the `JERRY_FEATURE_VM_THROW` feature enum value,
11694-
see: [jerry_is_feature_enabled](#jerry_is_feature_enabled).
11695-
11696-
**Prototype**
11697-
11698-
```c
11699-
void
11700-
jerry_set_vm_throw_callback (jerry_vm_throw_callback_t throw_cb,
11701-
void *user_p);
11702-
```
11703-
11704-
- `throw_cb` - callback which is called on throws (passing NULL disables this feature)
11705-
- `user_p` - user pointer passed to the `throw_cb` function
11706-
11707-
*New in version [[NEXT_RELEASE]]*.
11708-
11709-
**Example**
11710-
11711-
[doctest]: # (test="compile")
11712-
11713-
```c
11714-
#include "jerryscript.h"
11715-
11716-
static void
11717-
vm_throw_callback (const jerry_value_t error_value, /**< captured error */
11718-
void *user_p) /**< user pointer */
11719-
{
11720-
(void) error_value;
11721-
11722-
/* Counts the number of throws. */
11723-
int *counter_p = (int *) user_p;
11724-
(*counter_p)++;
11725-
}
11726-
11727-
int
11728-
main (void)
11729-
{
11730-
jerry_init (JERRY_INIT_EMPTY);
11731-
11732-
int counter = 0;
11733-
jerry_set_vm_throw_callback (vm_throw_callback, &counter);
11734-
11735-
const jerry_char_t script[] = "try { throw new Error('1') } catch (e) { throw new Error('2') }";
11736-
11737-
jerry_value_t parsed_code = jerry_parse (script, sizeof (script) - 1, NULL);
11738-
jerry_release_value (jerry_run (parsed_code));
11739-
jerry_release_value (parsed_code);
11740-
11741-
/* The counter contains 2. */
11742-
11743-
jerry_cleanup ();
11744-
return 0;
11745-
}
11746-
```
11747-
11748-
**See also**
11749-
11750-
- [jerry_vm_throw_callback_t](#jerry_vm_throw_callback_t)
11751-
1175211905
## jerry_get_resource_name
1175311906

1175411907
**Summary**

0 commit comments

Comments
 (0)