import type { Interpreter } from "../runtime/interpreter.js" import { typeError } from "../runtime/model.js" import { constructor, methods, receiver, requiresNew } from "../runtime/native.js" import { iteratorResult, IteratorObj, Position } from "../runtime/objects.js" // Every built-in iterator and helper shares the Iterator prototype; JS gives each collection its own, which is only // observable through getPrototypeOf. The helpers (`map`, `take`, `Iterator.from`, ...) are the prelude's. export const iteratorGlobals = (ctx: Interpreter) => { const builtins = ctx.builtins const proto = builtins.Iterator methods(builtins, proto, [ [ "next", 0, (thisValue) => { const cursor = receiver(IteratorObj, thisValue, "Iterator.prototype.next").cursor if (!(cursor instanceof Position)) throw typeError("Iterator.prototype.next called on an internal iterator.") const step = cursor.step() return iteratorResult(builtins.Object, step.value, step.done) }, ], ]) return constructor(builtins, proto, { name: "Iterator", length: 0, call: requiresNew("Iterator"), construct: () => { throw typeError("Iterator is abstract; use Iterator.from(...) or a built-in iterator.") }, }) }