Introduce new.target C api (#3522)

Added new "jerry_get_new_target" API function
and updated the unit test.

JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
Péter Gál
2020-01-18 10:58:33 +01:00
committed by Dániel Bátyai
parent f46d061d19
commit 24af089643
4 changed files with 164 additions and 13 deletions
+132
View File
@@ -7655,6 +7655,138 @@ main (void)
- [jerry_create_external_function](#jerry_create_external_function)
## jerry_get_new_target
**Summary**
Returns the current "new.target" JavaScript function at the call site.
If used outside of a native C function it will return "undefined" value.
*Notes*:
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
is no longer needed.
- This feature depends on build option (`JERRY_ES2015`) and can be checked
in runtime with the `JERRY_FEATURE_SYMBOL` feature enum value (as symbols are enabled in case of ES2015),
see: [jerry_is_feature_enabled](#jerry_is_feature_enabled).
- If the ES2015 mode is not enabled this method will always return the "undefined" value.
**Prototype**
```c
jerry_value_t
jerry_get_new_target (void);
```
- return
- "undefined" - if at the call site it was not a constructor call.
- function object - if the current call site is in a constructor call.
*New in version 2.2*.
**Example 1**
[doctest]: # (name="02.API-REFERENCE-jsnewtarget-01.c")
```c
#include <stdio.h>
#include <string.h>
#include <jerryscript.h>
static jerry_value_t
demo_handler (const jerry_value_t func_obj_val,
const jerry_value_t this_val,
const jerry_value_t args_p[],
const jerry_length_t args_cnt)
{
jerry_value_t new_target = jerry_get_new_target ();
/* new_target is the "demo" JS function object */
if (jerry_value_get_type (new_target) == JERRY_TYPE_FUNCTION)
{
printf ("This is a construct call\r\n");
}
jerry_release_value (new_target);
return jerry_create_undefined ();
}
int
main (int argc, char** argv)
{
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t function_val = jerry_create_external_function (demo_handler);
jerry_value_t ret_val = jerry_construct_object (function_val, NULL, 0);
jerry_release_value (ret_val);
jerry_release_value (function_val);
jerry_cleanup ();
return 0;
}
```
**Example 2**
[doctest]: # (name="02.API-REFERENCE-jsnewtarget-02.c")
```c
#include <stdio.h>
#include <string.h>
#include <jerryscript.h>
static jerry_value_t
demo_handler (const jerry_value_t func_obj_val,
const jerry_value_t this_val,
const jerry_value_t args_p[],
const jerry_length_t args_cnt)
{
jerry_value_t new_target = jerry_get_new_target ();
/* new_target is a JS function object */
if (jerry_value_get_type (new_target) == JERRY_TYPE_FUNCTION)
{
printf ("This is a construct call\r\n");
}
jerry_release_value (new_target);
return jerry_create_undefined ();
}
int
main (int argc, char** argv)
{
jerry_init (JERRY_INIT_EMPTY);
/* register C method */
jerry_value_t global_obj_val = jerry_get_global_object ();
jerry_value_t function_val = jerry_create_external_function (demo_handler);
jerry_value_t function_name_val = jerry_create_string ((const jerry_char_t *) "demo");
jerry_value_t result_val = jerry_set_property (global_obj_val, function_name_val, function_val);
jerry_release_value (result_val);
jerry_release_value (function_name_val);
jerry_release_value (function_val);
jerry_release_value (global_obj_val);
/* Invoke C method via JS */
const char *src = "new demo ()";
jerry_value_t ret_val = jerry_eval ((const jerry_char_t *) src,
strlen (src),
JERRY_PARSE_NO_OPTS);
jerry_release_value (ret_val);
jerry_cleanup ();
return 0;
}
```
**See also**
- [jerry_construct_object](#jerry_construct_object)
# ArrayBuffer and TypedArray functions