Skip to content

Creating a new Closure #508

Description

@thestrangedev

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.

Activity

  1. KevinEady commented on Jul 15, 2019

    @KevinEady
    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?

  2. Veetaha commented on Aug 23, 2019

    @Veetaha

    In simple words, I assume that the core problem here is that handles can't outlive the execution time of the invoked c++ handler.

  3. gabrielschulhof commented on Sep 23, 2019

    @gabrielschulhof
    Contributor

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

  4. added a commit that references this issue on Dec 11, 2019
  5. added a commit that references this issue on Aug 24, 2022
  6. added a commit that references this issue on Aug 26, 2022
  7. added a commit that references this issue on Sep 19, 2022
  8. added a commit that references this issue on Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions