Skip to content

Commit e514e5b

Browse files
committed
Add check for setTime implementation to see if the this argument is really a Date object.
JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
1 parent 2533db6 commit e514e5b

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -347,24 +347,32 @@ ecma_builtin_date_prototype_set_time (ecma_value_t this_arg, /**< this argument
347347
{
348348
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
349349

350-
/* 1. */
351-
ECMA_OP_TO_NUMBER_TRY_CATCH (t, time, ret_value);
352-
ecma_number_t *value_p = ecma_alloc_number ();
353-
*value_p = ecma_date_time_clip (t);
350+
if (!ecma_is_value_object (this_arg)
351+
|| ecma_object_get_class_name (ecma_get_object_from_value (this_arg)) != LIT_MAGIC_STRING_DATE_UL)
352+
{
353+
ret_value = ecma_raise_type_error ("Incompatible type");
354+
}
355+
else
356+
{
357+
/* 1. */
358+
ECMA_OP_TO_NUMBER_TRY_CATCH (t, time, ret_value);
359+
ecma_number_t *value_p = ecma_alloc_number ();
360+
*value_p = ecma_date_time_clip (t);
354361

355-
/* 2. */
356-
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
362+
/* 2. */
363+
ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);
357364

358-
ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p,
359-
ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
365+
ecma_property_t *prim_value_prop_p = ecma_get_internal_property (obj_p,
366+
ECMA_INTERNAL_PROPERTY_PRIMITIVE_NUMBER_VALUE);
360367

361-
ecma_number_t *prim_value_num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t,
368+
ecma_number_t *prim_value_num_p = ECMA_GET_NON_NULL_POINTER (ecma_number_t,
362369
prim_value_prop_p->u.internal_property.value);
363-
*prim_value_num_p = *value_p;
370+
*prim_value_num_p = *value_p;
364371

365-
/* 3. */
366-
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (value_p));
367-
ECMA_OP_TO_NUMBER_FINALIZE (t);
372+
/* 3. */
373+
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (value_p));
374+
ECMA_OP_TO_NUMBER_FINALIZE (t);
375+
}
368376

369377
return ret_value;
370378
} /* ecma_builtin_date_prototype_set_time */
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 setMethods =
17+
[
18+
"setTime",
19+
"setMilliseconds",
20+
"setSeconds",
21+
"setUTCMilliseconds",
22+
"setSeconds",
23+
"setUTCSeconds",
24+
"setMinutes",
25+
"setUTCMinutes",
26+
"setHours",
27+
"setUTCHours",
28+
"setDate",
29+
"setUTCDate",
30+
"setMonth",
31+
"setUTCMonth",
32+
"setFullYear",
33+
"setUTCFullYear"
34+
]
35+
36+
for(var i in setMethods)
37+
{
38+
var setMethod = setMethods[i];
39+
try
40+
{
41+
({method: Date.prototype[setMethod]}).method(0);
42+
}
43+
catch (e)
44+
{
45+
assert(e instanceof TypeError);
46+
}
47+
}

0 commit comments

Comments
 (0)