Skip to main content

lua_stdlib/
math_lib.rs

1//! Standard mathematical library — `math.*`
2//!
3//! Translated from `src/lmathlib.c` (Lua 5.4.7, 782 lines, 28 functions).
4//!
5//! The PRNG is xoshiro256** operating on four 64-bit words. In C the
6//! implementation has two code paths (64-bit integers vs two 32-bit halves);
7//! Rust always has `u64`, so only the 64-bit path is kept.
8//!
9//! Deprecated compat functions guarded by `LUA_COMPAT_MATHLIB` (cosh, sinh,
10//! tanh, pow, frexp, ldexp, log10, atan2) are omitted; we target Lua 5.4
11//! semantics only. See PORTING.md §13.
12
13// PORT NOTE: All imports below will be unresolved until Phase B lands the
14// lua-types crate. Expected Phase-A errors: E0432, E0412, E0433, E0425.
15use lua_types::{LuaError, LuaType, LuaValue};
16use crate::state_stub::{LuaState, LuaStateStubExt as _};
17
18// ── Constants ──────────────────────────────────────────────────────────────
19
20///
21/// Higher precision than `std::f64::consts::PI`; matches the C source literal.
22const PI: f64 = 3.141592653589793238462643383279502884_f64;
23
24/// Number of binary digits in the mantissa of `lua_Number` (f64).
25const FIGS: u32 = 53; // DBL_MANT_DIG for f64
26
27/// Bits to discard from the 64-bit random word before float conversion.
28const SHIFT64_FIG: u32 = 64 - FIGS; // = 11
29
30// ── Type aliases for library registration ─────────────────────────────────
31
32/// A Lua C-style function: takes the Lua state, returns count of pushed values.
33/// PORT NOTE: Phase B will unify with `lua_types::LuaCFunction`.
34type LuaCFunction = fn(&mut LuaState) -> Result<usize, LuaError>;
35
36/// An entry in the library registration table (name, optional function).
37/// `None` is used for placeholder entries whose values are set manually
38/// (e.g. `pi`, `huge`, `maxinteger`, `mininteger`, `random`, `randomseed`).
39/// PORT NOTE: Phase B will unify with `lua_types::LibReg`.
40#[expect(dead_code, reason = "ported stdlib helper; not yet wired into the runtime")]
41struct LibReg {
42    name: &'static [u8],
43    func: Option<LuaCFunction>,
44}
45
46// ── PRNG state ────────────────────────────────────────────────────────────
47
48/// State for the xoshiro256** PRNG.
49///
50/// In C this is stored as raw `lua_newuserdatauv` memory and accessed by
51/// casting the userdata pointer. Until typed-userdata closure upvalues land
52/// in Phase B, we keep the PRNG state in a thread-local cell so that
53/// `math.random` and `math.randomseed` are callable from Lua. This collapses
54/// per-lua_State PRNG isolation to per-thread, which is sufficient for the
55/// 5.4 test corpus.
56struct RanState {
57    s: [u64; 4],
58}
59
60thread_local! {
61    static RAN_STATE: std::cell::RefCell<RanState> =
62        std::cell::RefCell::new(RanState { s: [0xff, 0xff, 0xff, 0xff] });
63}
64
65// ── Pure PRNG algorithms ──────────────────────────────────────────────────
66
67/// Advance the xoshiro256** state by one step and return the next raw 64-bit
68/// pseudo-random value.
69///
70fn next_rand(s: &mut [u64; 4]) -> u64 {
71    let s0 = s[0];
72    let s1 = s[1];
73    let s2 = s[2] ^ s0;
74    let s3 = s[3] ^ s1;
75    let res = s1.wrapping_mul(5).rotate_left(7).wrapping_mul(9);
76    s[0] = s0 ^ s3;
77    s[1] = s1 ^ s2;
78    s[2] = s2 ^ (s1 << 17);
79    s[3] = s3.rotate_left(45);
80    res
81}
82
83/// Convert a raw 64-bit PRNG output to a float in [0.0, 1.0).
84///
85/// Takes the top FIGS=53 bits, interprets them as a signed integer, scales
86/// by `scaleFIG = 0.5 / 2^52`, then corrects the two's-complement sign.
87fn rand_to_float(x: u64) -> f64 {
88    let sx = (x >> SHIFT64_FIG) as i64;
89    //            = 0.5 / 2^52
90    let scale_fig: f64 = 0.5 / ((1u64 << (FIGS - 1)) as f64);
91    let mut res = (sx as f64) * scale_fig;
92    if sx < 0 {
93        res += 1.0;
94    }
95    debug_assert!(0.0 <= res && res < 1.0);
96    res
97}
98
99/// Initialise the four PRNG words from two seed values.
100///
101///
102/// PORT NOTE: The Lua pushes (n1, n2) are done at the call site in Rust so
103/// that this function does not need `&mut LuaState`, avoiding a borrow
104/// conflict with the upvalue `RanState`.
105fn set_seed_words(s: &mut [u64; 4], n1: u64, n2: u64) {
106    s[0] = n1;
107    s[1] = 0xff; // avoid a zero state
108    s[2] = n2;
109    s[3] = 0;
110    for _ in 0..16 {
111        next_rand(s); // discard initial values to "spread" seed
112    }
113}
114
115/// Project `ran` uniformly into [0, n].
116///
117///
118/// Uses rejection sampling with the smallest Mersenne number ≥ n as a mask.
119/// Takes `&mut [u64; 4]` rather than `&mut RanState` to avoid nested borrows
120/// at call sites.
121fn project(mut ran: u64, n: u64, s: &mut [u64; 4]) -> u64 {
122    if (n & n.wrapping_add(1)) == 0 {
123        return ran & n;
124    }
125    // Compute the smallest (2^b - 1) not smaller than n.
126    let mut lim = n;
127    lim |= lim >> 1;
128    lim |= lim >> 2;
129    lim |= lim >> 4;
130    lim |= lim >> 8;
131    lim |= lim >> 16;
132    lim |= lim >> 32; // u64 always has 64 bits; C guards this with #if
133    debug_assert!((lim & lim.wrapping_add(1)) == 0); // lim+1 is a power of 2
134    debug_assert!(lim >= n);
135    debug_assert!((lim >> 1) < n);
136    loop {
137        ran &= lim;
138        if ran <= n {
139            break;
140        }
141        ran = next_rand(s);
142    }
143    ran
144}
145
146// ── Helpers ───────────────────────────────────────────────────────────────
147
148/// Convert `d` to integer and push it; push the float unchanged if it doesn't
149/// fit exactly in an i64.
150///
151fn push_num_int(state: &mut LuaState, d: f64) {
152    //    else lua_pushnumber(L, d);
153    //
154    // lua_numbertointeger: d >= LUA_MININTEGER as float &&
155    //                      d <  -(LUA_MININTEGER as float)
156    let min_f = i64::MIN as f64; // -2^63
157    let max_plus1_f = -(i64::MIN as f64); // 2^63 (one past i64::MAX as float)
158    if d >= min_f && d < max_plus1_f {
159        state.push(LuaValue::Int(d as i64));
160    } else {
161        state.push(LuaValue::Float(d));
162    }
163}
164
165// ── Basic math functions ──────────────────────────────────────────────────
166
167/// `math.abs(x)` — absolute value, preserving integer type when possible.
168///
169fn math_abs(state: &mut LuaState) -> Result<usize, LuaError> {
170    if matches!(state.value_at(1), LuaValue::Int(_)) {
171        let n = state.to_integer(1).unwrap_or(0);
172        let n = if n < 0 {
173            (0u64.wrapping_sub(n as u64)) as i64
174        } else {
175            n
176        };
177        state.push(LuaValue::Int(n));
178    } else {
179        let x = state.check_number(1)?;
180        state.push(LuaValue::Float(x.abs()));
181    }
182    Ok(1)
183}
184
185/// `math.sin(x)` — sine (radians).
186///
187fn math_sin(state: &mut LuaState) -> Result<usize, LuaError> {
188    let x = state.check_number(1)?;
189    state.push(LuaValue::Float(x.sin()));
190    Ok(1)
191}
192
193/// `math.cos(x)` — cosine (radians).
194///
195fn math_cos(state: &mut LuaState) -> Result<usize, LuaError> {
196    let x = state.check_number(1)?;
197    state.push(LuaValue::Float(x.cos()));
198    Ok(1)
199}
200
201/// `math.tan(x)` — tangent (radians).
202///
203fn math_tan(state: &mut LuaState) -> Result<usize, LuaError> {
204    let x = state.check_number(1)?;
205    state.push(LuaValue::Float(x.tan()));
206    Ok(1)
207}
208
209/// `math.asin(x)` — arc-sine, result in radians.
210///
211fn math_asin(state: &mut LuaState) -> Result<usize, LuaError> {
212    let x = state.check_number(1)?;
213    state.push(LuaValue::Float(x.asin()));
214    Ok(1)
215}
216
217/// `math.acos(x)` — arc-cosine, result in radians.
218///
219fn math_acos(state: &mut LuaState) -> Result<usize, LuaError> {
220    let x = state.check_number(1)?;
221    state.push(LuaValue::Float(x.acos()));
222    Ok(1)
223}
224
225/// `math.atan(y [, x])` — arc-tangent of y/x (defaults x=1), result in
226/// radians. Subsumes C's `atan2` when x is provided.
227///
228fn math_atan(state: &mut LuaState) -> Result<usize, LuaError> {
229    let y = state.check_number(1)?;
230    let x = state.opt_number(2, 1.0)?;
231    state.push(LuaValue::Float(y.atan2(x)));
232    Ok(1)
233}
234
235/// `math.tointeger(x)` — convert x to an integer or return false.
236///
237fn math_toint(state: &mut LuaState) -> Result<usize, LuaError> {
238    // TODO(port): state.to_integer_opt(1) should return Option<i64>;
239    // the method name/signature will be confirmed in Phase B.
240    let maybe_n: Option<i64> = state.to_integer_opt(1);
241    if let Some(n) = maybe_n {
242        state.push(LuaValue::Int(n));
243    } else {
244        state.check_any(1)?;
245        // PORT NOTE: luaL_pushfail in Lua 5.4 pushes false (not nil).
246        state.push(LuaValue::Bool(false));
247    }
248    Ok(1)
249}
250
251/// `math.floor(x)` — largest integer ≤ x.
252///
253fn math_floor(state: &mut LuaState) -> Result<usize, LuaError> {
254    if matches!(state.value_at(1), LuaValue::Int(_)) {
255        // Must go through the public C-API set_top (relative to the call
256        // frame); the inherent LuaState::set_top treats its argument as an
257        // absolute StackIdx.
258        lua_vm::api::set_top(state, 1)?;
259    } else {
260        let d = state.check_number(1)?.floor();
261        push_num_int(state, d);
262    }
263    Ok(1)
264}
265
266/// `math.ceil(x)` — smallest integer ≥ x.
267///
268fn math_ceil(state: &mut LuaState) -> Result<usize, LuaError> {
269    if matches!(state.value_at(1), LuaValue::Int(_)) {
270        // Public C-API set_top (relative); inherent LuaState::set_top is absolute.
271        lua_vm::api::set_top(state, 1)?;
272    } else {
273        let d = state.check_number(1)?.ceil();
274        push_num_int(state, d);
275    }
276    Ok(1)
277}
278
279/// `math.fmod(x, y)` — floating-point remainder (same sign as x).
280///
281fn math_fmod(state: &mut LuaState) -> Result<usize, LuaError> {
282    if matches!(state.value_at(1), LuaValue::Int(_))
283        && matches!(state.value_at(2), LuaValue::Int(_))
284    {
285        let a = state.to_integer(1).unwrap_or(0);
286        let d = state.to_integer(2).unwrap_or(0);
287        if (d as u64).wrapping_add(1) <= 1 {
288            if d == 0 {
289                return Err(LuaError::arg_error(2, "zero"));
290            }
291            state.push(LuaValue::Int(0));
292        } else {
293            state.push(LuaValue::Int(a % d));
294        }
295    } else {
296        let x = state.check_number(1)?;
297        let y = state.check_number(2)?;
298        state.push(LuaValue::Float(x % y));
299    }
300    Ok(1)
301}
302
303/// `math.modf(x)` — split into integer and fractional parts; returns 2 values.
304///
305///
306/// PORT NOTE: Does not use `modf` (avoids `double *` / `float *` ABI mismatch
307/// for non-double `lua_Number`). Instead, uses ceil/floor + subtraction.
308fn math_modf(state: &mut LuaState) -> Result<usize, LuaError> {
309    if matches!(state.value_at(1), LuaValue::Int(_)) {
310        // Public C-API set_top (relative); inherent LuaState::set_top is absolute.
311        lua_vm::api::set_top(state, 1)?; // integer part is the integer itself
312        state.push(LuaValue::Float(0.0)); // no fractional part
313    } else {
314        let n = state.check_number(1)?;
315        let ip = if n < 0.0 { n.ceil() } else { n.floor() };
316        push_num_int(state, ip);
317        let frac = if n == ip { 0.0 } else { n - ip };
318        state.push(LuaValue::Float(frac));
319    }
320    Ok(2)
321}
322
323/// `math.sqrt(x)` — square root.
324///
325fn math_sqrt(state: &mut LuaState) -> Result<usize, LuaError> {
326    let x = state.check_number(1)?;
327    state.push(LuaValue::Float(x.sqrt()));
328    Ok(1)
329}
330
331/// `math.ult(m, n)` — unsigned less-than on integers.
332///
333fn math_ult(state: &mut LuaState) -> Result<usize, LuaError> {
334    let a = state.check_integer(1)?;
335    let b = state.check_integer(2)?;
336    state.push(LuaValue::Bool((a as u64) < (b as u64)));
337    Ok(1)
338}
339
340/// `math.log(x [, base])` — logarithm; natural if base omitted.
341///
342fn math_log(state: &mut LuaState) -> Result<usize, LuaError> {
343    let x = state.check_number(1)?;
344    let res = if matches!(state.type_at(2), LuaType::None | LuaType::Nil) {
345        x.ln()
346    } else {
347        let base = state.check_number(2)?;
348        if base == 2.0 {
349            x.log2()
350        } else if base == 10.0 {
351            x.log10()
352        } else {
353            x.ln() / base.ln()
354        }
355    };
356    state.push(LuaValue::Float(res));
357    Ok(1)
358}
359
360/// `math.exp(x)` — e raised to the power x.
361///
362fn math_exp(state: &mut LuaState) -> Result<usize, LuaError> {
363    let x = state.check_number(1)?;
364    state.push(LuaValue::Float(x.exp()));
365    Ok(1)
366}
367
368/// `math.deg(x)` — convert radians to degrees.
369///
370fn math_deg(state: &mut LuaState) -> Result<usize, LuaError> {
371    let x = state.check_number(1)?;
372    state.push(LuaValue::Float(x * (180.0 / PI)));
373    Ok(1)
374}
375
376/// `math.rad(x)` — convert degrees to radians.
377///
378fn math_rad(state: &mut LuaState) -> Result<usize, LuaError> {
379    let x = state.check_number(1)?;
380    state.push(LuaValue::Float(x * (PI / 180.0)));
381    Ok(1)
382}
383
384/// `math.min(x, ...)` — minimum of all arguments (uses Lua `<` comparison).
385///
386fn math_min(state: &mut LuaState) -> Result<usize, LuaError> {
387    let n = state.get_top();
388    let mut imin: i32 = 1;
389    if n < 1 {
390        return Err(LuaError::arg_error(1, "value expected"));
391    }
392    for i in 2..=n {
393        if state.compare_lt(i, imin)? {
394            imin = i;
395        }
396    }
397    state.push_value(imin)?;
398    Ok(1)
399}
400
401/// `math.max(x, ...)` — maximum of all arguments (uses Lua `<` comparison).
402///
403fn math_max(state: &mut LuaState) -> Result<usize, LuaError> {
404    let n = state.get_top();
405    let mut imax: i32 = 1;
406    if n < 1 {
407        return Err(LuaError::arg_error(1, "value expected"));
408    }
409    for i in 2..=n {
410        if state.compare_lt(imax, i)? {
411            imax = i;
412        }
413    }
414    state.push_value(imax)?;
415    Ok(1)
416}
417
418/// `math.type(x)` — return `"integer"`, `"float"`, or false for non-numbers.
419///
420fn math_type(state: &mut LuaState) -> Result<usize, LuaError> {
421    if matches!(state.type_at(1), LuaType::Number) {
422        if matches!(state.value_at(1), LuaValue::Int(_)) {
423            state.push_string(b"integer")?;
424        } else {
425            state.push_string(b"float")?;
426        }
427    } else {
428        state.check_any(1)?;
429        // PORT NOTE: luaL_pushfail pushes false in Lua 5.4.4+.
430        state.push(LuaValue::Bool(false));
431    }
432    Ok(1)
433}
434
435// ── PRNG-backed Lua functions ─────────────────────────────────────────────
436
437/// `math.random([m [, n]])` — pseudo-random number generation.
438///
439///
440/// With no arguments: float in [0, 1).
441/// With one argument n: integer in [1, n] (or full random u64 if n == 0).
442/// With two arguments m, n: integer in [m, n].
443fn math_random(state: &mut LuaState) -> Result<usize, LuaError> {
444    // TODO(port): RanState is stored as typed userdata in closure upvalue 1.
445    // Phase B must implement `state.upvalue_userdata_mut::<RanState>(1)` using
446    // interior mutability (e.g. GcRef<RefCell<RanState>>) to avoid the borrow
447    // conflict between &mut RanState and subsequent &mut LuaState push calls.
448    //
449    // For Phase A: advance PRNG and get args via separate borrows.
450    let rv = advance_prng(state)?;
451    let n_args = state.get_top();
452
453    if n_args == 0 {
454        state.push(LuaValue::Float(rand_to_float(rv)));
455        return Ok(1);
456    }
457
458    let (low, up) = match n_args {
459        1 => {
460            let up = state.check_integer(1)?;
461            if up == 0 {
462                // I2UInt(rv) = rv (trivial for u64)
463                state.push(LuaValue::Int(rv as i64));
464                return Ok(1);
465            }
466            (1i64, up)
467        }
468        2 => {
469            let low = state.check_integer(1)?;
470            let up = state.check_integer(2)?;
471            (low, up)
472        }
473        _ => {
474            return Err(LuaError::runtime(format_args!(
475                "wrong number of arguments"
476            )));
477        }
478    };
479
480    if low > up {
481        return Err(LuaError::arg_error(1, "interval is empty"));
482    }
483
484    let range = (up as u64).wrapping_sub(low as u64);
485    let p = project_from_upvalue(state, rv, range)?;
486    state.push(LuaValue::Int((p as u64).wrapping_add(low as u64) as i64));
487    Ok(1)
488}
489
490/// `math.randomseed([x [, y]])` — seed the PRNG; returns two seed values.
491///
492fn math_randomseed(state: &mut LuaState) -> Result<usize, LuaError> {
493    // TODO(port): same upvalue userdata access issue as math_random.
494    if matches!(state.type_at(1), LuaType::None) {
495        // randseed uses time(NULL) and address of L for entropy.
496        apply_random_seed(state)?;
497    } else {
498        //    lua_Integer n2 = luaL_optinteger(L, 2, 0);
499        let n1 = state.check_integer(1)? as u64;
500        let n2 = state.opt_integer(2, 0)? as u64;
501        apply_set_seed(state, n1, n2)?;
502    }
503    Ok(2)
504}
505
506/// Advance the PRNG stored in the thread-local `RAN_STATE` and return the
507/// raw 64-bit output.
508///
509/// PORT NOTE: In C this draws from the userdata in closure upvalue 1. The
510/// Rust port stores the PRNG state in a thread-local until typed-userdata
511/// closure upvalues are wired up. Storage location is the only difference;
512/// the algorithm is unchanged.
513fn advance_prng(_state: &mut LuaState) -> Result<u64, LuaError> {
514    Ok(RAN_STATE.with(|r| next_rand(&mut r.borrow_mut().s)))
515}
516
517/// Apply rejection sampling for `math.random` using the thread-local PRNG.
518///
519/// PORT NOTE: see `advance_prng` for the thread-local rationale.
520fn project_from_upvalue(
521    _state: &mut LuaState,
522    ran: u64,
523    n: u64,
524) -> Result<u64, LuaError> {
525    Ok(RAN_STATE.with(|r| project(ran, n, &mut r.borrow_mut().s)))
526}
527
528/// Seed the PRNG from wall-clock time (entropy source).
529///
530///
531/// TODO(port): must write n1 and n2 back to the upvalue RanState.
532fn apply_random_seed(state: &mut LuaState) -> Result<(), LuaError> {
533    let entropy = state.global().entropy_hook.map(|hook| hook()).unwrap_or(0);
534    let seed1 = entropy;
535    // TODO(port): C also mixes address entropy; keep the second seed derived
536    // deterministically unless a richer host entropy API is added.
537    let seed2: u64 = entropy.rotate_left(17) ^ 0x9e37_79b9_7f4a_7c15;
538    apply_set_seed(state, seed1, seed2)
539}
540
541/// Apply explicit seeds to the PRNG and push them onto the stack.
542///
543///
544/// PORT NOTE: writes seeds into the thread-local RanState (see `advance_prng`).
545fn apply_set_seed(state: &mut LuaState, n1: u64, n2: u64) -> Result<(), LuaError> {
546    RAN_STATE.with(|r| set_seed_words(&mut r.borrow_mut().s, n1, n2));
547    state.push(LuaValue::Int(n1 as i64));
548    state.push(LuaValue::Int(n2 as i64));
549    Ok(())
550}
551
552/// Register `math.random` and `math.randomseed` on the math library table at
553/// stack top, after seeding the thread-local PRNG.
554///
555///
556/// PORT NOTE: C stores the PRNG inside a userdata bound as upvalue 1 of both
557/// closures. Until typed userdata closure upvalues are available, the Rust
558/// port keeps the PRNG in a thread-local (see `RAN_STATE`) and registers the
559/// functions as plain non-closure entries on the library table.
560fn set_rand_func(state: &mut LuaState) -> Result<(), LuaError> {
561    apply_random_seed(state)?;
562    state.pop_n(2);
563
564    state.push_c_function(math_random)?;
565    state.set_field(-2, b"random")?;
566    state.push_c_function(math_randomseed)?;
567    state.set_field(-2, b"randomseed")?;
568    Ok(())
569}
570
571// ── Library registration table ────────────────────────────────────────────
572
573/// The `math` library function table.
574///
575///
576/// Placeholder entries (`None`) are filled in manually by `luaopen_math`
577/// (`pi`, `huge`, `maxinteger`, `mininteger`) or by `set_rand_func`
578/// (`random`, `randomseed`).
579#[expect(dead_code, reason = "ported stdlib helper; not yet wired into the runtime")]
580static MATHLIB: &[LibReg] = &[
581    LibReg { name: b"abs",        func: Some(math_abs)    },
582    LibReg { name: b"acos",       func: Some(math_acos)   },
583    LibReg { name: b"asin",       func: Some(math_asin)   },
584    LibReg { name: b"atan",       func: Some(math_atan)   },
585    LibReg { name: b"ceil",       func: Some(math_ceil)   },
586    LibReg { name: b"cos",        func: Some(math_cos)    },
587    LibReg { name: b"deg",        func: Some(math_deg)    },
588    LibReg { name: b"exp",        func: Some(math_exp)    },
589    LibReg { name: b"tointeger",  func: Some(math_toint)  },
590    LibReg { name: b"floor",      func: Some(math_floor)  },
591    LibReg { name: b"fmod",       func: Some(math_fmod)   },
592    LibReg { name: b"ult",        func: Some(math_ult)    },
593    LibReg { name: b"log",        func: Some(math_log)    },
594    LibReg { name: b"max",        func: Some(math_max)    },
595    LibReg { name: b"min",        func: Some(math_min)    },
596    LibReg { name: b"modf",       func: Some(math_modf)   },
597    LibReg { name: b"rad",        func: Some(math_rad)    },
598    LibReg { name: b"sin",        func: Some(math_sin)    },
599    LibReg { name: b"sqrt",       func: Some(math_sqrt)   },
600    LibReg { name: b"tan",        func: Some(math_tan)    },
601    LibReg { name: b"type",       func: Some(math_type)   },
602    // Placeholders; values are set manually in luaopen_math / set_rand_func.
603    LibReg { name: b"random",     func: None },
604    LibReg { name: b"randomseed", func: None },
605    LibReg { name: b"pi",         func: None },
606    LibReg { name: b"huge",       func: None },
607    LibReg { name: b"maxinteger", func: None },
608    LibReg { name: b"mininteger", func: None },
609];
610
611static MATHLIB_FUNCS: &[(&[u8], LuaCFunction)] = &[
612    (b"abs",        math_abs),
613    (b"acos",       math_acos),
614    (b"asin",       math_asin),
615    (b"atan",       math_atan),
616    (b"ceil",       math_ceil),
617    (b"cos",        math_cos),
618    (b"deg",        math_deg),
619    (b"exp",        math_exp),
620    (b"tointeger",  math_toint),
621    (b"floor",      math_floor),
622    (b"fmod",       math_fmod),
623    (b"ult",        math_ult),
624    (b"log",        math_log),
625    (b"max",        math_max),
626    (b"min",        math_min),
627    (b"modf",       math_modf),
628    (b"rad",        math_rad),
629    (b"sin",        math_sin),
630    (b"sqrt",       math_sqrt),
631    (b"tan",        math_tan),
632    (b"type",       math_type),
633];
634
635// ── Module entry point ────────────────────────────────────────────────────
636
637/// Open the `math` library: create the table, populate constants, register
638/// the PRNG functions with their shared `RanState` upvalue.
639///
640///
641/// `LUAMOD_API` → `pub` (see macros.tsv).
642pub fn luaopen_math(state: &mut LuaState) -> Result<usize, LuaError> {
643    // Creates a new table and registers all non-None entries from MATHLIB.
644    state.new_lib(MATHLIB_FUNCS)?;
645
646    state.push(LuaValue::Float(PI));
647    state.set_field(-2, b"pi")?;
648
649    state.push(LuaValue::Float(f64::INFINITY));
650    state.set_field(-2, b"huge")?;
651
652    // LUA_MAXINTEGER = i64::MAX (lua_Integer is int64_t in default config).
653    state.push(LuaValue::Int(i64::MAX));
654    state.set_field(-2, b"maxinteger")?;
655
656    state.push(LuaValue::Int(i64::MIN));
657    state.set_field(-2, b"mininteger")?;
658
659    // Registers math.random and math.randomseed as upvalue-bearing closures.
660    set_rand_func(state)?;
661
662    Ok(1)
663}
664
665// ──────────────────────────────────────────────────────────────────────────
666// PORT STATUS
667//   source:        src/lmathlib.c  (782 lines, 28 functions)
668//   target_crate:  lua-stdlib
669//   confidence:    medium
670//   todos:         16
671//   port_notes:    8
672//   unsafe_blocks: 0
673//   notes:         All basic math functions are mechanically faithful. The
674//                  PRNG xoshiro256** algorithm is correctly translated using
675//                  native u64 (only the 64-bit code path; the 32-bit fallback
676//                  is dropped). The main Phase-B work is wiring up the upvalue
677//                  RanState userdata: advance_prng, project_from_upvalue,
678//                  apply_random_seed, apply_set_seed, and set_rand_func all
679//                  carry TODO(port) stubs where typed userdata + interior
680//                  mutability (RefCell) is required to avoid borrow conflicts.
681//                  Deprecated LUA_COMPAT_MATHLIB functions are omitted per
682//                  PORTING.md §13. state.new_lib, state.set_field,
683//                  state.compare_lt, state.push_value, state.opt_number,
684//                  state.opt_integer, state.check_integer, state.check_number,
685//                  state.check_any, state.to_integer_opt, state.get_top,
686//                  state.set_top, state.pop_n API names assumed; Phase B
687//                  will reconcile with the actual LuaState impl.
688// ──────────────────────────────────────────────────────────────────────────