mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-16 15:45:27 -05:00
@@ -73,7 +73,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
||||
|
||||
const generatorHelper = `
|
||||
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
var _ = { label: 0, sent: function() { if (sent[0] === 1) throw sent[1]; return sent[1]; }, trys: [], stack: [] }, sent, f;
|
||||
var _ = { label: 0, sent: function() { if (sent[0] === 1) throw sent[1]; return sent[1]; }, trys: [], stack: [] }, sent, y, f, v, r;
|
||||
function step(op) {
|
||||
if (f) throw new TypeError("Generator is already executing.");
|
||||
while (1) {
|
||||
@@ -83,12 +83,18 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
case 2: return { value: op[1], done: true };
|
||||
}
|
||||
try {
|
||||
switch (f = 1, op[0]) {
|
||||
if (f = 1, y) {
|
||||
v = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"];
|
||||
if (v && !(v = v.call(y, op[1])).done) return v;
|
||||
if (y = void 0, v) op[0] = 0, op[1] = v.value; continue;
|
||||
}
|
||||
switch (op[0]) {
|
||||
case 0: case 1: sent = op; break;
|
||||
case 4: return _.label++, { value: op[1], done: false };
|
||||
case 5: _.label++, y = op[1], op = [0]; continue;
|
||||
case 7: op = _.stack.pop(), _.trys.pop(); continue;
|
||||
default:
|
||||
var r = _.trys.length > 0 && _.trys[_.trys.length - 1];
|
||||
r = _.trys.length > 0 && _.trys[_.trys.length - 1];
|
||||
if (!r && (op[0] === 6 || op[0] === 2)) { _.done = 1; continue; }
|
||||
if (op[0] === 3 && (!r || (op[1] > r[0] && op[1] < r[3]))) { _.label = op[1]; break; }
|
||||
if (op[0] === 6 && _.label < r[1]) { _.label = r[1], sent = op; break; }
|
||||
@@ -99,8 +105,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||
}
|
||||
op = body.call(thisArg, _);
|
||||
}
|
||||
catch (e) { op = [6, e]; }
|
||||
finally { f = 0, sent = void 0; }
|
||||
catch (e) { op = [6, e], y = void 0; }
|
||||
finally { f = 0, sent = v = r = void 0; }
|
||||
}
|
||||
}
|
||||
return {
|
||||
|
||||
@@ -2055,9 +2055,24 @@ namespace ts {
|
||||
if (!isBlock(loopBody)) {
|
||||
loopBody = createBlock([loopBody], /*location*/ undefined, /*multiline*/ true);
|
||||
}
|
||||
|
||||
const isAsyncBlockContainingAwait =
|
||||
containingNonArrowFunction
|
||||
&& (containingNonArrowFunction.emitFlags & NodeEmitFlags.AsyncFunctionBody) !== 0
|
||||
&& (node.statement.transformFlags & TransformFlags.ContainsYield) !== 0;
|
||||
|
||||
let loopBodyFlags: NodeEmitFlags = 0;
|
||||
if (currentState.containsLexicalThis) {
|
||||
loopBodyFlags |= NodeEmitFlags.CapturesThis;
|
||||
}
|
||||
|
||||
if (isAsyncBlockContainingAwait) {
|
||||
loopBodyFlags |= NodeEmitFlags.AsyncFunctionBody;
|
||||
}
|
||||
|
||||
const convertedLoopVariable =
|
||||
createVariableStatement(
|
||||
/*modifiers*/ undefined,
|
||||
/*modifiers*/ undefined,
|
||||
createVariableDeclarationList(
|
||||
[
|
||||
createVariableDeclaration(
|
||||
@@ -2065,16 +2080,14 @@ namespace ts {
|
||||
/*type*/ undefined,
|
||||
setNodeEmitFlags(
|
||||
createFunctionExpression(
|
||||
/*asteriskToken*/ undefined,
|
||||
isAsyncBlockContainingAwait ? createToken(SyntaxKind.AsteriskToken) : undefined,
|
||||
/*name*/ undefined,
|
||||
/*typeParameters*/ undefined,
|
||||
loopParameters,
|
||||
/*type*/ undefined,
|
||||
<Block>loopBody
|
||||
),
|
||||
currentState.containsLexicalThis
|
||||
? NodeEmitFlags.CapturesThis
|
||||
: 0
|
||||
loopBodyFlags
|
||||
)
|
||||
)
|
||||
]
|
||||
@@ -2160,7 +2173,7 @@ namespace ts {
|
||||
));
|
||||
}
|
||||
|
||||
const convertedLoopBodyStatements = generateCallToConvertedLoop(functionName, loopParameters, currentState);
|
||||
const convertedLoopBodyStatements = generateCallToConvertedLoop(functionName, loopParameters, currentState, isAsyncBlockContainingAwait);
|
||||
let loop: IterationStatement;
|
||||
if (convert) {
|
||||
loop = convert(node, convertedLoopBodyStatements);
|
||||
@@ -2173,12 +2186,17 @@ namespace ts {
|
||||
loop = visitEachChild(loop, visitor, context);
|
||||
// set loop statement
|
||||
loop.statement = createBlock(
|
||||
generateCallToConvertedLoop(functionName, loopParameters, currentState),
|
||||
convertedLoopBodyStatements,
|
||||
/*location*/ undefined,
|
||||
/*multiline*/ true
|
||||
);
|
||||
|
||||
// reset and re-aggregate the transform flags
|
||||
loop.transformFlags = 0;
|
||||
aggregateTransformFlags(loop);
|
||||
}
|
||||
|
||||
|
||||
statements.push(
|
||||
currentParent.kind === SyntaxKind.LabeledStatement
|
||||
? createLabel((<LabeledStatement>currentParent).label, loop)
|
||||
@@ -2199,7 +2217,7 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function generateCallToConvertedLoop(loopFunctionExpressionName: Identifier, parameters: ParameterDeclaration[], state: ConvertedLoopState): Statement[] {
|
||||
function generateCallToConvertedLoop(loopFunctionExpressionName: Identifier, parameters: ParameterDeclaration[], state: ConvertedLoopState, isAsyncBlockContainingAwait: boolean): Statement[] {
|
||||
const outerConvertedLoopState = convertedLoopState;
|
||||
|
||||
const statements: Statement[] = [];
|
||||
@@ -2212,8 +2230,9 @@ namespace ts {
|
||||
!state.labeledNonLocalContinues;
|
||||
|
||||
const call = createCall(loopFunctionExpressionName, /*typeArguments*/ undefined, map(parameters, p => <Identifier>p.name));
|
||||
const callResult = isAsyncBlockContainingAwait ? createYield(createToken(SyntaxKind.AsteriskToken), call) : call;
|
||||
if (isSimpleLoop) {
|
||||
statements.push(createStatement(call));
|
||||
statements.push(createStatement(callResult));
|
||||
copyOutParameters(state.loopOutParameters, CopyDirection.ToOriginal, statements);
|
||||
}
|
||||
else {
|
||||
@@ -2221,7 +2240,7 @@ namespace ts {
|
||||
const stateVariable = createVariableStatement(
|
||||
/*modifiers*/ undefined,
|
||||
createVariableDeclarationList(
|
||||
[createVariableDeclaration(loopResultName, /*type*/ undefined, call)]
|
||||
[createVariableDeclaration(loopResultName, /*type*/ undefined, callResult)]
|
||||
)
|
||||
);
|
||||
statements.push(stateVariable);
|
||||
|
||||
@@ -906,7 +906,7 @@ namespace ts {
|
||||
*
|
||||
* @param node The node to visit.
|
||||
*/
|
||||
function visitYieldExpression(node: YieldExpression) {
|
||||
function visitYieldExpression(node: YieldExpression): LeftHandSideExpression {
|
||||
// [source]
|
||||
// x = yield a();
|
||||
//
|
||||
@@ -917,7 +917,14 @@ namespace ts {
|
||||
|
||||
// NOTE: we are explicitly not handling YieldStar at this time.
|
||||
const resumeLabel = defineLabel();
|
||||
emitYield(visitNode(node.expression, visitor, isExpression), /*location*/ node);
|
||||
const expression = visitNode(node.expression, visitor, isExpression);
|
||||
if (node.asteriskToken) {
|
||||
emitYieldStar(expression, /*location*/ node);
|
||||
}
|
||||
else {
|
||||
emitYield(expression, /*location*/ node);
|
||||
}
|
||||
|
||||
markLabel(resumeLabel);
|
||||
return createGeneratorResume();
|
||||
}
|
||||
@@ -2480,6 +2487,16 @@ namespace ts {
|
||||
emitWorker(OpCode.BreakWhenFalse, [label, condition], location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits a YieldStar operation for the provided expression.
|
||||
*
|
||||
* @param expression An optional value for the yield operation.
|
||||
* @param location An optional source map location for the assignment.
|
||||
*/
|
||||
function emitYieldStar(expression?: Expression, location?: TextRange): void {
|
||||
emitWorker(OpCode.YieldStar, [expression], location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits a Yield operation for the provided expression.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user