Saturday 22 April 2017

V8 JavaScript: Why do C++ constructor functions filter Proxy?

It seems that V8 C++ code (tester) is not treating Proxy as an object and thus returning the default target instead. The same scenario in pure JavaScript (other) works as expected.

/*
// C++ implementation of tester

static void tester(const FunctionCallbackInfo<Value>& info) {
    if (info.Length() > 0) {
        info.GetReturnValue().Set(info[0]);
    }
}
*/

function other(x) {
    return x;
}

{
    let a = tester({x: 10});
    let b = new tester({x: 10});
    let c = tester(new Proxy({}, {get: function(target, name) { return name; }}));
    let d = new tester(new Proxy({}, {get: function(target, name) { return name; }}));

    print(a.x);
    print(b.x);
    print(c.x);
    print(d.x);
}

{
    let a = other({x: 10});
    let b = new other({x: 10});
    let c = other(new Proxy({}, {get: function(target, name) { return name; }}));
    let d = new other(new Proxy({}, {get: function(target, name) { return name; }}));

    print(a.x);
    print(b.x);
    print(c.x);
    print(d.x);
}

Output:

10
10
x
undefined
10
10
x
x



via wickund

No comments:

Post a Comment