Skip to content

Commit 7b589d1

Browse files
Yuyuporerobika
authored andcommitted
Refactor code to use ToLength (#3210)
JerryScript-DCO-1.0-Signed-off-by: Daniella Barsony bella@inf.u-szeged.hu
1 parent 3fb6f15 commit 7b589d1

File tree

6 files changed

+46
-58
lines changed

6 files changed

+46
-58
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2245,18 +2245,16 @@ ecma_builtin_array_prototype_dispatch_routine (uint16_t builtin_routine_id, /**<
22452245
return len_value;
22462246
}
22472247

2248-
ecma_number_t length_number;
2249-
ecma_value_t ret_value = ecma_get_number (len_value, &length_number);
2248+
uint32_t length = 0;
2249+
ecma_value_t ret_value = ecma_op_to_length (len_value, &length);
22502250

2251-
if (!ecma_is_value_empty (ret_value))
2251+
if (ECMA_IS_VALUE_ERROR (ret_value))
22522252
{
22532253
ecma_free_value (len_value);
22542254
ecma_deref_object (obj_p);
22552255
return ret_value;
22562256
}
22572257

2258-
uint32_t length = ecma_number_to_uint32 (length_number);
2259-
22602258
ecma_value_t routine_arg_1 = arguments_list_p[0];
22612259
ecma_value_t routine_arg_2 = arguments_list_p[1];
22622260

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -965,18 +965,10 @@ ecma_builtin_typedarray_prototype_set (ecma_value_t this_arg, /**< this argument
965965
ecma_op_object_get_by_magic_id (source_obj_p, LIT_MAGIC_STRING_LENGTH),
966966
ret_val);
967967

968-
ECMA_OP_TO_NUMBER_TRY_CATCH (source_length_num, source_length, ret_val);
969-
970-
if (ecma_number_is_nan (source_length_num) || source_length_num <= 0)
971-
{
972-
source_length_num = 0;
973-
}
974-
975-
uint32_t source_length_uint32 = ecma_number_to_uint32 (source_length_num);
976-
977-
if ((ecma_number_t) source_length_uint32 != source_length_num)
968+
uint32_t source_length_uint32;
969+
if (ECMA_IS_VALUE_ERROR (ecma_op_to_length (source_length, &source_length_uint32)))
978970
{
979-
return ecma_raise_range_error (ECMA_ERR_MSG ("Invalid source length"));
971+
return ECMA_VALUE_ERROR;
980972
}
981973

982974
/* 20. if srcLength + targetOffset > targetLength, throw a RangeError */
@@ -1012,7 +1004,6 @@ ecma_builtin_typedarray_prototype_set (ecma_value_t this_arg, /**< this argument
10121004
target_byte_index += target_info.element_size;
10131005
}
10141006

1015-
ECMA_OP_TO_NUMBER_FINALIZE (source_length_num);
10161007
ECMA_FINALIZE (source_length);
10171008
ECMA_FINALIZE (source_obj);
10181009
ECMA_OP_TO_NUMBER_FINALIZE (target_offset_num);

jerry-core/ecma/operations/ecma-dataview-object.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,39 +104,14 @@ ecma_op_dataview_create (const ecma_value_t *arguments_list_p, /**< arguments li
104104
if (arguments_list_len > 2)
105105
{
106106
/* 12.a */
107-
ecma_number_t byte_length;
108-
ecma_value_t byte_length_value = ecma_get_number (arguments_list_p[2], &byte_length);
107+
ecma_value_t byte_length_value = ecma_op_to_length (arguments_list_p[2], &viewByteLength);
109108

110109
/* 12.b */
111110
if (ECMA_IS_VALUE_ERROR (byte_length_value))
112111
{
113112
return byte_length_value;
114113
}
115114

116-
int32_t byte_length_int32 = ecma_number_to_int32 (byte_length);
117-
118-
if (ecma_number_is_nan (byte_length))
119-
{
120-
viewByteLength = 0;
121-
}
122-
else if (ecma_number_is_infinity (byte_length))
123-
{
124-
if (ecma_number_is_negative (byte_length))
125-
{
126-
return ecma_raise_range_error (ECMA_ERR_MSG ("Invalid DataView length"));
127-
}
128-
129-
viewByteLength = UINT32_MAX;
130-
}
131-
else if (byte_length_int32 <= 0)
132-
{
133-
viewByteLength = 0;
134-
}
135-
else
136-
{
137-
viewByteLength = JERRY_MIN ((ecma_length_t) byte_length_int32, UINT32_MAX);
138-
}
139-
140115
/* 12.c */
141116
if ((ecma_number_t) offset + viewByteLength > buffer_byte_length)
142117
{

jerry-core/ecma/operations/ecma-typedarray-object.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,6 @@ ecma_op_create_typedarray (const ecma_value_t *arguments_list_p, /**< the arg li
960960
: ECMA_VALUE_UNDEFINED);
961961

962962
ECMA_OP_TO_NUMBER_TRY_CATCH (num2, arg2, ret);
963-
964963
uint32_t offset = ecma_number_to_uint32 (num2);
965964

966965
if (ecma_number_is_negative (ecma_number_to_int32 (num2)) || (offset % (uint32_t) (1 << element_size_shift) != 0))
@@ -993,9 +992,11 @@ ecma_op_create_typedarray (const ecma_value_t *arguments_list_p, /**< the arg li
993992
}
994993
else
995994
{
996-
ECMA_OP_TO_NUMBER_TRY_CATCH (num3, arg3, ret);
997-
int32_t new_length = ecma_number_to_int32 (num3);
998-
new_length = (new_length > 0) ? new_length : 0;
995+
uint32_t new_length;
996+
if (ECMA_IS_VALUE_ERROR (ecma_op_to_length (arg3, &new_length)))
997+
{
998+
return ECMA_VALUE_ERROR;
999+
}
9991000

10001001
if ((uint32_t) new_length > (UINT32_MAX >> element_size_shift))
10011002
{
@@ -1010,8 +1011,6 @@ ecma_op_create_typedarray (const ecma_value_t *arguments_list_p, /**< the arg li
10101011
ret = ecma_raise_range_error (ECMA_ERR_MSG ("Invalid length."));
10111012
}
10121013
}
1013-
1014-
ECMA_OP_TO_NUMBER_FINALIZE (num3);
10151014
}
10161015

10171016
if (ecma_is_value_empty (ret))

tests/jerry/array-prototype-push.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ try {
5353
}
5454
assert(a.length === 4294967295)
5555

56-
5756
var o = { length : 4294967294, push : Array.prototype.push };
5857
assert(o.push("x") === 4294967295);
5958
assert(o.length === 4294967295);
@@ -67,14 +66,6 @@ try {
6766
assert(o.length === 4294967296);
6867
assert(o[4294967295] === "y");
6968

70-
try {
71-
assert(o.push("z") === 1);
72-
} catch (e) {
73-
assert(false);
74-
}
75-
assert(o.length === 1);
76-
assert(o[0] === "z");
77-
7869
/* ES v5.1 15.4.4.7.5.
7970
Checking behavior when array is non-extensible while pushing */
8071
var arr = [];
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
var o = { length : 4294967294, push : Array.prototype.push };
16+
assert(o.push("x") === 4294967295);
17+
assert(o.length === 4294967295);
18+
assert(o[4294967294] === "x");
19+
20+
try {
21+
assert(o.push("y") === 4294967296);
22+
} catch (e) {
23+
assert(false);
24+
}
25+
assert(o.length === 4294967296);
26+
assert(o[4294967295] === "y");
27+
28+
try {
29+
assert(o.push("z") === 1);
30+
} catch (e) {
31+
assert(false);
32+
}
33+
assert(o.length === 1);
34+
assert(o[0] === "z");

0 commit comments

Comments
 (0)