Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit 4a2d164

Browse files
committed
Normative: Add lexically scoped definitions
This patch specifies lexically scoped declarations in class bodies, with a token preceding them. Because we haven't figured out which token to use (#9), <placeholder> is used instead. Some main design points: - let, const, class and function (including async fns, generators) are supported, and var declarations are not supported. - Function declarations are hoisted to the top of the block, before evaluating the extends clause. - let, const and class declarations are evaluated interspersed with static public field initializers. - The class is visible with a binding initialized when the lexical declarations are evaluated. - Unlike static public field initializers which are in a scope analogous to a method (with super property access, and this being the class), the scope of lexical declarations inherits directly from the outer scope, similarly to computed proerty names and the extends clause, which implies: - this, super, new.target are inherited from the lexical context, and are not related to the current class definition. - arguments is not poisoned and is inherited from an outer definition - yield, await can be used if possible in the enclosing function.
1 parent 4d89ed7 commit 4a2d164

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

spec.html

+54-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ <h1>Syntax</h1>
2323
`static` MethodDefinition[?Yield, ?Await]
2424
FieldDefinition[?Yield, ?Await] `;`
2525
<ins>`static` FieldDefinition[?Yield, ?Await] `;`</ins>
26+
<ins>`&lt;placeholder>` Declaration[?Yield, ?Await]</ins>
2627
`;`
2728
</emu-grammar>
2829

@@ -41,19 +42,30 @@ <h1>Static Semantics: Early Errors</h1>
4142
<emu-clause id=sec-algorithms>
4243
<h1>Algorithms</h1>
4344

45+
<emu-clause id="runtime-semantics-class-element-evaluation">
4446
<h1>Runtime Semantics: ClassElementEvaluation</h1>
4547
<p>With parameters _object_ and _enumerable_.</p>
4648

4749
<emu-grammar><ins>ClassElement : `static` FieldDefinition `;`</ins></emu-grammar>
4850
<emu-alg>
49-
<ins>1. Return ClassFieldDefinitionEvaluation of |FieldDefinition| with parameter *true* and _object_.</ins>
51+
1. <ins>Return ClassFieldDefinitionEvaluation of |FieldDefinition| with parameter *true* and _object_.</ins>
5052
</emu-alg>
5153

5254
<emu-grammar>ClassElement : FieldDefinition `;`</emu-grammar>
5355
<emu-alg>
5456
1. Return ClassFieldDefinitionEvaluation of |FieldDefinition| with parameter <ins>*false* and</ins> _object_.
5557
</emu-alg>
5658

59+
<emu-grammar><ins>ClassElement : `&lt;placeholder>` Declaration</ins></emu-grammar>
60+
<ins class="block"><emu-alg>
61+
1. Return a List containing Record {
62+
[[Declaration]]: |Declaration|
63+
[[Static]]: *true*,
64+
[[IsAnonymousFunctionDefinition]]: *false*
65+
}.
66+
</emu-alg></ins>
67+
</emu-clause>
68+
5769
<emu-clause id="runtime-semantics-class-field-definition-evaluation">
5870
<h1>Runtime Semantics: ClassFieldDefinitionEvaluation</h1>
5971

@@ -92,7 +104,11 @@ <h1>InitializeStaticFields(_F_)</h1>
92104
1. Let _fieldRecords_ be the value of _F_'s [[Fields]] internal slot.
93105
1. For each item _fieldRecord_ in order from _fieldRecords_,
94106
1. If _fieldRecord_.[[Static]] is *true*, then
95-
1. Perform ? DefineField(_F_, _fieldRecord_).
107+
1. <ins>If _fieldRecord_ has a [[Declaration]] field,</ins>
108+
1. <ins>Let _ret_ be the result of evaluating _fieldRecord_.[[Declaration]].</ins>
109+
1. <ins>ReturnIfAbrupt(_ret_).</ins>
110+
1. <ins>Else,</ins>
111+
1. Perform ? DefineField(_F_, _fieldRecord_).
96112
1. Return.
97113
</emu-alg>
98114
</emu-clause>
@@ -119,6 +135,37 @@ <h1>Static Semantics: IsStatic</h1>
119135
</emu-alg>
120136
</emu-clause>
121137

138+
<emu-clause id="sec-block-static-semantics-lexicallyscopeddeclarations">
139+
<h1>Static Semantics: LexicallyScopedDeclarations</h1>
140+
<emu-see-also-para op="LexicallyScopedDeclarations"></emu-see-also-para>
141+
<emu-grammar>ClassBody : ClassElementList</emu-grammar>
142+
<emu-alg>
143+
1. Return the LexicallyScopedDeclarations of |ClassElementList|.
144+
</emu-alg>
145+
<emu-grammar>ClassElementList : ClassElementList ClassElement</emu-grammar>
146+
<emu-alg>
147+
1. Let _declarations_ be LexicallyScopedDeclarations of |ClassElementList|.
148+
1. Append to _declarations_ the elements of the LexicallyScopedDeclarations of |ClassElement|.
149+
1. Return _declarations_.
150+
</emu-alg>
151+
<emu-grammar>ClassElementList : ClassElement</emu-grammar>
152+
<emu-alg>
153+
1. Return the LexicallyScopedDeclarations of |ClassElement|.
154+
</emu-alg>
155+
<emu-grammar>ClassElement : MethodDefinition</emu-grammar>
156+
<emu-grammar>ClassElement : `static` MethodDefinition</emu-grammar>
157+
<emu-grammar>FieldDefinition[?Yield, ?Await] `;`</emu-grammar>
158+
<emu-grammar>`static` FieldDefinition[?Yield, ?Await] `;`</emu-grammar>
159+
<emu-grammar>ClassElement : `;`</emu-grammar>
160+
<emu-alg>
161+
1. Return a new empty List.
162+
</emu-alg>
163+
<emu-grammar>ClassElement : `&lt;placeholder&gt;` Declaration</emu-grammar>
164+
<emu-alg>
165+
1. Return a new List containing DeclarationPart of |Declaration|.
166+
</emu-alg>
167+
</emu-clause>
168+
122169
<emu-clause id="runtime-semantics-class-definition-evaluation">
123170
<h1>Runtime Semantics: ClassDefinitionEvaluation</h1>
124171
<p>With parameter _className_.</p>
@@ -135,6 +182,7 @@ <h1>Runtime Semantics: ClassDefinitionEvaluation</h1>
135182
1. If |ClassBody_opt| is present, then
136183
1. For each element _dn_ of the PrivateBoundNames of |ClassBody_opt|,
137184
1. Perform _classPrivateEnvRec_.CreateImmutableBinding(_dn_, *true*).
185+
1. <ins>Perform ! BlockDeclarationInstantiation(|ClassBody|, _classScopeEnvRec_).</ins>
138186
1. If |ClassHeritage_opt| is not present, then
139187
1. Let _protoParent_ be the intrinsic object %ObjectPrototype%.
140188
1. Let _constructorParent_ be the intrinsic object %FunctionPrototype%.
@@ -186,17 +234,17 @@ <h1>Runtime Semantics: ClassDefinitionEvaluation</h1>
186234
1. Set the running execution context's PrivateNameEnvironment to _outerPrivateEnvironment_.
187235
1. Return Completion(_status_).
188236
1. Append to _fieldRecords_ the elements of _fields_.
189-
1. Set the running execution context's LexicalEnvironment to _lex_.
190237
1. If _className_ is not *undefined*, then
191238
1. Perform _classScopeEnvRec_.InitializeBinding(_className_, _F_).
239+
1. <del>Set the running execution context's LexicalEnvironment to _lex_.</del>
192240
1. Set the value of _F_'s [[Fields]] internal slot to _fieldRecords_.
193-
1. Set the running execution context's LexicalEnvironment to _classScope_.
194-
1. Set the running execution context's PrivateNameEnvironment to _outerPrivateEnvironment_.
195241
1. <ins>Let _result_ be InitializeStaticFields(_F_).</ins>
196242
1. <ins>If _result_ is an abrupt completion, then</ins>
197243
1. <ins>Set the running execution context's LexicalEnvironment to _lex_.</ins>
244+
1. <ins>Set the running execution context's PrivateNameEnvironment to _outerPrivateEnvironment_.</ins>
198245
1. <ins>Return Completion(_result_).</ins>
199-
1. Set the running execution context's LexicalEnvironment to _lex_.
246+
1. <ins>Set the running execution context's LexicalEnvironment to _lex_.</ins>
247+
1. Set the running execution context's PrivateNameEnvironment to _outerPrivateEnvironment_.
200248
1. Return _F_.
201249
</emu-alg>
202250
</emu-clause>

0 commit comments

Comments
 (0)