1use 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::{mem, ptr};
7
8pub const LUA_SIGNATURE: &[u8] = b"\x1bLua";
10
11pub const LUA_MULTRET: c_int = -1;
13
14#[doc(hidden)]
16pub const LUAI_MAXSTACK: c_int = c_int::MAX;
17
18pub const LUA_EXTRASPACE: usize = mem::size_of::<*const ()>();
20
21pub const LUA_REGISTRYINDEX: c_int = -(c_int::MAX / 2 + 1000);
25
26pub const fn lua_upvalueindex(i: c_int) -> c_int {
27 LUA_REGISTRYINDEX - i
28}
29
30pub const LUA_OK: c_int = 0;
34pub const LUA_YIELD: c_int = 1;
35pub const LUA_ERRRUN: c_int = 2;
36pub const LUA_ERRSYNTAX: c_int = 3;
37pub const LUA_ERRMEM: c_int = 4;
38pub const LUA_ERRERR: c_int = 5;
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_NUMTYPES: c_int = 9;
63
64pub const LUA_MINSTACK: c_int = 20;
66
67pub const LUA_RIDX_GLOBALS: lua_Integer = 2;
70pub const LUA_RIDX_MAINTHREAD: lua_Integer = 3;
71pub const LUA_RIDX_LAST: lua_Integer = 3;
72
73pub type lua_Number = c_double;
75
76pub type lua_Integer = i64;
78
79pub type lua_Unsigned = u64;
81
82pub type lua_KContext = isize;
84
85pub type lua_CFunction = unsafe extern "C-unwind" fn(L: *mut lua_State) -> c_int;
87
88pub type lua_KFunction =
90 unsafe extern "C-unwind" fn(L: *mut lua_State, status: c_int, ctx: lua_KContext) -> c_int;
91
92#[rustfmt::skip]
94pub type lua_Reader =
95 unsafe extern "C-unwind" fn(L: *mut lua_State, ud: *mut c_void, sz: *mut usize) -> *const c_char;
96#[rustfmt::skip]
97pub type lua_Writer =
98 unsafe extern "C-unwind" fn(L: *mut lua_State, p: *const c_void, sz: usize, ud: *mut c_void) -> c_int;
99
100#[rustfmt::skip]
102pub type lua_Alloc =
103 unsafe extern "C" fn(ud: *mut c_void, ptr: *mut c_void, osize: usize, nsize: usize) -> *mut c_void;
104
105pub type lua_WarnFunction = unsafe extern "C-unwind" fn(ud: *mut c_void, msg: *const c_char, tocont: c_int);
107
108#[cfg_attr(all(windows, raw_dylib), link(name = "lua55", kind = "raw-dylib"))]
109unsafe extern "C-unwind" {
110 pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void, seed: c_uint) -> *mut lua_State;
114 pub fn lua_close(L: *mut lua_State);
115 pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
116 pub fn lua_closethread(L: *mut lua_State, from: *mut lua_State) -> c_int;
117
118 pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
119
120 pub fn lua_version(L: *mut lua_State) -> lua_Number;
121
122 pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
126 pub fn lua_gettop(L: *mut lua_State) -> c_int;
127 pub fn lua_settop(L: *mut lua_State, idx: c_int);
128 pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
129 pub fn lua_rotate(L: *mut lua_State, idx: c_int, n: c_int);
130 pub fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int);
131 pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
132
133 pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
134
135 pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
139 pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
140 pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
141 pub fn lua_isinteger(L: *mut lua_State, idx: c_int) -> c_int;
142 pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
143 pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
144 pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
145
146 pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
147 pub fn lua_tointegerx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
148 pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
149 pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
150 #[link_name = "lua_rawlen"]
151 fn lua_rawlen_(L: *mut lua_State, idx: c_int) -> lua_Unsigned;
152 pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
153 pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
154 pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
155 pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
156}
157
158#[inline(always)]
161pub unsafe fn lua_rawlen(L: *mut lua_State, idx: c_int) -> usize {
162 lua_rawlen_(L, idx) as usize
163}
164
165pub const LUA_OPADD: c_int = 0;
169pub const LUA_OPSUB: c_int = 1;
170pub const LUA_OPMUL: c_int = 2;
171pub const LUA_OPMOD: c_int = 3;
172pub const LUA_OPPOW: c_int = 4;
173pub const LUA_OPDIV: c_int = 5;
174pub const LUA_OPIDIV: c_int = 6;
175pub const LUA_OPBAND: c_int = 7;
176pub const LUA_OPBOR: c_int = 8;
177pub const LUA_OPBXOR: c_int = 9;
178pub const LUA_OPSHL: c_int = 10;
179pub const LUA_OPSHR: c_int = 11;
180pub const LUA_OPUNM: c_int = 12;
181pub const LUA_OPBNOT: c_int = 13;
182
183pub const LUA_OPEQ: c_int = 0;
184pub const LUA_OPLT: c_int = 1;
185pub const LUA_OPLE: c_int = 2;
186
187#[cfg_attr(all(windows, raw_dylib), link(name = "lua55", kind = "raw-dylib"))]
188unsafe extern "C-unwind" {
189 pub fn lua_arith(L: *mut lua_State, op: c_int);
190 pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
191 pub fn lua_compare(L: *mut lua_State, idx1: c_int, idx2: c_int, op: c_int) -> c_int;
192}
193
194#[cfg_attr(all(windows, raw_dylib), link(name = "lua55", kind = "raw-dylib"))]
195unsafe extern "C-unwind" {
196 pub fn lua_pushnil(L: *mut lua_State);
200 pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
201 pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
202 pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, len: usize) -> *const c_char;
203 pub fn lua_pushexternalstring(
204 L: *mut lua_State,
205 s: *const c_char,
206 len: usize,
207 falloc: Option<lua_Alloc>,
208 ud: *mut c_void,
209 ) -> *const c_char;
210 pub fn lua_pushstring(L: *mut lua_State, s: *const c_char) -> *const c_char;
211 pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
213 pub fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, n: c_int);
214 pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
215 pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
216 pub fn lua_pushthread(L: *mut lua_State) -> c_int;
217
218 pub fn lua_getglobal(L: *mut lua_State, name: *const c_char) -> c_int;
222 pub fn lua_gettable(L: *mut lua_State, idx: c_int) -> c_int;
223 pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int;
224 pub fn lua_geti(L: *mut lua_State, idx: c_int, n: lua_Integer) -> c_int;
225 pub fn lua_rawget(L: *mut lua_State, idx: c_int) -> c_int;
226 pub fn lua_rawgeti(L: *mut lua_State, idx: c_int, n: lua_Integer) -> c_int;
227 pub fn lua_rawgetp(L: *mut lua_State, idx: c_int, p: *const c_void) -> c_int;
228
229 pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
230 pub fn lua_newuserdatauv(L: *mut lua_State, sz: usize, nuvalue: c_int) -> *mut c_void;
231 pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
232 pub fn lua_getiuservalue(L: *mut lua_State, idx: c_int, n: c_int) -> c_int;
233
234 pub fn lua_setglobal(L: *mut lua_State, name: *const c_char);
238 pub fn lua_settable(L: *mut lua_State, idx: c_int);
239 pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
240 pub fn lua_seti(L: *mut lua_State, idx: c_int, n: lua_Integer);
241 pub fn lua_rawset(L: *mut lua_State, idx: c_int);
242 pub fn lua_rawseti(L: *mut lua_State, idx: c_int, n: lua_Integer);
243 pub fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void);
244 pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
245 pub fn lua_setiuservalue(L: *mut lua_State, idx: c_int, n: c_int) -> c_int;
246
247 pub fn lua_callk(
251 L: *mut lua_State,
252 nargs: c_int,
253 nresults: c_int,
254 ctx: lua_KContext,
255 k: Option<lua_KFunction>,
256 );
257 pub fn lua_pcallk(
258 L: *mut lua_State,
259 nargs: c_int,
260 nresults: c_int,
261 errfunc: c_int,
262 ctx: lua_KContext,
263 k: Option<lua_KFunction>,
264 ) -> c_int;
265
266 pub fn lua_load(
267 L: *mut lua_State,
268 reader: lua_Reader,
269 data: *mut c_void,
270 chunkname: *const c_char,
271 mode: *const c_char,
272 ) -> c_int;
273
274 pub fn lua_dump(L: *mut lua_State, writer: lua_Writer, data: *mut c_void, strip: c_int) -> c_int;
275}
276
277#[inline(always)]
278pub unsafe fn lua_call(L: *mut lua_State, n: c_int, r: c_int) {
279 lua_callk(L, n, r, 0, None)
280}
281
282#[inline(always)]
283pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
284 lua_pcallk(L, n, r, f, 0, None)
285}
286
287#[cfg_attr(all(windows, raw_dylib), link(name = "lua55", kind = "raw-dylib"))]
288unsafe extern "C-unwind" {
289 pub fn lua_yieldk(
293 L: *mut lua_State,
294 nresults: c_int,
295 ctx: lua_KContext,
296 k: Option<lua_KFunction>,
297 ) -> c_int;
298 pub fn lua_resume(L: *mut lua_State, from: *mut lua_State, narg: c_int, nres: *mut c_int) -> c_int;
299 pub fn lua_status(L: *mut lua_State) -> c_int;
300 pub fn lua_isyieldable(L: *mut lua_State) -> c_int;
301}
302
303#[inline(always)]
304pub unsafe fn lua_yield(L: *mut lua_State, n: c_int) -> c_int {
305 lua_yieldk(L, n, 0, None)
306}
307
308#[cfg_attr(all(windows, raw_dylib), link(name = "lua55", kind = "raw-dylib"))]
312unsafe extern "C-unwind" {
313 pub fn lua_setwarnf(L: *mut lua_State, f: Option<lua_WarnFunction>, ud: *mut c_void);
314 pub fn lua_warning(L: *mut lua_State, msg: *const c_char, tocont: c_int);
315}
316
317pub const LUA_GCSTOP: c_int = 0;
321pub const LUA_GCRESTART: c_int = 1;
322pub const LUA_GCCOLLECT: c_int = 2;
323pub const LUA_GCCOUNT: c_int = 3;
324pub const LUA_GCCOUNTB: c_int = 4;
325pub const LUA_GCSTEP: c_int = 5;
326pub const LUA_GCISRUNNING: c_int = 6;
327pub const LUA_GCGEN: c_int = 7;
328pub const LUA_GCINC: c_int = 8;
329pub const LUA_GCPARAM: c_int = 9;
330
331pub const LUA_GCPMINORMUL: c_int = 0; pub const LUA_GCPMAJORMINOR: c_int = 1; pub const LUA_GCPMINORMAJOR: c_int = 2; pub const LUA_GCPPAUSE: c_int = 3; pub const LUA_GCPSTEPMUL: c_int = 4; pub const LUA_GCPSTEPSIZE: c_int = 5; pub const LUA_GCPNUM: c_int = 6; #[cfg_attr(all(windows, raw_dylib), link(name = "lua55", kind = "raw-dylib"))]
344unsafe extern "C-unwind" {
345 pub fn lua_gc(L: *mut lua_State, what: c_int, ...) -> c_int;
346}
347
348#[cfg_attr(all(windows, raw_dylib), link(name = "lua55", kind = "raw-dylib"))]
349unsafe extern "C-unwind" {
350 #[link_name = "lua_error"]
354 fn lua_error_(L: *mut lua_State) -> c_int;
355 pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
356 pub fn lua_concat(L: *mut lua_State, n: c_int);
357 pub fn lua_len(L: *mut lua_State, idx: c_int);
358 pub fn lua_numbertocstring(L: *mut lua_State, idx: c_int, buff: *mut c_char) -> c_uint;
359 pub fn lua_stringtonumber(L: *mut lua_State, s: *const c_char) -> usize;
360 pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
361 pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
362
363 pub fn lua_toclose(L: *mut lua_State, idx: c_int);
364 pub fn lua_closeslot(L: *mut lua_State, idx: c_int);
365}
366
367#[inline(always)]
371pub unsafe fn lua_error(L: *mut lua_State) -> ! {
372 lua_error_(L);
373 unreachable!();
374}
375
376#[inline(always)]
380pub unsafe fn lua_getextraspace(L: *mut lua_State) -> *mut c_void {
381 (L as *mut c_char).sub(LUA_EXTRASPACE) as *mut c_void
382}
383
384#[inline(always)]
385pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
386 lua_tonumberx(L, i, ptr::null_mut())
387}
388
389#[inline(always)]
390pub unsafe fn lua_tointeger(L: *mut lua_State, i: c_int) -> lua_Integer {
391 lua_tointegerx(L, i, ptr::null_mut())
392}
393
394#[inline(always)]
395pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
396 lua_settop(L, -n - 1)
397}
398
399#[inline(always)]
400pub unsafe fn lua_newtable(L: *mut lua_State) {
401 lua_createtable(L, 0, 0)
402}
403
404#[inline(always)]
405pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
406 lua_pushcfunction(L, f);
407 lua_setglobal(L, n)
408}
409
410#[inline(always)]
411pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
412 lua_pushcclosure(L, f, 0)
413}
414
415#[inline(always)]
416pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
417 (lua_type(L, n) == LUA_TFUNCTION) as c_int
418}
419
420#[inline(always)]
421pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
422 (lua_type(L, n) == LUA_TTABLE) as c_int
423}
424
425#[inline(always)]
426pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
427 (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
428}
429
430#[inline(always)]
431pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
432 (lua_type(L, n) == LUA_TNIL) as c_int
433}
434
435#[inline(always)]
436pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
437 (lua_type(L, n) == LUA_TBOOLEAN) as c_int
438}
439
440#[inline(always)]
441pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
442 (lua_type(L, n) == LUA_TTHREAD) as c_int
443}
444
445#[inline(always)]
446pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
447 (lua_type(L, n) == LUA_TNONE) as c_int
448}
449
450#[inline(always)]
451pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
452 (lua_type(L, n) <= 0) as c_int
453}
454
455#[inline(always)]
456pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
457 lua_pushstring(L, s.as_ptr());
458}
459
460#[inline(always)]
461pub unsafe fn lua_pushglobaltable(L: *mut lua_State) -> c_int {
462 lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
463}
464
465#[inline(always)]
466pub unsafe fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void {
467 if lua_islightuserdata(L, idx) != 0 {
468 return lua_touserdata(L, idx);
469 }
470 ptr::null_mut()
471}
472
473#[inline(always)]
474pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
475 lua_tolstring(L, i, ptr::null_mut())
476}
477
478#[inline(always)]
479pub unsafe fn lua_insert(L: *mut lua_State, idx: c_int) {
480 lua_rotate(L, idx, 1)
481}
482
483#[inline(always)]
484pub unsafe fn lua_remove(L: *mut lua_State, idx: c_int) {
485 lua_rotate(L, idx, -1);
486 lua_pop(L, 1)
487}
488
489#[inline(always)]
490pub unsafe fn lua_replace(L: *mut lua_State, idx: c_int) {
491 lua_copy(L, -1, idx);
492 lua_pop(L, 1)
493}
494
495#[inline(always)]
496pub unsafe fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int) {
497 lua_pushvalue(from, idx);
498 lua_xmove(from, to, 1);
499}
500
501#[inline(always)]
502pub unsafe fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void {
503 lua_newuserdatauv(L, sz, 1)
504}
505
506#[inline(always)]
507pub unsafe fn lua_getuservalue(L: *mut lua_State, idx: c_int) -> c_int {
508 lua_getiuservalue(L, idx, 1)
509}
510
511#[inline(always)]
512pub unsafe fn lua_setuservalue(L: *mut lua_State, idx: c_int) -> c_int {
513 lua_setiuservalue(L, idx, 1)
514}
515
516const LUA_IDSIZE: usize = 60;
522
523pub const LUA_HOOKCALL: c_int = 0;
525pub const LUA_HOOKRET: c_int = 1;
526pub const LUA_HOOKLINE: c_int = 2;
527pub const LUA_HOOKCOUNT: c_int = 3;
528pub const LUA_HOOKTAILCALL: c_int = 4;
529
530pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
532pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
533pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
534pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
535
536pub type lua_Hook = unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug);
538
539#[cfg_attr(all(windows, raw_dylib), link(name = "lua55", kind = "raw-dylib"))]
540unsafe extern "C-unwind" {
541 pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
542 pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
543 pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
544 pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
545 pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
546 pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
547
548 pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
549 pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);
550
551 pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int);
552 pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
553 pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
554 pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
555}
556
557#[repr(C)]
558pub struct lua_Debug {
559 pub event: c_int,
560 pub name: *const c_char, pub namewhat: *const c_char, pub what: *const c_char, pub source: *const c_char, pub srclen: usize, pub currentline: c_int, pub linedefined: c_int, pub lastlinedefined: c_int, pub nups: c_uchar, pub nparams: c_uchar, pub isvararg: c_char, pub extraargs: c_uchar, pub istailcall: c_char, pub ftransfer: c_int, pub ntransfer: c_int, pub short_src: [c_char; LUA_IDSIZE], i_ci: *mut c_void,
578}