factorio_mlua_sys/lua53/
lua.rs

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