Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return completions when yield* is used #20

Open
anba opened this issue Dec 3, 2024 · 3 comments
Open

Return completions when yield* is used #20

anba opened this issue Dec 3, 2024 · 3 comments

Comments

@anba
Copy link

anba commented Dec 3, 2024

var it = Iterator.concat({
  [Symbol.iterator]() { return this; },
  next() { console.log("next"); return {done: false}; },
  return() { console.log("return"); return {done: false}; },
});

Before #18:

js> it.next()
next
({value:(void 0), done:false})
js> it.return()
return
({value:(void 0), done:true})
js> it.return()
({value:(void 0), done:true})

When using yield* semantics:

js> it.next()
next
({done:false})
js> it.return()
return
({done:false})
js> it.return()
return
({done:false})

But the current spec requires something in between, because per spec return completions must always close the iterator, whereas yield* allows to continue after return completions.

After #18:

s> it.next()
next
({done:false})
js> it.return()
return
({value:(void 0), done:true})
js> it.return()
({value:(void 0), done:true})
@anba
Copy link
Author

anba commented Dec 3, 2024

SpiderMonkey implementation before #18:

for (var innerValue of allowContentIterWith(item, method)) {
  yield innerValue;
}

Implementation after #18:

// Step 3.a.i.
var iter = callContentFunction(method, item);

// Step 3.a.ii.
if (!IsObject(iter)) {
  ThrowTypeError(JSMSG_GET_ITER_RETURNED_PRIMITIVE);
}

// Step 3.a.iii.
var iterNext = iter.next;

// Steps 3.a.iv-v.
var wrapped = {
  next() {
    return callContentFunction(iterNext, iter);
  },
  return() {
    IteratorClose(item);
    return {done: true, value: undefined};
  },
};
yield* allowContentIterWithNext(wrapped, wrapped.next);

@michaelficarra
Copy link
Member

michaelficarra commented Dec 3, 2024

After #18:

s> it.next()
next
({done:false})
js> it.return()
return
({value:(void 0), done:true})
js> it.return()
({value:(void 0), done:true})

This seems like the correct behaviour. What are you saying is undesirable about it? That it can't be implemented as literally

function* concat(...iterables) {
  for (const it of iterables) yield* it;
}

?

@anba
Copy link
Author

anba commented Dec 3, 2024

Yes, it seems undesirable to add another wrapper to be able to use yield*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants