1use std::marker::{PhantomData, PhantomPinned};
4use std::os::raw::{c_char, c_double, c_float, c_int, c_uint, c_void};
5use std::ptr;
6
7pub const LUA_MULTRET: c_int = -1;
9
10const LUAI_MAXCSTACK: c_int = 100000;
12
13pub const LUA_REGISTRYINDEX: c_int = -LUAI_MAXCSTACK - 2000;
17pub const LUA_ENVIRONINDEX: c_int = -LUAI_MAXCSTACK - 2001;
18pub const LUA_GLOBALSINDEX: c_int = -LUAI_MAXCSTACK - 2002;
19
20pub const fn lua_upvalueindex(i: c_int) -> c_int {
21 LUA_GLOBALSINDEX - i
22}
23
24pub const LUA_OK: c_int = 0;
28pub const LUA_YIELD: c_int = 1;
29pub const LUA_ERRRUN: c_int = 2;
30pub const LUA_ERRSYNTAX: c_int = 3;
31pub const LUA_ERRMEM: c_int = 4;
32pub const LUA_ERRERR: c_int = 5;
33
34#[repr(C)]
36pub struct lua_State {
37 _data: [u8; 0],
38 _marker: PhantomData<(*mut u8, PhantomPinned)>,
39}
40
41pub const LUA_TNONE: c_int = -1;
45
46pub const LUA_TNIL: c_int = 0;
47pub const LUA_TBOOLEAN: c_int = 1;
48
49pub const LUA_TLIGHTUSERDATA: c_int = 2;
50pub const LUA_TNUMBER: c_int = 3;
51pub const LUA_TVECTOR: c_int = 4;
52
53pub const LUA_TSTRING: c_int = 5;
54pub const LUA_TTABLE: c_int = 6;
55pub const LUA_TFUNCTION: c_int = 7;
56pub const LUA_TUSERDATA: c_int = 8;
57pub const LUA_TTHREAD: c_int = 9;
58
59pub const LUA_MINSTACK: c_int = 20;
61
62pub type lua_Number = c_double;
64
65pub type lua_Integer = c_int;
67
68pub type lua_Unsigned = c_uint;
70
71pub type lua_CFunction = unsafe extern "C" fn(L: *mut lua_State) -> c_int;
73pub type lua_Continuation = unsafe extern "C" fn(L: *mut lua_State, status: c_int) -> c_int;
74
75pub type lua_Udestructor = unsafe extern "C" fn(*mut c_void);
77pub type lua_Destructor = unsafe extern "C" fn(L: *mut lua_State, *mut c_void);
78
79pub type lua_Alloc = unsafe extern "C" fn(
81 ud: *mut c_void,
82 ptr: *mut c_void,
83 osize: usize,
84 nsize: usize,
85) -> *mut c_void;
86
87extern "C" {
88 pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
92 pub fn lua_close(L: *mut lua_State);
93 pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
94 pub fn lua_mainthread(L: *mut lua_State) -> *mut lua_State;
95 pub fn lua_resetthread(L: *mut lua_State);
96 pub fn lua_isthreadreset(L: *mut lua_State) -> c_int;
97
98 pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
102 pub fn lua_gettop(L: *mut lua_State) -> c_int;
103 pub fn lua_settop(L: *mut lua_State, idx: c_int);
104 pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
105 pub fn lua_remove(L: *mut lua_State, idx: c_int);
106 pub fn lua_insert(L: *mut lua_State, idx: c_int);
107 pub fn lua_replace(L: *mut lua_State, idx: c_int);
108 pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
109 pub fn lua_rawcheckstack(L: *mut lua_State, sz: c_int);
110
111 pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
112 pub fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int);
113
114 pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
118 pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
119 pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
120 pub fn lua_isLfunction(L: *mut lua_State, idx: c_int) -> c_int;
121 pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
122 pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
123 pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
124
125 pub fn lua_equal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
126 pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
127 pub fn lua_lessthan(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
128
129 pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
130 #[link_name = "lua_tointegerx"]
131 pub fn lua_tointegerx_(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
132 pub fn lua_tounsignedx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Unsigned;
133 pub fn lua_tovector(L: *mut lua_State, idx: c_int) -> *const c_float;
134 pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
135 pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
136 pub fn lua_tostringatom(L: *mut lua_State, idx: c_int, atom: *mut c_int) -> *const c_char;
137 pub fn lua_namecallatom(L: *mut lua_State, atom: *mut c_int) -> *const c_char;
138 pub fn lua_objlen(L: *mut lua_State, idx: c_int) -> usize;
139 pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
140 pub fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
141 pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
142 pub fn lua_touserdatatagged(L: *mut lua_State, idx: c_int, tag: c_int) -> *mut c_void;
143 pub fn lua_userdatatag(L: *mut lua_State, idx: c_int) -> c_int;
144 pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
145 pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
146
147 pub fn lua_pushnil(L: *mut lua_State);
151 pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
152 pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
153 pub fn lua_pushunsigned(L: *mut lua_State, n: lua_Unsigned);
154 pub fn lua_pushvector(L: *mut lua_State, x: c_float, y: c_float, z: c_float);
155 #[link_name = "lua_pushlstring"]
156 pub fn lua_pushlstring_(L: *mut lua_State, s: *const c_char, l: usize);
157 #[link_name = "lua_pushstring"]
158 pub fn lua_pushstring_(L: *mut lua_State, s: *const c_char);
159 #[link_name = "lua_pushfstringL"]
161 pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
162 pub fn lua_pushcclosurek(
163 L: *mut lua_State,
164 f: lua_CFunction,
165 debugname: *const c_char,
166 nup: c_int,
167 cont: Option<lua_Continuation>,
168 );
169 pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
170 pub fn lua_pushthread(L: *mut lua_State) -> c_int;
171
172 pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
173 pub fn lua_newuserdatatagged(L: *mut lua_State, sz: usize, tag: c_int) -> *mut c_void;
174 pub fn lua_newuserdatadtor(L: *mut lua_State, sz: usize, dtor: lua_Udestructor) -> *mut c_void;
175
176 pub fn lua_gettable(L: *mut lua_State, idx: c_int) -> c_int;
180 pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int;
181 pub fn lua_rawgetfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int;
182 pub fn lua_rawget(L: *mut lua_State, idx: c_int) -> c_int;
183 #[link_name = "lua_rawgeti"]
184 pub fn lua_rawgeti_(L: *mut lua_State, idx: c_int, n: c_int) -> c_int;
185 pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
186
187 pub fn lua_setreadonly(L: *mut lua_State, idx: c_int, enabled: c_int);
188 pub fn lua_getreadonly(L: *mut lua_State, idx: c_int) -> c_int;
189 pub fn lua_setsafeenv(L: *mut lua_State, idx: c_int, enabled: c_int);
190
191 pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
192 pub fn lua_getfenv(L: *mut lua_State, idx: c_int);
193
194 pub fn lua_settable(L: *mut lua_State, idx: c_int);
198 pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
199 pub fn lua_rawset(L: *mut lua_State, idx: c_int);
200 #[link_name = "lua_rawseti"]
201 pub fn lua_rawseti_(L: *mut lua_State, idx: c_int, n: c_int);
202 pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
203 pub fn lua_setfenv(L: *mut lua_State, idx: c_int) -> c_int;
204
205 pub fn luau_load(
209 L: *mut lua_State,
210 chunkname: *const c_char,
211 data: *const c_char,
212 size: usize,
213 env: c_int,
214 ) -> c_int;
215 pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
216 pub fn lua_pcall(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
217
218 pub fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int;
222 pub fn lua_break(L: *mut lua_State) -> c_int;
223 #[link_name = "lua_resume"]
224 pub fn lua_resume_(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
225 pub fn lua_resumeerror(L: *mut lua_State, from: *mut lua_State) -> c_int;
226 pub fn lua_status(L: *mut lua_State) -> c_int;
227 pub fn lua_isyieldable(L: *mut lua_State) -> c_int;
228 pub fn lua_getthreaddata(L: *mut lua_State) -> *mut c_void;
229 pub fn lua_setthreaddata(L: *mut lua_State, data: *mut c_void);
230}
231
232pub const LUA_GCSTOP: c_int = 0;
236pub const LUA_GCRESTART: c_int = 1;
237pub const LUA_GCCOLLECT: c_int = 2;
238pub const LUA_GCCOUNT: c_int = 3;
239pub const LUA_GCCOUNTB: c_int = 4;
240pub const LUA_GCISRUNNING: c_int = 5;
241pub const LUA_GCSTEP: c_int = 6;
242pub const LUA_GCSETGOAL: c_int = 7;
243pub const LUA_GCSETSTEPMUL: c_int = 8;
244pub const LUA_GCSETSTEPSIZE: c_int = 9;
245
246extern "C" {
247 pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
248}
249
250extern "C" {
254 pub fn lua_setmemcat(L: *mut lua_State, category: c_int);
255 pub fn lua_totalbytes(L: *mut lua_State, category: c_int) -> usize;
256}
257
258extern "C" {
262 pub fn lua_error(L: *mut lua_State) -> !;
263 pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
264 pub fn lua_rawiter(L: *mut lua_State, idx: c_int, iter: c_int) -> c_int;
265 pub fn lua_concat(L: *mut lua_State, n: c_int);
266 pub fn lua_clock() -> c_double;
268 pub fn lua_setuserdatatag(L: *mut lua_State, idx: c_int, tag: c_int);
269 pub fn lua_setuserdatadtor(L: *mut lua_State, tag: c_int, dtor: Option<lua_Destructor>);
270 pub fn lua_getuserdatadtor(L: *mut lua_State, tag: c_int) -> Option<lua_Destructor>;
271 pub fn lua_clonefunction(L: *mut lua_State, idx: c_int);
272 pub fn lua_cleartable(L: *mut lua_State, idx: c_int);
273}
274
275pub const LUA_NOREF: c_int = -1;
279pub const LUA_REFNIL: c_int = 0;
280
281extern "C" {
282 pub fn lua_ref(L: *mut lua_State, idx: c_int) -> c_int;
283 pub fn lua_unref(L: *mut lua_State, r#ref: c_int);
284}
285
286#[inline(always)]
291pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
292 lua_tonumberx(L, i, ptr::null_mut())
293}
294
295#[inline(always)]
296pub unsafe fn lua_tointeger_(L: *mut lua_State, i: c_int) -> lua_Integer {
297 lua_tointegerx_(L, i, ptr::null_mut())
298}
299
300#[inline(always)]
301pub unsafe fn lua_tounsigned(L: *mut lua_State, i: c_int) -> lua_Unsigned {
302 lua_tounsignedx(L, i, ptr::null_mut())
303}
304
305#[inline(always)]
306pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
307 lua_settop(L, -n - 1)
308}
309
310#[inline(always)]
311pub unsafe fn lua_newtable(L: *mut lua_State) {
312 lua_createtable(L, 0, 0)
313}
314
315#[inline(always)]
316pub unsafe fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void {
317 lua_newuserdatatagged(L, sz, 0)
318}
319
320#[inline(always)]
323pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
324 (lua_type(L, n) == LUA_TFUNCTION) as c_int
325}
326
327#[inline(always)]
328pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
329 (lua_type(L, n) == LUA_TTABLE) as c_int
330}
331
332#[inline(always)]
333pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
334 (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
335}
336
337#[inline(always)]
338pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
339 (lua_type(L, n) == LUA_TNIL) as c_int
340}
341
342#[inline(always)]
343pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
344 (lua_type(L, n) == LUA_TBOOLEAN) as c_int
345}
346
347#[inline(always)]
348pub unsafe fn lua_isvector(L: *mut lua_State, n: c_int) -> c_int {
349 (lua_type(L, n) == LUA_TVECTOR) as c_int
350}
351
352#[inline(always)]
353pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
354 (lua_type(L, n) == LUA_TTHREAD) as c_int
355}
356
357#[inline(always)]
358pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
359 (lua_type(L, n) == LUA_TNONE) as c_int
360}
361
362#[inline(always)]
363pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
364 (lua_type(L, n) <= LUA_TNIL) as c_int
365}
366
367#[inline(always)]
368pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) {
369 use std::ffi::CString;
370 let c_str = CString::new(s).unwrap();
371 lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
372}
373
374pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
375 lua_pushcclosurek(L, f, ptr::null(), 0, None)
376}
377
378pub unsafe fn lua_pushcfunctiond(L: *mut lua_State, f: lua_CFunction, debugname: *const c_char) {
379 lua_pushcclosurek(L, f, debugname, 0, None)
380}
381
382pub unsafe fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, nup: c_int) {
383 lua_pushcclosurek(L, f, ptr::null(), nup, None)
384}
385
386pub unsafe fn lua_pushcclosured(
387 L: *mut lua_State,
388 f: lua_CFunction,
389 debugname: *const c_char,
390 nup: c_int,
391) {
392 lua_pushcclosurek(L, f, debugname, nup, None)
393}
394
395#[inline(always)]
396pub unsafe fn lua_setglobal(L: *mut lua_State, var: *const c_char) {
397 lua_setfield(L, LUA_GLOBALSINDEX, var)
398}
399
400#[inline(always)]
401pub unsafe fn lua_getglobal(L: *mut lua_State, var: *const c_char) -> c_int {
402 lua_getfield(L, LUA_GLOBALSINDEX, var)
403}
404
405#[inline(always)]
406pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
407 lua_tolstring(L, i, ptr::null_mut())
408}
409
410const LUA_IDSIZE: usize = 256;
416
417pub type lua_Hook = unsafe extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug);
419
420pub type lua_Coverage = unsafe extern "C" fn(
421 context: *mut c_void,
422 function: *const c_char,
423 linedefined: c_int,
424 depth: c_int,
425 hits: *const c_int,
426 size: usize,
427);
428
429extern "C" {
430 pub fn lua_stackdepth(L: *mut lua_State) -> c_int;
431 pub fn lua_getinfo(
432 L: *mut lua_State,
433 level: c_int,
434 what: *const c_char,
435 ar: *mut lua_Debug,
436 ) -> c_int;
437 pub fn lua_getargument(L: *mut lua_State, level: c_int, n: c_int) -> c_int;
438 pub fn lua_getlocal(L: *mut lua_State, level: c_int, n: c_int) -> *const c_char;
439 pub fn lua_setlocal(L: *mut lua_State, level: c_int, n: c_int) -> *const c_char;
440 pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
441 pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
442
443 pub fn lua_singlestep(L: *mut lua_State, enabled: c_int);
444 pub fn lua_breakpoint(
445 L: *mut lua_State,
446 funcindex: c_int,
447 line: c_int,
448 enabled: c_int,
449 ) -> c_int;
450
451 pub fn lua_getcoverage(
452 L: *mut lua_State,
453 funcindex: c_int,
454 context: *mut c_void,
455 callback: lua_Coverage,
456 );
457
458 pub fn lua_debugtrace(L: *mut lua_State) -> *const c_char;
459}
460
461#[repr(C)]
462pub struct lua_Debug {
463 pub name: *const c_char,
464 pub what: *const c_char,
465 pub source: *const c_char,
466 pub short_src: *const c_char,
467 pub linedefined: c_int,
468 pub currentline: c_int,
469 pub nupvals: u8,
470 pub nparams: u8,
471 pub isvararg: c_char,
472 pub userdata: *mut c_void,
473 pub ssbuf: [c_char; LUA_IDSIZE],
474}
475
476#[repr(C)]
482pub struct lua_Callbacks {
483 pub userdata: *mut c_void,
485
486 pub interrupt: Option<unsafe extern "C" fn(L: *mut lua_State, gc: c_int)>,
488 pub panic: Option<unsafe extern "C" fn(L: *mut lua_State, errcode: c_int)>,
490
491 pub userthread: Option<unsafe extern "C" fn(LP: *mut lua_State, L: *mut lua_State)>,
493 pub useratom: Option<unsafe extern "C" fn(s: *const c_char, l: usize) -> i16>,
495
496 pub debugbreak: Option<unsafe extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug)>,
498 pub debugstep: Option<unsafe extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug)>,
500 pub debuginterrupt: Option<unsafe extern "C" fn(L: *mut lua_State, ar: *mut lua_Debug)>,
502 pub debugprotectederror: Option<unsafe extern "C" fn(L: *mut lua_State)>,
504}
505
506extern "C" {
507 pub fn lua_callbacks(L: *mut lua_State) -> *mut lua_Callbacks;
508}