Ensure that fast array objects are always extensible (#4557)

This patch helps to eliminate some extra checks from Array.prototype methods

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik 2021-02-03 16:17:50 +01:00 committed by GitHub
parent 090b6307a6
commit 1712ad5dc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 32 deletions

View File

@ -390,12 +390,6 @@ ecma_builtin_array_prototype_object_pop (ecma_object_t *obj_p, /**< object */
if (ecma_op_object_is_fast_array (obj_p))
{
if (!ecma_op_ordinary_object_is_extensible (obj_p))
{
ecma_free_value (get_value);
return ecma_raise_type_error (ECMA_ERR_MSG ("Invalid argument type"));
}
ecma_delete_fast_array_properties (obj_p, (uint32_t) len);
return get_value;
@ -441,11 +435,6 @@ ecma_builtin_array_prototype_object_push (const ecma_value_t *argument_list_p, /
{
if (ecma_op_object_is_fast_array (obj_p))
{
if (!ecma_op_ordinary_object_is_extensible (obj_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Invalid argument type"));
}
if ((ecma_number_t) (length + arguments_number) > UINT32_MAX)
{
return ecma_raise_range_error (ECMA_ERR_MSG ("Invalid Array length"));
@ -540,8 +529,7 @@ ecma_builtin_array_prototype_object_reverse (ecma_value_t this_arg, /**< this ar
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
if (ext_obj_p->u.array.length_prop_and_hole_count < ECMA_FAST_ARRAY_HOLE_ONE
&& len != 0
&& ecma_op_ordinary_object_is_extensible (obj_p))
&& len != 0)
{
ecma_value_t *buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, obj_p->u1.property_list_cp);
@ -726,8 +714,7 @@ ecma_builtin_array_prototype_object_shift (ecma_object_t *obj_p, /**< object */
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
if (ext_obj_p->u.array.length_prop_and_hole_count < ECMA_FAST_ARRAY_HOLE_ONE
&& len != 0
&& ecma_op_ordinary_object_is_extensible (obj_p))
&& len != 0)
{
ecma_value_t *buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t, obj_p->u1.property_list_cp);
ecma_value_t ret_value = buffer_p[0];
@ -1514,8 +1501,7 @@ ecma_builtin_array_prototype_object_unshift (const ecma_value_t args[], /**< arg
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
if (ext_obj_p->u.array.length_prop_and_hole_count < ECMA_FAST_ARRAY_HOLE_ONE
&& len != 0
&& ecma_op_ordinary_object_is_extensible (obj_p))
&& len != 0)
{
if (args_number > UINT32_MAX - len)
{
@ -2302,8 +2288,7 @@ ecma_builtin_array_prototype_fill (ecma_value_t value, /**< value */
{
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
if (ext_obj_p->u.array.length_prop_and_hole_count < ECMA_FAST_ARRAY_HOLE_ONE
&& ecma_op_ordinary_object_is_extensible (obj_p))
if (ext_obj_p->u.array.length_prop_and_hole_count < ECMA_FAST_ARRAY_HOLE_ONE)
{
if (JERRY_UNLIKELY (obj_p->u1.property_list_cp == JMEM_CP_NULL))
{

View File

@ -1276,11 +1276,6 @@ ecma_op_object_put_with_receiver (ecma_object_t *object_p, /**< the object */
if (JERRY_LIKELY (ecma_op_array_is_fast_array (ext_object_p)))
{
if (JERRY_UNLIKELY (!ecma_op_ordinary_object_is_extensible (object_p)))
{
return ecma_reject (is_throw);
}
uint32_t index = ecma_string_get_array_index (property_name_p);
if (JERRY_UNLIKELY (index == ECMA_STRING_NOT_ARRAY_INDEX))
@ -3233,7 +3228,11 @@ ecma_op_ordinary_object_is_extensible (ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (object_p));
return (object_p->type_flags_refs & ECMA_OBJECT_FLAG_EXTENSIBLE) != 0;
bool is_extensible = (object_p->type_flags_refs & ECMA_OBJECT_FLAG_EXTENSIBLE) != 0;
JERRY_ASSERT (!ecma_op_object_is_fast_array (object_p) || is_extensible);
return is_extensible;
} /* ecma_op_ordinary_object_is_extensible */
/**
@ -3243,6 +3242,12 @@ void JERRY_ATTR_NOINLINE
ecma_op_ordinary_object_prevent_extensions (ecma_object_t *object_p) /**< object */
{
JERRY_ASSERT (!ECMA_OBJECT_IS_PROXY (object_p));
if (JERRY_UNLIKELY (ecma_op_object_is_fast_array (object_p)))
{
ecma_fast_array_convert_to_normal (object_p);
}
object_p->type_flags_refs &= (ecma_object_descriptor_t) ~ECMA_OBJECT_FLAG_EXTENSIBLE;
} /* ecma_op_ordinary_object_prevent_extensions */

View File

@ -12,12 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
var array = [$, Infinity]
var array = [5.1, Infinity]
Reflect.preventExtensions(array);
try {
var $ = array.pop()
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
assert(array.pop() == Infinity);