Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27937 > unrolled thread

Re: Expression for the global object

Started byBen Bacarisse <ben.usenet@bsb.me.uk>
First post2015-05-31 11:14 +0100
Last post2015-05-31 16:21 +0200
Articles 8 — 2 participants

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-31 11:14 +0100
    Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 15:08 +0200
      Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-31 14:56 +0100
        Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 16:12 +0200
          Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-31 15:57 +0100
            Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 17:33 +0200
              Re: Expression for the global object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-31 16:41 +0100
        Re: Expression for the global object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-31 16:21 +0200

#27937 — Re: Expression for the global object

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-31 11:14 +0100
SubjectRe: Expression for the global object
Message-ID<87twuthx5h.fsf@bsb.me.uk>
ram@zedat.fu-berlin.de (Stefan Ram) writes:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>What is the smallest scope- and host-independent expression
>>whose value is the global object?
>
> Function("return this")()

How does that help?  The identifier Function is looked-up like any
other.

-- 
Ben.

[toc] | [next] | [standalone]


#27939

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-31 15:08 +0200
Message-ID<5452925.Zt9fTqAOub@PointedEars.de>
In reply to#27937
Ben Bacarisse wrote:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>> What is the smallest scope- and host-independent expression
>>> whose value is the global object?
>> Function("return this")()
> 
> How does that help?  The identifier Function is looked-up like any
> other.

My test in Chromium 43¹ shows that this approach helps insofar that, 
different from the code of functions defined with a /FunctionDeclaration/ or 
/FunctionExpression/, this function code is only “strict function code […] 
if the function code begins with a Directive Prologue that contains a Use 
Strict Directive” [1][2]:

  Function('"use strict"; …')();

So if “Function” refers to the Function constructor, that expression – 
regardless whether the constructor is called as a function, without “new” – 
returns a reference to the global object, regardless whether the calling 
execution context is in strict mode.  (I stand corrected.)

However, it should be noted that not only local declarations can override 
“Function” as per identifier resolution (as you pointed out), but also that 
“Function” is one of the “constructor properties” of the global object for 
which no different property attributes are specified, meaning that it has 
the “attributes {[[Writable]]: true, [[Enumerable]]: false, 
[[Configurable]]: true}.” – it can be *redefined*. [3][4]  This is confirmed 
to be implemented so by

| >>> Object.getOwnPropertyDescriptor(Function("return this")(), "Function")
| Object {writable: true, enumerable: false, configurable: true}

in the same (default) environment.

The “Function” constructor is supported since JavaScript 1.1, JScript 1.0, 
and the first versions of all other major implementations. [5]

This leaves a low probability that it does not do what it is intended to do 
here, but still one greater than zero.  It is difficult to assess which 
probability would be greater: that someone fudges with “Function”, or that 
one’s code is not executed in the global context.  I think that, ultimately, 
one must defer to the fact that social problems cannot be solved completely 
using technology.

___________
¹  “Version 43.0.2357.65 Built on Debian stretch/sid, running on Debian 
   stretch/sid (64-bit)”,
   navigator.userAgent === "Mozilla/5.0 (X11; Linux x86_64) 
   AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.65 Safari/537.36"

[1] <http://ecma-international.org/ecma-262/5.1/#sec-10.1.1>
[2] <http://ecma-international.org/ecma-262/5.1/#sec-15.3.1>
[3] <http://ecma-international.org/ecma-262/5.1/#sec-15.1>
[4] <http://ecma-international.org/ecma-262/5.1/#sec-15.1.4>
[5] <http://PointedEars.de/es-matrix/?filter=^Function>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#27940

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-31 14:56 +0100
Message-ID<87382cj1f5.fsf@bsb.me.uk>
In reply to#27939
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Ben Bacarisse wrote:
>
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>>> What is the smallest scope- and host-independent expression
>>>> whose value is the global object?
>>> Function("return this")()
>> 
>> How does that help?  The identifier Function is looked-up like any
>> other.
>
> My test in Chromium 43¹ shows that this approach helps insofar that, 
> different from the code of functions defined with a /FunctionDeclaration/ or 
> /FunctionExpression/, this function code is only “strict function code […] 
> if the function code begins with a Directive Prologue that contains a Use 
> Strict Directive” [1][2]:
>
>   Function('"use strict"; …')();
>
> So if “Function” refers to the Function constructor, that expression – 
> regardless whether the constructor is called as a function, without “new” – 
> returns a reference to the global object, regardless whether the calling 
> execution context is in strict mode.  (I stand corrected.)

Oh, did you say otherwise?  I suggested using Function when you pointed
out the flaw in using thunk that returns this (my first suggestion), but
you didn't reply to that post (as far as I can recall).

You did say it could not be done, but that is probably true.  Your
argument was based on the treatment of identifiers, so another argument
is needed to show that there is no way to do it without any.  I can
think of other ways to do it[1], but I can't think of one that does not
use some identifier or other.

[1] For example, an indirect call to eval also works:

  (function () { return eval; })()("this")

but, again, not for Stefan's purpose.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#27942

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-31 16:12 +0200
Message-ID<7546503.qSl4iIXIz1@PointedEars.de>
In reply to#27940
Ben Bacarisse wrote:

> (function () { return eval; })()("this")

In all respects equivalent to

  eval("this")

which is equivalent to

  this

which is why it does not return a reference to the global object, but the 
“undefined” value, in strict function code that is not called as the method 
of an object.

<http://ecma-international.org/ecma-262/5.1/#sec-10.4.2>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#27945

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-31 15:57 +0100
Message-ID<87wpzohk1k.fsf@bsb.me.uk>
In reply to#27942
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Ben Bacarisse wrote:
>
>> (function () { return eval; })()("this")
>
> In all respects equivalent to
>
>   eval("this")
>
> which is equivalent to
>
>   this

Given:

  'use strict';
  console.log((function () { return eval; })()("this") === global);
  console.log(eval("this") === global);
  console.log(this === global);

nodejs prints:

  NaN
  undefined
  undefined

(global is nodejs specific, of course, but there are other simple ways
to test the value).

> which is why it does not return a reference to the global object, but the 
> “undefined” value, in strict function code that is not called as the method 
> of an object.
>
> <http://ecma-international.org/ecma-262/5.1/#sec-10.4.2>

Sure, but what does 15.1.2.1.1 (Direct Call to Eval) have to say about
it?  You can interpret the words there much more readily than I can, and
it would seem that at least one implementation has decided that the
evaluation of the thunk above is not a "direct call to eval".

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#27946

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-31 17:33 +0200
Message-ID<1658801.5VaNTTmUhE@PointedEars.de>
In reply to#27945
Ben Bacarisse wrote:

> Given:
> 
>   'use strict';
>   console.log((function () { return eval; })()("this") === global);
>   console.log(eval("this") === global);
>   console.log(this === global);
> 
> nodejs prints:
> 
>   NaN
>   undefined
>   undefined

No, it does not.  Because Node.js’s V8 JavaScript is not so badly designed 
that it would print “NaN” or “undefined” as the result of a “===” operation.

> […]
>> <http://ecma-international.org/ecma-262/5.1/#sec-10.4.2>
> 
> Sure, but what does 15.1.2.1.1 (Direct Call to Eval) have to say about
> it? You can interpret the words there much more readily than I can, and
> it would seem that at least one implementation has decided that the
> evaluation of the thunk above is not a "direct call to eval".

We’ll see.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#27947

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-31 16:41 +0100
Message-ID<87lhg4hhzz.fsf@bsb.me.uk>
In reply to#27946
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Ben Bacarisse wrote:
>
>> Given:
>> 
>>   'use strict';
>>   console.log((function () { return eval; })()("this") === global);
>>   console.log(eval("this") === global);
>>   console.log(this === global);
>> 
>> nodejs prints:
>> 
>>   NaN
>>   undefined
>>   undefined
>
> No, it does not.  Because Node.js’s V8 JavaScript is not so badly designed 
> that it would print “NaN” or “undefined” as the result of a “===”
> operation.

I edited the code to be more explicit[1] and obviously forgot to edit the
output.  It's

  true
  false
  false

>> […]
>>> <http://ecma-international.org/ecma-262/5.1/#sec-10.4.2>
>> 
>> Sure, but what does 15.1.2.1.1 (Direct Call to Eval) have to say about
>> it? You can interpret the words there much more readily than I can, and
>> it would seem that at least one implementation has decided that the
>> evaluation of the thunk above is not a "direct call to eval".
>
> We’ll see.

What do you mean by that?

[1] The original code showed access to, NaN, a property of the global
object:

  'use strict';
  console.log((function () { return eval; })()("this").NaN);
  console.log(eval("this").NaN);
  console.log(this.NaN);

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#27944

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-31 16:21 +0200
Message-ID<2563043.EPzh7ypYKR@PointedEars.de>
In reply to#27940
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Ben Bacarisse wrote:
>>>> Function("return this")()
>>> […]
>>
>> […] if “Function” refers to the Function constructor, that expression –
>> regardless whether the constructor is called as a function, without “new”
>> – returns a reference to the global object, regardless whether the
>> calling execution context is in strict mode.  (I stand corrected.)
> 
> Oh, did you say otherwise?  I suggested using Function when you pointed
> out the flaw in using thunk that returns this (my first suggestion), but
> you didn't reply to that post (as far as I can recall).

My followup that I thought I had posted yesterday was still in Drafts, which 
I have updated and posted now.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web