Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a new Closure #508

Closed
thestrangedev opened this issue Jun 30, 2019 · 3 comments
Closed

Creating a new Closure #508

thestrangedev opened this issue Jun 30, 2019 · 3 comments

Comments

@thestrangedev
Copy link

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.

@KevinEady
Copy link
Contributor

Hi @thai6070 ,

The void* data argument of Function::New will allow you to set arbitrary data for the function call, such that this data can be retrieved via cbInfo.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
Copy link

Veetaha commented 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
Copy link
Contributor

@thai6070 Hmmm ... we don't actually have a way of creating a weak reference with a callback.

gabrielschulhof pushed a commit to gabrielschulhof/node-addon-api that referenced this issue Sep 27, 2019
This allows one to tie the life cycle of one JavaScript object to
another. In effect, this allows for JavaScript-style closures.

Re: nodejs#508
gabrielschulhof pushed a commit to gabrielschulhof/node-addon-api that referenced this issue Sep 27, 2019
This allows one to tie the life cycle of one JavaScript object to
another. In effect, this allows for JavaScript-style closures.

Fixes: nodejs#508
gabrielschulhof pushed a commit to gabrielschulhof/node-addon-examples that referenced this issue Dec 11, 2019
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this issue Aug 24, 2022
This allows one to tie the life cycle of one JavaScript object to
another. In effect, this allows for JavaScript-style closures.

Fixes: nodejs/node-addon-api#508
PR_URL: nodejs/node-addon-api#551
Reviewed-By: Kevin Eady <[email protected]>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this issue Aug 26, 2022
This allows one to tie the life cycle of one JavaScript object to
another. In effect, this allows for JavaScript-style closures.

Fixes: nodejs/node-addon-api#508
PR_URL: nodejs/node-addon-api#551
Reviewed-By: Kevin Eady <[email protected]>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this issue Sep 19, 2022
This allows one to tie the life cycle of one JavaScript object to
another. In effect, this allows for JavaScript-style closures.

Fixes: nodejs/node-addon-api#508
PR_URL: nodejs/node-addon-api#551
Reviewed-By: Kevin Eady <[email protected]>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this issue Aug 11, 2023
This allows one to tie the life cycle of one JavaScript object to
another. In effect, this allows for JavaScript-style closures.

Fixes: nodejs/node-addon-api#508
PR_URL: nodejs/node-addon-api#551
Reviewed-By: Kevin Eady <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants