|
| 1 | +// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +#include "platform/assert.h" |
| 6 | + |
| 7 | +#include "vm/globals.h" |
| 8 | +#include "vm/symbols.h" |
| 9 | +#include "vm/unit_test.h" |
| 10 | + |
| 11 | +namespace dart { |
| 12 | + |
| 13 | +static intptr_t GetHash(Isolate* isolate, const ObjectPtr obj) { |
| 14 | +#if defined(HASH_IN_OBJECT_HEADER) |
| 15 | + return Object::GetCachedHash(obj); |
| 16 | +#else |
| 17 | + Heap* heap = isolate->group()->heap(); |
| 18 | + ASSERT(obj->IsDartInstance()); |
| 19 | + return heap->GetHash(obj); |
| 20 | +#endif |
| 21 | +} |
| 22 | + |
| 23 | +ISOLATE_UNIT_TEST_CASE(AsmIntrinsifier_SetHashIfNotSetYet) { |
| 24 | + auto I = Isolate::Current(); |
| 25 | + const auto& corelib = Library::Handle(Library::CoreLibrary()); |
| 26 | + const auto& name = String::Handle(String::New("_setHashIfNotSetYet")); |
| 27 | + const auto& symbol = String::Handle(Symbols::New(thread, name)); |
| 28 | + |
| 29 | + const auto& function = |
| 30 | + Function::Handle(corelib.LookupFunctionAllowPrivate(symbol)); |
| 31 | + const auto& object_class = |
| 32 | + Class::Handle(corelib.LookupClass(Symbols::Object())); |
| 33 | + |
| 34 | + auto& smi0 = Smi::Handle(Smi::New(0)); |
| 35 | + auto& smi21 = Smi::Handle(Smi::New(21)); |
| 36 | + auto& smi42 = Smi::Handle(Smi::New(42)); |
| 37 | + const auto& obj = Object::Handle(Instance::New(object_class)); |
| 38 | + const auto& args = Array::Handle(Array::New(2)); |
| 39 | + |
| 40 | + const auto& args_descriptor_array = |
| 41 | + Array::Handle(ArgumentsDescriptor::NewBoxed(0, 2, Array::empty_array())); |
| 42 | + |
| 43 | + // Initialized to 0 |
| 44 | + EXPECT_EQ(smi0.ptr(), Smi::New(GetHash(I, obj.ptr()))); |
| 45 | + |
| 46 | + // Lazily set to 42 on first call. |
| 47 | + args.SetAt(0, obj); |
| 48 | + args.SetAt(1, smi42); |
| 49 | + EXPECT_EQ(smi42.ptr(), |
| 50 | + DartEntry::InvokeFunction(function, args, args_descriptor_array)); |
| 51 | + EXPECT_EQ(smi42.ptr(), Smi::New(GetHash(I, obj.ptr()))); |
| 52 | + |
| 53 | + // Stays at 42 on subsequent calls. |
| 54 | + args.SetAt(0, obj); |
| 55 | + args.SetAt(1, smi21); |
| 56 | + EXPECT_EQ(smi42.ptr(), |
| 57 | + DartEntry::InvokeFunction(function, args, args_descriptor_array)); |
| 58 | + EXPECT_EQ(smi42.ptr(), Smi::New(GetHash(I, obj.ptr()))); |
| 59 | + |
| 60 | + const auto& obj2 = Object::Handle(Instance::New(object_class)); |
| 61 | + const auto& smiMax = Smi::Handle(Smi::New(0xffffffff)); |
| 62 | + |
| 63 | + // Initialized to 0 |
| 64 | + EXPECT_EQ(smi0.ptr(), Smi::New(GetHash(I, obj2.ptr()))); |
| 65 | + |
| 66 | + // Lazily set to smiMax first call. |
| 67 | + args.SetAt(0, obj2); |
| 68 | + args.SetAt(1, smiMax); |
| 69 | + EXPECT_EQ(smiMax.ptr(), |
| 70 | + DartEntry::InvokeFunction(function, args, args_descriptor_array)); |
| 71 | + EXPECT_EQ(smiMax.ptr(), Smi::New(GetHash(I, obj2.ptr()))); |
| 72 | + |
| 73 | + // Stays at smiMax on subsequent calls. |
| 74 | + args.SetAt(0, obj2); |
| 75 | + args.SetAt(1, smi21); |
| 76 | + EXPECT_EQ(smiMax.ptr(), |
| 77 | + DartEntry::InvokeFunction(function, args, args_descriptor_array)); |
| 78 | + EXPECT_EQ(smiMax.ptr(), Smi::New(GetHash(I, obj2.ptr()))); |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace dart |
0 commit comments