Duplicate super call behaviour

Mark S. Miller erights at google.com
Sun Oct 25 00:45:04 UTC 2015


On Sat, Oct 24, 2015 at 8:00 PM, Sebastian McKenzie <sebmck at gmail.com>
wrote:

> I was recently reading the specification on the behaviour of duplicate
> super() calls in derived constructors. Reading the grammar the following is
> valid:
>
> ```
> class Foo {
>   constructor() {
>     console.log("foobar);
>   }
> }
>
> class Bar extends Foo {
>   constructor() {
>     super();
>     super();
>   }
> }
>
> new Bar;
> ```
>
> However my reading of the runtime semantics, it will actually execute the
> constructor body *twice. *And so "foo" will be printed two times.
>
> This is because the only thing I've found stopping duplicate super calls
> is step 10 of SuperCall in 12.3.5.1
> <http://www.ecma-international.org/ecma-262/6.0/index.html#sec-super-keyword-runtime-semantics-evaluation>
> :
>
>    1. Let newTarget be GetNewTarget().
>    2. If newTarget is undefined, throw a ReferenceError exception.
>    3. Let func be GetSuperConstructor().
>    4. ReturnIfAbrupt(func).
>    5. Let argList be ArgumentListEvaluation of Arguments.
>    6. ReturnIfAbrupt(argList).
>    7. Let result be Construct(func, argList, newTarget).
>    8. ReturnIfAbrupt(result).
>    9. Let thisER be GetThisEnvironment( ).
>    10. Return thisER.BindThisValue(result).
>
> Since BindThisValue will throw a ReferenceError if `this` has already been
> initialised:
>
>
>    1. Let envRec be the function Environment Record for which the method
>    was invoked.
>    2. Assert: envRec.[[thisBindingStatus]] is not "lexical".
>    3. If envRec.[[thisBindingStatus]] is "initialized", throw a
>    ReferenceError exception.
>
> But this check is performed at step 10 whereas the super constructor is
> actually evaluated at step 7.
>
> Is my reading correct? If so, to me this behaviour seems quite unexpected.
>

I'm surprised. Good catch! Frankly, this case had never occurred to me and
I do not remember discussing it. I would certainly prefer that it be an
error without constructing twice.




>
> Any insight (or correction) is extremely appreciated, thank you!
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>


-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151024/fbbb0475/attachment-0001.html>


More information about the es-discuss mailing list