|
| 1 | +#include <cppgc/allocation.h> |
| 2 | +#include <cppgc/garbage-collected.h> |
| 3 | +#include <cppgc/heap.h> |
| 4 | +#include <node.h> |
| 5 | +#include <v8-cppgc.h> |
| 6 | +#include <v8.h> |
| 7 | +#include "node_test_fixture.h" |
| 8 | + |
| 9 | +// This tests that Node.js can work with an existing CppHeap. |
| 10 | + |
| 11 | +// Mimic the Blink layout. |
| 12 | +static int kWrappableTypeIndex = 0; |
| 13 | +static int kWrappableInstanceIndex = 1; |
| 14 | +static uint16_t kEmbedderID = 0x1; |
| 15 | + |
| 16 | +// Mimic a class that does not know about Node.js. |
| 17 | +class CppGCed : public cppgc::GarbageCollected<CppGCed> { |
| 18 | + public: |
| 19 | + static int kConstructCount; |
| 20 | + static int kDestructCount; |
| 21 | + static int kTraceCount; |
| 22 | + |
| 23 | + static void New(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 24 | + v8::Isolate* isolate = args.GetIsolate(); |
| 25 | + v8::Local<v8::Object> js_object = args.This(); |
| 26 | + CppGCed* gc_object = cppgc::MakeGarbageCollected<CppGCed>( |
| 27 | + isolate->GetCppHeap()->GetAllocationHandle()); |
| 28 | + js_object->SetAlignedPointerInInternalField(kWrappableTypeIndex, |
| 29 | + &kEmbedderID); |
| 30 | + js_object->SetAlignedPointerInInternalField(kWrappableInstanceIndex, |
| 31 | + gc_object); |
| 32 | + kConstructCount++; |
| 33 | + args.GetReturnValue().Set(js_object); |
| 34 | + } |
| 35 | + |
| 36 | + static v8::Local<v8::Function> GetConstructor( |
| 37 | + v8::Local<v8::Context> context) { |
| 38 | + auto ft = v8::FunctionTemplate::New(context->GetIsolate(), New); |
| 39 | + auto ot = ft->InstanceTemplate(); |
| 40 | + ot->SetInternalFieldCount(2); |
| 41 | + return ft->GetFunction(context).ToLocalChecked(); |
| 42 | + } |
| 43 | + |
| 44 | + CppGCed() = default; |
| 45 | + |
| 46 | + ~CppGCed() { kDestructCount++; } |
| 47 | + |
| 48 | + void Trace(cppgc::Visitor* visitor) const { kTraceCount++; } |
| 49 | +}; |
| 50 | + |
| 51 | +int CppGCed::kConstructCount = 0; |
| 52 | +int CppGCed::kDestructCount = 0; |
| 53 | +int CppGCed::kTraceCount = 0; |
| 54 | + |
| 55 | +TEST_F(NodeZeroIsolateTestFixture, ExistingCppHeapTest) { |
| 56 | + v8::Isolate* isolate = |
| 57 | + node::NewIsolate(allocator.get(), ¤t_loop, platform.get()); |
| 58 | + |
| 59 | + // Create and attach the CppHeap before we set up the IsolateData so that |
| 60 | + // it recognizes the existing heap. |
| 61 | + std::unique_ptr<v8::CppHeap> cpp_heap = v8::CppHeap::Create( |
| 62 | + platform.get(), |
| 63 | + v8::CppHeapCreateParams( |
| 64 | + {}, |
| 65 | + v8::WrapperDescriptor( |
| 66 | + kWrappableTypeIndex, kWrappableInstanceIndex, kEmbedderID))); |
| 67 | + isolate->AttachCppHeap(cpp_heap.get()); |
| 68 | + |
| 69 | + // Try creating Context + IsolateData + Environment. |
| 70 | + { |
| 71 | + v8::Isolate::Scope isolate_scope(isolate); |
| 72 | + v8::HandleScope handle_scope(isolate); |
| 73 | + |
| 74 | + std::unique_ptr<node::IsolateData, decltype(&node::FreeIsolateData)> |
| 75 | + isolate_data{ |
| 76 | + node::CreateIsolateData(isolate, ¤t_loop, platform.get()), |
| 77 | + node::FreeIsolateData}; |
| 78 | + CHECK(isolate_data); |
| 79 | + |
| 80 | + { |
| 81 | + auto context = node::NewContext(isolate); |
| 82 | + CHECK(!context.IsEmpty()); |
| 83 | + v8::Context::Scope context_scope(context); |
| 84 | + |
| 85 | + // An Environment should already contain a few BaseObjects that are |
| 86 | + // supposed to have non-cppgc IDs. |
| 87 | + std::unique_ptr<node::Environment, decltype(&node::FreeEnvironment)> |
| 88 | + environment{ |
| 89 | + node::CreateEnvironment(isolate_data.get(), context, {}, {}), |
| 90 | + node::FreeEnvironment}; |
| 91 | + CHECK(environment); |
| 92 | + |
| 93 | + context->Global() |
| 94 | + ->Set(context, |
| 95 | + v8::String::NewFromUtf8(isolate, "CppGCed").ToLocalChecked(), |
| 96 | + CppGCed::GetConstructor(context)) |
| 97 | + .FromJust(); |
| 98 | + |
| 99 | + const char* code = |
| 100 | + "globalThis.array = [];" |
| 101 | + "for (let i = 0; i < 100; ++i) { array.push(new CppGCed()); }"; |
| 102 | + node::LoadEnvironment(environment.get(), code).ToLocalChecked(); |
| 103 | + // Request a GC and check if CppGCed::Trace() is invoked. |
| 104 | + isolate->LowMemoryNotification(); |
| 105 | + int exit_code = SpinEventLoop(environment.get()).FromJust(); |
| 106 | + EXPECT_EQ(exit_code, 0); |
| 107 | + } |
| 108 | + |
| 109 | + platform->DrainTasks(isolate); |
| 110 | + |
| 111 | + // Cleanup. |
| 112 | + isolate->DetachCppHeap(); |
| 113 | + cpp_heap->Terminate(); |
| 114 | + platform->DrainTasks(isolate); |
| 115 | + } |
| 116 | + |
| 117 | + platform->UnregisterIsolate(isolate); |
| 118 | + isolate->Dispose(); |
| 119 | + |
| 120 | + // Check that all the objects are created and destroyed properly. |
| 121 | + EXPECT_EQ(CppGCed::kConstructCount, 100); |
| 122 | + EXPECT_EQ(CppGCed::kDestructCount, 100); |
| 123 | + // GC does not have to feel pressured enough to visit all of them to |
| 124 | + // reclaim memory before we are done with the isolate and the entire |
| 125 | + // heap can be reclaimed. So just check at least some of them are traced. |
| 126 | + EXPECT_GT(CppGCed::kTraceCount, 0); |
| 127 | +} |
0 commit comments