test-transitions.cc 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2014 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>
#include <utility>

#include "src/v8.h"

#include "src/compilation-cache.h"
#include "src/execution.h"
#include "src/factory.h"
13
#include "src/field-type.h"
14
#include "src/global-handles.h"
15 16
#include "src/objects-inl.h"
#include "src/transitions.h"
17
#include "test/cctest/cctest.h"
18
#include "test/cctest/test-transitions.h"
19

20 21
namespace v8 {
namespace internal {
22 23 24 25 26 27 28 29 30 31 32 33 34

TEST(TransitionArray_SimpleFieldTransitions) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();

  Handle<String> name1 = factory->InternalizeUtf8String("foo");
  Handle<String> name2 = factory->InternalizeUtf8String("bar");
  PropertyAttributes attributes = NONE;

  Handle<Map> map0 = Map::Create(isolate, 0);
  Handle<Map> map1 =
35
      Map::CopyWithField(map0, name1, handle(FieldType::Any(), isolate),
36 37
                         attributes, kMutable, Representation::Tagged(),
                         OMIT_TRANSITION)
38
          .ToHandleChecked();
39
  Handle<Map> map2 =
40
      Map::CopyWithField(map0, name2, handle(FieldType::Any(), isolate),
41 42
                         attributes, kMutable, Representation::Tagged(),
                         OMIT_TRANSITION)
43
          .ToHandleChecked();
44

45 46
  CHECK(map0->raw_transitions()->IsSmi());

47 48 49 50 51 52 53 54 55 56 57 58 59
  {
    TestTransitionsAccessor transitions(map0);
    transitions.Insert(name1, map1, SIMPLE_PROPERTY_TRANSITION);
  }
  {
    TestTransitionsAccessor transitions(map0);
    CHECK(transitions.IsWeakCellEncoding());
    CHECK_EQ(*map1, transitions.SearchTransition(*name1, kData, attributes));
    CHECK_EQ(1, transitions.NumberOfTransitions());
    CHECK_EQ(*name1, transitions.GetKey(0));
    CHECK_EQ(*map1, transitions.GetTarget(0));

    transitions.Insert(name2, map2, SIMPLE_PROPERTY_TRANSITION);
60
  }
61 62 63 64 65 66 67 68 69 70 71 72 73
  {
    TestTransitionsAccessor transitions(map0);
    CHECK(transitions.IsFullTransitionArrayEncoding());

    CHECK_EQ(*map1, transitions.SearchTransition(*name1, kData, attributes));
    CHECK_EQ(*map2, transitions.SearchTransition(*name2, kData, attributes));
    CHECK_EQ(2, transitions.NumberOfTransitions());
    for (int i = 0; i < 2; i++) {
      Name* key = transitions.GetKey(i);
      Map* target = transitions.GetTarget(i);
      CHECK((key == *name1 && target == *map1) ||
            (key == *name2 && target == *map2));
    }
74

75 76
    DCHECK(transitions.IsSortedNoDuplicates());
  }
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
}


TEST(TransitionArray_FullFieldTransitions) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();

  Handle<String> name1 = factory->InternalizeUtf8String("foo");
  Handle<String> name2 = factory->InternalizeUtf8String("bar");
  PropertyAttributes attributes = NONE;

  Handle<Map> map0 = Map::Create(isolate, 0);
  Handle<Map> map1 =
92
      Map::CopyWithField(map0, name1, handle(FieldType::Any(), isolate),
93 94
                         attributes, kMutable, Representation::Tagged(),
                         OMIT_TRANSITION)
95
          .ToHandleChecked();
96
  Handle<Map> map2 =
97
      Map::CopyWithField(map0, name2, handle(FieldType::Any(), isolate),
98 99
                         attributes, kMutable, Representation::Tagged(),
                         OMIT_TRANSITION)
100
          .ToHandleChecked();
101

102 103
  CHECK(map0->raw_transitions()->IsSmi());

104 105 106
  {
    TestTransitionsAccessor transitions(map0);
    transitions.Insert(name1, map1, PROPERTY_TRANSITION);
107
  }
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  {
    TestTransitionsAccessor transitions(map0);
    CHECK(transitions.IsFullTransitionArrayEncoding());
    CHECK_EQ(*map1, transitions.SearchTransition(*name1, kData, attributes));
    CHECK_EQ(1, transitions.NumberOfTransitions());
    CHECK_EQ(*name1, transitions.GetKey(0));
    CHECK_EQ(*map1, transitions.GetTarget(0));

    transitions.Insert(name2, map2, PROPERTY_TRANSITION);
  }
  {
    TestTransitionsAccessor transitions(map0);
    CHECK(transitions.IsFullTransitionArrayEncoding());

    CHECK_EQ(*map1, transitions.SearchTransition(*name1, kData, attributes));
    CHECK_EQ(*map2, transitions.SearchTransition(*name2, kData, attributes));
    CHECK_EQ(2, transitions.NumberOfTransitions());
    for (int i = 0; i < 2; i++) {
      Name* key = transitions.GetKey(i);
      Map* target = transitions.GetTarget(i);
      CHECK((key == *name1 && target == *map1) ||
            (key == *name2 && target == *map2));
    }
131

132 133
    DCHECK(transitions.IsSortedNoDuplicates());
  }
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
}


TEST(TransitionArray_DifferentFieldNames) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();

  const int PROPS_COUNT = 10;
  Handle<String> names[PROPS_COUNT];
  Handle<Map> maps[PROPS_COUNT];
  PropertyAttributes attributes = NONE;

  Handle<Map> map0 = Map::Create(isolate, 0);
149
  CHECK(map0->raw_transitions()->IsSmi());
150 151 152 153 154

  for (int i = 0; i < PROPS_COUNT; i++) {
    EmbeddedVector<char, 64> buffer;
    SNPrintF(buffer, "prop%d", i);
    Handle<String> name = factory->InternalizeUtf8String(buffer.start());
155 156 157 158 159
    Handle<Map> map =
        Map::CopyWithField(map0, name, handle(FieldType::Any(), isolate),
                           attributes, kMutable, Representation::Tagged(),
                           OMIT_TRANSITION)
            .ToHandleChecked();
160 161 162
    names[i] = name;
    maps[i] = map;

163
    TransitionsAccessor(map0).Insert(name, map, PROPERTY_TRANSITION);
164 165
  }

166
  TransitionsAccessor transitions(map0);
167
  for (int i = 0; i < PROPS_COUNT; i++) {
168 169
    CHECK_EQ(*maps[i],
             transitions.SearchTransition(*names[i], kData, attributes));
170 171
  }
  for (int i = 0; i < PROPS_COUNT; i++) {
172 173
    Name* key = transitions.GetKey(i);
    Map* target = transitions.GetTarget(i);
174 175 176 177 178 179
    for (int j = 0; j < PROPS_COUNT; j++) {
      if (*names[i] == key) {
        CHECK_EQ(*maps[i], target);
        break;
      }
    }
180 181
  }

182
  DCHECK(transitions.IsSortedNoDuplicates());
183 184 185 186 187 188 189 190 191 192
}


TEST(TransitionArray_SameFieldNamesDifferentAttributesSimple) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();

  Handle<Map> map0 = Map::Create(isolate, 0);
193
  CHECK(map0->raw_transitions()->IsSmi());
194 195 196 197 198 199 200 201 202 203

  const int ATTRS_COUNT = (READ_ONLY | DONT_ENUM | DONT_DELETE) + 1;
  STATIC_ASSERT(ATTRS_COUNT == 8);
  Handle<Map> attr_maps[ATTRS_COUNT];
  Handle<String> name = factory->InternalizeUtf8String("foo");

  // Add transitions for same field name but different attributes.
  for (int i = 0; i < ATTRS_COUNT; i++) {
    PropertyAttributes attributes = static_cast<PropertyAttributes>(i);

204 205 206 207
    Handle<Map> map =
        Map::CopyWithField(map0, name, FieldType::Any(isolate), attributes,
                           kMutable, Representation::Tagged(), OMIT_TRANSITION)
            .ToHandleChecked();
208 209
    attr_maps[i] = map;

210
    TransitionsAccessor(map0).Insert(name, map, PROPERTY_TRANSITION);
211 212 213
  }

  // Ensure that transitions for |name| field are valid.
214
  TransitionsAccessor transitions(map0);
215 216
  for (int i = 0; i < ATTRS_COUNT; i++) {
    PropertyAttributes attributes = static_cast<PropertyAttributes>(i);
217 218
    CHECK_EQ(*attr_maps[i],
             transitions.SearchTransition(*name, kData, attributes));
219 220
    // All transitions use the same key, so this check doesn't need to
    // care about ordering.
221
    CHECK_EQ(*name, transitions.GetKey(i));
222 223
  }

224
  DCHECK(transitions.IsSortedNoDuplicates());
225 226 227 228 229 230 231 232 233 234 235 236 237 238
}


TEST(TransitionArray_SameFieldNamesDifferentAttributes) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();

  const int PROPS_COUNT = 10;
  Handle<String> names[PROPS_COUNT];
  Handle<Map> maps[PROPS_COUNT];

  Handle<Map> map0 = Map::Create(isolate, 0);
239
  CHECK(map0->raw_transitions()->IsSmi());
240 241 242 243 244 245 246

  // Some number of fields.
  for (int i = 0; i < PROPS_COUNT; i++) {
    EmbeddedVector<char, 64> buffer;
    SNPrintF(buffer, "prop%d", i);
    Handle<String> name = factory->InternalizeUtf8String(buffer.start());
    Handle<Map> map =
247
        Map::CopyWithField(map0, name, handle(FieldType::Any(), isolate), NONE,
248
                           kMutable, Representation::Tagged(), OMIT_TRANSITION)
249
            .ToHandleChecked();
250 251 252
    names[i] = name;
    maps[i] = map;

253
    TransitionsAccessor(map0).Insert(name, map, PROPERTY_TRANSITION);
254 255 256 257 258 259 260 261 262 263 264
  }

  const int ATTRS_COUNT = (READ_ONLY | DONT_ENUM | DONT_DELETE) + 1;
  STATIC_ASSERT(ATTRS_COUNT == 8);
  Handle<Map> attr_maps[ATTRS_COUNT];
  Handle<String> name = factory->InternalizeUtf8String("foo");

  // Add transitions for same field name but different attributes.
  for (int i = 0; i < ATTRS_COUNT; i++) {
    PropertyAttributes attributes = static_cast<PropertyAttributes>(i);

265 266 267 268 269
    Handle<Map> map =
        Map::CopyWithField(map0, name, handle(FieldType::Any(), isolate),
                           attributes, kMutable, Representation::Tagged(),
                           OMIT_TRANSITION)
            .ToHandleChecked();
270 271
    attr_maps[i] = map;

272
    TransitionsAccessor(map0).Insert(name, map, PROPERTY_TRANSITION);
273 274 275
  }

  // Ensure that transitions for |name| field are valid.
276
  TransitionsAccessor transitions(map0);
277
  for (int i = 0; i < ATTRS_COUNT; i++) {
278
    PropertyAttributes attr = static_cast<PropertyAttributes>(i);
279
    CHECK_EQ(*attr_maps[i], transitions.SearchTransition(*name, kData, attr));
280 281 282
  }

  // Ensure that info about the other fields still valid.
283
  CHECK_EQ(PROPS_COUNT + ATTRS_COUNT, transitions.NumberOfTransitions());
284
  for (int i = 0; i < PROPS_COUNT + ATTRS_COUNT; i++) {
285 286
    Name* key = transitions.GetKey(i);
    Map* target = transitions.GetTarget(i);
287 288 289 290 291 292 293 294 295 296 297 298 299
    if (key == *name) {
      // Attributes transition.
      PropertyAttributes attributes =
          target->GetLastDescriptorDetails().attributes();
      CHECK_EQ(*attr_maps[static_cast<int>(attributes)], target);
    } else {
      for (int j = 0; j < PROPS_COUNT; j++) {
        if (*names[j] == key) {
          CHECK_EQ(*maps[j], target);
          break;
        }
      }
    }
300 301
  }

302
  DCHECK(transitions.IsSortedNoDuplicates());
303
}
304 305 306

}  // namespace internal
}  // namespace v8