emit.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Emitter = void 0;
  6. var _assert = require("assert");
  7. var leap = require("./leap.js");
  8. var meta = require("./meta.js");
  9. var util = require("./util.js");
  10. var _core = require("@babel/core");
  11. const PENDING_LOCATION = Number.MAX_VALUE;
  12. function getDeclError(node) {
  13. return new Error("all declarations should have been transformed into " + "assignments before the Exploder began its work: " + JSON.stringify(node));
  14. }
  15. const catchParamVisitor = {
  16. Identifier: function (path, state) {
  17. if (path.node.name === state.catchParamName && util.isReference(path)) {
  18. util.replaceWithOrRemove(path, state.getSafeParam());
  19. }
  20. },
  21. Scope: function (path, state) {
  22. if (path.scope.hasOwnBinding(state.catchParamName)) {
  23. path.skip();
  24. }
  25. }
  26. };
  27. class Emitter {
  28. constructor(contextId) {
  29. this.nextTempId = void 0;
  30. this.contextId = void 0;
  31. this.listing = void 0;
  32. this.marked = void 0;
  33. this.insertedLocs = void 0;
  34. this.finalLoc = void 0;
  35. this.tryEntries = void 0;
  36. this.leapManager = void 0;
  37. this.nextTempId = 0;
  38. this.contextId = contextId;
  39. this.listing = [];
  40. this.marked = [true];
  41. this.insertedLocs = new Set();
  42. this.finalLoc = this.loc();
  43. this.tryEntries = [];
  44. this.leapManager = new leap.LeapManager(this);
  45. }
  46. loc() {
  47. const l = _core.types.numericLiteral(PENDING_LOCATION);
  48. this.insertedLocs.add(l);
  49. return l;
  50. }
  51. getInsertedLocs() {
  52. return this.insertedLocs;
  53. }
  54. getContextId() {
  55. return _core.types.cloneNode(this.contextId);
  56. }
  57. mark(loc) {
  58. const index = this.listing.length;
  59. if (loc.value === PENDING_LOCATION) {
  60. loc.value = index;
  61. } else {
  62. _assert.strictEqual(loc.value, index);
  63. }
  64. this.marked[index] = true;
  65. return loc;
  66. }
  67. emit(node) {
  68. if (_core.types.isExpression(node)) {
  69. node = _core.types.expressionStatement(node);
  70. }
  71. _core.types.assertStatement(node);
  72. this.listing.push(node);
  73. }
  74. emitAssign(lhs, rhs) {
  75. this.emit(this.assign(lhs, rhs));
  76. return lhs;
  77. }
  78. assign(lhs, rhs) {
  79. return _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(lhs), rhs));
  80. }
  81. contextProperty(name, computed) {
  82. return _core.types.memberExpression(this.getContextId(), computed ? _core.types.stringLiteral(name) : _core.types.identifier(name), !!computed);
  83. }
  84. clearPendingException(tryLoc, assignee) {
  85. const catchCall = _core.types.callExpression(this.contextProperty("catch", true), [_core.types.cloneNode(tryLoc)]);
  86. if (assignee) {
  87. this.emitAssign(assignee, catchCall);
  88. } else {
  89. this.emit(catchCall);
  90. }
  91. }
  92. jump(toLoc) {
  93. this.emitAssign(this.contextProperty("next"), toLoc);
  94. this.emit(_core.types.breakStatement());
  95. }
  96. jumpIf(test, toLoc) {
  97. this.emit(_core.types.ifStatement(test, _core.types.blockStatement([this.assign(this.contextProperty("next"), toLoc), _core.types.breakStatement()])));
  98. }
  99. jumpIfNot(test, toLoc) {
  100. let negatedTest;
  101. if (_core.types.isUnaryExpression(test) && test.operator === "!") {
  102. negatedTest = test.argument;
  103. } else {
  104. negatedTest = _core.types.unaryExpression("!", test);
  105. }
  106. this.emit(_core.types.ifStatement(negatedTest, _core.types.blockStatement([this.assign(this.contextProperty("next"), toLoc), _core.types.breakStatement()])));
  107. }
  108. makeTempVar() {
  109. return this.contextProperty("t" + this.nextTempId++);
  110. }
  111. getContextFunction(id) {
  112. return _core.types.functionExpression(id || null, [this.getContextId()], _core.types.blockStatement([this.getDispatchLoop()]), false, false);
  113. }
  114. getDispatchLoop() {
  115. const self = this;
  116. const cases = [];
  117. let current;
  118. let alreadyEnded = false;
  119. self.listing.forEach(function (stmt, i) {
  120. if (self.marked.hasOwnProperty(i)) {
  121. cases.push(_core.types.switchCase(_core.types.numericLiteral(i), current = []));
  122. alreadyEnded = false;
  123. }
  124. if (!alreadyEnded) {
  125. current.push(stmt);
  126. if (_core.types.isCompletionStatement(stmt)) alreadyEnded = true;
  127. }
  128. });
  129. this.finalLoc.value = this.listing.length;
  130. cases.push(_core.types.switchCase(this.finalLoc, []), _core.types.switchCase(_core.types.stringLiteral("end"), [_core.types.returnStatement(_core.types.callExpression(this.contextProperty("stop"), []))]));
  131. return _core.types.whileStatement(_core.types.numericLiteral(1), _core.types.switchStatement(_core.types.assignmentExpression("=", this.contextProperty("prev"), this.contextProperty("next")), cases));
  132. }
  133. getTryLocsList() {
  134. if (this.tryEntries.length === 0) {
  135. return null;
  136. }
  137. let lastLocValue = 0;
  138. return _core.types.arrayExpression(this.tryEntries.map(function (tryEntry) {
  139. const thisLocValue = tryEntry.firstLoc.value;
  140. _assert.ok(thisLocValue >= lastLocValue, "try entries out of order");
  141. lastLocValue = thisLocValue;
  142. const ce = tryEntry.catchEntry;
  143. const fe = tryEntry.finallyEntry;
  144. const locs = [tryEntry.firstLoc, ce ? ce.firstLoc : null];
  145. if (fe) {
  146. locs[2] = fe.firstLoc;
  147. locs[3] = fe.afterLoc;
  148. }
  149. return _core.types.arrayExpression(locs.map(loc => loc && _core.types.cloneNode(loc)));
  150. }));
  151. }
  152. explode(path, ignoreResult) {
  153. const node = path.node;
  154. const self = this;
  155. if (_core.types.isDeclaration(node)) throw getDeclError(node);
  156. if (path.isStatement()) return self.explodeStatement(path);
  157. if (path.isExpression()) return self.explodeExpression(path, ignoreResult);
  158. switch (node.type) {
  159. case "VariableDeclarator":
  160. throw getDeclError(node);
  161. case "ObjectProperty":
  162. case "SwitchCase":
  163. case "CatchClause":
  164. throw new Error(node.type + " nodes should be handled by their parents");
  165. default:
  166. throw new Error("unknown Node of type " + JSON.stringify(node.type));
  167. }
  168. }
  169. explodeStatement(path, labelId = null) {
  170. const stmt = path.node;
  171. const self = this;
  172. let before, after, head;
  173. if (path.isBlockStatement()) {
  174. path.get("body").forEach(function (path) {
  175. self.explodeStatement(path);
  176. });
  177. return;
  178. }
  179. if (!meta.containsLeap(stmt)) {
  180. self.emit(stmt);
  181. return;
  182. }
  183. switch (path.type) {
  184. case "ExpressionStatement":
  185. self.explodeExpression(path.get("expression"), true);
  186. break;
  187. case "LabeledStatement":
  188. after = this.loc();
  189. self.leapManager.withEntry(new leap.LabeledEntry(after, path.node.label), function () {
  190. self.explodeStatement(path.get("body"), path.node.label);
  191. });
  192. self.mark(after);
  193. break;
  194. case "WhileStatement":
  195. before = this.loc();
  196. after = this.loc();
  197. self.mark(before);
  198. self.jumpIfNot(self.explodeExpression(path.get("test")), after);
  199. self.leapManager.withEntry(new leap.LoopEntry(after, before, labelId), function () {
  200. self.explodeStatement(path.get("body"));
  201. });
  202. self.jump(before);
  203. self.mark(after);
  204. break;
  205. case "DoWhileStatement":
  206. const first = this.loc();
  207. const test = this.loc();
  208. after = this.loc();
  209. self.mark(first);
  210. self.leapManager.withEntry(new leap.LoopEntry(after, test, labelId), function () {
  211. self.explode(path.get("body"));
  212. });
  213. self.mark(test);
  214. self.jumpIf(self.explodeExpression(path.get("test")), first);
  215. self.mark(after);
  216. break;
  217. case "ForStatement":
  218. head = this.loc();
  219. const update = this.loc();
  220. after = this.loc();
  221. if (path.node.init) {
  222. self.explode(path.get("init"), true);
  223. }
  224. self.mark(head);
  225. if (path.node.test) {
  226. self.jumpIfNot(self.explodeExpression(path.get("test")), after);
  227. } else {}
  228. self.leapManager.withEntry(new leap.LoopEntry(after, update, labelId), function () {
  229. self.explodeStatement(path.get("body"));
  230. });
  231. self.mark(update);
  232. if (path.node.update) {
  233. self.explode(path.get("update"), true);
  234. }
  235. self.jump(head);
  236. self.mark(after);
  237. break;
  238. case "TypeCastExpression":
  239. return self.explodeExpression(path.get("expression"));
  240. case "ForInStatement":
  241. head = this.loc();
  242. after = this.loc();
  243. const keyIterNextFn = self.makeTempVar();
  244. self.emitAssign(keyIterNextFn, _core.types.callExpression(util.runtimeProperty("keys"), [self.explodeExpression(path.get("right"))]));
  245. self.mark(head);
  246. const keyInfoTmpVar = self.makeTempVar();
  247. self.jumpIf(_core.types.memberExpression(_core.types.assignmentExpression("=", keyInfoTmpVar, _core.types.callExpression(_core.types.cloneNode(keyIterNextFn), [])), _core.types.identifier("done"), false), after);
  248. self.emitAssign(path.node.left, _core.types.memberExpression(_core.types.cloneNode(keyInfoTmpVar), _core.types.identifier("value"), false));
  249. self.leapManager.withEntry(new leap.LoopEntry(after, head, labelId), function () {
  250. self.explodeStatement(path.get("body"));
  251. });
  252. self.jump(head);
  253. self.mark(after);
  254. break;
  255. case "BreakStatement":
  256. self.emitAbruptCompletion({
  257. type: "break",
  258. target: self.leapManager.getBreakLoc(path.node.label)
  259. });
  260. break;
  261. case "ContinueStatement":
  262. self.emitAbruptCompletion({
  263. type: "continue",
  264. target: self.leapManager.getContinueLoc(path.node.label)
  265. });
  266. break;
  267. case "SwitchStatement":
  268. const disc = self.emitAssign(self.makeTempVar(), self.explodeExpression(path.get("discriminant")));
  269. after = this.loc();
  270. const defaultLoc = this.loc();
  271. let condition = defaultLoc;
  272. const caseLocs = [];
  273. const cases = path.node.cases || [];
  274. for (let i = cases.length - 1; i >= 0; --i) {
  275. const c = cases[i];
  276. if (c.test) {
  277. condition = _core.types.conditionalExpression(_core.types.binaryExpression("===", _core.types.cloneNode(disc), c.test), caseLocs[i] = this.loc(), condition);
  278. } else {
  279. caseLocs[i] = defaultLoc;
  280. }
  281. }
  282. const discriminant = path.get("discriminant");
  283. util.replaceWithOrRemove(discriminant, condition);
  284. self.jump(self.explodeExpression(discriminant));
  285. self.leapManager.withEntry(new leap.SwitchEntry(after), function () {
  286. path.get("cases").forEach(function (casePath) {
  287. const i = casePath.key;
  288. self.mark(caseLocs[i]);
  289. casePath.get("consequent").forEach(function (path) {
  290. self.explodeStatement(path);
  291. });
  292. });
  293. });
  294. self.mark(after);
  295. if (defaultLoc.value === PENDING_LOCATION) {
  296. self.mark(defaultLoc);
  297. _assert.strictEqual(after.value, defaultLoc.value);
  298. }
  299. break;
  300. case "IfStatement":
  301. const elseLoc = path.node.alternate && this.loc();
  302. after = this.loc();
  303. self.jumpIfNot(self.explodeExpression(path.get("test")), elseLoc || after);
  304. self.explodeStatement(path.get("consequent"));
  305. if (elseLoc) {
  306. self.jump(after);
  307. self.mark(elseLoc);
  308. self.explodeStatement(path.get("alternate"));
  309. }
  310. self.mark(after);
  311. break;
  312. case "ReturnStatement":
  313. self.emitAbruptCompletion({
  314. type: "return",
  315. value: self.explodeExpression(path.get("argument"))
  316. });
  317. break;
  318. case "WithStatement":
  319. throw new Error("WithStatement not supported in generator functions.");
  320. case "TryStatement":
  321. after = this.loc();
  322. const handler = path.node.handler;
  323. const catchLoc = handler && this.loc();
  324. const catchEntry = catchLoc && new leap.CatchEntry(catchLoc, handler.param);
  325. const finallyLoc = path.node.finalizer && this.loc();
  326. const finallyEntry = finallyLoc && new leap.FinallyEntry(finallyLoc, after);
  327. const tryEntry = new leap.TryEntry(self.getUnmarkedCurrentLoc(), catchEntry, finallyEntry);
  328. self.tryEntries.push(tryEntry);
  329. self.updateContextPrevLoc(tryEntry.firstLoc);
  330. self.leapManager.withEntry(tryEntry, function () {
  331. self.explodeStatement(path.get("block"));
  332. if (catchLoc) {
  333. if (finallyLoc) {
  334. self.jump(finallyLoc);
  335. } else {
  336. self.jump(after);
  337. }
  338. self.updateContextPrevLoc(self.mark(catchLoc));
  339. const bodyPath = path.get("handler.body");
  340. const safeParam = self.makeTempVar();
  341. self.clearPendingException(tryEntry.firstLoc, safeParam);
  342. bodyPath.traverse(catchParamVisitor, {
  343. getSafeParam: () => _core.types.cloneNode(safeParam),
  344. catchParamName: handler.param.name
  345. });
  346. self.leapManager.withEntry(catchEntry, function () {
  347. self.explodeStatement(bodyPath);
  348. });
  349. }
  350. if (finallyLoc) {
  351. self.updateContextPrevLoc(self.mark(finallyLoc));
  352. self.leapManager.withEntry(finallyEntry, function () {
  353. self.explodeStatement(path.get("finalizer"));
  354. });
  355. self.emit(_core.types.returnStatement(_core.types.callExpression(self.contextProperty("finish"), [finallyEntry.firstLoc])));
  356. }
  357. });
  358. self.mark(after);
  359. break;
  360. case "ThrowStatement":
  361. self.emit(_core.types.throwStatement(self.explodeExpression(path.get("argument"))));
  362. break;
  363. case "ClassDeclaration":
  364. self.emit(self.explodeClass(path));
  365. break;
  366. default:
  367. throw new Error("unknown Statement of type " + JSON.stringify(stmt.type));
  368. }
  369. }
  370. emitAbruptCompletion(record) {
  371. _assert.notStrictEqual(record.type, "normal", "normal completions are not abrupt");
  372. const abruptArgs = [_core.types.stringLiteral(record.type)];
  373. if (record.type === "break" || record.type === "continue") {
  374. abruptArgs[1] = this.insertedLocs.has(record.target) ? record.target : _core.types.cloneNode(record.target);
  375. } else if (record.type === "return" || record.type === "throw") {
  376. if (record.value) {
  377. abruptArgs[1] = _core.types.cloneNode(record.value);
  378. }
  379. }
  380. this.emit(_core.types.returnStatement(_core.types.callExpression(this.contextProperty("abrupt"), abruptArgs)));
  381. }
  382. getUnmarkedCurrentLoc() {
  383. return _core.types.numericLiteral(this.listing.length);
  384. }
  385. updateContextPrevLoc(loc) {
  386. if (loc) {
  387. if (loc.value === PENDING_LOCATION) {
  388. loc.value = this.listing.length;
  389. } else {
  390. _assert.strictEqual(loc.value, this.listing.length);
  391. }
  392. } else {
  393. loc = this.getUnmarkedCurrentLoc();
  394. }
  395. this.emitAssign(this.contextProperty("prev"), loc);
  396. }
  397. explodeViaTempVar(tempVar, childPath, hasLeapingChildren, ignoreChildResult) {
  398. _assert.ok(!ignoreChildResult || !tempVar, "Ignoring the result of a child expression but forcing it to " + "be assigned to a temporary variable?");
  399. let result = this.explodeExpression(childPath, ignoreChildResult);
  400. if (ignoreChildResult) {} else if (tempVar || hasLeapingChildren && !_core.types.isLiteral(result)) {
  401. result = this.emitAssign(tempVar || this.makeTempVar(), result);
  402. }
  403. return result;
  404. }
  405. explodeExpression(path, ignoreResult) {
  406. const expr = path.node;
  407. if (!expr) {
  408. return expr;
  409. }
  410. const self = this;
  411. let result;
  412. let after;
  413. function finish(expr) {
  414. if (ignoreResult) {
  415. self.emit(expr);
  416. }
  417. return expr;
  418. }
  419. if (!meta.containsLeap(expr)) {
  420. return finish(expr);
  421. }
  422. const hasLeapingChildren = meta.containsLeap.onlyChildren(expr);
  423. switch (path.type) {
  424. case "MemberExpression":
  425. return finish(_core.types.memberExpression(self.explodeExpression(path.get("object")), path.node.computed ? self.explodeViaTempVar(null, path.get("property"), hasLeapingChildren) : path.node.property, path.node.computed));
  426. case "CallExpression":
  427. const calleePath = path.get("callee");
  428. const argsPath = path.get("arguments");
  429. let newCallee;
  430. let newArgs;
  431. const hasLeapingArgs = argsPath.some(argPath => meta.containsLeap(argPath.node));
  432. let injectFirstArg = null;
  433. if (_core.types.isMemberExpression(calleePath.node)) {
  434. if (hasLeapingArgs) {
  435. const newObject = self.explodeViaTempVar(self.makeTempVar(), calleePath.get("object"), hasLeapingChildren);
  436. const newProperty = calleePath.node.computed ? self.explodeViaTempVar(null, calleePath.get("property"), hasLeapingChildren) : calleePath.node.property;
  437. injectFirstArg = newObject;
  438. newCallee = _core.types.memberExpression(_core.types.memberExpression(_core.types.cloneNode(newObject), newProperty, calleePath.node.computed), _core.types.identifier("call"), false);
  439. } else {
  440. newCallee = self.explodeExpression(calleePath);
  441. }
  442. } else {
  443. newCallee = self.explodeViaTempVar(null, calleePath, hasLeapingChildren);
  444. if (_core.types.isMemberExpression(newCallee)) {
  445. newCallee = _core.types.sequenceExpression([_core.types.numericLiteral(0), _core.types.cloneNode(newCallee)]);
  446. }
  447. }
  448. if (hasLeapingArgs) {
  449. newArgs = argsPath.map(argPath => self.explodeViaTempVar(null, argPath, hasLeapingChildren));
  450. if (injectFirstArg) newArgs.unshift(injectFirstArg);
  451. newArgs = newArgs.map(arg => _core.types.cloneNode(arg));
  452. } else {
  453. newArgs = path.node.arguments;
  454. }
  455. return finish(_core.types.callExpression(newCallee, newArgs));
  456. case "NewExpression":
  457. return finish(_core.types.newExpression(self.explodeViaTempVar(null, path.get("callee"), hasLeapingChildren), path.get("arguments").map(function (argPath) {
  458. return self.explodeViaTempVar(null, argPath, hasLeapingChildren);
  459. })));
  460. case "ObjectExpression":
  461. return finish(_core.types.objectExpression(path.get("properties").map(function (propPath) {
  462. if (propPath.isObjectProperty()) {
  463. return _core.types.objectProperty(propPath.node.key, self.explodeViaTempVar(null, propPath.get("value"), hasLeapingChildren), propPath.node.computed);
  464. } else {
  465. return propPath.node;
  466. }
  467. })));
  468. case "ArrayExpression":
  469. return finish(_core.types.arrayExpression(path.get("elements").map(function (elemPath) {
  470. if (!elemPath.node) {
  471. return null;
  472. }
  473. if (elemPath.isSpreadElement()) {
  474. return _core.types.spreadElement(self.explodeViaTempVar(null, elemPath.get("argument"), hasLeapingChildren));
  475. } else {
  476. return self.explodeViaTempVar(null, elemPath, hasLeapingChildren);
  477. }
  478. })));
  479. case "SequenceExpression":
  480. const lastIndex = path.node.expressions.length - 1;
  481. path.get("expressions").forEach(function (exprPath) {
  482. if (exprPath.key === lastIndex) {
  483. result = self.explodeExpression(exprPath, ignoreResult);
  484. } else {
  485. self.explodeExpression(exprPath, true);
  486. }
  487. });
  488. return result;
  489. case "LogicalExpression":
  490. after = this.loc();
  491. if (!ignoreResult) {
  492. result = self.makeTempVar();
  493. }
  494. const left = self.explodeViaTempVar(result, path.get("left"), hasLeapingChildren);
  495. if (path.node.operator === "&&") {
  496. self.jumpIfNot(left, after);
  497. } else {
  498. _assert.strictEqual(path.node.operator, "||");
  499. self.jumpIf(left, after);
  500. }
  501. self.explodeViaTempVar(result, path.get("right"), hasLeapingChildren, ignoreResult);
  502. self.mark(after);
  503. return result;
  504. case "ConditionalExpression":
  505. const elseLoc = this.loc();
  506. after = this.loc();
  507. const test = self.explodeExpression(path.get("test"));
  508. self.jumpIfNot(test, elseLoc);
  509. if (!ignoreResult) {
  510. result = self.makeTempVar();
  511. }
  512. self.explodeViaTempVar(result, path.get("consequent"), hasLeapingChildren, ignoreResult);
  513. self.jump(after);
  514. self.mark(elseLoc);
  515. self.explodeViaTempVar(result, path.get("alternate"), hasLeapingChildren, ignoreResult);
  516. self.mark(after);
  517. return result;
  518. case "UnaryExpression":
  519. return finish(_core.types.unaryExpression(path.node.operator, self.explodeExpression(path.get("argument")), !!path.node.prefix));
  520. case "BinaryExpression":
  521. return finish(_core.types.binaryExpression(path.node.operator, self.explodeViaTempVar(null, path.get("left"), hasLeapingChildren), self.explodeViaTempVar(null, path.get("right"), hasLeapingChildren)));
  522. case "AssignmentExpression":
  523. if (path.node.operator === "=") {
  524. return finish(_core.types.assignmentExpression(path.node.operator, self.explodeExpression(path.get("left")), self.explodeExpression(path.get("right"))));
  525. }
  526. const lhs = self.explodeExpression(path.get("left"));
  527. const temp = self.emitAssign(self.makeTempVar(), lhs);
  528. return finish(_core.types.assignmentExpression("=", _core.types.cloneNode(lhs), _core.types.assignmentExpression(path.node.operator, _core.types.cloneNode(temp), self.explodeExpression(path.get("right")))));
  529. case "UpdateExpression":
  530. return finish(_core.types.updateExpression(path.node.operator, self.explodeExpression(path.get("argument")), path.node.prefix));
  531. case "YieldExpression":
  532. after = this.loc();
  533. const arg = path.node.argument && self.explodeExpression(path.get("argument"));
  534. if (arg && path.node.delegate) {
  535. const result = self.makeTempVar();
  536. const ret = _core.types.returnStatement(_core.types.callExpression(self.contextProperty("delegateYield"), [arg, _core.types.stringLiteral(result.property.name), after]));
  537. ret.loc = expr.loc;
  538. self.emit(ret);
  539. self.mark(after);
  540. return result;
  541. }
  542. self.emitAssign(self.contextProperty("next"), after);
  543. const ret = _core.types.returnStatement(_core.types.cloneNode(arg) || null);
  544. ret.loc = expr.loc;
  545. self.emit(ret);
  546. self.mark(after);
  547. return self.contextProperty("sent");
  548. case "ClassExpression":
  549. return finish(self.explodeClass(path));
  550. default:
  551. throw new Error("unknown Expression of type " + JSON.stringify(expr.type));
  552. }
  553. }
  554. explodeClass(path) {
  555. const explodingChildren = [];
  556. if (path.node.superClass) {
  557. explodingChildren.push(path.get("superClass"));
  558. }
  559. path.get("body.body").forEach(member => {
  560. if (member.node.computed) {
  561. explodingChildren.push(member.get("key"));
  562. }
  563. });
  564. const hasLeapingChildren = explodingChildren.some(child => meta.containsLeap(child));
  565. for (let i = 0; i < explodingChildren.length; i++) {
  566. const child = explodingChildren[i];
  567. const isLast = i === explodingChildren.length - 1;
  568. if (isLast) {
  569. child.replaceWith(this.explodeExpression(child));
  570. } else {
  571. child.replaceWith(this.explodeViaTempVar(null, child, hasLeapingChildren));
  572. }
  573. }
  574. return path.node;
  575. }
  576. }
  577. exports.Emitter = Emitter;
  578. //# sourceMappingURL=emit.js.map