Implement Object.setPrototypeOf from ES2015 specification (#1666)
`Object.prototype.__proto__` has been implemented by most JS engines to give R/W access to prototype chains, well before it made it into the standard. JerryScript has decided not to implement it, exactly because it was not part of ES 5.1. The only fully ES5.1-compatible way of accessing the prototype chain is `Object.getPrototypeOf` for reading. However, ES2015 defines `Object.setPrototypeOf` for rewriting the prototype chain, and JerryScript has now an ES2015 subset profile. So, this commit adds its implementation to JerryScript. Note, this commit does _not_ add `Object.prototype.__proto__`, since that is in the Annex B of ES2015 specification, which is optional for non-web-browser hosts. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
@@ -135,6 +135,127 @@ ecma_builtin_object_object_get_prototype_of (ecma_value_t this_arg, /**< 'this'
|
||||
return ret_value;
|
||||
} /* ecma_builtin_object_object_get_prototype_of */
|
||||
|
||||
#ifndef CONFIG_DISABLE_ES2015_BUILTIN
|
||||
/**
|
||||
* [[SetPrototypeOf]]
|
||||
*
|
||||
* See also:
|
||||
* ES2015 9.1.2
|
||||
*/
|
||||
static bool
|
||||
ecma_set_prototype_of (ecma_value_t o_value, /**< O */
|
||||
ecma_value_t v_value) /**< V */
|
||||
{
|
||||
/* 1. */
|
||||
JERRY_ASSERT (ecma_is_value_object (o_value));
|
||||
JERRY_ASSERT (ecma_is_value_object (v_value) || ecma_is_value_null (v_value));
|
||||
|
||||
ecma_object_t *o_p = ecma_get_object_from_value (o_value);
|
||||
ecma_object_t *v_p = ecma_is_value_null (v_value) ? NULL : ecma_get_object_from_value (v_value);
|
||||
|
||||
/* 3., 4. */
|
||||
if (v_p == ecma_get_object_prototype (o_p))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/* 2., 5. */
|
||||
if (!ecma_get_object_extensible (o_p))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 6., 7., 8. */
|
||||
ecma_object_t *p_p = v_p;
|
||||
while (true)
|
||||
{
|
||||
/* a. */
|
||||
if (p_p == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
/* b. */
|
||||
if (p_p == o_p)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/* c.i. TODO: es2015-subset profile does not support having a different
|
||||
* [[GetPrototypeOf]] internal method */
|
||||
|
||||
/* c.ii. */
|
||||
p_p = ecma_get_object_prototype (p_p);
|
||||
}
|
||||
|
||||
/* 9. */
|
||||
ECMA_SET_POINTER (o_p->prototype_or_outer_reference_cp, v_p);
|
||||
|
||||
/* 10. */
|
||||
return true;
|
||||
} /* ecma_set_prototype_of */
|
||||
|
||||
/**
|
||||
* The Object object's 'setPrototypeOf' routine
|
||||
*
|
||||
* See also:
|
||||
* ES2015 19.1.2.18
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
static ecma_value_t
|
||||
ecma_builtin_object_object_set_prototype_of (ecma_value_t this_arg, /**< 'this' argument */
|
||||
ecma_value_t arg1, /**< routine's first argument */
|
||||
ecma_value_t arg2) /**< routine's second argument */
|
||||
{
|
||||
JERRY_UNUSED (this_arg);
|
||||
ecma_value_t ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_EMPTY);
|
||||
|
||||
/* 1., 2. */
|
||||
ECMA_TRY_CATCH (unused_value,
|
||||
ecma_op_check_object_coercible (arg1),
|
||||
ret_value);
|
||||
|
||||
/* 3. */
|
||||
if (!ecma_is_value_object (arg2) && !ecma_is_value_null (arg2))
|
||||
{
|
||||
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("proto is neither Object nor Null."));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 4. */
|
||||
if (!ecma_is_value_object (arg1))
|
||||
{
|
||||
ret_value = ecma_copy_value (arg1);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 5. */
|
||||
bool status = ecma_set_prototype_of (arg1, arg2);
|
||||
|
||||
/* 6. TODO: es2015-subset profile does not support having a different
|
||||
* [[SetPrototypeOf]] internal method */
|
||||
|
||||
/* 7. */
|
||||
if (!status)
|
||||
{
|
||||
ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("cannot set prototype."));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 8. */
|
||||
ret_value = ecma_copy_value (arg1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ECMA_FINALIZE (unused_value);
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_builtin_object_object_set_prototype_of */
|
||||
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
|
||||
|
||||
/**
|
||||
* The Object object's 'getOwnPropertyNames' routine
|
||||
*
|
||||
|
||||
@@ -60,6 +60,10 @@ ROUTINE (LIT_MAGIC_STRING_CREATE, ecma_builtin_object_object_create, 2, 2)
|
||||
ROUTINE (LIT_MAGIC_STRING_DEFINE_PROPERTIES_UL, ecma_builtin_object_object_define_properties, 2, 2)
|
||||
ROUTINE (LIT_MAGIC_STRING_DEFINE_PROPERTY_UL, ecma_builtin_object_object_define_property, 3, 3)
|
||||
|
||||
#ifndef CONFIG_DISABLE_ES2015_BUILTIN
|
||||
ROUTINE (LIT_MAGIC_STRING_SET_PROTOTYPE_OF_UL, ecma_builtin_object_object_set_prototype_of, 2, 2)
|
||||
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
|
||||
|
||||
#undef SIMPLE_VALUE
|
||||
#undef NUMBER_VALUE
|
||||
#undef STRING_VALUE
|
||||
|
||||
@@ -262,6 +262,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_DEFINE_PROPERTY_UL, "defineProperty")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_PROTOTYPE_OF_UL, "getPrototypeOf")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_UTC_FULL_YEAR_UL, "getUTCFullYear")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_HAS_OWN_PROPERTY_UL, "hasOwnProperty")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_PROTOTYPE_OF_UL, "setPrototypeOf")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UTC_FULL_YEAR_UL, "setUTCFullYear")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_LOCALE_STRING_UL, "toLocaleString")
|
||||
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (15, LIT_MAGIC_STRING_GET_MILLISECONDS_UL)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
CONFIG_DISABLE_ARRAYBUFFER_BUILTIN
|
||||
CONFIG_DISABLE_ES2015_BUILTIN
|
||||
CONFIG_DISABLE_TYPEDARRAY_BUILTIN
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ CONFIG_DISABLE_ARRAY_BUILTIN
|
||||
CONFIG_DISABLE_BOOLEAN_BUILTIN
|
||||
CONFIG_DISABLE_DATE_BUILTIN
|
||||
CONFIG_DISABLE_ERROR_BUILTINS
|
||||
CONFIG_DISABLE_ES2015_BUILTIN
|
||||
CONFIG_DISABLE_JSON_BUILTIN
|
||||
CONFIG_DISABLE_MATH_BUILTIN
|
||||
CONFIG_DISABLE_NUMBER_BUILTIN
|
||||
@@ -11,4 +12,3 @@ CONFIG_DISABLE_REGEXP_BUILTIN
|
||||
CONFIG_DISABLE_STRING_BUILTIN
|
||||
CONFIG_DISABLE_TYPEDARRAY_BUILTIN
|
||||
CONFIG_DISABLE_UNICODE_CASE_CONVERSION
|
||||
|
||||
|
||||
Reference in New Issue
Block a user