-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathGenerator.ts
More file actions
27 lines (22 loc) · 1007 Bytes
/
Generator.ts
File metadata and controls
27 lines (22 loc) · 1007 Bytes
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
import { GeneratorIterator } from "./GeneratorIterator";
function generatorIterator(this: GeneratorIterator) {
return this;
}
function generatorNext(this: GeneratorIterator, ...args: any[]) {
const co = this.____coroutine;
if (coroutine.status(co) === "dead") return { done: true };
const [status, value] = coroutine.resume(co, ...args);
if (!status) throw value;
return { value, done: coroutine.status(co) === "dead" };
}
export function __TS__Generator(this: void, fn: (this: void, ...args: any[]) => any) {
return function (this: void, ...args: any[]): GeneratorIterator {
const argsLength = select("#", ...args);
return {
// Using explicit this there, since we don't pass arguments after the first nil and context is likely to be nil
____coroutine: coroutine.create(() => fn(...(unpack ?? table.unpack)(args, 1, argsLength))),
[Symbol.iterator]: generatorIterator,
next: generatorNext,
};
};
}