Implement realm object and support realms for built-ins and JS functions (#4354)

- Type for realm objects is introduced (ecma_global_object_t)
- Realm reference is added to built-in objects and ECMAScript functions
- Resolving built-ins, global environments, and scopes require realm object
- Unnecessary global object accesses are removed from the code

Missing: external functions and static snapshot functions have no realm reference

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-12-08 14:36:36 +01:00
committed by GitHub
parent 7cb9f808f7
commit df92c86ecf
32 changed files with 1128 additions and 188 deletions
+30 -2
View File
@@ -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 */
@@ -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 */
@@ -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
{
@@ -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++)
@@ -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
{
@@ -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
@@ -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))
+2 -1
View File
@@ -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;
+37 -5
View File
@@ -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);
@@ -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);
@@ -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)));
}
@@ -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 */
@@ -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
*