forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.ts
More file actions
31 lines (26 loc) · 1.08 KB
/
Generator.ts
File metadata and controls
31 lines (26 loc) · 1.08 KB
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
28
29
30
31
interface GeneratorIterator {
____coroutine: LuaThread;
[Symbol.iterator](): GeneratorIterator;
next: typeof __TS__GeneratorNext;
}
function __TS__GeneratorIterator(this: GeneratorIterator) {
return this;
}
function __TS__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" };
}
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]: __TS__GeneratorIterator,
next: __TS__GeneratorNext,
};
};
}