Skip to main content

luaur_vm/functions/
luaopen_math.rs

1//! Node: `cxx:Function:Luau.VM:VM/src/lmathlib.cpp:517:luaopen_math`
2//! Source: `VM/src/lmathlib.cpp:473-541` (hand-ported)
3
4use crate::functions::lua_encodepointer::lua_encodepointer;
5use crate::functions::lua_l_register::lua_l_register;
6use crate::functions::lua_pushnumber::lua_pushnumber;
7use crate::functions::lua_setfield::lua_setfield;
8use crate::functions::math_abs::math_abs;
9use crate::functions::math_acos::math_acos;
10use crate::functions::math_asin::math_asin;
11use crate::functions::math_atan::math_atan;
12use crate::functions::math_atan_2::math_atan2;
13use crate::functions::math_ceil::math_ceil;
14use crate::functions::math_clamp::math_clamp;
15use crate::functions::math_cos::math_cos;
16use crate::functions::math_cosh::math_cosh;
17use crate::functions::math_deg::math_deg;
18use crate::functions::math_exp::math_exp;
19use crate::functions::math_floor::math_floor;
20use crate::functions::math_fmod::math_fmod;
21use crate::functions::math_frexp::math_frexp;
22use crate::functions::math_isfinite::math_isfinite;
23use crate::functions::math_isinf::math_isinf;
24use crate::functions::math_isnan::math_isnan;
25use crate::functions::math_ldexp::math_ldexp;
26use crate::functions::math_lerp::math_lerp;
27use crate::functions::math_log::math_log;
28use crate::functions::math_log_10::math_log_10;
29use crate::functions::math_map::math_map;
30use crate::functions::math_max::math_max;
31use crate::functions::math_min::math_min;
32use crate::functions::math_modf::math_modf;
33use crate::functions::math_noise::math_noise;
34use crate::functions::math_pow::math_pow;
35use crate::functions::math_rad::math_rad;
36use crate::functions::math_random::math_random;
37use crate::functions::math_randomseed::math_randomseed;
38use crate::functions::math_round::math_round;
39use crate::functions::math_sign::math_sign;
40use crate::functions::math_sin::math_sin;
41use crate::functions::math_sinh::math_sinh;
42use crate::functions::math_sqrt::math_sqrt;
43use crate::functions::math_tan::math_tan;
44use crate::functions::math_tanh::math_tanh;
45use crate::functions::pcg_32_seed::pcg_32_seed;
46use crate::macros::luau_e::LUAU_E;
47use crate::macros::luau_nan::LUAU_NAN;
48use crate::macros::luau_phi::LUAU_PHI;
49use crate::macros::luau_pi::LUAU_PI;
50use crate::macros::luau_sqrt_2::LUAU_SQRT2;
51use crate::macros::luau_tau::LUAU_TAU;
52use crate::records::lua_l_reg::LuaLReg;
53use crate::type_aliases::lua_state::lua_State;
54
55struct MathFuncs([LuaLReg; 38]);
56unsafe impl Sync for MathFuncs {}
57
58static MATH_FUNCS: MathFuncs = MathFuncs([
59    LuaLReg {
60        name: c"abs".as_ptr(),
61        func: Some(math_abs),
62    },
63    LuaLReg {
64        name: c"acos".as_ptr(),
65        func: Some(math_acos),
66    },
67    LuaLReg {
68        name: c"asin".as_ptr(),
69        func: Some(math_asin),
70    },
71    LuaLReg {
72        name: c"atan2".as_ptr(),
73        func: Some(math_atan2),
74    },
75    LuaLReg {
76        name: c"atan".as_ptr(),
77        func: Some(math_atan),
78    },
79    LuaLReg {
80        name: c"ceil".as_ptr(),
81        func: Some(math_ceil),
82    },
83    LuaLReg {
84        name: c"cosh".as_ptr(),
85        func: Some(math_cosh),
86    },
87    LuaLReg {
88        name: c"cos".as_ptr(),
89        func: Some(math_cos),
90    },
91    LuaLReg {
92        name: c"deg".as_ptr(),
93        func: Some(math_deg),
94    },
95    LuaLReg {
96        name: c"exp".as_ptr(),
97        func: Some(math_exp),
98    },
99    LuaLReg {
100        name: c"floor".as_ptr(),
101        func: Some(math_floor),
102    },
103    LuaLReg {
104        name: c"fmod".as_ptr(),
105        func: Some(math_fmod),
106    },
107    LuaLReg {
108        name: c"frexp".as_ptr(),
109        func: Some(math_frexp),
110    },
111    LuaLReg {
112        name: c"ldexp".as_ptr(),
113        func: Some(math_ldexp),
114    },
115    LuaLReg {
116        name: c"log10".as_ptr(),
117        func: Some(math_log_10),
118    },
119    LuaLReg {
120        name: c"log".as_ptr(),
121        func: Some(math_log),
122    },
123    LuaLReg {
124        name: c"max".as_ptr(),
125        func: Some(math_max),
126    },
127    LuaLReg {
128        name: c"min".as_ptr(),
129        func: Some(math_min),
130    },
131    LuaLReg {
132        name: c"modf".as_ptr(),
133        func: Some(math_modf),
134    },
135    LuaLReg {
136        name: c"pow".as_ptr(),
137        func: Some(math_pow),
138    },
139    LuaLReg {
140        name: c"rad".as_ptr(),
141        func: Some(math_rad),
142    },
143    LuaLReg {
144        name: c"random".as_ptr(),
145        func: Some(math_random),
146    },
147    LuaLReg {
148        name: c"randomseed".as_ptr(),
149        func: Some(math_randomseed),
150    },
151    LuaLReg {
152        name: c"sinh".as_ptr(),
153        func: Some(math_sinh),
154    },
155    LuaLReg {
156        name: c"sin".as_ptr(),
157        func: Some(math_sin),
158    },
159    LuaLReg {
160        name: c"sqrt".as_ptr(),
161        func: Some(math_sqrt),
162    },
163    LuaLReg {
164        name: c"tanh".as_ptr(),
165        func: Some(math_tanh),
166    },
167    LuaLReg {
168        name: c"tan".as_ptr(),
169        func: Some(math_tan),
170    },
171    LuaLReg {
172        name: c"noise".as_ptr(),
173        func: Some(math_noise),
174    },
175    LuaLReg {
176        name: c"clamp".as_ptr(),
177        func: Some(math_clamp),
178    },
179    LuaLReg {
180        name: c"sign".as_ptr(),
181        func: Some(math_sign),
182    },
183    LuaLReg {
184        name: c"round".as_ptr(),
185        func: Some(math_round),
186    },
187    LuaLReg {
188        name: c"map".as_ptr(),
189        func: Some(math_map),
190    },
191    LuaLReg {
192        name: c"lerp".as_ptr(),
193        func: Some(math_lerp),
194    },
195    LuaLReg {
196        name: c"isnan".as_ptr(),
197        func: Some(math_isnan),
198    },
199    LuaLReg {
200        name: c"isinf".as_ptr(),
201        func: Some(math_isinf),
202    },
203    LuaLReg {
204        name: c"isfinite".as_ptr(),
205        func: Some(math_isfinite),
206    },
207    LuaLReg {
208        name: core::ptr::null(),
209        func: None,
210    },
211]);
212
213#[allow(non_snake_case)]
214pub unsafe fn luaopen_math(L: *mut lua_State) -> core::ffi::c_int {
215    let mut seed = lua_encodepointer(L, L as usize) as u64;
216    seed ^= 0;
217    pcg_32_seed(&mut (*(*L).global).rngstate, seed);
218
219    lua_l_register(L, c"math".as_ptr(), MATH_FUNCS.0.as_ptr());
220
221    lua_pushnumber(L, LUAU_PI);
222    lua_setfield(L, -2, c"pi".as_ptr());
223    lua_pushnumber(L, f64::INFINITY);
224    lua_setfield(L, -2, c"huge".as_ptr());
225    lua_pushnumber(L, LUAU_NAN);
226    lua_setfield(L, -2, c"nan".as_ptr());
227    lua_pushnumber(L, LUAU_E);
228    lua_setfield(L, -2, c"e".as_ptr());
229    lua_pushnumber(L, LUAU_PHI);
230    lua_setfield(L, -2, c"phi".as_ptr());
231    lua_pushnumber(L, LUAU_SQRT2);
232    lua_setfield(L, -2, c"sqrt2".as_ptr());
233    lua_pushnumber(L, LUAU_TAU);
234    lua_setfield(L, -2, c"tau".as_ptr());
235
236    1
237}