Skip to content

Creating a new Closure #508

@thestrangedev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions