1use std::ffi::CStr;
4use std::marker::{PhantomData, PhantomPinned};
5use std::os::raw::{c_char, c_double, c_int, c_uchar, c_void};
6use std::{mem, ptr};
7
8pub const LUA_SIGNATURE: &[u8] = b"\x1bLua";
10
11pub const LUA_MULTRET: c_int = -1;
13
14pub const LUAI_MAXSTACK: c_int = 1000000;
16
17pub const LUA_EXTRASPACE: usize = mem::size_of::<*const ()>();
19
20pub 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
29pub 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#[repr(C)]
42pub struct lua_State {
43 _data: [u8; 0],
44 _marker: PhantomData<(*mut u8, PhantomPinned)>,
45}
46
47pub 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
64pub const LUA_MINSTACK: c_int = 20;
66
67pub 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
72pub type lua_Number = c_double;
74
75pub type lua_Integer = i64;
77
78pub type lua_Unsigned = u64;
80
81pub type lua_KContext = isize;
83
84pub type lua_CFunction = unsafe extern "C-unwind" fn(L: *mut lua_State) -> c_int;
86
87pub type lua_KFunction =
89 unsafe extern "C-unwind" fn(L: *mut lua_State, status: c_int, ctx: lua_KContext) -> c_int;
90
91#[rustfmt::skip]
93pub type lua_Reader =
94 unsafe extern "C-unwind" fn(L: *mut lua_State, ud: *mut c_void, sz: *mut usize) -> *const c_char;
95#[rustfmt::skip]
96pub type lua_Writer =
97 unsafe extern "C-unwind" fn(L: *mut lua_State, p: *const c_void, sz: usize, ud: *mut c_void) -> c_int;
98
99#[rustfmt::skip]
101pub type lua_Alloc =
102 unsafe extern "C" fn(ud: *mut c_void, ptr: *mut c_void, osize: usize, nsize: usize) -> *mut c_void;
103
104#[cfg_attr(all(windows, raw_dylib), link(name = "lua53", kind = "raw-dylib"))]
105unsafe extern "C-unwind" {
106 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 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 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
152pub 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
170pub const LUA_OPEQ: c_int = 0;
171pub const LUA_OPLT: c_int = 1;
172pub const LUA_OPLE: c_int = 2;
173
174#[cfg_attr(all(windows, raw_dylib), link(name = "lua53", kind = "raw-dylib"))]
175unsafe extern "C-unwind" {
176 pub fn lua_arith(L: *mut lua_State, op: c_int);
177 pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
178 pub fn lua_compare(L: *mut lua_State, idx1: c_int, idx2: c_int, op: c_int) -> c_int;
179}
180
181#[cfg_attr(all(windows, raw_dylib), link(name = "lua53", kind = "raw-dylib"))]
182unsafe extern "C-unwind" {
183 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_pushlstring(L: *mut lua_State, s: *const c_char, len: usize) -> *const c_char;
190 pub fn lua_pushstring(L: *mut lua_State, s: *const c_char) -> *const c_char;
191 pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
193 pub fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, n: c_int);
194 pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
195 pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
196 pub fn lua_pushthread(L: *mut lua_State) -> c_int;
197
198 pub fn lua_getglobal(L: *mut lua_State, name: *const c_char) -> c_int;
202 pub fn lua_gettable(L: *mut lua_State, idx: c_int) -> c_int;
203 pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int;
204 pub fn lua_geti(L: *mut lua_State, idx: c_int, n: lua_Integer) -> c_int;
205 pub fn lua_rawget(L: *mut lua_State, idx: c_int) -> c_int;
206 pub fn lua_rawgeti(L: *mut lua_State, idx: c_int, n: lua_Integer) -> c_int;
207 pub fn lua_rawgetp(L: *mut lua_State, idx: c_int, p: *const c_void) -> c_int;
208
209 pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
210 pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void;
211 pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
212 pub fn lua_getuservalue(L: *mut lua_State, idx: c_int) -> c_int;
213
214 pub fn lua_setglobal(L: *mut lua_State, name: *const c_char);
218 pub fn lua_settable(L: *mut lua_State, idx: c_int);
219 pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
220 pub fn lua_seti(L: *mut lua_State, idx: c_int, n: lua_Integer);
221 pub fn lua_rawset(L: *mut lua_State, idx: c_int);
222 pub fn lua_rawseti(L: *mut lua_State, idx: c_int, n: lua_Integer);
223 pub fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void);
224 pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
225 pub fn lua_setuservalue(L: *mut lua_State, idx: c_int);
226
227 pub fn lua_callk(
231 L: *mut lua_State,
232 nargs: c_int,
233 nresults: c_int,
234 ctx: lua_KContext,
235 k: Option<lua_KFunction>,
236 );
237 pub fn lua_pcallk(
238 L: *mut lua_State,
239 nargs: c_int,
240 nresults: c_int,
241 errfunc: c_int,
242 ctx: lua_KContext,
243 k: Option<lua_KFunction>,
244 ) -> c_int;
245
246 pub fn lua_load(
247 L: *mut lua_State,
248 reader: lua_Reader,
249 data: *mut c_void,
250 chunkname: *const c_char,
251 mode: *const c_char,
252 ) -> c_int;
253
254 pub fn lua_dump(L: *mut lua_State, writer: lua_Writer, data: *mut c_void, strip: c_int) -> c_int;
255}
256
257#[inline(always)]
258pub unsafe fn lua_call(L: *mut lua_State, n: c_int, r: c_int) {
259 lua_callk(L, n, r, 0, None)
260}
261
262#[inline(always)]
263pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
264 lua_pcallk(L, n, r, f, 0, None)
265}
266
267#[cfg_attr(all(windows, raw_dylib), link(name = "lua53", kind = "raw-dylib"))]
268unsafe extern "C-unwind" {
269 pub fn lua_yieldk(
273 L: *mut lua_State,
274 nresults: c_int,
275 ctx: lua_KContext,
276 k: Option<lua_KFunction>,
277 ) -> c_int;
278 #[link_name = "lua_resume"]
279 pub fn lua_resume_(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
280 pub fn lua_status(L: *mut lua_State) -> c_int;
281 pub fn lua_isyieldable(L: *mut lua_State) -> c_int;
282}
283
284#[inline(always)]
285pub unsafe fn lua_yield(L: *mut lua_State, n: c_int) -> c_int {
286 lua_yieldk(L, n, 0, None)
287}
288
289pub const LUA_GCSTOP: c_int = 0;
293pub const LUA_GCRESTART: c_int = 1;
294pub const LUA_GCCOLLECT: c_int = 2;
295pub const LUA_GCCOUNT: c_int = 3;
296pub const LUA_GCCOUNTB: c_int = 4;
297pub const LUA_GCSTEP: c_int = 5;
298pub const LUA_GCSETPAUSE: c_int = 6;
299pub const LUA_GCSETSTEPMUL: c_int = 7;
300pub const LUA_GCISRUNNING: c_int = 9;
301
302#[cfg_attr(all(windows, raw_dylib), link(name = "lua53", kind = "raw-dylib"))]
303unsafe extern "C-unwind" {
304 pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
305}
306
307#[cfg_attr(all(windows, raw_dylib), link(name = "lua53", kind = "raw-dylib"))]
308unsafe extern "C-unwind" {
309 #[link_name = "lua_error"]
313 fn lua_error_(L: *mut lua_State) -> c_int;
314 pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
315 pub fn lua_concat(L: *mut lua_State, n: c_int);
316 pub fn lua_len(L: *mut lua_State, idx: c_int);
317 pub fn lua_stringtonumber(L: *mut lua_State, s: *const c_char) -> usize;
318 pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
319 pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
320}
321
322#[inline(always)]
326pub unsafe fn lua_error(L: *mut lua_State) -> ! {
327 lua_error_(L);
328 unreachable!();
329}
330
331#[inline(always)]
335pub unsafe fn lua_getextraspace(L: *mut lua_State) -> *mut c_void {
336 (L as *mut c_char).sub(LUA_EXTRASPACE) as *mut c_void
337}
338
339#[inline(always)]
340pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
341 lua_tonumberx(L, i, ptr::null_mut())
342}
343
344#[inline(always)]
345pub unsafe fn lua_tointeger(L: *mut lua_State, i: c_int) -> lua_Integer {
346 lua_tointegerx(L, i, ptr::null_mut())
347}
348
349#[inline(always)]
350pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
351 lua_settop(L, -n - 1)
352}
353
354#[inline(always)]
355pub unsafe fn lua_newtable(L: *mut lua_State) {
356 lua_createtable(L, 0, 0)
357}
358
359#[inline(always)]
360pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
361 lua_pushcfunction(L, f);
362 lua_setglobal(L, n)
363}
364
365#[inline(always)]
366pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
367 lua_pushcclosure(L, f, 0)
368}
369
370#[inline(always)]
371pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
372 (lua_type(L, n) == LUA_TFUNCTION) as c_int
373}
374
375#[inline(always)]
376pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
377 (lua_type(L, n) == LUA_TTABLE) as c_int
378}
379
380#[inline(always)]
381pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
382 (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
383}
384
385#[inline(always)]
386pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
387 (lua_type(L, n) == LUA_TNIL) as c_int
388}
389
390#[inline(always)]
391pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
392 (lua_type(L, n) == LUA_TBOOLEAN) as c_int
393}
394
395#[inline(always)]
396pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
397 (lua_type(L, n) == LUA_TTHREAD) as c_int
398}
399
400#[inline(always)]
401pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
402 (lua_type(L, n) == LUA_TNONE) as c_int
403}
404
405#[inline(always)]
406pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
407 (lua_type(L, n) <= 0) as c_int
408}
409
410#[inline(always)]
411pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
412 lua_pushstring(L, s.as_ptr());
413}
414
415#[inline(always)]
416pub unsafe fn lua_pushglobaltable(L: *mut lua_State) -> c_int {
417 lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
418}
419
420#[inline(always)]
421pub unsafe fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void {
422 if lua_islightuserdata(L, idx) != 0 {
423 return lua_touserdata(L, idx);
424 }
425 ptr::null_mut()
426}
427
428#[inline(always)]
429pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
430 lua_tolstring(L, i, ptr::null_mut())
431}
432
433#[inline(always)]
434pub unsafe fn lua_insert(L: *mut lua_State, idx: c_int) {
435 lua_rotate(L, idx, 1)
436}
437
438#[inline(always)]
439pub unsafe fn lua_remove(L: *mut lua_State, idx: c_int) {
440 lua_rotate(L, idx, -1);
441 lua_pop(L, 1)
442}
443
444#[inline(always)]
445pub unsafe fn lua_replace(L: *mut lua_State, idx: c_int) {
446 lua_copy(L, -1, idx);
447 lua_pop(L, 1)
448}
449
450#[inline(always)]
451pub unsafe fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int) {
452 lua_pushvalue(from, idx);
453 lua_xmove(from, to, 1);
454}
455
456const LUA_IDSIZE: usize = 60;
462
463pub const LUA_HOOKCALL: c_int = 0;
465pub const LUA_HOOKRET: c_int = 1;
466pub const LUA_HOOKLINE: c_int = 2;
467pub const LUA_HOOKCOUNT: c_int = 3;
468pub const LUA_HOOKTAILCALL: c_int = 4;
469
470pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
472pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
473pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
474pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
475
476pub type lua_Hook = unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug);
478
479#[cfg_attr(all(windows, raw_dylib), link(name = "lua53", kind = "raw-dylib"))]
480unsafe extern "C-unwind" {
481 pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
482 pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
483 pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
484 pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
485 pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
486 pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
487
488 pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
489 pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);
490
491 pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int);
492 pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
493 pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
494 pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
495}
496
497#[repr(C)]
498pub struct lua_Debug {
499 pub event: c_int,
500 pub name: *const c_char,
501 pub namewhat: *const c_char,
502 pub what: *const c_char,
503 pub source: *const c_char,
504 pub currentline: c_int,
505 pub linedefined: c_int,
506 pub lastlinedefined: c_int,
507 pub nups: c_uchar,
508 pub nparams: c_uchar,
509 pub isvararg: c_char,
510 pub istailcall: c_char,
511 pub short_src: [c_char; LUA_IDSIZE],
512 i_ci: *mut c_void,
514}