-
Notifications
You must be signed in to change notification settings - Fork 48
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
Proxy to Array fools most methods of checking whether Array is Array #13
Comments
I just checked these issues against the most recent version of v8 (3.17.16.2). Issue 1 from above (JSON.stringify dying on proxies) is fixed. Issue 3 from above (Arrays returning "object Object") is still there, as the following d8 console session shows:
|
Thanks for clarifying. I'd meant to report these results using that same Node version but had invoked the wrong one. Whoops. I see the same thing as @billmark, which is nice because the JSON.stringify issue I complained about as (1) above actually is fixed, but now that it works, it turns out that it, just like JSON2.stringify and util.inspect, does not think that proxy-to-array is an array. So scratch complaint (1), but (2) and (3) turn out to be even truer than I thought. |
More details: I also checked against the slightly more recent "bleeding_edge" version of v8, "3.18.0 (candidate)", and it still has the issue that calling Object.prototype.toString on a proxied array returns "[object Object]" rather than "[object Array]". |
…s, so as to improve transparency for Arrays. Cf. issue #13
I just committed a patch so that var p = Proxy([] , {});
Object.prototype.toString.call(p) // [object Array]
Array.isArray(p) // true However, |
Thanks, Tom. |
Has any of this made it's way to the npm module? |
Not yet. Thanks for bringing it to my attention. I'll upload a new npm package shortly. |
npm module v0.0.5 now contains the patches to |
Thanks, Tom! |
Thanks! |
Regarding the JSON.stringify, a workaround is to do this: var proxy = new Proxy(obj, {
get: function(target, property, receiver) {
if (property === 'toJSON') {
return () => target;
}
}); |
I'm not quite sure how to motivate this problem without describing 3 different phenomena at once so please bear with me:
{ "0": 'foo', "1": 'bar' }
instead of['foo', 'bar']
.Object.prototype.toString.apply(candidate) === '[object Array]'
test. Proxy to Array returns[object Object]
.It's the third of these that I think is arguably a bug in the proxy implementation (or I'd like to know why not, and how to work around it!).
Note there that the REPL's display of
p
shows it as an object with numeric keys, as I described in (2) above. So not only json2.js but alsoutil.inspect
(which the REPL uses) is treatingp
as a general Object, not as an Array.Also note that the builtin
JSON.stringify
on a proxy just fails with "illegal access".Here's the full battery of tests I can think of for whether something is an array (all of these return true for a, but mostly not for p):
So the
instanceof
test works on the proxy, but all the other tests fail. And a lot of code out there wants to use the Object.prototype.toString test. Now, the reasons for using the Object.prototype.toString test instead of the instanceof test might not be relevant to Node (namely, browser-side stuff with multiple frames or iframes). But is there any hope that Object.prototype.toString should be returning Array and not Object for arrays?Some further details:
The text was updated successfully, but these errors were encountered: