factorio_mlua_sys/lua51/
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_void};
5use std::ptr;
6
7// Mark for precompiled code (`<esc>Lua`)
8#[cfg(not(feature = "luajit"))]
9pub const LUA_SIGNATURE: &[u8] = b"\x1bLua";
10#[cfg(feature = "luajit")]
11pub const LUA_SIGNATURE: &[u8] = b"\x1bLJ";
12
13// Option for multiple returns in 'lua_pcall' and 'lua_call'
14pub const LUA_MULTRET: c_int = -1;
15
16//
17// Pseudo-indices
18//
19pub const LUA_REGISTRYINDEX: c_int = -10000;
20pub const LUA_ENVIRONINDEX: c_int = -10001;
21pub const LUA_GLOBALSINDEX: c_int = -10002;
22
23pub const fn lua_upvalueindex(i: c_int) -> c_int {
24    LUA_GLOBALSINDEX - i
25}
26
27//
28// Thread status
29//
30pub const LUA_OK: c_int = 0;
31pub const LUA_YIELD: c_int = 1;
32pub const LUA_ERRRUN: c_int = 2;
33pub const LUA_ERRSYNTAX: c_int = 3;
34pub const LUA_ERRMEM: c_int = 4;
35pub const LUA_ERRERR: c_int = 5;
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
59/// Type produced by LuaJIT FFI module
60#[cfg(feature = "luajit")]
61pub const LUA_TCDATA: c_int = 10;
62
63/// Minimum Lua stack available to a C function
64pub const LUA_MINSTACK: c_int = 20;
65
66/// A Lua number, usually equivalent to `f64`
67pub type lua_Number = c_double;
68
69/// A Lua integer, usually equivalent to `i64`
70#[cfg(target_pointer_width = "32")]
71pub type lua_Integer = i32;
72#[cfg(target_pointer_width = "64")]
73pub type lua_Integer = i64;
74
75/// Type for native C functions that can be passed to Lua.
76pub type lua_CFunction = unsafe extern "C" fn(L: *mut lua_State) -> c_int;
77
78// Type for functions that read/write blocks when loading/dumping Lua chunks
79pub type lua_Reader =
80    unsafe extern "C" fn(L: *mut lua_State, ud: *mut c_void, sz: *mut usize) -> *const c_char;
81pub type lua_Writer =
82    unsafe extern "C" fn(L: *mut lua_State, p: *const c_void, sz: usize, ud: *mut c_void) -> c_int;
83
84/// Type for memory-allocation functions
85pub type lua_Alloc = unsafe extern "C" fn(
86    ud: *mut c_void,
87    ptr: *mut c_void,
88    osize: usize,
89    nsize: usize,
90) -> *mut c_void;
91
92extern "C" {
93    //
94    // State manipulation
95    //
96    pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
97    pub fn lua_close(L: *mut lua_State);
98    pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
99
100    #[cfg(all(feature = "luajit", feature = "vendored"))]
101    pub fn lua_resetthread(L: *mut lua_State, th: *mut lua_State);
102
103    pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
104
105    //
106    // Basic stack manipulation
107    //
108    pub fn lua_gettop(L: *mut lua_State) -> c_int;
109    pub fn lua_settop(L: *mut lua_State, idx: c_int);
110    pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
111    pub fn lua_remove(L: *mut lua_State, idx: c_int);
112    pub fn lua_insert(L: *mut lua_State, idx: c_int);
113    pub fn lua_replace(L: *mut lua_State, idx: c_int);
114    pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
115
116    pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
117
118    //
119    // Access functions (stack -> C)
120    //
121    pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
122    pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
123    pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
124    pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
125    pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
126    pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
127
128    pub fn lua_equal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
129    pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
130    pub fn lua_lessthan(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
131
132    pub fn lua_tonumber(L: *mut lua_State, idx: c_int) -> lua_Number;
133    #[link_name = "lua_tointeger"]
134    pub fn lua_tointeger_(L: *mut lua_State, idx: c_int) -> lua_Integer;
135    pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
136    pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
137    pub fn lua_objlen(L: *mut lua_State, idx: c_int) -> usize;
138    pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
139    pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
140    pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
141    pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
142
143    //
144    // Push functions (C -> stack)
145    //
146    pub fn lua_pushnil(L: *mut lua_State);
147    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
148    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
149    #[link_name = "lua_pushlstring"]
150    pub fn lua_pushlstring_(L: *mut lua_State, s: *const c_char, l: usize);
151    #[link_name = "lua_pushstring"]
152    pub fn lua_pushstring_(L: *mut lua_State, s: *const c_char);
153    // lua_pushvfstring
154    pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
155    pub fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, n: c_int);
156    pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
157    pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
158    pub fn lua_pushthread(L: *mut lua_State) -> c_int;
159
160    //
161    // Get functions (Lua -> stack)
162    //
163    #[link_name = "lua_gettable"]
164    pub fn lua_gettable_(L: *mut lua_State, idx: c_int);
165    #[link_name = "lua_getfield"]
166    pub fn lua_getfield_(L: *mut lua_State, idx: c_int, k: *const c_char);
167    #[link_name = "lua_rawget"]
168    pub fn lua_rawget_(L: *mut lua_State, idx: c_int);
169    #[link_name = "lua_rawgeti"]
170    pub fn lua_rawgeti_(L: *mut lua_State, idx: c_int, n: c_int);
171    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
172    pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void;
173    pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
174    pub fn lua_getfenv(L: *mut lua_State, idx: c_int);
175
176    //
177    // Set functions (stack -> Lua)
178    //
179    pub fn lua_settable(L: *mut lua_State, idx: c_int);
180    pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
181    pub fn lua_rawset(L: *mut lua_State, idx: c_int);
182    #[link_name = "lua_rawseti"]
183    pub fn lua_rawseti_(L: *mut lua_State, idx: c_int, n: c_int);
184    pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
185    pub fn lua_setfenv(L: *mut lua_State, idx: c_int) -> c_int;
186
187    //
188    // 'load' and 'call' functions (load and run Lua code)
189    //
190    pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
191    pub fn lua_pcall(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
192    pub fn lua_cpcall(L: *mut lua_State, f: lua_CFunction, ud: *mut c_void) -> c_int;
193    pub fn lua_load(
194        L: *mut lua_State,
195        reader: lua_Reader,
196        data: *mut c_void,
197        chunkname: *const c_char,
198    ) -> c_int;
199
200    #[link_name = "lua_dump"]
201    pub fn lua_dump_(L: *mut lua_State, writer: lua_Writer, data: *mut c_void) -> c_int;
202
203    //
204    // Coroutine functions
205    //
206    pub fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int;
207    #[link_name = "lua_resume"]
208    pub fn lua_resume_(L: *mut lua_State, narg: c_int) -> c_int;
209    pub fn lua_status(L: *mut lua_State) -> c_int;
210}
211
212//
213// Garbage-collection function and options
214//
215pub const LUA_GCSTOP: c_int = 0;
216pub const LUA_GCRESTART: c_int = 1;
217pub const LUA_GCCOLLECT: c_int = 2;
218pub const LUA_GCCOUNT: c_int = 3;
219pub const LUA_GCCOUNTB: c_int = 4;
220pub const LUA_GCSTEP: c_int = 5;
221pub const LUA_GCSETPAUSE: c_int = 6;
222pub const LUA_GCSETSTEPMUL: c_int = 7;
223
224extern "C" {
225    pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
226}
227
228//
229// Miscellaneous functions
230//
231extern "C" {
232    pub fn lua_error(L: *mut lua_State) -> !;
233    pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
234    pub fn lua_concat(L: *mut lua_State, n: c_int);
235    pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
236    pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
237}
238
239//
240// Some useful macros (implemented as Rust functions)
241//
242#[inline(always)]
243pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
244    lua_settop(L, -n - 1)
245}
246
247#[inline(always)]
248pub unsafe fn lua_newtable(L: *mut lua_State) {
249    lua_createtable(L, 0, 0)
250}
251
252#[inline(always)]
253pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
254    lua_pushcfunction(L, f);
255    lua_setglobal(L, n)
256}
257
258#[inline(always)]
259pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
260    lua_pushcclosure(L, f, 0)
261}
262
263// TODO: lua_strlen
264
265#[inline(always)]
266pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
267    (lua_type(L, n) == LUA_TFUNCTION) as c_int
268}
269
270#[inline(always)]
271pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
272    (lua_type(L, n) == LUA_TTABLE) as c_int
273}
274
275#[inline(always)]
276pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
277    (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
278}
279
280#[inline(always)]
281pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
282    (lua_type(L, n) == LUA_TNIL) as c_int
283}
284
285#[inline(always)]
286pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
287    (lua_type(L, n) == LUA_TBOOLEAN) as c_int
288}
289
290#[inline(always)]
291pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
292    (lua_type(L, n) == LUA_TTHREAD) as c_int
293}
294
295#[inline(always)]
296pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
297    (lua_type(L, n) == LUA_TNONE) as c_int
298}
299
300#[inline(always)]
301pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
302    (lua_type(L, n) <= LUA_TNIL) as c_int
303}
304
305#[inline(always)]
306pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) {
307    use std::ffi::CString;
308    let c_str = CString::new(s).unwrap();
309    lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
310}
311
312#[inline(always)]
313pub unsafe fn lua_setglobal(L: *mut lua_State, var: *const c_char) {
314    lua_setfield(L, LUA_GLOBALSINDEX, var)
315}
316
317#[inline(always)]
318pub unsafe fn lua_getglobal_(L: *mut lua_State, var: *const c_char) {
319    lua_getfield_(L, LUA_GLOBALSINDEX, var)
320}
321
322#[inline(always)]
323pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
324    lua_tolstring(L, i, ptr::null_mut())
325}
326
327#[inline(always)]
328pub unsafe fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int) {
329    lua_pushvalue(from, idx);
330    lua_xmove(from, to, 1);
331}
332
333//
334// Debug API
335//
336
337// Maximum size for the description of the source of a function in debug information.
338const LUA_IDSIZE: usize = 60;
339
340// Event codes
341pub const LUA_HOOKCALL: c_int = 0;
342pub const LUA_HOOKRET: c_int = 1;
343pub const LUA_HOOKLINE: c_int = 2;
344pub const LUA_HOOKCOUNT: c_int = 3;
345pub const LUA_HOOKTAILCALL: c_int = 4;
346
347// Event masks
348pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
349pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
350pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
351pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
352
353/// Type for functions to be called on debug events.
354pub type lua_Hook = unsafe extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug);
355
356extern "C" {
357    pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
358    pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
359    pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
360    pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
361    pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
362    pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
363
364    pub fn lua_sethook(
365        L: *mut lua_State,
366        func: Option<lua_Hook>,
367        mask: c_int,
368        count: c_int,
369    ) -> c_int;
370    pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
371    pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
372    pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
373}
374
375#[repr(C)]
376pub struct lua_Debug {
377    pub event: c_int,
378    pub name: *const c_char,
379    pub namewhat: *const c_char,
380    pub what: *const c_char,
381    pub source: *const c_char,
382    pub currentline: c_int,
383    pub nups: c_int,
384    pub linedefined: c_int,
385    pub lastlinedefined: c_int,
386    pub short_src: [c_char; LUA_IDSIZE],
387    // lua.h mentions this is for private use
388    i_ci: c_int,
389}