generators-iteration.js 19.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

wingo@igalia.com's avatar
wingo@igalia.com committed
28
// Flags: --expose-gc
29 30 31 32 33

// Test generator iteration.

var GeneratorFunction = (function*(){yield 1;}).__proto__.constructor;

34 35 36 37
function assertIteratorResult(value, done, result) {
  assertEquals({ value: value, done: done}, result);
}

38 39 40 41 42 43
function assertIteratorIsClosed(iter) {
  assertIteratorResult(undefined, true, iter.next());
  assertDoesNotThrow(function() { iter.next(); });
}

function assertThrownIteratorIsClosed(iter) {
44
  assertIteratorIsClosed(iter);
45 46
}

47 48 49 50 51 52 53 54 55
function TestGeneratorResultPrototype() {
  function* g() { yield 1; }
  var iter = g();
  var result = iter.next();

  assertSame(Object.prototype, Object.getPrototypeOf(result));
  property_names = Object.getOwnPropertyNames(result);
  property_names.sort();
  assertEquals(["done", "value"], property_names);
56
  assertIteratorResult(1, false, result);
57 58 59
}
TestGeneratorResultPrototype()

60 61 62 63 64
function TestGenerator(g, expected_values_for_next,
                       send_val, expected_values_for_send) {
  function testNext(thunk) {
    var iter = thunk();
    for (var i = 0; i < expected_values_for_next.length; i++) {
65 66 67 68
      var v1 = expected_values_for_next[i];
      var v2 = i == expected_values_for_next.length - 1;
      // var v3 = iter.next();
      assertIteratorResult(v1, v2, iter.next());
69
    }
70
    assertIteratorIsClosed(iter);
71 72 73 74
  }
  function testSend(thunk) {
    var iter = thunk();
    for (var i = 0; i < expected_values_for_send.length; i++) {
75 76
      assertIteratorResult(expected_values_for_send[i],
                           i == expected_values_for_send.length - 1,
77
                           iter.next(send_val));
78
    }
79
    assertIteratorIsClosed(iter);
80 81 82 83 84
  }
  function testThrow(thunk) {
    for (var i = 0; i < expected_values_for_next.length; i++) {
      var iter = thunk();
      for (var j = 0; j < i; j++) {
85 86 87
        assertIteratorResult(expected_values_for_next[j],
                             j == expected_values_for_next.length - 1,
                             iter.next());
88 89 90
      }
      function Sentinel() {}
      assertThrows(function () { iter.throw(new Sentinel); }, Sentinel);
91
      assertThrownIteratorIsClosed(iter);
92 93 94 95 96 97 98
    }
  }

  testNext(g);
  testSend(g);
  testThrow(g);

99 100 101 102
  testNext(function*() { return yield* g(); });
  testSend(function*() { return yield* g(); });
  testThrow(function*() { return yield* g(); });

103
  if (g instanceof GeneratorFunction) {
104 105 106
    testNext(g);
    testSend(g);
    testThrow(g);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  }
}

TestGenerator(function* g1() { },
              [undefined],
              "foo",
              [undefined]);

TestGenerator(function* g2() { yield 1; },
              [1, undefined],
              "foo",
              [1, undefined]);

TestGenerator(function* g3() { yield 1; yield 2; },
              [1, 2, undefined],
              "foo",
              [1, 2, undefined]);

TestGenerator(function* g4() { yield 1; yield 2; return 3; },
              [1, 2, 3],
              "foo",
              [1, 2, 3]);

TestGenerator(function* g5() { return 1; },
              [1],
             "foo",
              [1]);

TestGenerator(function* g6() { var x = yield 1; return x; },
              [1, undefined],
              "foo",
              [1, "foo"]);

TestGenerator(function* g7() { var x = yield 1; yield 2; return x; },
              [1, 2, undefined],
              "foo",
              [1, 2, "foo"]);

TestGenerator(function* g8() { for (var x = 0; x < 4; x++) { yield x; } },
              [0, 1, 2, 3, undefined],
              "foo",
              [0, 1, 2, 3, undefined]);

// Generator with arguments.
TestGenerator(
    function g9() {
      return (function*(a, b, c, d) {
        yield a; yield b; yield c; yield d;
      })("fee", "fi", "fo", "fum");
    },
    ["fee", "fi", "fo", "fum", undefined],
    "foo",
    ["fee", "fi", "fo", "fum", undefined]);

// Too few arguments.
TestGenerator(
    function g10() {
      return (function*(a, b, c, d) {
        yield a; yield b; yield c; yield d;
      })("fee", "fi");
    },
    ["fee", "fi", undefined, undefined, undefined],
    "foo",
    ["fee", "fi", undefined, undefined, undefined]);

// Too many arguments.
TestGenerator(
    function g11() {
      return (function*(a, b, c, d) {
        yield a; yield b; yield c; yield d;
      })("fee", "fi", "fo", "fum", "I smell the blood of an Englishman");
    },
    ["fee", "fi", "fo", "fum", undefined],
    "foo",
    ["fee", "fi", "fo", "fum", undefined]);

// The arguments object.
TestGenerator(
    function g12() {
      return (function*(a, b, c, d) {
        for (var i = 0; i < arguments.length; i++) {
          yield arguments[i];
        }
      })("fee", "fi", "fo", "fum", "I smell the blood of an Englishman");
    },
    ["fee", "fi", "fo", "fum", "I smell the blood of an Englishman",
     undefined],
    "foo",
    ["fee", "fi", "fo", "fum", "I smell the blood of an Englishman",
     undefined]);

// Access to captured free variables.
TestGenerator(
    function g13() {
      return (function(a, b, c, d) {
        return (function*() {
          yield a; yield b; yield c; yield d;
        })();
      })("fee", "fi", "fo", "fum");
    },
    ["fee", "fi", "fo", "fum", undefined],
    "foo",
    ["fee", "fi", "fo", "fum", undefined]);

// Abusing the arguments object.
TestGenerator(
    function g14() {
      return (function*(a, b, c, d) {
        arguments[0] = "Be he live";
        arguments[1] = "or be he dead";
        arguments[2] = "I'll grind his bones";
        arguments[3] = "to make my bread";
        yield a; yield b; yield c; yield d;
      })("fee", "fi", "fo", "fum");
    },
    ["Be he live", "or be he dead", "I'll grind his bones", "to make my bread",
     undefined],
    "foo",
    ["Be he live", "or be he dead", "I'll grind his bones", "to make my bread",
     undefined]);

// Abusing the arguments object: strict mode.
TestGenerator(
    function g15() {
      return (function*(a, b, c, d) {
        "use strict";
        arguments[0] = "Be he live";
        arguments[1] = "or be he dead";
        arguments[2] = "I'll grind his bones";
        arguments[3] = "to make my bread";
        yield a; yield b; yield c; yield d;
      })("fee", "fi", "fo", "fum");
    },
    ["fee", "fi", "fo", "fum", undefined],
    "foo",
    ["fee", "fi", "fo", "fum", undefined]);

// GC.
TestGenerator(function* g16() { yield "baz"; gc(); yield "qux"; },
              ["baz", "qux", undefined],
              "foo",
              ["baz", "qux", undefined]);

// Receivers.
251 252 253 254 255 256 257 258 259
TestGenerator(
    function g17() {
      function* g() { yield this.x; yield this.y; }
      var o = { start: g, x: 1, y: 2 };
      return o.start();
    },
    [1, 2, undefined],
    "foo",
    [1, 2, undefined]);
260

261
TestGenerator(
262
    function* g19() {
263 264 265 266 267 268 269 270 271
      var x = 1;
      yield x;
      with({x:2}) { yield x; }
      yield x;
    },
    [1, 2, 1, undefined],
    "foo",
    [1, 2, 1, undefined]);

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
TestGenerator(
    function* g20() { yield (1 + (yield 2) + 3); },
    [2, NaN, undefined],
    "foo",
    [2, "1foo3", undefined]);

TestGenerator(
    function* g21() { return (1 + (yield 2) + 3); },
    [2, NaN],
    "foo",
    [2, "1foo3"]);

TestGenerator(
    function* g22() { yield (1 + (yield 2) + 3); yield (4 + (yield 5) + 6); },
    [2, NaN, 5, NaN, undefined],
    "foo",
    [2, "1foo3", 5, "4foo6", undefined]);

TestGenerator(
    function* g23() {
      return (yield (1 + (yield 2) + 3)) + (yield (4 + (yield 5) + 6));
    },
    [2, NaN, 5, NaN, NaN],
    "foo",
    [2, "1foo3", 5, "4foo6", "foofoo"]);

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
// Rewind a try context with and without operands on the stack.
TestGenerator(
    function* g24() {
      try {
        return (yield (1 + (yield 2) + 3)) + (yield (4 + (yield 5) + 6));
      } catch (e) {
        throw e;
      }
    },
    [2, NaN, 5, NaN, NaN],
    "foo",
    [2, "1foo3", 5, "4foo6", "foofoo"]);

// Yielding in a catch context, with and without operands on the stack.
TestGenerator(
    function* g25() {
      try {
        throw (yield (1 + (yield 2) + 3))
      } catch (e) {
        if (typeof e == 'object') throw e;
        return e + (yield (4 + (yield 5) + 6));
      }
    },
    [2, NaN, 5, NaN, NaN],
    "foo",
    [2, "1foo3", 5, "4foo6", "foofoo"]);

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
// Yield with no arguments yields undefined.
TestGenerator(
    function* g26() { return yield yield },
    [undefined, undefined, undefined],
    "foo",
    [undefined, "foo", "foo"]);

// A newline causes the parser to stop looking for an argument to yield.
TestGenerator(
    function* g27() {
      yield
      3
      return
    },
    [undefined, undefined],
    "foo",
    [undefined, undefined]);

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
// TODO(wingo): We should use TestGenerator for these, except that
// currently yield* will unconditionally propagate a throw() to the
// delegate iterator, which fails for these iterators that don't have
// throw().  See http://code.google.com/p/v8/issues/detail?id=3484.
(function() {
    function* g28() {
      yield* [1, 2, 3];
    }
    var iter = g28();
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.next());
    assertIteratorResult(undefined, true, iter.next());
})();

(function() {
    function* g29() {
      yield* "abc";
    }
    var iter = g29();
    assertIteratorResult("a", false, iter.next());
    assertIteratorResult("b", false, iter.next());
    assertIteratorResult("c", false, iter.next());
    assertIteratorResult(undefined, true, iter.next());
})();

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
// Generator function instances.
TestGenerator(GeneratorFunction(),
              [undefined],
              "foo",
              [undefined]);

TestGenerator(new GeneratorFunction(),
              [undefined],
              "foo",
              [undefined]);

TestGenerator(GeneratorFunction('yield 1;'),
              [1, undefined],
              "foo",
              [1, undefined]);

TestGenerator(
    function() { return GeneratorFunction('x', 'y', 'yield x + y;')(1, 2) },
    [3, undefined],
    "foo",
    [3, undefined]);

391 392 393 394 395 396 397 398 399
// Access to this with formal arguments.
TestGenerator(
    function () {
      return ({ x: 42, g: function* (a) { yield this.x } }).g(0);
    },
    [42, undefined],
    "foo",
    [42, undefined]);

neis's avatar
neis committed
400 401 402 403 404 405
// Test that yield* validates iterator results.
function TestDelegatingYield(junk) {
  var iterator = {next: () => junk};
  var iterable = {[Symbol.iterator]: () => iterator};
  function* g() { return yield* iterable };
  assertThrows(() => g().next(), TypeError);
406 407
}
TestDelegatingYield();
neis's avatar
neis committed
408 409 410
TestDelegatingYield(null);
TestDelegatingYield(42);
TestDelegatingYield(true);
411

412
function TestTryCatch(instantiate) {
413 414
  function* g() { yield 1; try { yield 2; } catch (e) { yield e; } yield 3; }
  function Sentinel() {}
415 416 417 418 419

  function Test1(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.next());
420
    assertIteratorIsClosed(iter);
421 422 423 424 425
  }
  Test1(instantiate(g));

  function Test2(iter) {
    assertThrows(function() { iter.throw(new Sentinel); }, Sentinel);
426
    assertThrownIteratorIsClosed(iter);
427 428 429 430 431 432
  }
  Test2(instantiate(g));

  function Test3(iter) {
    assertIteratorResult(1, false, iter.next());
    assertThrows(function() { iter.throw(new Sentinel); }, Sentinel);
433
    assertThrownIteratorIsClosed(iter);
434 435 436 437 438 439 440 441 442
  }
  Test3(instantiate(g));

  function Test4(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    var exn = new Sentinel;
    assertIteratorResult(exn, false, iter.throw(exn));
    assertIteratorResult(3, false, iter.next());
443
    assertIteratorIsClosed(iter);
444 445 446 447 448 449 450 451 452 453
  }
  Test4(instantiate(g));

  function Test5(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    var exn = new Sentinel;
    assertIteratorResult(exn, false, iter.throw(exn));
    assertIteratorResult(3, false, iter.next());
    assertThrows(function() { iter.throw(new Sentinel); }, Sentinel);
454
    assertThrownIteratorIsClosed(iter);
455 456 457 458 459 460 461 462 463
  }
  Test5(instantiate(g));

  function Test6(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    var exn = new Sentinel;
    assertIteratorResult(exn, false, iter.throw(exn));
    assertThrows(function() { iter.throw(new Sentinel); }, Sentinel);
464
    assertThrownIteratorIsClosed(iter);
465 466 467 468 469 470 471
  }
  Test6(instantiate(g));

  function Test7(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.next());
472
    assertIteratorIsClosed(iter);
473 474
  }
  Test7(instantiate(g));
475
}
476 477
TestTryCatch(function (g) { return g(); });
TestTryCatch(function* (g) { return yield* g(); });
478

479
function TestTryFinally(instantiate) {
480 481 482
  function* g() { yield 1; try { yield 2; } finally { yield 3; } yield 4; }
  function Sentinel() {}
  function Sentinel2() {}
483 484 485 486 487 488

  function Test1(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.next());
    assertIteratorResult(4, false, iter.next());
489
    assertIteratorIsClosed(iter);
490 491 492 493 494
  }
  Test1(instantiate(g));

  function Test2(iter) {
    assertThrows(function() { iter.throw(new Sentinel); }, Sentinel);
495
    assertThrownIteratorIsClosed(iter);
496 497 498 499 500 501
  }
  Test2(instantiate(g));

  function Test3(iter) {
    assertIteratorResult(1, false, iter.next());
    assertThrows(function() { iter.throw(new Sentinel); }, Sentinel);
502
    assertThrownIteratorIsClosed(iter);
503 504 505 506 507 508 509 510
  }
  Test3(instantiate(g));

  function Test4(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.throw(new Sentinel));
    assertThrows(function() { iter.next(); }, Sentinel);
511
    assertThrownIteratorIsClosed(iter);
512 513 514 515 516 517 518 519
  }
  Test4(instantiate(g));

  function Test5(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.throw(new Sentinel));
    assertThrows(function() { iter.throw(new Sentinel2); }, Sentinel2);
520
    assertThrownIteratorIsClosed(iter);
521 522 523 524 525 526 527 528
  }
  Test5(instantiate(g));

  function Test6(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.next());
    assertThrows(function() { iter.throw(new Sentinel); }, Sentinel);
529
    assertThrownIteratorIsClosed(iter);
530 531 532 533 534 535 536 537 538
  }
  Test6(instantiate(g));

  function Test7(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.next());
    assertIteratorResult(4, false, iter.next());
    assertThrows(function() { iter.throw(new Sentinel); }, Sentinel);
539
    assertThrownIteratorIsClosed(iter);
540 541 542 543 544 545 546 547
  }
  Test7(instantiate(g));

  function Test8(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.next());
    assertIteratorResult(4, false, iter.next());
548
    assertIteratorIsClosed(iter);
549 550
  }
  Test8(instantiate(g));
551
}
552 553
TestTryFinally(function (g) { return g(); });
TestTryFinally(function* (g) { return yield* g(); });
554

555
function TestNestedTry(instantiate) {
556 557 558 559 560 561 562 563 564 565 566 567
  function* g() {
    try {
      yield 1;
      try { yield 2; } catch (e) { yield e; }
      yield 3;
    } finally {
      yield 4;
    }
    yield 5;
  }
  function Sentinel() {}
  function Sentinel2() {}
568 569 570 571 572 573 574

  function Test1(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    assertIteratorResult(3, false, iter.next());
    assertIteratorResult(4, false, iter.next());
    assertIteratorResult(5, false, iter.next());
575
    assertIteratorIsClosed(iter);
576 577 578 579 580
  }
  Test1(instantiate(g));

  function Test2(iter) {
    assertThrows(function() { iter.throw(new Sentinel); }, Sentinel);
581
    assertThrownIteratorIsClosed(iter);
582 583 584 585 586 587 588
  }
  Test2(instantiate(g));

  function Test3(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(4, false, iter.throw(new Sentinel));
    assertThrows(function() { iter.next(); }, Sentinel);
589
    assertThrownIteratorIsClosed(iter);
590 591 592 593 594 595 596
  }
  Test3(instantiate(g));

  function Test4(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(4, false, iter.throw(new Sentinel));
    assertThrows(function() { iter.throw(new Sentinel2); }, Sentinel2);
597
    assertThrownIteratorIsClosed(iter);
598 599 600 601 602 603 604 605 606 607 608
  }
  Test4(instantiate(g));

  function Test5(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    var exn = new Sentinel;
    assertIteratorResult(exn, false, iter.throw(exn));
    assertIteratorResult(3, false, iter.next());
    assertIteratorResult(4, false, iter.next());
    assertIteratorResult(5, false, iter.next());
609
    assertIteratorIsClosed(iter);
610 611 612 613 614 615 616 617 618 619
  }
  Test5(instantiate(g));

  function Test6(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    var exn = new Sentinel;
    assertIteratorResult(exn, false, iter.throw(exn));
    assertIteratorResult(4, false, iter.throw(new Sentinel2));
    assertThrows(function() { iter.next(); }, Sentinel2);
620
    assertThrownIteratorIsClosed(iter);
621 622 623 624 625 626 627 628 629 630 631
  }
  Test6(instantiate(g));

  function Test7(iter) {
    assertIteratorResult(1, false, iter.next());
    assertIteratorResult(2, false, iter.next());
    var exn = new Sentinel;
    assertIteratorResult(exn, false, iter.throw(exn));
    assertIteratorResult(3, false, iter.next());
    assertIteratorResult(4, false, iter.throw(new Sentinel2));
    assertThrows(function() { iter.next(); }, Sentinel2);
632
    assertThrownIteratorIsClosed(iter);
633 634
  }
  Test7(instantiate(g));
635 636 637

  // That's probably enough.
}
638 639
TestNestedTry(function (g) { return g(); });
TestNestedTry(function* (g) { return yield* g(); });
640

641 642 643 644 645 646 647
function TestRecursion() {
  function TestNextRecursion() {
    function* g() { yield iter.next(); }
    var iter = g();
    return iter.next();
  }
  function TestSendRecursion() {
648
    function* g() { yield iter.next(42); }
649 650 651 652 653 654 655 656 657 658 659 660 661
    var iter = g();
    return iter.next();
  }
  function TestThrowRecursion() {
    function* g() { yield iter.throw(1); }
    var iter = g();
    return iter.next();
  }
  assertThrows(TestNextRecursion, Error);
  assertThrows(TestSendRecursion, Error);
  assertThrows(TestThrowRecursion, Error);
}
TestRecursion();
neis's avatar
neis committed
662 663 664 665 666 667 668 669 670 671 672 673 674


// Test yield* on non-iterable objects.
function* g(junk) { return yield* junk }
var non_iterables = [
  42,
  {[Symbol.iterator]: 42},
  {[Symbol.iterator]: () => 42},
  {[Symbol.iterator]: () => ({next: 42})},
];
for (let junk of non_iterables) {
  assertThrows(() => g(junk).next(), TypeError);
}