Closed
Description
I want to create a decorator like this by Js
function Decorator (objectA) {
return function (target, propertyKey, descriptor) {
const backup = descriptor.value;
descriptor.value = function (someParameters) {
//do something with someParameters and objectA
return backup.call(this, someParameters);
}
return descriptor;
}
}And my C++ code
Value Decorator (const CallbackInfo& info) {
//do something...
auto objectA = info[0].As<Object>();
return Function::New(info.Env(), [objectA] (const CallbackInfo& info) -> Value {
//do something...
auto target = info[0].As<Object>();
auto propertyKey = info[1].As<String>();
auto descriptor = info[2].As<Object>();
auto backup = descriptor.Get("value").As<Function>();
descriptor.Set("value", Function::New(env, [backup, descriptor, objectA] (const CallbackInfo& info) -> Value {
//I want to access backup, objectA, but they were already either deleted or changed to something new
}
return descriptor;
}
}With scalar types, I convert them to C++ types, so there's no problem. How about other types like object, function and array? I found the void* data argument can be passed to Funtion::New, but I didn't see any example.
Metadata
Metadata
Assignees
Labels
No labels
Activity
KevinEady commentedon Jul 15, 2019
Hi @thai6070 ,
The
void* dataargument ofFunction::Newwill allow you to set arbitrary data for the function call, such that this data can be retrieved viacbInfo.Data()inside the function's C++ implementation. Does this help? I'm not sure this actually solves your problem though, or what you are requesting an example / documentation of?Veetaha commentedon Aug 23, 2019
In simple words, I assume that the core problem here is that handles can't outlive the execution time of the invoked c++ handler.
gabrielschulhof commentedon Sep 23, 2019
@thai6070 Hmmm ... we don't actually have a way of creating a weak reference with a callback.
implement Object::AddFinalizer
implement Object::AddFinalizer
add closure example
implement Object::AddFinalizer
implement Object::AddFinalizer
implement Object::AddFinalizer
implement Object::AddFinalizer