Skip to content

Commit 45f6078

Browse files
authored
Implement array.fill (#1497)
1 parent b92cd0c commit 45f6078

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export enum LuaLibFeature {
99
ArrayConcat = "ArrayConcat",
1010
ArrayEntries = "ArrayEntries",
1111
ArrayEvery = "ArrayEvery",
12+
ArrayFill = "ArrayFill",
1213
ArrayFilter = "ArrayFilter",
1314
ArrayForEach = "ArrayForEach",
1415
ArrayFind = "ArrayFind",

src/lualib/ArrayFill.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.fill
2+
export function __TS__ArrayFill<T>(this: T[], value: T, start?: number, end?: number): T[] {
3+
let relativeStart = start ?? 0;
4+
let relativeEnd = end ?? this.length;
5+
6+
if (relativeStart < 0) {
7+
relativeStart += this.length;
8+
}
9+
10+
if (relativeEnd < 0) {
11+
relativeEnd += this.length;
12+
}
13+
14+
for (let i = relativeStart; i < relativeEnd; i++) {
15+
this[i] = value;
16+
}
17+
18+
return this;
19+
}

src/transformation/builtins/array.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ export function transformArrayPrototypeCall(
9494
return transformLuaLibFunction(context, LuaLibFeature.ArrayConcat, node, caller, ...params);
9595
case "entries":
9696
return transformLuaLibFunction(context, LuaLibFeature.ArrayEntries, node, caller);
97+
case "fill":
98+
return transformLuaLibFunction(context, LuaLibFeature.ArrayFill, node, caller, ...params);
9799
case "push":
98100
if (node.arguments.length === 1) {
99101
const param = params[0] ?? lua.createNilLiteral();

test/unit/builtins/array.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,33 @@ test.each([
788788
util.testExpression(literal).expectToHaveNoDiagnostics();
789789
});
790790

791+
describe("array.fill", () => {
792+
test.each(["[]", "[1]", "[1,2,3,4]"])("Fills full length of array without other parameters (%p)", arr => {
793+
util.testExpression`${arr}.fill(5)`.expectToMatchJsResult();
794+
});
795+
796+
test.each(["[1,2,3]", "[1,2,3,4,5,6]"])("Fills starting from start parameter (%p)", arr => {
797+
util.testExpression`${arr}.fill(5, 3)`.expectToMatchJsResult();
798+
});
799+
800+
test("handles negative start parameter", () => {
801+
util.testExpression`[1,2,3,4,5,6,7].fill(8, -3)`.expectToMatchJsResult();
802+
});
803+
804+
test("handles negative end parameter", () => {
805+
util.testExpression`[1,2,3,4,5,6,7].fill(8, -5, -2)`.expectToMatchJsResult();
806+
});
807+
808+
test("Fills starting from start parameter, up to ending parameter", () => {
809+
util.testExpression`[1,2,3,4,5,6,7,8].fill(5, 2, 6)`.expectToMatchJsResult();
810+
});
811+
812+
// NOTE: This is different from the default ECMAScript specification for the behavior, but for Lua this is much more useful
813+
test("Extends size of the array if ending size is larger than array", () => {
814+
util.testExpression`[1,2,3].fill(5, 0, 6)`.expectToEqual([5, 5, 5, 5, 5, 5]);
815+
});
816+
});
817+
791818
// Issue #1218: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1218
792819
test.each(["[1, 2, 3]", "undefined"])("prototype call on nullable array (%p)", value => {
793820
util.testFunction`

0 commit comments

Comments
 (0)