Allow changing the 'this' binding of a realm (#4357)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-12-14 10:05:30 +01:00
committed by GitHub
parent 7e135b04ed
commit fe29bf7390
15 changed files with 198 additions and 45 deletions
+8 -4
View File
@@ -159,6 +159,10 @@ ecma_gc_mark_global_object (ecma_global_object_t *global_object_p) /**< global o
ecma_gc_set_object_visited (ECMA_GET_NON_NULL_POINTER (ecma_object_t, global_object_p->global_env_cp));
#if ENABLED (JERRY_BUILTIN_REALMS)
ecma_gc_set_object_visited (ecma_get_object_from_value (global_object_p->this_binding));
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
#if ENABLED (JERRY_ESNEXT)
if (global_object_p->global_scope_cp != global_object_p->global_env_cp)
{
@@ -919,20 +923,20 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
}
#endif /* ENABLED (JERRY_SNAPSHOT_EXEC) */
ecma_value_t realm_value;
ecma_object_t *realm_p;
if (byte_code_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) byte_code_p;
realm_value = args_p->realm_value;
realm_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t, args_p->realm_value);
}
else
{
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) byte_code_p;
realm_value = args_p->realm_value;
realm_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t, args_p->realm_value);
}
ecma_gc_set_object_visited (ecma_get_object_from_value (realm_value));
ecma_gc_set_object_visited (realm_p);
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
break;
}
+1 -2
View File
@@ -486,8 +486,7 @@ ecma_module_evaluate (ecma_module_t *module_p) /**< module */
}
#if ENABLED (JERRY_BUILTIN_REALMS)
ecma_object_t *global_object_p;
global_object_p = ecma_get_object_from_value (ecma_op_function_get_realm (module_p->compiled_code_p));
ecma_object_t *global_object_p = (ecma_object_t *) ecma_op_function_get_realm (module_p->compiled_code_p);
#else /* !ENABLED (JERRY_BUILTIN_REALMS) */
ecma_object_t *global_object_p = ecma_builtin_get_global ();
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
@@ -614,6 +614,7 @@ ecma_builtin_create_global_object (void)
#if ENABLED (JERRY_BUILTIN_REALMS)
ECMA_SET_INTERNAL_VALUE_POINTER (global_object_p->extended_object.u.built_in.realm_value, global_object_p);
global_object_p->extra_realms_bitset = 0;
global_object_p->this_binding = ecma_make_object_value (object_p);
#else /* !ENABLED (JERRY_BUILTIN_REALMS) */
global_object_p->extended_object.u.built_in.continue_instantiated_bitset[0] = 0;
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
@@ -108,6 +108,7 @@ typedef struct
uint32_t extra_instantiated_bitset[1]; /**< extra bit set for instantiated properties */
#if ENABLED (JERRY_BUILTIN_REALMS)
uint32_t extra_realms_bitset; /**< extra bit set for instantiated properties when realms is enabled */
ecma_value_t this_binding; /**< 'this' binding of this global object */
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
jmem_cpointer_t global_env_cp; /**< global lexical environment */
#if ENABLED (JERRY_ESNEXT)
@@ -484,7 +484,7 @@ ecma_op_create_dynamic_function (const ecma_value_t *arguments_list_p, /**< argu
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)));
JERRY_ASSERT (global_object_p == (ecma_object_t *) ecma_op_function_get_realm (bytecode_p));
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
ecma_object_t *global_env_p = ecma_get_global_environment (global_object_p);
@@ -755,9 +755,9 @@ ecma_op_function_get_compiled_code (ecma_extended_object_t *function_p) /**< fun
* Note:
* Does not increase the reference counter.
*
* @return realm (global) object
* @return pointer to realm (global) object
*/
inline ecma_value_t JERRY_ATTR_ALWAYS_INLINE
inline ecma_global_object_t * JERRY_ATTR_ALWAYS_INLINE
ecma_op_function_get_realm (const ecma_compiled_code_t *bytecode_header_p) /**< byte code header */
{
ecma_value_t realm_value;
@@ -774,14 +774,14 @@ ecma_op_function_get_realm (const ecma_compiled_code_t *bytecode_header_p) /**<
}
#if ENABLED (JERRY_SNAPSHOT_EXEC)
if (JERRY_LIKELY (realm_value != ECMA_VALUE_UNDEFINED))
if (JERRY_LIKELY (realm_value != JMEM_CP_NULL))
{
return realm_value;
return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t, realm_value);
}
return ecma_make_object_value (ecma_builtin_get_global ());
return (ecma_global_object_t *) ecma_builtin_get_global ();
#else /* !ENABLED (JERRY_SNAPSHOT_EXEC) */
return realm_value;
return ECMA_GET_INTERNAL_VALUE_POINTER (ecma_global_object_t, realm_value);
#endif /* ENABLED (JERRY_SNAPSHOT_EXEC) */
} /* ecma_op_function_get_realm */
@@ -800,8 +800,7 @@ ecma_op_function_get_function_realm (ecma_object_t *func_obj_p) /**< function ob
{
ecma_extended_object_t *ext_function_obj_p = (ecma_extended_object_t *) func_obj_p;
const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_function_obj_p);
ecma_value_t realm_value = ecma_op_function_get_realm (bytecode_data_p);
return (ecma_global_object_t *) ecma_get_object_from_value (realm_value);
return ecma_op_function_get_realm (bytecode_data_p);
}
JERRY_ASSERT (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_NATIVE_FUNCTION);
@@ -1074,7 +1073,7 @@ ecma_op_function_call_simple (ecma_object_t *func_obj_p, /**< Function object */
shared_args.header.bytecode_header_p = bytecode_data_p;
#if ENABLED (JERRY_BUILTIN_REALMS)
ecma_value_t realm_value = ecma_op_function_get_realm (bytecode_data_p);
ecma_global_object_t *realm_p = ecma_op_function_get_realm (bytecode_data_p);
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
/* 1. */
@@ -1105,7 +1104,7 @@ ecma_op_function_call_simple (ecma_object_t *func_obj_p, /**< Function object */
{
/* 2. */
#if ENABLED (JERRY_BUILTIN_REALMS)
this_binding = realm_value;
this_binding = realm_p->this_binding;
#else /* !ENABLED (JERRY_BUILTIN_REALMS) */
this_binding = ecma_make_object_value (ecma_builtin_get_global ());
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
@@ -1155,7 +1154,7 @@ ecma_op_function_call_simple (ecma_object_t *func_obj_p, /**< Function object */
#if ENABLED (JERRY_BUILTIN_REALMS)
ecma_global_object_t *saved_global_object_p = JERRY_CONTEXT (global_object_p);
JERRY_CONTEXT (global_object_p) = (ecma_global_object_t *) ecma_get_object_from_value (realm_value);
JERRY_CONTEXT (global_object_p) = realm_p;
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
ret_value = vm_run (&shared_args.header, this_binding, scope_p);
@@ -1634,8 +1633,7 @@ ecma_op_lazy_instantiate_prototype_object (ecma_object_t *object_p) /**< the fun
const ecma_compiled_code_t *bytecode_data_p;
bytecode_data_p = ecma_op_function_get_compiled_code ((ecma_extended_object_t *) object_p);
ecma_value_t realm_value = ecma_op_function_get_realm (bytecode_data_p);
global_object_p = (ecma_global_object_t *) ecma_get_object_from_value (realm_value);
global_object_p = ecma_op_function_get_realm (bytecode_data_p);
}
else
{
@@ -57,7 +57,7 @@ const ecma_compiled_code_t *
ecma_op_function_get_compiled_code (ecma_extended_object_t *function_p);
#if ENABLED (JERRY_BUILTIN_REALMS)
ecma_value_t
ecma_global_object_t *
ecma_op_function_get_realm (const ecma_compiled_code_t *bytecode_header_p);
ecma_global_object_t *
@@ -90,8 +90,14 @@ static inline bool JERRY_ATTR_ALWAYS_INLINE
ecma_op_is_global_environment (ecma_object_t *lex_env_p) /**< lexical environment */
{
JERRY_ASSERT (ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
#if ENABLED (JERRY_BUILTIN_REALMS)
JERRY_ASSERT (lex_env_p->u2.outer_reference_cp != JMEM_CP_NULL
|| (ecma_make_object_value (ecma_get_lex_env_binding_object (lex_env_p))
== ((ecma_global_object_t *) ecma_builtin_get_global ())->this_binding));
#else /* !ENABLED (JERRY_BUILTIN_REALMS) */
JERRY_ASSERT (lex_env_p->u2.outer_reference_cp != JMEM_CP_NULL
|| ecma_get_lex_env_binding_object (lex_env_p) == ecma_builtin_get_global ());
#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
return lex_env_p->u2.outer_reference_cp == JMEM_CP_NULL;
} /* ecma_op_is_global_environment */