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