Refactor to{,UTC,ISO}String methods of the Date object.

JerryScript-DCO-1.0-Signed-off-by: Roland Takacs rtakacs.u-szeged@partner.samsung.com
This commit is contained in:
Roland Takacs 2015-08-03 10:01:29 +02:00
parent 833a5d1a55
commit 6082c32d45
3 changed files with 111 additions and 59 deletions

View File

@ -55,7 +55,27 @@
static ecma_completion_value_t
ecma_builtin_date_prototype_to_string (ecma_value_t this_arg) /**< this argument */
{
return ecma_date_object_to_string (this_arg, ECMA_DATE_LOCAL);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_TRY_CATCH (prim_value,
ecma_date_get_primitive_value (this_arg),
ret_value);
ecma_number_t *prim_num_p = ecma_get_number_from_value (prim_value);
if (ecma_number_is_nan (*prim_num_p))
{
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (magic_str_p));
}
else
{
ret_value = ecma_date_value_to_string (*prim_num_p, ECMA_DATE_LOCAL);
}
ECMA_FINALIZE (prim_value);
return ret_value;
} /* ecma_builtin_date_prototype_to_string */
/**
@ -1101,7 +1121,27 @@ ecma_builtin_date_prototype_set_utc_full_year (ecma_value_t this_arg, /**< this
static ecma_completion_value_t
ecma_builtin_date_prototype_to_utc_string (ecma_value_t this_arg) /**< this argument */
{
return ecma_date_object_to_string (this_arg, ECMA_DATE_UTC);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_TRY_CATCH (prim_value,
ecma_date_get_primitive_value (this_arg),
ret_value);
ecma_number_t *prim_num_p = ecma_get_number_from_value (prim_value);
if (ecma_number_is_nan (*prim_num_p))
{
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (magic_str_p));
}
else
{
ret_value = ecma_date_value_to_string (*prim_num_p, ECMA_DATE_UTC);
}
ECMA_FINALIZE (prim_value);
return ret_value;
} /* ecma_builtin_date_prototype_to_utc_string */
/**
@ -1116,7 +1156,27 @@ ecma_builtin_date_prototype_to_utc_string (ecma_value_t this_arg) /**< this argu
static ecma_completion_value_t
ecma_builtin_date_prototype_to_iso_string (ecma_value_t this_arg) /**< this argument */
{
return ecma_date_object_to_string (this_arg, ECMA_DATE_UTC);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ECMA_TRY_CATCH (prim_value,
ecma_date_get_primitive_value (this_arg),
ret_value);
ecma_number_t *prim_num_p = ecma_get_number_from_value (prim_value);
if (ecma_number_is_nan (*prim_num_p))
{
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (magic_str_p));
}
else
{
ret_value = ecma_date_value_to_string (*prim_num_p, ECMA_DATE_UTC);
}
ECMA_FINALIZE (prim_value);
return ret_value;
} /* ecma_builtin_date_prototype_to_iso_string */
/**

View File

@ -917,7 +917,9 @@ ecma_date_insert_num_with_sep (ecma_string_t **str_p, /**< input/output string *
*
* Used by:
* - The Date routine.
* - The ecma_date_object_to_string helper routine.
* - The Date.prototype.toString routine.
* - The Date.prototype.toISOString routine.
* - The Date.prototype.toUTCString routine.
*
* @return completion value
* Returned value must be freed with ecma_free_completion_value.
@ -926,63 +928,51 @@ ecma_completion_value_t
ecma_date_value_to_string (ecma_number_t datetime_num, /**<datetime */
ecma_date_timezone_t timezone) /**< timezone */
{
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ecma_string_t *output_str_p;
ecma_number_t milliseconds;
if (ecma_number_is_nan (datetime_num))
if (timezone == ECMA_DATE_LOCAL)
{
ecma_string_t *magic_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_INVALID_DATE_UL);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (magic_str_p));
datetime_num = ecma_date_local_time (datetime_num);
milliseconds = ecma_date_ms_from_time (datetime_num);
output_str_p = ecma_new_ecma_string_from_number (milliseconds);
ecma_date_insert_leading_zeros (&output_str_p, milliseconds, 3);
}
else
else /* ECMA_DATE_UTC */
{
ecma_string_t *output_str_p;
ecma_number_t milliseconds;
if (timezone == ECMA_DATE_LOCAL)
{
datetime_num = ecma_date_local_time (datetime_num);
milliseconds = ecma_date_ms_from_time (datetime_num);
output_str_p = ecma_new_ecma_string_from_number (milliseconds);
ecma_date_insert_leading_zeros (&output_str_p, milliseconds, 3);
}
else /* ECMA_DATE_UTC */
{
milliseconds = ecma_date_ms_from_time (datetime_num);
output_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
ecma_date_insert_num_with_sep (&output_str_p, milliseconds, LIT_MAGIC_STRING_Z_CHAR, 3);
}
ecma_number_t seconds = ecma_date_sec_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, seconds, LIT_MAGIC_STRING_DOT_CHAR, 2);
ecma_number_t minutes = ecma_date_min_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, minutes, LIT_MAGIC_STRING_COLON_CHAR, 2);
ecma_number_t hours = ecma_date_hour_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, hours, LIT_MAGIC_STRING_COLON_CHAR, 2);
ecma_number_t day = ecma_date_date_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, day, LIT_MAGIC_STRING_TIME_SEP_U, 2);
/*
* Note:
* 'ecma_date_month_from_time' (ECMA 262 v5, 15.9.1.4) returns a number from 0 to 11,
* but we have to print the month from 1 to 12 for ISO 8601 standard (ECMA 262 v5, 15.9.1.15).
*/
ecma_number_t month = ecma_date_month_from_time (datetime_num) + 1;
ecma_date_insert_num_with_sep (&output_str_p, month, LIT_MAGIC_STRING_MINUS_CHAR, 2);
ecma_number_t year = ecma_date_year_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, year, LIT_MAGIC_STRING_MINUS_CHAR, 4);
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (output_str_p));
milliseconds = ecma_date_ms_from_time (datetime_num);
output_str_p = ecma_get_magic_string (LIT_MAGIC_STRING__EMPTY);
ecma_date_insert_num_with_sep (&output_str_p, milliseconds, LIT_MAGIC_STRING_Z_CHAR, 3);
}
return ret_value;
ecma_number_t seconds = ecma_date_sec_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, seconds, LIT_MAGIC_STRING_DOT_CHAR, 2);
ecma_number_t minutes = ecma_date_min_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, minutes, LIT_MAGIC_STRING_COLON_CHAR, 2);
ecma_number_t hours = ecma_date_hour_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, hours, LIT_MAGIC_STRING_COLON_CHAR, 2);
ecma_number_t day = ecma_date_date_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, day, LIT_MAGIC_STRING_TIME_SEP_U, 2);
/*
* Note:
* 'ecma_date_month_from_time' (ECMA 262 v5, 15.9.1.4) returns a number from 0 to 11,
* but we have to print the month from 1 to 12 for ISO 8601 standard (ECMA 262 v5, 15.9.1.15).
*/
ecma_number_t month = ecma_date_month_from_time (datetime_num) + 1;
ecma_date_insert_num_with_sep (&output_str_p, month, LIT_MAGIC_STRING_MINUS_CHAR, 2);
ecma_number_t year = ecma_date_year_from_time (datetime_num);
ecma_date_insert_num_with_sep (&output_str_p, year, LIT_MAGIC_STRING_MINUS_CHAR, 4);
return ecma_make_normal_completion_value (ecma_make_string_value (output_str_p));
} /* ecma_date_value_to_string */
/**
* Common function to create a time zone specific string for objects.
* Common function to get the primitive value of the Date object.
*
* Used by:
* - The Date.prototype.toString routine.
@ -993,8 +983,7 @@ ecma_date_value_to_string (ecma_number_t datetime_num, /**<datetime */
* Returned value must be freed with ecma_free_completion_value.
*/
ecma_completion_value_t
ecma_date_object_to_string (ecma_value_t this_arg, /**< this argument */
ecma_date_timezone_t timezone) /**< timezone */
ecma_date_get_primitive_value (ecma_value_t this_arg) /**< this argument */
{
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
@ -1008,13 +997,16 @@ ecma_date_object_to_string (ecma_value_t this_arg, /**< this argument */
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p,
ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
ecma_number_t *prim_value_num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t,
prim_value_prop_p->u.internal_property.value);
ret_value = ecma_date_value_to_string (*prim_value_num_p, timezone);
JERRY_ASSERT (prim_value_prop_p != NULL);
ecma_number_t *prim_value_num_p = ecma_alloc_number ();
*prim_value_num_p = *ECMA_GET_NON_NULL_POINTER (ecma_number_t,
prim_value_prop_p->u.internal_property.value);
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (prim_value_num_p));
}
return ret_value;
} /* ecma_date_object_to_string */
} /* ecma_date_get_primitive_value */
/**
* @}

View File

@ -122,7 +122,7 @@ extern void ecma_date_insert_num_with_sep (ecma_string_t **str_p,
uint32_t length);
extern ecma_completion_value_t ecma_date_value_to_string (ecma_number_t datetime_num, ecma_date_timezone_t timezone);
extern ecma_completion_value_t ecma_date_object_to_string (ecma_value_t this_arg, ecma_date_timezone_t timezone);
extern ecma_completion_value_t ecma_date_get_primitive_value (ecma_value_t this_arg);
#endif /* !CONFIG_ECMA_COMPACT_PROFILE_DISABLE_DATE_BUILTIN */