Skip to content

Implement Date constructor. #322

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
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
101 changes: 98 additions & 3 deletions jerry-core/ecma/builtin-objects/ecma-builtin-date.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-conversion.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-try-catch-macro.h"
Expand Down Expand Up @@ -423,7 +424,7 @@ ecma_builtin_date_utc (ecma_value_t this_arg __attr_unused___, /**< this argumen

ECMA_TRY_CATCH (time_value, ecma_date_construct_helper (args, args_number), ret_value);

ecma_number_t *time_p = ecma_get_number_from_completion_value (time_value);
ecma_number_t *time_p = ecma_get_number_from_value (time_value);
ecma_number_t *time_clip_p = ecma_alloc_number ();
*time_clip_p = ecma_date_time_clip (*time_p);
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (time_clip_p));
Expand Down Expand Up @@ -458,25 +459,119 @@ ecma_builtin_date_now (ecma_value_t this_arg __attr_unused___) /**< this argumen
/**
* Handle calling [[Call]] of built-in Date object
*
* See also:
* ECMA-262 v5, 15.9.2.1
*
* @return completion-value
*/
ecma_completion_value_t
ecma_builtin_date_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (arguments_list_p, arguments_list_len);
/* FIXME:
* Fix this, after Date.prototype.toString is finished.
*/
return ecma_builtin_date_dispatch_construct (arguments_list_p, arguments_list_len);
} /* ecma_builtin_date_dispatch_call */

/**
* Handle calling [[Construct]] of built-in Date object
*
* See also:
* ECMA-262 v5, 15.9.3.1
*
* @return completion-value
*/
ecma_completion_value_t
ecma_builtin_date_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
ECMA_BUILTIN_CP_UNIMPLEMENTED (arguments_list_p, arguments_list_len);
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
ecma_number_t *prim_value_num_p = NULL;

ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_DATE_PROTOTYPE);
ecma_object_t *obj_p = ecma_create_object (prototype_obj_p,
true,
ECMA_OBJECT_TYPE_GENERAL);
ecma_deref_object (prototype_obj_p);

if (arguments_list_len == 0)
{
ECMA_TRY_CATCH (parse_res_value,
ecma_builtin_date_now (ecma_make_object_value (obj_p)),
ret_value);

prim_value_num_p = ecma_alloc_number ();
*prim_value_num_p = *ecma_get_number_from_value (parse_res_value);

ECMA_FINALIZE (parse_res_value)
}
else if (arguments_list_len == 1)
{
ECMA_TRY_CATCH (prim_comp_value,
ecma_op_to_primitive (arguments_list_p[0], ECMA_PREFERRED_TYPE_NUMBER),
ret_value);

if (ecma_is_value_string (prim_comp_value))
{
ECMA_TRY_CATCH (parse_res_value,
ecma_builtin_date_parse (ecma_make_object_value (obj_p), prim_comp_value),
ret_value);

prim_value_num_p = ecma_alloc_number ();
*prim_value_num_p = *ecma_get_number_from_value (parse_res_value);

ECMA_FINALIZE (parse_res_value);
}
else
{
ECMA_TRY_CATCH (prim_value, ecma_op_to_number (arguments_list_p[0]), ret_value);

prim_value_num_p = ecma_alloc_number ();
*prim_value_num_p = *ecma_get_number_from_value (prim_value);

ECMA_FINALIZE (prim_value);
}

ECMA_FINALIZE (prim_comp_value);
}
else if (arguments_list_len >= 2)
{
ECMA_TRY_CATCH (time_value,
ecma_date_construct_helper (arguments_list_p, arguments_list_len),
ret_value);

ecma_number_t *time_p = ecma_get_number_from_value (time_value);
prim_value_num_p = ecma_alloc_number ();
*prim_value_num_p = ecma_date_time_clip (ecma_date_utc (*time_p));

ECMA_FINALIZE (time_value);
}
else
{
prim_value_num_p = ecma_alloc_number ();
*prim_value_num_p = ecma_number_make_nan ();
}

if (ecma_is_completion_value_empty (ret_value))
{
ecma_property_t *class_prop_p = ecma_create_internal_property (obj_p,
ECMA_INTERNAL_PROPERTY_CLASS);
class_prop_p->u.internal_property.value = LIT_MAGIC_STRING_DATE_UL;

ecma_property_t *prim_value_prop_p = ecma_create_internal_property (obj_p,
ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
ECMA_SET_POINTER (prim_value_prop_p->u.internal_property.value, prim_value_num_p);

Copy link
Contributor

Choose a reason for hiding this comment

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

We can use ECMA_SET_NON_NULL_POINTER in the case, as prim_value_num_p is not NULL.

ret_value = ecma_make_normal_completion_value (ecma_make_object_value (obj_p));
}
else
{
JERRY_ASSERT (ecma_is_completion_value_throw (ret_value));
ecma_deref_object (obj_p);
}

return ret_value;
} /* ecma_builtin_date_dispatch_construct */

/**
Expand Down
14 changes: 12 additions & 2 deletions jerry-core/ecma/builtin-objects/ecma-builtin-helpers-date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,12 @@ ecma_date_week_day (ecma_number_t time) /**< time value */
ecma_number_t __attr_always_inline___
ecma_date_local_tza ()
{
JERRY_UNIMPLEMENTED ("The built-in is not implemented.");
/*
* FIXME:
* Get the real system time. ex: localtime_r, gmtime_r, daylight on Linux
* Introduce system macros at first.
*/
return ECMA_NUMBER_ZERO;
} /* ecma_date_local_tza */

/**
Expand All @@ -432,7 +437,12 @@ ecma_date_daylight_saving_ta (ecma_number_t time) /**< time value */
return time; /* time is NaN */
}

JERRY_UNIMPLEMENTED ("The built-in is not implemented.");
/*
* FIXME:
* Get the real system time. ex: localtime_r, gmtime_r, daylight on Linux
* Introduce system macros at first.
*/
return ECMA_NUMBER_ZERO;
} /* ecma_date_daylight_saving_ta */

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/jerry/date-construct.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

assert (Date.length == 7);
assert (Object.prototype.toString.call (Date.prototype) === '[object Date]');

Copy link
Contributor

Choose a reason for hiding this comment

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

Could we also have at least one test case which throws an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@galpeter, can you give me an example for what kind of test do you want?

Copy link
Contributor

Choose a reason for hiding this comment

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

@LaszloLango, maybe something like this:

d = new Date("asasd");
d = new Date({toString: function() { throw new Error("foo"); }});

and of course check if it is a valid value/error was thrown

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@galpeter, what will be the correct error in the first case? It's not throwing an error with the current implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@galpeter, updated the tests, please check. In the first case the primitive value of Date will be NaN, but we cannot test it because the missing of Date.prototype.valueOf() function.

var d;

try
{
d = new Date({toString: function() { throw new Error("foo"); }});
assert (false);
}
catch (e)
{
assert (e instanceof Error);
assert (e.message === "foo");
}

// FIXME: Add assert statements when getters for Date are finished.
d = Date("abcd");
// assert (isNaN(d.valueOf()));

d = Date();
// assert (!isNaN(d.valueOf()));
d = Date("2015-01-01");
// assert (d.valueOf() == 1420070400000);

d = Date(1420070400000);
// assert (d.valueOf() == 1420070400000);

d = Date(2015,0,1,0,0,0,0);
// assert (d.valueOf() == 1420070400000);

d = new Date();
// assert (isNaN(d.valueOf()));

d = new Date("2015-01-01");
// assert (d.valueOf() == 1420070400000);

d = new Date(1420070400000);
// assert (d.valueOf() == 1420070400000);

d = new Date(2015,0,1,0,0,0,0);
// assert (d.valueOf() == 1420070400000);