archives

« Bugzilla Issues Index

#2733 — 19.1.3.6 Object.prototype.toString handling for Proxy objects


Currently, the default behavior is to report a proxy as "[object Proxy]".

Somebody might try to use this as a Proxy type test. But note that it isn't reliable because "Proxy" is intentionally not on the legacy toStringTag back. list.

Also:

On Apr 24, 2014, at 9:27 AM, Allen Wirfs-Brock wrote:


On Apr 24, 2014, at 9:03 AM, Tom Van Cutsem wrote:

(reiterating what I already mentioned at the hangout, but to bring Brian up to date)

Based on feedback I received from developers that are using my Proxy shim, they clearly want Object.prototype.toString to be transparent in the face of proxies (i.e. O.p.toString.call(proxy) should return whatever O.p.toString.call(target) returns).

Allen raised the issue that this breaks the proxy's encapsulation, but a Proxy that wants to hide its target's type can still implement the @ToStringTag symbol to override the default behavior.

Mark and I continued discussing this quite a bit. Consider this:

let exec = RegExp.prototype.exec;
let toString = Object.prototype.toString;
...
if (toString.call(obj) === 'RegExp') {
exec.call(obj, string)
} else {
//something else
}

The above with throw on the exec.call if obj is a Proxy because obj does not have the internal state required of a RegExp object. In general proxies aren't transparent when passed as this values, so it isn't clear to me what your developers think they are asking for.

Allen



Cheers,
Tom
(adding Mark and Tom)

Yes, O.p.toString as an isProxy test has always been a bit of a concern since we eliminated Proxy.isProxy.

Mark and Tom, what do you think? Is this a concern? Is there some other way you know of for detecting a Proxy that makes this a non-issue.

If you think it is a problem, it would be easy enough to spec. O.p.toString to report "[object Object]" for Proxies.

Allen

On Apr 23, 2014, at 11:57 AM, Brian Terlson wrote:

That makes sense, thanks for explaining to me. I think the only other option would be ‘[object Object]’ but I don’t have a strong desire for this. I have a vague worry that people will write an isProxy function based on this, which would be a bad idea, but that’s about it.

From: Allen Wirfs-Brock [mailto:allen@wirfs-brock.com]
Sent: Tuesday, April 22, 2014 5:24 PM
To: Brian Terlson
Subject: Re: (new Proxy({},{})).toString() === '[object Proxy]'?


On Apr 22, 2014, at 4:26 PM, Brian Terlson wrote:


At least, according to the current spec. And, if I understand correctly, that’s unless the target has an @@toStringTag, in which case that will be used. Is this what we want for sure? I tried to search for rationale and failed…

Thanks for any insight!!

Well, it's mostly a byproduct of the fact that method invocation on proxies, by default, looks up the method on the Proxy target, but invoke it with the Proxy as the this value. But it seems like as a good a default as any. There are lots of ways the Proxy provider might define toString behavior (including by defined @@toStringTag on the target object.

I'm not sure any other default for O.p.toString on a Proxy would be any better. It seems like it would be a bit presumptuous for for O.p.toString to violate encapsulation by reach inside the Proxy and directly trying to invoke something on the target.

allen


fixed in rev25 editor's draft

Proxy objects default to either [object Object] or [object Function] depending upon whether or not they are callable.


fixed in rev25 editor's draft.


fixed in rev25 editor's draft