mlua_sys/lua52/
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_uchar, c_uint, c_void};
6use std::ptr;
7
8// Mark for precompiled code (`<esc>Lua`)
9pub const LUA_SIGNATURE: &[u8] = b"\x1bLua";
10
11// Option for multiple returns in 'lua_pcall' and 'lua_call'
12pub const LUA_MULTRET: c_int = -1;
13
14// Size of the Lua stack
15pub const LUAI_MAXSTACK: c_int = 1000000;
16
17//
18// Pseudo-indices
19//
20pub const LUA_REGISTRYINDEX: c_int = -LUAI_MAXSTACK - 1000;
21
22pub const fn lua_upvalueindex(i: c_int) -> c_int {
23    LUA_REGISTRYINDEX - i
24}
25
26//
27// Thread status
28//
29pub const LUA_OK: c_int = 0;
30pub const LUA_YIELD: c_int = 1;
31pub const LUA_ERRRUN: c_int = 2;
32pub const LUA_ERRSYNTAX: c_int = 3;
33pub const LUA_ERRMEM: c_int = 4;
34pub const LUA_ERRGCMM: c_int = 5;
35pub const LUA_ERRERR: c_int = 6;
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
59pub const LUA_NUMTAGS: c_int = 9;
60
61/// Minimum Lua stack available to a C function
62pub const LUA_MINSTACK: c_int = 20;
63
64// Predefined values in the registry
65pub const LUA_RIDX_MAINTHREAD: lua_Integer = 1;
66pub const LUA_RIDX_GLOBALS: lua_Integer = 2;
67pub const LUA_RIDX_LAST: lua_Integer = LUA_RIDX_GLOBALS;
68
69/// A Lua number, usually equivalent to `f64`
70pub type lua_Number = c_double;
71
72/// A Lua integer, usually equivalent to `i64`
73#[cfg(target_pointer_width = "32")]
74pub type lua_Integer = i32;
75#[cfg(target_pointer_width = "64")]
76pub type lua_Integer = i64;
77
78/// A Lua unsigned integer, equivalent to `u32` in Lua 5.2
79pub type lua_Unsigned = c_uint;
80
81/// Type for native C functions that can be passed to Lua
82pub type lua_CFunction = unsafe extern "C-unwind" fn(L: *mut lua_State) -> c_int;
83
84// Type for functions that read/write blocks when loading/dumping Lua chunks
85#[rustfmt::skip]
86pub type lua_Reader =
87    unsafe extern "C-unwind" fn(L: *mut lua_State, ud: *mut c_void, sz: *mut usize) -> *const c_char;
88#[rustfmt::skip]
89pub type lua_Writer =
90    unsafe extern "C-unwind" fn(L: *mut lua_State, p: *const c_void, sz: usize, ud: *mut c_void) -> c_int;
91
92/// Type for memory-allocation functions (no unwinding)
93#[rustfmt::skip]
94pub type lua_Alloc =
95    unsafe extern "C" fn(ud: *mut c_void, ptr: *mut c_void, osize: usize, nsize: usize) -> *mut c_void;
96
97#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
98unsafe extern "C-unwind" {
99    //
100    // State manipulation
101    //
102    pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
103    pub fn lua_close(L: *mut lua_State);
104    pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
105
106    pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
107
108    pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
109
110    //
111    // Basic stack manipulation
112    //
113    pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
114    pub fn lua_gettop(L: *mut lua_State) -> c_int;
115    pub fn lua_settop(L: *mut lua_State, idx: c_int);
116    pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
117    pub fn lua_remove(L: *mut lua_State, idx: c_int);
118    pub fn lua_insert(L: *mut lua_State, idx: c_int);
119    pub fn lua_replace(L: *mut lua_State, idx: c_int);
120    pub fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int);
121    pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
122
123    pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
124
125    //
126    // Access functions (stack -> C)
127    //
128    pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
129    pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
130    pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
131    pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
132    pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
133    pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
134
135    pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
136    #[link_name = "lua_tointegerx"]
137    pub fn lua_tointegerx_(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
138    pub fn lua_tounsignedx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Unsigned;
139    pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
140    pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
141    pub fn lua_rawlen(L: *mut lua_State, idx: c_int) -> usize;
142    pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
143    pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
144    pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
145    pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
146}
147
148//
149// Comparison and arithmetic functions
150//
151pub const LUA_OPADD: c_int = 0;
152pub const LUA_OPSUB: c_int = 1;
153pub const LUA_OPMUL: c_int = 2;
154pub const LUA_OPDIV: c_int = 3;
155pub const LUA_OPMOD: c_int = 4;
156pub const LUA_OPPOW: c_int = 5;
157pub const LUA_OPUNM: c_int = 6;
158
159pub const LUA_OPEQ: c_int = 0;
160pub const LUA_OPLT: c_int = 1;
161pub const LUA_OPLE: c_int = 2;
162
163#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
164unsafe extern "C-unwind" {
165    pub fn lua_arith(L: *mut lua_State, op: c_int);
166    pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
167    pub fn lua_compare(L: *mut lua_State, idx1: c_int, idx2: c_int, op: c_int) -> c_int;
168}
169
170#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
171unsafe extern "C-unwind" {
172    //
173    // Push functions (C -> stack)
174    //
175    pub fn lua_pushnil(L: *mut lua_State);
176    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
177    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
178    pub fn lua_pushunsigned(L: *mut lua_State, n: lua_Unsigned);
179    #[link_name = "lua_pushlstring"]
180    pub fn lua_pushlstring_(L: *mut lua_State, s: *const c_char, l: usize) -> *const c_char;
181    pub fn lua_pushstring(L: *mut lua_State, s: *const c_char) -> *const c_char;
182    // lua_pushvfstring
183    pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
184    pub fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, n: c_int);
185    pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
186    pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
187    pub fn lua_pushthread(L: *mut lua_State) -> c_int;
188
189    //
190    // Get functions (Lua -> stack)
191    //
192    #[link_name = "lua_getglobal"]
193    pub fn lua_getglobal_(L: *mut lua_State, name: *const c_char);
194    #[link_name = "lua_gettable"]
195    pub fn lua_gettable_(L: *mut lua_State, idx: c_int);
196    #[link_name = "lua_getfield"]
197    pub fn lua_getfield_(L: *mut lua_State, idx: c_int, k: *const c_char);
198    #[link_name = "lua_rawget"]
199    pub fn lua_rawget_(L: *mut lua_State, idx: c_int);
200    #[link_name = "lua_rawgeti"]
201    pub fn lua_rawgeti_(L: *mut lua_State, idx: c_int, n: c_int);
202    #[link_name = "lua_rawgetp"]
203    pub fn lua_rawgetp_(L: *mut lua_State, idx: c_int, p: *const c_void);
204    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
205    pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void;
206    pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
207    #[link_name = "lua_getuservalue"]
208    pub fn lua_getuservalue_(L: *mut lua_State, idx: c_int);
209
210    //
211    // Set functions (stack -> Lua)
212    //
213    pub fn lua_setglobal(L: *mut lua_State, var: *const c_char);
214    pub fn lua_settable(L: *mut lua_State, idx: c_int);
215    pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
216    pub fn lua_rawset(L: *mut lua_State, idx: c_int);
217    #[link_name = "lua_rawseti"]
218    pub fn lua_rawseti_(L: *mut lua_State, idx: c_int, n: c_int);
219    pub fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void);
220    pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
221    pub fn lua_setuservalue(L: *mut lua_State, idx: c_int);
222
223    //
224    // 'load' and 'call' functions (load and run Lua code)
225    //
226    pub fn lua_callk(L: *mut lua_State, nargs: c_int, nresults: c_int, ctx: c_int, k: Option<lua_CFunction>);
227    pub fn lua_pcallk(
228        L: *mut lua_State,
229        nargs: c_int,
230        nresults: c_int,
231        errfunc: c_int,
232        ctx: c_int,
233        k: Option<lua_CFunction>,
234    ) -> c_int;
235
236    pub fn lua_getctx(L: *mut lua_State, ctx: *mut c_int) -> c_int;
237
238    pub fn lua_load(
239        L: *mut lua_State,
240        reader: lua_Reader,
241        data: *mut c_void,
242        chunkname: *const c_char,
243        mode: *const c_char,
244    ) -> c_int;
245    #[link_name = "lua_dump"]
246    pub fn lua_dump_(L: *mut lua_State, writer: lua_Writer, data: *mut c_void) -> c_int;
247}
248
249#[inline(always)]
250pub unsafe fn lua_call(L: *mut lua_State, n: c_int, r: c_int) {
251    lua_callk(L, n, r, 0, None)
252}
253
254#[inline(always)]
255pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
256    lua_pcallk(L, n, r, f, 0, None)
257}
258
259#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
260unsafe extern "C-unwind" {
261    //
262    // Coroutine functions
263    //
264    pub fn lua_yieldk(L: *mut lua_State, nresults: c_int, ctx: c_int, k: Option<lua_CFunction>) -> c_int;
265    #[link_name = "lua_resume"]
266    pub fn lua_resume_(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
267    pub fn lua_status(L: *mut lua_State) -> c_int;
268}
269
270#[inline(always)]
271pub unsafe fn lua_yield(L: *mut lua_State, n: c_int) -> c_int {
272    lua_yieldk(L, n, 0, None)
273}
274
275//
276// Garbage-collection function and options
277//
278pub const LUA_GCSTOP: c_int = 0;
279pub const LUA_GCRESTART: c_int = 1;
280pub const LUA_GCCOLLECT: c_int = 2;
281pub const LUA_GCCOUNT: c_int = 3;
282pub const LUA_GCCOUNTB: c_int = 4;
283pub const LUA_GCSTEP: c_int = 5;
284pub const LUA_GCSETPAUSE: c_int = 6;
285pub const LUA_GCSETSTEPMUL: c_int = 7;
286pub const LUA_GCSETMAJORINC: c_int = 8;
287pub const LUA_GCISRUNNING: c_int = 9;
288pub const LUA_GCGEN: c_int = 10;
289pub const LUA_GCINC: c_int = 11;
290
291#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
292unsafe extern "C-unwind" {
293    pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
294}
295
296#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
297unsafe extern "C-unwind" {
298    //
299    // Miscellaneous functions
300    //
301    #[link_name = "lua_error"]
302    fn lua_error_(L: *mut lua_State) -> c_int;
303    pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
304    pub fn lua_concat(L: *mut lua_State, n: c_int);
305    pub fn lua_len(L: *mut lua_State, idx: c_int);
306    pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
307    pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
308}
309
310// lua_error does not return but is declared to return int, and Rust translates
311// ! to void which can cause link-time errors if the platform linker is aware
312// of return types and requires they match (for example: wasm does this).
313#[inline(always)]
314pub unsafe fn lua_error(L: *mut lua_State) -> ! {
315    lua_error_(L);
316    unreachable!();
317}
318
319//
320// Some useful macros (implemented as Rust functions)
321//
322#[inline(always)]
323pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
324    lua_tonumberx(L, i, ptr::null_mut())
325}
326
327#[inline(always)]
328pub unsafe fn lua_tointeger_(L: *mut lua_State, i: c_int) -> lua_Integer {
329    lua_tointegerx_(L, i, ptr::null_mut())
330}
331
332#[inline(always)]
333pub unsafe fn lua_tounsigned(L: *mut lua_State, i: c_int) -> lua_Unsigned {
334    lua_tounsignedx(L, i, ptr::null_mut())
335}
336
337#[inline(always)]
338pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
339    lua_settop(L, -n - 1)
340}
341
342#[inline(always)]
343pub unsafe fn lua_newtable(L: *mut lua_State) {
344    lua_createtable(L, 0, 0)
345}
346
347#[inline(always)]
348pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
349    lua_pushcfunction(L, f);
350    lua_setglobal(L, n)
351}
352
353#[inline(always)]
354pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
355    lua_pushcclosure(L, f, 0)
356}
357
358#[inline(always)]
359pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
360    (lua_type(L, n) == LUA_TFUNCTION) as c_int
361}
362
363#[inline(always)]
364pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
365    (lua_type(L, n) == LUA_TTABLE) as c_int
366}
367
368#[inline(always)]
369pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
370    (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
371}
372
373#[inline(always)]
374pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
375    (lua_type(L, n) == LUA_TNIL) as c_int
376}
377
378#[inline(always)]
379pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
380    (lua_type(L, n) == LUA_TBOOLEAN) as c_int
381}
382
383#[inline(always)]
384pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
385    (lua_type(L, n) == LUA_TTHREAD) as c_int
386}
387
388#[inline(always)]
389pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
390    (lua_type(L, n) == LUA_TNONE) as c_int
391}
392
393#[inline(always)]
394pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
395    (lua_type(L, n) <= 0) as c_int
396}
397
398#[inline(always)]
399pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
400    lua_pushstring(L, s.as_ptr());
401}
402
403#[inline(always)]
404pub unsafe fn lua_pushglobaltable(L: *mut lua_State) {
405    lua_rawgeti_(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS as _)
406}
407
408#[inline(always)]
409pub unsafe fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void {
410    if lua_islightuserdata(L, idx) != 0 {
411        return lua_touserdata(L, idx);
412    }
413    ptr::null_mut()
414}
415
416#[inline(always)]
417pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
418    lua_tolstring(L, i, ptr::null_mut())
419}
420
421#[inline(always)]
422pub unsafe fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int) {
423    lua_pushvalue(from, idx);
424    lua_xmove(from, to, 1);
425}
426
427//
428// Debug API
429//
430
431// Maximum size for the description of the source of a function in debug information.
432const LUA_IDSIZE: usize = 60;
433
434// Event codes
435pub const LUA_HOOKCALL: c_int = 0;
436pub const LUA_HOOKRET: c_int = 1;
437pub const LUA_HOOKLINE: c_int = 2;
438pub const LUA_HOOKCOUNT: c_int = 3;
439pub const LUA_HOOKTAILCALL: c_int = 4;
440
441// Event masks
442pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
443pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
444pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
445pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
446
447/// Type for functions to be called on debug events.
448pub type lua_Hook = unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug);
449
450#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
451unsafe extern "C-unwind" {
452    pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
453    pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
454    pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
455    pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
456    pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
457    pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
458
459    pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
460    pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);
461
462    pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int) -> c_int;
463    pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
464    pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
465    pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
466}
467
468#[repr(C)]
469pub struct lua_Debug {
470    pub event: c_int,
471    pub name: *const c_char,
472    pub namewhat: *const c_char,
473    pub what: *const c_char,
474    pub source: *const c_char,
475    pub currentline: c_int,
476    pub linedefined: c_int,
477    pub lastlinedefined: c_int,
478    pub nups: c_uchar,
479    pub nparams: c_uchar,
480    pub isvararg: c_char,
481    pub istailcall: c_char,
482    pub short_src: [c_char; LUA_IDSIZE],
483    // lua.h mentions this is for private use
484    i_ci: *mut c_void,
485}