Skip to content

Commit 0af000c

Browse files
committed
Implement Date constructor.
JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 632618d commit 0af000c

File tree

3 files changed

+167
-5
lines changed

3 files changed

+167
-5
lines changed

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

Lines changed: 98 additions & 3 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-conversion.h"
20+
#include "ecma-gc.h"
2021
#include "ecma-globals.h"
2122
#include "ecma-helpers.h"
2223
#include "ecma-try-catch-macro.h"
@@ -423,7 +424,7 @@ ecma_builtin_date_utc (ecma_value_t this_arg __attr_unused___, /**< this argumen
423424

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

426-
ecma_number_t *time_p = ecma_get_number_from_completion_value (time_value);
427+
ecma_number_t *time_p = ecma_get_number_from_value (time_value);
427428
ecma_number_t *time_clip_p = ecma_alloc_number ();
428429
*time_clip_p = ecma_date_time_clip (*time_p);
429430
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (time_clip_p));
@@ -458,25 +459,119 @@ ecma_builtin_date_now (ecma_value_t this_arg __attr_unused___) /**< this argumen
458459
/**
459460
* Handle calling [[Call]] of built-in Date object
460461
*
462+
* See also:
463+
* ECMA-262 v5, 15.9.2.1
464+
*
461465
* @return completion-value
462466
*/
463467
ecma_completion_value_t
464468
ecma_builtin_date_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
465469
ecma_length_t arguments_list_len) /**< number of arguments */
466470
{
467-
ECMA_BUILTIN_CP_UNIMPLEMENTED (arguments_list_p, arguments_list_len);
471+
/* FIXME:
472+
* Fix this, after Date.prototype.toString is finished.
473+
*/
474+
return ecma_builtin_date_dispatch_construct (arguments_list_p, arguments_list_len);
468475
} /* ecma_builtin_date_dispatch_call */
469476

470477
/**
471478
* Handle calling [[Construct]] of built-in Date object
472479
*
480+
* See also:
481+
* ECMA-262 v5, 15.9.3.1
482+
*
473483
* @return completion-value
474484
*/
475485
ecma_completion_value_t
476486
ecma_builtin_date_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
477487
ecma_length_t arguments_list_len) /**< number of arguments */
478488
{
479-
ECMA_BUILTIN_CP_UNIMPLEMENTED (arguments_list_p, arguments_list_len);
489+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
490+
ecma_number_t *prim_value_num_p = NULL;
491+
492+
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_DATE_PROTOTYPE);
493+
ecma_object_t *obj_p = ecma_create_object (prototype_obj_p,
494+
true,
495+
ECMA_OBJECT_TYPE_GENERAL);
496+
ecma_deref_object (prototype_obj_p);
497+
498+
if (arguments_list_len == 0)
499+
{
500+
ECMA_TRY_CATCH (parse_res_value,
501+
ecma_builtin_date_now (ecma_make_object_value (obj_p)),
502+
ret_value);
503+
504+
prim_value_num_p = ecma_alloc_number ();
505+
*prim_value_num_p = *ecma_get_number_from_value (parse_res_value);
506+
507+
ECMA_FINALIZE (parse_res_value)
508+
}
509+
else if (arguments_list_len == 1)
510+
{
511+
ECMA_TRY_CATCH (prim_comp_value,
512+
ecma_op_to_primitive (arguments_list_p[0], ECMA_PREFERRED_TYPE_NUMBER),
513+
ret_value);
514+
515+
if (ecma_is_value_string (prim_comp_value))
516+
{
517+
ECMA_TRY_CATCH (parse_res_value,
518+
ecma_builtin_date_parse (ecma_make_object_value (obj_p), prim_comp_value),
519+
ret_value);
520+
521+
prim_value_num_p = ecma_alloc_number ();
522+
*prim_value_num_p = *ecma_get_number_from_value (parse_res_value);
523+
524+
ECMA_FINALIZE (parse_res_value);
525+
}
526+
else
527+
{
528+
ECMA_TRY_CATCH (prim_value, ecma_op_to_number (arguments_list_p[0]), ret_value);
529+
530+
prim_value_num_p = ecma_alloc_number ();
531+
*prim_value_num_p = *ecma_get_number_from_value (prim_value);
532+
533+
ECMA_FINALIZE (prim_value);
534+
}
535+
536+
ECMA_FINALIZE (prim_comp_value);
537+
}
538+
else if (arguments_list_len >= 2)
539+
{
540+
ECMA_TRY_CATCH (time_value,
541+
ecma_date_construct_helper (arguments_list_p, arguments_list_len),
542+
ret_value);
543+
544+
ecma_number_t *time_p = ecma_get_number_from_value (time_value);
545+
prim_value_num_p = ecma_alloc_number ();
546+
*prim_value_num_p = ecma_date_time_clip (ecma_date_utc (*time_p));
547+
548+
ECMA_FINALIZE (time_value);
549+
}
550+
else
551+
{
552+
prim_value_num_p = ecma_alloc_number ();
553+
*prim_value_num_p = ecma_number_make_nan ();
554+
}
555+
556+
if (ecma_is_completion_value_empty (ret_value))
557+
{
558+
ecma_property_t *class_prop_p = ecma_create_internal_property (obj_p,
559+
ECMA_INTERNAL_PROPERTY_CLASS);
560+
class_prop_p->u.internal_property.value = LIT_MAGIC_STRING_DATE_UL;
561+
562+
ecma_property_t *prim_value_prop_p = ecma_create_internal_property (obj_p,
563+
ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
564+
ECMA_SET_POINTER (prim_value_prop_p->u.internal_property.value, prim_value_num_p);
565+
566+
ret_value = ecma_make_normal_completion_value (ecma_make_object_value (obj_p));
567+
}
568+
else
569+
{
570+
JERRY_ASSERT (ecma_is_completion_value_throw (ret_value));
571+
ecma_deref_object (obj_p);
572+
}
573+
574+
return ret_value;
480575
} /* ecma_builtin_date_dispatch_construct */
481576

482577
/**

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,12 @@ ecma_date_week_day (ecma_number_t time) /**< time value */
413413
ecma_number_t __attr_always_inline___
414414
ecma_date_local_tza ()
415415
{
416-
JERRY_UNIMPLEMENTED ("The built-in is not implemented.");
416+
/*
417+
* FIXME:
418+
* Get the real system time. ex: localtime_r, gmtime_r, daylight on Linux
419+
* Introduce system macros at first.
420+
*/
421+
return ECMA_NUMBER_ZERO;
417422
} /* ecma_date_local_tza */
418423

419424
/**
@@ -432,7 +437,12 @@ ecma_date_daylight_saving_ta (ecma_number_t time) /**< time value */
432437
return time; /* time is NaN */
433438
}
434439

435-
JERRY_UNIMPLEMENTED ("The built-in is not implemented.");
440+
/*
441+
* FIXME:
442+
* Get the real system time. ex: localtime_r, gmtime_r, daylight on Linux
443+
* Introduce system macros at first.
444+
*/
445+
return ECMA_NUMBER_ZERO;
436446
} /* ecma_date_daylight_saving_ta */
437447

438448
/**

tests/jerry/date-construct.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
assert (Date.length == 7);
17+
assert (Object.prototype.toString.call (Date.prototype) === '[object Date]');
18+
19+
var d;
20+
21+
try
22+
{
23+
d = new Date({toString: function() { throw new Error("foo"); }});
24+
assert (false);
25+
}
26+
catch (e)
27+
{
28+
assert (e instanceof Error);
29+
assert (e.message === "foo");
30+
}
31+
32+
// FIXME: Add assert statements when getters for Date are finished.
33+
d = Date("abcd");
34+
// assert (isNaN(d.valueOf()));
35+
36+
d = Date();
37+
// assert (!isNaN(d.valueOf()));
38+
d = Date("2015-01-01");
39+
// assert (d.valueOf() == 1420070400000);
40+
41+
d = Date(1420070400000);
42+
// assert (d.valueOf() == 1420070400000);
43+
44+
d = Date(2015,0,1,0,0,0,0);
45+
// assert (d.valueOf() == 1420070400000);
46+
47+
d = new Date();
48+
// assert (isNaN(d.valueOf()));
49+
50+
d = new Date("2015-01-01");
51+
// assert (d.valueOf() == 1420070400000);
52+
53+
d = new Date(1420070400000);
54+
// assert (d.valueOf() == 1420070400000);
55+
56+
d = new Date(2015,0,1,0,0,0,0);
57+
// assert (d.valueOf() == 1420070400000);

0 commit comments

Comments
 (0)