From cb4f392c05d3791f3af397b74b5e83a8022500ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Jan=20Fialka?= Date: Thu, 16 Oct 2025 08:27:56 +0200 Subject: [PATCH] Add `huge` constant and `type()` function to the `math` API (#1632) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matouš Jan Fialka --- client/space_lua/stdlib/math.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/client/space_lua/stdlib/math.ts b/client/space_lua/stdlib/math.ts index a90097a8..3d1f35e3 100644 --- a/client/space_lua/stdlib/math.ts +++ b/client/space_lua/stdlib/math.ts @@ -1,6 +1,37 @@ import { LuaBuiltinFunction, LuaRuntimeError, LuaTable } from "../runtime.ts"; export const mathApi = new LuaTable({ + // math constants + huge: 1 / 0, + + // math.type(x) => "integer" | "float" | nil + type: new LuaBuiltinFunction((_sf, x?: any) => { + // arg is mandatory + if (x === undefined) { + throw new LuaRuntimeError( + "bad argument #1 to 'math.type' (value expected)", + _sf, + ); + } + + if (typeof x === "number" || x instanceof Number) { + const n = Number(x); + + // NaN and +Inf/-Inf are floats + if (!Number.isFinite(n)) { + return "float"; + } + + return Number.isInteger(n) ? "integer" : "float"; + } + + if (typeof x === "bigint") { + return "integer"; + } + + return null; + }), + /** * When called without arguments, returns a pseudo-random float with * uniform distribution in the range [0,1). When called with two