On Thu, Jun 13, 2019 at 11:05 AM Darin Dimitrov
<darin.dimit...@gmail.com> wrote:
>
> I am trying to associate some custom C++ data with a Local<Function> instance.
>
> Currently I am able to achieve this only to instances created with this 
> function:
>
> Local<FunctionTemplate> tmpl = FunctionTemplate::New(isolate, [](const 
> FunctionCallbackInfo<Value> &info) {
>     Isolate* isolate = info.GetIsolate();
>     // Associate some custom C++ data with the instance created through this 
> function
>     info.This()->SetInternalField(0, External::New(isolate, nullptr));
> });
> tmpl->InstanceTemplate()->SetInternalFieldCount(1);
> Local<Function> func = tmpl->GetFunction(context).ToLocalChecked();
>
> Local<Object> instance = func->NewInstance(context).ToLocalChecked();
> // Getting back the C++ data associated with the instance
> Local<External> extData = instance->GetInternalField(0).As<External>();
>
> What I would like to achieve is to associate the function instance with 
> custom data:
>
> func->SetInternalField(0, External::New(isolate, nullptr));
>
> But unfortunately this throws an "Internal field out of bounds error".
>
> Is it possible to achieve this?

Yes, you can. Put the External on the Function with
Function::SetPrivate(). You can then retrieve it again with
Function::GetPrivate(). Create the Private key with Private::ForApi().

GetPrivate() and SetPrivate() are inherited from Object so it works
for Objects, Arrays, Maps and Sets as well.

-- 
-- 
v8-users mailing list
v8-users@googlegroups.com
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-users/CAHQurc9QnFDOdzKGWCpYJ3%3DixZizo_LhcTdEKZ8NLTwHzpM4SQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to