Skip to content

Implement Date.prototype.toJSON() #455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions jerry-core/ecma/builtin-objects/ecma-builtin-date-prototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "ecma-alloc.h"
#include "ecma-builtin-helpers.h"
#include "ecma-exceptions.h"
#include "ecma-function-object.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-objects.h"
Expand Down Expand Up @@ -1113,9 +1114,62 @@ ecma_builtin_date_prototype_to_iso_string (ecma_value_t this_arg) /**< this argu
*/
static ecma_completion_value_t
ecma_builtin_date_prototype_to_json (ecma_value_t this_arg, /**< this argument */
ecma_value_t arg) /**< key */
ecma_value_t arg __attr_unused___) /**< key */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();

/* 1. */
ECMA_TRY_CATCH (obj,
ecma_op_to_object (this_arg),
ret_value);

/* 2. */
ECMA_TRY_CATCH (tv,
ecma_op_to_primitive (obj, ECMA_PREFERRED_TYPE_NUMBER),
ret_value);

/* 3. */
if (ecma_is_value_number (tv))
{
ecma_number_t num_value_p = *ecma_get_number_from_value (tv);

if (ecma_number_is_nan (num_value_p) || ecma_number_is_infinity (num_value_p))
{
ret_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_NULL);
}
}

if (ecma_is_completion_value_empty (ret_value))
{
ecma_string_t *to_iso_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_TO_ISO_STRING_UL);
ecma_object_t *value_obj_p = ecma_get_object_from_value (obj);

/* 4. */
ECMA_TRY_CATCH (to_iso,
ecma_op_object_get (value_obj_p, to_iso_str_p),
ret_value);

/* 5. */
if (!ecma_op_is_callable (to_iso))
{
ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
}
/* 6. */
else
{
ecma_object_t *to_iso_obj_p = ecma_get_object_from_value (to_iso);
ret_value = ecma_op_function_call (to_iso_obj_p, this_arg, NULL, 0);
}

ECMA_FINALIZE (to_iso);

ecma_deref_ecma_string (to_iso_str_p);
}

ECMA_FINALIZE (tv);
ECMA_FINALIZE (obj);

return ret_value;
} /* ecma_builtin_date_prototype_to_json */

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/jerry/date-tostring.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,20 @@ catch (e)
assert (e instanceof TypeError);
assert (e.message === "Incompatible type");
}

assert (new Date (NaN).toJSON () == null);
assert (new Date ("2015-07-16").toJSON () == "2015-07-16T00:00:00.000Z");
assert (new Date ("2015-07-16T11:29:05.023").toJSON () == "2015-07-16T11:29:05.023Z");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add a test to see if the toJSON returns a string which can be processed by the Date constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a plus test case.

try
{
Date.prototype.toJSON.call(-1);
assert (false);
}
catch (e)
{
assert (e instanceof TypeError);
}

date_time = new Date ("2015-07-08T11:29:05.023").toJSON ();
assert (new Date (date_time) == "2015-07-08T11:29:05.023");