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