admin管理员组文章数量:1394088
I'm using the V8 API to create JavaScript objects. Some of these objects support iteration by setting up a native (intercepted) function at the Symbol.iterator
property.
Iterating such an object via for...of
works perfectly. However, if I wrap it in a null proxy (e.g., let x = new Proxy(obj, {});
), the resulting object is not iterable and throws a TypeError
with the message "Illegal invocation" if an attempt is made to iterate over it.
Wrapping a standard array doesn't exhibit this issue. Is this a V8 bug?
I'm using the V8 API to create JavaScript objects. Some of these objects support iteration by setting up a native (intercepted) function at the Symbol.iterator
property.
Iterating such an object via for...of
works perfectly. However, if I wrap it in a null proxy (e.g., let x = new Proxy(obj, {});
), the resulting object is not iterable and throws a TypeError
with the message "Illegal invocation" if an attempt is made to iterate over it.
Wrapping a standard array doesn't exhibit this issue. Is this a V8 bug?
Share Improve this question edited Dec 8, 2016 at 20:09 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Dec 8, 2016 at 18:09 AndromedaryAndromedary 3,5581 gold badge18 silver badges21 bronze badges 2- What would you expect to iterate over if you've wrapped it in a null proxy ? – Pogrindis Commented Dec 8, 2016 at 18:11
- @Pogrindis I'd expect the proxy to iterate over its target; that's what happens if the target is a standard array. – Andromedary Commented Dec 8, 2016 at 18:12
1 Answer
Reset to default 8Wrapping a standard array doesn't exhibit this issue.
Yes, that's how array iterators work. They don't care about the kind of the object they are iterating - they simply access its .length
and indexed properties (which are routed normally through the proxy).
However, other standard exotic objects don't behave that nice either. If you try to invoke [Symbol.iterator]()
on a typed array, map or set that is wrapped in a proxy, they'll bitch about being invoked on the wrong object.
Is this a V8 bug?
No, it's a bug in the application. You've got three choices:
- Create an iterator that does not depend on the internal slots of your custom objects, but rather uses their public (proxy-interceptable) property interface. Make sure your
[Symbol.iterator]
method does not typecheck its receiver. - Check the type of the receiver in your iterator method, and if it is a proxy (i.e. has a
[[ProxyTarget]]
internal slot) then use that value. I would strongly advise against this, as it does not match the standard behaviour and breaches the proxy when bypassing the handler. Don't use a null proxy:
let x = new Proxy(obj, { get(target, key, receiver) { if (key === Symbol.iterator) return target[Symbol.iterator].bind(target); else return Reflect.get(target, key, receiver); } });
版权声明:本文标题:javascript - V8: ES6 proxies don't support iteration protocol when targeting custom objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744598427a2614918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论