Implement toPrimitive method and Date.prototype.toPrimitve (#3287)

The algorithms are based on ECMA-262 v6 7.1.1 and 20.3.4.45

JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
Szilagyi Adam
2019-11-20 11:51:02 +01:00
committed by Robert Fancsik
parent c31452c138
commit a0a71da025
9 changed files with 243 additions and 18 deletions
@@ -22,6 +22,7 @@
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-objects.h"
#include "ecma-objects-general.h"
#include "ecma-try-catch-macro.h"
#if ENABLED (JERRY_BUILTIN_DATE)
@@ -98,6 +99,10 @@ enum
ECMA_DATE_PROTOTYPE_GET_TIME, /* ECMA-262 v5, 15.9.5.9 */
ECMA_DATE_PROTOTYPE_SET_TIME, /* ECMA-262 v5, 15.9.5.27 */
ECMA_DATE_PROTOTYPE_TO_JSON, /* ECMA-262 v5, 15.9.5.44 */
#if ENABLED (JERRY_ES2015)
ECMA_DATE_PROTOTYPE_TO_PRIMITIVE, /* ECMA-262 v6 20.3.4.45 */
#endif /* ENABLED (JERRY_ES2015) */
};
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-date-prototype.inc.h"
@@ -179,6 +184,46 @@ ecma_builtin_date_prototype_to_json (ecma_value_t this_arg) /**< this argument *
return ret_value;
} /* ecma_builtin_date_prototype_to_json */
#if ENABLED (JERRY_ES2015)
/**
* The Date.prototype object's toPrimitive routine
*
* See also:
* ECMA-262 v6, 20.3.4.45
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
static ecma_value_t
ecma_builtin_date_prototype_to_primitive (ecma_value_t this_arg, /**< this argument */
ecma_value_t hint_arg) /**< {"default", "number", "string"} */
{
if (ecma_is_value_object (this_arg) && ecma_is_value_string (hint_arg))
{
ecma_string_t *hint_str_p = ecma_get_string_from_value (hint_arg);
ecma_preferred_type_hint_t hint = ECMA_PREFERRED_TYPE_NUMBER;
if (hint_str_p == ecma_get_magic_string (LIT_MAGIC_STRING_STRING)
|| hint_str_p == ecma_get_magic_string (LIT_MAGIC_STRING_DEFAULT))
{
hint = ECMA_PREFERRED_TYPE_STRING;
}
else if (hint_str_p == ecma_get_magic_string (LIT_MAGIC_STRING_NUMBER))
{
hint = ECMA_PREFERRED_TYPE_NUMBER;
}
if (hint != ECMA_PREFERRED_TYPE_NO)
{
return ecma_op_general_object_ordinary_value (ecma_get_object_from_value (this_arg), hint);
}
}
return ecma_raise_type_error (ECMA_ERR_MSG ("Invalid argument type in toPrimitive."));
} /* ecma_builtin_date_prototype_to_primitive */
#endif /* ENABLED (JERRY_ES2015) */
/**
* Dispatch get date functions
*
@@ -560,6 +605,14 @@ ecma_builtin_date_prototype_dispatch_routine (uint16_t builtin_routine_id, /**<
return ecma_builtin_date_prototype_to_json (this_arg);
}
#if ENABLED (JERRY_ES2015)
if (JERRY_UNLIKELY (builtin_routine_id == ECMA_DATE_PROTOTYPE_TO_PRIMITIVE))
{
ecma_value_t argument = arguments_number > 0 ? arguments_list[0] : ECMA_VALUE_UNDEFINED;
return ecma_builtin_date_prototype_to_primitive (this_arg, argument);
}
#endif /* ENABLED (JERRY_ES2015) */
if (!ecma_is_value_object (this_arg)
|| !ecma_object_class_is (ecma_get_object_from_value (this_arg), LIT_MAGIC_STRING_DATE_UL))
{
@@ -68,6 +68,9 @@ ROUTINE (LIT_MAGIC_STRING_SET_UTC_FULL_YEAR_UL, ECMA_DATE_PROTOTYPE_SET_UTC_FULL
ROUTINE (LIT_MAGIC_STRING_TO_UTC_STRING_UL, ECMA_DATE_PROTOTYPE_TO_UTC_STRING, 0, 0)
ROUTINE (LIT_MAGIC_STRING_TO_ISO_STRING_UL, ECMA_DATE_PROTOTYPE_TO_ISO_STRING, 0, 0)
ROUTINE (LIT_MAGIC_STRING_TO_JSON_UL, ECMA_DATE_PROTOTYPE_TO_JSON, 1, 1)
#if ENABLED (JERRY_ES2015)
ROUTINE_CONFIGURABLE_ONLY (LIT_GLOBAL_SYMBOL_TO_PRIMITIVE, ECMA_DATE_PROTOTYPE_TO_PRIMITIVE, 1, 1)
#endif /* ENABLED (JERRY_ES2015) */
#if ENABLED (JERRY_BUILTIN_ANNEXB)