eval on non-strings

Mark S. Miller erights at google.com
Tue Feb 28 15:27:13 PST 2012


On Tue, Feb 28, 2012 at 2:18 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote:

>
> On Feb 28, 2012, at 9:20 AM, Mark S. Miller wrote:
>
> On Tue, Feb 28, 2012 at 7:54 AM, Allen Wirfs-Brock <allen at wirfs-brock.com>wrote:
> [...]
>
>> There is also a matter of maintaining internal insistency within a
>> language. [...] JS generally converts primitive values to wrapper objects
>> whenever one is used in a context that requires an object. [...]
>
>
> Some other violations of this rule that surprised me when I first came
> across them:
>
>  > 3 instanceof Number
> false
> > 'x' in 3
> TypeError: Cannot use 'in' operator to search for 'x' in 3
>
>
> I am not suggesting that we do anything to repair these -- it is likely
> too late. But depending on how many other such irregularities there are, it
> may not altogether be clear which direction the principle of least surprise
> should take us.
>
>
> Both of these were added to the spec. in ES3.  They are probably exactly
> the sort of thing I was warning against when I mentioned point in time or
> spec. writer  introduced inconsistencies.  Since exceptions were also
> spec'ed in ES3 I bet whoever spec'ed in and instanceof thought that
> throwing would be the "right thing" and perhaps didn't even think about
> maintaining consistency with the language's legacy.  who knows...
>
> Since, in and instanceof currently throw in the above situations we
> probably could get away with changing them to do ToObject.  Not necessarily
> saying we should, but it probably wouldn't break anything.  It would be a
> set towards  maintaining internal consistency.
>

Fixing "in" would be be great if we can. But the "instanceof" example above
correctly does not throw, it returns false, which we should assume we
cannot fix. "instanceof" does throw if its *right* operand is not an object
with a [[HasInstance]] method. Is that the case you have in mind?

-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120228/21b7237b/attachment-0001.html>


More information about the es-discuss mailing list