lu_sys/
lualib.rs

1#![allow(clippy::missing_safety_doc)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4
5use std::{
6    ffi::{CStr, c_char, c_double, c_float, c_int, c_uint, c_void},
7    ptr::null_mut,
8};
9
10use super::*;
11
12pub unsafe fn luaL_typeerror(L: *mut lua_State, narg: c_int, tname: *const c_char) -> ! {
13    unsafe { luaL_typeerrorL(L, narg, tname) }
14}
15
16pub unsafe fn luaL_argerror(L: *mut lua_State, narg: c_int, extramsg: *const c_char) -> ! {
17    unsafe { luaL_argerrorL(L, narg, extramsg) }
18}
19
20#[repr(C)]
21pub struct luaL_Reg {
22    pub name: *const c_char,
23    pub func: lua_CFunction,
24}
25
26unsafe extern "C-unwind" {
27    pub fn luaL_register(L: *mut lua_State, libname: *const c_char, l: *const luaL_Reg);
28
29    pub fn luaL_getmetafield(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
30
31    pub fn luaL_callmeta(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
32
33    pub fn luaL_typeerrorL(L: *mut lua_State, narg: c_int, tname: *const c_char) -> !;
34
35    pub fn luaL_argerrorL(L: *mut lua_State, narg: c_int, extramsg: *const c_char) -> !;
36
37    pub fn luaL_checklstring(L: *mut lua_State, numArg: c_int, l: *mut usize) -> *const c_char;
38
39    pub fn luaL_optlstring(
40        L: *mut lua_State,
41        numArg: c_int,
42        def: *const c_char,
43        l: *mut usize,
44    ) -> *const c_char;
45
46    pub fn luaL_checknumber(L: *mut lua_State, numArg: c_int) -> c_double;
47
48    pub fn luaL_optnumber(L: *mut lua_State, nArg: c_int, def: c_double) -> c_double;
49
50    pub fn luaL_checkboolean(L: *mut lua_State, narg: c_int) -> c_int;
51
52    pub fn luaL_optboolean(L: *mut lua_State, narg: c_int, def: c_int) -> c_int;
53
54    pub fn luaL_checkinteger(L: *mut lua_State, numArg: c_int) -> c_int;
55
56    pub fn luaL_optinteger(L: *mut lua_State, nArg: c_int, def: c_int) -> c_int;
57
58    pub fn luaL_checkunsigned(L: *mut lua_State, numArg: c_int) -> c_uint;
59
60    pub fn luaL_optunsigned(L: *mut lua_State, numArg: c_int, def: c_uint) -> c_uint;
61
62    pub fn luaL_checkvector(L: *mut lua_State, narg: c_int) -> *const c_float;
63
64    pub fn luaL_optvector(L: *mut lua_State, narg: c_int, def: *const c_float) -> *const c_float;
65
66    pub fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char);
67
68    pub fn luaL_checktype(L: *mut lua_State, narg: c_int, t: c_int);
69
70    pub fn luaL_checkany(L: *mut lua_State, narg: c_int);
71
72    pub fn luaL_newmetatable(L: *mut lua_State, tname: *const c_char) -> c_int;
73
74    pub fn luaL_checkudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void;
75
76    pub fn luaL_checkbuffer(L: *mut lua_State, narg: c_int, len: *mut usize) -> *mut c_void;
77
78    pub fn luaL_where(L: *mut lua_State, lvl: c_int);
79
80    pub fn luaL_checkoption(
81        L: *mut lua_State,
82        narg: c_int,
83        def: *const c_char,
84        lst: *const *const c_char,
85    ) -> c_int;
86
87    pub fn luaL_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
88
89    pub fn luaL_newstate() -> *mut lua_State;
90
91    pub fn luaL_findtable(
92        L: *mut lua_State,
93        idx: c_int,
94        fname: *const c_char,
95        szhint: c_int,
96    ) -> *const c_char;
97
98    pub fn luaL_typename(L: *mut lua_State, idx: c_int) -> *const c_char;
99
100    pub fn luaL_callyieldable(L: *mut lua_State, nargs: c_int, nresults: c_int) -> c_int;
101}
102
103pub unsafe fn luaL_argcheck(L: *mut lua_State, cond: bool, arg: c_int, extramsg: *const c_char) {
104    if !cond {
105        unsafe { luaL_argerror(L, arg, extramsg) }
106    }
107}
108
109pub unsafe fn luaL_argexpected(L: *mut lua_State, cond: bool, arg: c_int, tname: *const c_char) {
110    if !cond {
111        unsafe { luaL_typeerror(L, arg, tname) }
112    }
113}
114
115pub unsafe fn luaL_checkstring(L: *mut lua_State, n: c_int) -> *const c_char {
116    unsafe { luaL_checklstring(L, n, null_mut()) }
117}
118
119pub unsafe fn luaL_optstring(L: *mut lua_State, n: c_int, d: *const c_char) -> *const c_char {
120    unsafe { luaL_optlstring(L, n, d, null_mut()) }
121}
122
123pub unsafe fn luaL_getmetatable(L: *mut lua_State, n: *const c_char) -> lua_Type {
124    unsafe { lua_getfield(L, LUA_REGISTRYINDEX, n) }
125}
126
127pub unsafe fn luaL_opt<T>(
128    L: *mut lua_State,
129    f: fn(L: *mut lua_State, n: c_int) -> T,
130    n: c_int,
131    d: T,
132) -> T {
133    unsafe { if lua_isnoneornil(L, n) { d } else { f(L, n) } }
134}
135
136#[repr(C)]
137pub struct luaL_Strbuf {
138    pub p: *mut c_char,
139    pub end: *mut c_char,
140    pub L: *mut lua_State,
141    pub storage: *mut c_void, // !!!
142    pub buffer: [c_char; LUA_BUFFERSIZE as usize],
143}
144
145pub unsafe fn luaL_addchar(B: *mut luaL_Strbuf, c: c_char) {
146    unsafe {
147        if (*B).p >= (*B).end {
148            luaL_prepbuffsize(B, 1);
149        }
150
151        (*B).p.write(c);
152        (*B).p = (*B).p.add(1);
153    }
154}
155
156pub unsafe fn luaL_addstring(B: *mut luaL_Strbuf, s: *const c_char) {
157    unsafe { luaL_addlstring(B, s, libc::strlen(s)) }
158}
159
160unsafe extern "C-unwind" {
161    pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Strbuf);
162
163    pub fn luaL_buffinitsize(L: *mut lua_State, B: *mut luaL_Strbuf, size: usize) -> *mut c_char;
164
165    pub fn luaL_prepbuffsize(B: *mut luaL_Strbuf, size: usize) -> *mut c_char;
166
167    pub fn luaL_addlstring(B: *mut luaL_Strbuf, s: *const c_char, l: usize);
168
169    pub fn luaL_addvalue(B: *mut luaL_Strbuf);
170
171    pub fn luaL_addvalueany(B: *mut luaL_Strbuf, idx: c_int);
172
173    pub fn luaL_pushresult(B: *mut luaL_Strbuf);
174
175    pub fn luaL_pushresultsize(B: *mut luaL_Strbuf, size: usize);
176}
177
178pub const LUA_COLIBNAME: &CStr = c"coroutine";
179
180pub const LUA_TABLIBNAME: &CStr = c"table";
181
182pub const LUA_OSLIBNAME: &CStr = c"os";
183
184pub const LUA_STRLIBNAME: &CStr = c"string";
185
186pub const LUA_BITLIBNAME: &CStr = c"bit32";
187
188pub const LUA_BUFFERLIBNAME: &CStr = c"buffer";
189
190pub const LUA_UTF8LIBNAME: &CStr = c"utf8";
191
192pub const LUA_MATHLIBNAME: &CStr = c"math";
193
194pub const LUA_DBLIBNAME: &CStr = c"debug";
195
196pub const LUA_VECLIBNAME: &CStr = c"vector";
197
198unsafe extern "C-unwind" {
199    pub fn luaopen_base(L: *mut lua_State) -> c_int;
200
201    pub fn luaopen_coroutine(L: *mut lua_State) -> c_int;
202
203    pub fn luaopen_table(L: *mut lua_State) -> c_int;
204
205    pub fn luaopen_os(L: *mut lua_State) -> c_int;
206
207    pub fn luaopen_string(L: *mut lua_State) -> c_int;
208
209    pub fn luaopen_bit32(L: *mut lua_State) -> c_int;
210
211    pub fn luaopen_buffer(L: *mut lua_State) -> c_int;
212
213    pub fn luaopen_utf8(L: *mut lua_State) -> c_int;
214
215    pub fn luaopen_math(L: *mut lua_State) -> c_int;
216
217    pub fn luaopen_debug(L: *mut lua_State) -> c_int;
218
219    pub fn luaopen_vector(L: *mut lua_State) -> c_int;
220
221    pub fn luaL_openlibs(L: *mut lua_State);
222
223    pub fn luaL_sandbox(L: *mut lua_State);
224
225    pub fn luaL_sandboxthread(L: *mut lua_State);
226}