2015-10-02 15:14:16 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
#include <utility>
|
2015-10-02 15:14:16 +00:00
|
|
|
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/compilation-cache.h"
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/execution.h"
|
2019-05-22 12:44:24 +00:00
|
|
|
#include "src/handles/global-handles.h"
|
2018-04-09 19:11:22 +00:00
|
|
|
#include "src/heap/factory.h"
|
2015-10-02 15:14:16 +00:00
|
|
|
#include "src/ic/stub-cache.h"
|
2022-04-28 03:12:49 +00:00
|
|
|
#include "src/init/v8.h"
|
2018-07-25 14:11:56 +00:00
|
|
|
#include "src/objects/js-array-inl.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2022-04-28 03:12:49 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
2015-10-02 15:14:16 +00:00
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2022-04-28 03:12:49 +00:00
|
|
|
|
|
|
|
using ElementsKindTest = TestWithContext;
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Helper functions.
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <typename T, typename M>
|
2018-06-20 12:44:39 +00:00
|
|
|
bool EQUALS(Isolate* isolate, Handle<T> left, Handle<M> right) {
|
2015-10-02 15:14:16 +00:00
|
|
|
if (*left == *right) return true;
|
2018-12-08 02:59:17 +00:00
|
|
|
return Object::Equals(isolate, Handle<Object>::cast(left),
|
|
|
|
Handle<Object>::cast(right))
|
2015-10-02 15:14:16 +00:00
|
|
|
.FromJust();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename M>
|
2018-06-20 12:44:39 +00:00
|
|
|
bool EQUALS(Isolate* isolate, Handle<T> left, M right) {
|
|
|
|
return EQUALS(isolate, left, handle(right, isolate));
|
2015-10-02 15:14:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename M>
|
2018-06-20 12:44:39 +00:00
|
|
|
bool EQUALS(Isolate* isolate, T left, Handle<M> right) {
|
|
|
|
return EQUALS(isolate, handle(left, isolate), right);
|
2015-10-02 15:14:16 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 20:28:55 +00:00
|
|
|
bool ElementsKindIsHoleyElementsKindForRead(ElementsKind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case ElementsKind::HOLEY_SMI_ELEMENTS:
|
|
|
|
case ElementsKind::HOLEY_ELEMENTS:
|
|
|
|
case ElementsKind::HOLEY_DOUBLE_ELEMENTS:
|
2019-08-28 16:48:03 +00:00
|
|
|
case ElementsKind::HOLEY_NONEXTENSIBLE_ELEMENTS:
|
2019-05-07 20:28:55 +00:00
|
|
|
case ElementsKind::HOLEY_SEALED_ELEMENTS:
|
|
|
|
case ElementsKind::HOLEY_FROZEN_ELEMENTS:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ElementsKindIsHoleyElementsKind(ElementsKind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case ElementsKind::HOLEY_SMI_ELEMENTS:
|
|
|
|
case ElementsKind::HOLEY_ELEMENTS:
|
|
|
|
case ElementsKind::HOLEY_DOUBLE_ELEMENTS:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ElementsKindIsFastPackedElementsKind(ElementsKind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case ElementsKind::PACKED_SMI_ELEMENTS:
|
|
|
|
case ElementsKind::PACKED_ELEMENTS:
|
|
|
|
case ElementsKind::PACKED_DOUBLE_ELEMENTS:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-02 15:14:16 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
//
|
|
|
|
// Tests
|
|
|
|
//
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, SystemPointerElementsKind) {
|
2018-12-27 03:04:28 +00:00
|
|
|
CHECK_EQ(ElementsKindToShiftSize(SYSTEM_POINTER_ELEMENTS),
|
|
|
|
kSystemPointerSizeLog2);
|
|
|
|
CHECK_EQ(ElementsKindToByteSize(SYSTEM_POINTER_ELEMENTS), kSystemPointerSize);
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, JSObjectAddingProperties) {
|
|
|
|
Factory* factory = i_isolate()->factory();
|
|
|
|
v8::HandleScope scope(isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<FixedArray> empty_fixed_array(factory->empty_fixed_array());
|
2017-07-17 23:30:32 +00:00
|
|
|
Handle<PropertyArray> empty_property_array(factory->empty_property_array());
|
2017-11-08 12:56:08 +00:00
|
|
|
Handle<JSFunction> function =
|
2020-11-10 11:22:49 +00:00
|
|
|
factory->NewFunctionForTesting(factory->empty_string());
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Object> value(Smi::FromInt(42), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<JSObject> object = factory->NewJSObject(function);
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Map> previous_map(object->map(), i_isolate());
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, previous_map->elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), object->property_array(), empty_property_array));
|
|
|
|
CHECK(EQUALS(i_isolate(), object->elements(), empty_fixed_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// for the default constructor function no in-object properties are reserved
|
|
|
|
// hence adding a single property will initialize the property-array
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<String> name = MakeName("property", 0);
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(object, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_NE(object->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, object->map().elements_kind());
|
2017-07-11 20:56:40 +00:00
|
|
|
CHECK_LE(1, object->property_array().length());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), object->elements(), empty_fixed_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, JSObjectInObjectAddingProperties) {
|
|
|
|
Factory* factory = i_isolate()->factory();
|
|
|
|
v8::HandleScope scope(isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<FixedArray> empty_fixed_array(factory->empty_fixed_array());
|
2017-07-17 23:30:32 +00:00
|
|
|
Handle<PropertyArray> empty_property_array(factory->empty_property_array());
|
2017-11-08 12:56:08 +00:00
|
|
|
Handle<JSFunction> function =
|
2020-11-10 11:22:49 +00:00
|
|
|
factory->NewFunctionForTesting(factory->empty_string());
|
2015-10-02 15:14:16 +00:00
|
|
|
int nof_inobject_properties = 10;
|
|
|
|
// force in object properties by changing the expected_nof_properties
|
2019-02-19 05:30:10 +00:00
|
|
|
// (we always reserve 8 inobject properties slack on top).
|
|
|
|
function->shared().set_expected_nof_properties(nof_inobject_properties - 8);
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Object> value(Smi::FromInt(42), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<JSObject> object = factory->NewJSObject(function);
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Map> previous_map(object->map(), i_isolate());
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, previous_map->elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), object->property_array(), empty_property_array));
|
|
|
|
CHECK(EQUALS(i_isolate(), object->elements(), empty_fixed_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// we have reserved space for in-object properties, hence adding up to
|
|
|
|
// |nof_inobject_properties| will not create a property store
|
|
|
|
for (int i = 0; i < nof_inobject_properties; i++) {
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<String> name = MakeName("property", i);
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(object, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
}
|
|
|
|
CHECK_NE(object->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, object->map().elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), object->property_array(), empty_property_array));
|
|
|
|
CHECK(EQUALS(i_isolate(), object->elements(), empty_fixed_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// adding one more property will not fit in the in-object properties, thus
|
|
|
|
// creating a property store
|
|
|
|
int index = nof_inobject_properties + 1;
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<String> name = MakeName("property", index);
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(object, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_NE(object->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, object->map().elements_kind());
|
2015-10-02 15:14:16 +00:00
|
|
|
// there must be at least 1 element in the properies store
|
2017-07-11 20:56:40 +00:00
|
|
|
CHECK_LE(1, object->property_array().length());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), object->elements(), empty_fixed_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, JSObjectAddingElements) {
|
|
|
|
Factory* factory = i_isolate()->factory();
|
|
|
|
v8::HandleScope scope(isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<String> name;
|
|
|
|
Handle<FixedArray> empty_fixed_array(factory->empty_fixed_array());
|
2017-07-17 23:30:32 +00:00
|
|
|
Handle<PropertyArray> empty_property_array(factory->empty_property_array());
|
2017-11-08 12:56:08 +00:00
|
|
|
Handle<JSFunction> function =
|
2020-11-10 11:22:49 +00:00
|
|
|
factory->NewFunctionForTesting(factory->empty_string());
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Object> value(Smi::FromInt(42), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<JSObject> object = factory->NewJSObject(function);
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Map> previous_map(object->map(), i_isolate());
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, previous_map->elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), object->property_array(), empty_property_array));
|
|
|
|
CHECK(EQUALS(i_isolate(), object->elements(), empty_fixed_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// Adding an indexed element initializes the elements array
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(object, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
// no change in elements_kind => no map transition
|
|
|
|
CHECK_EQ(object->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, object->map().elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), object->property_array(), empty_property_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
CHECK_LE(1, object->elements().length());
|
|
|
|
|
|
|
|
// Adding more consecutive elements without a change in the backing store
|
|
|
|
int non_dict_backing_store_limit = 100;
|
|
|
|
for (int i = 1; i < non_dict_backing_store_limit; i++) {
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeName("", i);
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(object, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
}
|
|
|
|
// no change in elements_kind => no map transition
|
|
|
|
CHECK_EQ(object->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, object->map().elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), object->property_array(), empty_property_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
CHECK_LE(non_dict_backing_store_limit, object->elements().length());
|
|
|
|
|
|
|
|
// Adding an element at an very large index causes a change to
|
|
|
|
// DICTIONARY_ELEMENTS
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("100000000");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(object, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
// change in elements_kind => map transition
|
|
|
|
CHECK_NE(object->map(), *previous_map);
|
2017-02-07 14:20:02 +00:00
|
|
|
CHECK_EQ(DICTIONARY_ELEMENTS, object->map().elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), object->property_array(), empty_property_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
CHECK_LE(non_dict_backing_store_limit, object->elements().length());
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, JSArrayAddingProperties) {
|
|
|
|
Factory* factory = i_isolate()->factory();
|
|
|
|
v8::HandleScope scope(isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<FixedArray> empty_fixed_array(factory->empty_fixed_array());
|
2017-07-17 23:30:32 +00:00
|
|
|
Handle<PropertyArray> empty_property_array(factory->empty_property_array());
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Object> value(Smi::FromInt(42), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<JSArray> array =
|
2017-06-30 11:26:14 +00:00
|
|
|
factory->NewJSArray(ElementsKind::PACKED_SMI_ELEMENTS, 0, 0);
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Map> previous_map(array->map(), i_isolate());
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_SMI_ELEMENTS, previous_map->elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), array->property_array(), empty_property_array));
|
|
|
|
CHECK(EQUALS(i_isolate(), array->elements(), empty_fixed_array));
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(0, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// for the default constructor function no in-object properties are reserved
|
|
|
|
// hence adding a single property will initialize the property-array
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<String> name = MakeName("property", 0);
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
// No change in elements_kind but added property => new map
|
|
|
|
CHECK_NE(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_SMI_ELEMENTS, array->map().elements_kind());
|
2017-07-11 20:56:40 +00:00
|
|
|
CHECK_LE(1, array->property_array().length());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), array->elements(), empty_fixed_array));
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(0, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, JSArrayAddingElements) {
|
|
|
|
Factory* factory = i_isolate()->factory();
|
|
|
|
v8::HandleScope scope(isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<String> name;
|
|
|
|
Handle<FixedArray> empty_fixed_array(factory->empty_fixed_array());
|
2017-07-17 23:30:32 +00:00
|
|
|
Handle<PropertyArray> empty_property_array(factory->empty_property_array());
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Object> value(Smi::FromInt(42), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<JSArray> array =
|
2017-06-30 11:26:14 +00:00
|
|
|
factory->NewJSArray(ElementsKind::PACKED_SMI_ELEMENTS, 0, 0);
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Map> previous_map(array->map(), i_isolate());
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_SMI_ELEMENTS, previous_map->elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), array->property_array(), empty_property_array));
|
|
|
|
CHECK(EQUALS(i_isolate(), array->elements(), empty_fixed_array));
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(0, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// Adding an indexed element initializes the elements array
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
// no change in elements_kind => no map transition
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_SMI_ELEMENTS, array->map().elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), array->property_array(), empty_property_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
CHECK_LE(1, array->elements().length());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(1, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// Adding more consecutive elements without a change in the backing store
|
|
|
|
int non_dict_backing_store_limit = 100;
|
|
|
|
for (int i = 1; i < non_dict_backing_store_limit; i++) {
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeName("", i);
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
}
|
|
|
|
// no change in elements_kind => no map transition
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_SMI_ELEMENTS, array->map().elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), array->property_array(), empty_property_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
CHECK_LE(non_dict_backing_store_limit, array->elements().length());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(non_dict_backing_store_limit, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// Adding an element at an very large index causes a change to
|
|
|
|
// DICTIONARY_ELEMENTS
|
|
|
|
int index = 100000000;
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeName("", index);
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value, NONE)
|
|
|
|
.Check();
|
|
|
|
// change in elements_kind => map transition
|
|
|
|
CHECK_NE(array->map(), *previous_map);
|
2017-02-07 14:20:02 +00:00
|
|
|
CHECK_EQ(DICTIONARY_ELEMENTS, array->map().elements_kind());
|
2022-04-28 03:12:49 +00:00
|
|
|
CHECK(EQUALS(i_isolate(), array->property_array(), empty_property_array));
|
2015-10-02 15:14:16 +00:00
|
|
|
CHECK_LE(non_dict_backing_store_limit, array->elements().length());
|
|
|
|
CHECK_LE(array->elements().length(), index);
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(index + 1, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, JSArrayAddingElementsGeneralizingiFastSmiElements) {
|
|
|
|
Factory* factory = i_isolate()->factory();
|
|
|
|
v8::HandleScope scope(isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<String> name;
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Object> value_smi(Smi::FromInt(42), i_isolate());
|
|
|
|
Handle<Object> value_string(MakeString("value"));
|
2015-10-02 15:14:16 +00:00
|
|
|
Handle<Object> value_double = factory->NewNumber(3.1415);
|
|
|
|
|
|
|
|
Handle<JSArray> array =
|
2017-06-30 11:26:14 +00:00
|
|
|
factory->NewJSArray(ElementsKind::PACKED_SMI_ELEMENTS, 0, 0);
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Map> previous_map(array->map(), i_isolate());
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_SMI_ELEMENTS, previous_map->elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(0, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// `array[0] = smi_value` doesn't change the elements_kind
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_smi,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
// no change in elements_kind => no map transition
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_SMI_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(1, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// `delete array[0]` does not alter length, but changes the elments_kind
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-11-26 09:34:11 +00:00
|
|
|
CHECK(JSReceiver::DeletePropertyOrElement(array, name).FromMaybe(false));
|
2015-10-02 15:14:16 +00:00
|
|
|
CHECK_NE(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_SMI_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(1, Smi::ToInt(array->length()));
|
2022-04-28 03:12:49 +00:00
|
|
|
previous_map = handle(array->map(), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// add a couple of elements again
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_smi,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("1");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_smi,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_SMI_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(2, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// Adding a string to the array changes from FAST_HOLEY_SMI to FAST_HOLEY
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_string,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_NE(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(2, Smi::ToInt(array->length()));
|
2022-04-28 03:12:49 +00:00
|
|
|
previous_map = handle(array->map(), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// We don't transition back to FAST_SMI even if we remove the string
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_smi,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
|
|
|
|
|
|
|
// Adding a double doesn't change the map either
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_double,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, JSArrayAddingElementsGeneralizingFastElements) {
|
|
|
|
Factory* factory = i_isolate()->factory();
|
|
|
|
v8::HandleScope scope(isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<String> name;
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Object> value_smi(Smi::FromInt(42), i_isolate());
|
|
|
|
Handle<Object> value_string(MakeString("value"));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<JSArray> array =
|
2017-06-30 11:26:14 +00:00
|
|
|
factory->NewJSArray(ElementsKind::PACKED_ELEMENTS, 0, 0);
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Map> previous_map(array->map(), i_isolate());
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_ELEMENTS, previous_map->elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(0, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// `array[0] = smi_value` doesn't change the elements_kind
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_smi,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
// no change in elements_kind => no map transition
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(1, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// `delete array[0]` does not alter length, but changes the elments_kind
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-11-26 09:34:11 +00:00
|
|
|
CHECK(JSReceiver::DeletePropertyOrElement(array, name).FromMaybe(false));
|
2015-10-02 15:14:16 +00:00
|
|
|
CHECK_NE(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(1, Smi::ToInt(array->length()));
|
2022-04-28 03:12:49 +00:00
|
|
|
previous_map = handle(array->map(), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// add a couple of elements, elements_kind stays HOLEY
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_string,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("1");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_smi,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(2, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, JSArrayAddingElementsGeneralizingiFastDoubleElements) {
|
|
|
|
Factory* factory = i_isolate()->factory();
|
|
|
|
v8::HandleScope scope(isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
Handle<String> name;
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Object> value_smi(Smi::FromInt(42), i_isolate());
|
|
|
|
Handle<Object> value_string(MakeString("value"));
|
2015-10-02 15:14:16 +00:00
|
|
|
Handle<Object> value_double = factory->NewNumber(3.1415);
|
|
|
|
|
|
|
|
Handle<JSArray> array =
|
2017-06-30 11:26:14 +00:00
|
|
|
factory->NewJSArray(ElementsKind::PACKED_SMI_ELEMENTS, 0, 0);
|
2022-04-28 03:12:49 +00:00
|
|
|
Handle<Map> previous_map(array->map(), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
2017-06-30 11:26:14 +00:00
|
|
|
// `array[0] = value_double` changes |elements_kind| to PACKED_DOUBLE_ELEMENTS
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_double,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_NE(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_DOUBLE_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(1, Smi::ToInt(array->length()));
|
2022-04-28 03:12:49 +00:00
|
|
|
previous_map = handle(array->map(), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// `array[1] = value_smi` doesn't alter the |elements_kind|
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("1");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_smi,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(PACKED_DOUBLE_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(2, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// `delete array[0]` does not alter length, but changes the elments_kind
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-11-26 09:34:11 +00:00
|
|
|
CHECK(JSReceiver::DeletePropertyOrElement(array, name).FromMaybe(false));
|
2015-10-02 15:14:16 +00:00
|
|
|
CHECK_NE(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_DOUBLE_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(2, Smi::ToInt(array->length()));
|
2022-04-28 03:12:49 +00:00
|
|
|
previous_map = handle(array->map(), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// filling the hole `array[0] = value_smi` again doesn't transition back
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_double,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_DOUBLE_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(2, Smi::ToInt(array->length()));
|
2015-10-02 15:14:16 +00:00
|
|
|
|
2017-06-30 11:26:14 +00:00
|
|
|
// Adding a string to the array changes to elements_kind PACKED_ELEMENTS
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("1");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_string,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_NE(array->map(), *previous_map);
|
2017-06-30 11:26:14 +00:00
|
|
|
CHECK_EQ(HOLEY_ELEMENTS, array->map().elements_kind());
|
2017-07-10 12:58:27 +00:00
|
|
|
CHECK_EQ(2, Smi::ToInt(array->length()));
|
2022-04-28 03:12:49 +00:00
|
|
|
previous_map = handle(array->map(), i_isolate());
|
2015-10-02 15:14:16 +00:00
|
|
|
|
|
|
|
// Adding a double doesn't change the map
|
2022-04-28 03:12:49 +00:00
|
|
|
name = MakeString("0");
|
2015-10-02 15:14:16 +00:00
|
|
|
JSObject::DefinePropertyOrElementIgnoreAttributes(array, name, value_double,
|
|
|
|
NONE)
|
|
|
|
.Check();
|
|
|
|
CHECK_EQ(array->map(), *previous_map);
|
|
|
|
}
|
2017-08-11 11:22:28 +00:00
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, IsHoleyElementsKindForRead) {
|
2019-05-07 20:28:55 +00:00
|
|
|
for (int i = 0; i <= ElementsKind::LAST_ELEMENTS_KIND; i++) {
|
|
|
|
ElementsKind kind = static_cast<ElementsKind>(i);
|
|
|
|
CHECK_EQ(ElementsKindIsHoleyElementsKindForRead(kind),
|
|
|
|
IsHoleyElementsKindForRead(kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, IsHoleyElementsKind) {
|
2019-05-07 20:28:55 +00:00
|
|
|
for (int i = 0; i <= ElementsKind::LAST_ELEMENTS_KIND; i++) {
|
|
|
|
ElementsKind kind = static_cast<ElementsKind>(i);
|
|
|
|
CHECK_EQ(ElementsKindIsHoleyElementsKind(kind), IsHoleyElementsKind(kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:12:49 +00:00
|
|
|
TEST_F(ElementsKindTest, IsFastPackedElementsKind) {
|
2019-05-07 20:28:55 +00:00
|
|
|
for (int i = 0; i <= ElementsKind::LAST_ELEMENTS_KIND; i++) {
|
|
|
|
ElementsKind kind = static_cast<ElementsKind>(i);
|
|
|
|
CHECK_EQ(ElementsKindIsFastPackedElementsKind(kind),
|
|
|
|
IsFastPackedElementsKind(kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|