Skip to content

Commit 5a09ff2

Browse files
committed
Implement the AnnexB Date part
JerryScript-DCO-1.0-Signed-off-by: Kristof Kosztyo kkosztyo.u-szeged@partner.samsung.com
1 parent a26c454 commit 5a09ff2

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,105 @@ ecma_builtin_date_prototype_to_json (ecma_value_t this_arg, /**< this argument *
12191219
return ret_value;
12201220
} /* ecma_builtin_date_prototype_to_json */
12211221

1222+
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN
1223+
1224+
/**
1225+
* The Date.prototype object's 'getYear' routine
1226+
*
1227+
* See also:
1228+
* ECMA-262 v5, AnnexB.B.2.4
1229+
*
1230+
* @return completion value
1231+
* Returned value must be freed with ecma_free_completion_value.
1232+
*/
1233+
static ecma_completion_value_t
1234+
ecma_builtin_date_prototype_get_year (ecma_value_t this_arg) /**< this argument */
1235+
{
1236+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
1237+
1238+
/* 1. */
1239+
ECMA_TRY_CATCH (value, ecma_builtin_date_prototype_get_time (this_arg), ret_value);
1240+
ecma_number_t *this_num_p = ecma_get_number_from_value (value);
1241+
/* 2. */
1242+
if (ecma_number_is_nan (*this_num_p))
1243+
{
1244+
ecma_string_t *nan_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_NAN);
1245+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (nan_str_p));
1246+
}
1247+
else
1248+
{
1249+
/* 3. */
1250+
ecma_number_t *ret_num_p = ecma_alloc_number ();
1251+
*ret_num_p = ecma_date_year_from_time (ecma_date_local_time (*this_num_p)) - 1900;
1252+
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (ret_num_p));
1253+
}
1254+
ECMA_FINALIZE (value);
1255+
1256+
return ret_value;
1257+
} /* ecma_builtin_date_prototype_get_year */
1258+
1259+
/**
1260+
* The Date.prototype object's 'setYear' routine
1261+
*
1262+
* See also:
1263+
* ECMA-262 v5, AnnexB.B.2.5
1264+
*
1265+
* @return completion value
1266+
* Returned value must be freed with ecma_free_completion_value.
1267+
*/
1268+
static ecma_completion_value_t
1269+
ecma_builtin_date_prototype_set_year (ecma_value_t this_arg, /**< this argument */
1270+
ecma_value_t year) /**< year argument */
1271+
{
1272+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
1273+
1274+
/* 1. */
1275+
ECMA_TRY_CATCH (this_time_value, ecma_builtin_date_prototype_get_time (this_arg), ret_value);
1276+
ecma_number_t t = ecma_date_local_time (*ecma_get_number_from_value (this_time_value));
1277+
if (ecma_number_is_nan (t))
1278+
{
1279+
t = ECMA_NUMBER_ZERO;
1280+
}
1281+
1282+
/* 2. */
1283+
ecma_number_t y = ecma_number_make_nan ();
1284+
1285+
ECMA_OP_TO_NUMBER_TRY_CATCH (year_value, year, ret_value);
1286+
y = year_value;
1287+
1288+
/* 3. */
1289+
if (ecma_number_is_nan (y))
1290+
{
1291+
ret_value = ecma_date_set_internal_property (this_arg, 0, y, ECMA_DATE_UTC);
1292+
}
1293+
else
1294+
{
1295+
/* 4. */
1296+
if (y >= 0 && y <= 99)
1297+
{
1298+
y += 1900;
1299+
}
1300+
}
1301+
1302+
ECMA_OP_TO_NUMBER_FINALIZE (year_value);
1303+
1304+
if (ecma_is_completion_value_empty (ret_value))
1305+
{
1306+
/* 5-8. */
1307+
ecma_number_t m = ecma_date_month_from_time (t);
1308+
ecma_number_t dt = ecma_date_date_from_time (t);
1309+
ret_value = ecma_date_set_internal_property (this_arg,
1310+
ecma_date_make_day (y, m, dt),
1311+
ecma_date_time_within_day (t),
1312+
ECMA_DATE_UTC);
1313+
}
1314+
ECMA_FINALIZE (this_time_value);
1315+
1316+
return ret_value;
1317+
} /* ecma_builtin_date_prototype_set_year */
1318+
1319+
#endif /* CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN */
1320+
12221321
/**
12231322
* @}
12241323
* @}

jerry-core/ecma/builtin-objects/ecma-builtin-date-prototype.inc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ ROUTINE (LIT_MAGIC_STRING_TO_UTC_STRING_UL, ecma_builtin_date_prototype_to_utc_s
8383
ROUTINE (LIT_MAGIC_STRING_TO_ISO_STRING_UL, ecma_builtin_date_prototype_to_iso_string, 0, 0)
8484
ROUTINE (LIT_MAGIC_STRING_TO_JSON_UL, ecma_builtin_date_prototype_to_json, 1, 1)
8585

86+
#ifndef CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN
87+
88+
ROUTINE (LIT_MAGIC_STRING_GET_YEAR_UL, ecma_builtin_date_prototype_get_year, 0, 0)
89+
ROUTINE (LIT_MAGIC_STRING_SET_YEAR_UL, ecma_builtin_date_prototype_set_year, 1, 1)
90+
ROUTINE (LIT_MAGIC_STRING_TO_GMT_STRING_UL, ecma_builtin_date_prototype_to_utc_string, 0, 0)
91+
92+
#endif /* CONFIG_ECMA_COMPACT_PROFILE_DISABLE_ANNEXB_BUILTIN */
93+
8694
#undef OBJECT_ID
8795
#undef SIMPLE_VALUE
8896
#undef NUMBER_VALUE

jerry-core/lit/lit-magic-strings.inc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_TIME_UL, "getTime")
171171
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_FULL_YEAR_UL, "getFullYear")
172172
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_UTC_U, "UTC")
173173
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_UTC_FULL_YEAR_UL, "getUTCFullYear")
174+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_YEAR_UL, "getYear")
174175
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_MONTH_UL, "getMonth")
175176
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_UTC_MONTH_UL, "getUTCMonth")
176177
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_GET_DATE_UL, "getDate")
@@ -201,8 +202,10 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_MONTH_UL, "setMonth")
201202
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UTC_MONTH_UL, "setUTCMonth")
202203
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_FULL_YEAR_UL, "setFullYear")
203204
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UTC_FULL_YEAR_UL, "setUTCFullYear")
205+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_YEAR_UL, "setYear")
204206
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_UTC_STRING_UL, "toUTCString")
205207
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_ISO_STRING_UL, "toISOString")
208+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_GMT_STRING_UL, "toGMTString")
206209
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_JSON_UL, "toJSON")
207210
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MAX_VALUE_U, "MAX_VALUE")
208211
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_MIN_VALUE_U, "MIN_VALUE")

tests/jerry/date-annexb.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
var d = new Date(1999, 1, 1);
17+
assert (d.getYear() === 99);
18+
d = new Date(1874, 4, 9);
19+
assert (d.getYear() === -26);
20+
d = new Date(2015, 8, 17);
21+
assert (d.getYear() === 115);
22+
d = new Date(NaN);
23+
assert (isNaN (d.getYear()));
24+
25+
var d = new Date();
26+
d.setYear(91);
27+
assert (d.getFullYear() === 1991 && d.getYear() === 91);
28+
29+
d = new Date();
30+
d.setYear(NaN);
31+
assert (isNaN(d.valueOf()));
32+
33+
d = new Date();
34+
d.setYear(2015);
35+
assert (d.getFullYear() === 2015);
36+
37+
d = new Date(2000, 1, 29);
38+
d.setYear(2004);
39+
assert (d.getFullYear() === 2004 && d.getMonth() === 1 && d.getDate() === 29);
40+
d.setYear(2015);
41+
assert (d.getFullYear() === 2015 && d.getMonth() === 2 && d.getDate() === 1);
42+
43+
d = new Date(2015, 8, 17);
44+
assert (d.toGMTString() === "Thu, 17 Sep 2015 00:00:00 GMT");
45+
46+
d = new Date(NaN);
47+
assert (d.toGMTString() === "Invalid Date");

0 commit comments

Comments
 (0)