mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
Implement Array.prototype.splice().
JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
This commit is contained in:
parent
b0d4acd026
commit
dd94064a8e
@ -1435,6 +1435,311 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t this_arg, /**< 'this' ar
|
||||
return ret_value;
|
||||
} /* ecma_builtin_array_prototype_object_slice */
|
||||
|
||||
/**
|
||||
* The Array.prototype object's 'splice' routine
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v5, 15.4.4.12
|
||||
*
|
||||
* @return completion value
|
||||
* Returned value must be freed with ecma_free_completion_value.
|
||||
*/
|
||||
static ecma_completion_value_t
|
||||
ecma_builtin_array_prototype_object_splice (ecma_value_t this_arg, /**< this argument */
|
||||
const ecma_value_t args[], /**< arguments list */
|
||||
ecma_length_t args_number) /**< number of arguments */
|
||||
{
|
||||
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
|
||||
|
||||
/* 1. */
|
||||
ECMA_TRY_CATCH (obj_this,
|
||||
ecma_op_to_object (this_arg),
|
||||
ret_value);
|
||||
|
||||
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
|
||||
|
||||
/* 3. */
|
||||
ecma_string_t *length_magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH);
|
||||
|
||||
ECMA_TRY_CATCH (len_value,
|
||||
ecma_op_object_get (obj_p, length_magic_string_p),
|
||||
ret_value);
|
||||
|
||||
/* 4. */
|
||||
ECMA_OP_TO_NUMBER_TRY_CATCH (len_number,
|
||||
len_value,
|
||||
ret_value);
|
||||
|
||||
const uint32_t len = ecma_number_to_uint32 (len_number);
|
||||
|
||||
ecma_completion_value_t new_array = ecma_op_create_array_object (0, 0, false);
|
||||
ecma_object_t *new_array_p = ecma_get_object_from_completion_value (new_array);
|
||||
|
||||
uint32_t start = 0;
|
||||
uint32_t delete_count = 0;
|
||||
|
||||
if (args_number > 0)
|
||||
{
|
||||
/* 5. */
|
||||
ECMA_OP_TO_NUMBER_TRY_CATCH (start_num,
|
||||
args[0],
|
||||
ret_value);
|
||||
|
||||
int32_t relative_start = ecma_number_to_int32 (start_num);
|
||||
|
||||
/* 6. */
|
||||
if (relative_start < 0)
|
||||
{
|
||||
uint32_t start_abs = (uint32_t) - relative_start;
|
||||
if (start_abs > len)
|
||||
{
|
||||
start = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
start = len - start_abs;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
start = (uint32_t) relative_start;
|
||||
if (start > len)
|
||||
{
|
||||
start = len;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* If there is only one argument, that will be the start argument,
|
||||
* and we must delete the additional elements.
|
||||
*/
|
||||
if (args_number == 1)
|
||||
{
|
||||
delete_count = len - start;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 7. */
|
||||
ECMA_OP_TO_NUMBER_TRY_CATCH (delete_num,
|
||||
args[1],
|
||||
ret_value);
|
||||
|
||||
int32_t delete_count_int = ecma_number_to_int32 (delete_num);
|
||||
|
||||
if (delete_count_int > 0)
|
||||
{
|
||||
delete_count = (uint32_t) delete_count_int;
|
||||
}
|
||||
else
|
||||
{
|
||||
delete_count = 0;
|
||||
}
|
||||
|
||||
if (len - start < delete_count)
|
||||
{
|
||||
delete_count = len - start;
|
||||
}
|
||||
|
||||
ECMA_OP_TO_NUMBER_FINALIZE (delete_num);
|
||||
}
|
||||
|
||||
ECMA_OP_TO_NUMBER_FINALIZE (start_num);
|
||||
}
|
||||
|
||||
/* 8-9. */
|
||||
uint32_t k;
|
||||
|
||||
for (uint32_t del_item_idx, k = 0;
|
||||
k < delete_count && ecma_is_completion_value_empty (ret_value);
|
||||
k++)
|
||||
{
|
||||
/* 9.a */
|
||||
del_item_idx = k + start;
|
||||
ecma_string_t *idx_str_p = ecma_new_ecma_string_from_uint32 (del_item_idx);
|
||||
|
||||
/* 9.b */
|
||||
if (ecma_op_object_get_property (obj_p, idx_str_p) != NULL)
|
||||
{
|
||||
/* 9.c.i */
|
||||
ECMA_TRY_CATCH (get_value,
|
||||
ecma_op_object_get (obj_p, idx_str_p),
|
||||
ret_value);
|
||||
|
||||
ecma_string_t *idx_str_new_p = ecma_new_ecma_string_from_uint32 (k);
|
||||
|
||||
/* 9.c.ii
|
||||
* Using [[Put]] is equivalent to using [[DefineOwnProperty]] as specified the standard,
|
||||
* so we use [[Put]] instead for simplicity. No need for a try-catch block since it is called
|
||||
* with is_throw = false.
|
||||
*/
|
||||
ECMA_TRY_CATCH (put_value,
|
||||
ecma_op_object_put (new_array_p, idx_str_new_p, get_value, false),
|
||||
ret_value);
|
||||
|
||||
ECMA_FINALIZE (put_value);
|
||||
ecma_deref_ecma_string (idx_str_new_p);
|
||||
ECMA_FINALIZE (get_value);
|
||||
}
|
||||
|
||||
ecma_deref_ecma_string (idx_str_p);
|
||||
}
|
||||
|
||||
/* 11. */
|
||||
ecma_length_t item_count;
|
||||
|
||||
if (args_number > 2)
|
||||
{
|
||||
item_count = (ecma_length_t) (args_number - 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
item_count = 0;
|
||||
}
|
||||
|
||||
const uint32_t new_len = len - delete_count + item_count;
|
||||
|
||||
if (item_count != delete_count)
|
||||
{
|
||||
uint32_t from, to;
|
||||
|
||||
/* 12. */
|
||||
if (item_count < delete_count)
|
||||
{
|
||||
/* 12.b */
|
||||
for (k = start; k < (len - delete_count) && ecma_is_completion_value_empty (ret_value); k++)
|
||||
{
|
||||
from = k + delete_count;
|
||||
ecma_string_t *from_str_p = ecma_new_ecma_string_from_uint32 (from);
|
||||
|
||||
to = k + item_count;
|
||||
ecma_string_t *to_str_p = ecma_new_ecma_string_from_uint32 (to);
|
||||
|
||||
/* 12.b.iii */
|
||||
if (ecma_op_object_get_property (obj_p, from_str_p) != NULL)
|
||||
{
|
||||
/* 12.b.iv */
|
||||
ECMA_TRY_CATCH (get_value,
|
||||
ecma_op_object_get (obj_p, from_str_p),
|
||||
ret_value);
|
||||
|
||||
ECMA_TRY_CATCH (put_value,
|
||||
ecma_op_object_put (obj_p, to_str_p, get_value, true),
|
||||
ret_value);
|
||||
|
||||
ECMA_FINALIZE (put_value);
|
||||
ECMA_FINALIZE (get_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 12.b.v */
|
||||
ECMA_TRY_CATCH (del_value,
|
||||
ecma_op_object_delete (obj_p, to_str_p, true),
|
||||
ret_value);
|
||||
|
||||
ECMA_FINALIZE (del_value);
|
||||
}
|
||||
|
||||
ecma_deref_ecma_string (to_str_p);
|
||||
ecma_deref_ecma_string (from_str_p);
|
||||
}
|
||||
|
||||
/* 12.d */
|
||||
for (k = len; k > new_len && ecma_is_completion_value_empty (ret_value); k--)
|
||||
{
|
||||
ecma_string_t *str_idx_p = ecma_new_ecma_string_from_uint32 (k - 1);
|
||||
ECMA_TRY_CATCH (del_value,
|
||||
ecma_op_object_delete (obj_p, str_idx_p, true),
|
||||
ret_value);
|
||||
|
||||
ECMA_FINALIZE (del_value);
|
||||
ecma_deref_ecma_string (str_idx_p);
|
||||
}
|
||||
}
|
||||
/* 13. */
|
||||
else if (item_count > delete_count)
|
||||
{
|
||||
/* 13.b */
|
||||
for (k = len - delete_count; k > start && ecma_is_completion_value_empty (ret_value); k--)
|
||||
{
|
||||
from = k + delete_count - 1;
|
||||
ecma_string_t *from_str_p = ecma_new_ecma_string_from_uint32 (from);
|
||||
|
||||
to = k + item_count - 1;
|
||||
ecma_string_t *to_str_p = ecma_new_ecma_string_from_uint32 (to);
|
||||
|
||||
/* 13.b.iii */
|
||||
if (ecma_op_object_get_property (obj_p, from_str_p) != NULL)
|
||||
{
|
||||
/* 13.b.iv */
|
||||
ECMA_TRY_CATCH (get_value,
|
||||
ecma_op_object_get (obj_p, from_str_p),
|
||||
ret_value);
|
||||
|
||||
ECMA_TRY_CATCH (put_value,
|
||||
ecma_op_object_put (obj_p, to_str_p, get_value, true),
|
||||
ret_value);
|
||||
|
||||
ECMA_FINALIZE (put_value);
|
||||
ECMA_FINALIZE (get_value);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 13.b.v */
|
||||
ECMA_TRY_CATCH (del_value,
|
||||
ecma_op_object_delete (obj_p, to_str_p, true),
|
||||
ret_value);
|
||||
|
||||
ECMA_FINALIZE (del_value);
|
||||
}
|
||||
|
||||
ecma_deref_ecma_string (to_str_p);
|
||||
ecma_deref_ecma_string (from_str_p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 15. */
|
||||
ecma_length_t idx = 0;
|
||||
for (ecma_length_t arg_index = 2;
|
||||
arg_index < args_number && ecma_is_completion_value_empty (ret_value);
|
||||
arg_index++, idx++)
|
||||
{
|
||||
ecma_string_t *str_idx_p = ecma_new_ecma_string_from_uint32 ((uint32_t) (start + idx));
|
||||
ECMA_TRY_CATCH (put_value,
|
||||
ecma_op_object_put (obj_p, str_idx_p, args[arg_index], true),
|
||||
ret_value);
|
||||
|
||||
ECMA_FINALIZE (put_value);
|
||||
ecma_deref_ecma_string (str_idx_p);
|
||||
}
|
||||
|
||||
/* 16. */
|
||||
if (ecma_is_completion_value_empty (ret_value))
|
||||
{
|
||||
ECMA_TRY_CATCH (set_length_value,
|
||||
ecma_builtin_array_prototype_helper_set_length (obj_p, new_len),
|
||||
ret_value);
|
||||
|
||||
ECMA_FINALIZE (set_length_value);
|
||||
}
|
||||
|
||||
if (ecma_is_completion_value_empty (ret_value))
|
||||
{
|
||||
ret_value = new_array;
|
||||
}
|
||||
else
|
||||
{
|
||||
ecma_free_completion_value (new_array);
|
||||
}
|
||||
|
||||
ECMA_OP_TO_NUMBER_FINALIZE (len_number);
|
||||
ECMA_FINALIZE (len_value);
|
||||
ecma_deref_ecma_string (length_magic_string_p);
|
||||
ECMA_FINALIZE (obj_this);
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_builtin_array_prototype_object_splice */
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@ -71,6 +71,7 @@ ROUTINE (ECMA_MAGIC_STRING_UNSHIFT, ecma_builtin_array_prototype_object_unshift,
|
||||
ROUTINE (ECMA_MAGIC_STRING_EVERY, ecma_builtin_array_prototype_object_every, 2, 1)
|
||||
ROUTINE (ECMA_MAGIC_STRING_SOME, ecma_builtin_array_prototype_object_some, 2, 1)
|
||||
ROUTINE (ECMA_MAGIC_STRING_SLICE, ecma_builtin_array_prototype_object_slice, 2, 2)
|
||||
ROUTINE (ECMA_MAGIC_STRING_SPLICE, ecma_builtin_array_prototype_object_splice, NON_FIXED, 2)
|
||||
|
||||
#undef OBJECT_ID
|
||||
#undef SIMPLE_VALUE
|
||||
|
||||
113
tests/jerry/array_prototype_splice.js
Normal file
113
tests/jerry/array_prototype_splice.js
Normal file
@ -0,0 +1,113 @@
|
||||
// Copyright 2015 Samsung Electronics Co., Ltd.
|
||||
// Copyright 2015 University of Szeged.
|
||||
//
|
||||
// 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.
|
||||
|
||||
function setDefaultValues()
|
||||
{
|
||||
return [54, undefined, -127, "sunshine"];
|
||||
}
|
||||
|
||||
var array = setDefaultValues();
|
||||
var array1 = array.splice();
|
||||
|
||||
assert (array.length == 4);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array[2] == -127);
|
||||
assert (array[3] == "sunshine");
|
||||
assert (array1.length == 0);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues();
|
||||
var array2 = array.splice(2);
|
||||
|
||||
assert (array.length == 2);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array2.length == 2);
|
||||
assert (array2[0] == -127);
|
||||
assert (array2[1] == "sunshine");
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues();
|
||||
var array3 = array.splice(2, 1);
|
||||
|
||||
assert (array.length == 3);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array[2] == "sunshine");
|
||||
assert (array3.length == 1);
|
||||
assert (array3[0] == -127);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues();
|
||||
var array4 = array.splice(0, 3, 6720, "Szeged");
|
||||
|
||||
assert (array.length == 3);
|
||||
assert (array[0] == 6720);
|
||||
assert (array[1] == "Szeged");
|
||||
assert (array[2] == "sunshine");
|
||||
assert (array4.length == 3);
|
||||
assert (array4[0] == 54);
|
||||
assert (array4[1] == undefined);
|
||||
assert (array4[2] == -127);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues();
|
||||
var array5 = array.splice(-2, -2, 6720, "Szeged");
|
||||
|
||||
assert (array.length == 6);
|
||||
assert (array[0] == 54);
|
||||
assert (array[1] == undefined);
|
||||
assert (array[2] == 6720);
|
||||
assert (array[3] == "Szeged");
|
||||
assert (array[4] == -127);
|
||||
assert (array[5] == "sunshine");
|
||||
assert (array5.length == 0);
|
||||
|
||||
// --------------------------------------------------------
|
||||
array = setDefaultValues();
|
||||
var array6 = array.splice(undefined, undefined, undefined);
|
||||
|
||||
assert (array.length == 5);
|
||||
assert (array[0] == undefined);
|
||||
assert (array[1] == 54);
|
||||
assert (array[2] == undefined);
|
||||
assert (array[3] == -127);
|
||||
assert (array[4] == "sunshine");
|
||||
assert (array6.length == 0);
|
||||
|
||||
// Checking behavior when unable to get length
|
||||
var obj = {splice : Array.prototype.splice};
|
||||
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.splice(1, 2, "item1", "item2");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
|
||||
// Checking behavior when unable to get element
|
||||
var obj = {length : 1, splice : Array.prototype.splice};
|
||||
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
|
||||
|
||||
try {
|
||||
obj.splice(0, 1, "item1", "item2");
|
||||
assert (false);
|
||||
} catch (e) {
|
||||
assert (e.message === "foo");
|
||||
assert (e instanceof ReferenceError);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user