Refactor code to use ToLength (#3210)

JerryScript-DCO-1.0-Signed-off-by: Daniella Barsony bella@inf.u-szeged.hu
This commit is contained in:
Daniella Barsony
2019-10-21 14:08:13 +02:00
committed by Robert Fancsik
parent 3fb6f15730
commit 7b589d1381
6 changed files with 46 additions and 58 deletions
@@ -2245,18 +2245,16 @@ ecma_builtin_array_prototype_dispatch_routine (uint16_t builtin_routine_id, /**<
return len_value;
}
ecma_number_t length_number;
ecma_value_t ret_value = ecma_get_number (len_value, &length_number);
uint32_t length = 0;
ecma_value_t ret_value = ecma_op_to_length (len_value, &length);
if (!ecma_is_value_empty (ret_value))
if (ECMA_IS_VALUE_ERROR (ret_value))
{
ecma_free_value (len_value);
ecma_deref_object (obj_p);
return ret_value;
}
uint32_t length = ecma_number_to_uint32 (length_number);
ecma_value_t routine_arg_1 = arguments_list_p[0];
ecma_value_t routine_arg_2 = arguments_list_p[1];
@@ -965,18 +965,10 @@ ecma_builtin_typedarray_prototype_set (ecma_value_t this_arg, /**< this argument
ecma_op_object_get_by_magic_id (source_obj_p, LIT_MAGIC_STRING_LENGTH),
ret_val);
ECMA_OP_TO_NUMBER_TRY_CATCH (source_length_num, source_length, ret_val);
if (ecma_number_is_nan (source_length_num) || source_length_num <= 0)
uint32_t source_length_uint32;
if (ECMA_IS_VALUE_ERROR (ecma_op_to_length (source_length, &source_length_uint32)))
{
source_length_num = 0;
}
uint32_t source_length_uint32 = ecma_number_to_uint32 (source_length_num);
if ((ecma_number_t) source_length_uint32 != source_length_num)
{
return ecma_raise_range_error (ECMA_ERR_MSG ("Invalid source length"));
return ECMA_VALUE_ERROR;
}
/* 20. if srcLength + targetOffset > targetLength, throw a RangeError */
@@ -1012,7 +1004,6 @@ ecma_builtin_typedarray_prototype_set (ecma_value_t this_arg, /**< this argument
target_byte_index += target_info.element_size;
}
ECMA_OP_TO_NUMBER_FINALIZE (source_length_num);
ECMA_FINALIZE (source_length);
ECMA_FINALIZE (source_obj);
ECMA_OP_TO_NUMBER_FINALIZE (target_offset_num);
@@ -104,8 +104,7 @@ ecma_op_dataview_create (const ecma_value_t *arguments_list_p, /**< arguments li
if (arguments_list_len > 2)
{
/* 12.a */
ecma_number_t byte_length;
ecma_value_t byte_length_value = ecma_get_number (arguments_list_p[2], &byte_length);
ecma_value_t byte_length_value = ecma_op_to_length (arguments_list_p[2], &viewByteLength);
/* 12.b */
if (ECMA_IS_VALUE_ERROR (byte_length_value))
@@ -113,30 +112,6 @@ ecma_op_dataview_create (const ecma_value_t *arguments_list_p, /**< arguments li
return byte_length_value;
}
int32_t byte_length_int32 = ecma_number_to_int32 (byte_length);
if (ecma_number_is_nan (byte_length))
{
viewByteLength = 0;
}
else if (ecma_number_is_infinity (byte_length))
{
if (ecma_number_is_negative (byte_length))
{
return ecma_raise_range_error (ECMA_ERR_MSG ("Invalid DataView length"));
}
viewByteLength = UINT32_MAX;
}
else if (byte_length_int32 <= 0)
{
viewByteLength = 0;
}
else
{
viewByteLength = JERRY_MIN ((ecma_length_t) byte_length_int32, UINT32_MAX);
}
/* 12.c */
if ((ecma_number_t) offset + viewByteLength > buffer_byte_length)
{
@@ -960,7 +960,6 @@ ecma_op_create_typedarray (const ecma_value_t *arguments_list_p, /**< the arg li
: ECMA_VALUE_UNDEFINED);
ECMA_OP_TO_NUMBER_TRY_CATCH (num2, arg2, ret);
uint32_t offset = ecma_number_to_uint32 (num2);
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
}
else
{
ECMA_OP_TO_NUMBER_TRY_CATCH (num3, arg3, ret);
int32_t new_length = ecma_number_to_int32 (num3);
new_length = (new_length > 0) ? new_length : 0;
uint32_t new_length;
if (ECMA_IS_VALUE_ERROR (ecma_op_to_length (arg3, &new_length)))
{
return ECMA_VALUE_ERROR;
}
if ((uint32_t) new_length > (UINT32_MAX >> element_size_shift))
{
@@ -1010,8 +1011,6 @@ ecma_op_create_typedarray (const ecma_value_t *arguments_list_p, /**< the arg li
ret = ecma_raise_range_error (ECMA_ERR_MSG ("Invalid length."));
}
}
ECMA_OP_TO_NUMBER_FINALIZE (num3);
}
if (ecma_is_value_empty (ret))
-9
View File
@@ -53,7 +53,6 @@ try {
}
assert(a.length === 4294967295)
var o = { length : 4294967294, push : Array.prototype.push };
assert(o.push("x") === 4294967295);
assert(o.length === 4294967295);
@@ -67,14 +66,6 @@ try {
assert(o.length === 4294967296);
assert(o[4294967295] === "y");
try {
assert(o.push("z") === 1);
} catch (e) {
assert(false);
}
assert(o.length === 1);
assert(o[0] === "z");
/* ES v5.1 15.4.4.7.5.
Checking behavior when array is non-extensible while pushing */
var arr = [];
+34
View File
@@ -0,0 +1,34 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var o = { length : 4294967294, push : Array.prototype.push };
assert(o.push("x") === 4294967295);
assert(o.length === 4294967295);
assert(o[4294967294] === "x");
try {
assert(o.push("y") === 4294967296);
} catch (e) {
assert(false);
}
assert(o.length === 4294967296);
assert(o[4294967295] === "y");
try {
assert(o.push("z") === 1);
} catch (e) {
assert(false);
}
assert(o.length === 1);
assert(o[0] === "z");