factorio_mlua/ffi/lua_factorio/
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    pub fn lua_registertracehandler(handler: Option<extern "C" fn(*const c_char)>);
99    pub fn lua_trace(message: *const c_char);
100    pub fn lua_traceandabort(message: *const c_char) -> c_int;
101}
102
103extern "C" {
104    //
105    // State manipulation
106    //
107    pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
108    pub fn lua_close(L: *mut lua_State);
109    pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
110
111    pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
112
113    pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
114
115    //
116    // Basic stack manipulation
117    //
118    pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
119    pub fn lua_gettop(L: *mut lua_State) -> c_int;
120    pub fn lua_settop(L: *mut lua_State, idx: c_int);
121    pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
122    pub fn lua_remove(L: *mut lua_State, idx: c_int);
123    pub fn lua_insert(L: *mut lua_State, idx: c_int);
124    pub fn lua_replace(L: *mut lua_State, idx: c_int);
125    pub fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int);
126    pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
127
128    pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
129
130    //
131    // Access functions (stack -> C)
132    //
133    #[link_name = "lua_isnumberorstringconvertabletonumber"]
134    pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
135    #[link_name = "lua_isstringornumberconvertabletostring"]
136    pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
137    pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
138    pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
139    pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
140    pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
141    pub fn lua_getstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
142
143    pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
144    #[link_name = "lua_tointegerx"]
145    pub fn lua_tointegerx_(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
146    pub fn lua_tounsignedx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Unsigned;
147    pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
148    pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
149    pub fn lua_uncheckedtolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
150    pub fn lua_uncheckedaslstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
151    pub fn lua_rawlen(L: *mut lua_State, idx: c_int) -> usize;
152    pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
153    pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
154    pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
155    pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
156}
157
158//
159// Comparison and arithmetic functions
160//
161pub const LUA_OPADD: c_int = 0;
162pub const LUA_OPSUB: c_int = 1;
163pub const LUA_OPMUL: c_int = 2;
164pub const LUA_OPDIV: c_int = 3;
165pub const LUA_OPMOD: c_int = 4;
166pub const LUA_OPPOW: c_int = 5;
167pub const LUA_OPUNM: c_int = 6;
168
169extern "C" {
170    pub fn lua_arith(L: *mut lua_State, op: c_int);
171}
172
173pub const LUA_OPEQ: c_int = 0;
174pub const LUA_OPLT: c_int = 1;
175pub const LUA_OPLE: c_int = 2;
176
177extern "C" {
178    pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
179    pub fn lua_compare(L: *mut lua_State, idx1: c_int, idx2: c_int, op: c_int) -> c_int;
180}
181
182extern "C" {
183    //
184    // Push functions (C -> stack)
185    //
186    pub fn lua_pushnil(L: *mut lua_State);
187    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
188    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
189    pub fn lua_pushunsigned(L: *mut lua_State, n: lua_Unsigned);
190    #[link_name = "lua_pushlstring"]
191    pub fn lua_pushlstring_(L: *mut lua_State, s: *const c_char, l: usize) -> *const c_char;
192    pub fn lua_pushstring(L: *mut lua_State, s: *const c_char) -> *const c_char;
193    // lua_pushvfstring
194    pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
195    pub fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, n: c_int);
196    pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
197    pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
198    pub fn lua_pushthread(L: *mut lua_State) -> c_int;
199
200    //
201    // Get functions (Lua -> stack)
202    //
203    #[link_name = "lua_getglobal"]
204    pub fn lua_getglobal_(L: *mut lua_State, name: *const c_char);
205    #[link_name = "lua_gettable"]
206    pub fn lua_gettable_(L: *mut lua_State, idx: c_int);
207    pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char);
208    #[link_name = "lua_getfield"]
209    pub fn lua_getfield_(L: *mut lua_State, idx: c_int, k: *const c_char);
210    #[link_name = "lua_rawget"]
211    pub fn lua_rawget_(L: *mut lua_State, idx: c_int);
212    #[link_name = "lua_rawgeti"]
213    pub fn lua_rawgeti_(L: *mut lua_State, idx: c_int, n: c_int);
214    #[link_name = "lua_rawgetp"]
215    pub fn lua_rawgetp_(L: *mut lua_State, idx: c_int, p: *const c_void);
216    pub fn lua_rawgetglobal(L: *mut lua_State, var: *const c_char);
217    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
218    pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void;
219    pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
220    #[link_name = "lua_getuservalue"]
221    pub fn lua_getuservalue_(L: *mut lua_State, idx: c_int);
222    pub fn lua_rawwgetTablesizes(L: *mut lua_State, idx: c_int, outarraysize: *mut c_int, outhashsize: *mut c_int);
223
224    //
225    // Set functions (stack -> Lua)
226    //
227    pub fn lua_setglobal(L: *mut lua_State, var: *const c_char);
228    pub fn lua_settable(L: *mut lua_State, idx: c_int);
229    pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
230    pub fn lua_rawset(L: *mut lua_State, idx: c_int);
231    #[link_name = "lua_rawseti"]
232    pub fn lua_rawseti_(L: *mut lua_State, idx: c_int, n: c_int);
233    pub fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void);
234    pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
235    pub fn lua_setuservalue(L: *mut lua_State, idx: c_int);
236
237    //
238    // 'load' and 'call' functions (load and run Lua code)
239    //
240    pub fn lua_callk(
241        L: *mut lua_State,
242        nargs: c_int,
243        nresults: c_int,
244        ctx: c_int,
245        k: Option<lua_CFunction>,
246    );
247    pub fn lua_pcallk(
248        L: *mut lua_State,
249        nargs: c_int,
250        nresults: c_int,
251        errfunc: c_int,
252        ctx: c_int,
253        k: Option<lua_CFunction>,
254    ) -> c_int;
255
256    pub fn lua_getctx(L: *mut lua_State, ctx: *mut c_int) -> c_int;
257
258    pub fn lua_load(
259        L: *mut lua_State,
260        reader: lua_Reader,
261        data: *mut c_void,
262        chunkname: *const c_char,
263        mode: *const c_char,
264    ) -> c_int;
265    #[link_name = "lua_dump"]
266    pub fn lua_dump_(L: *mut lua_State, writer: lua_Writer, data: *mut c_void) -> c_int;
267}
268
269#[inline(always)]
270pub unsafe fn lua_call(L: *mut lua_State, n: c_int, r: c_int) {
271    lua_callk(L, n, r, 0, None)
272}
273
274#[inline(always)]
275pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
276    lua_pcallk(L, n, r, f, 0, None)
277}
278
279/* // Disabled in Factorio lua
280extern "C" {
281    //
282    // Coroutine functions
283    //
284    pub fn lua_yieldk(
285        L: *mut lua_State,
286        nresults: c_int,
287        ctx: c_int,
288        k: Option<lua_CFunction>,
289    ) -> c_int;
290    #[link_name = "lua_resume"]
291    pub fn lua_resume_(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
292    pub fn lua_status(L: *mut lua_State) -> c_int;
293}
294
295#[inline(always)]
296pub unsafe fn lua_yield(L: *mut lua_State, n: c_int) -> c_int {
297    lua_yieldk(L, n, 0, None)
298}
299*/
300
301//
302// Garbage-collection function and options
303//
304pub const LUA_GCSTOP: c_int = 0;
305pub const LUA_GCRESTART: c_int = 1;
306pub const LUA_GCCOLLECT: c_int = 2;
307pub const LUA_GCCOUNT: c_int = 3;
308pub const LUA_GCCOUNTB: c_int = 4;
309pub const LUA_GCSTEP: c_int = 5;
310pub const LUA_GCSETPAUSE: c_int = 6;
311pub const LUA_GCSETSTEPMUL: c_int = 7;
312pub const LUA_GCSETMAJORINC: c_int = 8;
313pub const LUA_GCISRUNNING: c_int = 9;
314pub const LUA_GCGEN: c_int = 10;
315pub const LUA_GCINC: c_int = 11;
316
317extern "C" {
318    pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
319}
320
321extern "C" {
322    //
323    // Miscellaneous functions
324    //
325    pub fn lua_error(L: *mut lua_State) -> !;
326    pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
327    pub fn lua_concat(L: *mut lua_State, n: c_int);
328    pub fn lua_len(L: *mut lua_State, idx: c_int);
329    pub fn lua_tablesize(L: *mut lua_State, idx: c_int, fuzzy: c_int) -> c_int;
330    pub fn lua_getnparams(L: *mut lua_State, idx: c_int) -> c_int;
331    pub fn lua_tableresize(L: *mut lua_State, idx: c_int, narr: c_int, nrec: c_int);
332    pub fn lua_isvalidIndex(L: *mut lua_State, idx: c_int) -> c_int;
333    pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
334    pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
335}
336
337//
338// Some useful macros (implemented as Rust functions)
339//
340#[inline(always)]
341pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
342    lua_tonumberx(L, i, ptr::null_mut())
343}
344
345#[inline(always)]
346pub unsafe fn lua_tointeger_(L: *mut lua_State, i: c_int) -> lua_Integer {
347    lua_tointegerx_(L, i, ptr::null_mut())
348}
349
350#[inline(always)]
351pub unsafe fn lua_tounsigned(L: *mut lua_State, i: c_int) -> lua_Unsigned {
352    lua_tounsignedx(L, i, ptr::null_mut())
353}
354
355#[inline(always)]
356pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
357    lua_settop(L, -n - 1)
358}
359
360#[inline(always)]
361pub unsafe fn lua_newtable(L: *mut lua_State) {
362    lua_createtable(L, 0, 0)
363}
364
365#[inline(always)]
366pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
367    lua_pushcfunction(L, f);
368    lua_setglobal(L, n)
369}
370
371#[inline(always)]
372pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
373    lua_pushcclosure(L, f, 0)
374}
375
376#[inline(always)]
377pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
378    (lua_type(L, n) == LUA_TFUNCTION) as c_int
379}
380
381#[inline(always)]
382pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
383    (lua_type(L, n) == LUA_TTABLE) as c_int
384}
385
386#[inline(always)]
387pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
388    (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
389}
390
391#[inline(always)]
392pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
393    (lua_type(L, n) == LUA_TNIL) as c_int
394}
395
396#[inline(always)]
397pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
398    (lua_type(L, n) == LUA_TBOOLEAN) as c_int
399}
400
401#[inline(always)]
402pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
403    (lua_type(L, n) == LUA_TTHREAD) as c_int
404}
405
406#[inline(always)]
407pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
408    (lua_type(L, n) == LUA_TNONE) as c_int
409}
410
411#[inline(always)]
412pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
413    (lua_type(L, n) <= 0) as c_int
414}
415
416#[inline(always)]
417pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
418    use std::ffi::CString;
419    let c_str = CString::new(s).unwrap();
420    lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
421}
422
423#[inline(always)]
424pub unsafe fn lua_pushglobaltable(L: *mut lua_State) {
425    lua_rawgeti_(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS as _)
426}
427
428#[inline(always)]
429pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
430    lua_tolstring(L, i, ptr::null_mut())
431}
432
433//
434// Debug API
435//
436
437// Maximum size for the description of the source of a function in debug information.
438const LUA_IDSIZE: usize = 60;
439
440// Event codes
441pub const LUA_HOOKCALL: c_int = 0;
442pub const LUA_HOOKRET: c_int = 1;
443pub const LUA_HOOKLINE: c_int = 2;
444pub const LUA_HOOKCOUNT: c_int = 3;
445pub const LUA_HOOKTAILCALL: c_int = 4;
446
447// Event masks
448pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
449pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
450pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
451pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
452
453/// Type for functions to be called on debug events.
454pub type lua_Hook = unsafe extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug);
455
456extern "C" {
457    pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
458    pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
459    pub fn lua_getnresults(L: *mut lua_State) -> c_int;
460    pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
461    pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
462    pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
463    pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
464
465    pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
466    pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);
467
468    pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int);
469    pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
470    pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
471    pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
472}
473
474#[repr(C)]
475pub struct lua_Debug {
476    pub event: c_int,
477    pub name: *const c_char,
478    pub namewhat: *const c_char,
479    pub what: *const c_char,
480    pub source: *const c_char,
481    pub currentline: c_int,
482    pub linedefined: c_int,
483    pub lastlinedefined: c_int,
484    pub nups: c_uchar,
485    pub nparams: c_uchar,
486    pub isvararg: c_char,
487    pub istailcall: c_char,
488    pub short_src: [c_char; LUA_IDSIZE],
489    // lua.h mentions this is for private use
490    i_ci: *mut c_void,
491}