Skip to content

Commit a221972

Browse files
authored
fix(5.0): bad unpack() call in SparseArraySpread (#1347)
1 parent b345e86 commit a221972

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { __TS__SparseArray } from "./SparseArray";
2+
import { __TS__Unpack } from "./Unpack";
23

34
export function __TS__SparseArraySpread<T>(this: void, sparseArray: __TS__SparseArray<T>): LuaMultiReturn<T[]> {
4-
return unpack(sparseArray, 1, sparseArray.sparseLength);
5+
return __TS__Unpack(sparseArray, 1, sparseArray.sparseLength);
56
}

src/lualib/5.0/Unpack.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
export const __TS__Unpack = unpack;
1+
/** @noSelfInFile */
2+
3+
// We're not interested in emulating all of the behaviors of unpack() from Lua
4+
// 5.1, just the ones needed by other parts of lualib.
5+
export function __TS__Unpack<T>(list: T[], i: number, j?: number): LuaMultiReturn<T[]> {
6+
if (i === 1 && j === undefined) {
7+
return unpack(list);
8+
} else {
9+
j ??= list.length;
10+
const slice: T[] = [];
11+
for (let n = i; n <= j; n++) {
12+
slice[n - i] = list[n - 1]; // We don't want to add 1 to the index into list.
13+
}
14+
return $multi(...slice);
15+
}
16+
}

src/lualib/declarations/tstl.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ interface LuaClassInstance extends LuaMetatable<any> {
1818
constructor: LuaClass;
1919
}
2020

21-
// Declare unpack for versions that have it instead of table.unpack
22-
declare const unpack: typeof table.unpack | undefined;
23-
2421
// Declare math atan2 for versions that have it instead of math.atan
2522
declare namespace math {
2623
const atan2: typeof atan;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @noSelfInFile */
2+
3+
// Declare unpack for versions that have it instead of table.unpack
4+
declare const unpack: typeof table.unpack | undefined;

0 commit comments

Comments
 (0)