Multiple globals and direct/indirect eval

Jeff Walden jwalden+es at MIT.EDU
Thu Mar 3 17:45:35 PST 2011

On 03/03/2011 04:41 PM, Allen Wirfs-Brock wrote:
> I think your real question reduces to this:
> //none strict mode code
> globalObj= function() {return this}();
> print(otherGlobal.eval("this") === globalObj)  //??
> The two different calls and the "indirect" name in your example may make the question seen like it is about something else (direct/indirect eval).

Not quite so.  For the example I gave, yes -- but you could see the direct/indirect distinction by putting the code I provided inside a function (and a little more gussying to demonstrate behavior better):

   var global = this;
   function f()
     var indirect = otherGlobal.eval;
     eval = indirect;
     print(eval("this") === this);
     print(eval("this) === global);
     print(eval("this") === otherGlobal);
     print(indirect("this") === this);
     print(indirect("this") === global);
     print(indirect("this") === otherGlobal);
   new f();

IE9/Opera prints false/false/true and false/false/true.  Chrome/Safari throws EvalError every time.  Firefox prints true/false/false and false/false/true if otherGlobal is same-origin, false/false/true and false/false/true if otherGlobal is different-origin-but-same-document.domain.

> A more  generally, do built-in functions capture their global environment at the time of their creation or do they they operate in the dynamic context of an ambient global environment. I hope it is the former.
> An example of the more general question would be:
> print((Object.getPrototypeOf(new otherGlobal.Array(0)) ===  Array.prototype)
> I believe the second example prints false for all browser implementations.

This is an orthogonal issue, I believe, but I might as well respond since it's being discussed.  There's a Firefox 4 bug that makes this not the case, ran out of time to fix it for release.  It'll be fixed in 5.0, and I could imagine I might get a fix for it in a 4.0 point release, although with a fast release cycle that may not be necessary.  If I recall correctly Nitro may be buggy this same way as well.  I think Chrome/IE9/Opera did not demonstrate the bug in my testing.  But in any case, printing false there is in my opinion the correct behavior.


More information about the es-discuss mailing list