Skip to content

Commit 094cb6d

Browse files
committed
Added comments for __generator, reduced overall size of helper
1 parent 24b802e commit 094cb6d

9 files changed

+238
-306
lines changed

src/compiler/emitter.ts

Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -71,49 +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, y, f, v, r;
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-
if (f = 1, y) {
87-
v = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"];
88-
if (v && !(v = v.call(y, op[1])).done) return v;
89-
if (y = void 0, v) op[0] = 0, op[1] = v.value; continue;
90-
}
91-
switch (op[0]) {
92-
case 0: case 1: sent = op; break;
93-
case 4: return _.label++, { value: op[1], done: false };
94-
case 5: _.label++, y = op[1], op = [0]; continue;
95-
case 7: op = _.stack.pop(), _.trys.pop(); continue;
96-
default:
97-
r = _.trys.length > 0 && _.trys[_.trys.length - 1];
98-
if (!r && (op[0] === 6 || op[0] === 2)) { _.done = 1; continue; }
99-
if (op[0] === 3 && (!r || (op[1] > r[0] && op[1] < r[3]))) { _.label = op[1]; break; }
100-
if (op[0] === 6 && _.label < r[1]) { _.label = r[1], sent = op; break; }
101-
if (r && _.label < r[2]) { _.label = r[2], _.stack.push(op); break; }
102-
if (r[2]) { _.stack.pop(); }
103-
_.trys.pop();
104-
continue;
105-
}
106-
op = body.call(thisArg, _);
107-
}
108-
catch (e) { op = [6, e], y = void 0; }
109-
finally { f = 0, sent = v = r = void 0; }
110-
}
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 };
111159
}
112-
return {
113-
next: function (v) { return step([0, v]); },
114-
"throw": function (v) { return step([1, v]); },
115-
"return": function (v) { return step([2, v]); }
116-
};
117160
};`;
118161

119162
// emit output for the __export helper function

src/compiler/transformers/generators.ts

Lines changed: 10 additions & 9 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 |

tests/baselines/reference/asyncAwaitIsolatedModules_es5.js

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,31 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
5050
});
5151
};
5252
var __generator = (this && this.__generator) || function (thisArg, body) {
53-
var _ = { label: 0, sent: function() { if (sent[0] === 1) throw sent[1]; return sent[1]; }, trys: [], stack: [] }, sent, y, f, v, r;
53+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;
54+
return { next: verb(0), "throw": verb(1), "return": verb(2) };
55+
function verb(n) { return function (v) { return step([n, v]); }; }
5456
function step(op) {
5557
if (f) throw new TypeError("Generator is already executing.");
56-
while (1) {
57-
if (_.done) switch (op[0]) {
58-
case 0: return { value: void 0, done: true };
59-
case 1: case 6: throw op[1];
60-
case 2: return { value: op[1], done: true };
58+
while (_) try {
59+
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
60+
if (y = 0, t) op = [0, t.value];
61+
switch (op[0]) {
62+
case 0: case 1: t = op; break;
63+
case 4: _.label++; return { value: op[1], done: false };
64+
case 5: _.label++; y = op[1]; op = [0]; continue;
65+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
66+
default:
67+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
68+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
69+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
70+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
71+
if (t[2]) _.ops.pop();
72+
_.trys.pop(); continue;
6173
}
62-
try {
63-
if (f = 1, y) {
64-
v = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"];
65-
if (v && !(v = v.call(y, op[1])).done) return v;
66-
if (y = void 0, v) op[0] = 0, op[1] = v.value; continue;
67-
}
68-
switch (op[0]) {
69-
case 0: case 1: sent = op; break;
70-
case 4: return _.label++, { value: op[1], done: false };
71-
case 5: _.label++, y = op[1], op = [0]; continue;
72-
case 7: op = _.stack.pop(), _.trys.pop(); continue;
73-
default:
74-
r = _.trys.length > 0 && _.trys[_.trys.length - 1];
75-
if (!r && (op[0] === 6 || op[0] === 2)) { _.done = 1; continue; }
76-
if (op[0] === 3 && (!r || (op[1] > r[0] && op[1] < r[3]))) { _.label = op[1]; break; }
77-
if (op[0] === 6 && _.label < r[1]) { _.label = r[1], sent = op; break; }
78-
if (r && _.label < r[2]) { _.label = r[2], _.stack.push(op); break; }
79-
if (r[2]) { _.stack.pop(); }
80-
_.trys.pop();
81-
continue;
82-
}
83-
op = body.call(thisArg, _);
84-
}
85-
catch (e) { op = [6, e], y = void 0; }
86-
finally { f = 0, sent = v = r = void 0; }
87-
}
74+
op = body.call(thisArg, _);
75+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
76+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
8877
}
89-
return {
90-
next: function (v) { return step([0, v]); },
91-
"throw": function (v) { return step([1, v]); },
92-
"return": function (v) { return step([2, v]); }
93-
};
9478
};
9579
var _this = this;
9680
function f0() {

tests/baselines/reference/asyncAwait_es5.js

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,47 +49,31 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
4949
});
5050
};
5151
var __generator = (this && this.__generator) || function (thisArg, body) {
52-
var _ = { label: 0, sent: function() { if (sent[0] === 1) throw sent[1]; return sent[1]; }, trys: [], stack: [] }, sent, y, f, v, r;
52+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;
53+
return { next: verb(0), "throw": verb(1), "return": verb(2) };
54+
function verb(n) { return function (v) { return step([n, v]); }; }
5355
function step(op) {
5456
if (f) throw new TypeError("Generator is already executing.");
55-
while (1) {
56-
if (_.done) switch (op[0]) {
57-
case 0: return { value: void 0, done: true };
58-
case 1: case 6: throw op[1];
59-
case 2: return { value: op[1], done: true };
57+
while (_) try {
58+
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
59+
if (y = 0, t) op = [0, t.value];
60+
switch (op[0]) {
61+
case 0: case 1: t = op; break;
62+
case 4: _.label++; return { value: op[1], done: false };
63+
case 5: _.label++; y = op[1]; op = [0]; continue;
64+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
65+
default:
66+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
67+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
68+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
69+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
70+
if (t[2]) _.ops.pop();
71+
_.trys.pop(); continue;
6072
}
61-
try {
62-
if (f = 1, y) {
63-
v = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"];
64-
if (v && !(v = v.call(y, op[1])).done) return v;
65-
if (y = void 0, v) op[0] = 0, op[1] = v.value; continue;
66-
}
67-
switch (op[0]) {
68-
case 0: case 1: sent = op; break;
69-
case 4: return _.label++, { value: op[1], done: false };
70-
case 5: _.label++, y = op[1], op = [0]; continue;
71-
case 7: op = _.stack.pop(), _.trys.pop(); continue;
72-
default:
73-
r = _.trys.length > 0 && _.trys[_.trys.length - 1];
74-
if (!r && (op[0] === 6 || op[0] === 2)) { _.done = 1; continue; }
75-
if (op[0] === 3 && (!r || (op[1] > r[0] && op[1] < r[3]))) { _.label = op[1]; break; }
76-
if (op[0] === 6 && _.label < r[1]) { _.label = r[1], sent = op; break; }
77-
if (r && _.label < r[2]) { _.label = r[2], _.stack.push(op); break; }
78-
if (r[2]) { _.stack.pop(); }
79-
_.trys.pop();
80-
continue;
81-
}
82-
op = body.call(thisArg, _);
83-
}
84-
catch (e) { op = [6, e], y = void 0; }
85-
finally { f = 0, sent = v = r = void 0; }
86-
}
73+
op = body.call(thisArg, _);
74+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
75+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
8776
}
88-
return {
89-
next: function (v) { return step([0, v]); },
90-
"throw": function (v) { return step([1, v]); },
91-
"return": function (v) { return step([2, v]); }
92-
};
9377
};
9478
var _this = this;
9579
function f0() {

0 commit comments

Comments
 (0)