Skip to content

Commit 7768b89

Browse files
committed
Normative: Define default constructors using spec steps
1 parent 6460fcd commit 7768b89

1 file changed

Lines changed: 40 additions & 22 deletions

File tree

spec.html

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10733,10 +10733,14 @@ <h1>[[Call]] ( _thisArgument_, _argumentsList_ )</h1>
1073310733
<p>The [[Call]] internal method of an ECMAScript function object _F_ takes arguments _thisArgument_ (an ECMAScript language value) and _argumentsList_ (a List of ECMAScript language values). It performs the following steps when called:</p>
1073410734
<emu-alg>
1073510735
1. Assert: _F_ is an ECMAScript function object.
10736-
1. If _F_.[[IsClassConstructor]] is *true*, throw a *TypeError* exception.
1073710736
1. Let _callerContext_ be the running execution context.
1073810737
1. Let _calleeContext_ be PrepareForOrdinaryCall(_F_, *undefined*).
1073910738
1. Assert: _calleeContext_ is now the running execution context.
10739+
1. If _F_.[[IsClassConstructor]] is *true*, then
10740+
1. Let _error_ be a newly created *TypeError* object.
10741+
1. NOTE: _error_ is created in _calleeContext_ with _F_'s associated Realm Record.
10742+
1. Remove _calleeContext_ from the execution context stack and restore _callerContext_ as the running execution context.
10743+
1. Return ThrowCompletion(_error_).
1074010744
1. Perform OrdinaryCallBindThis(_F_, _calleeContext_, _thisArgument_).
1074110745
1. Let _result_ be OrdinaryCallEvaluateBody(_F_, _argumentsList_).
1074210746
1. [id="step-call-pop-context-stack"] Remove _calleeContext_ from the execution context stack and restore _callerContext_ as the running execution context.
@@ -10914,10 +10918,11 @@ <h1>%ThrowTypeError% ( )</h1>
1091410918
<h1>MakeConstructor ( _F_ [ , _writablePrototype_ [ , _prototype_ ] ] )</h1>
1091510919
<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>
1091610920
<emu-alg>
10917-
1. Assert: _F_ is an ECMAScript function object.
10918-
1. Assert: IsConstructor(_F_) is *false*.
10919-
1. Assert: _F_ is an extensible object that does not have a *"prototype"* own property.
10920-
1. Set _F_.[[Construct]] to the definition specified in <emu-xref href="#sec-ecmascript-function-objects-construct-argumentslist-newtarget"></emu-xref>.
10921+
1. Assert: _F_ is an ECMAScript function object or a built-in function object.
10922+
1. If _F_ is an ECMAScript function object, then
10923+
1. Assert: IsConstructor(_F_) is *false*.
10924+
1. Assert: _F_ is an extensible object that does not have a *"prototype"* own property.
10925+
1. Set _F_.[[Construct]] to the definition specified in <emu-xref href="#sec-ecmascript-function-objects-construct-argumentslist-newtarget"></emu-xref>.
1092110926
1. Set _F_.[[ConstructorKind]] to ~base~.
1092210927
1. If _writablePrototype_ is not present, set _writablePrototype_ to *true*.
1092310928
1. If _prototype_ is not present, then
@@ -11155,7 +11160,7 @@ <h1>CreateBuiltinFunction ( _steps_, _length_, _name_, _internalSlotsList_ [ , _
1115511160
<p>The abstract operation CreateBuiltinFunction takes arguments _steps_, _length_, _name_, and _internalSlotsList_ (a List of names of internal slots) and optional arguments _realm_, _prototype_, and _prefix_. _internalSlotsList_ contains the names of additional internal slots that must be defined as part of the object. This operation creates a built-in function object. It performs the following steps when called:</p>
1115611161
<emu-alg>
1115711162
1. Assert: _steps_ is either a set of algorithm steps or other definition of a function's behaviour provided in this specification.
11158-
1. If _realm_ is not present, set _realm_ to the current Realm Record.
11163+
1. If _realm_ is not present or _realm_ is ~empty~, set _realm_ to the current Realm Record.
1115911164
1. Assert: _realm_ is a Realm Record.
1116011165
1. If _prototype_ is not present, set _prototype_ to _realm_.[[Intrinsics]].[[%Function.prototype%]].
1116111166
1. Let _func_ be a new built-in function object that when called performs the action described by _steps_. The new function object has internal slots whose names are the elements of _internalSlotsList_, and an [[InitialName]] internal slot.
@@ -20700,23 +20705,18 @@ <h1>Runtime Semantics: ClassDefinitionEvaluation</h1>
2070020705
1. Let _proto_ be ! OrdinaryObjectCreate(_protoParent_).
2070120706
1. If |ClassBody_opt| is not present, let _constructor_ be ~empty~.
2070220707
1. Else, let _constructor_ be ConstructorMethod of |ClassBody|.
20703-
1. If _constructor_ is ~empty~, then
20704-
1. If |ClassHeritage_opt| is present, then
20705-
1. Let _constructorText_ be the source text
20706-
<pre><code class="javascript">constructor(...args) { super(...args); }</code></pre>
20707-
1. Else,
20708-
1. Let _constructorText_ be the source text
20709-
<pre><code class="javascript">constructor() {}</code></pre>
20710-
1. Set _constructor_ to ParseText(_constructorText_, |MethodDefinition[~Yield, ~Await]|).
20711-
1. Assert: _constructor_ is a Parse Node.
2071220708
1. Set the running execution context's LexicalEnvironment to _classScope_.
20713-
1. Let _constructorInfo_ be ! DefineMethod of _constructor_ with arguments _proto_ and _constructorParent_.
20714-
1. Let _F_ be _constructorInfo_.[[Closure]].
20715-
1. Perform SetFunctionName(_F_, _className_).
20716-
1. Perform MakeConstructor(_F_, *false*, _proto_).
20709+
1. If _constructor_ is ~empty~, then
20710+
1. Let _steps_ be the algorithm steps defined in <emu-xref href="#sec-default-constructor-functions" title></emu-xref>.
20711+
1. Let _F_ be ! CreateBuiltinFunction(_steps_, 0, _className_, &laquo; [[ConstructorKind]], [[SourceText]] &raquo;, ~empty~, _constructorParent_).
20712+
1. Else,
20713+
1. Let _constructorInfo_ be ! DefineMethod of _constructor_ with arguments _proto_ and _constructorParent_.
20714+
1. Let _F_ be _constructorInfo_.[[Closure]].
20715+
1. Perform ! MakeClassConstructor(_F_).
20716+
1. Perform ! SetFunctionName(_F_, _className_).
20717+
1. Perform ! MakeConstructor(_F_, *false*, _proto_).
2071720718
1. If |ClassHeritage_opt| is present, set _F_.[[ConstructorKind]] to ~derived~.
20718-
1. Perform MakeClassConstructor(_F_).
20719-
1. Perform CreateMethodProperty(_proto_, *"constructor"*, _F_).
20719+
1. Perform ! CreateMethodProperty(_proto_, *"constructor"*, _F_).
2072020720
1. If |ClassBody_opt| is not present, let _methods_ be a new empty List.
2072120721
1. Else, let _methods_ be NonConstructorMethodDefinitions of |ClassBody|.
2072220722
1. For each |ClassElement| _m_ of _methods_, do
@@ -20732,6 +20732,24 @@ <h1>Runtime Semantics: ClassDefinitionEvaluation</h1>
2073220732
1. Perform _classScope_.InitializeBinding(_classBinding_, _F_).
2073320733
1. Return _F_.
2073420734
</emu-alg>
20735+
20736+
<emu-clause id="sec-default-constructor-functions">
20737+
<h1>Default Constructor Functions</h1>
20738+
<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>
20739+
<emu-alg>
20740+
1. If NewTarget is *undefined*, throw a *TypeError* exception.
20741+
1. Let _F_ be the active function object.
20742+
1. If _F_.[[ConstructorKind]] is ~derived~, then
20743+
1. NOTE: This branch behaves similarly to `constructor(...args) { super(...args); }`. The most notable distinction is that while the aforementioned ECMAScript source text observably calls the @@iterator method on `%Array.prototype%`, a Default Constructor Function does not.
20744+
1. Let _func_ be ! _F_.[[GetPrototypeOf]]().
20745+
1. If IsConstructor(_func_) is *false*, throw a *TypeError* exception.
20746+
1. Return ? Construct(_func_, _args_, NewTarget).
20747+
1. Else,
20748+
1. NOTE: This branch behaves similarly to `constructor() {}`.
20749+
1. Return ? OrdinaryCreateFromConstructor(NewTarget, *"%Object.prototype%"*).
20750+
</emu-alg>
20751+
<p>The *"length"* property of a default constructor function is *+0*<sub>𝔽</sub>.</p>
20752+
</emu-clause>
2073520753
</emu-clause>
2073620754

2073720755
<emu-clause id="sec-runtime-semantics-bindingclassdeclarationevaluation" type="sdo" aoid="BindingClassDeclarationEvaluation">
@@ -25189,9 +25207,9 @@ <h1>Function.prototype.toString ( )</h1>
2518925207
<p>When the `toString` method is called, the following steps are taken:</p>
2519025208
<emu-alg>
2519125209
1. Let _func_ be the *this* value.
25192-
1. If _func_ is a <emu-xref href="#sec-built-in-function-objects">built-in function object</emu-xref>, return an implementation-defined String source code representation of _func_. The representation must have the syntax of a |NativeFunction|. Additionally, if _func_ has an [[InitialName]] internal slot and _func_.[[InitialName]] is a String, the portion of the returned String that would be matched by |NativeFunctionAccessor?| |PropertyName| must be the value of _func_.[[InitialName]].
2519325210
1. If Type(_func_) is Object and _func_ has a [[SourceText]] internal slot and _func_.[[SourceText]] is a sequence of Unicode code points and ! HostHasSourceTextAvailable(_func_) is *true*, then
2519425211
1. Return ! CodePointsToString(_func_.[[SourceText]]).
25212+
1. If _func_ is a <emu-xref href="#sec-built-in-function-objects">built-in function object</emu-xref>, return an implementation-defined String source code representation of _func_. The representation must have the syntax of a |NativeFunction|. Additionally, if _func_ has an [[InitialName]] internal slot and _func_.[[InitialName]] is a String, the portion of the returned String that would be matched by |NativeFunctionAccessor?| |PropertyName| must be the value of _func_.[[InitialName]].
2519525213
1. If Type(_func_) is Object and IsCallable(_func_) is *true*, return an implementation-defined String source code representation of _func_. The representation must have the syntax of a |NativeFunction|.
2519625214
1. Throw a *TypeError* exception.
2519725215
</emu-alg>

0 commit comments

Comments
 (0)