test-object.cc 16.7 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5
#include "src/api/api-inl.h"
6
#include "src/common/globals.h"
7
#include "src/execution/isolate.h"
8
#include "src/handles/handles-inl.h"
9
#include "src/heap/factory.h"
10
#include "src/init/v8.h"
11 12
#include "src/objects/function-kind.h"
#include "src/objects/objects-inl.h"
13 14
#include "test/cctest/cctest.h"

15 16
namespace v8 {
namespace internal {
17 18 19

static void CheckObject(Isolate* isolate, Handle<Object> obj,
                        const char* string) {
20 21 22
  Handle<String> print_string = String::Flatten(
      isolate,
      Handle<String>::cast(Object::NoSideEffectsToString(isolate, obj)));
23
  CHECK(print_string->IsOneByteEqualTo(base::CStrVector(string)));
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
}

static void CheckSmi(Isolate* isolate, int value, const char* string) {
  Handle<Object> handle(Smi::FromInt(value), isolate);
  CheckObject(isolate, handle, string);
}

static void CheckString(Isolate* isolate, const char* value,
                        const char* string) {
  Handle<String> handle(isolate->factory()->NewStringFromAsciiChecked(value));
  CheckObject(isolate, handle, string);
}

static void CheckNumber(Isolate* isolate, double value, const char* string) {
  Handle<Object> number = isolate->factory()->NewNumber(value);
  CHECK(number->IsNumber());
  CheckObject(isolate, number, string);
}

static void CheckBoolean(Isolate* isolate, bool value, const char* string) {
  CheckObject(isolate, value ? isolate->factory()->true_value()
                             : isolate->factory()->false_value(),
              string);
}

TEST(NoSideEffectsToString) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();

  HandleScope scope(isolate);

  CheckString(isolate, "fisk hest", "fisk hest");
  CheckNumber(isolate, 42.3, "42.3");
  CheckSmi(isolate, 42, "42");
  CheckBoolean(isolate, true, "true");
  CheckBoolean(isolate, false, "false");
  CheckBoolean(isolate, false, "false");
62 63 64
  Handle<Object> smi_42 = handle(Smi::FromInt(42), isolate);
  CheckObject(isolate, BigInt::FromNumber(isolate, smi_42).ToHandleChecked(),
              "42");
65 66 67 68
  CheckObject(isolate, factory->undefined_value(), "undefined");
  CheckObject(isolate, factory->null_value(), "null");

  CheckObject(isolate, factory->error_to_string(), "[object Error]");
69 70
  CheckObject(isolate, factory->unscopables_symbol(),
              "Symbol(Symbol.unscopables)");
71 72 73 74 75 76 77 78 79
  CheckObject(isolate, factory->NewError(isolate->error_function(),
                                         factory->empty_string()),
              "Error");
  CheckObject(isolate, factory->NewError(
                           isolate->error_function(),
                           factory->NewStringFromAsciiChecked("fisk hest")),
              "Error: fisk hest");
  CheckObject(isolate, factory->NewJSObject(isolate->object_function()),
              "#<Object>");
80 81 82 83 84
  CheckObject(
      isolate,
      factory->NewJSProxy(factory->NewJSObject(isolate->object_function()),
                          factory->NewJSObject(isolate->object_function())),
      "#<Object>");
85
}
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
TEST(EnumCache) {
  LocalContext env;
  v8::Isolate* isolate = env->GetIsolate();
  i::Factory* factory = CcTest::i_isolate()->factory();
  v8::HandleScope scope(isolate);

  // Create a nice transition tree:
  // (a) --> (b) --> (c)   shared DescriptorArray 1
  //          |
  //          +---> (cc)   shared DescriptorArray 2
  CompileRun(
      "function O(a) { this.a = 1 };"

      "a = new O();"

      "b = new O();"
      "b.b = 2;"

      "c = new O();"
      "c.b = 2;"
      "c.c = 3;"

      "cc = new O();"
      "cc.b = 2;"
      "cc.cc = 4;");

  Handle<JSObject> a = Handle<JSObject>::cast(v8::Utils::OpenHandle(
      *env->Global()->Get(env.local(), v8_str("a")).ToLocalChecked()));
  Handle<JSObject> b = Handle<JSObject>::cast(v8::Utils::OpenHandle(
      *env->Global()->Get(env.local(), v8_str("b")).ToLocalChecked()));
  Handle<JSObject> c = Handle<JSObject>::cast(v8::Utils::OpenHandle(
      *env->Global()->Get(env.local(), v8_str("c")).ToLocalChecked()));
  Handle<JSObject> cc = Handle<JSObject>::cast(v8::Utils::OpenHandle(
      *env->Global()->Get(env.local(), v8_str("cc")).ToLocalChecked()));

  // Check the transition tree.
123 124 125 126
  CHECK_EQ(a->map().instance_descriptors(), b->map().instance_descriptors());
  CHECK_EQ(b->map().instance_descriptors(), c->map().instance_descriptors());
  CHECK_NE(c->map().instance_descriptors(), cc->map().instance_descriptors());
  CHECK_NE(b->map().instance_descriptors(), cc->map().instance_descriptors());
127 128

  // Check that the EnumLength is unset.
129 130 131 132
  CHECK_EQ(a->map().EnumLength(), kInvalidEnumCacheSentinel);
  CHECK_EQ(b->map().EnumLength(), kInvalidEnumCacheSentinel);
  CHECK_EQ(c->map().EnumLength(), kInvalidEnumCacheSentinel);
  CHECK_EQ(cc->map().EnumLength(), kInvalidEnumCacheSentinel);
133 134

  // Check that the EnumCache is empty.
135
  CHECK_EQ(a->map().instance_descriptors().enum_cache(),
136
           *factory->empty_enum_cache());
137
  CHECK_EQ(b->map().instance_descriptors().enum_cache(),
138
           *factory->empty_enum_cache());
139
  CHECK_EQ(c->map().instance_descriptors().enum_cache(),
140
           *factory->empty_enum_cache());
141
  CHECK_EQ(cc->map().instance_descriptors().enum_cache(),
142 143 144 145 146 147
           *factory->empty_enum_cache());

  // The EnumCache is shared on the DescriptorArray, creating it on {cc} has no
  // effect on the other maps.
  CompileRun("var s = 0; for (let key in cc) { s += cc[key] };");
  {
148 149 150 151
    CHECK_EQ(a->map().EnumLength(), kInvalidEnumCacheSentinel);
    CHECK_EQ(b->map().EnumLength(), kInvalidEnumCacheSentinel);
    CHECK_EQ(c->map().EnumLength(), kInvalidEnumCacheSentinel);
    CHECK_EQ(cc->map().EnumLength(), 3);
152

153
    CHECK_EQ(a->map().instance_descriptors().enum_cache(),
154
             *factory->empty_enum_cache());
155
    CHECK_EQ(b->map().instance_descriptors().enum_cache(),
156
             *factory->empty_enum_cache());
157
    CHECK_EQ(c->map().instance_descriptors().enum_cache(),
158 159
             *factory->empty_enum_cache());

160
    EnumCache enum_cache = cc->map().instance_descriptors().enum_cache();
161
    CHECK_NE(enum_cache, *factory->empty_enum_cache());
162 163
    CHECK_EQ(enum_cache.keys().length(), 3);
    CHECK_EQ(enum_cache.indices().length(), 3);
164 165 166 167 168 169
  }

  // Initializing the EnumCache for the the topmost map {a} will not create the
  // cache for the other maps.
  CompileRun("var s = 0; for (let key in a) { s += a[key] };");
  {
170 171 172 173
    CHECK_EQ(a->map().EnumLength(), 1);
    CHECK_EQ(b->map().EnumLength(), kInvalidEnumCacheSentinel);
    CHECK_EQ(c->map().EnumLength(), kInvalidEnumCacheSentinel);
    CHECK_EQ(cc->map().EnumLength(), 3);
174 175 176

    // The enum cache is shared on the descriptor array of maps {a}, {b} and
    // {c} only.
177
    EnumCache enum_cache = a->map().instance_descriptors().enum_cache();
178
    CHECK_NE(enum_cache, *factory->empty_enum_cache());
179
    CHECK_NE(cc->map().instance_descriptors().enum_cache(),
180
             *factory->empty_enum_cache());
181 182 183 184
    CHECK_NE(cc->map().instance_descriptors().enum_cache(), enum_cache);
    CHECK_EQ(a->map().instance_descriptors().enum_cache(), enum_cache);
    CHECK_EQ(b->map().instance_descriptors().enum_cache(), enum_cache);
    CHECK_EQ(c->map().instance_descriptors().enum_cache(), enum_cache);
185

186 187
    CHECK_EQ(enum_cache.keys().length(), 1);
    CHECK_EQ(enum_cache.indices().length(), 1);
188 189 190 191 192
  }

  // Creating the EnumCache for {c} will create a new EnumCache on the shared
  // DescriptorArray.
  Handle<EnumCache> previous_enum_cache(
193
      a->map().instance_descriptors().enum_cache(), a->GetIsolate());
194 195 196 197
  Handle<FixedArray> previous_keys(previous_enum_cache->keys(),
                                   a->GetIsolate());
  Handle<FixedArray> previous_indices(previous_enum_cache->indices(),
                                      a->GetIsolate());
198 199
  CompileRun("var s = 0; for (let key in c) { s += c[key] };");
  {
200 201 202 203
    CHECK_EQ(a->map().EnumLength(), 1);
    CHECK_EQ(b->map().EnumLength(), kInvalidEnumCacheSentinel);
    CHECK_EQ(c->map().EnumLength(), 3);
    CHECK_EQ(cc->map().EnumLength(), 3);
204

205
    EnumCache enum_cache = c->map().instance_descriptors().enum_cache();
206 207 208
    CHECK_NE(enum_cache, *factory->empty_enum_cache());
    // The keys and indices caches are updated.
    CHECK_EQ(enum_cache, *previous_enum_cache);
209 210
    CHECK_NE(enum_cache.keys(), *previous_keys);
    CHECK_NE(enum_cache.indices(), *previous_indices);
211 212
    CHECK_EQ(previous_keys->length(), 1);
    CHECK_EQ(previous_indices->length(), 1);
213 214
    CHECK_EQ(enum_cache.keys().length(), 3);
    CHECK_EQ(enum_cache.indices().length(), 3);
215 216 217

    // The enum cache is shared on the descriptor array of maps {a}, {b} and
    // {c} only.
218
    CHECK_NE(cc->map().instance_descriptors().enum_cache(),
219
             *factory->empty_enum_cache());
220 221
    CHECK_NE(cc->map().instance_descriptors().enum_cache(), enum_cache);
    CHECK_NE(cc->map().instance_descriptors().enum_cache(),
222
             *previous_enum_cache);
223 224 225
    CHECK_EQ(a->map().instance_descriptors().enum_cache(), enum_cache);
    CHECK_EQ(b->map().instance_descriptors().enum_cache(), enum_cache);
    CHECK_EQ(c->map().instance_descriptors().enum_cache(), enum_cache);
226 227 228 229 230
  }

  // {b} can reuse the existing EnumCache, hence we only need to set the correct
  // EnumLength on the map without modifying the cache itself.
  previous_enum_cache =
231
      handle(a->map().instance_descriptors().enum_cache(), a->GetIsolate());
232 233
  previous_keys = handle(previous_enum_cache->keys(), a->GetIsolate());
  previous_indices = handle(previous_enum_cache->indices(), a->GetIsolate());
234 235
  CompileRun("var s = 0; for (let key in b) { s += b[key] };");
  {
236 237 238 239
    CHECK_EQ(a->map().EnumLength(), 1);
    CHECK_EQ(b->map().EnumLength(), 2);
    CHECK_EQ(c->map().EnumLength(), 3);
    CHECK_EQ(cc->map().EnumLength(), 3);
240

241
    EnumCache enum_cache = c->map().instance_descriptors().enum_cache();
242 243 244
    CHECK_NE(enum_cache, *factory->empty_enum_cache());
    // The keys and indices caches are not updated.
    CHECK_EQ(enum_cache, *previous_enum_cache);
245 246 247 248
    CHECK_EQ(enum_cache.keys(), *previous_keys);
    CHECK_EQ(enum_cache.indices(), *previous_indices);
    CHECK_EQ(enum_cache.keys().length(), 3);
    CHECK_EQ(enum_cache.indices().length(), 3);
249 250 251

    // The enum cache is shared on the descriptor array of maps {a}, {b} and
    // {c} only.
252
    CHECK_NE(cc->map().instance_descriptors().enum_cache(),
253
             *factory->empty_enum_cache());
254 255
    CHECK_NE(cc->map().instance_descriptors().enum_cache(), enum_cache);
    CHECK_NE(cc->map().instance_descriptors().enum_cache(),
256
             *previous_enum_cache);
257 258 259
    CHECK_EQ(a->map().instance_descriptors().enum_cache(), enum_cache);
    CHECK_EQ(b->map().instance_descriptors().enum_cache(), enum_cache);
    CHECK_EQ(c->map().instance_descriptors().enum_cache(), enum_cache);
260 261 262
  }
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
TEST(ObjectMethodsThatTruncateMinusZero) {
  LocalContext env;
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();
  v8::HandleScope scope(env->GetIsolate());

  Handle<Object> minus_zero = factory->NewNumber(-1.0 * 0.0);
  CHECK(minus_zero->IsMinusZero());

  Handle<Object> result =
      Object::ToInteger(isolate, minus_zero).ToHandleChecked();
  CHECK(result->IsZero());

  result = Object::ToLength(isolate, minus_zero).ToHandleChecked();
  CHECK(result->IsZero());

  // Choose an error message template, doesn't matter which.
  result = Object::ToIndex(isolate, minus_zero,
                           MessageTemplate::kInvalidAtomicAccessIndex)
               .ToHandleChecked();
  CHECK(result->IsZero());
}

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
#define TEST_FUNCTION_KIND(Name)                                \
  TEST(Name) {                                                  \
    for (int i = 0; i < FunctionKind::kLastFunctionKind; i++) { \
      FunctionKind kind = static_cast<FunctionKind>(i);         \
      CHECK_EQ(FunctionKind##Name(kind), Name(kind));           \
    }                                                           \
  }

bool FunctionKindIsArrowFunction(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kArrowFunction:
    case FunctionKind::kAsyncArrowFunction:
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsArrowFunction)

bool FunctionKindIsAsyncGeneratorFunction(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kAsyncConciseGeneratorMethod:
308
    case FunctionKind::kStaticAsyncConciseGeneratorMethod:
309 310 311 312 313 314 315 316 317 318 319
    case FunctionKind::kAsyncGeneratorFunction:
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsAsyncGeneratorFunction)

bool FunctionKindIsGeneratorFunction(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kConciseGeneratorMethod:
320
    case FunctionKind::kStaticConciseGeneratorMethod:
321
    case FunctionKind::kAsyncConciseGeneratorMethod:
322
    case FunctionKind::kStaticAsyncConciseGeneratorMethod:
323 324 325 326 327 328 329 330 331 332 333 334 335 336
    case FunctionKind::kGeneratorFunction:
    case FunctionKind::kAsyncGeneratorFunction:
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsGeneratorFunction)

bool FunctionKindIsAsyncFunction(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kAsyncFunction:
    case FunctionKind::kAsyncArrowFunction:
    case FunctionKind::kAsyncConciseMethod:
337
    case FunctionKind::kStaticAsyncConciseMethod:
338
    case FunctionKind::kAsyncConciseGeneratorMethod:
339
    case FunctionKind::kStaticAsyncConciseGeneratorMethod:
340 341 342 343 344 345 346 347 348 349 350
    case FunctionKind::kAsyncGeneratorFunction:
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsAsyncFunction)

bool FunctionKindIsConciseMethod(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kConciseMethod:
351
    case FunctionKind::kStaticConciseMethod:
352
    case FunctionKind::kConciseGeneratorMethod:
353
    case FunctionKind::kStaticConciseGeneratorMethod:
354
    case FunctionKind::kAsyncConciseMethod:
355
    case FunctionKind::kStaticAsyncConciseMethod:
356
    case FunctionKind::kAsyncConciseGeneratorMethod:
357
    case FunctionKind::kStaticAsyncConciseGeneratorMethod:
358 359 360 361 362 363 364 365 366 367 368
    case FunctionKind::kClassMembersInitializerFunction:
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsConciseMethod)

bool FunctionKindIsAccessorFunction(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kGetterFunction:
369
    case FunctionKind::kStaticGetterFunction:
370
    case FunctionKind::kSetterFunction:
371
    case FunctionKind::kStaticSetterFunction:
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsAccessorFunction)

bool FunctionKindIsDefaultConstructor(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kDefaultBaseConstructor:
    case FunctionKind::kDefaultDerivedConstructor:
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsDefaultConstructor)

bool FunctionKindIsBaseConstructor(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kBaseConstructor:
    case FunctionKind::kDefaultBaseConstructor:
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsBaseConstructor)

bool FunctionKindIsDerivedConstructor(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kDefaultDerivedConstructor:
    case FunctionKind::kDerivedConstructor:
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsDerivedConstructor)

bool FunctionKindIsClassConstructor(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kBaseConstructor:
    case FunctionKind::kDefaultBaseConstructor:
    case FunctionKind::kDefaultDerivedConstructor:
    case FunctionKind::kDerivedConstructor:
      return true;
    default:
      return false;
  }
}
TEST_FUNCTION_KIND(IsClassConstructor)

bool FunctionKindIsConstructable(FunctionKind kind) {
  switch (kind) {
    case FunctionKind::kGetterFunction:
428
    case FunctionKind::kStaticGetterFunction:
429
    case FunctionKind::kSetterFunction:
430
    case FunctionKind::kStaticSetterFunction:
431 432 433 434
    case FunctionKind::kArrowFunction:
    case FunctionKind::kAsyncArrowFunction:
    case FunctionKind::kAsyncFunction:
    case FunctionKind::kAsyncConciseMethod:
435
    case FunctionKind::kStaticAsyncConciseMethod:
436
    case FunctionKind::kAsyncConciseGeneratorMethod:
437
    case FunctionKind::kStaticAsyncConciseGeneratorMethod:
438 439 440
    case FunctionKind::kAsyncGeneratorFunction:
    case FunctionKind::kGeneratorFunction:
    case FunctionKind::kConciseGeneratorMethod:
441
    case FunctionKind::kStaticConciseGeneratorMethod:
442
    case FunctionKind::kConciseMethod:
443
    case FunctionKind::kStaticConciseMethod:
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    case FunctionKind::kClassMembersInitializerFunction:
      return false;
    default:
      return true;
  }
}
TEST_FUNCTION_KIND(IsConstructable)

bool FunctionKindIsStrictFunctionWithoutPrototype(FunctionKind kind) {
  return IsArrowFunction(kind) || IsConciseMethod(kind) ||
         IsAccessorFunction(kind);
}
TEST_FUNCTION_KIND(IsStrictFunctionWithoutPrototype)

#undef TEST_FUNCTION_KIND

460 461
}  // namespace internal
}  // namespace v8