factorio_mlua_sys/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    #[link_name = "lua_getfield"]
208    pub fn lua_getfield_(L: *mut lua_State, idx: c_int, k: *const c_char);
209    #[link_name = "lua_rawget"]
210    pub fn lua_rawget_(L: *mut lua_State, idx: c_int);
211    #[link_name = "lua_rawgeti"]
212    pub fn lua_rawgeti_(L: *mut lua_State, idx: c_int, n: c_int);
213    #[link_name = "lua_rawgetp"]
214    pub fn lua_rawgetp_(L: *mut lua_State, idx: c_int, p: *const c_void);
215    pub fn lua_rawgetglobal(L: *mut lua_State, var: *const c_char);
216    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
217    pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void;
218    pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
219    #[link_name = "lua_getuservalue"]
220    pub fn lua_getuservalue_(L: *mut lua_State, idx: c_int);
221    pub fn lua_rawwgetTablesizes(
222        L: *mut lua_State,
223        idx: c_int,
224        outarraysize: *mut c_int,
225        outhashsize: *mut c_int,
226    );
227
228    //
229    // Set functions (stack -> Lua)
230    //
231    pub fn lua_setglobal(L: *mut lua_State, var: *const c_char);
232    pub fn lua_settable(L: *mut lua_State, idx: c_int);
233    pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
234    pub fn lua_rawset(L: *mut lua_State, idx: c_int);
235    #[link_name = "lua_rawseti"]
236    pub fn lua_rawseti_(L: *mut lua_State, idx: c_int, n: c_int);
237    pub fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void);
238    pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
239    pub fn lua_setuservalue(L: *mut lua_State, idx: c_int);
240
241    //
242    // 'load' and 'call' functions (load and run Lua code)
243    //
244    pub fn lua_callk(
245        L: *mut lua_State,
246        nargs: c_int,
247        nresults: c_int,
248        ctx: c_int,
249        k: Option<lua_CFunction>,
250    );
251    pub fn lua_pcallk(
252        L: *mut lua_State,
253        nargs: c_int,
254        nresults: c_int,
255        errfunc: c_int,
256        ctx: c_int,
257        k: Option<lua_CFunction>,
258    ) -> c_int;
259
260    pub fn lua_getctx(L: *mut lua_State, ctx: *mut c_int) -> c_int;
261
262    pub fn lua_load(
263        L: *mut lua_State,
264        reader: lua_Reader,
265        data: *mut c_void,
266        chunkname: *const c_char,
267        mode: *const c_char,
268    ) -> c_int;
269    #[link_name = "lua_dump"]
270    pub fn lua_dump_(L: *mut lua_State, writer: lua_Writer, data: *mut c_void) -> c_int;
271}
272
273#[inline(always)]
274pub unsafe fn lua_call(L: *mut lua_State, n: c_int, r: c_int) {
275    lua_callk(L, n, r, 0, None)
276}
277
278#[inline(always)]
279pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
280    lua_pcallk(L, n, r, f, 0, None)
281}
282
283/* // Disabled in Factorio lua
284extern "C" {
285    //
286    // Coroutine functions
287    //
288    pub fn lua_yieldk(
289        L: *mut lua_State,
290        nresults: c_int,
291        ctx: c_int,
292        k: Option<lua_CFunction>,
293    ) -> c_int;
294    #[link_name = "lua_resume"]
295    pub fn lua_resume_(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
296}
297
298#[inline(always)]
299pub unsafe fn lua_yield(L: *mut lua_State, n: c_int) -> c_int {
300    lua_yieldk(L, n, 0, None)
301}
302*/
303
304extern "C" {
305    pub fn lua_status(L: *mut lua_State) -> c_int;
306}
307
308//
309// Garbage-collection function and options
310//
311pub const LUA_GCSTOP: c_int = 0;
312pub const LUA_GCRESTART: c_int = 1;
313pub const LUA_GCCOLLECT: c_int = 2;
314pub const LUA_GCCOUNT: c_int = 3;
315pub const LUA_GCCOUNTB: c_int = 4;
316pub const LUA_GCSTEP: c_int = 5;
317pub const LUA_GCSETPAUSE: c_int = 6;
318pub const LUA_GCSETSTEPMUL: c_int = 7;
319pub const LUA_GCSETMAJORINC: c_int = 8;
320pub const LUA_GCISRUNNING: c_int = 9;
321pub const LUA_GCGEN: c_int = 10;
322pub const LUA_GCINC: c_int = 11;
323
324extern "C" {
325    pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
326}
327
328extern "C" {
329    //
330    // Miscellaneous functions
331    //
332    pub fn lua_error(L: *mut lua_State) -> !;
333    pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
334    pub fn lua_concat(L: *mut lua_State, n: c_int);
335    pub fn lua_len(L: *mut lua_State, idx: c_int);
336    pub fn lua_tablesize(L: *mut lua_State, idx: c_int, fuzzy: c_int) -> c_int;
337    pub fn lua_getnparams(L: *mut lua_State, idx: c_int) -> c_int;
338    pub fn lua_tableresize(L: *mut lua_State, idx: c_int, narr: c_int, nrec: c_int);
339    pub fn lua_isvalidIndex(L: *mut lua_State, idx: c_int) -> c_int;
340    pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
341    pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
342}
343
344//
345// Some useful macros (implemented as Rust functions)
346//
347#[inline(always)]
348pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
349    lua_tonumberx(L, i, ptr::null_mut())
350}
351
352#[inline(always)]
353pub unsafe fn lua_tointeger_(L: *mut lua_State, i: c_int) -> lua_Integer {
354    lua_tointegerx_(L, i, ptr::null_mut())
355}
356
357#[inline(always)]
358pub unsafe fn lua_tounsigned(L: *mut lua_State, i: c_int) -> lua_Unsigned {
359    lua_tounsignedx(L, i, ptr::null_mut())
360}
361
362#[inline(always)]
363pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
364    lua_settop(L, -n - 1)
365}
366
367#[inline(always)]
368pub unsafe fn lua_newtable(L: *mut lua_State) {
369    lua_createtable(L, 0, 0)
370}
371
372#[inline(always)]
373pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
374    lua_pushcfunction(L, f);
375    lua_setglobal(L, n)
376}
377
378#[inline(always)]
379pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
380    lua_pushcclosure(L, f, 0)
381}
382
383#[inline(always)]
384pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
385    (lua_type(L, n) == LUA_TFUNCTION) as c_int
386}
387
388#[inline(always)]
389pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
390    (lua_type(L, n) == LUA_TTABLE) as c_int
391}
392
393#[inline(always)]
394pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
395    (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
396}
397
398#[inline(always)]
399pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
400    (lua_type(L, n) == LUA_TNIL) as c_int
401}
402
403#[inline(always)]
404pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
405    (lua_type(L, n) == LUA_TBOOLEAN) as c_int
406}
407
408#[inline(always)]
409pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
410    (lua_type(L, n) == LUA_TTHREAD) as c_int
411}
412
413#[inline(always)]
414pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
415    (lua_type(L, n) == LUA_TNONE) as c_int
416}
417
418#[inline(always)]
419pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
420    (lua_type(L, n) <= 0) as c_int
421}
422
423#[inline(always)]
424pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
425    use std::ffi::CString;
426    let c_str = CString::new(s).unwrap();
427    lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
428}
429
430#[inline(always)]
431pub unsafe fn lua_pushglobaltable(L: *mut lua_State) {
432    lua_rawgeti_(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS as _)
433}
434
435#[inline(always)]
436pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
437    lua_tolstring(L, i, ptr::null_mut())
438}
439
440#[inline(always)]
441pub unsafe fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int) {
442    lua_pushvalue(from, idx);
443    lua_xmove(from, to, 1);
444}
445
446//
447// Debug API
448//
449
450// Maximum size for the description of the source of a function in debug information.
451const LUA_IDSIZE: usize = 60;
452
453// Event codes
454pub const LUA_HOOKCALL: c_int = 0;
455pub const LUA_HOOKRET: c_int = 1;
456pub const LUA_HOOKLINE: c_int = 2;
457pub const LUA_HOOKCOUNT: c_int = 3;
458pub const LUA_HOOKTAILCALL: c_int = 4;
459
460// Event masks
461pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
462pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
463pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
464pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
465
466/// Type for functions to be called on debug events.
467pub type lua_Hook = unsafe extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug);
468
469extern "C" {
470    pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
471    pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
472    pub fn lua_getnresults(L: *mut lua_State) -> c_int;
473    pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
474    pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
475    pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
476    pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
477
478    pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
479    pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);
480
481    pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int);
482    pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
483    pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
484    pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
485}
486
487#[repr(C)]
488pub struct lua_Debug {
489    pub event: c_int,
490    pub name: *const c_char,
491    pub namewhat: *const c_char,
492    pub what: *const c_char,
493    pub source: *const c_char,
494    pub currentline: c_int,
495    pub linedefined: c_int,
496    pub lastlinedefined: c_int,
497    pub nups: c_uchar,
498    pub nparams: c_uchar,
499    pub isvararg: c_char,
500    pub istailcall: c_char,
501    pub short_src: [c_char; LUA_IDSIZE],
502    // lua.h mentions this is for private use
503    i_ci: *mut c_void,
504}