-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathIterator.ts
More file actions
39 lines (33 loc) · 1.4 KB
/
Iterator.ts
File metadata and controls
39 lines (33 loc) · 1.4 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
32
33
34
35
36
37
38
39
import { GeneratorIterator } from "./GeneratorIterator";
function iteratorGeneratorStep(this: GeneratorIterator): LuaMultiReturn<[true, any] | []> {
const co = this.____coroutine;
const [status, value] = coroutine.resume(co);
if (!status) throw value;
if (coroutine.status(co) === "dead") return $multi();
return $multi(true, value);
}
function iteratorIteratorStep<T>(this: Iterator<T>): LuaMultiReturn<[true, T] | []> {
const result = this.next();
if (result.done) return $multi();
return $multi(true, result.value);
}
function iteratorStringStep(this: string, index: number): LuaMultiReturn<[number, string] | []> {
index += 1;
if (index > this.length) return $multi();
return $multi(index, string.sub(this, index, index));
}
export function __TS__Iterator<T>(
this: void,
iterable: string | GeneratorIterator | Iterable<T> | readonly T[]
): LuaMultiReturn<[(...args: any[]) => [any, any] | [], ...any[]]> | LuaIterable<LuaMultiReturn<[number, T]>> {
if (typeof iterable === "string") {
return $multi(iteratorStringStep, iterable, 0);
} else if ("____coroutine" in iterable) {
return $multi(iteratorGeneratorStep, iterable);
} else if (iterable[Symbol.iterator]) {
const iterator = iterable[Symbol.iterator]();
return $multi(iteratorIteratorStep, iterator);
} else {
return ipairs(iterable as readonly T[]);
}
}