1use std::marker::{PhantomData, PhantomPinned};
4use std::os::raw::{c_char, c_double, c_int, c_uchar, c_uint, c_void};
5use std::ptr;
6
7pub const LUA_SIGNATURE: &[u8] = b"\x1bLua";
9
10pub const LUA_MULTRET: c_int = -1;
12
13pub const LUAI_MAXSTACK: c_int = 1000000;
15
16pub const LUA_REGISTRYINDEX: c_int = -LUAI_MAXSTACK - 1000;
20
21pub const fn lua_upvalueindex(i: c_int) -> c_int {
22 LUA_REGISTRYINDEX - i
23}
24
25pub const LUA_OK: c_int = 0;
29pub const LUA_YIELD: c_int = 1;
30pub const LUA_ERRRUN: c_int = 2;
31pub const LUA_ERRSYNTAX: c_int = 3;
32pub const LUA_ERRMEM: c_int = 4;
33pub const LUA_ERRGCMM: c_int = 5;
34pub const LUA_ERRERR: c_int = 6;
35
36#[repr(C)]
38pub struct lua_State {
39 _data: [u8; 0],
40 _marker: PhantomData<(*mut u8, PhantomPinned)>,
41}
42
43pub const LUA_TNONE: c_int = -1;
47
48pub const LUA_TNIL: c_int = 0;
49pub const LUA_TBOOLEAN: c_int = 1;
50pub const LUA_TLIGHTUSERDATA: c_int = 2;
51pub const LUA_TNUMBER: c_int = 3;
52pub const LUA_TSTRING: c_int = 4;
53pub const LUA_TTABLE: c_int = 5;
54pub const LUA_TFUNCTION: c_int = 6;
55pub const LUA_TUSERDATA: c_int = 7;
56pub const LUA_TTHREAD: c_int = 8;
57
58pub const LUA_NUMTAGS: c_int = 9;
59
60pub const LUA_MINSTACK: c_int = 20;
62
63pub const LUA_RIDX_MAINTHREAD: lua_Integer = 1;
65pub const LUA_RIDX_GLOBALS: lua_Integer = 2;
66pub const LUA_RIDX_LAST: lua_Integer = LUA_RIDX_GLOBALS;
67
68pub type lua_Number = c_double;
70
71#[cfg(target_pointer_width = "32")]
73pub type lua_Integer = i32;
74#[cfg(target_pointer_width = "64")]
75pub type lua_Integer = i64;
76
77pub type lua_Unsigned = c_uint;
79
80pub type lua_CFunction = unsafe extern "C-unwind" fn(L: *mut lua_State) -> c_int;
82
83#[rustfmt::skip]
85pub type lua_Reader =
86 unsafe extern "C-unwind" fn(L: *mut lua_State, ud: *mut c_void, sz: *mut usize) -> *const c_char;
87#[rustfmt::skip]
88pub type lua_Writer =
89 unsafe extern "C-unwind" fn(L: *mut lua_State, p: *const c_void, sz: usize, ud: *mut c_void) -> c_int;
90
91#[rustfmt::skip]
93pub type lua_Alloc =
94 unsafe extern "C-unwind" fn(ud: *mut c_void, ptr: *mut c_void, osize: usize, nsize: usize) -> *mut c_void;
95
96#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
97extern "C-unwind" {
98 pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
102 pub fn lua_close(L: *mut lua_State);
103 pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
104
105 pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
106
107 pub fn lua_version(L: *mut lua_State) -> *const lua_Number;
108
109 pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
113 pub fn lua_gettop(L: *mut lua_State) -> c_int;
114 pub fn lua_settop(L: *mut lua_State, idx: c_int);
115 pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
116 pub fn lua_remove(L: *mut lua_State, idx: c_int);
117 pub fn lua_insert(L: *mut lua_State, idx: c_int);
118 pub fn lua_replace(L: *mut lua_State, idx: c_int);
119 pub fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int);
120 pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
121
122 pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
123
124 pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
128 pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
129 pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
130 pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
131 pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
132 pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
133
134 pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
135 #[link_name = "lua_tointegerx"]
136 pub fn lua_tointegerx_(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
137 pub fn lua_tounsignedx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Unsigned;
138 pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
139 pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
140 pub fn lua_rawlen(L: *mut lua_State, idx: c_int) -> usize;
141 pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
142 pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
143 pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
144 pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
145}
146
147pub const LUA_OPADD: c_int = 0;
151pub const LUA_OPSUB: c_int = 1;
152pub const LUA_OPMUL: c_int = 2;
153pub const LUA_OPDIV: c_int = 3;
154pub const LUA_OPMOD: c_int = 4;
155pub const LUA_OPPOW: c_int = 5;
156pub const LUA_OPUNM: c_int = 6;
157
158pub const LUA_OPEQ: c_int = 0;
159pub const LUA_OPLT: c_int = 1;
160pub const LUA_OPLE: c_int = 2;
161
162#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
163extern "C-unwind" {
164 pub fn lua_arith(L: *mut lua_State, op: c_int);
165 pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
166 pub fn lua_compare(L: *mut lua_State, idx1: c_int, idx2: c_int, op: c_int) -> c_int;
167}
168
169#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
170extern "C-unwind" {
171 pub fn lua_pushnil(L: *mut lua_State);
175 pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
176 pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
177 pub fn lua_pushunsigned(L: *mut lua_State, n: lua_Unsigned);
178 #[link_name = "lua_pushlstring"]
179 pub fn lua_pushlstring_(L: *mut lua_State, s: *const c_char, l: usize) -> *const c_char;
180 pub fn lua_pushstring(L: *mut lua_State, s: *const c_char) -> *const c_char;
181 pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
183 pub fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, n: c_int);
184 pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
185 pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
186 pub fn lua_pushthread(L: *mut lua_State) -> c_int;
187
188 #[link_name = "lua_getglobal"]
192 pub fn lua_getglobal_(L: *mut lua_State, name: *const c_char);
193 #[link_name = "lua_gettable"]
194 pub fn lua_gettable_(L: *mut lua_State, idx: c_int);
195 #[link_name = "lua_getfield"]
196 pub fn lua_getfield_(L: *mut lua_State, idx: c_int, k: *const c_char);
197 #[link_name = "lua_rawget"]
198 pub fn lua_rawget_(L: *mut lua_State, idx: c_int);
199 #[link_name = "lua_rawgeti"]
200 pub fn lua_rawgeti_(L: *mut lua_State, idx: c_int, n: c_int);
201 #[link_name = "lua_rawgetp"]
202 pub fn lua_rawgetp_(L: *mut lua_State, idx: c_int, p: *const c_void);
203 pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
204 pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void;
205 pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
206 #[link_name = "lua_getuservalue"]
207 pub fn lua_getuservalue_(L: *mut lua_State, idx: c_int);
208
209 pub fn lua_setglobal(L: *mut lua_State, var: *const c_char);
213 pub fn lua_settable(L: *mut lua_State, idx: c_int);
214 pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
215 pub fn lua_rawset(L: *mut lua_State, idx: c_int);
216 #[link_name = "lua_rawseti"]
217 pub fn lua_rawseti_(L: *mut lua_State, idx: c_int, n: c_int);
218 pub fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void);
219 pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
220 pub fn lua_setuservalue(L: *mut lua_State, idx: c_int);
221
222 pub fn lua_callk(L: *mut lua_State, nargs: c_int, nresults: c_int, ctx: c_int, k: Option<lua_CFunction>);
226 pub fn lua_pcallk(
227 L: *mut lua_State,
228 nargs: c_int,
229 nresults: c_int,
230 errfunc: c_int,
231 ctx: c_int,
232 k: Option<lua_CFunction>,
233 ) -> c_int;
234
235 pub fn lua_getctx(L: *mut lua_State, ctx: *mut c_int) -> c_int;
236
237 pub fn lua_load(
238 L: *mut lua_State,
239 reader: lua_Reader,
240 data: *mut c_void,
241 chunkname: *const c_char,
242 mode: *const c_char,
243 ) -> c_int;
244 #[link_name = "lua_dump"]
245 pub fn lua_dump_(L: *mut lua_State, writer: lua_Writer, data: *mut c_void) -> c_int;
246}
247
248#[inline(always)]
249pub unsafe fn lua_call(L: *mut lua_State, n: c_int, r: c_int) {
250 lua_callk(L, n, r, 0, None)
251}
252
253#[inline(always)]
254pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
255 lua_pcallk(L, n, r, f, 0, None)
256}
257
258#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
259extern "C-unwind" {
260 pub fn lua_yieldk(L: *mut lua_State, nresults: c_int, ctx: c_int, k: Option<lua_CFunction>) -> c_int;
264 #[link_name = "lua_resume"]
265 pub fn lua_resume_(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
266 pub fn lua_status(L: *mut lua_State) -> c_int;
267}
268
269#[inline(always)]
270pub unsafe fn lua_yield(L: *mut lua_State, n: c_int) -> c_int {
271 lua_yieldk(L, n, 0, None)
272}
273
274pub const LUA_GCSTOP: c_int = 0;
278pub const LUA_GCRESTART: c_int = 1;
279pub const LUA_GCCOLLECT: c_int = 2;
280pub const LUA_GCCOUNT: c_int = 3;
281pub const LUA_GCCOUNTB: c_int = 4;
282pub const LUA_GCSTEP: c_int = 5;
283pub const LUA_GCSETPAUSE: c_int = 6;
284pub const LUA_GCSETSTEPMUL: c_int = 7;
285pub const LUA_GCSETMAJORINC: c_int = 8;
286pub const LUA_GCISRUNNING: c_int = 9;
287pub const LUA_GCGEN: c_int = 10;
288pub const LUA_GCINC: c_int = 11;
289
290#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
291extern "C-unwind" {
292 pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
293}
294
295#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
296extern "C-unwind" {
297 #[link_name = "lua_error"]
301 fn lua_error_(L: *mut lua_State) -> c_int;
302 pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
303 pub fn lua_concat(L: *mut lua_State, n: c_int);
304 pub fn lua_len(L: *mut lua_State, idx: c_int);
305 pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
306 pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void);
307}
308
309#[inline(always)]
313pub unsafe fn lua_error(L: *mut lua_State) -> ! {
314 lua_error_(L);
315 unreachable!();
316}
317
318#[inline(always)]
322pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
323 lua_tonumberx(L, i, ptr::null_mut())
324}
325
326#[inline(always)]
327pub unsafe fn lua_tointeger_(L: *mut lua_State, i: c_int) -> lua_Integer {
328 lua_tointegerx_(L, i, ptr::null_mut())
329}
330
331#[inline(always)]
332pub unsafe fn lua_tounsigned(L: *mut lua_State, i: c_int) -> lua_Unsigned {
333 lua_tounsignedx(L, i, ptr::null_mut())
334}
335
336#[inline(always)]
337pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
338 lua_settop(L, -n - 1)
339}
340
341#[inline(always)]
342pub unsafe fn lua_newtable(L: *mut lua_State) {
343 lua_createtable(L, 0, 0)
344}
345
346#[inline(always)]
347pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
348 lua_pushcfunction(L, f);
349 lua_setglobal(L, n)
350}
351
352#[inline(always)]
353pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
354 lua_pushcclosure(L, f, 0)
355}
356
357#[inline(always)]
358pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
359 (lua_type(L, n) == LUA_TFUNCTION) as c_int
360}
361
362#[inline(always)]
363pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
364 (lua_type(L, n) == LUA_TTABLE) as c_int
365}
366
367#[inline(always)]
368pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
369 (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
370}
371
372#[inline(always)]
373pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
374 (lua_type(L, n) == LUA_TNIL) as c_int
375}
376
377#[inline(always)]
378pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
379 (lua_type(L, n) == LUA_TBOOLEAN) as c_int
380}
381
382#[inline(always)]
383pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
384 (lua_type(L, n) == LUA_TTHREAD) as c_int
385}
386
387#[inline(always)]
388pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
389 (lua_type(L, n) == LUA_TNONE) as c_int
390}
391
392#[inline(always)]
393pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
394 (lua_type(L, n) <= 0) as c_int
395}
396
397#[inline(always)]
398pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
399 use std::ffi::CString;
400 let c_str = CString::new(s).unwrap();
401 lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
402}
403
404#[inline(always)]
405pub unsafe fn lua_pushglobaltable(L: *mut lua_State) {
406 lua_rawgeti_(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS as _)
407}
408
409#[inline(always)]
410pub unsafe fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void {
411 if lua_islightuserdata(L, idx) != 0 {
412 return lua_touserdata(L, idx);
413 }
414 ptr::null_mut()
415}
416
417#[inline(always)]
418pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
419 lua_tolstring(L, i, ptr::null_mut())
420}
421
422#[inline(always)]
423pub unsafe fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int) {
424 lua_pushvalue(from, idx);
425 lua_xmove(from, to, 1);
426}
427
428const LUA_IDSIZE: usize = 60;
434
435pub const LUA_HOOKCALL: c_int = 0;
437pub const LUA_HOOKRET: c_int = 1;
438pub const LUA_HOOKLINE: c_int = 2;
439pub const LUA_HOOKCOUNT: c_int = 3;
440pub const LUA_HOOKTAILCALL: c_int = 4;
441
442pub const LUA_MASKCALL: c_int = 1 << (LUA_HOOKCALL as usize);
444pub const LUA_MASKRET: c_int = 1 << (LUA_HOOKRET as usize);
445pub const LUA_MASKLINE: c_int = 1 << (LUA_HOOKLINE as usize);
446pub const LUA_MASKCOUNT: c_int = 1 << (LUA_HOOKCOUNT as usize);
447
448pub type lua_Hook = unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug);
450
451#[cfg_attr(all(windows, raw_dylib), link(name = "lua52", kind = "raw-dylib"))]
452extern "C-unwind" {
453 pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
454 pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
455 pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
456 pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
457 pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
458 pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
459
460 pub fn lua_upvalueid(L: *mut lua_State, fidx: c_int, n: c_int) -> *mut c_void;
461 pub fn lua_upvaluejoin(L: *mut lua_State, fidx1: c_int, n1: c_int, fidx2: c_int, n2: c_int);
462
463 pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int) -> c_int;
464 pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
465 pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
466 pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
467}
468
469#[repr(C)]
470pub struct lua_Debug {
471 pub event: c_int,
472 pub name: *const c_char,
473 pub namewhat: *const c_char,
474 pub what: *const c_char,
475 pub source: *const c_char,
476 pub currentline: c_int,
477 pub linedefined: c_int,
478 pub lastlinedefined: c_int,
479 pub nups: c_uchar,
480 pub nparams: c_uchar,
481 pub isvararg: c_char,
482 pub istailcall: c_char,
483 pub short_src: [c_char; LUA_IDSIZE],
484 i_ci: *mut c_void,
486}