Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/02.API-REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ Possible compile time enabled feature types:
- JERRY_FEATURE_SET - Set support
- JERRY_FEATURE_WEAKMAP - WeakMap support
- JERRY_FEATURE_WEAKSET - WeakSet support
- JERRY_FEATURE_BIGINT - BigInt support
- JERRY_FEATURE_REALM - realm support

*New in version 2.0*.

Expand Down Expand Up @@ -5969,6 +5971,38 @@ jerry_create_undefined (void);
- [jerry_release_value](#jerry_release_value)


## jerry_create_realm

**Summary**

Creates a `jerry_value_t` representing a new global object.

**Prototype**

```c
jerry_value_t
jerry_create_realm (void);
```

- return value - realm object value

**Example**

```c
{
jerry_value_t realm_value = jerry_create_realm ();

... // usage of the value

jerry_release_value (realm_value);
}
```

**See also**

- [jerry_release_value](#jerry_release_value)


# General API functions of JS objects

## jerry_has_property
Expand Down
32 changes: 30 additions & 2 deletions jerry-core/api/jerry-snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ snapshot_get_global_flags (bool has_regex, /**< regex literal is present */
#if ENABLED (JERRY_ESNEXT)
flags |= (has_class ? JERRY_SNAPSHOT_HAS_CLASS_LITERAL : 0);
#endif /* ENABLED (JERRY_ESNEXT) */
#if ENABLED (JERRY_BUILTIN_REALMS)
flags |= JERRY_SNAPSHOT_HAS_REALM_VALUE;
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */

return flags;
} /* snapshot_get_global_flags */
Expand All @@ -67,6 +70,9 @@ snapshot_check_global_flags (uint32_t global_flags) /**< global flags */
#if ENABLED (JERRY_ESNEXT)
global_flags &= (uint32_t) ~JERRY_SNAPSHOT_HAS_CLASS_LITERAL;
#endif /* ENABLED (JERRY_ESNEXT) */
#if ENABLED (JERRY_BUILTIN_REALMS)
global_flags |= JERRY_SNAPSHOT_HAS_REALM_VALUE;
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */

return global_flags == snapshot_get_global_flags (false, false);
} /* snapshot_check_global_flags */
Expand Down Expand Up @@ -380,6 +386,10 @@ static_snapshot_add_compiled_code (ecma_compiled_code_t *compiled_code_p, /**< c
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) buffer_p;
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);

#if ENABLED (JERRY_BUILTIN_REALMS)
args_p->realm_value = ECMA_VALUE_UNDEFINED;
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
}
else
{
Expand All @@ -388,6 +398,10 @@ static_snapshot_add_compiled_code (ecma_compiled_code_t *compiled_code_p, /**< c
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) buffer_p;
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);

#if ENABLED (JERRY_BUILTIN_REALMS)
args_p->realm_value = ECMA_VALUE_UNDEFINED;
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
}

for (uint32_t i = 0; i < const_literal_end; i++)
Expand Down Expand Up @@ -585,6 +599,10 @@ snapshot_load_compiled_code (const uint8_t *base_addr_p, /**< base address of th
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
header_size = sizeof (cbc_uint16_arguments_t);

#if ENABLED (JERRY_BUILTIN_REALMS)
args_p->realm_value = ecma_make_object_value (ecma_builtin_get_global ());
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
}
else
{
Expand All @@ -595,6 +613,10 @@ snapshot_load_compiled_code (const uint8_t *base_addr_p, /**< base address of th
const_literal_end = (uint32_t) (args_p->const_literal_end - args_p->register_end);
literal_end = (uint32_t) (args_p->literal_end - args_p->register_end);
header_size = sizeof (cbc_uint8_arguments_t);

#if ENABLED (JERRY_BUILTIN_REALMS)
args_p->realm_value = ecma_make_object_value (ecma_builtin_get_global ());
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
}

if (copy_bytecode
Expand Down Expand Up @@ -1003,14 +1025,20 @@ jerry_snapshot_result (const uint32_t *snapshot_p, /**< snapshot */

if (as_function)
{
ecma_object_t *global_object_p = ecma_builtin_get_global ();

#if ENABLED (JERRY_BUILTIN_REALMS)
JERRY_ASSERT (global_object_p == ecma_get_object_from_value (ecma_op_function_get_realm (bytecode_p)));
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */

#if ENABLED (JERRY_ESNEXT)
if (bytecode_p->status_flags & CBC_CODE_FLAGS_LEXICAL_BLOCK_NEEDED)
{
ecma_create_global_lexical_block ();
ecma_create_global_lexical_block (global_object_p);
}
#endif /* ENABLED (JERRY_ESNEXT) */

ecma_object_t *lex_env_p = ecma_get_global_scope ();
ecma_object_t *lex_env_p = ecma_get_global_scope (global_object_p);
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_p);

if (!(bytecode_p->status_flags & CBC_CODE_FLAGS_STATIC_FUNCTION))
Expand Down
3 changes: 2 additions & 1 deletion jerry-core/api/jerry-snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ typedef enum
{
/* 8 bits are reserved for dynamic features */
JERRY_SNAPSHOT_HAS_REGEX_LITERAL = (1u << 0), /**< byte code has regex literal */
JERRY_SNAPSHOT_HAS_CLASS_LITERAL = (1u << 1), /**< byte code has class literal */
JERRY_SNAPSHOT_HAS_REALM_VALUE = (1u << 1), /**< byte code has realm value */
JERRY_SNAPSHOT_HAS_CLASS_LITERAL = (1u << 2), /**< byte code has class literal */
/* 24 bits are reserved for compile time features */
JERRY_SNAPSHOT_FOUR_BYTE_CPOINTER = (1u << 8) /**< deprecated, an unused placeholder now */
} jerry_snapshot_global_flags_t;
Expand Down
42 changes: 37 additions & 5 deletions jerry-core/api/jerry.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,13 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
return ecma_create_error_reference_from_context ();
}

ecma_object_t *lex_env_p = ecma_get_global_environment ();
ecma_object_t *global_object_p = ecma_builtin_get_global ();

#if ENABLED (JERRY_BUILTIN_REALMS)
JERRY_ASSERT (global_object_p == ecma_get_object_from_value (ecma_op_function_get_realm (bytecode_data_p)));
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */

ecma_object_t *lex_env_p = ecma_get_global_environment (global_object_p);
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_data_p);
ecma_bytecode_deref (bytecode_data_p);

Expand Down Expand Up @@ -541,7 +547,13 @@ jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (u
return ecma_create_error_reference_from_context ();
}

ecma_object_t *lex_env_p = ecma_get_global_environment ();
ecma_object_t *global_object_p = ecma_builtin_get_global ();

#if ENABLED (JERRY_BUILTIN_REALMS)
JERRY_ASSERT (global_object_p == ecma_get_object_from_value (ecma_op_function_get_realm (bytecode_p)));
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */

ecma_object_t *lex_env_p = ecma_get_global_environment (global_object_p);
ecma_object_t *func_obj_p = ecma_op_create_simple_function_object (lex_env_p, bytecode_p);
ecma_bytecode_deref (bytecode_p);

Expand Down Expand Up @@ -586,10 +598,9 @@ jerry_run (const jerry_value_t func_val) /**< function to run */

ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) func_obj_p;

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

if (scope_p != ecma_get_global_environment ())
if (CBC_FUNCTION_GET_TYPE (bytecode_data_p->status_flags) != CBC_FUNCTION_SCRIPT)
{
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p)));
}
Expand Down Expand Up @@ -1341,6 +1352,9 @@ jerry_is_feature_enabled (const jerry_feature_t feature) /**< feature to check *
#if ENABLED (JERRY_BUILTIN_BIGINT)
|| feature == JERRY_FEATURE_BIGINT
#endif /* ENABLED (JERRY_BUILTIN_BIGINT) */
#if ENABLED (JERRY_BUILTIN_REALMS)
|| feature == JERRY_FEATURE_REALM
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
);
} /* jerry_is_feature_enabled */

Expand Down Expand Up @@ -2299,6 +2313,24 @@ jerry_create_regexp_sz (const jerry_char_t *pattern_p, /**< zero-terminated UTF-
#endif /* ENABLED (JERRY_BUILTIN_REGEXP) */
} /* jerry_create_regexp_sz */

/**
* Creates a new realm (global object).
*
* @return new realm object
*/
jerry_value_t
jerry_create_realm (void)
{
jerry_assert_api_available ();

#if ENABLED (JERRY_BUILTIN_REALMS)
ecma_global_object_t *global_object_p = ecma_builtin_create_global_object ();
return ecma_make_object_value ((ecma_object_t *) global_object_p);
#else /* !ENABLED (JERRY_BUILTIN_REALMS) */
return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Realms are disabled.")));
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
} /* jerry_create_realm */

/**
* Get length of an array object
*
Expand Down
8 changes: 8 additions & 0 deletions jerry-core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
# define JERRY_ESNEXT 1
#endif /* !defined (JERRY_ESNEXT) */

#ifndef JERRY_BUILTIN_REALMS
# define JERRY_BUILTIN_REALMS JERRY_ESNEXT
#endif /* !defined (JERRY_BUILTIN_REALMS) */

#ifndef JERRY_BUILTIN_DATAVIEW
# define JERRY_BUILTIN_DATAVIEW JERRY_ESNEXT
#endif /* !defined (JERRY_BUILTIN_DATAVIEW) */
Expand Down Expand Up @@ -523,6 +527,10 @@
|| ((JERRY_ESNEXT != 0) && (JERRY_ESNEXT != 1))
# error "Invalid value for JERRY_ESNEXT macro."
#endif
#if !defined (JERRY_BUILTIN_REALMS) \
|| ((JERRY_BUILTIN_REALMS != 0) && (JERRY_BUILTIN_REALMS != 1))
# error "Invalid value for JERRY_BUILTIN_REALMS macro."
#endif
#if !defined (JERRY_BUILTIN_DATAVIEW) \
|| ((JERRY_BUILTIN_DATAVIEW != 0) && (JERRY_BUILTIN_DATAVIEW != 1))
# error "Invalid value for JERRY_BUILTIN_DATAVIEW macro."
Expand Down
Loading