Skip to content

Commit ed16f09

Browse files
author
Roland Takacs
committed
Implement Date.prototype.toJSON()
JerryScript-DCO-1.0-Signed-off-by: Roland Takacs rtakacs.u-szeged@partner.samsung.com
1 parent 021fc62 commit ed16f09

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-date-prototype.cpp

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "ecma-alloc.h"
1818
#include "ecma-builtin-helpers.h"
1919
#include "ecma-exceptions.h"
20+
#include "ecma-function-object.h"
2021
#include "ecma-globals.h"
2122
#include "ecma-helpers.h"
2223
#include "ecma-objects.h"
@@ -1113,9 +1114,62 @@ ecma_builtin_date_prototype_to_iso_string (ecma_value_t this_arg) /**< this argu
11131114
*/
11141115
static ecma_completion_value_t
11151116
ecma_builtin_date_prototype_to_json (ecma_value_t this_arg, /**< this argument */
1116-
ecma_value_t arg) /**< key */
1117+
ecma_value_t arg __attr_unused___) /**< key */
11171118
{
1118-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
1119+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
1120+
1121+
/* 1. */
1122+
ECMA_TRY_CATCH (obj,
1123+
ecma_op_to_object (this_arg),
1124+
ret_value);
1125+
1126+
/* 2. */
1127+
ECMA_TRY_CATCH (tv,
1128+
ecma_op_to_primitive (obj, ECMA_PREFERRED_TYPE_NUMBER),
1129+
ret_value);
1130+
1131+
/* 3. */
1132+
if (ecma_is_value_number (tv))
1133+
{
1134+
ecma_number_t num_value_p = *ecma_get_number_from_value (tv);
1135+
1136+
if (ecma_number_is_nan (num_value_p) || ecma_number_is_infinity (num_value_p))
1137+
{
1138+
ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_NULL);
1139+
}
1140+
}
1141+
1142+
if (ecma_is_completion_value_empty (ret_value))
1143+
{
1144+
ecma_string_t *to_iso_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_TO_ISO_STRING_UL);
1145+
ecma_object_t *value_obj_p = ecma_get_object_from_value (obj);
1146+
1147+
/* 4. */
1148+
ECMA_TRY_CATCH (to_iso,
1149+
ecma_op_object_get (value_obj_p, to_iso_str_p),
1150+
ret_value);
1151+
1152+
/* 5. */
1153+
if (!ecma_op_is_callable (to_iso))
1154+
{
1155+
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
1156+
}
1157+
/* 6. */
1158+
else
1159+
{
1160+
ecma_object_t *to_iso_obj_p = ecma_get_object_from_value (to_iso);
1161+
ret_value = ecma_op_function_call (to_iso_obj_p, this_arg, NULL, 0);
1162+
}
1163+
1164+
ECMA_FINALIZE (to_iso);
1165+
1166+
ecma_deref_ecma_string (to_iso_str_p);
1167+
}
1168+
1169+
ECMA_FINALIZE (tv);
1170+
ECMA_FINALIZE (obj);
1171+
1172+
return ret_value;
11191173
} /* ecma_builtin_date_prototype_to_json */
11201174

11211175
/**

tests/jerry/date-tostring.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,20 @@ catch (e)
8888
assert (e instanceof TypeError);
8989
assert (e.message === "Incompatible type");
9090
}
91+
92+
assert (new Date (NaN).toJSON () == null);
93+
assert (new Date ("2015-07-16").toJSON () == "2015-07-16T00:00:00.000Z");
94+
assert (new Date ("2015-07-16T11:29:05.023").toJSON () == "2015-07-16T11:29:05.023Z");
95+
96+
try
97+
{
98+
Date.prototype.toJSON.call(-1);
99+
assert (false);
100+
}
101+
catch (e)
102+
{
103+
assert (e instanceof TypeError);
104+
}
105+
106+
date_time = new Date ("2015-07-08T11:29:05.023").toJSON ();
107+
assert (new Date (date_time) == "2015-07-08T11:29:05.023");

0 commit comments

Comments
 (0)