|
| 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 <algorithm> |
| 8 | + |
| 9 | +class CppGCed : public cppgc::GarbageCollected<CppGCed> { |
| 10 | + public: |
| 11 | + static uint16_t states[2]; |
| 12 | + static constexpr int kDestructCount = 0; |
| 13 | + static constexpr int kTraceCount = 1; |
| 14 | + |
| 15 | + static void New(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 16 | + v8::Isolate* isolate = args.GetIsolate(); |
| 17 | + v8::Local<v8::Object> js_object = args.This(); |
| 18 | + CppGCed* gc_object = cppgc::MakeGarbageCollected<CppGCed>( |
| 19 | + isolate->GetCppHeap()->GetAllocationHandle()); |
| 20 | + node::SetCppgcReference(isolate, js_object, gc_object); |
| 21 | + args.GetReturnValue().Set(js_object); |
| 22 | + } |
| 23 | + |
| 24 | + static v8::Local<v8::Function> GetConstructor( |
| 25 | + v8::Local<v8::Context> context) { |
| 26 | + auto ft = v8::FunctionTemplate::New(context->GetIsolate(), New); |
| 27 | + auto ot = ft->InstanceTemplate(); |
| 28 | + v8::WrapperDescriptor descriptor = |
| 29 | + context->GetIsolate()->GetCppHeap()->wrapper_descriptor(); |
| 30 | + uint16_t required_size = std::max(descriptor.wrappable_instance_index, |
| 31 | + descriptor.wrappable_type_index); |
| 32 | + ot->SetInternalFieldCount(required_size + 1); |
| 33 | + return ft->GetFunction(context).ToLocalChecked(); |
| 34 | + } |
| 35 | + |
| 36 | + CppGCed() = default; |
| 37 | + |
| 38 | + ~CppGCed() { states[kDestructCount]++; } |
| 39 | + |
| 40 | + void Trace(cppgc::Visitor* visitor) const { states[kTraceCount]++; } |
| 41 | +}; |
| 42 | + |
| 43 | +uint16_t CppGCed::states[] = {0, 0}; |
| 44 | + |
| 45 | +void InitModule(v8::Local<v8::Object> exports) { |
| 46 | + v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 47 | + auto context = isolate->GetCurrentContext(); |
| 48 | + |
| 49 | + auto store = v8::ArrayBuffer::NewBackingStore( |
| 50 | + CppGCed::states, |
| 51 | + sizeof(uint16_t) * 2, |
| 52 | + [](void*, size_t, void*) {}, |
| 53 | + nullptr); |
| 54 | + auto ab = v8::ArrayBuffer::New(isolate, std::move(store)); |
| 55 | + |
| 56 | + exports |
| 57 | + ->Set(context, |
| 58 | + v8::String::NewFromUtf8(isolate, "CppGCed").ToLocalChecked(), |
| 59 | + CppGCed::GetConstructor(context)) |
| 60 | + .FromJust(); |
| 61 | + exports |
| 62 | + ->Set(context, |
| 63 | + v8::String::NewFromUtf8(isolate, "states").ToLocalChecked(), |
| 64 | + v8::Uint16Array::New(ab, 0, 2)) |
| 65 | + .FromJust(); |
| 66 | + exports |
| 67 | + ->Set(context, |
| 68 | + v8::String::NewFromUtf8(isolate, "kDestructCount").ToLocalChecked(), |
| 69 | + v8::Integer::New(isolate, CppGCed::kDestructCount)) |
| 70 | + .FromJust(); |
| 71 | + exports |
| 72 | + ->Set(context, |
| 73 | + v8::String::NewFromUtf8(isolate, "kTraceCount").ToLocalChecked(), |
| 74 | + v8::Integer::New(isolate, CppGCed::kTraceCount)) |
| 75 | + .FromJust(); |
| 76 | +} |
| 77 | + |
| 78 | +NODE_MODULE(NODE_GYP_MODULE_NAME, InitModule) |
0 commit comments