Skip to content

Commit 02334d8

Browse files
authored
Merge pull request #10890 from Microsoft/fix10889
Fix captured block scope variables in downlevel async.
2 parents 873850b + 094cb6d commit 02334d8

14 files changed

+722
-270
lines changed

src/compiler/emitter.ts

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,43 +71,92 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7171
});
7272
};`;
7373

74+
// The __generator helper is used by down-level transformations to emulate the runtime
75+
// semantics of an ES2015 generator function. When called, this helper returns an
76+
// object that implements the Iterator protocol, in that it has `next`, `return`, and
77+
// `throw` methods that step through the generator when invoked.
78+
//
79+
// parameters:
80+
// thisArg The value to use as the `this` binding for the transformed generator body.
81+
// body A function that acts as the transformed generator body.
82+
//
83+
// variables:
84+
// _ Persistent state for the generator that is shared between the helper and the
85+
// generator body. The state object has the following members:
86+
// sent() - A method that returns or throws the current completion value.
87+
// label - The next point at which to resume evaluation of the generator body.
88+
// trys - A stack of protected regions (try/catch/finally blocks).
89+
// ops - A stack of pending instructions when inside of a finally block.
90+
// f A value indicating whether the generator is executing.
91+
// y An iterator to delegate for a yield*.
92+
// t A temporary variable that holds one of the following values (note that these
93+
// cases do not overlap):
94+
// - The completion value when resuming from a `yield` or `yield*`.
95+
// - The error value for a catch block.
96+
// - The current protected region (array of try/catch/finally/end labels).
97+
// - The verb (`next`, `throw`, or `return` method) to delegate to the expression
98+
// of a `yield*`.
99+
// - The result of evaluating the verb delegated to the expression of a `yield*`.
100+
//
101+
// functions:
102+
// verb(n) Creates a bound callback to the `step` function for opcode `n`.
103+
// step(op) Evaluates opcodes in a generator body until execution is suspended or
104+
// completed.
105+
//
106+
// The __generator helper understands a limited set of instructions:
107+
// 0: next(value?) - Start or resume the generator with the specified value.
108+
// 1: throw(error) - Resume the generator with an exception. If the generator is
109+
// suspended inside of one or more protected regions, evaluates
110+
// any intervening finally blocks between the current label and
111+
// the nearest catch block or function boundary. If uncaught, the
112+
// exception is thrown to the caller.
113+
// 2: return(value?) - Resume the generator as if with a return. If the generator is
114+
// suspended inside of one or more protected regions, evaluates any
115+
// intervening finally blocks.
116+
// 3: break(label) - Jump to the specified label. If the label is outside of the
117+
// current protected region, evaluates any intervening finally
118+
// blocks.
119+
// 4: yield(value?) - Yield execution to the caller with an optional value. When
120+
// resumed, the generator will continue at the next label.
121+
// 5: yield*(value) - Delegates evaluation to the supplied iterator. When
122+
// delegation completes, the generator will continue at the next
123+
// label.
124+
// 6: catch(error) - Handles an exception thrown from within the generator body. If
125+
// the current label is inside of one or more protected regions,
126+
// evaluates any intervening finally blocks between the current
127+
// label and the nearest catch block or function boundary. If
128+
// uncaught, the exception is thrown to the caller.
129+
// 7: endfinally - Ends a finally block, resuming the last instruction prior to
130+
// entering a finally block.
131+
//
132+
// For examples of how these are used, see the comments in ./transformers/generators.ts
74133
const generatorHelper = `
75134
var __generator = (this && this.__generator) || function (thisArg, body) {
76-
var _ = { label: 0, sent: function() { if (sent[0] === 1) throw sent[1]; return sent[1]; }, trys: [], stack: [] }, sent, f;
135+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;
136+
return { next: verb(0), "throw": verb(1), "return": verb(2) };
137+
function verb(n) { return function (v) { return step([n, v]); }; }
77138
function step(op) {
78139
if (f) throw new TypeError("Generator is already executing.");
79-
while (1) {
80-
if (_.done) switch (op[0]) {
81-
case 0: return { value: void 0, done: true };
82-
case 1: case 6: throw op[1];
83-
case 2: return { value: op[1], done: true };
84-
}
85-
try {
86-
switch (f = 1, op[0]) {
87-
case 0: case 1: sent = op; break;
88-
case 4: return _.label++, { value: op[1], done: false };
89-
case 7: op = _.stack.pop(), _.trys.pop(); continue;
90-
default:
91-
var r = _.trys.length > 0 && _.trys[_.trys.length - 1];
92-
if (!r && (op[0] === 6 || op[0] === 2)) { _.done = 1; continue; }
93-
if (op[0] === 3 && (!r || (op[1] > r[0] && op[1] < r[3]))) { _.label = op[1]; break; }
94-
if (op[0] === 6 && _.label < r[1]) { _.label = r[1], sent = op; break; }
95-
if (r && _.label < r[2]) { _.label = r[2], _.stack.push(op); break; }
96-
if (r[2]) { _.stack.pop(); }
97-
_.trys.pop();
98-
continue;
99-
}
100-
op = body.call(thisArg, _);
101-
}
102-
catch (e) { op = [6, e]; }
103-
finally { f = 0, sent = void 0; }
104-
}
140+
while (_) try {
141+
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
142+
if (y = 0, t) op = [0, t.value];
143+
switch (op[0]) {
144+
case 0: case 1: t = op; break;
145+
case 4: _.label++; return { value: op[1], done: false };
146+
case 5: _.label++; y = op[1]; op = [0]; continue;
147+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
148+
default:
149+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
150+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
151+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
152+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
153+
if (t[2]) _.ops.pop();
154+
_.trys.pop(); continue;
155+
}
156+
op = body.call(thisArg, _);
157+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
158+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
105159
}
106-
return {
107-
next: function (v) { return step([0, v]); },
108-
"throw": function (v) { return step([1, v]); },
109-
"return": function (v) { return step([2, v]); }
110-
};
111160
};`;
112161

113162
// emit output for the __export helper function

src/compiler/transformers/es6.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,26 +2055,39 @@ namespace ts {
20552055
if (!isBlock(loopBody)) {
20562056
loopBody = createBlock([loopBody], /*location*/ undefined, /*multiline*/ true);
20572057
}
2058+
2059+
const isAsyncBlockContainingAwait =
2060+
containingNonArrowFunction
2061+
&& (containingNonArrowFunction.emitFlags & NodeEmitFlags.AsyncFunctionBody) !== 0
2062+
&& (node.statement.transformFlags & TransformFlags.ContainsYield) !== 0;
2063+
2064+
let loopBodyFlags: NodeEmitFlags = 0;
2065+
if (currentState.containsLexicalThis) {
2066+
loopBodyFlags |= NodeEmitFlags.CapturesThis;
2067+
}
2068+
2069+
if (isAsyncBlockContainingAwait) {
2070+
loopBodyFlags |= NodeEmitFlags.AsyncFunctionBody;
2071+
}
2072+
20582073
const convertedLoopVariable =
20592074
createVariableStatement(
2060-
/*modifiers*/ undefined,
2075+
/*modifiers*/ undefined,
20612076
createVariableDeclarationList(
20622077
[
20632078
createVariableDeclaration(
20642079
functionName,
20652080
/*type*/ undefined,
20662081
setNodeEmitFlags(
20672082
createFunctionExpression(
2068-
/*asteriskToken*/ undefined,
2083+
isAsyncBlockContainingAwait ? createToken(SyntaxKind.AsteriskToken) : undefined,
20692084
/*name*/ undefined,
20702085
/*typeParameters*/ undefined,
20712086
loopParameters,
20722087
/*type*/ undefined,
20732088
<Block>loopBody
20742089
),
2075-
currentState.containsLexicalThis
2076-
? NodeEmitFlags.CapturesThis
2077-
: 0
2090+
loopBodyFlags
20782091
)
20792092
)
20802093
]
@@ -2160,7 +2173,7 @@ namespace ts {
21602173
));
21612174
}
21622175

2163-
const convertedLoopBodyStatements = generateCallToConvertedLoop(functionName, loopParameters, currentState);
2176+
const convertedLoopBodyStatements = generateCallToConvertedLoop(functionName, loopParameters, currentState, isAsyncBlockContainingAwait);
21642177
let loop: IterationStatement;
21652178
if (convert) {
21662179
loop = convert(node, convertedLoopBodyStatements);
@@ -2173,12 +2186,17 @@ namespace ts {
21732186
loop = visitEachChild(loop, visitor, context);
21742187
// set loop statement
21752188
loop.statement = createBlock(
2176-
generateCallToConvertedLoop(functionName, loopParameters, currentState),
2189+
convertedLoopBodyStatements,
21772190
/*location*/ undefined,
21782191
/*multiline*/ true
21792192
);
2193+
2194+
// reset and re-aggregate the transform flags
2195+
loop.transformFlags = 0;
2196+
aggregateTransformFlags(loop);
21802197
}
21812198

2199+
21822200
statements.push(
21832201
currentParent.kind === SyntaxKind.LabeledStatement
21842202
? createLabel((<LabeledStatement>currentParent).label, loop)
@@ -2199,7 +2217,7 @@ namespace ts {
21992217
}
22002218
}
22012219

2202-
function generateCallToConvertedLoop(loopFunctionExpressionName: Identifier, parameters: ParameterDeclaration[], state: ConvertedLoopState): Statement[] {
2220+
function generateCallToConvertedLoop(loopFunctionExpressionName: Identifier, parameters: ParameterDeclaration[], state: ConvertedLoopState, isAsyncBlockContainingAwait: boolean): Statement[] {
22032221
const outerConvertedLoopState = convertedLoopState;
22042222

22052223
const statements: Statement[] = [];
@@ -2212,16 +2230,17 @@ namespace ts {
22122230
!state.labeledNonLocalContinues;
22132231

22142232
const call = createCall(loopFunctionExpressionName, /*typeArguments*/ undefined, map(parameters, p => <Identifier>p.name));
2233+
const callResult = isAsyncBlockContainingAwait ? createYield(createToken(SyntaxKind.AsteriskToken), call) : call;
22152234
if (isSimpleLoop) {
2216-
statements.push(createStatement(call));
2235+
statements.push(createStatement(callResult));
22172236
copyOutParameters(state.loopOutParameters, CopyDirection.ToOriginal, statements);
22182237
}
22192238
else {
22202239
const loopResultName = createUniqueName("state");
22212240
const stateVariable = createVariableStatement(
22222241
/*modifiers*/ undefined,
22232242
createVariableDeclarationList(
2224-
[createVariableDeclaration(loopResultName, /*type*/ undefined, call)]
2243+
[createVariableDeclaration(loopResultName, /*type*/ undefined, callResult)]
22252244
)
22262245
);
22272246
statements.push(stateVariable);

src/compiler/transformers/generators.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
// .brfalse LABEL, (x) - Jump to a label IIF the expression `x` is falsey.
2222
// If jumping out of a protected region, all .finally
2323
// blocks are executed.
24-
// .yield RESUME, (x) - Yield the value of the optional expression `x`.
25-
// Resume at the label RESUME.
26-
// .yieldstar RESUME, (x) - Delegate yield to the value of the optional
27-
// expression `x`. Resume at the label RESUME.
24+
// .yield (x) - Yield the value of the optional expression `x`.
25+
// Resume at the next label.
26+
// .yieldstar (x) - Delegate yield to the value of the optional
27+
// expression `x`. Resume at the next label.
28+
// NOTE: `x` must be an Iterator, not an Iterable.
2829
// .loop CONTINUE, BREAK - Marks the beginning of a loop. Any "continue" or
2930
// "break" abrupt completions jump to the CONTINUE or
3031
// BREAK labels, respectively.
@@ -80,13 +81,13 @@
8081
// -------------------------------|----------------------------------------------
8182
// .brfalse LABEL, (x) | if (!(x)) return [3, /*break*/, LABEL];
8283
// -------------------------------|----------------------------------------------
83-
// .yield RESUME, (x) | return [4 /*yield*/, x];
84+
// .yield (x) | return [4 /*yield*/, x];
8485
// .mark RESUME | case RESUME:
85-
// a = %sent%; | a = state.sent();
86+
// a = %sent%; | a = state.sent();
8687
// -------------------------------|----------------------------------------------
87-
// .yieldstar RESUME, (X) | return [5 /*yield**/, x];
88+
// .yieldstar (x) | return [5 /*yield**/, x];
8889
// .mark RESUME | case RESUME:
89-
// a = %sent%; | a = state.sent();
90+
// a = %sent%; | a = state.sent();
9091
// -------------------------------|----------------------------------------------
9192
// .with (_a) | with (_a) {
9293
// a(); | a();
@@ -109,7 +110,7 @@
109110
// .br END | return [3 /*break*/, END];
110111
// .catch (e) |
111112
// .mark CATCH | case CATCH:
112-
// | e = state.error;
113+
// | e = state.sent();
113114
// b(); | b();
114115
// .br END | return [3 /*break*/, END];
115116
// .finally |
@@ -906,7 +907,7 @@ namespace ts {
906907
*
907908
* @param node The node to visit.
908909
*/
909-
function visitYieldExpression(node: YieldExpression) {
910+
function visitYieldExpression(node: YieldExpression): LeftHandSideExpression {
910911
// [source]
911912
// x = yield a();
912913
//
@@ -917,7 +918,14 @@ namespace ts {
917918

918919
// NOTE: we are explicitly not handling YieldStar at this time.
919920
const resumeLabel = defineLabel();
920-
emitYield(visitNode(node.expression, visitor, isExpression), /*location*/ node);
921+
const expression = visitNode(node.expression, visitor, isExpression);
922+
if (node.asteriskToken) {
923+
emitYieldStar(expression, /*location*/ node);
924+
}
925+
else {
926+
emitYield(expression, /*location*/ node);
927+
}
928+
921929
markLabel(resumeLabel);
922930
return createGeneratorResume();
923931
}
@@ -2480,6 +2488,16 @@ namespace ts {
24802488
emitWorker(OpCode.BreakWhenFalse, [label, condition], location);
24812489
}
24822490

2491+
/**
2492+
* Emits a YieldStar operation for the provided expression.
2493+
*
2494+
* @param expression An optional value for the yield operation.
2495+
* @param location An optional source map location for the assignment.
2496+
*/
2497+
function emitYieldStar(expression?: Expression, location?: TextRange): void {
2498+
emitWorker(OpCode.YieldStar, [expression], location);
2499+
}
2500+
24832501
/**
24842502
* Emits a Yield operation for the provided expression.
24852503
*

0 commit comments

Comments
 (0)