refactor: flatten documented callback metadata

This commit is contained in:
Zef Hemel
2026-07-16 09:12:29 +02:00
parent 37d6c59555
commit 2020fbb16d
19 changed files with 1120 additions and 1373 deletions
+160 -224
View File
@@ -45,14 +45,10 @@ export const mathApi = new LuaTable({
}
return null;
},
documentation: {
description:
"Returns `integer` or `float` for a number, or `nil` for other values.",
parameters: [{ name: "x", description: "Value to inspect." }],
returns: [
{ type: "string|nil", description: "Numeric subtype or `nil`." },
],
},
description:
"Returns `integer` or `float` for a number, or `nil` for other values.",
parameters: [{ name: "x", description: "Value to inspect." }],
returns: [{ type: "string|nil", description: "Numeric subtype or `nil`." }],
}),
/**
@@ -77,14 +73,12 @@ export const mathApi = new LuaTable({
}
return null;
},
documentation: {
description:
"Converts a value to an integer when it has an exact finite integral representation.",
parameters: [{ name: "x", description: "Value to convert." }],
returns: [
{ type: "integer|nil", description: "Converted integer or `nil`." },
],
},
description:
"Converts a value to an integer when it has an exact finite integral representation.",
parameters: [{ name: "x", description: "Value to convert." }],
returns: [
{ type: "integer|nil", description: "Converted integer or `nil`." },
],
}),
/**
@@ -106,25 +100,23 @@ export const mathApi = new LuaTable({
throw new LuaRuntimeError(e.message, _sf);
}
},
documentation: {
description:
"Returns a pseudo-random float or an integer in a requested inclusive range.",
signatures: [
"math.random(): number",
"math.random(n): integer",
"math.random(m, n): integer",
],
parameters: [
{ name: "m", type: "integer", optional: true },
{ name: "n", type: "integer", optional: true },
],
returns: [{ type: "number", description: "Pseudo-random result." }],
examples: [
{
code: "print(math.random())\nprint(math.random(10))\nprint(math.random(5, 10))",
},
],
},
description:
"Returns a pseudo-random float or an integer in a requested inclusive range.",
signatures: [
"math.random(): number",
"math.random(n): integer",
"math.random(m, n): integer",
],
parameters: [
{ name: "m", type: "integer", optional: true },
{ name: "n", type: "integer", optional: true },
],
returns: [{ type: "number", description: "Pseudo-random result." }],
examples: [
{
code: "print(math.random())\nprint(math.random(10))\nprint(math.random(5, 10))",
},
],
}),
/**
* Seeds the pseudo-random generator. With no arguments, uses a
@@ -137,78 +129,64 @@ export const mathApi = new LuaTable({
const [s1, s2] = prng.randomseed(x, y);
return new LuaMultiRes([s1, s2]);
},
documentation: {
description:
"Seeds the pseudo-random generator and returns the two seeds used.",
signatures: [
"math.randomseed(): integer, integer",
"math.randomseed(x, y): integer, integer",
],
parameters: [
{ name: "x", type: "integer", optional: true },
{ name: "y", type: "integer", optional: true },
],
returns: [
{ type: "integer", description: "First seed." },
{ type: "integer", description: "Second seed." },
],
},
description:
"Seeds the pseudo-random generator and returns the two seeds used.",
signatures: [
"math.randomseed(): integer, integer",
"math.randomseed(x, y): integer, integer",
],
parameters: [
{ name: "x", type: "integer", optional: true },
{ name: "y", type: "integer", optional: true },
],
returns: [
{ type: "integer", description: "First seed." },
{ type: "integer", description: "Second seed." },
],
}),
// Basic functions
abs: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.abs(untagNumber(x)),
documentation: {
description: "Returns the absolute value of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
},
description: "Returns the absolute value of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
}),
ceil: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.ceil(untagNumber(x)),
documentation: {
description: "Returns the smallest integer greater than or equal to `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "integer" }],
},
description: "Returns the smallest integer greater than or equal to `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "integer" }],
}),
floor: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.floor(untagNumber(x)),
documentation: {
description: "Returns the largest integer less than or equal to `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "integer" }],
},
description: "Returns the largest integer less than or equal to `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "integer" }],
}),
max: new LuaBuiltinFunction({
callback: (_sf, ...args: number[]) => Math.max(...args.map(untagNumber)),
documentation: {
description: "Returns the greatest of its arguments.",
signatures: ["math.max(x, ...): number"],
returns: [{ type: "number" }],
},
description: "Returns the greatest of its arguments.",
signatures: ["math.max(x, ...): number"],
returns: [{ type: "number" }],
}),
min: new LuaBuiltinFunction({
callback: (_sf, ...args: number[]) => Math.min(...args.map(untagNumber)),
documentation: {
description: "Returns the least of its arguments.",
signatures: ["math.min(x, ...): number"],
returns: [{ type: "number" }],
},
description: "Returns the least of its arguments.",
signatures: ["math.min(x, ...): number"],
returns: [{ type: "number" }],
}),
// Rounding and remainder
fmod: new LuaBuiltinFunction({
callback: (_sf, x: number, y: number) => untagNumber(x) % untagNumber(y),
documentation: {
description:
"Returns the remainder of `x / y` with the quotient rounded toward zero.",
parameters: [
{ name: "x", type: "number" },
{ name: "y", type: "number" },
],
returns: [{ type: "number" }],
},
description:
"Returns the remainder of `x / y` with the quotient rounded toward zero.",
parameters: [
{ name: "x", type: "number" },
{ name: "y", type: "number" },
],
returns: [{ type: "number" }],
}),
modf: new LuaBuiltinFunction({
callback: (_sf, x: number) => {
@@ -218,15 +196,13 @@ export const mathApi = new LuaTable({
const frac = makeLuaFloat(xn - int);
return new LuaMultiRes([int, frac]);
},
documentation: {
description: "Splits `x` into its integral and fractional parts.",
parameters: [{ name: "x", type: "number" }],
returns: [
{ type: "integer", description: "Integral part." },
{ type: "float", description: "Fractional part." },
],
examples: [{ code: "local integer, fraction = math.modf(3.14)" }],
},
description: "Splits `x` into its integral and fractional parts.",
parameters: [{ name: "x", type: "number" }],
returns: [
{ type: "integer", description: "Integral part." },
{ type: "float", description: "Fractional part." },
],
examples: [{ code: "local integer, fraction = math.modf(3.14)" }],
}),
// Returns m and e such that x = m * 2^e, 0.5 <= |m| < 1 (or m=0 when x=0).
@@ -251,39 +227,33 @@ export const mathApi = new LuaTable({
}
return new LuaMultiRes([m, e]);
},
documentation: {
description:
"Decomposes `x` into a normalized fraction and a power-of-two exponent.",
parameters: [{ name: "x", type: "number" }],
returns: [
{ type: "number", description: "Fraction." },
{ type: "integer", description: "Exponent." },
],
},
description:
"Decomposes `x` into a normalized fraction and a power-of-two exponent.",
parameters: [{ name: "x", type: "number" }],
returns: [
{ type: "number", description: "Fraction." },
{ type: "integer", description: "Exponent." },
],
}),
// Returns m * 2^e (the inverse of frexp). Mirrors C99/Lua.
ldexp: new LuaBuiltinFunction({
callback: (_sf, m: number, e: number) =>
untagNumber(m) * 2 ** untagNumber(e),
documentation: {
description: "Returns `m * 2^e`, the inverse of `math.frexp`.",
parameters: [
{ name: "m", type: "number" },
{ name: "e", type: "integer" },
],
returns: [{ type: "number" }],
},
description: "Returns `m * 2^e`, the inverse of `math.frexp`.",
parameters: [
{ name: "m", type: "number" },
{ name: "e", type: "integer" },
],
returns: [{ type: "number" }],
}),
// Power and logarithms
exp: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.exp(untagNumber(x)),
documentation: {
description: "Returns `e` raised to `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
},
description: "Returns `e` raised to `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
}),
log: new LuaBuiltinFunction({
callback: (_sf, x: number, base?: number) => {
@@ -292,79 +262,63 @@ export const mathApi = new LuaTable({
}
return Math.log(untagNumber(x)) / Math.log(untagNumber(base));
},
documentation: {
description:
"Returns the logarithm of `x`, using the natural base unless another base is supplied.",
parameters: [
{ name: "x", type: "number" },
{ name: "base", type: "number", optional: true },
],
returns: [{ type: "number" }],
examples: [{ code: "print(math.log(100, 10)) -- 2" }],
},
description:
"Returns the logarithm of `x`, using the natural base unless another base is supplied.",
parameters: [
{ name: "x", type: "number" },
{ name: "base", type: "number", optional: true },
],
returns: [{ type: "number" }],
examples: [{ code: "print(math.log(100, 10)) -- 2" }],
}),
// Power function (deprecated in Lua 5.4 but retained for compatibility)
pow: new LuaBuiltinFunction({
callback: (_sf, x: number, y: number) => untagNumber(x) ** untagNumber(y),
documentation: {
description: "Returns `x` raised to the power `y`.",
parameters: [
{ name: "x", type: "number" },
{ name: "y", type: "number" },
],
returns: [{ type: "number" }],
deprecated: "Use the `^` operator instead.",
},
description: "Returns `x` raised to the power `y`.",
parameters: [
{ name: "x", type: "number" },
{ name: "y", type: "number" },
],
returns: [{ type: "number" }],
deprecated: "Use the `^` operator instead.",
}),
sqrt: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.sqrt(untagNumber(x)),
documentation: {
description: "Returns the square root of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
},
description: "Returns the square root of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
}),
// Trigonometric functions
cos: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.cos(untagNumber(x)),
documentation: {
description: "Returns the cosine of `x` radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
},
description: "Returns the cosine of `x` radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
}),
sin: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.sin(untagNumber(x)),
documentation: {
description: "Returns the sine of `x` radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
},
description: "Returns the sine of `x` radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
}),
tan: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.tan(untagNumber(x)),
documentation: {
description: "Returns the tangent of `x` radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
},
description: "Returns the tangent of `x` radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
}),
acos: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.acos(untagNumber(x)),
documentation: {
description: "Returns the arc cosine of `x` in radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
},
description: "Returns the arc cosine of `x` in radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
}),
asin: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.asin(untagNumber(x)),
documentation: {
description: "Returns the arc sine of `x` in radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
},
description: "Returns the arc sine of `x` in radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
}),
atan: new LuaBuiltinFunction({
callback: (_sf, y: number, x?: number) => {
@@ -373,77 +327,63 @@ export const mathApi = new LuaTable({
}
return Math.atan2(untagNumber(y), untagNumber(x));
},
documentation: {
description:
"Returns the arc tangent of `y/x` in radians, using `1` for omitted `x`.",
parameters: [
{ name: "y", type: "number" },
{ name: "x", type: "number", optional: true },
],
returns: [{ type: "number" }],
},
description:
"Returns the arc tangent of `y/x` in radians, using `1` for omitted `x`.",
parameters: [
{ name: "y", type: "number" },
{ name: "x", type: "number", optional: true },
],
returns: [{ type: "number" }],
}),
// Hyperbolic functions (deprecated in Lua 5.4 but retained for compatibility)
cosh: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.cosh(untagNumber(x)),
documentation: {
description: "Returns the hyperbolic cosine of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
deprecated: "Retained for compatibility with older Lua versions.",
},
description: "Returns the hyperbolic cosine of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
deprecated: "Retained for compatibility with older Lua versions.",
}),
sinh: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.sinh(untagNumber(x)),
documentation: {
description: "Returns the hyperbolic sine of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
deprecated: "Retained for compatibility with older Lua versions.",
},
description: "Returns the hyperbolic sine of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
deprecated: "Retained for compatibility with older Lua versions.",
}),
tanh: new LuaBuiltinFunction({
callback: (_sf, x: number) => Math.tanh(untagNumber(x)),
documentation: {
description: "Returns the hyperbolic tangent of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
deprecated: "Retained for compatibility with older Lua versions.",
},
description: "Returns the hyperbolic tangent of `x`.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
deprecated: "Retained for compatibility with older Lua versions.",
}),
// Additional utility
deg: new LuaBuiltinFunction({
callback: (_sf, x: number) => (untagNumber(x) * 180) / Math.PI,
documentation: {
description: "Converts an angle from radians to degrees.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
examples: [{ code: "print(math.deg(math.pi)) -- 180" }],
},
description: "Converts an angle from radians to degrees.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
examples: [{ code: "print(math.deg(math.pi)) -- 180" }],
}),
rad: new LuaBuiltinFunction({
callback: (_sf, x: number) => (untagNumber(x) * Math.PI) / 180,
documentation: {
description: "Converts an angle from degrees to radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
},
description: "Converts an angle from degrees to radians.",
parameters: [{ name: "x", type: "number" }],
returns: [{ type: "number" }],
}),
ult: new LuaBuiltinFunction({
callback: (_sf, m: number, n: number) => {
return untagNumber(m) >>> 0 < untagNumber(n) >>> 0;
},
documentation: {
description: "Compares two integers as unsigned 32-bit values.",
parameters: [
{ name: "m", type: "integer" },
{ name: "n", type: "integer" },
],
returns: [{ type: "boolean" }],
examples: [{ code: "print(math.ult(2, 3)) -- true" }],
},
description: "Compares two integers as unsigned 32-bit values.",
parameters: [
{ name: "m", type: "integer" },
{ name: "n", type: "integer" },
],
returns: [{ type: "boolean" }],
examples: [{ code: "print(math.ult(2, 3)) -- true" }],
}),
// Keep the cosineSimilarity utility function
@@ -472,17 +412,13 @@ export const mathApi = new LuaTable({
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
},
documentation: {
description:
"Returns the cosine similarity between two equal-length numeric vectors.",
parameters: [
{ name: "vecA", type: "table" },
{ name: "vecB", type: "table" },
],
returns: [{ type: "number", description: "Cosine similarity." }],
examples: [
{ code: "print(math.cosineSimilarity({1, 2, 3}, {4, 5, 6}))" },
],
},
description:
"Returns the cosine similarity between two equal-length numeric vectors.",
parameters: [
{ name: "vecA", type: "table" },
{ name: "vecB", type: "table" },
],
returns: [{ type: "number", description: "Cosine similarity." }],
examples: [{ code: "print(math.cosineSimilarity({1, 2, 3}, {4, 5, 6}))" }],
}),
});