mlua_sys/lua51/
lua.rs

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