Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/lualib/Iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@ function __TS__IteratorIteratorStep<T>(this: Iterator<T>): [true, T] | [] {
return [true, result.value];
}

/** @tupleReturn */
function __TS__IteratorStringStep(this: string, index: number): [number, string] | [] {
index += 1;
if (index > this.length) return [];
return [index, string.sub(this, index, index)];
}

/** @tupleReturn */
function __TS__Iterator<T>(
this: void,
iterable: Iterable<T> | GeneratorIterator | readonly T[]
): [(...args: any[]) => [any, T] | [], ...any[]] {
): [(...args: any[]) => [any, any] | [], ...any[]] {
if ("____coroutine" in iterable) {
return [__TS__IteratorGeneratorStep, iterable];
} else if (iterable[Symbol.iterator]) {
const iterator = iterable[Symbol.iterator]();
return [__TS__IteratorIteratorStep, iterator];
} else if (typeof iterable === "string") {
return [__TS__IteratorStringStep, iterable, 0];
} else {
return ipairs(iterable as readonly T[]) as any;
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/loops.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,16 @@ test("forof with array typed as iterable", () => {
`.expectToMatchJsResult();
});

test.each(["", "abc", "a\0c"])("forof string (%p)", string => {
util.testFunctionTemplate`
const results: string[] = [];
for (const x of ${string}) {
results.push(x);
}
return results;
`.expectToMatchJsResult();
});

describe("for...of empty destructuring", () => {
const declareTests = (destructuringPrefix: string) => {
test("array", () => {
Expand Down