Skip to content

Commit

Permalink
Runtime: yield* should throw if not iterable (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSodaSea committed Aug 4, 2023
1 parent b3bf1a9 commit d3bac38
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/runtime/runtime.js
Expand Up @@ -487,7 +487,7 @@ var runtime = (function (exports) {
};

function values(iterable) {
if (iterable) {
if (iterable || iterable === "") {
var iteratorMethod = iterable[iteratorSymbol];
if (iteratorMethod) {
return iteratorMethod.call(iterable);
Expand Down Expand Up @@ -517,8 +517,7 @@ var runtime = (function (exports) {
}
}

// Return an iterator with no values.
return { next: doneResult };
throw new TypeError(typeof iterable + " is not iterable");
}
exports.values = values;

Expand Down
22 changes: 22 additions & 0 deletions test/tests.es6.js
Expand Up @@ -1504,6 +1504,28 @@ describe("delegated yield", function() {
check(gen(iterator), [], "1foo");
});

it("should work with empty string", function() {
function* f() {
yield* "";
};

assert.deepEqual(f().next(), { value: undefined, done: true });
});

it("should throw if not iterable", function() {
function* f(x) {
yield* x;
};

assert.throws(() => f(undefined).next(), TypeError);
assert.throws(() => f(null).next(), TypeError);
assert.throws(() => f(false).next(), TypeError);
assert.throws(() => f(true).next(), TypeError);
assert.throws(() => f(0).next(), TypeError);
assert.throws(() => f(1).next(), TypeError);
assert.throws(() => f({}).next(), TypeError);
});

it("should throw if the delegated iterable's iterator doesn't have .next", function() {
var it = function* () {
yield* {
Expand Down

0 comments on commit d3bac38

Please sign in to comment.