deno fmt
This commit is contained in:
@@ -1,33 +1,33 @@
|
||||
import {
|
||||
jsToLuaValue,
|
||||
LuaBuiltinFunction,
|
||||
LuaTable,
|
||||
luaValueToJS,
|
||||
jsToLuaValue,
|
||||
LuaBuiltinFunction,
|
||||
LuaTable,
|
||||
luaValueToJS,
|
||||
} from "$common/space_lua/runtime.ts";
|
||||
|
||||
export const jsApi = new LuaTable({
|
||||
new: new LuaBuiltinFunction(
|
||||
(constructorFn: any, ...args) => {
|
||||
return new constructorFn(
|
||||
...args.map(luaValueToJS),
|
||||
);
|
||||
},
|
||||
),
|
||||
importModule: new LuaBuiltinFunction((url) => {
|
||||
return import(url);
|
||||
}),
|
||||
/**
|
||||
* Binds a function to an object, so that the function can be called with the object as `this`. Some JS APIs require this.
|
||||
*/
|
||||
bind: new LuaBuiltinFunction((fn: any, obj: any, ...args: any[]) => {
|
||||
return fn.bind(obj, ...args);
|
||||
}),
|
||||
tolua: new LuaBuiltinFunction(jsToLuaValue),
|
||||
tojs: new LuaBuiltinFunction(luaValueToJS),
|
||||
log: new LuaBuiltinFunction((...args) => {
|
||||
console.log(...args);
|
||||
}),
|
||||
// assignGlobal: new LuaBuiltinFunction((name: string, value: any) => {
|
||||
// (globalThis as any)[name] = value;
|
||||
// }),
|
||||
new: new LuaBuiltinFunction(
|
||||
(constructorFn: any, ...args) => {
|
||||
return new constructorFn(
|
||||
...args.map(luaValueToJS),
|
||||
);
|
||||
},
|
||||
),
|
||||
importModule: new LuaBuiltinFunction((url) => {
|
||||
return import(url);
|
||||
}),
|
||||
/**
|
||||
* Binds a function to an object, so that the function can be called with the object as `this`. Some JS APIs require this.
|
||||
*/
|
||||
bind: new LuaBuiltinFunction((fn: any, obj: any, ...args: any[]) => {
|
||||
return fn.bind(obj, ...args);
|
||||
}),
|
||||
tolua: new LuaBuiltinFunction(jsToLuaValue),
|
||||
tojs: new LuaBuiltinFunction(luaValueToJS),
|
||||
log: new LuaBuiltinFunction((...args) => {
|
||||
console.log(...args);
|
||||
}),
|
||||
// assignGlobal: new LuaBuiltinFunction((name: string, value: any) => {
|
||||
// (globalThis as any)[name] = value;
|
||||
// }),
|
||||
});
|
||||
|
||||
@@ -1,103 +1,102 @@
|
||||
import { LuaBuiltinFunction, LuaTable } from "$common/space_lua/runtime.ts";
|
||||
|
||||
export const osApi = new LuaTable({
|
||||
time: new LuaBuiltinFunction((tbl?: LuaTable) => {
|
||||
if (tbl) {
|
||||
// Build a date object from the table
|
||||
const date = new Date();
|
||||
if (!tbl.has("year")) {
|
||||
throw new Error("time(): year is required");
|
||||
}
|
||||
date.setFullYear(tbl.get("year"));
|
||||
if (!tbl.has("month")) {
|
||||
throw new Error("time(): month is required");
|
||||
}
|
||||
date.setMonth(tbl.get("month") - 1);
|
||||
if (!tbl.has("day")) {
|
||||
throw new Error("time(): day is required");
|
||||
}
|
||||
date.setDate(tbl.get("day"));
|
||||
date.setHours(tbl.get("hour") ?? 12);
|
||||
date.setMinutes(tbl.get("min") ?? 0);
|
||||
date.setSeconds(tbl.get("sec") ?? 0);
|
||||
return Math.floor(date.getTime() / 1000);
|
||||
} else {
|
||||
return Math.floor(Date.now() / 1000);
|
||||
}
|
||||
}),
|
||||
/**
|
||||
* Returns a string or a table containing date and time, formatted according to the given string format.
|
||||
* If the time argument is present, this is the time to be formatted (see the os.time function for a description of this value). Otherwise, date formats the current time.
|
||||
* If format starts with '!', then the date is formatted in Coordinated Universal Time. After this optional character, if format is the string "*t", then date returns a table with the following fields: year, month (1–12), day (1–31), hour (0–23), min (0–59), sec (0–61, due to leap seconds), wday (weekday, 1–7, Sunday is 1), yday (day of the year, 1–366), and isdst (daylight saving flag, a boolean). This last field may be absent if the information is not available.
|
||||
* If format is not "*t", then date returns the date as a string, formatted according to the same rules as the ISO C function strftime.
|
||||
* If format is absent, it defaults to "%c", which gives a human-readable date and time representation using the current locale.
|
||||
*/
|
||||
date: new LuaBuiltinFunction((format: string, timestamp?: number) => {
|
||||
const date = timestamp ? new Date(timestamp * 1000) : new Date();
|
||||
time: new LuaBuiltinFunction((tbl?: LuaTable) => {
|
||||
if (tbl) {
|
||||
// Build a date object from the table
|
||||
const date = new Date();
|
||||
if (!tbl.has("year")) {
|
||||
throw new Error("time(): year is required");
|
||||
}
|
||||
date.setFullYear(tbl.get("year"));
|
||||
if (!tbl.has("month")) {
|
||||
throw new Error("time(): month is required");
|
||||
}
|
||||
date.setMonth(tbl.get("month") - 1);
|
||||
if (!tbl.has("day")) {
|
||||
throw new Error("time(): day is required");
|
||||
}
|
||||
date.setDate(tbl.get("day"));
|
||||
date.setHours(tbl.get("hour") ?? 12);
|
||||
date.setMinutes(tbl.get("min") ?? 0);
|
||||
date.setSeconds(tbl.get("sec") ?? 0);
|
||||
return Math.floor(date.getTime() / 1000);
|
||||
} else {
|
||||
return Math.floor(Date.now() / 1000);
|
||||
}
|
||||
}),
|
||||
/**
|
||||
* Returns a string or a table containing date and time, formatted according to the given string format.
|
||||
* If the time argument is present, this is the time to be formatted (see the os.time function for a description of this value). Otherwise, date formats the current time.
|
||||
* If format starts with '!', then the date is formatted in Coordinated Universal Time. After this optional character, if format is the string "*t", then date returns a table with the following fields: year, month (1–12), day (1–31), hour (0–23), min (0–59), sec (0–61, due to leap seconds), wday (weekday, 1–7, Sunday is 1), yday (day of the year, 1–366), and isdst (daylight saving flag, a boolean). This last field may be absent if the information is not available.
|
||||
* If format is not "*t", then date returns the date as a string, formatted according to the same rules as the ISO C function strftime.
|
||||
* If format is absent, it defaults to "%c", which gives a human-readable date and time representation using the current locale.
|
||||
*/
|
||||
date: new LuaBuiltinFunction((format: string, timestamp?: number) => {
|
||||
const date = timestamp ? new Date(timestamp * 1000) : new Date();
|
||||
|
||||
// Default Lua-like format when no format string is provided
|
||||
if (!format) {
|
||||
return date.toDateString() + " " + date.toLocaleTimeString();
|
||||
}
|
||||
// Default Lua-like format when no format string is provided
|
||||
if (!format) {
|
||||
return date.toDateString() + " " + date.toLocaleTimeString();
|
||||
}
|
||||
|
||||
// Define mappings for Lua-style placeholders
|
||||
const formatMap: { [key: string]: () => string } = {
|
||||
// Year
|
||||
"%Y": () => date.getFullYear().toString(),
|
||||
"%y": () => (date.getFullYear() % 100).toString().padStart(2, "0"),
|
||||
// Month
|
||||
"%m": () => (date.getMonth() + 1).toString().padStart(2, "0"),
|
||||
"%b": () => date.toLocaleString("en-US", { month: "short" }),
|
||||
"%B": () => date.toLocaleString("en-US", { month: "long" }),
|
||||
// Day
|
||||
"%d": () => date.getDate().toString().padStart(2, "0"),
|
||||
"%e": () => date.getDate().toString(),
|
||||
// Hour
|
||||
"%H": () => date.getHours().toString().padStart(2, "0"),
|
||||
"%I": () =>
|
||||
(date.getHours() % 12 || 12).toString().padStart(2, "0"),
|
||||
// Minute
|
||||
"%M": () => date.getMinutes().toString().padStart(2, "0"),
|
||||
// Second
|
||||
"%S": () => date.getSeconds().toString().padStart(2, "0"),
|
||||
// AM/PM
|
||||
"%p": () => date.getHours() >= 12 ? "PM" : "AM",
|
||||
// Day of the week
|
||||
"%A": () => date.toLocaleString("en-US", { weekday: "long" }),
|
||||
"%a": () => date.toLocaleString("en-US", { weekday: "short" }),
|
||||
"%w": () => date.getDay().toString(),
|
||||
// Day of the year
|
||||
"%j": () => {
|
||||
const start = new Date(date.getFullYear(), 0, 0);
|
||||
const diff = date.getTime() - start.getTime();
|
||||
const oneDay = 1000 * 60 * 60 * 24;
|
||||
const dayOfYear = Math.floor(diff / oneDay);
|
||||
return dayOfYear.toString().padStart(3, "0");
|
||||
},
|
||||
// Time zone
|
||||
"%Z": () => {
|
||||
const match = date.toTimeString().match(/\((.*)\)/);
|
||||
return match ? match[1] : "";
|
||||
},
|
||||
"%z": () => {
|
||||
const offset = -date.getTimezoneOffset();
|
||||
const sign = offset >= 0 ? "+" : "-";
|
||||
const absOffset = Math.abs(offset);
|
||||
const hours = Math.floor(absOffset / 60).toString().padStart(
|
||||
2,
|
||||
"0",
|
||||
);
|
||||
const minutes = (absOffset % 60).toString().padStart(2, "0");
|
||||
return `${sign}${hours}${minutes}`;
|
||||
},
|
||||
// Literal %
|
||||
"%%": () => "%",
|
||||
};
|
||||
// Define mappings for Lua-style placeholders
|
||||
const formatMap: { [key: string]: () => string } = {
|
||||
// Year
|
||||
"%Y": () => date.getFullYear().toString(),
|
||||
"%y": () => (date.getFullYear() % 100).toString().padStart(2, "0"),
|
||||
// Month
|
||||
"%m": () => (date.getMonth() + 1).toString().padStart(2, "0"),
|
||||
"%b": () => date.toLocaleString("en-US", { month: "short" }),
|
||||
"%B": () => date.toLocaleString("en-US", { month: "long" }),
|
||||
// Day
|
||||
"%d": () => date.getDate().toString().padStart(2, "0"),
|
||||
"%e": () => date.getDate().toString(),
|
||||
// Hour
|
||||
"%H": () => date.getHours().toString().padStart(2, "0"),
|
||||
"%I": () => (date.getHours() % 12 || 12).toString().padStart(2, "0"),
|
||||
// Minute
|
||||
"%M": () => date.getMinutes().toString().padStart(2, "0"),
|
||||
// Second
|
||||
"%S": () => date.getSeconds().toString().padStart(2, "0"),
|
||||
// AM/PM
|
||||
"%p": () => date.getHours() >= 12 ? "PM" : "AM",
|
||||
// Day of the week
|
||||
"%A": () => date.toLocaleString("en-US", { weekday: "long" }),
|
||||
"%a": () => date.toLocaleString("en-US", { weekday: "short" }),
|
||||
"%w": () => date.getDay().toString(),
|
||||
// Day of the year
|
||||
"%j": () => {
|
||||
const start = new Date(date.getFullYear(), 0, 0);
|
||||
const diff = date.getTime() - start.getTime();
|
||||
const oneDay = 1000 * 60 * 60 * 24;
|
||||
const dayOfYear = Math.floor(diff / oneDay);
|
||||
return dayOfYear.toString().padStart(3, "0");
|
||||
},
|
||||
// Time zone
|
||||
"%Z": () => {
|
||||
const match = date.toTimeString().match(/\((.*)\)/);
|
||||
return match ? match[1] : "";
|
||||
},
|
||||
"%z": () => {
|
||||
const offset = -date.getTimezoneOffset();
|
||||
const sign = offset >= 0 ? "+" : "-";
|
||||
const absOffset = Math.abs(offset);
|
||||
const hours = Math.floor(absOffset / 60).toString().padStart(
|
||||
2,
|
||||
"0",
|
||||
);
|
||||
const minutes = (absOffset % 60).toString().padStart(2, "0");
|
||||
return `${sign}${hours}${minutes}`;
|
||||
},
|
||||
// Literal %
|
||||
"%%": () => "%",
|
||||
};
|
||||
|
||||
// Replace format placeholders with corresponding values
|
||||
return format.replace(/%[A-Za-z%]/g, (match) => {
|
||||
const formatter = formatMap[match];
|
||||
return formatter ? formatter() : match;
|
||||
});
|
||||
}),
|
||||
// Replace format placeholders with corresponding values
|
||||
return format.replace(/%[A-Za-z%]/g, (match) => {
|
||||
const formatter = formatMap[match];
|
||||
return formatter ? formatter() : match;
|
||||
});
|
||||
}),
|
||||
});
|
||||
|
||||
+100
-100
@@ -1,106 +1,106 @@
|
||||
import {
|
||||
LuaBuiltinFunction,
|
||||
LuaMultiRes,
|
||||
LuaTable,
|
||||
luaToString,
|
||||
LuaBuiltinFunction,
|
||||
LuaMultiRes,
|
||||
LuaTable,
|
||||
luaToString,
|
||||
} from "$common/space_lua/runtime.ts";
|
||||
|
||||
export const stringApi = new LuaTable({
|
||||
byte: new LuaBuiltinFunction((s: string, i?: number, j?: number) => {
|
||||
i = i ?? 1;
|
||||
j = j ?? i;
|
||||
const result = [];
|
||||
for (let k = i; k <= j; k++) {
|
||||
result.push(s.charCodeAt(k - 1));
|
||||
byte: new LuaBuiltinFunction((s: string, i?: number, j?: number) => {
|
||||
i = i ?? 1;
|
||||
j = j ?? i;
|
||||
const result = [];
|
||||
for (let k = i; k <= j; k++) {
|
||||
result.push(s.charCodeAt(k - 1));
|
||||
}
|
||||
return new LuaMultiRes(result);
|
||||
}),
|
||||
char: new LuaBuiltinFunction((...args: number[]) => {
|
||||
return String.fromCharCode(...args);
|
||||
}),
|
||||
find: new LuaBuiltinFunction(
|
||||
(s: string, pattern: string, init?: number, plain?: boolean) => {
|
||||
init = init ?? 1;
|
||||
plain = plain ?? false;
|
||||
const result = s.slice(init - 1).match(pattern);
|
||||
if (!result) {
|
||||
return new LuaMultiRes([]);
|
||||
}
|
||||
return new LuaMultiRes([
|
||||
result.index! + 1,
|
||||
result.index! + result[0].length,
|
||||
]);
|
||||
},
|
||||
),
|
||||
format: new LuaBuiltinFunction((format: string, ...args: any[]) => {
|
||||
return format.replace(/%./g, (match) => {
|
||||
switch (match) {
|
||||
case "%s":
|
||||
return luaToString(args.shift());
|
||||
case "%d":
|
||||
return String(args.shift());
|
||||
default:
|
||||
return match;
|
||||
}
|
||||
});
|
||||
}),
|
||||
gmatch: new LuaBuiltinFunction((s: string, pattern: string) => {
|
||||
const regex = new RegExp(pattern, "g");
|
||||
return () => {
|
||||
const result = regex.exec(s);
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
return new LuaMultiRes(result.slice(1));
|
||||
};
|
||||
}),
|
||||
gsub: new LuaBuiltinFunction(
|
||||
(s: string, pattern: string, repl: string, n?: number) => {
|
||||
n = n ?? Infinity;
|
||||
const regex = new RegExp(pattern, "g");
|
||||
let result = s;
|
||||
let match: RegExpExecArray | null;
|
||||
for (let i = 0; i < n; i++) {
|
||||
match = regex.exec(result);
|
||||
if (!match) {
|
||||
break;
|
||||
}
|
||||
return new LuaMultiRes(result);
|
||||
}),
|
||||
char: new LuaBuiltinFunction((...args: number[]) => {
|
||||
return String.fromCharCode(...args);
|
||||
}),
|
||||
find: new LuaBuiltinFunction(
|
||||
(s: string, pattern: string, init?: number, plain?: boolean) => {
|
||||
init = init ?? 1;
|
||||
plain = plain ?? false;
|
||||
const result = s.slice(init - 1).match(pattern);
|
||||
if (!result) {
|
||||
return new LuaMultiRes([]);
|
||||
}
|
||||
return new LuaMultiRes([
|
||||
result.index! + 1,
|
||||
result.index! + result[0].length,
|
||||
]);
|
||||
},
|
||||
),
|
||||
format: new LuaBuiltinFunction((format: string, ...args: any[]) => {
|
||||
return format.replace(/%./g, (match) => {
|
||||
switch (match) {
|
||||
case "%s":
|
||||
return luaToString(args.shift());
|
||||
case "%d":
|
||||
return String(args.shift());
|
||||
default:
|
||||
return match;
|
||||
}
|
||||
});
|
||||
}),
|
||||
gmatch: new LuaBuiltinFunction((s: string, pattern: string) => {
|
||||
const regex = new RegExp(pattern, "g");
|
||||
return () => {
|
||||
const result = regex.exec(s);
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
return new LuaMultiRes(result.slice(1));
|
||||
};
|
||||
}),
|
||||
gsub: new LuaBuiltinFunction(
|
||||
(s: string, pattern: string, repl: string, n?: number) => {
|
||||
n = n ?? Infinity;
|
||||
const regex = new RegExp(pattern, "g");
|
||||
let result = s;
|
||||
let match: RegExpExecArray | null;
|
||||
for (let i = 0; i < n; i++) {
|
||||
match = regex.exec(result);
|
||||
if (!match) {
|
||||
break;
|
||||
}
|
||||
result = result.replace(match[0], repl);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
),
|
||||
len: new LuaBuiltinFunction((s: string) => {
|
||||
return s.length;
|
||||
}),
|
||||
lower: new LuaBuiltinFunction((s: string) => {
|
||||
return luaToString(s.toLowerCase());
|
||||
}),
|
||||
upper: new LuaBuiltinFunction((s: string) => {
|
||||
return luaToString(s.toUpperCase());
|
||||
}),
|
||||
match: new LuaBuiltinFunction(
|
||||
(s: string, pattern: string, init?: number) => {
|
||||
init = init ?? 1;
|
||||
const result = s.slice(init - 1).match(pattern);
|
||||
if (!result) {
|
||||
return new LuaMultiRes([]);
|
||||
}
|
||||
return new LuaMultiRes(result.slice(1));
|
||||
},
|
||||
),
|
||||
rep: new LuaBuiltinFunction((s: string, n: number, sep?: string) => {
|
||||
sep = sep ?? "";
|
||||
return s.repeat(n) + sep;
|
||||
}),
|
||||
reverse: new LuaBuiltinFunction((s: string) => {
|
||||
return s.split("").reverse().join("");
|
||||
}),
|
||||
sub: new LuaBuiltinFunction((s: string, i: number, j?: number) => {
|
||||
j = j ?? s.length;
|
||||
return s.slice(i - 1, j);
|
||||
}),
|
||||
split: new LuaBuiltinFunction((s: string, sep: string) => {
|
||||
return s.split(sep);
|
||||
}),
|
||||
result = result.replace(match[0], repl);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
),
|
||||
len: new LuaBuiltinFunction((s: string) => {
|
||||
return s.length;
|
||||
}),
|
||||
lower: new LuaBuiltinFunction((s: string) => {
|
||||
return luaToString(s.toLowerCase());
|
||||
}),
|
||||
upper: new LuaBuiltinFunction((s: string) => {
|
||||
return luaToString(s.toUpperCase());
|
||||
}),
|
||||
match: new LuaBuiltinFunction(
|
||||
(s: string, pattern: string, init?: number) => {
|
||||
init = init ?? 1;
|
||||
const result = s.slice(init - 1).match(pattern);
|
||||
if (!result) {
|
||||
return new LuaMultiRes([]);
|
||||
}
|
||||
return new LuaMultiRes(result.slice(1));
|
||||
},
|
||||
),
|
||||
rep: new LuaBuiltinFunction((s: string, n: number, sep?: string) => {
|
||||
sep = sep ?? "";
|
||||
return s.repeat(n) + sep;
|
||||
}),
|
||||
reverse: new LuaBuiltinFunction((s: string) => {
|
||||
return s.split("").reverse().join("");
|
||||
}),
|
||||
sub: new LuaBuiltinFunction((s: string, i: number, j?: number) => {
|
||||
j = j ?? s.length;
|
||||
return s.slice(i - 1, j);
|
||||
}),
|
||||
split: new LuaBuiltinFunction((s: string, sep: string) => {
|
||||
return s.split(sep);
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
import {
|
||||
type ILuaFunction,
|
||||
LuaBuiltinFunction,
|
||||
LuaTable,
|
||||
type ILuaFunction,
|
||||
LuaBuiltinFunction,
|
||||
LuaTable,
|
||||
} from "$common/space_lua/runtime.ts";
|
||||
|
||||
export const tableApi = new LuaTable({
|
||||
concat: new LuaBuiltinFunction(
|
||||
(tbl: LuaTable, sep?: string, i?: number, j?: number) => {
|
||||
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);
|
||||
},
|
||||
),
|
||||
insert: new LuaBuiltinFunction(
|
||||
(tbl: LuaTable, posOrValue: number | any, value?: any) => {
|
||||
if (value === undefined) {
|
||||
value = posOrValue;
|
||||
posOrValue = tbl.length + 1;
|
||||
}
|
||||
tbl.insert(posOrValue, value);
|
||||
},
|
||||
),
|
||||
remove: new LuaBuiltinFunction((tbl: LuaTable, pos?: number) => {
|
||||
pos = pos ?? tbl.length;
|
||||
tbl.remove(pos);
|
||||
}),
|
||||
sort: new LuaBuiltinFunction((tbl: LuaTable, comp?: ILuaFunction) => {
|
||||
return tbl.sort(comp);
|
||||
}),
|
||||
concat: new LuaBuiltinFunction(
|
||||
(tbl: LuaTable, sep?: string, i?: number, j?: number) => {
|
||||
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);
|
||||
},
|
||||
),
|
||||
insert: new LuaBuiltinFunction(
|
||||
(tbl: LuaTable, posOrValue: number | any, value?: any) => {
|
||||
if (value === undefined) {
|
||||
value = posOrValue;
|
||||
posOrValue = tbl.length + 1;
|
||||
}
|
||||
tbl.insert(posOrValue, value);
|
||||
},
|
||||
),
|
||||
remove: new LuaBuiltinFunction((tbl: LuaTable, pos?: number) => {
|
||||
pos = pos ?? tbl.length;
|
||||
tbl.remove(pos);
|
||||
}),
|
||||
sort: new LuaBuiltinFunction((tbl: LuaTable, comp?: ILuaFunction) => {
|
||||
return tbl.sort(comp);
|
||||
}),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user