Swatinem / proxy

Small shim providing `new Proxy(target, handler)`
13 stars 5 forks source link

Should not use Proxy.createFunction for typeof target === 'function' #4

Closed blake-regalia closed 9 years ago

blake-regalia commented 9 years ago

new Proxy(target, handler) allows you to extend functions as if they were plain objects. For example, in ES6:

let simple = function() {
  return 'called';
};
var proxy = new Proxy(simple, {
  get: function(target, prop) {
    return 'getting property '+prop;
  }
});

proxy(); // 'called'
proxy['test']; // outputs: 'getting property test'

However, this shim calls Proxy.createFunction, which only allows traps for calling and constructing the function, not any other traps.

neico commented 9 years ago

I think you didn't read the code well, index.js#L97 clearly shows that it's getting the other traps, createFunction only has "construct" and "apply" as arguments because that's what the old api was doing...

your code works perfectly fine...

blake-regalia commented 9 years ago
var Proxy = require('harmony-proxy');

var simple = function() {
    return 'called';    
};
var proxy = new Proxy(simple, {
    get: function(target, prop) {
        return 'getting property '+prop;
    },
});

dies with:

/Users/.../harmony-proxy/lib/index.js:92 return Proxy.createFunction( ^

TypeError: Proxy.createFunction called with non-function for 'call' trap at Object.createFunction (native) at new ProxyShim (/Users/.../harmony-proxy/lib/index.js:92:15)

Which means it needs apply and construct traps (which ES6 does not require for function targets!)

neico commented 9 years ago

Was fixed with https://github.com/Swatinem/proxy/pull/3

but @Swatinem didn't update the npm package... (he also didn't fix travis yet, which is quite the shame)

change your package.json line to

"harmony-proxy": "Swatinem/proxy"

this will get the git repo version where it's fixed after doing a npm update

blake-regalia commented 9 years ago

Ah, i see. Thank you!

Swatinem commented 9 years ago

I just released 0.1.0. Also added a test for that case, fixed some lints and the travis build. enjoy, and thanks for your interest in the project :-)