I'm trying to create a template for a wrapper around a C++ class in V8:
var obj1 = myfunc(1);
var obj2 = myfunc(2);
This should create two JS objects which are the wrappers around these C++ objects:
new MyClass(1, shared);
new MyClass(2, shared);
, shared
being a single instance of another class, shared between all objects in the scope.
Here's what I got so far:
static void myfuncConstructor(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
uint64_t var1;
if (args[0].IsEmpty() || !args[0]->IsNumber())
return;
var1 = args[0]->NumberValue();
AnotherClass* shared = IDontKnowHowToGetShared();
// ^ How do I do this?
MyClass* obj = new MyClass(var1, AnotherClass* shared);
// ^ this has to be shared between all instances
args.This()->SetInternalField(0, External::New(isolate, obj));
}
Local<FunctionTemplate> myfuncTemplate(Isolate* isolate, AnotherClass* shared)
{
Local<FunctionTemplate> t = FunctionTemplate::New(isolate, myfuncConstructor);
Local<ObjectTemplate> instance = t->InstanceTemplate();
instance->SetInternalFieldCount(1);
IDontKnowHowToSetShared(shared);
// ^ How do I do this?
return t;
}
GlobalClass::Compile (const char* code, AnotherClass* shared)
{
Isolate::Scope isolate_scope(pimpl_->isolate);
HandleScope handle_scope(pimpl_->isolate);
Handle<ObjectTemplate> global = ObjectTemplate::New(pimpl_->isolate);
global->Set(String::NewFromUtf8(pimpl_->isolate, "myfunc"), myfuncTemplate(pimpl_->isolate, shared));
...
}