2024-10-10 02:35:07 +08:00
|
|
|
import {
|
2024-10-11 21:34:27 +08:00
|
|
|
type ILuaFunction,
|
|
|
|
LuaBuiltinFunction,
|
2025-01-14 03:25:39 +08:00
|
|
|
luaEquals,
|
|
|
|
LuaRuntimeError,
|
2024-10-11 21:34:27 +08:00
|
|
|
LuaTable,
|
2025-01-14 03:25:39 +08:00
|
|
|
type LuaValue,
|
2024-10-10 02:35:07 +08:00
|
|
|
} from "$common/space_lua/runtime.ts";
|
|
|
|
|
|
|
|
export const tableApi = new LuaTable({
|
2025-01-16 03:47:58 +08:00
|
|
|
/**
|
|
|
|
* Concatenates the elements of a table into a string, using a separator.
|
|
|
|
* @param tbl - The table to concatenate.
|
|
|
|
* @param sep - The separator to use between elements.
|
|
|
|
* @param i - The start index.
|
|
|
|
* @param j - The end index.
|
|
|
|
* @returns The concatenated string.
|
|
|
|
*/
|
2024-10-11 21:34:27 +08:00
|
|
|
concat: new LuaBuiltinFunction(
|
2024-10-20 21:06:23 +08:00
|
|
|
(_sf, tbl: LuaTable, sep?: string, i?: number, j?: number) => {
|
2024-10-11 21:34:27 +08:00
|
|
|
sep = sep ?? "";
|
|
|
|
i = i ?? 1;
|
|
|
|
j = j ?? tbl.length;
|
|
|
|
const result = [];
|
|
|
|
for (let k = i; k <= j; k++) {
|
|
|
|
result.push(tbl.get(k));
|
|
|
|
}
|
|
|
|
return result.join(sep);
|
|
|
|
},
|
|
|
|
),
|
2025-01-16 03:47:58 +08:00
|
|
|
/**
|
|
|
|
* Inserts an element into a table at a specified position.
|
|
|
|
* @param tbl - The table to insert the element into.
|
|
|
|
* @param posOrValue - The position or value to insert.
|
|
|
|
* @param value - The value to insert.
|
|
|
|
*/
|
2024-10-11 21:34:27 +08:00
|
|
|
insert: new LuaBuiltinFunction(
|
2024-10-20 21:06:23 +08:00
|
|
|
(_sf, tbl: LuaTable, posOrValue: number | any, value?: any) => {
|
2024-10-11 21:34:27 +08:00
|
|
|
if (value === undefined) {
|
2025-01-08 18:12:26 +08:00
|
|
|
let pos = 1;
|
|
|
|
while (tbl.get(pos) !== null) {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
tbl.set(pos, posOrValue);
|
|
|
|
} else {
|
|
|
|
tbl.insert(posOrValue, value);
|
2024-10-11 21:34:27 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2025-01-16 03:47:58 +08:00
|
|
|
/**
|
|
|
|
* Removes an element from a table at a specified position.
|
|
|
|
* @param tbl - The table to remove the element from.
|
|
|
|
* @param pos - The position of the element to remove.
|
|
|
|
*/
|
2024-10-20 21:06:23 +08:00
|
|
|
remove: new LuaBuiltinFunction((_sf, tbl: LuaTable, pos?: number) => {
|
2024-10-11 21:34:27 +08:00
|
|
|
pos = pos ?? tbl.length;
|
|
|
|
tbl.remove(pos);
|
|
|
|
}),
|
2025-01-16 03:47:58 +08:00
|
|
|
/**
|
|
|
|
* Sorts a table.
|
|
|
|
* @param tbl - The table to sort.
|
|
|
|
* @param comp - The comparison function.
|
|
|
|
* @returns The sorted table.
|
|
|
|
*/
|
2024-10-20 21:06:23 +08:00
|
|
|
sort: new LuaBuiltinFunction((sf, tbl: LuaTable, comp?: ILuaFunction) => {
|
|
|
|
return tbl.sort(comp, sf);
|
2024-10-11 21:34:27 +08:00
|
|
|
}),
|
2025-01-16 03:47:58 +08:00
|
|
|
/**
|
|
|
|
* Returns the keys of a table.
|
|
|
|
* @param tbl - The table to get the keys from.
|
|
|
|
* @returns The keys of the table.
|
|
|
|
*/
|
2025-01-12 23:54:04 +08:00
|
|
|
keys: new LuaBuiltinFunction((_sf, tbl: LuaTable) => {
|
|
|
|
return tbl.keys();
|
|
|
|
}),
|
2025-01-16 03:47:58 +08:00
|
|
|
/**
|
|
|
|
* Checks if a table (used as an array) contains a value.
|
|
|
|
* @param tbl - The table to check.
|
|
|
|
* @param value - The value to check for.
|
|
|
|
* @returns True if the value is in the table, false otherwise.
|
|
|
|
*/
|
2025-01-14 03:25:39 +08:00
|
|
|
includes: new LuaBuiltinFunction(
|
|
|
|
(sf, tbl: LuaTable | Record<string, any>, value: LuaValue) => {
|
2025-01-16 22:33:18 +08:00
|
|
|
if (!tbl) {
|
|
|
|
return false;
|
|
|
|
}
|
2025-01-14 03:25:39 +08:00
|
|
|
if (tbl instanceof LuaTable) {
|
|
|
|
// Iterate over the table
|
|
|
|
for (const key of tbl.keys()) {
|
|
|
|
if (luaEquals(tbl.get(key), value)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} else if (Array.isArray(tbl)) {
|
|
|
|
return !!tbl.find((item) => luaEquals(item, value));
|
|
|
|
} else {
|
|
|
|
throw new LuaRuntimeError(
|
|
|
|
`Cannot use includes on a non-table or non-array value`,
|
|
|
|
sf,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2024-10-10 02:35:07 +08:00
|
|
|
});
|