Not sure if this is the right place to ask for help, sorry. I am usingto create a print() function in JS from the following function:
global->Set(String::NewFromUtf8(isolate, "print", NewStringType::kNormal). ToLocalChecked(), FunctionTemplate::New(isolate, V8Instance::js_print));Implementation:
static void js_print(const v8::FunctionCallbackInfo<v8::Value> &args);
void V8Instance::js_print(const v8::FunctionCallbackInfo<v8::Value> &args) {
for (int i = 0; i < args.Length(); i++) {
v8::HandleScope handle_scope(args.GetIsolate());
v8::String::Utf8Value str(args[i]);
std::cout << *str << std::endl;
}
std::cout << std::endl;
}
As you may be able to tell, both of these are happening inside a class, V8Instance. This works fine, but I have to make js_print a static function to use it in FunctionTemplate::New.
My problem is that I need access to a variable which is a non-static member of V8Instance within the js_print function. Is there a way I can either:
Thanks for any help in advance. :)
You can wrap a pointer to your instance in a v8::External that you
then pass as the |data| argument to v8::FunctionTemplate::New(). It's
available in the callback as args.Data().
Local<External> self = External::New(isolate, (void *) this);