mlua_sys/luau/
lauxlib.rs

1//! Contains definitions from `lualib.h`.
2
3use std::os::raw::{c_char, c_float, c_int, c_void};
4use std::ptr;
5
6use super::lua::{self, lua_CFunction, lua_Number, lua_State, lua_Unsigned, LUA_REGISTRYINDEX};
7
8// Key, in the registry, for table of loaded modules
9pub const LUA_LOADED_TABLE: *const c_char = cstr!("_LOADED");
10
11#[repr(C)]
12pub struct luaL_Reg {
13    pub name: *const c_char,
14    pub func: lua_CFunction,
15}
16
17unsafe extern "C-unwind" {
18    pub fn luaL_register(L: *mut lua_State, libname: *const c_char, l: *const luaL_Reg);
19    #[link_name = "luaL_getmetafield"]
20    pub fn luaL_getmetafield_(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
21    pub fn luaL_callmeta(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
22    #[link_name = "luaL_typeerrorL"]
23    pub fn luaL_typeerror(L: *mut lua_State, narg: c_int, tname: *const c_char) -> !;
24    #[link_name = "luaL_argerrorL"]
25    pub fn luaL_argerror(L: *mut lua_State, narg: c_int, extramsg: *const c_char) -> !;
26    pub fn luaL_checklstring(L: *mut lua_State, narg: c_int, l: *mut usize) -> *const c_char;
27    pub fn luaL_optlstring(
28        L: *mut lua_State,
29        narg: c_int,
30        def: *const c_char,
31        l: *mut usize,
32    ) -> *const c_char;
33    pub fn luaL_checknumber(L: *mut lua_State, narg: c_int) -> lua_Number;
34    pub fn luaL_optnumber(L: *mut lua_State, narg: c_int, def: lua_Number) -> lua_Number;
35
36    pub fn luaL_checkboolean(L: *mut lua_State, narg: c_int) -> c_int;
37    pub fn luaL_optboolean(L: *mut lua_State, narg: c_int, def: c_int) -> c_int;
38
39    #[link_name = "luaL_checkinteger"]
40    pub fn luaL_checkinteger_(L: *mut lua_State, narg: c_int) -> c_int;
41    #[link_name = "luaL_optinteger"]
42    pub fn luaL_optinteger_(L: *mut lua_State, narg: c_int, def: c_int) -> c_int;
43    pub fn luaL_checkunsigned(L: *mut lua_State, narg: c_int) -> lua_Unsigned;
44    pub fn luaL_optunsigned(L: *mut lua_State, narg: c_int, def: lua_Unsigned) -> lua_Unsigned;
45
46    pub fn luaL_checkvector(L: *mut lua_State, narg: c_int) -> *const c_float;
47    pub fn luaL_optvector(L: *mut lua_State, narg: c_int, def: *const c_float) -> *const c_float;
48
49    #[link_name = "luaL_checkstack"]
50    pub fn luaL_checkstack_(L: *mut lua_State, sz: c_int, msg: *const c_char);
51    pub fn luaL_checktype(L: *mut lua_State, narg: c_int, t: c_int);
52    pub fn luaL_checkany(L: *mut lua_State, narg: c_int);
53
54    #[link_name = "luaL_newmetatable"]
55    pub fn luaL_newmetatable_(L: *mut lua_State, tname: *const c_char) -> c_int;
56    pub fn luaL_checkudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void;
57
58    pub fn luaL_checkbuffer(L: *mut lua_State, narg: c_int, len: *mut usize) -> *mut c_void;
59
60    pub fn luaL_where(L: *mut lua_State, lvl: c_int);
61
62    #[link_name = "luaL_errorL"]
63    pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> !;
64
65    pub fn luaL_checkoption(
66        L: *mut lua_State,
67        narg: c_int,
68        def: *const c_char,
69        lst: *const *const c_char,
70    ) -> c_int;
71
72    #[link_name = "luaL_tolstring"]
73    pub fn luaL_tolstring_(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
74
75    pub fn luaL_newstate() -> *mut lua_State;
76
77    pub fn luaL_findtable(
78        L: *mut lua_State,
79        idx: c_int,
80        fname: *const c_char,
81        szhint: c_int,
82    ) -> *const c_char;
83
84    pub fn luaL_typename(L: *mut lua_State, idx: c_int) -> *const c_char;
85
86    pub fn luaL_callyieldable(L: *mut lua_State, nargs: c_int, nresults: c_int) -> c_int;
87
88    // sandbox libraries and globals
89    #[link_name = "luaL_sandbox"]
90    pub fn luaL_sandbox_(L: *mut lua_State);
91    pub fn luaL_sandboxthread(L: *mut lua_State);
92}
93
94//
95// Some useful macros (implemented as Rust functions)
96//
97
98#[inline(always)]
99pub unsafe fn luaL_argcheck(L: *mut lua_State, cond: c_int, arg: c_int, extramsg: *const c_char) {
100    if cond == 0 {
101        luaL_argerror(L, arg, extramsg);
102    }
103}
104
105#[inline(always)]
106pub unsafe fn luaL_argexpected(L: *mut lua_State, cond: c_int, arg: c_int, tname: *const c_char) {
107    if cond == 0 {
108        luaL_typeerror(L, arg, tname);
109    }
110}
111
112#[inline(always)]
113pub unsafe fn luaL_checkstring(L: *mut lua_State, n: c_int) -> *const c_char {
114    luaL_checklstring(L, n, ptr::null_mut())
115}
116
117#[inline(always)]
118pub unsafe fn luaL_optstring(L: *mut lua_State, n: c_int, d: *const c_char) -> *const c_char {
119    luaL_optlstring(L, n, d, ptr::null_mut())
120}
121
122// TODO: luaL_opt
123
124#[inline(always)]
125pub unsafe fn luaL_getmetatable(L: *mut lua_State, n: *const c_char) -> c_int {
126    lua::lua_getfield(L, LUA_REGISTRYINDEX, n)
127}
128
129#[inline(always)]
130pub unsafe fn luaL_ref(L: *mut lua_State, t: c_int) -> c_int {
131    assert_eq!(t, LUA_REGISTRYINDEX);
132    let r = lua::lua_ref(L, -1);
133    lua::lua_pop(L, 1);
134    r
135}
136
137#[inline(always)]
138pub unsafe fn luaL_unref(L: *mut lua_State, t: c_int, r#ref: c_int) {
139    assert_eq!(t, LUA_REGISTRYINDEX);
140    lua::lua_unref(L, r#ref)
141}
142
143pub unsafe fn luaL_sandbox(L: *mut lua_State, enabled: c_int) {
144    use super::lua::*;
145
146    // set all libraries to read-only
147    lua_pushnil(L);
148    while lua_next(L, LUA_GLOBALSINDEX) != 0 {
149        if lua_istable(L, -1) != 0 {
150            lua_setreadonly(L, -1, enabled);
151        }
152        lua_pop(L, 1);
153    }
154
155    // set all builtin metatables to read-only
156    lua_pushliteral(L, c"");
157    if lua_getmetatable(L, -1) != 0 {
158        lua_setreadonly(L, -1, enabled);
159        lua_pop(L, 2);
160    } else {
161        lua_pop(L, 1);
162    }
163
164    // set globals to readonly and activate safeenv since the env is immutable
165    lua_setreadonly(L, LUA_GLOBALSINDEX, enabled);
166    lua_setsafeenv(L, LUA_GLOBALSINDEX, enabled);
167}
168
169//
170// Generic Buffer Manipulation
171//
172
173/// Buffer size used for on-stack string operations. This limit depends on native stack size.
174pub const LUA_BUFFERSIZE: usize = 512;
175
176#[repr(C)]
177pub struct luaL_Strbuf {
178    p: *mut c_char,   // current position in buffer
179    end: *mut c_char, // end of the current buffer
180    L: *mut lua_State,
181    storage: *mut c_void, // TString
182    buffer: [c_char; LUA_BUFFERSIZE],
183}
184
185// For compatibility
186pub type luaL_Buffer = luaL_Strbuf;
187
188unsafe extern "C-unwind" {
189    pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Strbuf);
190    pub fn luaL_buffinitsize(L: *mut lua_State, B: *mut luaL_Strbuf, size: usize) -> *mut c_char;
191    pub fn luaL_prepbuffsize(B: *mut luaL_Strbuf, size: usize) -> *mut c_char;
192    pub fn luaL_addlstring(B: *mut luaL_Strbuf, s: *const c_char, l: usize);
193    pub fn luaL_addvalue(B: *mut luaL_Strbuf);
194    pub fn luaL_addvalueany(B: *mut luaL_Strbuf, idx: c_int);
195    pub fn luaL_pushresult(B: *mut luaL_Strbuf);
196    pub fn luaL_pushresultsize(B: *mut luaL_Strbuf, size: usize);
197}
198
199pub unsafe fn luaL_addchar(B: *mut luaL_Strbuf, c: c_char) {
200    if (*B).p >= (*B).end {
201        luaL_prepbuffsize(B, 1);
202    }
203    *(*B).p = c;
204    (*B).p = (*B).p.add(1);
205}
206
207pub unsafe fn luaL_addstring(B: *mut luaL_Strbuf, s: *const c_char) {
208    // Calculate length of s
209    let mut len = 0;
210    while *s.add(len) != 0 {
211        len += 1;
212    }
213    luaL_addlstring(B, s, len);
214}
215
216pub unsafe fn luaL_addunsigned(B: *mut luaL_Strbuf, mut n: lua_Unsigned) {
217    let mut buf: [c_char; 32] = [0; 32];
218    let mut i = 32;
219    loop {
220        i -= 1;
221        let digit = (n % 10) as u8;
222        buf[i] = (b'0' + digit) as c_char;
223        n /= 10;
224        if n == 0 {
225            break;
226        }
227    }
228    luaL_addlstring(B, buf.as_ptr().add(i), 32 - i);
229}