factorio_mlua_sys/lua52/
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_uint, c_void};
5use std::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//
17// Pseudo-indices
18//
19pub const LUA_REGISTRYINDEX: c_int = -LUAI_MAXSTACK - 1000;
20
21pub const fn lua_upvalueindex(i: c_int) -> c_int {
22    LUA_REGISTRYINDEX - i
23}
24
25//
26// Thread status
27//
28pub const LUA_OK: c_int = 0;
29pub const LUA_YIELD: c_int = 1;
30pub const LUA_ERRRUN: c_int = 2;
31pub const LUA_ERRSYNTAX: c_int = 3;
32pub const LUA_ERRMEM: c_int = 4;
33pub const LUA_ERRGCMM: c_int = 5;
34pub const LUA_ERRERR: c_int = 6;
35
36/// A raw Lua state associated with a thread.
37#[repr(C)]
38pub struct lua_State {
39    _data: [u8; 0],
40    _marker: PhantomData<(*mut u8, PhantomPinned)>,
41}
42
43//
44// Basic types
45//
46pub const LUA_TNONE: c_int = -1;
47
48pub const LUA_TNIL: c_int = 0;
49pub const LUA_TBOOLEAN: c_int = 1;
50pub const LUA_TLIGHTUSERDATA: c_int = 2;
51pub const LUA_TNUMBER: c_int = 3;
52pub const LUA_TSTRING: c_int = 4;
53pub const LUA_TTABLE: c_int = 5;
54pub const LUA_TFUNCTION: c_int = 6;
55pub const LUA_TUSERDATA: c_int = 7;
56pub const LUA_TTHREAD: c_int = 8;
57
58pub const LUA_NUMTAGS: c_int = 9;
59
60/// Minimum Lua stack available to a C function
61pub const LUA_MINSTACK: c_int = 20;
62
63// Predefined values in the registry
64pub const LUA_RIDX_MAINTHREAD: lua_Integer = 1;
65pub const LUA_RIDX_GLOBALS: lua_Integer = 2;
66pub const LUA_RIDX_LAST: lua_Integer = LUA_RIDX_GLOBALS;
67
68/// A Lua number, usually equivalent to `f64`
69pub type lua_Number = c_double;
70
71/// A Lua integer, usually equivalent to `i64`
72#[cfg(target_pointer_width = "32")]
73pub type lua_Integer = i32;
74#[cfg(target_pointer_width = "64")]
75pub type lua_Integer = i64;
76
77/// A Lua unsigned integer, equivalent to `u32` in Lua 5.2
78pub type lua_Unsigned = c_uint;
79
80/// Type for native C functions that can be passed to Lua
81pub type lua_CFunction = unsafe extern "C" fn(L: *mut lua_State) -> c_int;
82
83// Type for functions that read/write blocks when loading/dumping Lua chunks
84pub type lua_Reader =
85    unsafe extern "C" fn(L: *mut lua_State, ud: *mut c_void, sz: *mut usize) -> *const c_char;
86pub type lua_Writer =
87    unsafe extern "C" fn(L: *mut lua_State, p: *const c_void, sz: usize, ud: *mut c_void) -> c_int;
88
89/// Type for memory-allocation functions
90pub type lua_Alloc = unsafe extern "C" fn(
91    ud: *mut c_void,
92    ptr: *mut c_void,
93    osize: usize,
94    nsize: usize,
95) -> *mut c_void;
96
97extern "C" {
98    //
99    // State manipulation
100    //
101    pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
102    pub fn lua_close(L: *mut lua_State);
103    pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
104
105    pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
106
107    pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
108
109    //
110    // Basic stack manipulation
111    //
112    pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
113    pub fn lua_gettop(L: *mut lua_State) -> c_int;
114    pub fn lua_settop(L: *mut lua_State, idx: c_int);
115    pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
116    pub fn lua_remove(L: *mut lua_State, idx: c_int);
117    pub fn lua_insert(L: *mut lua_State, idx: c_int);
118    pub fn lua_replace(L: *mut lua_State, idx: c_int);
119    pub fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int);
120    pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
121
122    pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
123
124    //
125    // Access functions (stack -> C)
126    //
127    pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
128    pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
129    pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
130    pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
131    pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
132    pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
133
134    pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
135    #[link_name = "lua_tointegerx"]
136    pub fn lua_tointegerx_(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
137    pub fn lua_tounsignedx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Unsigned;
138    pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
139    pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
140    pub fn lua_rawlen(L: *mut lua_State, idx: c_int) -> usize;
141    pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
142    pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
143    pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
144    pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
145}
146
147//
148// Comparison and arithmetic functions
149//
150pub const LUA_OPADD: c_int = 0;
151pub const LUA_OPSUB: c_int = 1;
152pub const LUA_OPMUL: c_int = 2;
153pub const LUA_OPDIV: c_int = 3;
154pub const LUA_OPMOD: c_int = 4;
155pub const LUA_OPPOW: c_int = 5;
156pub const LUA_OPUNM: c_int = 6;
157
158extern "C" {
159    pub fn lua_arith(L: *mut lua_State, op: c_int);
160}
161
162pub const LUA_OPEQ: c_int = 0;
163pub const LUA_OPLT: c_int = 1;
164pub const LUA_OPLE: c_int = 2;
165
166extern "C" {
167    pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
168    pub fn lua_compare(L: *mut lua_State, idx1: c_int, idx2: c_int, op: c_int) -> c_int;
169}
170
171extern "C" {
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(
227        L: *mut lua_State,
228        nargs: c_int,
229        nresults: c_int,
230        ctx: c_int,
231        k: Option<lua_CFunction>,
232    );
233    pub fn lua_pcallk(
234        L: *mut lua_State,
235        nargs: c_int,
236        nresults: c_int,
237        errfunc: c_int,
238        ctx: c_int,
239        k: Option<lua_CFunction>,
240    ) -> c_int;
241
242    pub fn lua_getctx(L: *mut lua_State, ctx: *mut c_int) -> c_int;
243
244    pub fn lua_load(
245        L: *mut lua_State,
246        reader: lua_Reader,
247        data: *mut c_void,
248        chunkname: *const c_char,
249        mode: *const c_char,
250    ) -> c_int;
251    #[link_name = "lua_dump"]
252    pub fn lua_dump_(L: *mut lua_State, writer: lua_Writer, data: *mut c_void) -> c_int;
253}
254
255#[inline(always)]
256pub unsafe fn lua_call(L: *mut lua_State, n: c_int, r: c_int) {
257    lua_callk(L, n, r, 0, None)
258}
259
260#[inline(always)]
261pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
262    lua_pcallk(L, n, r, f, 0, None)
263}
264
265extern "C" {
266    //
267    // Coroutine functions
268    //
269    pub fn lua_yieldk(
270        L: *mut lua_State,
271        nresults: c_int,
272        ctx: c_int,
273        k: Option<lua_CFunction>,
274    ) -> c_int;
275    #[link_name = "lua_resume"]
276    pub fn lua_resume_(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
277    pub fn lua_status(L: *mut lua_State) -> c_int;
278}
279
280#[inline(always)]
281pub unsafe fn lua_yield(L: *mut lua_State, n: c_int) -> c_int {
282    lua_yieldk(L, n, 0, None)
283}
284
285//
286// Garbage-collection function and options
287//
288pub const LUA_GCSTOP: c_int = 0;
289pub const LUA_GCRESTART: c_int = 1;
290pub const LUA_GCCOLLECT: c_int = 2;
291pub const LUA_GCCOUNT: c_int = 3;
292pub const LUA_GCCOUNTB: c_int = 4;
293pub const LUA_GCSTEP: c_int = 5;
294pub const LUA_GCSETPAUSE: c_int = 6;
295pub const LUA_GCSETSTEPMUL: c_int = 7;
296pub const LUA_GCSETMAJORINC: c_int = 8;
297pub const LUA_GCISRUNNING: c_int = 9;
298pub const LUA_GCGEN: c_int = 10;
299pub const LUA_GCINC: c_int = 11;
300
301extern "C" {
302    pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
303}
304
305extern "C" {
306    //
307    // Miscellaneous functions
308    //
309    pub fn lua_error(L: *mut lua_State) -> !;
310    pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
311    pub fn lua_concat(L: *mut lua_State, n: c_int);
312    pub fn lua_len(L: *mut lua_State, idx: c_int);
313    pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
314    pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
315}
316
317//
318// Some useful macros (implemented as Rust functions)
319//
320#[inline(always)]
321pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
322    lua_tonumberx(L, i, ptr::null_mut())
323}
324
325#[inline(always)]
326pub unsafe fn lua_tointeger_(L: *mut lua_State, i: c_int) -> lua_Integer {
327    lua_tointegerx_(L, i, ptr::null_mut())
328}
329
330#[inline(always)]
331pub unsafe fn lua_tounsigned(L: *mut lua_State, i: c_int) -> lua_Unsigned {
332    lua_tounsignedx(L, i, ptr::null_mut())
333}
334
335#[inline(always)]
336pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
337    lua_settop(L, -n - 1)
338}
339
340#[inline(always)]
341pub unsafe fn lua_newtable(L: *mut lua_State) {
342    lua_createtable(L, 0, 0)
343}
344
345#[inline(always)]
346pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
347    lua_pushcfunction(L, f);
348    lua_setglobal(L, n)
349}
350
351#[inline(always)]
352pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
353    lua_pushcclosure(L, f, 0)
354}
355
356#[inline(always)]
357pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
358    (lua_type(L, n) == LUA_TFUNCTION) as c_int
359}
360
361#[inline(always)]
362pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
363    (lua_type(L, n) == LUA_TTABLE) as c_int
364}
365
366#[inline(always)]
367pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
368    (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
369}
370
371#[inline(always)]
372pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
373    (lua_type(L, n) == LUA_TNIL) as c_int
374}
375
376#[inline(always)]
377pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
378    (lua_type(L, n) == LUA_TBOOLEAN) as c_int
379}
380
381#[inline(always)]
382pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
383    (lua_type(L, n) == LUA_TTHREAD) as c_int
384}
385
386#[inline(always)]
387pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
388    (lua_type(L, n) == LUA_TNONE) as c_int
389}
390
391#[inline(always)]
392pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
393    (lua_type(L, n) <= 0) as c_int
394}
395
396#[inline(always)]
397pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
398    use std::ffi::CString;
399    let c_str = CString::new(s).unwrap();
400    lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
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_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
410    lua_tolstring(L, i, ptr::null_mut())
411}
412
413#[inline(always)]
414pub unsafe fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int) {
415    lua_pushvalue(from, idx);
416    lua_xmove(from, to, 1);
417}
418
419//
420// Debug API
421//
422
423// Maximum size for the description of the source of a function in debug information.
424const LUA_IDSIZE: usize = 60;
425
426// Event codes
427pub const LUA_HOOKCALL: c_int = 0;
428pub const LUA_HOOKRET: c_int = 1;
429pub const LUA_HOOKLINE: c_int = 2;
430pub const LUA_HOOKCOUNT: c_int = 3;
431pub const LUA_HOOKTAILCALL: c_int = 4;
432
433// Event masks
434pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
435pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
436pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
437pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
438
439/// Type for functions to be called on debug events.
440pub type lua_Hook = unsafe extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug);
441
442extern "C" {
443    pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
444    pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
445    pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
446    pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
447    pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
448    pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
449
450    pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
451    pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);
452
453    pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int);
454    pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
455    pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
456    pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
457}
458
459#[repr(C)]
460pub struct lua_Debug {
461    pub event: c_int,
462    pub name: *const c_char,
463    pub namewhat: *const c_char,
464    pub what: *const c_char,
465    pub source: *const c_char,
466    pub currentline: c_int,
467    pub linedefined: c_int,
468    pub lastlinedefined: c_int,
469    pub nups: c_uchar,
470    pub nparams: c_uchar,
471    pub isvararg: c_char,
472    pub istailcall: c_char,
473    pub short_src: [c_char; LUA_IDSIZE],
474    // lua.h mentions this is for private use
475    i_ci: *mut c_void,
476}