Skip to content

Commit

Permalink
Normative: Define default constructors using spec steps
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Oct 27, 2020
1 parent 096f30b commit beee2f7
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -8349,10 +8349,11 @@ <h1>%ThrowTypeError% ( )</h1>
<h1>MakeConstructor ( _F_ [ , _writablePrototype_ [ , _prototype_ ] ] )</h1>
<p>The abstract operation MakeConstructor takes argument _F_ (a function object) and optional arguments _writablePrototype_ (a Boolean) and _prototype_ (an Object). It converts _F_ into a constructor. It performs the following steps when called:</p>
<emu-alg>
1. Assert: _F_ is an ECMAScript function object.
1. Assert: IsConstructor(_F_) is *false*.
1. Assert: _F_ is an extensible object that does not have a *"prototype"* own property.
1. Set _F_.[[Construct]] to the definition specified in <emu-xref href="#sec-ecmascript-function-objects-construct-argumentslist-newtarget"></emu-xref>.
1. Assert: _F_ is an ECMAScript function object or a built-in function object.
1. If _F_ is an ECMAScript function object, then
1. Assert: IsConstructor(_F_) is *false*.
1. Assert: _F_ is an extensible object that does not have a *"prototype"* own property.
1. Set _F_.[[Construct]] to the definition specified in <emu-xref href="#sec-ecmascript-function-objects-construct-argumentslist-newtarget"></emu-xref>.
1. Set _F_.[[ConstructorKind]] to ~base~.
1. If _writablePrototype_ is not present, set _writablePrototype_ to *true*.
1. If _prototype_ is not present, then
Expand Down Expand Up @@ -21135,22 +21136,19 @@ <h1>Runtime Semantics: ClassDefinitionEvaluation</h1>
1. Let _proto_ be ! OrdinaryObjectCreate(_protoParent_).
1. If |ClassBody_opt| is not present, let _constructor_ be ~empty~.
1. Else, let _constructor_ be ConstructorMethod of |ClassBody|.
1. If _constructor_ is ~empty~, then
1. If |ClassHeritage_opt| is present, then
1. Set _constructor_ to the result of parsing the source text
<pre><code class="javascript">constructor(...args) { super(...args); }</code></pre>
using the syntactic grammar with the goal symbol |MethodDefinition[~Yield, ~Await]|.
1. Else,
1. Set _constructor_ to the result of parsing the source text
<pre><code class="javascript">constructor() {}</code></pre>
using the syntactic grammar with the goal symbol |MethodDefinition[~Yield, ~Await]|.
1. Set the running execution context's LexicalEnvironment to _classScope_.
1. Let _constructorInfo_ be ! DefineMethod of _constructor_ with arguments _proto_ and _constructorParent_.
1. Let _F_ be _constructorInfo_.[[Closure]].
1. If _constructor_ is ~empty~, then
1. Let _steps_ be the algorithm steps defined in <emu-xref href="#sec-default-constructor-functions"></emu-xref>.
1. Let _F_ be ! CreateBuiltinFunction(_steps_, &laquo; [[ConstructorKind]], [[HomeObject]] &raquo;, ~empty~, _constructorParent_, *true*).
1. Perform ! SetFunctionLength(_F_, *+0*<sub>𝔽</sub>).
1. Set _F_.[[HomeObject]] to _proto_.
1. Else,
1. Let _constructorInfo_ be ! DefineMethod of _constructor_ with arguments _proto_ and _constructorParent_.
1. Let _F_ be _constructorInfo_.[[Closure]].
1. Perform MakeClassConstructor(_F_).
1. Perform SetFunctionName(_F_, _className_).
1. Perform MakeConstructor(_F_, *false*, _proto_).
1. If |ClassHeritage_opt| is present, set _F_.[[ConstructorKind]] to ~derived~.
1. Perform MakeClassConstructor(_F_).
1. Perform CreateMethodProperty(_proto_, *"constructor"*, _F_).
1. If |ClassBody_opt| is not present, let _methods_ be a new empty List.
1. Else, let _methods_ be NonConstructorMethodDefinitions of |ClassBody|.
Expand All @@ -21167,6 +21165,23 @@ <h1>Runtime Semantics: ClassDefinitionEvaluation</h1>
1. Perform _classScope_.InitializeBinding(_classBinding_, _F_).
1. Return _F_.
</emu-alg>

<emu-clause id="sec-default-constructor-functions">
<h1>Default Constructor Functions</h1>
<p>When a Default Constructor Function is called with zero or more arguments which form the rest parameter ..._args_, the following steps are taken:</p>
<emu-alg>
1. If NewTarget is *undefined*, throw a *TypeError* exception.
1. If _F_.[[ConstructorKind]] is ~derived~, then
1. NOTE: This branch behaves similarly to `constructor(...args) { super(...args); }`. The most notable distinction is that while the aforementioned ECMAScript source text is susceptible to pollution of `%Array.prototype[@@iterator]`, this code is not.
1. Let _F_ be the active function object.
1. Let _func_ be ? _F_.[[GetPrototypeOf]]().
1. If IsConstructor(_func_) is *false*, throw a *TypeError* exception.
1. Return ? Construct(_func_, _args_, NewTarget).
1. Else,
1. NOTE: This branch behaves similarly to `constructor() {}`.
1. Return ? OrdinaryCreateFromConstructor(NewTarget, `"%Object.prototype%"`).
</emu-alg>
</emu-clause>
</emu-clause>

<emu-clause id="sec-runtime-semantics-bindingclassdeclarationevaluation">
Expand Down

0 comments on commit beee2f7

Please sign in to comment.