|
| 1 | +#include <libplatform/libplatform.h> |
| 2 | +#include <v8-cppgc.h> |
| 3 | +#include <v8.h> |
| 4 | + |
| 5 | +#include <cppgc/allocation.h> |
| 6 | +#include <cppgc/default-platform.h> |
| 7 | +#include <cppgc/garbage-collected.h> |
| 8 | +#include <cppgc/heap.h> |
| 9 | +#include <cppgc/member.h> |
| 10 | +#include <cppgc/platform.h> |
| 11 | +#include <cppgc/visitor.h> |
| 12 | + |
| 13 | +class Wrappable : public v8::Object::Wrappable { |
| 14 | + public: |
| 15 | + void Trace(cppgc::Visitor* visitor) const override { |
| 16 | + v8::Object::Wrappable::Trace(visitor); |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +int main(int argc, char* argv[]) { |
| 21 | + std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform(); |
| 22 | + v8::V8::InitializePlatform(platform.get()); |
| 23 | + cppgc::InitializeProcess(platform->GetPageAllocator()); |
| 24 | + v8::V8::Initialize(); |
| 25 | + |
| 26 | + auto heap = v8::CppHeap::Create(platform.get(), v8::CppHeapCreateParams{{}}); |
| 27 | + v8::Isolate::CreateParams create_params; |
| 28 | + create_params.array_buffer_allocator = |
| 29 | + v8::ArrayBuffer::Allocator::NewDefaultAllocator(); |
| 30 | + create_params.cpp_heap = heap.release(); |
| 31 | + |
| 32 | + v8::Isolate* isolate = v8::Isolate::New(create_params); |
| 33 | + { |
| 34 | + v8::Isolate::Scope isolate_scope(isolate); |
| 35 | + v8::HandleScope handle_scope(isolate); |
| 36 | + v8::Local<v8::Context> context = v8::Context::New(isolate); |
| 37 | + v8::Context::Scope context_scope(context); |
| 38 | + |
| 39 | + v8::Local<v8::Object> obj = v8::Object::New(isolate); |
| 40 | + Wrappable* wrappable = cppgc::MakeGarbageCollected<Wrappable>( |
| 41 | + isolate->GetCppHeap()->GetAllocationHandle()); |
| 42 | + v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag>( |
| 43 | + isolate, obj, wrappable); |
| 44 | + v8::Local<v8::String> source = |
| 45 | + v8::String::NewFromUtf8Literal(isolate, "'Hello' + ', World!'"); |
| 46 | + v8::Local<v8::Script> script = |
| 47 | + v8::Script::Compile(context, source).ToLocalChecked(); |
| 48 | + v8::Local<v8::Value> result = script->Run(context).ToLocalChecked(); |
| 49 | + v8::String::Utf8Value utf8(isolate, result); |
| 50 | + printf("%s\n", *utf8); |
| 51 | + } |
| 52 | + |
| 53 | + isolate->Dispose(); |
| 54 | + cppgc::ShutdownProcess(); |
| 55 | + v8::V8::Dispose(); |
| 56 | + v8::V8::DisposePlatform(); |
| 57 | + delete create_params.array_buffer_allocator; |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
0 commit comments