Question about joined function object of ECMA-262 3rd edition

liorean liorean at gmail.com
Sun Aug 5 10:32:20 PDT 2007


On 05/08/07, Shijun He <hax.sfo at gmail.com> wrote:
> I read your post again today. And I found the key point is the
> definition of "Equated".

Yeah, but my argument was flawed since the spec never actually says
what I read out from it.

> 13.1.1
> Both uses obtained their FunctionBody from the same location in the
> source text of the same ECMAScript program. This source text consists
> of global code and any contained function codes according to the
> definitions in section 10.1.2.
>
> Q1: what is "source text of the same ECMAScript program" or "this source text"?

"this source text" refers to this: "... FunctionBody from the same
location in the source text of the same ECMAScript program"

"the same location in the source text of the same ECMAScript program"
refers to the source code being from the same place in the same
program (or file), not different places in the same program, or from
different programs.

> Q2: what is global code and what is contained function codes?

I included the definition of global code and function code right after
the equated grammar productions quote:

~~~~
10.1.2 Types of Executable Code
There are three types of ECMAScript executable code:
•  Global code is source text that is treated as an ECMAScript
Program. The global code of a particular Program does not include any
source text that is parsed as part of a FunctionBody.
/.../
•  Function code is source text that is parsed as part of a
FunctionBody. The function code of a particular FunctionBody does not
include any source text that is parsed as part of a nested
FunctionBody. /.../
~~~~

Global code is straightly as defined. By "any contained function
codes" they refer to the function codes of the functions that are
joined, and from the use of the word "any" presumably their nested
function codes as well.

> Let's give a example here:
>
> function A() {
>   return function B(x) {
>     return x * x;
>   }
> }
> var b1 = A();
> var b2 = A();
>
> Is the "source text" means all code or just means "return x * x"?
> I think it means all codes, then the global code of it is:
>   function A() {...}
>   var b1 = A();
>   var b2 = A();
> The contained function codes are:
>   return function B(x) {...}
> and
>   return x * x;
>
> You said: "if a nested function relies on the containing function, the code
> it relies on is not part of either the global code or it's contained
> function code"
>
> So I don't understand it. Do you mean the source text should be "return x * x"?

The contained function code of B is "return x * x", yes, and the
joined functions are B, but from different calls to A, so they have
different [[Scope]]. Now, consider this code:

    function A(y) {
        return function B(x) {
            return x * y;
        }
    }
    var
        b1 = A(1),
        b2 = A(2);

Here the contained function code of B is "return x * y". The function
bodies are in the terms of ES3 equated - their only difference is
[[Scope]]. Which means that b1 and b2 formally may be joined here, but
an implementation cannot reuse the same function object since the y
variable from closure is used (i.e.B depends on the containing
function A). The engine would have to actually implement the joining
mechanism or some equivalent*.

My argument was wrong here - since y is part of A's variable object
and not B's variable object, I considered it part of the containing
function code (A's code). But it really isn't, so the flaw in my
argument.

* Actually implementing the joining mechanism is not an optimisation
by any means - I cannot see it actually improving the engine in any
area, neither memory footprint, byte code size nor performance. The
optimisation lies in the cases where you can skip having different
objects at all, not in the case of having two objects that are always
in sync. Keeping two distinct but identical objects in sync after they
have been created is a net loss performance wise and footprint wise,
not a net gain. Every change to a propety of one of the joined
function objects have to be duplicated on the other object.
-- 
David "liorean" Andersson


More information about the Es4-discuss mailing list