Skip to main content

luaur_vm/functions/
luaopen_integer.rs

1use crate::functions::lua_l_register::lua_l_register;
2use crate::functions::lua_pushinteger_64::lua_pushinteger_64;
3use crate::functions::lua_setfield::lua_setfield;
4use crate::records::lua_l_reg::LuaLReg;
5use crate::type_aliases::lua_state::lua_State;
6
7pub unsafe fn luaopen_integer(l: *mut lua_State) -> core::ffi::c_int {
8    // Register the integer library functions
9    // Note: int64lib is defined in lintlib.cpp; in this translation context we use the local static.
10    lua_l_register(l, c"int64".as_ptr(), INT64LIB.as_ptr());
11
12    // Push LLONG_MAX and set it as "maxsigned"
13    lua_pushinteger_64(l, i64::MAX);
14    lua_setfield(l, -2, c"maxsigned".as_ptr());
15
16    // Push LLONG_MIN and set it as "minsigned"
17    lua_pushinteger_64(l, i64::MIN);
18    lua_setfield(l, -2, c"minsigned".as_ptr());
19
20    1
21}
22
23// Define the int64lib luaL_Reg table locally as in the C++ source.
24// We use a Sync wrapper or just raw pointers in a way that satisfies the compiler for statics.
25struct SyncLuaLReg([LuaLReg; 1]);
26unsafe impl Sync for SyncLuaLReg {}
27
28static INT64LIB: SyncLuaLReg = SyncLuaLReg([LuaLReg {
29    name: core::ptr::null(),
30    func: None,
31}]);
32
33impl core::ops::Deref for SyncLuaLReg {
34    type Target = [LuaLReg; 1];
35    fn deref(&self) -> &Self::Target {
36        &self.0
37    }
38}