Skip to content

Commit 0fd5939

Browse files
committed
Implement global realm and support realms for built-in objects
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent de37e1e commit 0fd5939

32 files changed

+1102
-186
lines changed

docs/02.API-REFERENCE.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ Possible compile time enabled feature types:
141141
- JERRY_FEATURE_SET - Set support
142142
- JERRY_FEATURE_WEAKMAP - WeakMap support
143143
- JERRY_FEATURE_WEAKSET - WeakSet support
144+
- JERRY_FEATURE_BIGINT - BigInt support
145+
- JERRY_FEATURE_REALM - realm support
144146

145147
*New in version 2.0*.
146148

@@ -5969,6 +5971,38 @@ jerry_create_undefined (void);
59695971
- [jerry_release_value](#jerry_release_value)
59705972

59715973

5974+
## jerry_create_realm
5975+
5976+
**Summary**
5977+
5978+
Creates a `jerry_value_t` representing a new global object.
5979+
5980+
**Prototype**
5981+
5982+
```c
5983+
jerry_value_t
5984+
jerry_create_realm (void);
5985+
```
5986+
5987+
- return value - realm object value
5988+
5989+
**Example**
5990+
5991+
```c
5992+
{
5993+
jerry_value_t realm_value = jerry_create_realm ();
5994+
5995+
... // usage of the value
5996+
5997+
jerry_release_value (realm_value);
5998+
}
5999+
```
6000+
6001+
**See also**
6002+
6003+
- [jerry_release_value](#jerry_release_value)
6004+
6005+
59726006
# General API functions of JS objects
59736007

59746008
## jerry_has_property

jerry-core/api/jerry-snapshot.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ snapshot_get_global_flags (bool has_regex, /**< regex literal is present */
4949
#if ENABLED (JERRY_ESNEXT)
5050
flags |= (has_class ? JERRY_SNAPSHOT_HAS_CLASS_LITERAL : 0);
5151
#endif /* ENABLED (JERRY_ESNEXT) */
52+
#if ENABLED (JERRY_BUILTIN_REALMS)
53+
flags |= JERRY_SNAPSHOT_HAS_REALM_VALUE;
54+
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
5255

5356
return flags;
5457
} /* snapshot_get_global_flags */
@@ -67,6 +70,9 @@ snapshot_check_global_flags (uint32_t global_flags) /**< global flags */
6770
#if ENABLED (JERRY_ESNEXT)
6871
global_flags &= (uint32_t) ~JERRY_SNAPSHOT_HAS_CLASS_LITERAL;
6972
#endif /* ENABLED (JERRY_ESNEXT) */
73+
#if ENABLED (JERRY_BUILTIN_REALMS)
74+
global_flags |= JERRY_SNAPSHOT_HAS_REALM_VALUE;
75+
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
7076

7177
return global_flags == snapshot_get_global_flags (false, false);
7278
} /* snapshot_check_global_flags */
@@ -380,6 +386,10 @@ static_snapshot_add_compiled_code (ecma_compiled_code_t *compiled_code_p, /**< c
380386
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) buffer_p;
381387
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
382388
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);
389+
390+
#if ENABLED (JERRY_BUILTIN_REALMS)
391+
args_p->realm_value = ECMA_VALUE_UNDEFINED;
392+
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
383393
}
384394
else
385395
{
@@ -388,6 +398,10 @@ static_snapshot_add_compiled_code (ecma_compiled_code_t *compiled_code_p, /**< c
388398
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) buffer_p;
389399
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
390400
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);
401+
402+
#if ENABLED (JERRY_BUILTIN_REALMS)
403+
args_p->realm_value = ECMA_VALUE_UNDEFINED;
404+
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
391405
}
392406

393407
for (uint32_t i = 0; i < const_literal_end; i++)
@@ -585,6 +599,10 @@ snapshot_load_compiled_code (const uint8_t *base_addr_p, /**< base address of th
585599
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);
586600
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
587601
header_size = sizeof (cbc_uint16_arguments_t);
602+
603+
#if ENABLED (JERRY_BUILTIN_REALMS)
604+
args_p->realm_value = ecma_make_object_value (ecma_builtin_get_global ());
605+
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
588606
}
589607
else
590608
{
@@ -595,6 +613,10 @@ snapshot_load_compiled_code (const uint8_t *base_addr_p, /**< base address of th
595613
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);
596614
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
597615
header_size = sizeof (cbc_uint8_arguments_t);
616+
617+
#if ENABLED (JERRY_BUILTIN_REALMS)
618+
args_p->realm_value = ecma_make_object_value (ecma_builtin_get_global ());
619+
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
598620
}
599621

600622
if (copy_bytecode
@@ -1003,14 +1025,20 @@ jerry_snapshot_result (const uint32_t *snapshot_p, /**< snapshot */
10031025

10041026
if (as_function)
10051027
{
1028+
ecma_object_t *global_object_p = ecma_builtin_get_global ();
1029+
1030+
#if ENABLED (JERRY_BUILTIN_REALMS)
1031+
JERRY_ASSERT (global_object_p == ecma_get_object_from_value (ecma_op_function_get_realm (bytecode_p)));
1032+
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
1033+
10061034
#if ENABLED (JERRY_ESNEXT)
10071035
if (bytecode_p->status_flags & CBC_CODE_FLAGS_LEXICAL_BLOCK_NEEDED)
10081036
{
1009-
ecma_create_global_lexical_block ();
1037+
ecma_create_global_lexical_block (global_object_p);
10101038
}
10111039
#endif /* ENABLED (JERRY_ESNEXT) */
10121040

1013-
ecma_object_t *lex_env_p = ecma_get_global_scope ();
1041+
ecma_object_t *lex_env_p = ecma_get_global_scope (global_object_p);
10141042
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_p);
10151043

10161044
if (!(bytecode_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION))

jerry-core/api/jerry-snapshot.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ typedef enum
4545
{
4646
/* 8 bits are reserved for dynamic features */
4747
JERRY_SNAPSHOT_HAS_REGEX_LITERAL = (1u << 0), /**< byte code has regex literal */
48-
JERRY_SNAPSHOT_HAS_CLASS_LITERAL = (1u << 1), /**< byte code has class literal */
48+
JERRY_SNAPSHOT_HAS_REALM_VALUE = (1u << 1), /**< byte code has realm value */
49+
JERRY_SNAPSHOT_HAS_CLASS_LITERAL = (1u << 2), /**< byte code has class literal */
4950
/* 24 bits are reserved for compile time features */
5051
JERRY_SNAPSHOT_FOUR_BYTE_CPOINTER = (1u << 8) /**< deprecated, an unused placeholder now */
5152
} jerry_snapshot_global_flags_t;

jerry-core/api/jerry.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
468468
return ecma_create_error_reference_from_context ();
469469
}
470470

471-
ecma_object_t *lex_env_p = ecma_get_global_environment ();
471+
ecma_object_t *lex_env_p = ecma_get_global_environment (ecma_builtin_get_global ());
472472
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_data_p);
473473
ecma_bytecode_deref (bytecode_data_p);
474474

@@ -541,7 +541,7 @@ jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (u
541541
return ecma_create_error_reference_from_context ();
542542
}
543543

544-
ecma_object_t *lex_env_p = ecma_get_global_environment ();
544+
ecma_object_t *lex_env_p = ecma_get_global_environment (ecma_builtin_get_global ());
545545
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_p);
546546
ecma_bytecode_deref (bytecode_p);
547547

@@ -586,10 +586,9 @@ jerry_run (const jerry_value_t func_val) /**< function to run */
586586

587587
ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) func_obj_p;
588588

589-
ecma_object_t *scope_p = ECMA_GET_NON_NULL_POINTER_FROM_POINTER_TAG (ecma_object_t,
590-
ext_func_p->u.function.scope_cp);
589+
const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);
591590

592-
if (scope_p != ecma_get_global_environment ())
591+
if (CBC_FUNCTION_GET_TYPE (bytecode_data_p->status_flags) != CBC_FUNCTION_SCRIPT)
593592
{
594593
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
595594
}
@@ -1341,6 +1340,9 @@ jerry_is_feature_enabled (const jerry_feature_t feature) /**< feature to check *
13411340
#if ENABLED (JERRY_BUILTIN_BIGINT)
13421341
|| feature == JERRY_FEATURE_BIGINT
13431342
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
1343+
#if ENABLED (JERRY_BUILTIN_REALMS)
1344+
|| feature == JERRY_FEATURE_REALM
1345+
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
13441346
);
13451347
} /* jerry_is_feature_enabled */
13461348

@@ -2299,6 +2301,24 @@ jerry_create_regexp_sz (const jerry_char_t *pattern_p, /**< zero-terminated UTF-
22992301
#endif /* ENABLED (JERRY_BUILTIN_REGEXP) */
23002302
} /* jerry_create_regexp_sz */
23012303

2304+
/**
2305+
* Creates a new realm (global object).
2306+
*
2307+
* @return new realm object
2308+
*/
2309+
jerry_value_t
2310+
jerry_create_realm (void)
2311+
{
2312+
jerry_assert_api_available ();
2313+
2314+
#if ENABLED (JERRY_BUILTIN_REALMS)
2315+
ecma_global_object_t *global_object_p = ecma_builtin_create_global_object ();
2316+
return ecma_make_object_value ((ecma_object_t *) global_object_p);
2317+
#else /* !ENABLED (JERRY_BUILTIN_REALMS) */
2318+
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Realms are disabled.")));
2319+
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
2320+
} /* jerry_create_realm */
2321+
23022322
/**
23032323
* Get length of an array object
23042324
*

jerry-core/config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
# define JERRY_ESNEXT 1
8080
#endif /* !defined (JERRY_ESNEXT) */
8181

82+
#ifndef JERRY_BUILTIN_REALMS
83+
# define JERRY_BUILTIN_REALMS JERRY_ESNEXT
84+
#endif /* !defined (JERRY_BUILTIN_REALMS) */
85+
8286
#ifndef JERRY_BUILTIN_DATAVIEW
8387
# define JERRY_BUILTIN_DATAVIEW JERRY_ESNEXT
8488
#endif /* !defined (JERRY_BUILTIN_DATAVIEW) */
@@ -523,6 +527,10 @@
523527
|| ((JERRY_ESNEXT != 0) && (JERRY_ESNEXT != 1))
524528
# error "Invalid value for JERRY_ESNEXT macro."
525529
#endif
530+
#if !defined (JERRY_BUILTIN_REALMS) \
531+
|| ((JERRY_BUILTIN_REALMS != 0) && (JERRY_BUILTIN_REALMS != 1))
532+
# error "Invalid value for JERRY_BUILTIN_REALMS macro."
533+
#endif
526534
#if !defined (JERRY_BUILTIN_DATAVIEW) \
527535
|| ((JERRY_BUILTIN_DATAVIEW != 0) && (JERRY_BUILTIN_DATAVIEW != 1))
528536
# error "Invalid value for JERRY_BUILTIN_DATAVIEW macro."

0 commit comments

Comments
 (0)