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
2 changes: 2 additions & 0 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export enum LuaLibFeature {
WeakMap = "WeakMap",
WeakSet = "WeakSet",
SourceMapTraceBack = "SourceMapTraceBack",
Spread = "Spread",
StringConcat = "StringConcat",
StringEndsWith = "StringEndsWith",
StringReplace = "StringReplace",
Expand All @@ -62,6 +63,7 @@ const luaLibDependencies: {[lib in LuaLibFeature]?: LuaLibFeature[]} = {
Set: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol],
WeakMap: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol],
WeakSet: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol],
Spread: [LuaLibFeature.Iterator],
SymbolRegistry: [LuaLibFeature.Symbol],
};

Expand Down
7 changes: 6 additions & 1 deletion src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4561,9 +4561,14 @@ export class LuaTransformer {
const innerExpression = this.expectExpression(this.transformExpression(expression.expression));
if (tsHelper.isTupleReturnCall(expression.expression, this.checker)) {
return innerExpression;
} else {
}

const type = this.checker.getTypeAtLocation(expression.expression);
if (tsHelper.isArrayType(type, this.checker, this.program)) {
return this.createUnpackCall(innerExpression, expression);
}

return this.transformLuaLibFunction(LuaLibFeature.Spread, expression, innerExpression);
}

public transformStringLiteral(literal: ts.StringLiteralLike): ExpressionVisitResult {
Expand Down
13 changes: 13 additions & 0 deletions src/lualib/Spread.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
declare function unpack<T>(this: void, list: T[], i?: number, j?: number): T[];

declare namespace table {
export function unpack<T>(this: void, list: T[], i?: number, j?: number): T[];
}

function __TS__Spread<T>(this: void, iterable: Iterable<T>): T[] {
const arr: T[] = [];
for (const item of iterable) {
arr[arr.length] = item;
}
return (table.unpack || unpack)(arr);
}
20 changes: 20 additions & 0 deletions test/unit/spreadElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,23 @@ test("Spread Element Lua JIT", () => {
const lua = util.transpileString(`[...[0, 1, 2]]`, options);
expect(lua).toBe("local ____ = {unpack({\n 0,\n 1,\n 2,\n})}");
});

test("Spread Element Iterable", () => {
const code = `
const it = {
i: -1,
[Symbol.iterator]() {
return this;
},
next() {
++this.i;
return {
value: 2 ** this.i,
done: this.i == 9,
}
}
};
const arr = [...it];
return JSONStringify(arr)`;
expect(JSON.parse(util.transpileAndExecute(code))).toEqual([1, 2, 4, 8, 16, 32, 64, 128, 256]);
});