Skip to content

Commit c2e2472

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

File tree

3 files changed

+141
-8
lines changed

3 files changed

+141
-8
lines changed

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

Lines changed: 100 additions & 6 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"
@@ -324,25 +325,118 @@ ecma_builtin_date_now (ecma_value_t this_arg) /**< this argument */
324325
/**
325326
* Handle calling [[Call]] of built-in Date object
326327
*
328+
* See also:
329+
* ECMA-262 v5, 15.9.2.1
330+
*
327331
* @return completion-value
328332
*/
329333
ecma_completion_value_t
330-
ecma_builtin_date_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
331-
ecma_length_t arguments_list_len) /**< number of arguments */
334+
ecma_builtin_date_dispatch_call (const ecma_value_t *args_list_p, /**< arguments list */
335+
ecma_length_t args_len) /**< number of arguments */
332336
{
333-
ECMA_BUILTIN_CP_UNIMPLEMENTED (arguments_list_p, arguments_list_len);
337+
/* FIXME:
338+
* Fix this, after Date.prototype.toString is finished.
339+
*/
340+
return ecma_builtin_date_dispatch_construct (args_list_p, args_len);
334341
} /* ecma_builtin_date_dispatch_call */
335342

336343
/**
337344
* Handle calling [[Construct]] of built-in Date object
338345
*
346+
* See also:
347+
* ECMA-262 v5, 15.9.3.1
348+
*
339349
* @return completion-value
340350
*/
341351
ecma_completion_value_t
342-
ecma_builtin_date_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
343-
ecma_length_t arguments_list_len) /**< number of arguments */
352+
ecma_builtin_date_dispatch_construct (const ecma_value_t *args_list_p, /**< arguments list */
353+
ecma_length_t args_len) /**< number of arguments */
344354
{
345-
ECMA_BUILTIN_CP_UNIMPLEMENTED (arguments_list_p, arguments_list_len);
355+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
356+
ecma_number_t *prim_value_num_p;
357+
358+
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_DATE_PROTOTYPE);
359+
ecma_object_t *obj_p = ecma_create_object (prototype_obj_p,
360+
true,
361+
ECMA_OBJECT_TYPE_GENERAL);
362+
ecma_deref_object (prototype_obj_p);
363+
364+
if (args_len == 0)
365+
{
366+
ECMA_TRY_CATCH (parse_res_value,
367+
ecma_builtin_date_now (ecma_make_object_value (obj_p)),
368+
ret_value);
369+
370+
prim_value_num_p= ecma_alloc_number ();
371+
*prim_value_num_p = *ecma_get_number_from_completion_value (parse_res_value);
372+
373+
ECMA_FINALIZE (parse_res_value)
374+
}
375+
else if (args_len == 1)
376+
{
377+
ECMA_TRY_CATCH (prim_comp_value,
378+
ecma_op_to_primitive (args_list_p[0], ECMA_PREFERRED_TYPE_NUMBER),
379+
ret_value);
380+
381+
if (ecma_is_value_string (prim_comp_value))
382+
{
383+
ECMA_TRY_CATCH (parse_res_value,
384+
ecma_builtin_date_parse (ecma_make_object_value (obj_p), prim_comp_value),
385+
ret_value);
386+
387+
prim_value_num_p= ecma_alloc_number ();
388+
*prim_value_num_p = *ecma_get_number_from_completion_value (parse_res_value);
389+
390+
ECMA_FINALIZE (parse_res_value);
391+
}
392+
else
393+
{
394+
ECMA_TRY_CATCH (prim_value, ecma_op_to_number (args_list_p[0]), ret_value);
395+
396+
prim_value_num_p= ecma_alloc_number ();
397+
*prim_value_num_p = *ecma_get_number_from_value (prim_value);
398+
399+
ECMA_FINALIZE (prim_value);
400+
}
401+
402+
ECMA_FINALIZE (prim_comp_value);
403+
}
404+
else if (args_len >= 2)
405+
{
406+
ECMA_TRY_CATCH (time_value,
407+
ecma_date_construct_helper (args_list_p, args_len),
408+
ret_value);
409+
410+
ecma_number_t *time_p = ecma_get_number_from_completion_value (time_value);
411+
prim_value_num_p= ecma_alloc_number ();
412+
*prim_value_num_p = ecma_date_time_clip (ecma_date_utc (*time_p));
413+
414+
ECMA_FINALIZE (time_value);
415+
}
416+
else
417+
{
418+
prim_value_num_p= ecma_alloc_number ();
419+
*prim_value_num_p = ECMA_NUMBER_ZERO;
420+
}
421+
422+
if (ecma_is_completion_value_empty (ret_value))
423+
{
424+
ecma_property_t *class_prop_p = ecma_create_internal_property (obj_p,
425+
ECMA_INTERNAL_PROPERTY_CLASS);
426+
class_prop_p->u.internal_property.value = LIT_MAGIC_STRING_DATE_UL;
427+
428+
ecma_property_t *prim_value_prop_p = ecma_create_internal_property (obj_p,
429+
ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
430+
ECMA_SET_POINTER (prim_value_prop_p->u.internal_property.value, prim_value_num_p);
431+
432+
ret_value = ecma_make_normal_completion_value (ecma_make_object_value (obj_p));
433+
}
434+
else if (ecma_is_completion_value_throw (ret_value))
435+
{
436+
ecma_deref_object (obj_p);
437+
}
438+
439+
return ret_value;
346440
} /* ecma_builtin_date_dispatch_construct */
347441

348442
/**

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
// FIXME: Add assert statements when getters for Date are finished.
20+
var d;
21+
d = Date();
22+
d = Date("2015-01-01");
23+
d = Date(1420070400000);
24+
d = Date(2015,0,1,0,0,0,0);
25+
26+
d = new Date();
27+
d = new Date("2015-01-01");
28+
d = new Date(1420070400000);
29+
d = new Date(2015,0,1,0,0,0,0);

0 commit comments

Comments
 (0)