|
| 1 | +// Copyright 2020 the V8 project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "src/api/api.h" |
| 6 | +#include "src/base/platform/semaphore.h" |
| 7 | +#include "src/handles/handles-inl.h" |
| 8 | +#include "src/handles/local-handles-inl.h" |
| 9 | +#include "src/handles/persistent-handles.h" |
| 10 | +#include "src/heap/heap.h" |
| 11 | +#include "src/heap/local-heap.h" |
| 12 | +#include "test/cctest/cctest.h" |
| 13 | +#include "test/cctest/heap/heap-utils.h" |
| 14 | + |
| 15 | +namespace v8 { |
| 16 | +namespace internal { |
| 17 | + |
| 18 | +static constexpr int kNumHandles = kHandleBlockSize * 2 + kHandleBlockSize / 2; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +class PersistentHandlesThread final : public v8::base::Thread { |
| 23 | + public: |
| 24 | + PersistentHandlesThread(Heap* heap, std::vector<Handle<JSObject>> handles, |
| 25 | + std::unique_ptr<PersistentHandles> ph, |
| 26 | + Handle<Name> name, base::Semaphore* sema_started) |
| 27 | + : v8::base::Thread(base::Thread::Options("ThreadWithLocalHeap")), |
| 28 | + heap_(heap), |
| 29 | + handles_(std::move(handles)), |
| 30 | + ph_(std::move(ph)), |
| 31 | + name_(name), |
| 32 | + sema_started_(sema_started) {} |
| 33 | + |
| 34 | + void Run() override { |
| 35 | + LocalHeap local_heap(heap_, std::move(ph_)); |
| 36 | + LocalHandleScope scope(&local_heap); |
| 37 | + Address object = handles_[0]->ptr(); |
| 38 | + |
| 39 | + for (int i = 0; i < kNumHandles; i++) { |
| 40 | + handles_.push_back( |
| 41 | + Handle<JSObject>::cast(local_heap.NewPersistentHandle(object))); |
| 42 | + } |
| 43 | + |
| 44 | + sema_started_->Signal(); |
| 45 | + |
| 46 | + for (Handle<JSObject> handle : handles_) { |
| 47 | + // Lookup the named property on the {map}. |
| 48 | + CHECK(name_->IsUniqueName()); |
| 49 | + Handle<Map> map(handle->map(), &local_heap); |
| 50 | + |
| 51 | + Handle<DescriptorArray> descriptors( |
| 52 | + map->synchronized_instance_descriptors(), &local_heap); |
| 53 | + bool is_background_thread = true; |
| 54 | + InternalIndex const number = |
| 55 | + descriptors->Search(*name_, *map, is_background_thread); |
| 56 | + CHECK(number.is_found()); |
| 57 | + } |
| 58 | + |
| 59 | + CHECK_EQ(handles_.size(), kNumHandles * 2); |
| 60 | + |
| 61 | + CHECK(!ph_); |
| 62 | + ph_ = local_heap.DetachPersistentHandles(); |
| 63 | + } |
| 64 | + |
| 65 | + Heap* heap_; |
| 66 | + std::vector<Handle<JSObject>> handles_; |
| 67 | + std::unique_ptr<PersistentHandles> ph_; |
| 68 | + Handle<Name> name_; |
| 69 | + base::Semaphore* sema_started_; |
| 70 | +}; |
| 71 | + |
| 72 | +// Uses linear search on a flat object, with up to 8 elements. |
| 73 | +TEST(LinearSearchFlatObject) { |
| 74 | + CcTest::InitializeVM(); |
| 75 | + FLAG_local_heaps = true; |
| 76 | + Isolate* isolate = CcTest::i_isolate(); |
| 77 | + |
| 78 | + std::unique_ptr<PersistentHandles> ph = isolate->NewPersistentHandles(); |
| 79 | + std::vector<Handle<JSObject>> handles; |
| 80 | + |
| 81 | + auto factory = isolate->factory(); |
| 82 | + HandleScope handle_scope(isolate); |
| 83 | + |
| 84 | + Handle<JSFunction> function = |
| 85 | + factory->NewFunctionForTest(factory->empty_string()); |
| 86 | + Handle<JSObject> js_object = factory->NewJSObject(function); |
| 87 | + Handle<String> name = CcTest::MakeString("property"); |
| 88 | + Handle<Object> value = CcTest::MakeString("dummy_value"); |
| 89 | + // For the default constructor function no in-object properties are reserved |
| 90 | + // hence adding a single property will initialize the property-array. |
| 91 | + JSObject::DefinePropertyOrElementIgnoreAttributes(js_object, name, value, |
| 92 | + NONE) |
| 93 | + .Check(); |
| 94 | + |
| 95 | + Address object = js_object->ptr(); |
| 96 | + for (int i = 0; i < kNumHandles; i++) { |
| 97 | + handles.push_back(Handle<JSObject>::cast(ph->NewHandle(object))); |
| 98 | + } |
| 99 | + |
| 100 | + Handle<Name> persistent_name = Handle<Name>::cast(ph->NewHandle(name->ptr())); |
| 101 | + |
| 102 | + base::Semaphore sema_started(0); |
| 103 | + |
| 104 | + // Pass persistent handles to background thread. |
| 105 | + std::unique_ptr<PersistentHandlesThread> thread(new PersistentHandlesThread( |
| 106 | + isolate->heap(), std::move(handles), std::move(ph), persistent_name, |
| 107 | + &sema_started)); |
| 108 | + CHECK(thread->Start()); |
| 109 | + |
| 110 | + sema_started.Wait(); |
| 111 | + |
| 112 | + // Exercise descriptor in main thread too. |
| 113 | + for (int i = 0; i < 7; ++i) { |
| 114 | + Handle<String> filler_name = CcTest::MakeName("filler_property_", i); |
| 115 | + Handle<Object> filler_value = CcTest::MakeString("dummy_value"); |
| 116 | + JSObject::DefinePropertyOrElementIgnoreAttributes(js_object, filler_name, |
| 117 | + filler_value, NONE) |
| 118 | + .Check(); |
| 119 | + } |
| 120 | + CHECK_EQ(js_object->map().NumberOfOwnDescriptors(), 8); |
| 121 | + |
| 122 | + thread->Join(); |
| 123 | +} |
| 124 | + |
| 125 | +} // anonymous namespace |
| 126 | + |
| 127 | +} // namespace internal |
| 128 | +} // namespace v8 |
0 commit comments