v8::V8::InitializeICU();
v8::Persistent<v8::Context> persistentContext;
v8::Isolate *pIsolate;
void init() {
platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(&(*platform.get()));
v8::V8::Initialize();
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
pIsolate = v8::Isolate::New(create_params);auto ft = v8::FunctionTemplate::New(pIsolate);
ft->SetClassName(v8::String::NewFromUtf8(pIsolate, "MyClass"));
ft->ReadOnlyPrototype();
ft->SetCallHandler(MyClass::constructorCallback);
ft->SetLength(1);
v8::Eternal<v8::FunctionTemplate> v8MyClassConstructor(pIsolate, ft);
auto global_template = v8::ObjectTemplate::New(pIsolate);
global_template->Set(v8::String::NewFromUtf8(pIsolate, "MyClass"), v8MyClassConstructor.Get(pIsolate));
// Enter the context for compiling and running the hello world script.
v8::Local<v8::Context> context = v8::Context::New(pIsolate, nullptr, global_template);
persistentContext.Reset(pIsolate, context);
}
void bindClass() {
auto ctx = persistentContext.Get(pIsolate);
auto global = ctx->GetGlobalObjectTemplate(); // No such function, trying to get global_template variable from init() method
auto ft = v8::FunctionTemplate::New(pIsolate); ft->SetClassName(v8::String::NewFromUtf8(pIsolate, "Foo"));
// set up another class called "Foo"
global->Set(v8::String::NewFromUtf8(pIsolate, "Foo"), ft));
}
void main() {
init();
// some code here...
bindClass(); // bind more class to global later...
}// Create a template for the global object and set the
// built-in global functions.
Local<ObjectTemplate> global = ObjectTemplate::New(isolate);
global->Set(String::NewFromUtf8(isolate, "log"),
FunctionTemplate::New(isolate, LogCallback));
// Each processor gets its own context so different processors
// do not affect each other.
Persistent<Context> context = Context::New(isolate, NULL, global);