From: Thomas 'PointedEars' Lahn on
kangax wrote:

> If I'm reading spec correctly, one of the ways to get global object
> from within _any_ code in ES5 is by using indirect call to eval.
>
> To quote Annex E from ES5:
>
> "indirect calls to the eval function use the global environment as both
> the variable environment and lexical environment for the eval code"

You have omitted the important part that immediately follows:

| . In Edition 3, the variable and lexical environments of the caller
| of an indirect e was used as the environments for the eval code.

For this paragraph is part of the *informal* Annex E because that section
of ECMAScript Edition 5 describes features of Edition 5 that are
incompatible with Edition 3:

| Annex E
| (informative)
|
| Additions and Changes in the 5th Edition that
| Introduce Incompatibilities with the 3rd Edition

As a result, if your interpretation would be correct, the OP would be
writing incompatible code, and ISTM that the purpose of their question
was to write most compatible code instead.

> This should mean that any indirect eval call should give you global
> object:
>
> ...
> var global = 1,eval('this');
>
> // or:
> var x, global = (x=eval)('this');
>
> // or:
> var global = 1&&eval('this');
>
> // etc.
> ...

I do not think you are understanding the Specification correctly. `this'
is neither part of the variable environment nor of the lexical environment
of an execution context. (That it would be is the basis of the common
misconception of confusing `this' with scope.)


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
From: kangax on
On 2/22/10 1:10 AM, Thomas 'PointedEars' Lahn wrote:
> kangax wrote:
>
>> If I'm reading spec correctly, one of the ways to get global object
>> from within _any_ code in ES5 is by using indirect call to eval.
>>
>> To quote Annex E from ES5:
>>
>> "indirect calls to the eval function use the global environment as both
>> the variable environment and lexical environment for the eval code"
>
> You have omitted the important part that immediately follows:
>
> | . In Edition 3, the variable and lexical environments of the caller
> | of an indirect e was used as the environments for the eval code.
>
> For this paragraph is part of the *informal* Annex E because that section

The formal part is 10.4.2:

1. If there is no calling context or if the eval code is not being
evaluated by a direct call (15.1.2.1.1) to the eval function then,

a. Initialize the execution context as if it was a global execution
context using the eval code as C as described in 10.4.1.1.

[...]

Let's see 10.4.1.1:

The following steps are performed to initialize a global execution
context for ECMAScript code C:

1. Set the VariableEnvironment to the Global Environment.
2. Set the LexicalEnvironment to the Global Environment.

3. Set the ThisBinding to the global object.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

> of ECMAScript Edition 5 describes features of Edition 5 that are
> incompatible with Edition 3:
>
> | Annex E
> | (informative)
> |
> | Additions and Changes in the 5th Edition that
> | Introduce Incompatibilities with the 3rd Edition
>
> As a result, if your interpretation would be correct, the OP would be
> writing incompatible code, and ISTM that the purpose of their question
> was to write most compatible code instead.

Maybe. I was thinking in terms of pure ES5 behavior.

>
>> This should mean that any indirect eval call should give you global
>> object:
>>
>> ...
>> var global = 1,eval('this');

Just realized that this is still a direct call (`eval` is still a
reference with name "eval" that is MemberExpression in CallExpression).

Should be:

var global = (1,eval)('this');

>>
>> // or:
>> var x, global = (x=eval)('this');
>>
>> // or:
>> var global = 1&&eval('this');

Same here: (1&&eval)('this')

>>
>> // etc.
>> ...
>
> I do not think you are understanding the Specification correctly. `this'

And I think you're not reading Specification carefully.

> is neither part of the variable environment nor of the lexical environment
> of an execution context. [...]

Of course it's not. It's a so-called ThisBinding component (see 10.3)

> (That it would be is the basis of the common
> misconception of confusing `this' with scope.)

You actually think I would confuse `this` with scope? :)

--
kangax
From: Lasse Reichstein Nielsen on
Jorge <jorge(a)jorgechamorro.com> writes:

> (globalObject).window === (globalObject).self === (globalObject)

In IE8:
var global = function(){return this;}();
global.window === global; // => false

Didn't help, sorry.
(Might be a bug in ===, but it's still false :)
/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Lasse Reichstein Nielsen on
kangax <kangax(a)gmail.com> writes:

> If I'm reading spec correctly, one of the ways to get global object
> from within _any_ code in ES5 is by using indirect call to eval.

Not if there is another variable called "eval" in scope.
If you are certain to have access to "eval" on the global object, you
are pretty close to haveing the global object itself.

Another approach is Function("return this")(), but Function may be
shadowed too, and Function.prototype.constructor is configurable,
so you're not guaranteed to be able to get to Function as
(function(){}).constructor.

I can't find a way that's guaranteed to get you out of a strict
mode jail that tries to hide the global object.
/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Jorge on
On Feb 22, 5:37 pm, Lasse Reichstein Nielsen <lrn.unr...(a)gmail.com>
wrote:
> Jorge <jo...(a)jorgechamorro.com> writes:
> > (globalObject).window === (globalObject).self === (globalObject)
>
> In IE8:
>  var global = function(){return this;}();
>  global.window === global;  // => false
>
> Didn't help, sorry.
> (Might be a bug in ===, but it's still false :)

Yes yes. And there are many many more odd things that are true -only-
in IEs. Are you saying that's the way it should be ? Are you -maybe-
advocating to standardize that behaviour ? Because if not, you'd
better think of it as just "another bug in IE".
--
Jorge.