Skip to content

Commit c386d53

Browse files
committed
Optimize lualib functions
1 parent 8565af8 commit c386d53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+356
-290
lines changed

src/LuaLib.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export enum LuaLibFeature {
1919
ArrayReduce = "ArrayReduce",
2020
ArrayReduceRight = "ArrayReduceRight",
2121
ArrayReverse = "ArrayReverse",
22-
ArrayShift = "ArrayShift",
2322
ArrayUnshift = "ArrayUnshift",
2423
ArraySort = "ArraySort",
2524
ArraySlice = "ArraySlice",
@@ -79,7 +78,6 @@ export enum LuaLibFeature {
7978
StringAccess = "StringAccess",
8079
StringCharAt = "StringCharAt",
8180
StringCharCodeAt = "StringCharCodeAt",
82-
StringConcat = "StringConcat",
8381
StringEndsWith = "StringEndsWith",
8482
StringIncludes = "StringIncludes",
8583
StringPadEnd = "StringPadEnd",

src/lualib/ArrayConcat.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
export function __TS__ArrayConcat(this: void, arr1: any[], ...args: any[]): any[] {
2-
const out: any[] = [];
3-
for (const val of arr1) {
4-
out[out.length] = val;
1+
export function __TS__ArrayConcat<T>(this: T[], ...items: Array<T | T[]>): T[] {
2+
const result: T[] = [];
3+
let len = 0;
4+
for (const i of $range(1, this.length)) {
5+
len++;
6+
result[len - 1] = this[i - 1];
57
}
6-
for (const arg of args) {
7-
if (Array.isArray(arg)) {
8-
const argAsArray = arg;
9-
for (const val of argAsArray) {
10-
out[out.length] = val;
8+
for (const i of $range(1, items.length)) {
9+
const item = items[i - 1];
10+
if (Array.isArray(item)) {
11+
for (const j of $range(1, item.length)) {
12+
len++;
13+
result[len - 1] = item[j - 1];
1114
}
1215
} else {
13-
out[out.length] = arg;
16+
len++;
17+
result[len - 1] = item;
1418
}
1519
}
1620

17-
return out;
21+
return result;
1822
}

src/lualib/ArrayEvery.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export function __TS__ArrayEvery<T>(
2-
this: void,
3-
arr: T[],
4-
callbackfn: (value: T, index?: number, array?: any[]) => boolean
2+
this: T[],
3+
callbackfn: (value: T, index?: number, array?: any[]) => boolean,
4+
thisArg?: any
55
): boolean {
6-
for (let i = 0; i < arr.length; i++) {
7-
if (!callbackfn(arr[i], i, arr)) {
6+
for (const i of $range(1, this.length)) {
7+
if (!callbackfn.call(thisArg, this[i - 1], i - 1, this)) {
88
return false;
99
}
1010
}

src/lualib/ArrayFilter.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
export function __TS__ArrayFilter<T>(
2-
this: void,
3-
arr: T[],
4-
callbackfn: (value: T, index?: number, array?: any[]) => boolean
2+
this: T[],
3+
callbackfn: (value: T, index?: number, array?: any[]) => boolean,
4+
thisArg?: any
55
): T[] {
66
const result: T[] = [];
7-
for (let i = 0; i < arr.length; i++) {
8-
if (callbackfn(arr[i], i, arr)) {
9-
result[result.length] = arr[i];
7+
let len = 0;
8+
for (const i of $range(1, this.length)) {
9+
if (callbackfn.call(thisArg, this[i - 1], i - 1, this)) {
10+
len++;
11+
result[len - 1] = this[i - 1];
1012
}
1113
}
1214
return result;

src/lualib/ArrayFind.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
// https://www.ecma-international.org/ecma-262/10.0/index.html#sec-array.prototype.find
22
export function __TS__ArrayFind<T>(
3-
this: void,
4-
arr: T[],
5-
predicate: (value: T, index: number, obj: T[]) => unknown
3+
this: T[],
4+
predicate: (value: T, index: number, obj: T[]) => unknown,
5+
thisArg?: any
66
): T | undefined {
7-
const len = arr.length;
8-
let k = 0;
9-
while (k < len) {
10-
const elem = arr[k];
11-
if (predicate(elem, k, arr)) {
7+
for (const i of $range(1, this.length)) {
8+
const elem = this[i - 1];
9+
if (predicate.call(thisArg, elem, i - 1, this)) {
1210
return elem;
1311
}
14-
k += 1;
1512
}
1613

1714
return undefined;

src/lualib/ArrayFindIndex.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
export function __TS__ArrayFindIndex<T>(
2-
this: void,
3-
arr: T[],
4-
callbackFn: (element: T, index?: number, array?: T[]) => boolean
2+
this: T[],
3+
callbackFn: (element: T, index?: number, array?: T[]) => boolean,
4+
thisArg?: any
55
): number {
6-
for (let i = 0, len = arr.length; i < len; i++) {
7-
if (callbackFn(arr[i], i, arr)) {
8-
return i;
6+
for (const i of $range(1, this.length)) {
7+
if (callbackFn.call(thisArg, this[i - 1], i - 1, this)) {
8+
return i - 1;
99
}
1010
}
1111
return -1;

src/lualib/ArrayFlat.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
export function __TS__ArrayFlat(this: void, array: any[], depth = 1): any[] {
2-
let result: any[] = [];
3-
for (const value of array) {
1+
export function __TS__ArrayFlat(this: any[], depth = 1): any[] {
2+
const result: any[] = [];
3+
let len = 0;
4+
for (const i of $range(1, this.length)) {
5+
const value = this[i - 1];
46
if (depth > 0 && Array.isArray(value)) {
5-
result = result.concat(__TS__ArrayFlat(value, depth - 1));
7+
let toAdd: any[];
8+
if (depth === 1) {
9+
toAdd = value;
10+
} else {
11+
toAdd = value.flat(depth - 1);
12+
}
13+
for (const j of $range(1, toAdd.length)) {
14+
const val = toAdd[j - 1];
15+
len++;
16+
result[len - 1] = val;
17+
}
618
} else {
7-
result[result.length] = value;
19+
len++;
20+
result[len - 1] = value;
821
}
922
}
1023

src/lualib/ArrayFlatMap.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
export function __TS__ArrayFlatMap<T, U>(
2-
this: void,
3-
array: T[],
4-
callback: (value: T, index: number, array: T[]) => U | readonly U[]
2+
this: T[],
3+
callback: (value: T, index: number, array: T[]) => U | readonly U[],
4+
thisArg?: any
55
): U[] {
6-
let result: U[] = [];
7-
for (let i = 0; i < array.length; i++) {
8-
const value = callback(array[i], i, array);
9-
if (type(value) === "table" && Array.isArray(value)) {
10-
result = result.concat(value);
6+
const result: U[] = [];
7+
let len = 0;
8+
for (const i of $range(1, this.length)) {
9+
const value = callback.call(thisArg, this[i - 1], i - 1, this);
10+
if (Array.isArray(value)) {
11+
for (const j of $range(1, value.length)) {
12+
len++;
13+
result[len - 1] = value[j - 1];
14+
}
1115
} else {
12-
result[result.length] = value as U;
16+
len++;
17+
result[len - 1] = value as U;
1318
}
1419
}
1520

src/lualib/ArrayForEach.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export function __TS__ArrayForEach<T>(
2-
this: void,
3-
arr: T[],
4-
callbackFn: (value: T, index?: number, array?: any[]) => any
2+
this: T[],
3+
callbackFn: (value: T, index?: number, array?: any[]) => any,
4+
thisArg?: any
55
): void {
6-
for (let i = 0; i < arr.length; i++) {
7-
callbackFn(arr[i], i, arr);
6+
for (const i of $range(1, this.length)) {
7+
callbackFn.call(thisArg, this[i - 1], i - 1, this);
88
}
99
}

src/lualib/ArrayIncludes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export function __TS__ArrayIncludes<T>(this: T[], searchElement: T, fromIndex =
1111
k = 0;
1212
}
1313

14-
for (const i of $range(k, len)) {
15-
if (this[i] === searchElement) {
14+
for (const i of $range(k + 1, len)) {
15+
if (this[i - 1] === searchElement) {
1616
return true;
1717
}
1818
}

0 commit comments

Comments
 (0)