@@ -828,8 +828,9 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
828828 JERRY_ASSERT (start <= len && end <= len );
829829
830830 bool use_fast_path = ecma_op_object_is_fast_array (obj_p );
831+ uint32_t copied_length = (end > start ) ? end - start : 0 ;
831832#if ENABLED (JERRY_ES2015 )
832- ecma_value_t new_array = ecma_op_array_species_create (obj_p , 0 );
833+ ecma_value_t new_array = ecma_op_array_species_create (obj_p , copied_length );
833834
834835 if (ECMA_IS_VALUE_ERROR (new_array ))
835836 {
@@ -846,25 +847,45 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t arg1, /**< start */
846847 /* 9. */
847848 uint32_t n = 0 ;
848849
849- if (use_fast_path )
850+ if (use_fast_path && copied_length > 0 )
850851 {
851852 ecma_extended_object_t * ext_from_obj_p = (ecma_extended_object_t * ) obj_p ;
852853
853- if (ext_from_obj_p -> u .array .u .hole_count < ECMA_FAST_ARRAY_HOLE_ONE
854- && len != 0
855- && start < end )
854+ if (ext_from_obj_p -> u .array .u .hole_count < ECMA_FAST_ARRAY_HOLE_ONE )
856855 {
857- uint32_t length = end - start ;
858856 ecma_extended_object_t * ext_to_obj_p = (ecma_extended_object_t * ) new_array_p ;
859- ecma_value_t * to_buffer_p = ecma_fast_array_extend (new_array_p , length );
857+
858+ #if ENABLED (JERRY_ES2015 )
859+ uint32_t target_length = ext_to_obj_p -> u .array .length ;
860+ ecma_value_t * to_buffer_p ;
861+ if (copied_length == target_length )
862+ {
863+ to_buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t , new_array_p -> u1 .property_list_cp );
864+ }
865+ else if (copied_length > target_length )
866+ {
867+ to_buffer_p = ecma_fast_array_extend (new_array_p , copied_length );
868+ }
869+ else
870+ {
871+ ecma_delete_fast_array_properties (new_array_p , copied_length );
872+ to_buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t , new_array_p -> u1 .property_list_cp );
873+ }
874+ #else /* !ENABLED (JERRY_ES2015) */
875+ ecma_value_t * to_buffer_p = ecma_fast_array_extend (new_array_p , copied_length );
876+ #endif /* ENABLED (JERRY_ES2015) */
877+
860878 ecma_value_t * from_buffer_p = ECMA_GET_NON_NULL_POINTER (ecma_value_t , obj_p -> u1 .property_list_cp );
861879
862880 for (uint32_t k = start ; k < end ; k ++ , n ++ )
863881 {
882+ #if ENABLED (JERRY_ES2015 )
883+ ecma_free_value_if_not_object (to_buffer_p [n ]);
884+ #endif /* ENABLED (JERRY_ES2015) */
864885 to_buffer_p [n ] = ecma_copy_value_if_not_object (from_buffer_p [k ]);
865886 }
866887
867- ext_to_obj_p -> u .array .u .hole_count -= length * ECMA_FAST_ARRAY_HOLE_ONE ;
888+ ext_to_obj_p -> u .array .u .hole_count &= ECMA_FAST_ARRAY_HOLE_ONE - 1 ;
868889
869890 return new_array ;
870891 }
0 commit comments