Multiple globals and direct/indirect eval

Allen Wirfs-Brock allen at
Thu Mar 3 16:41:43 PST 2011


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).

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.


On Mar 3, 2011, at 2:39 PM, Jeff Walden wrote:

> A few months back I noticed an interesting interaction between how direct eval is defined and multiple globals.  What happens if, in one global, you call an eval from another global as if it were a direct eval?
>  var indirect = otherGlobal.eval;
>  eval = indirect;
>  print(eval("this") === this);
>  print(indirect("this") === this);
> Standards currently don't say what should happen here because it's multiple globals, so what should this do?  IE9 and Opera print false both times for this.  Firefox prints true, then false -- but only if the global and otherGlobal are from the same origin (so on pages with the same scheme/host/port, more or less).  If they're from different origins (but have set document.domain to the same value) Firefox too prints false.  Chrome and Safari throw an EvalError calling another window's eval (for both "direct" and indirect calls) without that window as |this| for the call.
> The Chrome/Safari behavior would resurrect the vestigial EvalError, so I don't think it makes sense.  It also contradicts the specification of the steps in the definition of the eval function.  Firefox's behavior is inconsistent and seems not amenable to host-agnostic specification as ECMA would require.  Thus we are left with the IE9/Opera behavior, which seems sensible and natural to me: an eval function should always act in the context of the global from which it came.
> What needs to be done to standardize this behavior?  And more generally, what needs to be done to begin standardizing multiple globals in ECMAScript, including issues like this one?
> Jeff
> _______________________________________________
> es-discuss mailing list
> es-discuss at

More information about the es-discuss mailing list