[Space Lua] Implement math.tointeger

Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
This commit is contained in:
Matouš Jan Fialka
2026-02-20 11:26:59 +01:00
parent 3b29aa8497
commit 559fc0b94d
2 changed files with 47 additions and 0 deletions
+21
View File
@@ -40,6 +40,27 @@ export const mathApi = new LuaTable({
}
return null;
}),
/**
* If the value x is representable as a Lua integer, returns an
* integer with that value. Otherwise returns nil.
*/
tointeger: new LuaBuiltinFunction((_sf, x?: any) => {
if (x === undefined || x === null) return null;
if (typeof x === "number") {
// plain integer
if (Number.isInteger(x)) return x;
return null;
}
if (isTaggedFloat(x)) {
// float with integer value e.g. 3.0
if (Number.isInteger(x.value)) return x.value;
return null;
}
if (typeof x === "bigint") return x;
return null;
}),
/**
* When called without arguments, returns a pseudo-random float with
* uniform distribution in the range [0,1). When called with two