// 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 "src/base/utils/random-number-generator.h" #include "src/code-factory.h" #include "src/code-stub-assembler.h" #include "src/compiler/node.h" #include "src/isolate.h" #include "test/cctest/compiler/code-assembler-tester.h" #include "test/cctest/compiler/function-tester.h" namespace v8 { namespace internal { using compiler::CodeAssemblerTester; using compiler::FunctionTester; using compiler::Node; TEST(FixedArrayAccessSmiIndex) { Isolate* isolate(CcTest::InitIsolateOnce()); CodeAssemblerTester data(isolate); CodeStubAssembler m(data.state()); Handle array = isolate->factory()->NewFixedArray(5); array->set(4, Smi::FromInt(733)); m.Return(m.LoadFixedArrayElement(m.HeapConstant(array), m.SmiTag(m.Int32Constant(4)), 0, CodeStubAssembler::SMI_PARAMETERS)); Handle code = data.GenerateCode(); FunctionTester ft(code); MaybeHandle result = ft.Call(); CHECK_EQ(733, Handle::cast(result.ToHandleChecked())->value()); } TEST(LoadHeapNumberValue) { Isolate* isolate(CcTest::InitIsolateOnce()); CodeAssemblerTester data(isolate); CodeStubAssembler m(data.state()); Handle number = isolate->factory()->NewHeapNumber(1234); m.Return(m.SmiTag( m.ChangeFloat64ToUint32(m.LoadHeapNumberValue(m.HeapConstant(number))))); Handle code = data.GenerateCode(); FunctionTester ft(code); MaybeHandle result = ft.Call(); CHECK_EQ(1234, Handle::cast(result.ToHandleChecked())->value()); } TEST(LoadInstanceType) { Isolate* isolate(CcTest::InitIsolateOnce()); CodeAssemblerTester data(isolate); CodeStubAssembler m(data.state()); Handle undefined = isolate->factory()->undefined_value(); m.Return(m.SmiTag(m.LoadInstanceType(m.HeapConstant(undefined)))); Handle code = data.GenerateCode(); FunctionTester ft(code); MaybeHandle result = ft.Call(); CHECK_EQ(InstanceType::ODDBALL_TYPE, Handle::cast(result.ToHandleChecked())->value()); } TEST(DecodeWordFromWord32) { Isolate* isolate(CcTest::InitIsolateOnce()); CodeAssemblerTester data(isolate); CodeStubAssembler m(data.state()); class TestBitField : public BitField {}; m.Return( m.SmiTag(m.DecodeWordFromWord32(m.Int32Constant(0x2f)))); Handle code = data.GenerateCode(); FunctionTester ft(code); MaybeHandle result = ft.Call(); // value = 00101111 // mask = 00111000 // result = 101 CHECK_EQ(5, Handle::cast(result.ToHandleChecked())->value()); } TEST(JSFunction) { const int kNumParams = 3; // Receiver, left, right. Isolate* isolate(CcTest::InitIsolateOnce()); CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); m.Return(m.SmiFromWord32(m.Int32Add(m.SmiToWord32(m.Parameter(1)), m.SmiToWord32(m.Parameter(2))))); Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); MaybeHandle result = ft.Call(isolate->factory()->undefined_value(), handle(Smi::FromInt(23), isolate), handle(Smi::FromInt(34), isolate)); CHECK_EQ(57, Handle::cast(result.ToHandleChecked())->value()); } TEST(ComputeIntegerHash) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 2; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); m.Return(m.SmiFromWord32(m.ComputeIntegerHash( m.SmiToWord32(m.Parameter(0)), m.SmiToWord32(m.Parameter(1))))); Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Handle hash_seed = isolate->factory()->hash_seed(); base::RandomNumberGenerator rand_gen(FLAG_random_seed); for (int i = 0; i < 1024; i++) { int k = rand_gen.NextInt(Smi::kMaxValue); Handle key(Smi::FromInt(k), isolate); Handle result = ft.Call(key, hash_seed).ToHandleChecked(); uint32_t hash = ComputeIntegerHash(k, hash_seed->value()); Smi* expected = Smi::FromInt(hash & Smi::kMaxValue); CHECK_EQ(expected, Smi::cast(*result)); } } TEST(ToString) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); m.Return(m.ToString(m.Parameter(kNumParams + 2), m.Parameter(0))); Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Handle test_cases = isolate->factory()->NewFixedArray(5); Handle smi_test = isolate->factory()->NewFixedArray(2); smi_test->set(0, Smi::FromInt(42)); Handle str(isolate->factory()->InternalizeUtf8String("42")); smi_test->set(1, *str); test_cases->set(0, *smi_test); Handle number_test = isolate->factory()->NewFixedArray(2); Handle num(isolate->factory()->NewHeapNumber(3.14)); number_test->set(0, *num); str = isolate->factory()->InternalizeUtf8String("3.14"); number_test->set(1, *str); test_cases->set(1, *number_test); Handle string_test = isolate->factory()->NewFixedArray(2); str = isolate->factory()->InternalizeUtf8String("test"); string_test->set(0, *str); string_test->set(1, *str); test_cases->set(2, *string_test); Handle oddball_test = isolate->factory()->NewFixedArray(2); oddball_test->set(0, isolate->heap()->undefined_value()); str = isolate->factory()->InternalizeUtf8String("undefined"); oddball_test->set(1, *str); test_cases->set(3, *oddball_test); Handle tostring_test = isolate->factory()->NewFixedArray(2); Handle js_array_storage = isolate->factory()->NewFixedArray(2); js_array_storage->set(0, Smi::FromInt(1)); js_array_storage->set(1, Smi::FromInt(2)); Handle js_array = isolate->factory()->NewJSArray(2); JSArray::SetContent(js_array, js_array_storage); tostring_test->set(0, *js_array); str = isolate->factory()->InternalizeUtf8String("1,2"); tostring_test->set(1, *str); test_cases->set(4, *tostring_test); for (int i = 0; i < 5; ++i) { Handle test = handle(FixedArray::cast(test_cases->get(i))); Handle obj = handle(test->get(0), isolate); Handle expected = handle(String::cast(test->get(1))); Handle result = ft.Call(obj).ToHandleChecked(); CHECK(result->IsString()); CHECK(String::Equals(Handle::cast(result), expected)); } } TEST(FlattenString) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); m.Return(m.FlattenString(m.Parameter(0))); Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Handle test_cases(isolate->factory()->NewFixedArray(4)); Handle expected( isolate->factory()->InternalizeUtf8String("hello, world!")); test_cases->set(0, *expected); Handle string( isolate->factory()->InternalizeUtf8String("filler hello, world! filler")); Handle sub_string( isolate->factory()->NewProperSubString(string, 7, 20)); test_cases->set(1, *sub_string); Handle hello(isolate->factory()->InternalizeUtf8String("hello,")); Handle world(isolate->factory()->InternalizeUtf8String(" world!")); Handle cons_str( isolate->factory()->NewConsString(hello, world).ToHandleChecked()); test_cases->set(2, *cons_str); Handle empty(isolate->factory()->InternalizeUtf8String("")); Handle fake_cons_str( isolate->factory()->NewConsString(expected, empty).ToHandleChecked()); test_cases->set(3, *fake_cons_str); for (int i = 0; i < 4; ++i) { Handle test = handle(String::cast(test_cases->get(i))); Handle result = ft.Call(test).ToHandleChecked(); CHECK(result->IsString()); CHECK(Handle::cast(result)->IsFlat()); CHECK(String::Equals(Handle::cast(result), expected)); } } TEST(TryToName) { typedef CodeStubAssembler::Label Label; typedef CodeStubAssembler::Variable Variable; Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 3; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); enum Result { kKeyIsIndex, kKeyIsUnique, kBailout }; { Node* key = m.Parameter(0); Node* expected_result = m.Parameter(1); Node* expected_arg = m.Parameter(2); Label passed(&m), failed(&m); Label if_keyisindex(&m), if_keyisunique(&m), if_bailout(&m); Variable var_index(&m, MachineType::PointerRepresentation()); m.TryToName(key, &if_keyisindex, &var_index, &if_keyisunique, &if_bailout); m.Bind(&if_keyisindex); m.GotoUnless( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kKeyIsIndex))), &failed); m.Branch(m.WordEqual(m.SmiUntag(expected_arg), var_index.value()), &passed, &failed); m.Bind(&if_keyisunique); m.GotoUnless( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kKeyIsUnique))), &failed); m.Branch(m.WordEqual(expected_arg, key), &passed, &failed); m.Bind(&if_bailout); m.Branch( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kBailout))), &passed, &failed); m.Bind(&passed); m.Return(m.BooleanConstant(true)); m.Bind(&failed); m.Return(m.BooleanConstant(false)); } Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Handle expect_index(Smi::FromInt(kKeyIsIndex), isolate); Handle expect_unique(Smi::FromInt(kKeyIsUnique), isolate); Handle expect_bailout(Smi::FromInt(kBailout), isolate); { // TryToName() => if_keyisindex: smi value. Handle key(Smi::kZero, isolate); ft.CheckTrue(key, expect_index, key); } { // TryToName() => if_keyisindex: smi value. Handle key(Smi::FromInt(153), isolate); ft.CheckTrue(key, expect_index, key); } { // TryToName() => if_keyisindex: smi value. // A subsequent bounds check needs to take care of this case. Handle key(Smi::FromInt(-1), isolate); ft.CheckTrue(key, expect_index, key); } { // TryToName() => if_keyisindex: number. Handle key(isolate->factory()->NewHeapNumber(153)); Handle index(Smi::FromInt(153), isolate); ft.CheckTrue(key, expect_index, index); } { // TryToName() => if_keyisunique: . Handle key = isolate->factory()->NewSymbol(); ft.CheckTrue(key, expect_unique, key); } { // TryToName() => if_keyisunique: Handle key = isolate->factory()->InternalizeUtf8String("test"); ft.CheckTrue(key, expect_unique, key); } { // TryToName() => if_keyisindex: number. Handle key = isolate->factory()->InternalizeUtf8String("153"); Handle index(Smi::FromInt(153), isolate); ft.CheckTrue(key, expect_index, index); } { // TryToName() => bailout Handle key = isolate->factory()->InternalizeUtf8String("4294967294"); ft.CheckTrue(key, expect_bailout); } { // TryToName() => if_keyisindex: number. Handle key = isolate->factory()->NewStringFromAsciiChecked("153"); uint32_t dummy; CHECK(key->AsArrayIndex(&dummy)); CHECK(key->HasHashCode()); CHECK(!key->IsInternalizedString()); Handle index(Smi::FromInt(153), isolate); ft.CheckTrue(key, expect_index, index); } { // TryToName() => bailout. Handle key = isolate->factory()->NewStringFromAsciiChecked("153"); CHECK(!key->HasHashCode()); ft.CheckTrue(key, expect_bailout); } { // TryToName() => bailout. Handle key = isolate->factory()->NewStringFromAsciiChecked("test"); ft.CheckTrue(key, expect_bailout); } } namespace { template void TestEntryToIndex() { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); { Node* entry = m.SmiUntag(m.Parameter(0)); Node* result = m.EntryToIndex(entry); m.Return(m.SmiTag(result)); } Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); // Test a wide range of entries but staying linear in the first 100 entries. for (int entry = 0; entry < Dictionary::kMaxCapacity; entry = entry * 1.01 + 1) { Handle result = ft.Call(handle(Smi::FromInt(entry), isolate)).ToHandleChecked(); CHECK_EQ(Dictionary::EntryToIndex(entry), Smi::cast(*result)->value()); } } TEST(NameDictionaryEntryToIndex) { TestEntryToIndex(); } TEST(GlobalDictionaryEntryToIndex) { TestEntryToIndex(); } } // namespace namespace { template void TestNameDictionaryLookup() { typedef CodeStubAssembler::Label Label; typedef CodeStubAssembler::Variable Variable; Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 4; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); enum Result { kFound, kNotFound }; { Node* dictionary = m.Parameter(0); Node* unique_name = m.Parameter(1); Node* expected_result = m.Parameter(2); Node* expected_arg = m.Parameter(3); Label passed(&m), failed(&m); Label if_found(&m), if_not_found(&m); Variable var_name_index(&m, MachineType::PointerRepresentation()); m.NameDictionaryLookup(dictionary, unique_name, &if_found, &var_name_index, &if_not_found); m.Bind(&if_found); m.GotoUnless( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kFound))), &failed); m.Branch(m.Word32Equal(m.SmiToWord32(expected_arg), var_name_index.value()), &passed, &failed); m.Bind(&if_not_found); m.Branch( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kNotFound))), &passed, &failed); m.Bind(&passed); m.Return(m.BooleanConstant(true)); m.Bind(&failed); m.Return(m.BooleanConstant(false)); } Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Handle expect_found(Smi::FromInt(kFound), isolate); Handle expect_not_found(Smi::FromInt(kNotFound), isolate); Handle dictionary = Dictionary::New(isolate, 40); PropertyDetails fake_details = PropertyDetails::Empty(); Factory* factory = isolate->factory(); Handle keys[] = { factory->InternalizeUtf8String("0"), factory->InternalizeUtf8String("42"), factory->InternalizeUtf8String("-153"), factory->InternalizeUtf8String("0.0"), factory->InternalizeUtf8String("4.2"), factory->InternalizeUtf8String(""), factory->InternalizeUtf8String("name"), factory->NewSymbol(), factory->NewPrivateSymbol(), }; for (size_t i = 0; i < arraysize(keys); i++) { Handle value = factory->NewPropertyCell(); dictionary = Dictionary::Add(dictionary, keys[i], value, fake_details); } for (size_t i = 0; i < arraysize(keys); i++) { int entry = dictionary->FindEntry(keys[i]); int name_index = Dictionary::EntryToIndex(entry) + Dictionary::kEntryKeyIndex; CHECK_NE(Dictionary::kNotFound, entry); Handle expected_name_index(Smi::FromInt(name_index), isolate); ft.CheckTrue(dictionary, keys[i], expect_found, expected_name_index); } Handle non_existing_keys[] = { factory->InternalizeUtf8String("1"), factory->InternalizeUtf8String("-42"), factory->InternalizeUtf8String("153"), factory->InternalizeUtf8String("-1.0"), factory->InternalizeUtf8String("1.3"), factory->InternalizeUtf8String("a"), factory->InternalizeUtf8String("boom"), factory->NewSymbol(), factory->NewPrivateSymbol(), }; for (size_t i = 0; i < arraysize(non_existing_keys); i++) { int entry = dictionary->FindEntry(non_existing_keys[i]); CHECK_EQ(Dictionary::kNotFound, entry); ft.CheckTrue(dictionary, non_existing_keys[i], expect_not_found); } } } // namespace TEST(NameDictionaryLookup) { TestNameDictionaryLookup(); } TEST(GlobalDictionaryLookup) { TestNameDictionaryLookup(); } namespace { template void TestNumberDictionaryLookup() { typedef CodeStubAssembler::Label Label; typedef CodeStubAssembler::Variable Variable; Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 4; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); enum Result { kFound, kNotFound }; { Node* dictionary = m.Parameter(0); Node* key = m.SmiToWord32(m.Parameter(1)); Node* expected_result = m.Parameter(2); Node* expected_arg = m.Parameter(3); Label passed(&m), failed(&m); Label if_found(&m), if_not_found(&m); Variable var_entry(&m, MachineType::PointerRepresentation()); m.NumberDictionaryLookup(dictionary, key, &if_found, &var_entry, &if_not_found); m.Bind(&if_found); m.GotoUnless( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kFound))), &failed); m.Branch(m.Word32Equal(m.SmiToWord32(expected_arg), var_entry.value()), &passed, &failed); m.Bind(&if_not_found); m.Branch( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kNotFound))), &passed, &failed); m.Bind(&passed); m.Return(m.BooleanConstant(true)); m.Bind(&failed); m.Return(m.BooleanConstant(false)); } Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Handle expect_found(Smi::FromInt(kFound), isolate); Handle expect_not_found(Smi::FromInt(kNotFound), isolate); const int kKeysCount = 1000; Handle dictionary = Dictionary::New(isolate, kKeysCount); uint32_t keys[kKeysCount]; Handle fake_value(Smi::FromInt(42), isolate); PropertyDetails fake_details = PropertyDetails::Empty(); base::RandomNumberGenerator rand_gen(FLAG_random_seed); for (int i = 0; i < kKeysCount; i++) { int random_key = rand_gen.NextInt(Smi::kMaxValue); keys[i] = static_cast(random_key); if (dictionary->FindEntry(keys[i]) != Dictionary::kNotFound) continue; dictionary = Dictionary::Add(dictionary, keys[i], fake_value, fake_details); } // Now try querying existing keys. for (int i = 0; i < kKeysCount; i++) { int entry = dictionary->FindEntry(keys[i]); CHECK_NE(Dictionary::kNotFound, entry); Handle key(Smi::FromInt(keys[i]), isolate); Handle expected_entry(Smi::FromInt(entry), isolate); ft.CheckTrue(dictionary, key, expect_found, expected_entry); } // Now try querying random keys which do not exist in the dictionary. for (int i = 0; i < kKeysCount;) { int random_key = rand_gen.NextInt(Smi::kMaxValue); int entry = dictionary->FindEntry(random_key); if (entry != Dictionary::kNotFound) continue; i++; Handle key(Smi::FromInt(random_key), isolate); ft.CheckTrue(dictionary, key, expect_not_found); } } } // namespace TEST(SeededNumberDictionaryLookup) { TestNumberDictionaryLookup(); } TEST(UnseededNumberDictionaryLookup) { TestNumberDictionaryLookup(); } namespace { void AddProperties(Handle object, Handle names[], size_t count) { Isolate* isolate = object->GetIsolate(); for (size_t i = 0; i < count; i++) { Handle value(Smi::FromInt(static_cast(42 + i)), isolate); JSObject::AddProperty(object, names[i], value, NONE); } } Handle CreateAccessorPair(FunctionTester* ft, const char* getter_body, const char* setter_body) { Handle pair = ft->isolate->factory()->NewAccessorPair(); if (getter_body) { pair->set_getter(*ft->NewFunction(getter_body)); } if (setter_body) { pair->set_setter(*ft->NewFunction(setter_body)); } return pair; } void AddProperties(Handle object, Handle names[], size_t names_count, Handle values[], size_t values_count, int seed = 0) { Isolate* isolate = object->GetIsolate(); for (size_t i = 0; i < names_count; i++) { Handle value = values[(seed + i) % values_count]; if (value->IsAccessorPair()) { Handle pair = Handle::cast(value); Handle getter(pair->getter(), isolate); Handle setter(pair->setter(), isolate); JSObject::DefineAccessor(object, names[i], getter, setter, NONE).Check(); } else { JSObject::AddProperty(object, names[i], value, NONE); } } } } // namespace TEST(TryHasOwnProperty) { typedef CodeStubAssembler::Label Label; Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 4; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); enum Result { kFound, kNotFound, kBailout }; { Node* object = m.Parameter(0); Node* unique_name = m.Parameter(1); Node* expected_result = m.Parameter(2); Label passed(&m), failed(&m); Label if_found(&m), if_not_found(&m), if_bailout(&m); Node* map = m.LoadMap(object); Node* instance_type = m.LoadMapInstanceType(map); m.TryHasOwnProperty(object, map, instance_type, unique_name, &if_found, &if_not_found, &if_bailout); m.Bind(&if_found); m.Branch(m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kFound))), &passed, &failed); m.Bind(&if_not_found); m.Branch( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kNotFound))), &passed, &failed); m.Bind(&if_bailout); m.Branch( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kBailout))), &passed, &failed); m.Bind(&passed); m.Return(m.BooleanConstant(true)); m.Bind(&failed); m.Return(m.BooleanConstant(false)); } Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Handle expect_found(Smi::FromInt(kFound), isolate); Handle expect_not_found(Smi::FromInt(kNotFound), isolate); Handle expect_bailout(Smi::FromInt(kBailout), isolate); Factory* factory = isolate->factory(); Handle deleted_property_name = factory->InternalizeUtf8String("deleted"); Handle names[] = { factory->InternalizeUtf8String("a"), factory->InternalizeUtf8String("bb"), factory->InternalizeUtf8String("ccc"), factory->InternalizeUtf8String("dddd"), factory->InternalizeUtf8String("eeeee"), factory->InternalizeUtf8String(""), factory->InternalizeUtf8String("name"), factory->NewSymbol(), factory->NewPrivateSymbol(), }; std::vector> objects; { // Fast object, no inobject properties. int inobject_properties = 0; Handle map = Map::Create(isolate, inobject_properties); Handle object = factory->NewJSObjectFromMap(map); AddProperties(object, names, arraysize(names)); CHECK_EQ(JS_OBJECT_TYPE, object->map()->instance_type()); CHECK_EQ(inobject_properties, object->map()->GetInObjectProperties()); CHECK(!object->map()->is_dictionary_map()); objects.push_back(object); } { // Fast object, all inobject properties. int inobject_properties = arraysize(names) * 2; Handle map = Map::Create(isolate, inobject_properties); Handle object = factory->NewJSObjectFromMap(map); AddProperties(object, names, arraysize(names)); CHECK_EQ(JS_OBJECT_TYPE, object->map()->instance_type()); CHECK_EQ(inobject_properties, object->map()->GetInObjectProperties()); CHECK(!object->map()->is_dictionary_map()); objects.push_back(object); } { // Fast object, half inobject properties. int inobject_properties = arraysize(names) / 2; Handle map = Map::Create(isolate, inobject_properties); Handle object = factory->NewJSObjectFromMap(map); AddProperties(object, names, arraysize(names)); CHECK_EQ(JS_OBJECT_TYPE, object->map()->instance_type()); CHECK_EQ(inobject_properties, object->map()->GetInObjectProperties()); CHECK(!object->map()->is_dictionary_map()); objects.push_back(object); } { // Dictionary mode object. Handle function = factory->NewFunction(factory->empty_string()); Handle object = factory->NewJSObject(function); AddProperties(object, names, arraysize(names)); JSObject::NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0, "test"); JSObject::AddProperty(object, deleted_property_name, object, NONE); CHECK(JSObject::DeleteProperty(object, deleted_property_name, SLOPPY) .FromJust()); CHECK_EQ(JS_OBJECT_TYPE, object->map()->instance_type()); CHECK(object->map()->is_dictionary_map()); objects.push_back(object); } { // Global object. Handle function = factory->NewFunction(factory->empty_string()); JSFunction::EnsureHasInitialMap(function); function->initial_map()->set_instance_type(JS_GLOBAL_OBJECT_TYPE); function->initial_map()->set_is_prototype_map(true); function->initial_map()->set_dictionary_map(true); Handle object = factory->NewJSGlobalObject(function); AddProperties(object, names, arraysize(names)); JSObject::AddProperty(object, deleted_property_name, object, NONE); CHECK(JSObject::DeleteProperty(object, deleted_property_name, SLOPPY) .FromJust()); CHECK_EQ(JS_GLOBAL_OBJECT_TYPE, object->map()->instance_type()); CHECK(object->map()->is_dictionary_map()); objects.push_back(object); } { for (Handle object : objects) { for (size_t name_index = 0; name_index < arraysize(names); name_index++) { Handle name = names[name_index]; CHECK(JSReceiver::HasProperty(object, name).FromJust()); ft.CheckTrue(object, name, expect_found); } } } { Handle non_existing_names[] = { factory->NewSymbol(), factory->InternalizeUtf8String("ne_a"), factory->InternalizeUtf8String("ne_bb"), factory->NewPrivateSymbol(), factory->InternalizeUtf8String("ne_ccc"), factory->InternalizeUtf8String("ne_dddd"), deleted_property_name, }; for (Handle object : objects) { for (size_t key_index = 0; key_index < arraysize(non_existing_names); key_index++) { Handle name = non_existing_names[key_index]; CHECK(!JSReceiver::HasProperty(object, name).FromJust()); ft.CheckTrue(object, name, expect_not_found); } } } { Handle function = factory->NewFunction(factory->empty_string()); Handle object = factory->NewJSProxy(function, objects[0]); CHECK_EQ(JS_PROXY_TYPE, object->map()->instance_type()); ft.CheckTrue(object, names[0], expect_bailout); } { Handle object = isolate->global_proxy(); CHECK_EQ(JS_GLOBAL_PROXY_TYPE, object->map()->instance_type()); ft.CheckTrue(object, names[0], expect_bailout); } } TEST(TryGetOwnProperty) { typedef CodeStubAssembler::Label Label; typedef CodeStubAssembler::Variable Variable; Isolate* isolate(CcTest::InitIsolateOnce()); Factory* factory = isolate->factory(); const int kNumParams = 2; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); Handle not_found_symbol = factory->NewSymbol(); Handle bailout_symbol = factory->NewSymbol(); { Node* object = m.Parameter(0); Node* unique_name = m.Parameter(1); Node* context = m.Parameter(kNumParams + 2); Variable var_value(&m, MachineRepresentation::kTagged); Label if_found(&m), if_not_found(&m), if_bailout(&m); Node* map = m.LoadMap(object); Node* instance_type = m.LoadMapInstanceType(map); m.TryGetOwnProperty(context, object, object, map, instance_type, unique_name, &if_found, &var_value, &if_not_found, &if_bailout); m.Bind(&if_found); m.Return(var_value.value()); m.Bind(&if_not_found); m.Return(m.HeapConstant(not_found_symbol)); m.Bind(&if_bailout); m.Return(m.HeapConstant(bailout_symbol)); } Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Handle deleted_property_name = factory->InternalizeUtf8String("deleted"); Handle names[] = { factory->InternalizeUtf8String("bb"), factory->NewSymbol(), factory->InternalizeUtf8String("a"), factory->InternalizeUtf8String("ccc"), factory->InternalizeUtf8String("esajefe"), factory->NewPrivateSymbol(), factory->InternalizeUtf8String("eeeee"), factory->InternalizeUtf8String("p1"), factory->InternalizeUtf8String("acshw23e"), factory->InternalizeUtf8String(""), factory->InternalizeUtf8String("dddd"), factory->NewPrivateSymbol(), factory->InternalizeUtf8String("name"), factory->InternalizeUtf8String("p2"), factory->InternalizeUtf8String("p3"), factory->InternalizeUtf8String("p4"), factory->NewPrivateSymbol(), }; Handle values[] = { factory->NewFunction(factory->empty_string()), factory->NewSymbol(), factory->InternalizeUtf8String("a"), CreateAccessorPair(&ft, "() => 188;", "() => 199;"), factory->NewFunction(factory->InternalizeUtf8String("bb")), factory->InternalizeUtf8String("ccc"), CreateAccessorPair(&ft, "() => 88;", nullptr), handle(Smi::FromInt(1), isolate), factory->InternalizeUtf8String(""), CreateAccessorPair(&ft, nullptr, "() => 99;"), factory->NewHeapNumber(4.2), handle(Smi::FromInt(153), isolate), factory->NewJSObject(factory->NewFunction(factory->empty_string())), factory->NewPrivateSymbol(), }; STATIC_ASSERT(arraysize(values) < arraysize(names)); base::RandomNumberGenerator rand_gen(FLAG_random_seed); std::vector> objects; { // Fast object, no inobject properties. int inobject_properties = 0; Handle map = Map::Create(isolate, inobject_properties); Handle object = factory->NewJSObjectFromMap(map); AddProperties(object, names, arraysize(names), values, arraysize(values), rand_gen.NextInt()); CHECK_EQ(JS_OBJECT_TYPE, object->map()->instance_type()); CHECK_EQ(inobject_properties, object->map()->GetInObjectProperties()); CHECK(!object->map()->is_dictionary_map()); objects.push_back(object); } { // Fast object, all inobject properties. int inobject_properties = arraysize(names) * 2; Handle map = Map::Create(isolate, inobject_properties); Handle object = factory->NewJSObjectFromMap(map); AddProperties(object, names, arraysize(names), values, arraysize(values), rand_gen.NextInt()); CHECK_EQ(JS_OBJECT_TYPE, object->map()->instance_type()); CHECK_EQ(inobject_properties, object->map()->GetInObjectProperties()); CHECK(!object->map()->is_dictionary_map()); objects.push_back(object); } { // Fast object, half inobject properties. int inobject_properties = arraysize(names) / 2; Handle map = Map::Create(isolate, inobject_properties); Handle object = factory->NewJSObjectFromMap(map); AddProperties(object, names, arraysize(names), values, arraysize(values), rand_gen.NextInt()); CHECK_EQ(JS_OBJECT_TYPE, object->map()->instance_type()); CHECK_EQ(inobject_properties, object->map()->GetInObjectProperties()); CHECK(!object->map()->is_dictionary_map()); objects.push_back(object); } { // Dictionary mode object. Handle function = factory->NewFunction(factory->empty_string()); Handle object = factory->NewJSObject(function); AddProperties(object, names, arraysize(names), values, arraysize(values), rand_gen.NextInt()); JSObject::NormalizeProperties(object, CLEAR_INOBJECT_PROPERTIES, 0, "test"); JSObject::AddProperty(object, deleted_property_name, object, NONE); CHECK(JSObject::DeleteProperty(object, deleted_property_name, SLOPPY) .FromJust()); CHECK_EQ(JS_OBJECT_TYPE, object->map()->instance_type()); CHECK(object->map()->is_dictionary_map()); objects.push_back(object); } { // Global object. Handle object = isolate->global_object(); AddProperties(object, names, arraysize(names), values, arraysize(values), rand_gen.NextInt()); JSObject::AddProperty(object, deleted_property_name, object, NONE); CHECK(JSObject::DeleteProperty(object, deleted_property_name, SLOPPY) .FromJust()); CHECK_EQ(JS_GLOBAL_OBJECT_TYPE, object->map()->instance_type()); CHECK(object->map()->is_dictionary_map()); objects.push_back(object); } // TODO(ishell): test proxy and interceptors when they are supported. { for (Handle object : objects) { for (size_t name_index = 0; name_index < arraysize(names); name_index++) { Handle name = names[name_index]; Handle expected_value = JSReceiver::GetProperty(object, name).ToHandleChecked(); Handle value = ft.Call(object, name).ToHandleChecked(); CHECK(expected_value->SameValue(*value)); } } } { Handle non_existing_names[] = { factory->NewSymbol(), factory->InternalizeUtf8String("ne_a"), factory->InternalizeUtf8String("ne_bb"), factory->NewPrivateSymbol(), factory->InternalizeUtf8String("ne_ccc"), factory->InternalizeUtf8String("ne_dddd"), deleted_property_name, }; for (Handle object : objects) { for (size_t key_index = 0; key_index < arraysize(non_existing_names); key_index++) { Handle name = non_existing_names[key_index]; Handle expected_value = JSReceiver::GetProperty(object, name).ToHandleChecked(); CHECK(expected_value->IsUndefined(isolate)); Handle value = ft.Call(object, name).ToHandleChecked(); CHECK_EQ(*not_found_symbol, *value); } } } { Handle function = factory->NewFunction(factory->empty_string()); Handle object = factory->NewJSProxy(function, objects[0]); CHECK_EQ(JS_PROXY_TYPE, object->map()->instance_type()); Handle value = ft.Call(object, names[0]).ToHandleChecked(); // Proxies are not supported yet. CHECK_EQ(*bailout_symbol, *value); } { Handle object = isolate->global_proxy(); CHECK_EQ(JS_GLOBAL_PROXY_TYPE, object->map()->instance_type()); // Global proxies are not supported yet. Handle value = ft.Call(object, names[0]).ToHandleChecked(); CHECK_EQ(*bailout_symbol, *value); } } namespace { void AddElement(Handle object, uint32_t index, Handle value, PropertyAttributes attributes = NONE) { JSObject::AddDataElement(object, index, value, attributes).ToHandleChecked(); } } // namespace TEST(TryLookupElement) { typedef CodeStubAssembler::Label Label; Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 3; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); enum Result { kFound, kNotFound, kBailout }; { Node* object = m.Parameter(0); Node* index = m.SmiToWord32(m.Parameter(1)); Node* expected_result = m.Parameter(2); Label passed(&m), failed(&m); Label if_found(&m), if_not_found(&m), if_bailout(&m); Node* map = m.LoadMap(object); Node* instance_type = m.LoadMapInstanceType(map); m.TryLookupElement(object, map, instance_type, index, &if_found, &if_not_found, &if_bailout); m.Bind(&if_found); m.Branch(m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kFound))), &passed, &failed); m.Bind(&if_not_found); m.Branch( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kNotFound))), &passed, &failed); m.Bind(&if_bailout); m.Branch( m.WordEqual(expected_result, m.SmiConstant(Smi::FromInt(kBailout))), &passed, &failed); m.Bind(&passed); m.Return(m.BooleanConstant(true)); m.Bind(&failed); m.Return(m.BooleanConstant(false)); } Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Factory* factory = isolate->factory(); Handle smi0(Smi::kZero, isolate); Handle smi1(Smi::FromInt(1), isolate); Handle smi7(Smi::FromInt(7), isolate); Handle smi13(Smi::FromInt(13), isolate); Handle smi42(Smi::FromInt(42), isolate); Handle expect_found(Smi::FromInt(kFound), isolate); Handle expect_not_found(Smi::FromInt(kNotFound), isolate); Handle expect_bailout(Smi::FromInt(kBailout), isolate); #define CHECK_FOUND(object, index) \ CHECK(JSReceiver::HasElement(object, index).FromJust()); \ ft.CheckTrue(object, smi##index, expect_found); #define CHECK_NOT_FOUND(object, index) \ CHECK(!JSReceiver::HasElement(object, index).FromJust()); \ ft.CheckTrue(object, smi##index, expect_not_found); { Handle object = factory->NewJSArray(0, FAST_SMI_ELEMENTS); AddElement(object, 0, smi0); AddElement(object, 1, smi0); CHECK_EQ(FAST_SMI_ELEMENTS, object->map()->elements_kind()); CHECK_FOUND(object, 0); CHECK_FOUND(object, 1); CHECK_NOT_FOUND(object, 7); CHECK_NOT_FOUND(object, 13); CHECK_NOT_FOUND(object, 42); } { Handle object = factory->NewJSArray(0, FAST_HOLEY_SMI_ELEMENTS); AddElement(object, 0, smi0); AddElement(object, 13, smi0); CHECK_EQ(FAST_HOLEY_SMI_ELEMENTS, object->map()->elements_kind()); CHECK_FOUND(object, 0); CHECK_NOT_FOUND(object, 1); CHECK_NOT_FOUND(object, 7); CHECK_FOUND(object, 13); CHECK_NOT_FOUND(object, 42); } { Handle object = factory->NewJSArray(0, FAST_ELEMENTS); AddElement(object, 0, smi0); AddElement(object, 1, smi0); CHECK_EQ(FAST_ELEMENTS, object->map()->elements_kind()); CHECK_FOUND(object, 0); CHECK_FOUND(object, 1); CHECK_NOT_FOUND(object, 7); CHECK_NOT_FOUND(object, 13); CHECK_NOT_FOUND(object, 42); } { Handle object = factory->NewJSArray(0, FAST_HOLEY_ELEMENTS); AddElement(object, 0, smi0); AddElement(object, 13, smi0); CHECK_EQ(FAST_HOLEY_ELEMENTS, object->map()->elements_kind()); CHECK_FOUND(object, 0); CHECK_NOT_FOUND(object, 1); CHECK_NOT_FOUND(object, 7); CHECK_FOUND(object, 13); CHECK_NOT_FOUND(object, 42); } { Handle constructor = isolate->string_function(); Handle object = factory->NewJSObject(constructor); Handle str = factory->InternalizeUtf8String("ab"); Handle::cast(object)->set_value(*str); AddElement(object, 13, smi0); CHECK_EQ(FAST_STRING_WRAPPER_ELEMENTS, object->map()->elements_kind()); CHECK_FOUND(object, 0); CHECK_FOUND(object, 1); CHECK_NOT_FOUND(object, 7); CHECK_FOUND(object, 13); CHECK_NOT_FOUND(object, 42); } { Handle constructor = isolate->string_function(); Handle object = factory->NewJSObject(constructor); Handle str = factory->InternalizeUtf8String("ab"); Handle::cast(object)->set_value(*str); AddElement(object, 13, smi0); JSObject::NormalizeElements(object); CHECK_EQ(SLOW_STRING_WRAPPER_ELEMENTS, object->map()->elements_kind()); CHECK_FOUND(object, 0); CHECK_FOUND(object, 1); CHECK_NOT_FOUND(object, 7); CHECK_FOUND(object, 13); CHECK_NOT_FOUND(object, 42); } // TODO(ishell): uncomment once NO_ELEMENTS kind is supported. // { // Handle map = Map::Create(isolate, 0); // map->set_elements_kind(NO_ELEMENTS); // Handle object = factory->NewJSObjectFromMap(map); // CHECK_EQ(NO_ELEMENTS, object->map()->elements_kind()); // // CHECK_NOT_FOUND(object, 0); // CHECK_NOT_FOUND(object, 1); // CHECK_NOT_FOUND(object, 7); // CHECK_NOT_FOUND(object, 13); // CHECK_NOT_FOUND(object, 42); // } #undef CHECK_FOUND #undef CHECK_NOT_FOUND { Handle handler = factory->NewJSArray(0); Handle function = factory->NewFunction(factory->empty_string()); Handle object = factory->NewJSProxy(function, handler); CHECK_EQ(JS_PROXY_TYPE, object->map()->instance_type()); ft.CheckTrue(object, smi0, expect_bailout); } { Handle object = isolate->global_object(); CHECK_EQ(JS_GLOBAL_OBJECT_TYPE, object->map()->instance_type()); ft.CheckTrue(object, smi0, expect_bailout); } { Handle object = isolate->global_proxy(); CHECK_EQ(JS_GLOBAL_PROXY_TYPE, object->map()->instance_type()); ft.CheckTrue(object, smi0, expect_bailout); } } TEST(DeferredCodePhiHints) { typedef compiler::Node Node; typedef CodeStubAssembler::Label Label; typedef CodeStubAssembler::Variable Variable; Isolate* isolate(CcTest::InitIsolateOnce()); CodeAssemblerTester data(isolate); CodeStubAssembler m(data.state()); Label block1(&m, Label::kDeferred); m.Goto(&block1); m.Bind(&block1); { Variable var_object(&m, MachineRepresentation::kTagged); Label loop(&m, &var_object); var_object.Bind(m.IntPtrConstant(0)); m.Goto(&loop); m.Bind(&loop); { Node* map = m.LoadMap(var_object.value()); var_object.Bind(map); m.Goto(&loop); } } CHECK(!data.GenerateCode().is_null()); } TEST(TestOutOfScopeVariable) { typedef CodeStubAssembler::Label Label; typedef CodeStubAssembler::Variable Variable; Isolate* isolate(CcTest::InitIsolateOnce()); CodeAssemblerTester data(isolate); CodeStubAssembler m(data.state()); Label block1(&m); Label block2(&m); Label block3(&m); Label block4(&m); m.Branch(m.WordEqual(m.Parameter(0), m.IntPtrConstant(0)), &block1, &block4); m.Bind(&block4); { Variable var_object(&m, MachineRepresentation::kTagged); m.Branch(m.WordEqual(m.Parameter(0), m.IntPtrConstant(0)), &block2, &block3); m.Bind(&block2); var_object.Bind(m.IntPtrConstant(55)); m.Goto(&block1); m.Bind(&block3); var_object.Bind(m.IntPtrConstant(66)); m.Goto(&block1); } m.Bind(&block1); CHECK(!data.GenerateCode().is_null()); } TEST(GotoIfException) { typedef CodeStubAssembler::Label Label; typedef CodeStubAssembler::Variable Variable; Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); Node* context = m.HeapConstant(Handle(isolate->native_context())); Node* to_string_tag = m.HeapConstant(isolate->factory()->to_string_tag_symbol()); Variable exception(&m, MachineRepresentation::kTagged); Label exception_handler(&m); Callable to_string = CodeFactory::ToString(isolate); Node* string = m.CallStub(to_string, context, to_string_tag); m.GotoIfException(string, &exception_handler, &exception); m.Return(string); m.Bind(&exception_handler); m.Return(exception.value()); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call().ToHandleChecked(); // Should be a TypeError. CHECK(result->IsJSObject()); Handle constructor = Object::GetPropertyOrElement(result, isolate->factory()->constructor_string()) .ToHandleChecked(); CHECK(constructor->SameValue(*isolate->type_error_function())); } TEST(GotoIfExceptionMultiple) { typedef CodeStubAssembler::Label Label; typedef CodeStubAssembler::Variable Variable; Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 4; // receiver, first, second, third CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); Node* context = m.HeapConstant(Handle(isolate->native_context())); Node* first_value = m.Parameter(0); Node* second_value = m.Parameter(1); Node* third_value = m.Parameter(2); Label exception_handler1(&m); Label exception_handler2(&m); Label exception_handler3(&m); Variable return_value(&m, MachineRepresentation::kWord32); Variable error(&m, MachineRepresentation::kTagged); return_value.Bind(m.Int32Constant(0)); // try { return ToString(param1) } catch (e) { ... } Callable to_string = CodeFactory::ToString(isolate); Node* string = m.CallStub(to_string, context, first_value); m.GotoIfException(string, &exception_handler1, &error); m.Return(string); // try { ToString(param2); return 7 } catch (e) { ... } m.Bind(&exception_handler1); return_value.Bind(m.Int32Constant(7)); error.Bind(m.UndefinedConstant()); string = m.CallStub(to_string, context, second_value); m.GotoIfException(string, &exception_handler2, &error); m.Return(m.SmiFromWord32(return_value.value())); // try { ToString(param3); return 7 & ~2; } catch (e) { return e; } m.Bind(&exception_handler2); // Return returnValue & ~2 error.Bind(m.UndefinedConstant()); string = m.CallStub(to_string, context, third_value); m.GotoIfException(string, &exception_handler3, &error); m.Return(m.SmiFromWord32( m.Word32And(return_value.value(), m.Word32Xor(m.Int32Constant(2), m.Int32Constant(-1))))); m.Bind(&exception_handler3); m.Return(error.value()); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result; // First handler does not throw, returns result of first value. result = ft.Call(isolate->factory()->undefined_value(), isolate->factory()->to_string_tag_symbol()) .ToHandleChecked(); CHECK(String::cast(*result)->IsOneByteEqualTo(OneByteVector("undefined"))); // First handler returns a number. result = ft.Call(isolate->factory()->to_string_tag_symbol(), isolate->factory()->undefined_value()) .ToHandleChecked(); CHECK_EQ(7, Smi::cast(*result)->value()); // First handler throws, second handler returns a number. result = ft.Call(isolate->factory()->to_string_tag_symbol(), isolate->factory()->to_primitive_symbol()) .ToHandleChecked(); CHECK_EQ(7 & ~2, Smi::cast(*result)->value()); // First handler throws, second handler throws, third handler returns thrown // value. result = ft.Call(isolate->factory()->to_string_tag_symbol(), isolate->factory()->to_primitive_symbol(), isolate->factory()->unscopables_symbol()) .ToHandleChecked(); // Should be a TypeError. CHECK(result->IsJSObject()); Handle constructor = Object::GetPropertyOrElement(result, isolate->factory()->constructor_string()) .ToHandleChecked(); CHECK(constructor->SameValue(*isolate->type_error_function())); } TEST(AllocateJSObjectFromMap) { Isolate* isolate(CcTest::InitIsolateOnce()); Factory* factory = isolate->factory(); const int kNumParams = 3; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); { Node* map = m.Parameter(0); Node* properties = m.Parameter(1); Node* elements = m.Parameter(2); Node* result = m.AllocateJSObjectFromMap(map, properties, elements); m.Return(result); } Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); Handle maps[] = { handle(isolate->object_function()->initial_map(), isolate), handle(isolate->array_function()->initial_map(), isolate), }; #define VERIFY(result, map_value, properties_value, elements_value) \ CHECK_EQ(result->map(), map_value); \ CHECK_EQ(result->properties(), properties_value); \ CHECK_EQ(result->elements(), elements_value); { Handle empty_fixed_array = factory->empty_fixed_array(); for (size_t i = 0; i < arraysize(maps); i++) { Handle map = maps[i]; Handle result = Handle::cast( ft.Call(map, empty_fixed_array, empty_fixed_array).ToHandleChecked()); VERIFY(result, *map, *empty_fixed_array, *empty_fixed_array); CHECK(result->HasFastProperties()); #ifdef VERIFY_HEAP isolate->heap()->Verify(); #endif } } { // TODO(cbruni): handle in-object properties Handle object = Handle::cast( v8::Utils::OpenHandle(*CompileRun("var object = {a:1,b:2, 1:1, 2:2}; " "object"))); JSObject::NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, 0, "Normalize"); Handle result = Handle::cast( ft.Call(handle(object->map()), handle(object->properties()), handle(object->elements())) .ToHandleChecked()); VERIFY(result, object->map(), object->properties(), object->elements()); CHECK(!result->HasFastProperties()); #ifdef VERIFY_HEAP isolate->heap()->Verify(); #endif } #undef VERIFY } TEST(AllocateNameDictionary) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); { Node* capacity = m.Parameter(0); Node* result = m.AllocateNameDictionary(m.SmiUntag(capacity)); m.Return(result); } Handle code = data.GenerateCode(); FunctionTester ft(code, kNumParams); { for (int i = 0; i < 256; i = i * 1.1 + 1) { Handle result = ft.Call(handle(Smi::FromInt(i), isolate)).ToHandleChecked(); Handle dict = NameDictionary::New(isolate, i); // Both dictionaries should be memory equal. int size = FixedArrayBase::kHeaderSize + (dict->length() - 1) * kPointerSize; CHECK_EQ(0, memcmp(*dict, *result, size)); } } } TEST(PopAndReturnConstant) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 4; const int kNumProgrammaticParams = 2; CodeAssemblerTester data(isolate, kNumParams - kNumProgrammaticParams); CodeStubAssembler m(data.state()); // Call a function that return |kNumProgramaticParams| parameters in addition // to those specified by the static descriptor. |kNumProgramaticParams| is // specified as a constant. m.PopAndReturn(m.Int32Constant(kNumProgrammaticParams), m.SmiConstant(Smi::FromInt(1234))); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result; for (int test_count = 0; test_count < 100; ++test_count) { result = ft.Call(isolate->factory()->undefined_value(), Handle(Smi::FromInt(1234), isolate), isolate->factory()->undefined_value(), isolate->factory()->undefined_value()) .ToHandleChecked(); CHECK_EQ(1234, Handle::cast(result)->value()); } } TEST(PopAndReturnVariable) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 4; const int kNumProgrammaticParams = 2; CodeAssemblerTester data(isolate, kNumParams - kNumProgrammaticParams); CodeStubAssembler m(data.state()); // Call a function that return |kNumProgramaticParams| parameters in addition // to those specified by the static descriptor. |kNumProgramaticParams| is // passed in as a parameter to the function so that it can't be recongized as // a constant. m.PopAndReturn(m.SmiUntag(m.Parameter(1)), m.SmiConstant(Smi::FromInt(1234))); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result; for (int test_count = 0; test_count < 100; ++test_count) { result = ft.Call(isolate->factory()->undefined_value(), Handle(Smi::FromInt(1234), isolate), isolate->factory()->undefined_value(), Handle(Smi::FromInt(kNumProgrammaticParams), isolate)) .ToHandleChecked(); CHECK_EQ(1234, Handle::cast(result)->value()); } } TEST(OneToTwoByteStringCopy) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 2; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); m.CopyStringCharacters( m.Parameter(0), m.Parameter(1), m.SmiConstant(Smi::FromInt(0)), m.SmiConstant(Smi::FromInt(0)), m.SmiConstant(Smi::FromInt(5)), String::ONE_BYTE_ENCODING, String::TWO_BYTE_ENCODING, CodeStubAssembler::SMI_PARAMETERS); m.Return(m.SmiConstant(Smi::FromInt(0))); Handle code = data.GenerateCode(); CHECK(!code.is_null()); Handle string1 = isolate->factory()->InternalizeUtf8String("abcde"); uc16 array[] = {1000, 1001, 1002, 1003, 1004}; Vector str(array); Handle string2 = isolate->factory()->NewStringFromTwoByte(str).ToHandleChecked(); FunctionTester ft(code, 2); ft.Call(string1, string2); CHECK_EQ(Handle::cast(string1)->GetChars()[0], Handle::cast(string2)->GetChars()[0]); CHECK_EQ(Handle::cast(string1)->GetChars()[1], Handle::cast(string2)->GetChars()[1]); CHECK_EQ(Handle::cast(string1)->GetChars()[2], Handle::cast(string2)->GetChars()[2]); CHECK_EQ(Handle::cast(string1)->GetChars()[3], Handle::cast(string2)->GetChars()[3]); CHECK_EQ(Handle::cast(string1)->GetChars()[4], Handle::cast(string2)->GetChars()[4]); } TEST(OneToOneByteStringCopy) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 2; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); m.CopyStringCharacters( m.Parameter(0), m.Parameter(1), m.SmiConstant(Smi::FromInt(0)), m.SmiConstant(Smi::FromInt(0)), m.SmiConstant(Smi::FromInt(5)), String::ONE_BYTE_ENCODING, String::ONE_BYTE_ENCODING, CodeStubAssembler::SMI_PARAMETERS); m.Return(m.SmiConstant(Smi::FromInt(0))); Handle code = data.GenerateCode(); CHECK(!code.is_null()); Handle string1 = isolate->factory()->InternalizeUtf8String("abcde"); uint8_t array[] = {100, 101, 102, 103, 104}; Vector str(array); Handle string2 = isolate->factory()->NewStringFromOneByte(str).ToHandleChecked(); FunctionTester ft(code, 2); ft.Call(string1, string2); CHECK_EQ(Handle::cast(string1)->GetChars()[0], Handle::cast(string2)->GetChars()[0]); CHECK_EQ(Handle::cast(string1)->GetChars()[1], Handle::cast(string2)->GetChars()[1]); CHECK_EQ(Handle::cast(string1)->GetChars()[2], Handle::cast(string2)->GetChars()[2]); CHECK_EQ(Handle::cast(string1)->GetChars()[3], Handle::cast(string2)->GetChars()[3]); CHECK_EQ(Handle::cast(string1)->GetChars()[4], Handle::cast(string2)->GetChars()[4]); } TEST(OneToOneByteStringCopyNonZeroStart) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 2; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); m.CopyStringCharacters( m.Parameter(0), m.Parameter(1), m.SmiConstant(Smi::FromInt(0)), m.SmiConstant(Smi::FromInt(3)), m.SmiConstant(Smi::FromInt(2)), String::ONE_BYTE_ENCODING, String::ONE_BYTE_ENCODING, CodeStubAssembler::SMI_PARAMETERS); m.Return(m.SmiConstant(Smi::FromInt(0))); Handle code = data.GenerateCode(); CHECK(!code.is_null()); Handle string1 = isolate->factory()->InternalizeUtf8String("abcde"); uint8_t array[] = {100, 101, 102, 103, 104}; Vector str(array); Handle string2 = isolate->factory()->NewStringFromOneByte(str).ToHandleChecked(); FunctionTester ft(code, 2); ft.Call(string1, string2); CHECK_EQ(Handle::cast(string1)->GetChars()[0], Handle::cast(string2)->GetChars()[3]); CHECK_EQ(Handle::cast(string1)->GetChars()[1], Handle::cast(string2)->GetChars()[4]); CHECK_EQ(100, Handle::cast(string2)->GetChars()[0]); CHECK_EQ(101, Handle::cast(string2)->GetChars()[1]); CHECK_EQ(102, Handle::cast(string2)->GetChars()[2]); } TEST(TwoToTwoByteStringCopy) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 2; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); m.CopyStringCharacters( m.Parameter(0), m.Parameter(1), m.SmiConstant(Smi::FromInt(0)), m.SmiConstant(Smi::FromInt(0)), m.SmiConstant(Smi::FromInt(5)), String::TWO_BYTE_ENCODING, String::TWO_BYTE_ENCODING, CodeStubAssembler::SMI_PARAMETERS); m.Return(m.SmiConstant(Smi::FromInt(0))); Handle code = data.GenerateCode(); CHECK(!code.is_null()); uc16 array1[] = {2000, 2001, 2002, 2003, 2004}; Vector str1(array1); Handle string1 = isolate->factory()->NewStringFromTwoByte(str1).ToHandleChecked(); uc16 array2[] = {1000, 1001, 1002, 1003, 1004}; Vector str2(array2); Handle string2 = isolate->factory()->NewStringFromTwoByte(str2).ToHandleChecked(); FunctionTester ft(code, 2); ft.Call(string1, string2); CHECK_EQ(Handle::cast(string1)->GetChars()[0], Handle::cast(string2)->GetChars()[0]); CHECK_EQ(Handle::cast(string1)->GetChars()[1], Handle::cast(string2)->GetChars()[1]); CHECK_EQ(Handle::cast(string1)->GetChars()[2], Handle::cast(string2)->GetChars()[2]); CHECK_EQ(Handle::cast(string1)->GetChars()[3], Handle::cast(string2)->GetChars()[3]); CHECK_EQ(Handle::cast(string1)->GetChars()[4], Handle::cast(string2)->GetChars()[4]); } TEST(Arguments) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 4; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); CodeStubArguments arguments(&m, m.IntPtrConstant(3)); CSA_ASSERT( &m, m.WordEqual(arguments.AtIndex(0), m.SmiConstant(Smi::FromInt(12)))); CSA_ASSERT( &m, m.WordEqual(arguments.AtIndex(1), m.SmiConstant(Smi::FromInt(13)))); CSA_ASSERT( &m, m.WordEqual(arguments.AtIndex(2), m.SmiConstant(Smi::FromInt(14)))); m.Return(arguments.GetReceiver()); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call(isolate->factory()->undefined_value(), Handle(Smi::FromInt(12), isolate), Handle(Smi::FromInt(13), isolate), Handle(Smi::FromInt(14), isolate)) .ToHandleChecked(); CHECK_EQ(*isolate->factory()->undefined_value(), *result); } TEST(ArgumentsForEach) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 4; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); CodeStubArguments arguments(&m, m.IntPtrConstant(3)); CodeStubAssembler::Variable sum(&m, MachineType::PointerRepresentation()); CodeStubAssembler::VariableList list({&sum}, m.zone()); sum.Bind(m.IntPtrConstant(0)); arguments.ForEach( list, [&m, &sum](Node* arg) { sum.Bind(m.IntPtrAdd(sum.value(), arg)); }); m.Return(sum.value()); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call(isolate->factory()->undefined_value(), Handle(Smi::FromInt(12), isolate), Handle(Smi::FromInt(13), isolate), Handle(Smi::FromInt(14), isolate)) .ToHandleChecked(); CHECK_EQ(Smi::FromInt(12 + 13 + 14), *result); } TEST(IsDebugActive) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); CodeStubAssembler::Label if_active(&m), if_not_active(&m); m.Branch(m.IsDebugActive(), &if_active, &if_not_active); m.Bind(&if_active); m.Return(m.TrueConstant()); m.Bind(&if_not_active); m.Return(m.FalseConstant()); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); CHECK_EQ(false, isolate->debug()->is_active()); Handle result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); CHECK_EQ(isolate->heap()->false_value(), *result); bool* debug_is_active = reinterpret_cast( ExternalReference::debug_is_active_address(isolate).address()); // Cheat to enable debug (TODO: do this properly). *debug_is_active = true; result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); CHECK_EQ(isolate->heap()->true_value(), *result); // Reset debug mode. *debug_is_active = false; } class AppendJSArrayCodeStubAssembler : public CodeStubAssembler { public: AppendJSArrayCodeStubAssembler(compiler::CodeAssemblerState* state, ElementsKind kind) : CodeStubAssembler(state), kind_(kind) {} void TestAppendJSArrayImpl(Isolate* isolate, CodeAssemblerTester* tester, Object* o1, Object* o2, Object* o3, Object* o4, int initial_size, int result_size) { typedef CodeStubAssembler::Variable Variable; typedef CodeStubAssembler::Label Label; Handle array = isolate->factory()->NewJSArray( kind_, 2, initial_size, INITIALIZE_ARRAY_ELEMENTS_WITH_HOLE); JSObject::SetElement(isolate, array, 0, Handle(Smi::FromInt(1), isolate), SLOPPY) .Check(); JSObject::SetElement(isolate, array, 1, Handle(Smi::FromInt(2), isolate), SLOPPY) .Check(); CodeStubArguments args(this, IntPtrConstant(kNumParams)); Variable arg_index(this, MachineType::PointerRepresentation()); Label bailout(this); arg_index.Bind(IntPtrConstant(0)); Node* length = BuildAppendJSArray( kind_, HeapConstant(Handle(isolate->context(), isolate)), HeapConstant(array), args, arg_index, &bailout); Return(length); Bind(&bailout); Return(SmiTag(IntPtrAdd(arg_index.value(), IntPtrConstant(2)))); Handle code = tester->GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call(Handle(o1, isolate), Handle(o2, isolate), Handle(o3, isolate), Handle(o4, isolate)) .ToHandleChecked(); CHECK_EQ(kind_, array->GetElementsKind()); CHECK_EQ(result_size, Handle::cast(result)->value()); CHECK_EQ(result_size, Smi::cast(array->length())->value()); Object* obj = *JSObject::GetElement(isolate, array, 2).ToHandleChecked(); CHECK_EQ(result_size < 3 ? isolate->heap()->undefined_value() : o1, obj); obj = *JSObject::GetElement(isolate, array, 3).ToHandleChecked(); CHECK_EQ(result_size < 4 ? isolate->heap()->undefined_value() : o2, obj); obj = *JSObject::GetElement(isolate, array, 4).ToHandleChecked(); CHECK_EQ(result_size < 5 ? isolate->heap()->undefined_value() : o3, obj); obj = *JSObject::GetElement(isolate, array, 5).ToHandleChecked(); CHECK_EQ(result_size < 6 ? isolate->heap()->undefined_value() : o4, obj); } static void TestAppendJSArray(Isolate* isolate, ElementsKind kind, Object* o1, Object* o2, Object* o3, Object* o4, int initial_size, int result_size) { CodeAssemblerTester data(isolate, kNumParams); AppendJSArrayCodeStubAssembler m(data.state(), kind); m.TestAppendJSArrayImpl(isolate, &data, o1, o2, o3, o4, initial_size, result_size); } private: static const int kNumParams = 4; ElementsKind kind_; }; TEST(BuildAppendJSArrayFastElement) { Isolate* isolate(CcTest::InitIsolateOnce()); AppendJSArrayCodeStubAssembler::TestAppendJSArray( isolate, FAST_ELEMENTS, Smi::FromInt(3), Smi::FromInt(4), Smi::FromInt(5), Smi::FromInt(6), 6, 6); } TEST(BuildAppendJSArrayFastElementGrow) { Isolate* isolate(CcTest::InitIsolateOnce()); AppendJSArrayCodeStubAssembler::TestAppendJSArray( isolate, FAST_ELEMENTS, Smi::FromInt(3), Smi::FromInt(4), Smi::FromInt(5), Smi::FromInt(6), 2, 6); } TEST(BuildAppendJSArrayFastSmiElement) { Isolate* isolate(CcTest::InitIsolateOnce()); AppendJSArrayCodeStubAssembler::TestAppendJSArray( isolate, FAST_SMI_ELEMENTS, Smi::FromInt(3), Smi::FromInt(4), Smi::FromInt(5), Smi::FromInt(6), 6, 6); } TEST(BuildAppendJSArrayFastSmiElementGrow) { Isolate* isolate(CcTest::InitIsolateOnce()); AppendJSArrayCodeStubAssembler::TestAppendJSArray( isolate, FAST_SMI_ELEMENTS, Smi::FromInt(3), Smi::FromInt(4), Smi::FromInt(5), Smi::FromInt(6), 2, 6); } TEST(BuildAppendJSArrayFastSmiElementObject) { Isolate* isolate(CcTest::InitIsolateOnce()); AppendJSArrayCodeStubAssembler::TestAppendJSArray( isolate, FAST_SMI_ELEMENTS, Smi::FromInt(3), Smi::FromInt(4), isolate->heap()->undefined_value(), Smi::FromInt(6), 6, 4); } TEST(BuildAppendJSArrayFastSmiElementObjectGrow) { Isolate* isolate(CcTest::InitIsolateOnce()); AppendJSArrayCodeStubAssembler::TestAppendJSArray( isolate, FAST_SMI_ELEMENTS, Smi::FromInt(3), Smi::FromInt(4), isolate->heap()->undefined_value(), Smi::FromInt(6), 2, 4); } TEST(BuildAppendJSArrayFastDoubleElements) { Isolate* isolate(CcTest::InitIsolateOnce()); AppendJSArrayCodeStubAssembler::TestAppendJSArray( isolate, FAST_DOUBLE_ELEMENTS, Smi::FromInt(3), Smi::FromInt(4), Smi::FromInt(5), Smi::FromInt(6), 6, 6); } TEST(BuildAppendJSArrayFastDoubleElementsGrow) { Isolate* isolate(CcTest::InitIsolateOnce()); AppendJSArrayCodeStubAssembler::TestAppendJSArray( isolate, FAST_DOUBLE_ELEMENTS, Smi::FromInt(3), Smi::FromInt(4), Smi::FromInt(5), Smi::FromInt(6), 2, 6); } TEST(BuildAppendJSArrayFastDoubleElementsObject) { Isolate* isolate(CcTest::InitIsolateOnce()); AppendJSArrayCodeStubAssembler::TestAppendJSArray( isolate, FAST_DOUBLE_ELEMENTS, Smi::FromInt(3), Smi::FromInt(4), isolate->heap()->undefined_value(), Smi::FromInt(6), 6, 4); } namespace { template void Recompile(Args... args) { Stub stub(args...); stub.DeleteStubFromCacheForTesting(); stub.GetCode(); } } // namespace TEST(CodeStubAssemblerGraphsCorrectness) { // The test does not work with interpreter because bytecode handlers taken // from the snapshot already refer to precompiled stubs from the snapshot // and there is no way to trigger bytecode handlers recompilation. if (FLAG_ignition || FLAG_turbo) return; v8::Isolate::CreateParams create_params; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* v8_isolate = v8::Isolate::New(create_params); Isolate* isolate = reinterpret_cast(v8_isolate); { v8::Isolate::Scope isolate_scope(v8_isolate); LocalContext env(v8_isolate); v8::HandleScope scope(v8_isolate); FLAG_csa_verify = true; // Recompile some stubs here. Recompile(isolate, LoadGlobalICState(NOT_INSIDE_TYPEOF)); Recompile(isolate, LoadGlobalICState(NOT_INSIDE_TYPEOF)); Recompile(isolate); Recompile(isolate); Recompile(isolate); Recompile(isolate); Recompile(isolate, StoreICState(STRICT)); Recompile(isolate, StoreICState(STRICT)); Recompile(isolate, StoreICState(STRICT)); Recompile(isolate, StoreICState(STRICT)); } v8_isolate->Dispose(); } TEST(IsPromiseHookEnabled) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); m.Return(m.SelectBooleanConstant(m.IsPromiseHookEnabled())); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); CHECK_EQ(false, isolate->IsPromiseHookEnabled()); Handle result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); CHECK_EQ(isolate->heap()->false_value(), *result); isolate->EnablePromiseHook(); result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); CHECK_EQ(isolate->heap()->true_value(), *result); isolate->DisablePromiseHook(); result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); CHECK_EQ(isolate->heap()->false_value(), *result); } TEST(AllocateJSPromise) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); Node* const context = m.Parameter(kNumParams + 2); Node* const promise = m.AllocateJSPromise(context); m.Return(promise); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); CHECK(result->IsJSPromise()); } TEST(PromiseInit) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); Node* const context = m.Parameter(kNumParams + 2); Node* const promise = m.AllocateJSPromise(context); m.PromiseInit(promise); m.Return(promise); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); CHECK(result->IsJSPromise()); Handle js_promise = Handle::cast(result); CHECK_EQ(kPromisePending, js_promise->status()); CHECK_EQ(isolate->heap()->undefined_value(), js_promise->result()); CHECK(!js_promise->has_handler()); } TEST(PromiseSet) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); Node* const context = m.Parameter(kNumParams + 2); Node* const promise = m.AllocateJSPromise(context); m.PromiseSet(promise, m.SmiConstant(kPromisePending), m.SmiConstant(1)); m.Return(promise); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); CHECK(result->IsJSPromise()); Handle js_promise = Handle::cast(result); CHECK_EQ(kPromisePending, js_promise->status()); CHECK_EQ(Smi::FromInt(1), js_promise->result()); CHECK(!js_promise->has_handler()); } TEST(AllocatePromiseReactionJobInfo) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); Node* const context = m.Parameter(kNumParams + 2); Node* const tasks = m.AllocateFixedArray(FAST_ELEMENTS, m.Int32Constant(1)); m.StoreFixedArrayElement(tasks, 0, m.UndefinedConstant()); Node* const deferred = m.AllocateFixedArray(FAST_ELEMENTS, m.Int32Constant(1)); m.StoreFixedArrayElement(deferred, 0, m.UndefinedConstant()); Node* const info = m.AllocatePromiseReactionJobInfo(m.SmiConstant(1), tasks, deferred, context); m.Return(info); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call(isolate->factory()->undefined_value()).ToHandleChecked(); CHECK(result->IsPromiseReactionJobInfo()); Handle promise_info = Handle::cast(result); CHECK_EQ(Smi::FromInt(1), promise_info->value()); CHECK(promise_info->tasks()->IsFixedArray()); CHECK(promise_info->deferred()->IsFixedArray()); CHECK(promise_info->context()->IsContext()); CHECK(promise_info->debug_id()->IsUndefined(isolate)); CHECK(promise_info->debug_name()->IsUndefined(isolate)); } TEST(IsSymbol) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); Node* const symbol = m.Parameter(0); m.Return(m.SelectBooleanConstant(m.IsSymbol(symbol))); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call(isolate->factory()->NewSymbol()).ToHandleChecked(); CHECK_EQ(isolate->heap()->true_value(), *result); result = ft.Call(isolate->factory()->empty_string()).ToHandleChecked(); CHECK_EQ(isolate->heap()->false_value(), *result); } TEST(IsPrivateSymbol) { Isolate* isolate(CcTest::InitIsolateOnce()); const int kNumParams = 1; CodeAssemblerTester data(isolate, kNumParams); CodeStubAssembler m(data.state()); Node* const symbol = m.Parameter(0); m.Return(m.SelectBooleanConstant(m.IsPrivateSymbol(symbol))); Handle code = data.GenerateCode(); CHECK(!code.is_null()); FunctionTester ft(code, kNumParams); Handle result = ft.Call(isolate->factory()->NewSymbol()).ToHandleChecked(); CHECK_EQ(isolate->heap()->false_value(), *result); result = ft.Call(isolate->factory()->empty_string()).ToHandleChecked(); CHECK_EQ(isolate->heap()->false_value(), *result); result = ft.Call(isolate->factory()->NewPrivateSymbol()).ToHandleChecked(); CHECK_EQ(isolate->heap()->true_value(), *result); } } // namespace internal } // namespace v8