-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Illegal invocation error using ES6 Proxy and node.js #11629
Comments
Maybe it is because the const os = require('os');
const p = new Proxy(os, {});
console.log(p.cpus);
console.log(p.cpus.call(os)); |
receiver for the default get proxy handler is handled with |
This case is also weird (note that I use node.js v7.7.1):
|
@FranckFreiburger var p = new Proxy(require('os'), {});
console.log(p.arch.toString());
console.log(p.arch());
|
This issue isn't reproducible on master or v7.x-staging, probably as a side effect of #11564. Either way, the next release of v7.x will have this fixed. |
This bug has already been reported upstream by as issue 5773. /cc @nodejs/v8 |
Note that all
|
Is there anything Node.js can/should do about this other than await https://bugs.chromium.org/p/v8/issues/detail?id=5773 getting fixed in V8? Should this issue remain open? |
This is still an issue but AFAICT there is nothing Node.js can do about it. Repro on v8.x: const binding = process.binding('util');
const isDate = binding.isDate;
console.log(isDate(new Date)); // true
console.log(isDate.call(binding, new Date)); // true
console.log(isDate.call(new Proxy(binding, {}), new Date)); // illegal invocation |
This is still an issue as of Node v9.8.0. Can/should anyone push for a v8 fix, or should there be any sort of Node workaround? Or are users expected to rebind to the unproxied contexts for all of these calls? |
BTW, this comes up in surprisingly common places, e.g. trying to stringify a Proxied Buffer. let buf = new Proxy(Buffer.from('test'), { get: (t, n) => t[n] })
console.log(buf) |
fwiw this is intended behavior on the side of proxies. check out https://github.com/nodejs/node/pull/18131/files#diff-cc63475f116262e9cbea02374e344c01R671 for an example of a proxy that covers this use case. |
Even if this is fully compliant with the spec, it's quite a pain. As Proxies become a more widely adopted feature I expect many more people will run into this. At the very least, there should be a workaround on the Node side. |
@mappum that's the general idea of the membrane concept: https://www.npmjs.com/package/es7-membrane |
The examples listed in this issue work with master so I think this can be closed out. As to this example (edited for brevity): new Proxy(Buffer.from('test'), { get: (t, n) => t[n] }).toString() That invokes Talk of "there should be a workaround on the Node side" is misguided because it's not a sensible construct. Write it like this and note the new Proxy(Buffer.from('test'), { get: (t, n) => t[n].bind(t) }).toString() |
…e#11629 has been fixed.
On chrome I have same problem for events (eg. click event) - here is solution const eventProxy = new Proxy(event, {
get: (target, prop) => ()=> target[prop].apply(target),
}); working editable example here which shows how to add your code before/after original function execution |
Hello,
I am unable to call a node.js native function through a No-op forwarding proxy.
The following case works properly with a v8 native function:
Whereas the following case throw TypeError: Illegal invocation
what's wrong ?
The text was updated successfully, but these errors were encountered: