Added new target support to Boolean, String, Number Object (#4368)

JerryScript-DCO-1.0-Signed-off-by: bence gabor kis kisbg@inf.u-szeged.hu
This commit is contained in:
kisbg
2020-12-17 09:44:59 +01:00
committed by GitHub
parent e2be8f4c79
commit 1937f820e1
9 changed files with 211 additions and 17 deletions
@@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "jcontext.h"
#include "ecma-alloc.h"
#include "ecma-boolean-object.h"
#include "ecma-builtins.h"
@@ -22,6 +23,7 @@
#include "ecma-helpers.h"
#include "ecma-objects.h"
#include "ecma-objects-general.h"
#include "ecma-function-object.h"
/** \addtogroup ecma ECMA
* @{
@@ -42,13 +44,28 @@ ecma_value_t
ecma_op_create_boolean_object (ecma_value_t arg) /**< argument passed to the Boolean constructor */
{
bool boolean_value = ecma_op_to_boolean (arg);
ecma_builtin_id_t proto_id;
#if ENABLED (JERRY_BUILTIN_BOOLEAN)
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE);
proto_id = ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE;
#else /* ENABLED (JERRY_BUILTIN_BOOLEAN) */
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE);
proto_id = ECMA_BUILTIN_ID_OBJECT_PROTOTYPE;
#endif /* !(ENABLED (JERRY_BUILTIN_BOOLEAN) */
ecma_object_t *prototype_obj_p = ecma_builtin_get (proto_id);
#if ENABLED (JERRY_ESNEXT)
ecma_object_t *new_target = JERRY_CONTEXT (current_new_target);
if (new_target)
{
prototype_obj_p = ecma_op_get_prototype_from_constructor (new_target, proto_id);
if (JERRY_UNLIKELY (prototype_obj_p == NULL))
{
return ECMA_VALUE_ERROR;
}
}
#endif /* ENABLED (JERRY_ESNEXT) */
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_CLASS);
@@ -57,6 +74,13 @@ ecma_op_create_boolean_object (ecma_value_t arg) /**< argument passed to the Boo
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_BOOLEAN_UL;
ext_object_p->u.class_prop.u.value = ecma_make_boolean_value (boolean_value);
#if ENABLED (JERRY_ESNEXT)
if (new_target)
{
ecma_deref_object (prototype_obj_p);
}
#endif /* ENABLED (JERRY_ESNEXT) */
return ecma_make_object_value (object_p);
} /* ecma_op_create_boolean_object */
+39 -5
View File
@@ -531,14 +531,22 @@ ecma_value_t
ecma_op_to_object (ecma_value_t value) /**< ecma value */
{
ecma_check_value_type_is_spec_defined (value);
ecma_builtin_id_t proto_id = ECMA_BUILTIN_ID_OBJECT_PROTOTYPE;
uint16_t lit_id;
if (ecma_is_value_number (value))
{
return ecma_op_create_number_object (value);
#if ENABLED (JERRY_BUILTIN_NUMBER)
proto_id = ECMA_BUILTIN_ID_NUMBER_PROTOTYPE;
#endif /* ENABLED (JERRY_BUILTIN_NUMBER) */
lit_id = LIT_MAGIC_STRING_NUMBER_UL;
}
else if (ecma_is_value_string (value))
{
return ecma_op_create_string_object (&value, 1);
#if ENABLED (JERRY_BUILTIN_STRING)
proto_id = ECMA_BUILTIN_ID_STRING_PROTOTYPE;
#endif /* ENABLED (JERRY_BUILTIN_STRING) */
lit_id = LIT_MAGIC_STRING_STRING_UL;
}
else if (ecma_is_value_object (value))
{
@@ -547,7 +555,8 @@ ecma_op_to_object (ecma_value_t value) /**< ecma value */
#if ENABLED (JERRY_ESNEXT)
else if (ecma_is_value_symbol (value))
{
return ecma_op_create_symbol_object (value);
proto_id = ECMA_BUILTIN_ID_SYMBOL_PROTOTYPE;
lit_id = LIT_MAGIC_STRING_SYMBOL_UL;
}
#endif /* ENABLED (JERRY_ESNEXT) */
#if ENABLED (JERRY_BUILTIN_BIGINT)
@@ -566,12 +575,37 @@ ecma_op_to_object (ecma_value_t value) /**< ecma value */
else
{
JERRY_ASSERT (ecma_is_value_boolean (value));
return ecma_op_create_boolean_object (value);
#if ENABLED (JERRY_BUILTIN_BOOLEAN)
proto_id = ECMA_BUILTIN_ID_BOOLEAN_PROTOTYPE;
#endif /* ENABLED (JERRY_BUILTIN_BOOLEAN) */
lit_id = LIT_MAGIC_STRING_BOOLEAN_UL;
}
}
return ecma_op_create_class_object (proto_id, value, lit_id);
} /* ecma_op_to_object */
/**
* Create a ECMA_OBJECT_TYPE_CLASS object from the given arguments.
*
* @return ecma_value - constructed object
*/
ecma_value_t
ecma_op_create_class_object (ecma_builtin_id_t proto_id, /**< prototype id */
ecma_value_t value, /**< ecma value */
uint16_t class_id) /**< magic string id */
{
ecma_object_t *object_p = ecma_create_object (ecma_builtin_get (proto_id),
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_CLASS);
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
ext_object_p->u.class_prop.class_id = class_id;
ext_object_p->u.class_prop.u.value = ecma_copy_value_if_not_object (value);
return ecma_make_object_value (object_p);
} /* ecma_op_create_class_object */
/**
* FromPropertyDescriptor operation.
*
@@ -18,6 +18,7 @@
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-builtins.h"
/** \addtogroup ecma ECMA
* @{
@@ -59,6 +60,9 @@ ecma_string_t *ecma_op_to_string (ecma_value_t value);
ecma_string_t *ecma_op_to_property_key (ecma_value_t value);
ecma_value_t ecma_op_to_object (ecma_value_t value);
bool ecma_op_is_integer (ecma_number_t value);
ecma_value_t ecma_op_create_class_object (ecma_builtin_id_t proto_id,
ecma_value_t value,
uint16_t lit_id);
ecma_value_t ecma_op_to_integer (ecma_value_t value, ecma_number_t *number_p);
ecma_value_t ecma_op_to_length (ecma_value_t value, ecma_length_t *length);
#if ENABLED (JERRY_ESNEXT)
@@ -22,6 +22,8 @@
#include "ecma-number-object.h"
#include "ecma-objects.h"
#include "ecma-objects-general.h"
#include "ecma-function-object.h"
#include "jcontext.h"
/** \addtogroup ecma ECMA
* @{
@@ -50,12 +52,24 @@ ecma_op_create_number_object (ecma_value_t arg) /**< argument passed to the Numb
}
conv_to_num_completion = ecma_make_number_value (num);
ecma_builtin_id_t proto_id;
#if ENABLED (JERRY_BUILTIN_NUMBER)
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_NUMBER_PROTOTYPE);
proto_id = ECMA_BUILTIN_ID_NUMBER_PROTOTYPE;
#else /* ENABLED (JERRY_BUILTIN_NUMBER) */
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE);
proto_id = ECMA_BUILTIN_ID_OBJECT_PROTOTYPE;
#endif /* ENABLED (JERRY_BUILTIN_NUMBER) */
ecma_object_t *prototype_obj_p = ecma_builtin_get (proto_id);
#if ENABLED (JERRY_ESNEXT)
ecma_object_t *new_target = JERRY_CONTEXT (current_new_target);
if (new_target)
{
prototype_obj_p = ecma_op_get_prototype_from_constructor (new_target, proto_id);
if (JERRY_UNLIKELY (prototype_obj_p == NULL))
{
return ECMA_VALUE_ERROR;
}
}
#endif /* ENABLED (JERRY_ESNEXT) */
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_CLASS);
@@ -65,7 +79,12 @@ ecma_op_create_number_object (ecma_value_t arg) /**< argument passed to the Numb
/* Pass reference (no need to free conv_to_num_completion). */
ext_object_p->u.class_prop.u.value = conv_to_num_completion;
#if ENABLED (JERRY_ESNEXT)
if (new_target)
{
ecma_deref_object (prototype_obj_p);
}
#endif /* ENABLED (JERRY_ESNEXT) */
return ecma_make_object_value (object_p);
} /* ecma_op_create_number_object */
@@ -22,6 +22,8 @@
#include "ecma-objects.h"
#include "ecma-objects-general.h"
#include "ecma-string-object.h"
#include "ecma-function-object.h"
#include "jcontext.h"
/** \addtogroup ecma ECMA
* @{
@@ -60,12 +62,24 @@ ecma_op_create_string_object (const ecma_value_t *arguments_list_p, /**< list of
prim_value = ecma_make_string_value (str_p);
}
ecma_builtin_id_t proto_id;
#if ENABLED (JERRY_BUILTIN_STRING)
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_STRING_PROTOTYPE);
proto_id = ECMA_BUILTIN_ID_STRING_PROTOTYPE;
#else /* !ENABLED (JERRY_BUILTIN_STRING) */
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE);
proto_id = ECMA_BUILTIN_ID_OBJECT_PROTOTYPE;
#endif /* ENABLED (JERRY_BUILTIN_STRING) */
ecma_object_t *prototype_obj_p = ecma_builtin_get (proto_id);
#if ENABLED (JERRY_ESNEXT)
ecma_object_t *new_target = JERRY_CONTEXT (current_new_target);
if (new_target)
{
prototype_obj_p = ecma_op_get_prototype_from_constructor (new_target, proto_id);
if (JERRY_UNLIKELY (prototype_obj_p == NULL))
{
return ECMA_VALUE_ERROR;
}
}
#endif /* ENABLED (JERRY_ESNEXT) */
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_CLASS);
@@ -74,6 +88,12 @@ ecma_op_create_string_object (const ecma_value_t *arguments_list_p, /**< list of
ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_STRING_UL;
ext_object_p->u.class_prop.u.value = prim_value;
#if ENABLED (JERRY_ESNEXT)
if (new_target)
{
ecma_deref_object (prototype_obj_p);
}
#endif /* ENABLED (JERRY_ESNEXT) */
return ecma_make_object_value (object_p);
} /* ecma_op_create_string_object */