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