Accessing class variables within static function bound with FunctionTemplate

42 views
Skip to first unread message

Jack

unread,
Aug 1, 2016, 7:49:38 PM8/1/16
to v8-users
Not sure if this is the right place to ask for help, sorry.

I am using 
global->Set(String::NewFromUtf8(isolate, "print", NewStringType::kNormal). ToLocalChecked(), FunctionTemplate::New(isolate, V8Instance::js_print));
to create a print() function in JS from the following function:
static void js_print(const v8::FunctionCallbackInfo<v8::Value> &args);
Implementation:
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: 
  • Use js_print as a non-static class member function
  • Pass a reference or pointer to my class variable to js_print such that is available whenever it is called

Thanks for any help in advance. :)

Ben Noordhuis

unread,
Aug 2, 2016, 2:23:09 AM8/2/16
to v8-users
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().

Jack

unread,
Aug 2, 2016, 7:10:27 AM8/2/16
to v8-users
On Tuesday, 2 August 2016 07:23:09 UTC+1, Ben Noordhuis wrote:
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().

 Thanks for the reply. Sorry for the trouble, but could someone show an example of creating an External object from a pointer to my class? I assume I need to cast the pointer to void * but I can't figure out how to create the object.

Thanks.

Jack

unread,
Aug 2, 2016, 7:44:40 AM8/2/16
to v8-users
Ok I figured out I need to use:
Local<External> self = External::New(isolate, (void *) this);

However I can't figure out how to extract the pointer again in the function. How do I cast the Local<External> returned by args.Data() back to MyClass *

Ben Noordhuis

unread,
Aug 3, 2016, 3:22:17 AM8/3/16
to v8-users
On Tue, Aug 2, 2016 at 1:44 PM, Jack <jack....@gmail.com> wrote:
> Ok I figured out I need to use:
> Local<External> self = External::New(isolate, (void *) this);
>
> However I can't figure out how to extract the pointer again in the function.
> How do I cast the Local<External> returned by args.Data() back to MyClass *

auto data = args.Data().As<v8::External>();
auto self = static_cast<MyClass*>(data->Value());

Jack

unread,
Aug 3, 2016, 4:56:25 AM8/3/16
to v8-users
Great, thanks for all your help Ben! :)
Reply all
Reply to author
Forward
0 new messages