-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspec.html
328 lines (303 loc) · 14.5 KB
/
spec.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<pre class=metadata>
title: Syntactic Tail Calls
contributors: Brian Terlson, Eric Faust
</pre>
<link rel="stylesheet" href="ecmarkup.css">
<script src="ecmarkup.js"></script>
<emu-intro>
This document introduces explicit syntax for tail calls. For motivation and an introduction, see
<a href="https://github.com/tc39/proposal-ptc-syntax/blob/master/README.md">the explainer document</a>.
The specification here defines the grammar for explicit tail calls and the runtime semantics which
guarantee tail call elimination for explicit tail call sites and removes guaranteed tail call
elimination from the cases where it was implicitly supported in ES2015. This specification still
<em>permits</em> tail call elimination in other cases, but does not <em>require</em> it.
<emu-intro id="TailCallExpression-grammar-examples">
<h1>Examples</h1>
<p>The following examples are all syntactically valid: </p>
<pre><code class=javascript>
function foo1() { return continue bar(); }
function foo2() { return continue bar`template`; }
function foo3() { return bool ? continue bar() : baz }
let foo4 = () => continue foo();
let foo5 = () => bool ? continue bar() : baz;
</pre></code>
<p>The following examples are all syntax errors: </p>
<pre><code class=javascript>
continue bar();
// continue in a statement context is still a ContinueStatement.
(continue bar());
// continue is only syntactically valid under a return statement
function foo1() { return continue baz; }
function foo2() { return continue 1 + baz() }
function foo3() { return continue baz(), 1 }
// things that aren't tail calls are syntax errors
</pre></code>
</emu-intro>
</emu-intro>
<emu-clause id="TailCallExpression">
<h1>Tail Calls</h1>
<h2>Syntax</h2>
<emu-grammar>
AssignmentExpression[In, Yield] :
ConditionalExpression[?In, ?Yield]
[+Yield] YieldExpression[?In]
ArrowFunction[?In, ?Yield]
LeftHandSideExpression[?Yield] `=` AssignmentExpression[?In, ?Yield]
LeftHandSideExpression[?Yield] AssignmentOperator AssignmentExpression[?In, ?Yield]
<ins>TailCallExpression[?Yield]</ins>
TailCallExpression[Yield] :
`continue` MemberExpression[?Yield] Arguments[?Yield]
`continue` CallExpression[?Yield] Arguments[?Yield]
`continue` MemberExpression[?Yield] TemplateLiteral[?Yield]
`continue` CallExpression[?Yield] TemplateLiteral[?Yield]
</emu-grammar>
<emu-clause id="TailCallExpression-early-errors">
<h1>Static Semantics: Early Errors</h1>
<ul>
<li>It is a Syntax Error IsInTailPosition of |TailCallExpression| is *false*.</li>
<li>It is a Syntax Error if the StringValue of the |MemberExpression| is `eval`.</li>
</ul>
</emu-clause>
<emu-clause id="TailCallExpression-evaluation">
<h1>Runtime Semantics: Evaluation</h1>
<emu-grammar>TailCallExpression : `continue` MemberExpression Arguments</emu-grammar>
<emu-alg>
1. Let _ref_ be the result of evaluating |MemberExpression|.
1. Let _func_ be ? GetValue(_ref_).
1. If Type(_ref_) is Reference, then
1. If IsPropertyReference(_ref_) is *true*, then
1. Let _thisValue_ be GetThisValue(_ref_).
1. Else, the base of _ref_ is an Environment Record
1. Let _refEnv_ be GetBase(_ref_).
1. Let _thisValue_ be _refEnv_.WithBaseObject().
1. Else Type(_ref_) is not Reference,
1. Let _thisValue_ be *undefined*.
1. Return ? EvaluateDirectCall(_func_, _thisValue_, |Arguments|, *true*).
</emu-alg>
<emu-grammar>TailCallExpression : `continue` CallExpression Arguments</emu-grammar>
<emu-alg>
1. Let _ref_ be the result of evaluating |CallExpression|.
1. Return ? EvaluateCall(_ref_, |Arguments|, *true*).
</emu-alg>
</emu-clause>
<emu-grammar>TailCallExpression : `continue` MemberExpression TemplateLiteral</emu-grammar>
<emu-alg>
1. Let _tagRef_ be the result of evaluating |MemberExpression|.
1. Return ? EvaluateCall(_tagRef_, |TemplateLiteral|, *true*).
</emu-alg>
<emu-grammar>TailCallExpression : `continue` CallExpression TemplateLiteral</emu-grammar>
<emu-alg>
1. Let _tagRef_ be the result of evaluating |CallExpression|.
1. Return ? EvaluateCall(_tagRef_, |TemplateLiteral|, *true*).
</emu-clause>
</emu-clause>
</emu-clause>
<emu-clause id="patches">
<h1>Modifications to existing algorithms</h1>
<emu-clause id="sec-function-calls">
<h1>Function Calls</h1>
<!-- es6num="12.3.4.1" -->
<emu-clause id="sec-function-calls-runtime-semantics-evaluation">
<h1>Runtime Semantics: Evaluation</h1>
<emu-grammar>CallExpression : MemberExpression Arguments</emu-grammar>
<emu-alg>
1. Let _ref_ be the result of evaluating |MemberExpression|.
1. Let _func_ be ? GetValue(_ref_).
1. If Type(_ref_) is Reference and IsPropertyReference(_ref_) is *false* and GetReferencedName(_ref_) is `"eval"`, then
1. If SameValue(_func_, %eval%) is *true*, then
1. Let _argList_ be ? ArgumentListEvaluation(|Arguments|).
1. If _argList_ has no elements, return *undefined*.
1. Let _evalText_ be the first element of _argList_.
1. If the source code matching this |CallExpression| is strict code, let _strictCaller_ be *true*. Otherwise let _strictCaller_ be *false*.
1. Let _evalRealm_ be the current Realm Record.
1. Perform ? HostEnsureCanCompileStrings(_evalRealm_, _evalRealm_).
1. Return ? PerformEval(_evalText_, _evalRealm_, _strictCaller_, *true*).
1. If Type(_ref_) is Reference, then
1. If IsPropertyReference(_ref_) is *true*, then
1. Let _thisValue_ be GetThisValue(_ref_).
1. Else, the base of _ref_ is an Environment Record
1. Let _refEnv_ be GetBase(_ref_).
1. Let _thisValue_ be _refEnv_.WithBaseObject().
1. Else Type(_ref_) is not Reference,
1. Let _thisValue_ be *undefined*.
1. <del>Let _thisCall_ be this |CallExpression|.</del>
1. <del>Let _tailCall_ be IsInTailPosition(_thisCall_).</del>
1. Return ? EvaluateDirectCall(_func_, _thisValue_, |Arguments|, <del>_tailCall_</del><ins>*false*</ins>).
</emu-alg>
<p>A |CallExpression| evaluation that executes step 3.a.vi is a <dfn>direct eval</dfn>.</p>
<emu-grammar>CallExpression : CallExpression Arguments</emu-grammar>
<emu-alg>
1. Let _ref_ be the result of evaluating |CallExpression|.
1. <del>Let _thisCall_ be this |CallExpression|.</del>
1. <del>Let _tailCall_ be IsInTailPosition(_thisCall_).</del>
1. Return ? EvaluateCall(_ref_, |Arguments|, <del>_tailCall_</del><ins>*false*</ins>).
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-tagged-templates">
<h1>Tagged Templates</h1>
<!-- es6num="12.3.7.1" -->
<emu-clause id="sec-tagged-templates-runtime-semantics-evaluation">
<h1>Runtime Semantics: Evaluation</h1>
<emu-grammar>MemberExpression : MemberExpression TemplateLiteral</emu-grammar>
<emu-alg>
1. Let _tagRef_ be the result of evaluating |MemberExpression|.
1. <del>Let _thisCall_ be this |CallExpression|.</del>
1. <del>Let _tailCall_ be IsInTailPosition(_thisCall_).</del>
1. Return ? EvaluateCall(_tagRef_, |TemplateLiteral|, <del>_tailCall_</del><ins>*false*</ins>).
</emu-alg>
<emu-grammar>CallExpression : CallExpression TemplateLiteral</emu-grammar>
<emu-alg>
1. Let _tagRef_ be the result of evaluating |CallExpression|.
1. <del>Let _thisCall_ be this |CallExpression|.</del>
1. <del>Let _tailCall_ be IsInTailPosition(_thisCall_).</del>
1. Return ? EvaluateCall(_tagRef_, |TemplateLiteral|, <del>_tailCall_</del><ins>*false*</ins>).
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-static-semantics-hasproductionintailposition">
<h1>Static Semantics: HasProductionInTailPosition</h1>
<p>With parameter _nonterminal_.</p>
<!-- es6num="14.6.2.2" -->
<emu-clause id="sec-expression-rules">
<h1>Expression Rules</h1>
<emu-note>
<p>A potential tail position call that is immediately followed by return GetValue of the call result is also a possible tail position call. Function calls cannot return reference values, so such a GetValue operation will always returns the same value as the actual function call result.</p>
</emu-note>
<emu-grammar>
AssignmentExpression :
YieldExpression
ArrowFunction
LeftHandSideExpression `=` AssignmentExpression
LeftHandSideExpression AssignmentOperator AssignmentExpression
BitwiseANDExpression : BitwiseANDExpression `&` EqualityExpression
BitwiseXORExpression : BitwiseXORExpression `^` BitwiseANDExpression
BitwiseORExpression : BitwiseORExpression `|` BitwiseXORExpression
EqualityExpression :
EqualityExpression `==` RelationalExpression
EqualityExpression `!=` RelationalExpression
EqualityExpression `===` RelationalExpression
EqualityExpression `!==` RelationalExpression
RelationalExpression :
RelationalExpression `<` ShiftExpression
RelationalExpression `>` ShiftExpression
RelationalExpression `<=` ShiftExpression
RelationalExpression `>=` ShiftExpression
RelationalExpression `instanceof` ShiftExpression
RelationalExpression `in` ShiftExpression
ShiftExpression :
ShiftExpression `<<` AdditiveExpression
ShiftExpression `>>` AdditiveExpression
ShiftExpression `>>>` AdditiveExpression
AdditiveExpression :
AdditiveExpression `+` MultiplicativeExpression
AdditiveExpression `-` MultiplicativeExpression
MultiplicativeExpression :
MultiplicativeExpression MultiplicativeOperator ExponentiationExpression
ExponentiationExpression :
UpdateExpression `**` ExponentiationExpression
UpdateExpression :
LeftHandSideExpression `++`
LeftHandSideExpression `--`
`++` UnaryExpression
`--` UnaryExpression
UnaryExpression :
`delete` UnaryExpression
`void` UnaryExpression
`typeof` UnaryExpression
`+` UnaryExpression
`-` UnaryExpression
`~` UnaryExpression
`!` UnaryExpression
CallExpression :
SuperCall
CallExpression `[` Expression `]`
CallExpression `.` IdentifierName
NewExpression : `new` NewExpression
MemberExpression :
MemberExpression `[` Expression `]`
MemberExpression `.` IdentifierName
SuperProperty
MetaProperty
`new` MemberExpression Arguments
PrimaryExpression :
`this`
IdentifierReference
Literal
ArrayLiteral
ObjectLiteral
FunctionExpression
ClassExpression
GeneratorExpression
RegularExpressionLiteral
TemplateLiteral
</emu-grammar>
<emu-alg>
1. Return *false*.
</emu-alg>
<emu-grammar>
Expression :
AssignmentExpression
Expression `,` AssignmentExpression
</emu-grammar>
<emu-alg>
1. Return HasProductionInTailPosition of |AssignmentExpression| with argument _nonterminal_.
</emu-alg>
<emu-grammar>ConditionalExpression : LogicalORExpression `?` AssignmentExpression `:` AssignmentExpression</emu-grammar>
<emu-alg>
1. Let _has_ be HasProductionInTailPosition of the first |AssignmentExpression| with argument _nonterminal_.
1. If _has_ is *true*, return *true*.
1. Return HasProductionInTailPosition of the second |AssignmentExpression| with argument _nonterminal_.
</emu-alg>
<emu-grammar>LogicalANDExpression : LogicalANDExpression `&&` BitwiseORExpression</emu-grammar>
<emu-alg>
1. Return HasProductionInTailPosition of |BitwiseORExpression| with argument _nonterminal_.
</emu-alg>
<emu-grammar>LogicalORExpression : LogicalORExpression `||` LogicalANDExpression</emu-grammar>
<emu-alg>
1. Return HasProductionInTailPosition of |LogicalANDExpression| with argument _nonterminal_.
</emu-alg>
<emu-grammar>
CallExpression :
MemberExpression Arguments
CallExpression Arguments
CallExpression TemplateLiteral
</emu-grammar>
<emu-alg>
1. <del>If this |CallExpression| is _nonterminal_, return *true*.</del>
1. Return *false*.
</emu-alg>
<emu-grammar>
MemberExpression :
MemberExpression TemplateLiteral
</emu-grammar>
<emu-alg>
1. <del>If this |MemberExpression| is _nonterminal_, return *true*.</del>
1. Return *false*.
</emu-alg>
<emu-grammar>PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList</emu-grammar>
<emu-alg>
1. Let _expr_ be CoveredParenthesizedExpression of |CoverParenthesizedExpressionAndArrowParameterList|.
1. Return HasProductionInTailPosition of _expr_ with argument _nonterminal_.
</emu-alg>
<emu-grammar>
ParenthesizedExpression :
`(` Expression `)`
</emu-grammar>
<emu-alg>
1. Return HasProductionInTailPosition of |Expression| with argument _nonterminal_.
</emu-alg>
<emu-grammar>
TailCallExpression[Yield] :
`continue` MemberExpression[?Yield] Arguments[?Yield]
`continue` CallExpression[?Yield] Arguments[?Yield]
`continue` MemberExpression[?Yield] TemplateLiteral[?Yield]
`continue` CallExpression[?Yield] TemplateLiteral[?Yield]
</emu-grammar>
<emu-alg>
1. <ins>If this |TailCallExpression| is _nonterminal_, return *true*.</ins>
1. <ins>Return *false*.</ins>
</emu-alg>
</emu-clause>
</emu-clause>
</emu-clause>