mlua_sys/lua51/
lua.rs

1//! Contains definitions from `lua.h`.
2
3use std::ffi::CStr;
4use std::marker::{PhantomData, PhantomPinned};
5use std::os::raw::{c_char, c_double, c_int, c_void};
6use std::ptr;
7
8// Mark for precompiled code (`<esc>Lua`)
9#[cfg(not(feature = "luajit"))]
10pub const LUA_SIGNATURE: &[u8] = b"\x1bLua";
11#[cfg(feature = "luajit")]
12pub const LUA_SIGNATURE: &[u8] = b"\x1bLJ";
13
14// Option for multiple returns in 'lua_pcall' and 'lua_call'
15pub const LUA_MULTRET: c_int = -1;
16
17//
18// Pseudo-indices
19//
20pub const LUA_REGISTRYINDEX: c_int = -10000;
21pub const LUA_ENVIRONINDEX: c_int = -10001;
22pub const LUA_GLOBALSINDEX: c_int = -10002;
23
24pub const fn lua_upvalueindex(i: c_int) -> c_int {
25    LUA_GLOBALSINDEX - i
26}
27
28//
29// Thread status
30//
31pub const LUA_OK: c_int = 0;
32pub const LUA_YIELD: c_int = 1;
33pub const LUA_ERRRUN: c_int = 2;
34pub const LUA_ERRSYNTAX: c_int = 3;
35pub const LUA_ERRMEM: c_int = 4;
36pub const LUA_ERRERR: c_int = 5;
37
38/// A raw Lua state associated with a thread.
39#[repr(C)]
40pub struct lua_State {
41    _data: [u8; 0],
42    _marker: PhantomData<(*mut u8, PhantomPinned)>,
43}
44
45//
46// Basic types
47//
48pub const LUA_TNONE: c_int = -1;
49
50pub const LUA_TNIL: c_int = 0;
51pub const LUA_TBOOLEAN: c_int = 1;
52pub const LUA_TLIGHTUSERDATA: c_int = 2;
53pub const LUA_TNUMBER: c_int = 3;
54pub const LUA_TSTRING: c_int = 4;
55pub const LUA_TTABLE: c_int = 5;
56pub const LUA_TFUNCTION: c_int = 6;
57pub const LUA_TUSERDATA: c_int = 7;
58pub const LUA_TTHREAD: c_int = 8;
59
60/// Type produced by LuaJIT FFI module
61#[cfg(feature = "luajit")]
62pub const LUA_TCDATA: c_int = 10;
63
64/// Minimum Lua stack available to a C function
65pub const LUA_MINSTACK: c_int = 20;
66
67/// A Lua number, usually equivalent to `f64`
68pub type lua_Number = c_double;
69
70/// A Lua integer, usually equivalent to `i64`
71#[cfg(target_pointer_width = "32")]
72pub type lua_Integer = i32;
73#[cfg(target_pointer_width = "64")]
74pub type lua_Integer = i64;
75
76/// Type for native C functions that can be passed to Lua.
77pub type lua_CFunction = unsafe extern "C-unwind" fn(L: *mut lua_State) -> c_int;
78
79// Type for functions that read/write blocks when loading/dumping Lua chunks
80#[rustfmt::skip]
81pub type lua_Reader =
82    unsafe extern "C-unwind" fn(L: *mut lua_State, ud: *mut c_void, sz: *mut usize) -> *const c_char;
83#[rustfmt::skip]
84pub type lua_Writer =
85    unsafe extern "C-unwind" fn(L: *mut lua_State, p: *const c_void, sz: usize, ud: *mut c_void) -> c_int;
86
87/// Type for memory-allocation functions (no unwinding)
88#[rustfmt::skip]
89pub type lua_Alloc =
90    unsafe extern "C" fn(ud: *mut c_void, ptr: *mut c_void, osize: usize, nsize: usize) -> *mut c_void;
91
92#[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))]
93unsafe extern "C-unwind" {
94    //
95    // State manipulation
96    //
97    pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
98    pub fn lua_close(L: *mut lua_State);
99    pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
100
101    pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
102
103    //
104    // Basic stack manipulation
105    //
106    pub fn lua_gettop(L: *mut lua_State) -> c_int;
107    pub fn lua_settop(L: *mut lua_State, idx: c_int);
108    pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
109    pub fn lua_remove(L: *mut lua_State, idx: c_int);
110    pub fn lua_insert(L: *mut lua_State, idx: c_int);
111    pub fn lua_replace(L: *mut lua_State, idx: c_int);
112    pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
113
114    pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
115
116    //
117    // Access functions (stack -> C)
118    //
119    pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
120    pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
121    pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
122    pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
123    pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
124    pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
125
126    pub fn lua_equal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
127    pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
128    pub fn lua_lessthan(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
129
130    pub fn lua_tonumber(L: *mut lua_State, idx: c_int) -> lua_Number;
131    #[link_name = "lua_tointeger"]
132    pub fn lua_tointeger_(L: *mut lua_State, idx: c_int) -> lua_Integer;
133    pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
134    pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
135    pub fn lua_objlen(L: *mut lua_State, idx: c_int) -> usize;
136    pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
137    pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
138    pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
139    pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
140
141    //
142    // Push functions (C -> stack)
143    //
144    pub fn lua_pushnil(L: *mut lua_State);
145    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
146    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
147    #[link_name = "lua_pushlstring"]
148    pub fn lua_pushlstring_(L: *mut lua_State, s: *const c_char, l: usize);
149    #[link_name = "lua_pushstring"]
150    pub fn lua_pushstring_(L: *mut lua_State, s: *const c_char);
151    // lua_pushvfstring
152    pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
153    pub fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, n: c_int);
154    pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
155    pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
156    pub fn lua_pushthread(L: *mut lua_State) -> c_int;
157
158    //
159    // Get functions (Lua -> stack)
160    //
161    #[link_name = "lua_gettable"]
162    pub fn lua_gettable_(L: *mut lua_State, idx: c_int);
163    #[link_name = "lua_getfield"]
164    pub fn lua_getfield_(L: *mut lua_State, idx: c_int, k: *const c_char);
165    #[link_name = "lua_rawget"]
166    pub fn lua_rawget_(L: *mut lua_State, idx: c_int);
167    #[link_name = "lua_rawgeti"]
168    pub fn lua_rawgeti_(L: *mut lua_State, idx: c_int, n: c_int);
169    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
170    pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void;
171    pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
172    pub fn lua_getfenv(L: *mut lua_State, idx: c_int);
173
174    //
175    // Set functions (stack -> Lua)
176    //
177    pub fn lua_settable(L: *mut lua_State, idx: c_int);
178    pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
179    pub fn lua_rawset(L: *mut lua_State, idx: c_int);
180    #[link_name = "lua_rawseti"]
181    pub fn lua_rawseti_(L: *mut lua_State, idx: c_int, n: c_int);
182    pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
183    pub fn lua_setfenv(L: *mut lua_State, idx: c_int) -> c_int;
184
185    //
186    // 'load' and 'call' functions (load and run Lua code)
187    //
188    pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
189    pub fn lua_pcall(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
190    pub fn lua_cpcall(L: *mut lua_State, f: lua_CFunction, ud: *mut c_void) -> c_int;
191    pub fn lua_load(
192        L: *mut lua_State,
193        reader: lua_Reader,
194        data: *mut c_void,
195        chunkname: *const c_char,
196    ) -> c_int;
197
198    #[link_name = "lua_dump"]
199    pub fn lua_dump_(L: *mut lua_State, writer: lua_Writer, data: *mut c_void) -> c_int;
200
201    //
202    // Coroutine functions
203    //
204    pub fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int;
205    #[link_name = "lua_resume"]
206    pub fn lua_resume_(L: *mut lua_State, narg: c_int) -> c_int;
207    pub fn lua_status(L: *mut lua_State) -> c_int;
208}
209
210//
211// Garbage-collection function and options
212//
213pub const LUA_GCSTOP: c_int = 0;
214pub const LUA_GCRESTART: c_int = 1;
215pub const LUA_GCCOLLECT: c_int = 2;
216pub const LUA_GCCOUNT: c_int = 3;
217pub const LUA_GCCOUNTB: c_int = 4;
218pub const LUA_GCSTEP: c_int = 5;
219pub const LUA_GCSETPAUSE: c_int = 6;
220pub const LUA_GCSETSTEPMUL: c_int = 7;
221
222#[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))]
223unsafe extern "C-unwind" {
224    pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
225}
226
227//
228// Miscellaneous functions
229//
230#[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))]
231unsafe extern "C-unwind" {
232    #[link_name = "lua_error"]
233    fn lua_error_(L: *mut lua_State) -> c_int;
234    pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
235    pub fn lua_concat(L: *mut lua_State, n: c_int);
236    pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
237    pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
238}
239
240// lua_error does not return but is declared to return int, and Rust translates
241// ! to void which can cause link-time errors if the platform linker is aware
242// of return types and requires they match (for example: wasm does this).
243#[inline(always)]
244pub unsafe fn lua_error(L: *mut lua_State) -> ! {
245    lua_error_(L);
246    unreachable!();
247}
248
249//
250// Some useful macros (implemented as Rust functions)
251//
252#[inline(always)]
253pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
254    lua_settop(L, -n - 1)
255}
256
257#[inline(always)]
258pub unsafe fn lua_newtable(L: *mut lua_State) {
259    lua_createtable(L, 0, 0)
260}
261
262#[inline(always)]
263pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
264    lua_pushcfunction(L, f);
265    lua_setglobal(L, n)
266}
267
268#[inline(always)]
269pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
270    lua_pushcclosure(L, f, 0)
271}
272
273// TODO: lua_strlen
274
275#[inline(always)]
276pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
277    (lua_type(L, n) == LUA_TFUNCTION) as c_int
278}
279
280#[inline(always)]
281pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
282    (lua_type(L, n) == LUA_TTABLE) as c_int
283}
284
285#[inline(always)]
286pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
287    (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
288}
289
290#[inline(always)]
291pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
292    (lua_type(L, n) == LUA_TNIL) as c_int
293}
294
295#[inline(always)]
296pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
297    (lua_type(L, n) == LUA_TBOOLEAN) as c_int
298}
299
300#[inline(always)]
301pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
302    (lua_type(L, n) == LUA_TTHREAD) as c_int
303}
304
305#[inline(always)]
306pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
307    (lua_type(L, n) == LUA_TNONE) as c_int
308}
309
310#[inline(always)]
311pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
312    (lua_type(L, n) <= LUA_TNIL) as c_int
313}
314
315#[inline(always)]
316pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
317    lua_pushstring_(L, s.as_ptr());
318}
319
320#[inline(always)]
321pub unsafe fn lua_setglobal(L: *mut lua_State, var: *const c_char) {
322    lua_setfield(L, LUA_GLOBALSINDEX, var)
323}
324
325#[inline(always)]
326pub unsafe fn lua_getglobal_(L: *mut lua_State, var: *const c_char) {
327    lua_getfield_(L, LUA_GLOBALSINDEX, var)
328}
329
330#[inline(always)]
331pub unsafe fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void {
332    if lua_islightuserdata(L, idx) != 0 {
333        return lua_touserdata(L, idx);
334    }
335    ptr::null_mut()
336}
337
338#[inline(always)]
339pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
340    lua_tolstring(L, i, ptr::null_mut())
341}
342
343#[inline(always)]
344pub unsafe fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int) {
345    lua_pushvalue(from, idx);
346    lua_xmove(from, to, 1);
347}
348
349//
350// Debug API
351//
352
353// Maximum size for the description of the source of a function in debug information.
354const LUA_IDSIZE: usize = 60;
355
356// Event codes
357pub const LUA_HOOKCALL: c_int = 0;
358pub const LUA_HOOKRET: c_int = 1;
359pub const LUA_HOOKLINE: c_int = 2;
360pub const LUA_HOOKCOUNT: c_int = 3;
361pub const LUA_HOOKTAILCALL: c_int = 4;
362
363// Event masks
364pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
365pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
366pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
367pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
368
369/// Type for functions to be called on debug events.
370pub type lua_Hook = unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug);
371
372#[cfg_attr(all(windows, raw_dylib), link(name = "lua51", kind = "raw-dylib"))]
373unsafe extern "C-unwind" {
374    pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
375    pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
376    pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
377    pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
378    pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
379    pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
380
381    pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int) -> c_int;
382    pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
383    pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
384    pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
385}
386
387#[repr(C)]
388pub struct lua_Debug {
389    pub event: c_int,
390    pub name: *const c_char,
391    pub namewhat: *const c_char,
392    pub what: *const c_char,
393    pub source: *const c_char,
394    pub currentline: c_int,
395    pub nups: c_int,
396    pub linedefined: c_int,
397    pub lastlinedefined: c_int,
398    pub short_src: [c_char; LUA_IDSIZE],
399    // lua.h mentions this is for private use
400    i_ci: c_int,
401}