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