tests.es6.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  1. /**
  2. * Copyright (c) 2014, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * https://raw.github.com/facebook/regenerator/master/LICENSE file. An
  7. * additional grant of patent rights can be found in the PATENTS file in
  8. * the same directory.
  9. */
  10. var assert = require("assert");
  11. var runningInTranslation = /\.wrap\(/.test(function*(){});
  12. var iteratorSymbol = typeof Symbol === "function"
  13. && Symbol.iterator
  14. || "@@iterator";
  15. function check(g, yields, returnValue) {
  16. for (var i = 0; i < yields.length; ++i) {
  17. var info = g.next(i);
  18. assert.deepEqual(info.value, yields[i]);
  19. assert.strictEqual(info.done, false);
  20. }
  21. assert.deepEqual(
  22. i > 0 ? g.next(i) : g.next(),
  23. { value: returnValue, done: true }
  24. );
  25. }
  26. // A version of `throw` whose behavior can't be statically analyzed.
  27. // Useful for testing dynamic exception dispatching.
  28. function raise(argument) {
  29. throw argument;
  30. }
  31. function assertAlreadyFinished(generator) {
  32. assert.deepEqual(generator.next(), {
  33. value: void 0,
  34. done: true
  35. });
  36. }
  37. describe("regeneratorRuntime", function() {
  38. it("should be defined globally", function() {
  39. var global = Function("return this")();
  40. assert.ok("regeneratorRuntime" in global);
  41. assert.strictEqual(global.regeneratorRuntime, regeneratorRuntime);
  42. });
  43. it("should have a .wrap method", function() {
  44. assert.strictEqual(typeof regeneratorRuntime.wrap, "function");
  45. });
  46. it("should have a .mark method", function() {
  47. assert.strictEqual(typeof regeneratorRuntime.mark, "function");
  48. });
  49. it("should be the object name returned by util.runtimeProperty", function() {
  50. assert.strictEqual(
  51. require("../lib/util").runtimeProperty("foo").object.name,
  52. "regeneratorRuntime"
  53. );
  54. });
  55. });
  56. (runningInTranslation ? describe : xdescribe)("@@iterator", function() {
  57. it("is defined on Generator.prototype and returns this", function() {
  58. function *gen(){}
  59. var iterator = gen();
  60. assert.ok(!iterator.hasOwnProperty(iteratorSymbol));
  61. assert.ok(!Object.getPrototypeOf(iterator).hasOwnProperty(iteratorSymbol));
  62. assert.ok(Object.getPrototypeOf(
  63. Object.getPrototypeOf(iterator)
  64. ).hasOwnProperty(iteratorSymbol));
  65. assert.strictEqual(iterator[iteratorSymbol](), iterator);
  66. });
  67. });
  68. describe("simple argument yielder", function() {
  69. it("should yield only its first argument", function() {
  70. function *gen(x) {
  71. yield x;
  72. }
  73. check(gen("oyez"), ["oyez"]);
  74. check(gen("foo", "bar"), ["foo"]);
  75. });
  76. it("should support multiple yields in expression", function() {
  77. function *gen() { return (yield 0) + (yield 0); }
  78. var itr = gen();
  79. itr.next();
  80. itr.next(1);
  81. assert.equal(itr.next(2).value, 3);
  82. });
  83. });
  84. function *range(n) {
  85. for (var i = 0; i < n; ++i) {
  86. yield i;
  87. }
  88. }
  89. describe("range generator", function() {
  90. it("should yield the empty range", function() {
  91. check(range(0), []);
  92. })
  93. it("should yield the range 0..n-1", function() {
  94. check(range(5), [0, 1, 2, 3, 4]);
  95. });
  96. });
  97. describe("collatz generator", function() {
  98. function *gen(n) {
  99. var count = 0;
  100. yield n;
  101. while (n !== 1) {
  102. count += 1;
  103. if (n % 2) {
  104. yield n = n * 3 + 1;
  105. } else {
  106. yield n >>= 1;
  107. }
  108. }
  109. return count;
  110. }
  111. function collatz(n) {
  112. var result = [n];
  113. while (n !== 1) {
  114. if (n % 2) {
  115. n *= 3;
  116. n += 1;
  117. } else {
  118. n >>= 1;
  119. }
  120. result.push(n);
  121. }
  122. return result;
  123. }
  124. var seven = collatz(7);
  125. var fiftyTwo = seven.slice(seven.indexOf(52));
  126. var eightyTwo = collatz(82);
  127. it("seven", function() {
  128. check(gen(7), seven, 16);
  129. });
  130. it("fifty two", function() {
  131. check(gen(52), fiftyTwo, 11);
  132. });
  133. it("eighty two", function() {
  134. check(gen(82), eightyTwo, 110);
  135. });
  136. });
  137. describe("throw", function() {
  138. (runningInTranslation ? it : xit)("should complete generator", function() {
  139. function *gen(x) {
  140. throw 1;
  141. }
  142. var u = gen();
  143. try {
  144. u.next();
  145. } catch (err) {
  146. assert.strictEqual(err, 1);
  147. }
  148. assertAlreadyFinished(u);
  149. });
  150. });
  151. describe("try-catch generator", function() {
  152. function *usingThrow(x) {
  153. yield 0;
  154. try {
  155. yield 1;
  156. if (x % 2 === 0)
  157. throw 2;
  158. yield x;
  159. } catch (x) {
  160. yield x;
  161. }
  162. yield 3;
  163. }
  164. function *usingRaise(x) {
  165. yield 0;
  166. try {
  167. yield 1;
  168. if (x % 2 === 0)
  169. raise(2);
  170. yield x;
  171. } catch (x) {
  172. yield x;
  173. }
  174. yield 3;
  175. }
  176. it("should catch static exceptions properly", function() {
  177. check(usingThrow(4), [0, 1, 2, 3]);
  178. check(usingThrow(5), [0, 1, 5, 3]);
  179. });
  180. it("should catch dynamic exceptions properly", function() {
  181. check(usingRaise(4), [0, 1, 2, 3]);
  182. check(usingRaise(5), [0, 1, 5, 3]);
  183. });
  184. });
  185. describe("nested generators in try-catch", function() {
  186. function *gen() {
  187. try {
  188. nonExistent;
  189. } catch (e) {
  190. yield function* () {
  191. yield e;
  192. }
  193. }
  194. }
  195. it('should get a reference to the caught error', function () {
  196. var genFun2 = gen().next().value;
  197. assert.ok(regeneratorRuntime.isGeneratorFunction(genFun2));
  198. var gen2 = genFun2();
  199. var res = gen2.next();
  200. assert.ok(res.value instanceof ReferenceError);
  201. // Note that we don't do strict equality over the message because it varies
  202. // across browsers (if we ever want to run tests in browsers).
  203. assert.ok(res.value.message.match(/nonExistent/));
  204. });
  205. });
  206. describe("try-finally generator", function() {
  207. function *usingThrow(condition) {
  208. yield 0;
  209. try {
  210. yield 1;
  211. throw 2;
  212. yield 3;
  213. } finally {
  214. if (condition) {
  215. yield 4;
  216. return 5;
  217. }
  218. yield 6;
  219. return 7;
  220. }
  221. }
  222. function *usingRaise(condition) {
  223. yield 0;
  224. try {
  225. yield 1;
  226. raise(2);
  227. yield 3;
  228. } finally {
  229. if (condition) {
  230. yield 4;
  231. return 5;
  232. }
  233. yield 6;
  234. return 7;
  235. }
  236. }
  237. function *usingAbrupt(abruptType, finallyAbruptType) {
  238. yield 0;
  239. for (;;) {
  240. try {
  241. yield 1;
  242. if (abruptType === "return") {
  243. return 2;
  244. } else if (abruptType === "break") {
  245. break;
  246. } else if (abruptType === "continue") {
  247. abruptType = "return";
  248. continue;
  249. }
  250. }
  251. finally {
  252. yield 3;
  253. if (finallyAbruptType === "return") {
  254. return 4;
  255. } else if (finallyAbruptType === "break") {
  256. break;
  257. } else if (finallyAbruptType === "continue") {
  258. finallyAbruptType = null;
  259. continue;
  260. }
  261. }
  262. }
  263. return 5;
  264. }
  265. it("should honor return", function() {
  266. check(usingAbrupt("return", null), [0, 1, 3], 2);
  267. });
  268. it("should honor break", function() {
  269. check(usingAbrupt("break", null), [0, 1, 3], 5);
  270. });
  271. it("should honor continue", function() {
  272. check(usingAbrupt("continue", null), [0, 1, 3, 1, 3], 2);
  273. });
  274. it("should override abrupt with return", function() {
  275. check(usingAbrupt("return", "return"), [0, 1, 3], 4);
  276. check(usingAbrupt("break", "return"), [0, 1, 3], 4);
  277. check(usingAbrupt("continue", "return"), [0, 1, 3], 4);
  278. });
  279. it("should override abrupt with break", function() {
  280. check(usingAbrupt("return", "break"), [0, 1, 3], 5);
  281. check(usingAbrupt("break", "break"), [0, 1, 3], 5);
  282. check(usingAbrupt("continue", "break"), [0, 1, 3], 5);
  283. });
  284. it("should override abrupt with continue", function() {
  285. check(usingAbrupt("return", "continue"), [0, 1, 3, 1, 3], 2);
  286. check(usingAbrupt("break", "continue"), [0, 1, 3, 1, 3], 5);
  287. check(usingAbrupt("continue", "continue"), [0, 1, 3, 1, 3], 2);
  288. });
  289. it("should execute finally blocks statically", function() {
  290. check(usingThrow(true), [0, 1, 4], 5);
  291. check(usingThrow(false), [0, 1, 6], 7);
  292. });
  293. it("should execute finally blocks dynamically", function() {
  294. check(usingRaise(true), [0, 1, 4], 5);
  295. check(usingRaise(false), [0, 1, 6], 7);
  296. });
  297. it("should execute finally blocks before throwing", function() {
  298. var uncaughtError = new Error("uncaught");
  299. function *uncaught(condition) {
  300. try {
  301. yield 0;
  302. if (condition) {
  303. yield 1;
  304. raise(uncaughtError);
  305. }
  306. yield 2;
  307. } finally {
  308. yield 3;
  309. }
  310. yield 4;
  311. }
  312. check(uncaught(false), [0, 2, 3, 4]);
  313. var u = uncaught(true);
  314. assert.deepEqual(u.next(), { value: 0, done: false });
  315. assert.deepEqual(u.next(), { value: 1, done: false });
  316. assert.deepEqual(u.next(), { value: 3, done: false });
  317. try {
  318. u.next();
  319. assert.ok(false, "should have thrown an exception");
  320. } catch (err) {
  321. assert.strictEqual(err, uncaughtError);
  322. }
  323. });
  324. it("should throw correct error when finally contains catch", function() {
  325. var right = new Error("right");
  326. var wrong = new Error("wrong");
  327. function *gen() {
  328. try {
  329. yield 0;
  330. raise(right);
  331. } finally {
  332. yield 1;
  333. try {
  334. raise(wrong);
  335. } catch (err) {
  336. assert.strictEqual(err, wrong);
  337. yield 2;
  338. }
  339. }
  340. }
  341. var g = gen();
  342. assert.deepEqual(g.next(), {
  343. value: 0,
  344. done: false
  345. });
  346. assert.deepEqual(g.next(), {
  347. value: 1,
  348. done: false
  349. });
  350. assert.deepEqual(g.next(), {
  351. value: 2,
  352. done: false
  353. });
  354. try {
  355. g.next();
  356. assert.ok(false, "should have thrown an exception");
  357. } catch (err) {
  358. assert.strictEqual(err, right);
  359. }
  360. });
  361. it("should run finally after break within try", function() {
  362. function *gen() {
  363. try {
  364. yield 0;
  365. while (true) {
  366. yield 1;
  367. break;
  368. }
  369. } finally {
  370. yield 2;
  371. }
  372. yield 3;
  373. }
  374. check(gen(), [0, 1, 2, 3]);
  375. });
  376. });
  377. describe("try-catch-finally generator", function() {
  378. function *usingThrow() {
  379. yield 0;
  380. try {
  381. try {
  382. yield 1;
  383. throw 2;
  384. yield 3;
  385. } catch (x) {
  386. throw yield x;
  387. } finally {
  388. yield 5;
  389. }
  390. } catch (thrown) {
  391. yield thrown;
  392. }
  393. yield 6;
  394. }
  395. function *usingRaise() {
  396. yield 0;
  397. try {
  398. try {
  399. yield 1;
  400. raise(2);
  401. yield 3;
  402. } catch (x) {
  403. throw yield x;
  404. } finally {
  405. yield 5;
  406. }
  407. } catch (thrown) {
  408. yield thrown;
  409. }
  410. yield 6;
  411. }
  412. it("should statically catch and then finalize", function() {
  413. check(usingThrow(), [0, 1, 2, 5, 3, 6]);
  414. });
  415. it("should dynamically catch and then finalize", function() {
  416. check(usingRaise(), [0, 1, 2, 5, 3, 6]);
  417. });
  418. it("should execute catch and finally blocks at most once", function() {
  419. var error = new Error();
  420. function *gen() {
  421. try {
  422. switch (1) {
  423. case 1:
  424. yield "a";
  425. break;
  426. default:
  427. break;
  428. }
  429. throw error;
  430. } catch (e) {
  431. assert.strictEqual(e, error);
  432. yield "b";
  433. do {
  434. do {
  435. yield "c";
  436. break;
  437. } while (false);
  438. yield "d";
  439. break;
  440. } while (false);
  441. yield "e";
  442. } finally {
  443. yield "f";
  444. }
  445. }
  446. check(gen(), ["a", "b", "c", "d", "e", "f"]);
  447. });
  448. it("should handle backwards jumps in labeled loops", function() {
  449. function *gen() {
  450. var firstTime = true;
  451. outer:
  452. while (true) {
  453. yield 0;
  454. try {
  455. while (true) {
  456. yield 1;
  457. if (firstTime) {
  458. firstTime = false;
  459. yield 2;
  460. continue outer;
  461. } else {
  462. yield 3;
  463. break;
  464. }
  465. }
  466. yield 4;
  467. break;
  468. } finally {
  469. yield 5;
  470. }
  471. yield 6;
  472. }
  473. yield 7;
  474. }
  475. check(gen(), [0, 1, 2, 5, 0, 1, 3, 4, 5, 7]);
  476. });
  477. it("should handle loop continue statements properly", function() {
  478. var error = new Error("thrown");
  479. var markers = [];
  480. function *gen() {
  481. var c = 2;
  482. while (c > 0) {
  483. try {
  484. markers.push("try");
  485. yield c;
  486. } catch (e) {
  487. assert.strictEqual(e, error);
  488. markers.push("catch");
  489. continue;
  490. } finally {
  491. markers.push("finally");
  492. }
  493. markers.push("decrement");
  494. --c;
  495. }
  496. }
  497. var g = gen();
  498. assert.deepEqual(g.next(), { value: 2, done: false });
  499. assert.deepEqual(g.throw(error), { value: 2, done: false });
  500. assert.deepEqual(g.next(), { value: 1, done: false });
  501. assert.deepEqual(g.next(), { value: void 0, done: true });
  502. assert.deepEqual(markers, [
  503. "try",
  504. "catch",
  505. "finally",
  506. "try",
  507. "finally",
  508. "decrement",
  509. "try",
  510. "finally",
  511. "decrement"
  512. ]);
  513. });
  514. });
  515. describe("dynamic exception", function() {
  516. function *gen(x, fname) {
  517. try {
  518. return fns[fname](x);
  519. } catch (thrown) {
  520. yield thrown;
  521. }
  522. }
  523. var fns = {
  524. f: function(x) {
  525. throw x;
  526. },
  527. g: function(x) {
  528. return x;
  529. }
  530. };
  531. it("should be dispatched correctly", function() {
  532. check(gen("asdf", "f"), ["asdf"]);
  533. check(gen("asdf", "g"), [], "asdf");
  534. });
  535. });
  536. describe("nested finally blocks", function() {
  537. function *usingThrow() {
  538. try {
  539. try {
  540. try {
  541. throw "thrown";
  542. } finally {
  543. yield 1;
  544. }
  545. } catch (thrown) {
  546. yield thrown;
  547. } finally {
  548. yield 2;
  549. }
  550. } finally {
  551. yield 3;
  552. }
  553. }
  554. function *usingRaise() {
  555. try {
  556. try {
  557. try {
  558. raise("thrown");
  559. } finally {
  560. yield 1;
  561. }
  562. } catch (thrown) {
  563. yield thrown;
  564. } finally {
  565. yield 2;
  566. }
  567. } finally {
  568. yield 3;
  569. }
  570. }
  571. it("should statically execute in order", function() {
  572. check(usingThrow(), [1, "thrown", 2, 3]);
  573. });
  574. it("should dynamically execute in order", function() {
  575. check(usingRaise(), [1, "thrown", 2, 3]);
  576. });
  577. });
  578. describe("for-in loop generator", function() {
  579. it("should handle the simple case", function() {
  580. function *gen() {
  581. var count = 0;
  582. var obj = {foo: 1, bar: 2};
  583. for (var key in obj) {
  584. assert(obj.hasOwnProperty(key), key + " must be own property");
  585. yield [key, obj[key]];
  586. count += 1;
  587. }
  588. return count;
  589. }
  590. check(gen(), [["foo", 1], ["bar", 2]], 2);
  591. });
  592. it("should handle break in loop", function() {
  593. function *gen(obj) {
  594. var count = 0;
  595. for (var key in (yield "why not", obj)) {
  596. if (obj.hasOwnProperty(key)) {
  597. if (key === "skip") {
  598. break;
  599. }
  600. count += 1;
  601. yield [key, obj[key]];
  602. }
  603. }
  604. return count;
  605. }
  606. check(
  607. gen({ a: 1, b: 2, skip: 3, c: 4 }),
  608. ["why not", ["a", 1], ["b", 2]],
  609. 2
  610. );
  611. });
  612. it("should handle property deletion in loop", function() {
  613. function *gen() {
  614. var count = 0;
  615. var obj = {foo: 1, bar: 2};
  616. for (var key in obj) {
  617. assert(obj.hasOwnProperty(key), key + " must be own property");
  618. yield [key, obj[key]];
  619. delete obj.bar;
  620. count += 1;
  621. }
  622. return count;
  623. }
  624. check(gen(), [["foo", 1]], 1);
  625. });
  626. it("should loop over inherited properties", function() {
  627. function *gen() {
  628. var count = 0;
  629. function Foo() {
  630. this.baz = 1
  631. }
  632. Foo.prototype.bar = 2;
  633. var foo = new Foo();
  634. for (var key in foo) {
  635. yield [key, foo[key]];
  636. count += 1;
  637. }
  638. return count;
  639. }
  640. check(gen(), [["baz", 1], ["bar", 2]], 2);
  641. });
  642. it("should handle risky object expressions", function() {
  643. function a(sent) {
  644. assert.strictEqual(sent, 1);
  645. a.called = true;
  646. }
  647. function b(sent) {
  648. assert.strictEqual(sent, 2);
  649. b.called = true;
  650. return { callee: b };
  651. }
  652. function *gen() {
  653. assert.ok(!a.called);
  654. assert.ok(!b.called);
  655. for (var key in a(yield 0), b(yield 1)) {
  656. assert.ok(a.called);
  657. assert.ok(b.called);
  658. assert.strictEqual(yield key, 3);
  659. }
  660. for (var key in a(1), { foo: "foo", bar: "bar" }) {
  661. yield key;
  662. }
  663. }
  664. check(gen(), [0, 1, "callee", "foo", "bar"]);
  665. });
  666. it("should allow non-Identifier left-hand expressions", function() {
  667. var obj = {};
  668. var baz = { a: 1, b: 2, c: 3 };
  669. var markers = [];
  670. function foo() {
  671. markers.push("called foo");
  672. return obj;
  673. }
  674. function *gen() {
  675. for (foo().bar in baz) {
  676. markers.push(obj.bar);
  677. yield obj.bar;
  678. }
  679. }
  680. check(gen(), ["a", "b", "c"]);
  681. assert.deepEqual(markers, [
  682. "called foo",
  683. "a",
  684. "called foo",
  685. "b",
  686. "called foo",
  687. "c"
  688. ]);
  689. });
  690. });
  691. describe("yield chain", function() {
  692. function *gen(n) {
  693. return yield yield yield yield n;
  694. }
  695. it("should have correct associativity", function() {
  696. check(gen(5), [5, 1, 2, 3], 4);
  697. check(gen("asdf"), ["asdf", 1, 2, 3], 4);
  698. });
  699. });
  700. describe("object literal generator", function() {
  701. function *gen(a, b) {
  702. yield {
  703. a: a - (yield a),
  704. b: yield b
  705. };
  706. }
  707. it("should yield the correct object", function() {
  708. check(gen(1, 2), [1, 2, { a: 0, b: 2 }]);
  709. check(gen(4, 2), [4, 2, { a: 3, b: 2 }]);
  710. });
  711. });
  712. describe("switch statement generator", function() {
  713. function *gen(a) {
  714. switch (yield a) {
  715. case (yield "x") - a:
  716. return "first case";
  717. case (yield "y") - a:
  718. return "second case";
  719. }
  720. }
  721. it("should jump to the correct cases", function() {
  722. check(gen(1), [1, "x"], "first case");
  723. check(gen(2), [2, "x", "y"], "second case");
  724. });
  725. });
  726. describe("infinite sequence generator", function() {
  727. function *gen(start, step) {
  728. step = step || 1;
  729. while (true) {
  730. yield start;
  731. start += step;
  732. }
  733. }
  734. function *limit(g, stop) {
  735. while (true) {
  736. var info = g.next();
  737. if (info.done) {
  738. return;
  739. } else if (info.value < stop) {
  740. yield info.value;
  741. } else {
  742. return;
  743. }
  744. }
  745. }
  746. it("should generate a lot of plausible values", function() {
  747. var g = gen(10, 2);
  748. assert.deepEqual(g.next(), { value: 10, done: false });
  749. assert.deepEqual(g.next(), { value: 12, done: false });
  750. assert.deepEqual(g.next(), { value: 14, done: false });
  751. assert.deepEqual(g.next(), { value: 16, done: false });
  752. var sum = 10 + 12 + 14 + 16;
  753. for (var n = 0; n < 1000; ++n) {
  754. var info = g.next();
  755. sum += info.value;
  756. assert.strictEqual(info.done, false);
  757. }
  758. assert.strictEqual(sum, 1017052);
  759. });
  760. it("should allow limiting", function() {
  761. check(limit(gen(10, 3), 20), [10, 13, 16, 19]);
  762. });
  763. });
  764. describe("generator function expression", function() {
  765. it("should behave just like a declared generator", function() {
  766. check(function *(x, y) {
  767. yield x;
  768. yield y;
  769. yield x + y;
  770. return x * y;
  771. }(3, 7), [3, 7, 10], 21);
  772. })
  773. });
  774. describe("generator reentry attempt", function() {
  775. function *gen(x) {
  776. try {
  777. (yield x).next(x);
  778. } catch (err) {
  779. yield err;
  780. }
  781. return x + 1;
  782. }
  783. it("should complain with a TypeError", function() {
  784. var g = gen(3);
  785. assert.deepEqual(g.next(), { value: 3, done: false });
  786. var complaint = g.next(g); // Sending the generator to itself.
  787. assert.ok(complaint.value instanceof Error);
  788. assert.strictEqual(
  789. complaint.value.message,
  790. "Generator is already running"
  791. );
  792. assert.deepEqual(g.next(), { value: 4, done: true });
  793. });
  794. });
  795. describe("completed generator", function() {
  796. function *gen() {
  797. return "ALL DONE";
  798. }
  799. (runningInTranslation ? it : xit)
  800. ("should refuse to resume", function() {
  801. var g = gen();
  802. assert.deepEqual(g.next(), {
  803. value: "ALL DONE", done: true
  804. });
  805. assertAlreadyFinished(g);
  806. });
  807. });
  808. describe("delegated yield", function() {
  809. it("should delegate correctly", function() {
  810. function *gen(condition) {
  811. yield 0;
  812. if (condition) {
  813. yield 1;
  814. yield* gen(false);
  815. yield 2;
  816. }
  817. yield 3;
  818. }
  819. check(gen(true), [0, 1, 0, 3, 2, 3]);
  820. check(gen(false), [0, 3]);
  821. });
  822. it("should cope with empty delegatees", function() {
  823. function *gen(condition) {
  824. if (condition) {
  825. yield 0;
  826. yield* gen(false);
  827. yield 1;
  828. }
  829. }
  830. check(gen(true), [0, 1]);
  831. check(gen(false), []);
  832. });
  833. it("should support deeper nesting", function() {
  834. function *outer(n) {
  835. yield n;
  836. yield* middle(n - 1, inner(n + 10));
  837. yield n + 1;
  838. }
  839. function *middle(n, plusTen) {
  840. yield n;
  841. yield* inner(n - 1);
  842. yield n + 1;
  843. yield* plusTen;
  844. }
  845. function *inner(n) {
  846. yield n;
  847. }
  848. check(outer(5), [5, 4, 3, 5, 15, 6]);
  849. });
  850. it("should pass sent values through", function() {
  851. function *outer(n) {
  852. yield* inner(n << 1);
  853. yield "zxcv";
  854. }
  855. function *inner(n) {
  856. return yield yield yield n;
  857. }
  858. var g = outer(3);
  859. assert.deepEqual(g.next(), { value: 6, done: false });
  860. assert.deepEqual(g.next(1), { value: 1, done: false });
  861. assert.deepEqual(g.next(2), { value: 2, done: false });
  862. assert.deepEqual(g.next(4), { value: "zxcv", done: false });
  863. assert.deepEqual(g.next(5), { value: void 0, done: true });
  864. });
  865. it("should be governed by enclosing try statements", function() {
  866. var error = new Error("thrown");
  867. function *outer(n) {
  868. try {
  869. yield 0;
  870. yield* inner(n);
  871. yield 1;
  872. } catch (err) {
  873. yield err.message;
  874. }
  875. yield 4;
  876. }
  877. function *inner(n) {
  878. while (n --> 0) {
  879. try {
  880. if (n === 3) {
  881. raise(error);
  882. }
  883. } finally {
  884. yield n;
  885. }
  886. }
  887. }
  888. check(outer(3), [0, 2, 1, 0, 1, 4]);
  889. check(outer(5), [0, 4, 3, "thrown", 4]);
  890. });
  891. it("should dispatch .thrown exceptions correctly", function() {
  892. var count = 0;
  893. function *gen() {
  894. yield* inner();
  895. try {
  896. yield* inner();
  897. } catch (err) {
  898. // pass
  899. }
  900. return yield* inner();
  901. }
  902. function *inner() {
  903. return yield count++;
  904. }
  905. var g = gen();
  906. assert.deepEqual(g.next(), {
  907. value: 0,
  908. done: false
  909. });
  910. assert.deepEqual(g.next(), {
  911. value: 1,
  912. done: false
  913. });
  914. assert.deepEqual(g.throw(new Error("lol")), {
  915. value: 2,
  916. done: false,
  917. });
  918. assert.deepEqual(g.next("sent"), {
  919. value: "sent",
  920. done: true
  921. });
  922. });
  923. it("should call .return methods of delegate iterators", function() {
  924. var throwee = new Error("argument to gen.throw");
  925. var thrownFromThrow = new Error("thrown from throw method");
  926. var thrownFromReturn = new Error("thrown from return method");
  927. function *gen(delegate) {
  928. try {
  929. return yield* delegate;
  930. } catch (err) {
  931. return err;
  932. }
  933. }
  934. function check(throwMethod, returnMethod) {
  935. var throwCalled = false;
  936. var returnCalled = false;
  937. var count = 0;
  938. var iterator = {
  939. next: function() {
  940. return { value: count++, done: false };
  941. }
  942. };
  943. iterator[iteratorSymbol] = function() {
  944. return this;
  945. };
  946. if (throwMethod) {
  947. iterator["throw"] = function() {
  948. throwCalled = true;
  949. return throwMethod.apply(this, arguments);
  950. };
  951. }
  952. if (returnMethod) {
  953. iterator["return"] = function() {
  954. returnCalled = true;
  955. return returnMethod.apply(this, arguments);
  956. };
  957. }
  958. var g = gen(iterator);
  959. assert.deepEqual(g.next(), { value: 0, done: false });
  960. assert.deepEqual(g.next(), { value: 1, done: false });
  961. assert.deepEqual(g.next(), { value: 2, done: false });
  962. assert.deepEqual(g.next(), { value: 3, done: false });
  963. assert.strictEqual(throwCalled, false);
  964. assert.strictEqual(returnCalled, false);
  965. var result = {};
  966. result.throwResult = g.throw(throwee);
  967. result.throwCalled = throwCalled;
  968. result.returnCalled = returnCalled;
  969. return result;
  970. }
  971. var checkResult = check(undefined, function() {
  972. throw thrownFromReturn;
  973. });
  974. if (runningInTranslation) {
  975. // BUG: Node v0.11 and v0.12 neglect to call .return here.
  976. assert.strictEqual(checkResult.throwResult.value, thrownFromReturn);
  977. } else {
  978. // This is the TypeError that results from trying to call the
  979. // undefined .throw method of the iterator.
  980. assert.ok(checkResult.throwResult.value instanceof TypeError);
  981. }
  982. assert.strictEqual(checkResult.throwResult.done, true);
  983. assert.strictEqual(checkResult.throwCalled, false);
  984. // BUG: Node v0.11 and v0.12 neglect to call .return here.
  985. assert.strictEqual(checkResult.returnCalled, runningInTranslation);
  986. checkResult = check(undefined, function() {
  987. return { value: "from return", done: true };
  988. });
  989. assert.notStrictEqual(checkResult.throwResult.value, throwee);
  990. // This is the TypeError that results from trying to call the
  991. // undefined .throw method of the iterator.
  992. assert.ok(checkResult.throwResult.value instanceof TypeError);
  993. assert.strictEqual(checkResult.throwResult.done, true);
  994. assert.strictEqual(checkResult.throwCalled, false);
  995. // BUG: Node v0.11 and v0.12 neglect to call .return here.
  996. assert.strictEqual(checkResult.returnCalled, runningInTranslation);
  997. var checkResult = check(function(thrown) {
  998. return { value: "from throw", done: true };
  999. }, function() {
  1000. throw thrownFromReturn;
  1001. });
  1002. assert.strictEqual(checkResult.throwResult.value, "from throw");
  1003. assert.strictEqual(checkResult.throwResult.done, true);
  1004. assert.strictEqual(checkResult.throwCalled, true);
  1005. assert.strictEqual(checkResult.returnCalled, false);
  1006. var checkResult = check(function(thrown) {
  1007. throw thrownFromThrow;
  1008. }, function() {
  1009. throw thrownFromReturn;
  1010. });
  1011. assert.strictEqual(checkResult.throwResult.value, thrownFromThrow);
  1012. assert.strictEqual(checkResult.throwResult.done, true);
  1013. assert.strictEqual(checkResult.throwCalled, true);
  1014. assert.strictEqual(checkResult.returnCalled, false);
  1015. var checkResult = check(undefined, undefined);
  1016. assert.notStrictEqual(checkResult.throwResult.value, throwee);
  1017. // This is the TypeError that results from trying to call the
  1018. // undefined .throw method of the iterator.
  1019. assert.ok(checkResult.throwResult.value instanceof TypeError);
  1020. assert.strictEqual(checkResult.throwResult.done, true);
  1021. assert.strictEqual(checkResult.throwCalled, false);
  1022. assert.strictEqual(checkResult.returnCalled, false);
  1023. });
  1024. it("should not be required to have a .return method", function() {
  1025. function *gen(delegate) {
  1026. return yield* delegate;
  1027. }
  1028. var inner = range(5);
  1029. var iterator = { next: inner.next.bind(inner) };
  1030. iterator[iteratorSymbol] = function() {
  1031. return this;
  1032. };
  1033. var g = gen(iterator);
  1034. assert.deepEqual(g.next(), { value: 0, done: false });
  1035. assert.deepEqual(g.next(), { value: 1, done: false });
  1036. assert.deepEqual(g.next(), { value: 2, done: false });
  1037. if (typeof g.return === "function") {
  1038. assert.deepEqual(g.return(-1), { value: -1, done: true });
  1039. assert.deepEqual(g.next(), { value: void 0, done: true });
  1040. }
  1041. });
  1042. (runningInTranslation ? it : xit)
  1043. ("should support any iterable argument", function() {
  1044. function *gen() {
  1045. yield 0;
  1046. yield* [
  1047. yield "one",
  1048. yield "two",
  1049. yield "three"
  1050. ];
  1051. yield 5;
  1052. }
  1053. check(gen(), [0, "one", "two", "three", 2, 3, 4, 5]);
  1054. function *string() {
  1055. return yield* "asdf";
  1056. }
  1057. check(string(), ["a", "s", "d", "f"]);
  1058. });
  1059. it("should evaluate to the return value of the delegate", function() {
  1060. function *inner() {
  1061. yield 1;
  1062. return 2;
  1063. }
  1064. function *outer(delegate) {
  1065. return yield* delegate;
  1066. }
  1067. check(outer(inner()), [1], 2);
  1068. var arrayDelegate = [3, 4];
  1069. if (!runningInTranslation) {
  1070. // Node v0.11 doesn't know how to turn arrays into iterators over
  1071. // their elements without a little help.
  1072. arrayDelegate = regeneratorRuntime.values(arrayDelegate);
  1073. }
  1074. check(outer(arrayDelegate), [3, 4], void 0); // See issue #143.
  1075. if (!runningInTranslation) {
  1076. return;
  1077. }
  1078. check(outer({
  1079. next: function() {
  1080. return { value: "oyez", done: true };
  1081. }
  1082. }), [], "oyez");
  1083. });
  1084. it("should work as a subexpression", function() {
  1085. function *inner(arg) {
  1086. return arg;
  1087. }
  1088. function *gen(delegate) {
  1089. // Unfortunately these parentheses appear to be necessary.
  1090. return 1 + (yield* delegate);
  1091. }
  1092. check(gen(inner(2)), [], 3);
  1093. check(gen(inner(3)), [], 4);
  1094. if (!runningInTranslation) {
  1095. return;
  1096. }
  1097. check(gen({
  1098. next: function() {
  1099. return { value: "foo", done: true };
  1100. }
  1101. }), [], "1foo");
  1102. });
  1103. });
  1104. describe("function declaration hoisting", function() {
  1105. it("should work even if the declarations are out of order", function() {
  1106. function *gen(n) {
  1107. yield increment(n);
  1108. function increment(x) {
  1109. return x + 1;
  1110. }
  1111. if (n % 2) {
  1112. yield halve(decrement(n));
  1113. function halve(x) {
  1114. return x >> 1;
  1115. }
  1116. function decrement(x) {
  1117. return x - 1;
  1118. }
  1119. } else {
  1120. // The behavior of function declarations nested inside conditional
  1121. // blocks is notoriously underspecified, and in V8 it appears the
  1122. // halve function is still defined when we take this branch, so
  1123. // "undefine" it for consistency with regenerator semantics.
  1124. halve = void 0;
  1125. }
  1126. yield typeof halve;
  1127. yield increment(increment(n));
  1128. }
  1129. check(gen(3), [4, 1, "function", 5]);
  1130. check(gen(4), [5, "undefined", 6]);
  1131. });
  1132. it("should work for nested generator function declarations", function() {
  1133. function *outer(n) {
  1134. yield 0;
  1135. assert.ok(regeneratorRuntime.isGeneratorFunction(inner));
  1136. return yield* inner(n);
  1137. // Note that this function declaration comes after everything else
  1138. // in the outer function, but needs to be fully available above.
  1139. function *inner(n) {
  1140. yield n - 1;
  1141. yield n;
  1142. return yield n + 1;
  1143. }
  1144. }
  1145. check(outer(2), [0, 1, 2, 3], 4);
  1146. });
  1147. it("should not interfere with function rebinding", function() {
  1148. function rebindTo(value) {
  1149. var oldValue = toBeRebound;
  1150. toBeRebound = value;
  1151. return oldValue;
  1152. }
  1153. function *toBeRebound() {
  1154. var originalValue = toBeRebound;
  1155. yield toBeRebound;
  1156. assert.strictEqual(rebindTo(42), originalValue);
  1157. yield toBeRebound;
  1158. assert.strictEqual(rebindTo("asdf"), 42);
  1159. yield toBeRebound;
  1160. }
  1161. var original = toBeRebound;
  1162. check(toBeRebound(), [original, 42, "asdf"]);
  1163. function attemptToRebind(value) {
  1164. var oldValue = safe;
  1165. safe = value;
  1166. return oldValue;
  1167. }
  1168. var safe = function *safe() {
  1169. var originalValue = safe;
  1170. yield safe;
  1171. assert.strictEqual(attemptToRebind(42), originalValue);
  1172. yield safe;
  1173. assert.strictEqual(attemptToRebind("asdf"), 42);
  1174. yield safe;
  1175. }
  1176. original = safe;
  1177. check(safe(), [safe, safe, safe]);
  1178. });
  1179. });
  1180. describe("the arguments object", function() {
  1181. it("should work in simple variadic functions", function() {
  1182. function *sum() {
  1183. var result = 0;
  1184. for (var i = 0; i < arguments.length; ++i) {
  1185. yield result += arguments[i];
  1186. }
  1187. return result;
  1188. }
  1189. check(sum(1, 2, 3), [1, 3, 6], 6);
  1190. check(sum(9, -5, 3, 0, 2), [9, 4, 7, 7, 9], 9);
  1191. });
  1192. it("should alias function parameters", function() {
  1193. function *gen(x, y) {
  1194. yield x;
  1195. ++arguments[0];
  1196. yield x;
  1197. yield y;
  1198. --arguments[1];
  1199. yield y;
  1200. var temp = y;
  1201. y = x;
  1202. x = temp;
  1203. yield x;
  1204. yield y;
  1205. }
  1206. check(gen(3, 7), [3, 4, 7, 6, 6, 4]);
  1207. check(gen(10, -5), [10, 11, -5, -6, -6, 11]);
  1208. });
  1209. it("should be shadowable by explicit declarations", function() {
  1210. function *asParameter(x, arguments) {
  1211. arguments = arguments + 1;
  1212. yield x + arguments;
  1213. }
  1214. check(asParameter(4, 5), [10]);
  1215. check(asParameter("asdf", "zxcv"), ["asdfzxcv1"]);
  1216. function *asVariable(x) {
  1217. // TODO References to arguments before the variable declaration
  1218. // seem to see the object instead of the undefined value.
  1219. var arguments = x + 1;
  1220. yield arguments;
  1221. }
  1222. check(asVariable(4), [5]);
  1223. check(asVariable("asdf"), ["asdf1"]);
  1224. });
  1225. it("should not get confused by properties", function() {
  1226. function *gen(args) {
  1227. var obj = { arguments: args };
  1228. yield obj.arguments;
  1229. obj.arguments = "oyez";
  1230. yield obj;
  1231. }
  1232. check(gen(42), [42, { arguments: "oyez" }]);
  1233. });
  1234. it("supports .callee", function() {
  1235. function *gen(doYield) {
  1236. yield 1;
  1237. if (doYield) {
  1238. yield 2;
  1239. } else {
  1240. yield 3
  1241. yield* arguments.callee(true);
  1242. yield 4
  1243. }
  1244. yield 5;
  1245. }
  1246. check(gen(false), [1, 3, 1, 2, 5, 4, 5]);
  1247. });
  1248. });
  1249. describe("catch parameter shadowing", function() {
  1250. it("should leave outer variables unmodified", function() {
  1251. function *gen(x) {
  1252. var y = x + 1;
  1253. try {
  1254. throw x + 2;
  1255. } catch (x) {
  1256. yield x;
  1257. x += 1;
  1258. yield x;
  1259. }
  1260. yield x;
  1261. try {
  1262. throw x + 3;
  1263. } catch (y) {
  1264. yield y;
  1265. y *= 2;
  1266. yield y;
  1267. }
  1268. yield y;
  1269. }
  1270. check(gen(1), [3, 4, 1, 4, 8, 2]);
  1271. check(gen(2), [4, 5, 2, 5, 10, 3]);
  1272. });
  1273. it("should not replace variables defined in inner scopes", function() {
  1274. function *gen(x) {
  1275. try {
  1276. throw x;
  1277. } catch (x) {
  1278. yield x;
  1279. yield (function(x) {
  1280. return x += 1;
  1281. }(x + 1));
  1282. yield (function() {
  1283. var x = arguments[0];
  1284. return x * 2;
  1285. }(x + 2));
  1286. yield (function() {
  1287. function notCalled(x) {
  1288. throw x;
  1289. }
  1290. x >>= 1;
  1291. return x;
  1292. }());
  1293. yield x -= 1;
  1294. }
  1295. yield x;
  1296. }
  1297. check(gen(10), [10, 12, 24, 5, 4, 10]);
  1298. check(gen(11), [11, 13, 26, 5, 4, 11]);
  1299. });
  1300. it("should allow nested catch parameters of the same name", function() {
  1301. function *gen() {
  1302. try {
  1303. raise("e1");
  1304. } catch (e) {
  1305. yield e;
  1306. try {
  1307. raise("e2");
  1308. } catch (e) {
  1309. yield e;
  1310. }
  1311. yield e;
  1312. }
  1313. }
  1314. check(gen(), ["e1", "e2", "e1"]);
  1315. });
  1316. it("should not interfere with non-referential identifiers", function() {
  1317. function *gen() {
  1318. try {
  1319. yield 1;
  1320. raise(new Error("oyez"));
  1321. yield 2;
  1322. } catch (e) {
  1323. yield 3;
  1324. e.e = "e.e";
  1325. e[e.message] = "e.oyez";
  1326. return {
  1327. e: e,
  1328. identity: function(x) {
  1329. var e = x;
  1330. return e;
  1331. }
  1332. };
  1333. }
  1334. yield 4;
  1335. }
  1336. var g = gen();
  1337. assert.deepEqual(g.next(), { value: 1, done: false });
  1338. assert.deepEqual(g.next(), { value: 3, done: false });
  1339. var info = g.next();
  1340. assert.strictEqual(info.done, true);
  1341. assert.strictEqual(info.value.e.message, "oyez");
  1342. assert.strictEqual(info.value.e.e, "e.e");
  1343. assert.strictEqual(info.value.e.oyez, "e.oyez");
  1344. assert.strictEqual(info.value.identity("same"), "same");
  1345. });
  1346. });
  1347. describe("empty while loops", function() {
  1348. it("should be preserved in generated code", function() {
  1349. function *gen(x) {
  1350. while (x) {
  1351. // empty while loop
  1352. }
  1353. do {
  1354. // empty do-while loop
  1355. } while (x);
  1356. return gen.toString();
  1357. }
  1358. var info = gen(false).next();
  1359. assert.strictEqual(info.done, true);
  1360. assert.ok(/empty while loop/.test(info.value));
  1361. assert.ok(/empty do-while loop/.test(info.value));
  1362. });
  1363. });
  1364. describe("object literals with multiple yields", function() {
  1365. it("should receive different sent values", function() {
  1366. function *gen(fn) {
  1367. return {
  1368. a: yield "a",
  1369. b: yield "b",
  1370. c: fn(yield "c", yield "d"),
  1371. d: [yield "e", yield "f"]
  1372. };
  1373. }
  1374. check(gen(function sum(x, y) {
  1375. return x + y;
  1376. }), ["a", "b", "c", "d", "e", "f"], {
  1377. a: 1,
  1378. b: 2,
  1379. c: 3 + 4,
  1380. d: [5, 6]
  1381. });
  1382. });
  1383. });
  1384. describe("generator .throw method", function() {
  1385. (runningInTranslation ? it : xit)("should complete generator", function() {
  1386. function *gen(x) {
  1387. yield 2;
  1388. throw 1;
  1389. }
  1390. var u = gen();
  1391. u.next();
  1392. try {
  1393. u.throw(2);
  1394. } catch (err) {
  1395. assert.strictEqual(err, 2);
  1396. }
  1397. assertAlreadyFinished(u);
  1398. });
  1399. it("should work after the final call to .next", function() {
  1400. function *gen() {
  1401. yield 1;
  1402. }
  1403. var g = gen();
  1404. assert.deepEqual(g.next(), { value: 1, done: false });
  1405. var exception = new Error("unhandled exception");
  1406. try {
  1407. g.throw(exception);
  1408. assert.ok(false, "should have thrown an exception");
  1409. } catch (err) {
  1410. assert.strictEqual(err, exception);
  1411. }
  1412. });
  1413. it("should immediately complete a new-born generator", function() {
  1414. var began = false;
  1415. function *gen() {
  1416. began = true;
  1417. yield 1;
  1418. }
  1419. var g = gen();
  1420. var exception = new Error("unhandled exception");
  1421. try {
  1422. g.throw(exception);
  1423. assert.ok(false, "should have thrown an exception");
  1424. } catch (err) {
  1425. assert.strictEqual(err, exception);
  1426. assert.strictEqual(began, false);
  1427. }
  1428. });
  1429. it("should not propagate errors handled inside a delegate", function() {
  1430. function *outer() {
  1431. try {
  1432. yield* inner();
  1433. } catch (err) {
  1434. return -1;
  1435. }
  1436. return 1;
  1437. }
  1438. function *inner() {
  1439. try {
  1440. yield void 0;
  1441. } catch (e) {
  1442. return;
  1443. }
  1444. }
  1445. var g = outer();
  1446. g.next();
  1447. assert.equal(g.throw(new Error('foo')).value, 1);
  1448. });
  1449. it("should propagate errors unhandled inside a delegate", function() {
  1450. function *outer() {
  1451. try {
  1452. yield* inner();
  1453. } catch (err) {
  1454. return -1;
  1455. }
  1456. return 1;
  1457. }
  1458. function *inner() {
  1459. yield void 0;
  1460. }
  1461. var g = outer();
  1462. g.next();
  1463. assert.equal(g.throw(new Error('foo')).value, -1);
  1464. });
  1465. });
  1466. describe("unqualified function calls", function() {
  1467. it("should have a global `this` object", function() {
  1468. function getThis() {
  1469. return this;
  1470. }
  1471. // This is almost certainly the global object, but there's a chance it
  1472. // might be null or undefined (in strict mode).
  1473. var unqualifiedThis = getThis();
  1474. function *invoke() {
  1475. // It seems like a bug in the ES6 spec that we have to yield an
  1476. // argument instead of just calling (yield)().
  1477. return (yield "dummy")();
  1478. }
  1479. var g = invoke();
  1480. var info = g.next();
  1481. assert.deepEqual(info, { value: "dummy", done: false });
  1482. info = g.next(getThis);
  1483. // Avoid using assert.strictEqual when the arguments might equal the
  1484. // global object, since JSON.stringify chokes on circular structures.
  1485. assert.ok(info.value === unqualifiedThis);
  1486. assert.strictEqual(info.done, true);
  1487. });
  1488. });
  1489. describe("yield* expression results", function () {
  1490. it("have correct values", function () {
  1491. function* foo() {
  1492. yield 0;
  1493. return yield* bar();
  1494. }
  1495. function* bar() {
  1496. yield 1;
  1497. return 2;
  1498. }
  1499. check(foo(), [0, 1], 2);
  1500. });
  1501. it("can be used in complex expressions", function () {
  1502. function pumpNumber(gen) {
  1503. var n = 0;
  1504. while (true) {
  1505. var res = n > 0 ? gen.next(n) : gen.next();
  1506. n = res.value;
  1507. if (res.done) {
  1508. return n;
  1509. }
  1510. }
  1511. }
  1512. function* foo() {
  1513. return (yield* bar()) + (yield* bar());
  1514. }
  1515. function* bar() {
  1516. return (yield 2) + (yield 3);
  1517. }
  1518. assert.strictEqual(pumpNumber(bar()), 5);
  1519. assert.strictEqual(pumpNumber(foo()), 10);
  1520. });
  1521. });
  1522. describe("isGeneratorFunction", function() {
  1523. it("should work for function declarations", function() {
  1524. // Do the assertions up here to make sure the generator function is
  1525. // marked at the beginning of the block the function is declared in.
  1526. assert.strictEqual(
  1527. regeneratorRuntime.isGeneratorFunction(genFun),
  1528. true
  1529. );
  1530. assert.strictEqual(
  1531. regeneratorRuntime.isGeneratorFunction(normalFun),
  1532. false
  1533. );
  1534. function normalFun() {
  1535. return 0;
  1536. }
  1537. function *genFun() {
  1538. yield 0;
  1539. }
  1540. });
  1541. it("should work for function expressions", function() {
  1542. assert.strictEqual(
  1543. regeneratorRuntime.isGeneratorFunction(function *genFun() {
  1544. yield 0;
  1545. }),
  1546. true
  1547. );
  1548. assert.strictEqual(
  1549. regeneratorRuntime.isGeneratorFunction(function normalFun() {
  1550. return 0;
  1551. }),
  1552. false
  1553. );
  1554. });
  1555. });
  1556. describe("new expressions", function() {
  1557. it("should be able to contain yield sub-expressions", function() {
  1558. function A(first, second) {
  1559. this.first = first;
  1560. this.second = second;
  1561. }
  1562. function *gen() {
  1563. return yield new (yield 0)(yield 1, yield 2);
  1564. }
  1565. var g = gen();
  1566. assert.deepEqual(g.next(), { value: 0, done: false });
  1567. assert.deepEqual(g.next(A), { value: 1, done: false });
  1568. assert.deepEqual(g.next("asdf"), { value: 2, done: false });
  1569. var info = g.next("zxcv");
  1570. assert.strictEqual(info.done, false);
  1571. assert.ok(info.value instanceof A);
  1572. assert.strictEqual(info.value.first, "asdf");
  1573. assert.strictEqual(info.value.second, "zxcv");
  1574. assert.deepEqual(g.next("qwer"), { value: "qwer", done: true });
  1575. });
  1576. });
  1577. describe("block binding", function() {
  1578. it("should translate block binding correctly", function() {
  1579. "use strict";
  1580. function *gen() {
  1581. var a$0 = 0, a$1 = 1;
  1582. let a = 3;
  1583. {
  1584. let a = 1;
  1585. yield a + a$0;
  1586. }
  1587. {
  1588. let a = 2;
  1589. yield a - 1 + a$1;
  1590. }
  1591. yield a;
  1592. }
  1593. var g = gen();
  1594. assert.deepEqual(g.next(), { value: 1, done: false });
  1595. assert.deepEqual(g.next(), { value: 2, done: false });
  1596. assert.deepEqual(g.next(), { value: 3, done: false });
  1597. assert.deepEqual(g.next(), { value: void 0, done: true });
  1598. });
  1599. it("should translate block binding with iife correctly", function() {
  1600. "use strict";
  1601. function *gen() {
  1602. let arr = [];
  1603. for (let x = 0; x < 3; x++) {
  1604. let y = x;
  1605. arr.push(function() { return y; });
  1606. }
  1607. {
  1608. let x;
  1609. while( x = arr.pop() ) {
  1610. yield x;
  1611. }
  1612. }
  1613. }
  1614. var g = gen();
  1615. assert.equal(g.next().value(), 2);
  1616. assert.equal(g.next().value(), 1);
  1617. assert.equal(g.next().value(), 0);
  1618. assert.deepEqual(g.next(), { value: void 0, done: true });
  1619. });
  1620. });
  1621. describe("newborn generators", function() {
  1622. it("should be able to yield* non-newborn generators", function() {
  1623. function *inner() {
  1624. return [yield 1, yield 2];
  1625. }
  1626. function *outer(delegate) {
  1627. return yield* delegate;
  1628. }
  1629. var n = inner();
  1630. assert.deepEqual(n.next(), {
  1631. value: 1,
  1632. done: false
  1633. });
  1634. var g = outer(n);
  1635. // I would really like to be able to pass 3 to g.next here, but V8
  1636. // ignores values sent to newborn generators, and SpiderMonkey throws
  1637. // a TypeError.
  1638. assert.deepEqual(g.next(), {
  1639. value: 2,
  1640. done: false
  1641. });
  1642. assert.deepEqual(g.next(4), {
  1643. value: [void 0, 4],
  1644. done: true
  1645. });
  1646. });
  1647. it("should support the ignore-initial-yield wrapper idiom", function() {
  1648. var markers = [];
  1649. function *inner() {
  1650. markers.push(0);
  1651. var sent1 = yield 1;
  1652. markers.push(2);
  1653. var sent2 = yield 2;
  1654. markers.push(3);
  1655. return [sent1, sent2];
  1656. }
  1657. function wrapper(delegate) {
  1658. var gen = (function*() {
  1659. // This yield is the "initial yield" whose argument we ignore.
  1660. var sent = yield "ignored", info;
  1661. markers.push(1);
  1662. while (!(info = delegate.next(sent)).done) {
  1663. sent = yield info.value;
  1664. }
  1665. markers.push(4);
  1666. return info.value;
  1667. })();
  1668. // Ensure that gen is not newborn and that the next invocation of
  1669. // gen.next(value) can send value to the initial yield expression.
  1670. gen.next();
  1671. return gen;
  1672. }
  1673. var n = inner();
  1674. assert.deepEqual(n.next(), {
  1675. value: 1,
  1676. done: false
  1677. });
  1678. var g = wrapper(n);
  1679. // Unlike in the previous spec, it's fine to pass 3 to g.next here,
  1680. // because g is not newborn, because g.next was already called once
  1681. // before g was returned from the wrapper function.
  1682. assert.deepEqual(g.next(3), {
  1683. value: 2,
  1684. done: false
  1685. });
  1686. assert.deepEqual(g.next(4), {
  1687. value: [3, 4],
  1688. done: true
  1689. });
  1690. // Ensure we encountered the marker points in the expected order.
  1691. assert.deepEqual(markers, [0, 1, 2, 3, 4]);
  1692. });
  1693. it("should allow chaining newborn and non-newborn generators", function() {
  1694. function *range(n) {
  1695. for (var i = 0; i < n; ++i) {
  1696. yield i;
  1697. }
  1698. }
  1699. function *chain(a, b) {
  1700. yield* a;
  1701. yield* b;
  1702. }
  1703. check(chain(range(3), range(5)), [0, 1, 2, 0, 1, 2, 3, 4]);
  1704. function *y3(x) {
  1705. return yield yield yield x;
  1706. }
  1707. function *y5(x) {
  1708. return yield yield yield yield yield x;
  1709. }
  1710. check(
  1711. chain(y3("foo"), y5("bar")),
  1712. ["foo", 1, 2, "bar", 4, 5, 6, 7]
  1713. );
  1714. var g3 = y3("three");
  1715. assert.deepEqual(g3.next(), {
  1716. value: "three",
  1717. done: false
  1718. });
  1719. var g5 = y5("five");
  1720. assert.deepEqual(g5.next(), {
  1721. value: "five",
  1722. done: false
  1723. });
  1724. var undef; // A little easier to read than void 0.
  1725. check(chain(g3, g5), [undef, 1, undef, 3, 4, 5]);
  1726. });
  1727. });
  1728. describe("labeled break and continue statements", function() {
  1729. it("should be able to exit multiple try statements", function() {
  1730. var e1 = "first";
  1731. var e2 = "second";
  1732. var e3 = "third";
  1733. var e4 = "fourth";
  1734. function *gen(n, which) {
  1735. try {
  1736. yield 0;
  1737. raise(e1);
  1738. } finally {
  1739. yield 1;
  1740. loop:
  1741. for (var i = 0; i < n; ++i) {
  1742. yield i;
  1743. try {
  1744. raise(e2);
  1745. } finally {
  1746. yield 2;
  1747. try {
  1748. raise(e3);
  1749. } finally {
  1750. yield 3;
  1751. try {
  1752. raise(e4);
  1753. } finally {
  1754. yield 4;
  1755. if (which === "break") {
  1756. yield "breaking";
  1757. break loop;
  1758. }
  1759. if (which === "continue") {
  1760. yield "continuing";
  1761. continue loop;
  1762. }
  1763. yield 5;
  1764. }
  1765. }
  1766. }
  1767. }
  1768. yield 6;
  1769. }
  1770. }
  1771. try {
  1772. check(gen(1, "break"), [
  1773. 0, 1, 0, 2, 3, 4, "breaking", 6
  1774. ]);
  1775. assert.ok(false, "should have thrown an exception");
  1776. } catch (err) {
  1777. assert.strictEqual(err, e1);
  1778. }
  1779. try {
  1780. check(gen(3, "continue"), [
  1781. 0, 1, 0, 2, 3, 4, "continuing",
  1782. 1, 2, 3, 4, "continuing",
  1783. 2, 2, 3, 4, "continuing",
  1784. 6 // Loop finished naturally.
  1785. ]);
  1786. assert.ok(false, "should have thrown an exception");
  1787. } catch (err) {
  1788. assert.strictEqual(err, e1);
  1789. }
  1790. try {
  1791. check(gen(3, "neither"), [
  1792. 0, 1, 0, 2, 3, 4, 5
  1793. ]);
  1794. assert.ok(false, "should have thrown an exception");
  1795. } catch (err) {
  1796. assert.strictEqual(err, e4);
  1797. }
  1798. });
  1799. it("should allow breaking from any labeled statement", function() {
  1800. function* gen(limit) {
  1801. yield 0;
  1802. for (var i = 0; i < limit; ++i) {
  1803. yield 1;
  1804. label1: {
  1805. yield 2;
  1806. break label1;
  1807. yield 3;
  1808. }
  1809. label2:
  1810. if (limit === 3) label3: {
  1811. yield 4;
  1812. if (i === 0) break label2;
  1813. yield 5;
  1814. if (i === 1) break label3;
  1815. label4: yield 6;
  1816. // This should break from the for-loop.
  1817. if (i === 2) xxx: break;
  1818. yield 7;
  1819. }
  1820. // This should be a no-op.
  1821. xxx: break xxx;
  1822. yield 8
  1823. }
  1824. yield 9;
  1825. }
  1826. check(gen(0), [0, 9]);
  1827. check(gen(1), [0, 1, 2, 8, 9]);
  1828. check(gen(2), [0, 1, 2, 8, 1, 2, 8, 9]);
  1829. check(gen(3), [0, 1, 2, 4, 8, 1, 2, 4, 5, 8, 1, 2, 4, 5, 6, 9]);
  1830. });
  1831. });
  1832. describe("for loop with var decl and no update expression", function() {
  1833. // https://github.com/facebook/regenerator/issues/103
  1834. function *range() {
  1835. for (var i = 0; false; ) {
  1836. }
  1837. }
  1838. it("should compile and run", function() {
  1839. check(range(), []);
  1840. });
  1841. });
  1842. describe("generator function prototype", function() {
  1843. function getProto(obj) {
  1844. return Object.getPrototypeOf
  1845. ? Object.getPrototypeOf(obj)
  1846. : obj.__proto__;
  1847. }
  1848. it("should follow the expected object model", function() {
  1849. var GeneratorFunctionPrototype = getProto(f);
  1850. var GeneratorFunction = GeneratorFunctionPrototype.constructor;
  1851. assert.strictEqual(GeneratorFunction.name, 'GeneratorFunction');
  1852. assert.strictEqual(GeneratorFunction.prototype,
  1853. GeneratorFunctionPrototype);
  1854. assert.strictEqual(GeneratorFunctionPrototype.prototype.constructor,
  1855. GeneratorFunctionPrototype);
  1856. assert.strictEqual(GeneratorFunctionPrototype.prototype,
  1857. getProto(f.prototype));
  1858. assert.strictEqual(getProto(GeneratorFunctionPrototype),
  1859. Function.prototype);
  1860. if (typeof process === "undefined" ||
  1861. process.version.slice(1, 3) === "0.") {
  1862. // Node version strings start with 0.
  1863. assert.strictEqual(GeneratorFunctionPrototype.name,
  1864. "GeneratorFunctionPrototype");
  1865. } else if (process.version.slice(1, 3) === "1.") {
  1866. // iojs version strings start with 1., and iojs gets this .name
  1867. // property wrong. TODO report this?
  1868. assert.strictEqual(GeneratorFunctionPrototype.name, "");
  1869. }
  1870. assert.strictEqual(typeof f2, "function");
  1871. assert.strictEqual(f2.constructor, GeneratorFunction);
  1872. assert.ok(f2 instanceof GeneratorFunction);
  1873. assert.strictEqual(f2.name, "f2");
  1874. var g = f();
  1875. assert.ok(g instanceof f);
  1876. assert.strictEqual(getProto(g), f.prototype);
  1877. assert.deepEqual([], Object.getOwnPropertyNames(f.prototype));
  1878. // assert.deepEqual([], Object.getOwnPropertyNames(g));
  1879. f.prototype.x = 42;
  1880. var g2 = f();
  1881. assert.strictEqual(g2.x, 42);
  1882. var g3 = new f();
  1883. assert.strictEqual(g3.x, 42);
  1884. function* f2() {
  1885. yield 1;
  1886. }
  1887. assert.strictEqual(getProto(f), getProto(f2));
  1888. assert.strictEqual(f.hasOwnProperty('constructor'), false);
  1889. assert.strictEqual(getProto(f).constructor.name, 'GeneratorFunction');
  1890. // Intentionally at the end to test hoisting.
  1891. function* f() {
  1892. yield this;
  1893. }
  1894. function* f() {
  1895. yield 1;
  1896. }
  1897. var f2 = f;
  1898. f = 42;
  1899. var g = f2();
  1900. assert.deepEqual(g.next(), { value: 1, done: false });
  1901. assert.deepEqual(g.next(), { value: void 0, done: true });
  1902. assert.ok(g instanceof f2);
  1903. });
  1904. });
  1905. describe("for-of loops", function() {
  1906. (runningInTranslation ? it : xit)
  1907. ("should work for Arrays", function() {
  1908. var sum = 0;
  1909. for (var x of [1, 2].concat(3)) {
  1910. sum += x;
  1911. }
  1912. assert.strictEqual(sum, 6);
  1913. });
  1914. it("should work for generators", function() {
  1915. var value, values = [];
  1916. for (value of range(3))
  1917. values.push(value);
  1918. assert.deepEqual(values, [0, 1, 2]);
  1919. });
  1920. it("should work inside of generators", function() {
  1921. function *yieldPermutations(list) {
  1922. if (list.length < 2) {
  1923. yield list;
  1924. return 1;
  1925. }
  1926. var count = 0;
  1927. var first = list.slice(0, 1);
  1928. var genRest = yieldPermutations(list.slice(1));
  1929. for (var perm of genRest) {
  1930. for (var i = 0; i < list.length; ++i) {
  1931. var prefix = perm.slice(0, i);
  1932. var suffix = perm.slice(i);
  1933. yield prefix.concat(first, suffix);
  1934. }
  1935. count += i;
  1936. }
  1937. return count;
  1938. }
  1939. var count = 0;
  1940. for (var perm of yieldPermutations([])) {
  1941. assert.deepEqual(perm, []);
  1942. ++count;
  1943. }
  1944. assert.strictEqual(count, 1);
  1945. check(yieldPermutations([1]), [[1]], 1);
  1946. check(yieldPermutations([2, 1]), [
  1947. [2, 1],
  1948. [1, 2]
  1949. ], 2);
  1950. check(yieldPermutations([1,3,2]), [
  1951. [1, 3, 2],
  1952. [3, 1, 2],
  1953. [3, 2, 1],
  1954. [1, 2, 3],
  1955. [2, 1, 3],
  1956. [2, 3, 1]
  1957. ], 6);
  1958. });
  1959. });
  1960. describe("generator return method", function() {
  1961. if (!runningInTranslation) {
  1962. // The return method has not been specified or implemented natively,
  1963. // yet, so these tests need only pass in translation.
  1964. return;
  1965. }
  1966. it("should work with newborn generators", function() {
  1967. function *gen() {
  1968. yield 0;
  1969. }
  1970. var g = gen();
  1971. assert.deepEqual(g.return("argument"), {
  1972. value: "argument",
  1973. done: true
  1974. });
  1975. assertAlreadyFinished(g);
  1976. });
  1977. it("should behave as if generator actually returned", function() {
  1978. var executedFinally = false;
  1979. function *gen() {
  1980. try {
  1981. yield 0;
  1982. } catch (err) {
  1983. assert.ok(false, "should not have executed the catch handler");
  1984. } finally {
  1985. executedFinally = true;
  1986. }
  1987. }
  1988. var g = gen();
  1989. assert.deepEqual(g.next(), { value: 0, done: false });
  1990. assert.deepEqual(g.return("argument"), {
  1991. value: "argument",
  1992. done: true
  1993. });
  1994. assert.strictEqual(executedFinally, true);
  1995. assertAlreadyFinished(g);
  1996. });
  1997. it("should return both delegate and delegator", function() {
  1998. var checkpoints = [];
  1999. function* callee(errorToThrow) {
  2000. try {
  2001. yield 1;
  2002. yield 2;
  2003. } finally {
  2004. checkpoints.push("callee finally");
  2005. if (errorToThrow) {
  2006. throw errorToThrow;
  2007. }
  2008. }
  2009. }
  2010. function* caller(errorToThrow) {
  2011. try {
  2012. yield 0;
  2013. yield* callee(errorToThrow);
  2014. yield 3;
  2015. } finally {
  2016. checkpoints.push("caller finally");
  2017. }
  2018. }
  2019. var g1 = caller();
  2020. assert.deepEqual(g1.next(), { value: 0, done: false });
  2021. assert.deepEqual(g1.next(), { value: 1, done: false });
  2022. assert.deepEqual(g1.return(-1), { value: -1, done: true });
  2023. assert.deepEqual(checkpoints, [
  2024. "callee finally",
  2025. "caller finally"
  2026. ]);
  2027. var error = new Error("thrown from callee");
  2028. var g2 = caller(error);
  2029. assert.deepEqual(g2.next(), { value: 0, done: false });
  2030. assert.deepEqual(g2.next(), { value: 1, done: false });
  2031. try {
  2032. g2.return(-1);
  2033. assert.ok(false, "should have thrown an exception");
  2034. } catch (thrown) {
  2035. assert.strictEqual(thrown, error);
  2036. }
  2037. assert.deepEqual(checkpoints, [
  2038. "callee finally",
  2039. "caller finally",
  2040. "callee finally",
  2041. "caller finally"
  2042. ]);
  2043. });
  2044. });
  2045. describe("expressions containing yield subexpressions", function() {
  2046. it("should evaluate all subexpressions before yielding", function() {
  2047. function *gen(x) {
  2048. return x * (yield (function(y) { x = y }));
  2049. }
  2050. var g = gen(2);
  2051. var result = g.next();
  2052. assert.strictEqual(result.done, false);
  2053. result.value(5);
  2054. assert.deepEqual(g.next(5), {
  2055. value: 10,
  2056. done: true
  2057. });
  2058. });
  2059. it("should work even with getter member expressions", function() {
  2060. function *gen() {
  2061. return a.b + (yield "asdf");
  2062. }
  2063. var a = {};
  2064. var b = 0;
  2065. Object.defineProperty(a, "b", {
  2066. get: function() {
  2067. return ++b;
  2068. }
  2069. });
  2070. var g = gen();
  2071. assert.strictEqual(a.b, 1);
  2072. assert.deepEqual(g.next(), {
  2073. value: "asdf",
  2074. done: false
  2075. });
  2076. assert.strictEqual(a.b, 3);
  2077. assert.deepEqual(g.next(2), {
  2078. value: 4,
  2079. done: true
  2080. });
  2081. });
  2082. it("should evaluate all array elements before yielding", function() {
  2083. function *gen() {
  2084. return [a, yield "asdf", a];
  2085. }
  2086. var a = 1;
  2087. var g = gen();
  2088. assert.deepEqual(g.next(), {
  2089. value: "asdf",
  2090. done: false
  2091. });
  2092. a = 3;
  2093. assert.deepEqual(g.next(2), {
  2094. value: [1, 2, 3],
  2095. done: true
  2096. });
  2097. });
  2098. it("should handle callee member expressions correctly", function() {
  2099. function *gen() {
  2100. a = a.slice(0).concat(yield "asdf");
  2101. return a;
  2102. }
  2103. var a = [];
  2104. var g = gen();
  2105. assert.deepEqual(g.next(), {
  2106. value: "asdf",
  2107. done: false
  2108. });
  2109. a.push(1);
  2110. assert.deepEqual(g.next(2), {
  2111. value: [2],
  2112. done: true
  2113. });
  2114. });
  2115. it("should handle implicit stringification correctly", function() {
  2116. function *gen() {
  2117. return a + (yield "asdf");
  2118. }
  2119. var a = [1, 2];
  2120. var g = gen();
  2121. assert.deepEqual(g.next(), {
  2122. value: "asdf",
  2123. done: false
  2124. });
  2125. a = [4,5];
  2126. assert.deepEqual(g.next(",3"), {
  2127. value: "1,2,3",
  2128. done: true
  2129. });
  2130. });
  2131. });