Skip to main content

low_luajit/
ffi.rs

1//! The raw (and unsafe) LuaJIT C API.
2//!
3//! Some things are not yet safely exposed or their exposure is limited in some way;
4//! if you need something very specific from the LuaJIT C API, you're free to use these directly at your own discretion.
5//!
6//! *(Many API functions are actually just C macros, which have been recreated as accurately as possible.)*
7
8#![allow(non_camel_case_types)]
9#![allow(non_snake_case)]
10#![allow(clippy::inline_always)]
11#![allow(clippy::missing_safety_doc)]
12
13use core::{ffi::{CStr, c_char, c_double, c_int, c_long, c_void}, marker::{PhantomData, PhantomPinned}, mem::MaybeUninit, ptr};
14
15// --- Types Aliases ---
16pub type lua_CFunction = unsafe extern "C-unwind" fn(L: *mut lua_State) -> c_int;
17
18pub type lua_Reader = unsafe extern "C-unwind" fn(L: *mut lua_State, ud: *mut c_void, sz: *mut usize) -> *const c_char;
19pub type lua_Writer = unsafe extern "C-unwind" fn(L: *mut lua_State, p: *const c_void, sz: usize, ud: *mut c_void) -> c_int;
20
21pub type lua_Alloc = unsafe extern "C" fn(ud: *mut c_void, ptr: *mut c_void, osize: usize, nsize: usize) -> *mut c_void;
22
23pub type lua_Number = c_double;
24pub type lua_Integer = isize; // https://github.com/rust-lang/rust/issues/88345
25
26pub type lua_Hook = unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug);
27
28pub type luaJIT_profile_callback = unsafe extern "C-unwind" fn(L: *mut lua_State, samples: c_int, vmstate: c_int);
29
30// --- Constants ---
31pub const LUA_MULTRET: c_int = -1;
32
33pub const LUA_REGISTRYINDEX: c_int = -10000;
34pub const LUA_ENVIRONINDEX: c_int = -10001;
35pub const LUA_GLOBALSINDEX: c_int = -10002;
36
37pub const LUA_OK: c_int = 0;
38pub const LUA_YIELD: c_int = 1;
39pub const LUA_ERRRUN: c_int = 2;
40pub const LUA_ERRSYNTAX: c_int = 3;
41pub const LUA_ERRMEM: c_int = 4;
42pub const LUA_ERRERR: c_int = 5;
43pub const LUA_ERRFILE: c_int = LUA_ERRERR + 1;
44
45pub const LUA_TNONE: c_int = -1;
46pub const LUA_TNIL: c_int = 0;
47pub const LUA_TBOOLEAN: c_int = 1;
48pub const LUA_TLIGHTUSERDATA: c_int = 2;
49pub const LUA_TNUMBER: c_int = 3;
50pub const LUA_TSTRING: c_int = 4;
51pub const LUA_TTABLE: c_int = 5;
52pub const LUA_TFUNCTION: c_int = 6;
53pub const LUA_TUSERDATA: c_int = 7;
54pub const LUA_TTHREAD: c_int = 8;
55
56pub const LUA_MINSTACK: c_int = 20;
57
58pub const LUA_GCSTOP: c_int = 0;
59pub const LUA_GCRESTART: c_int = 1;
60pub const LUA_GCCOLLECT: c_int = 2;
61pub const LUA_GCCOUNT: c_int = 3;
62pub const LUA_GCCOUNTB: c_int = 4;
63pub const LUA_GCSTEP: c_int = 5;
64pub const LUA_GCSETPAUSE: c_int = 6;
65pub const LUA_GCSETSTEPMUL: c_int = 7;
66pub const LUA_GCISRUNNING: c_int = 9;
67
68pub const LUA_IDSIZE: usize = 60;
69
70pub const LUA_HOOKCALL: c_int = 0;
71pub const LUA_HOOKRET: c_int = 1;
72pub const LUA_HOOKLINE: c_int = 2;
73pub const LUA_HOOKCOUNT: c_int = 3;
74pub const LUA_HOOKTAILRET: c_int = 4;
75
76pub const LUA_MASKCALL: c_int = 1 << LUA_HOOKCALL;
77pub const LUA_MASKRET: c_int = 1 << LUA_HOOKRET;
78pub const LUA_MASKLINE: c_int = 1 << LUA_HOOKLINE;
79pub const LUA_MASKCOUNT: c_int = 1 << LUA_HOOKCOUNT;
80
81pub const LUA_NOREF: c_int = -2;
82pub const LUA_REFNIL: c_int = -1;
83
84/*#[cfg(windows)]
85pub const LUAL_BUFFERSIZE: usize = 512;
86#[cfg(not(windows))]
87pub const LUAL_BUFFERSIZE: usize = 0;*/
88
89pub const LUAJIT_MODE_MASK: c_int = 0x00FF;
90
91pub const LUAJIT_MODE_ENGINE: c_int = 0;
92pub const LUAJIT_MODE_DEBUG: c_int = 1;
93pub const LUAJIT_MODE_FUNC: c_int = 2;
94pub const LUAJIT_MODE_ALLFUNC: c_int = 3;
95pub const LUAJIT_MODE_ALLSUBFUNC: c_int = 4;
96pub const LUAJIT_MODE_TRACE: c_int = 5;
97pub const LUAJIT_MODE_WRAPCFUNC: c_int = 0x10;
98pub const LUAJIT_MODE_MAX: c_int = 0x11;
99
100pub const LUAJIT_MODE_OFF: c_int = 0x0000;
101pub const LUAJIT_MODE_ON: c_int = 0x0100;
102pub const LUAJIT_MODE_FLUSH: c_int = 0x0200;
103
104// --- Structs ---
105#[repr(C)]
106pub struct lua_State((), PhantomData<(*mut c_void, PhantomPinned)>);
107
108#[repr(C)]
109#[derive(Debug, Clone)]
110pub struct lua_Debug {
111	pub event: c_int,
112	pub name: MaybeUninit<*const c_char>,
113	pub namewhat: MaybeUninit<*const c_char>,
114	pub what: MaybeUninit<*const c_char>,
115	pub source: MaybeUninit<*const c_char>,
116	pub currentline: c_int,
117	pub nups: MaybeUninit<c_int>,
118	pub linedefined: MaybeUninit<c_int>,
119	pub lastlinedefined: MaybeUninit<c_int>,
120	pub short_src: MaybeUninit<[c_char; LUA_IDSIZE]>,
121	pub(crate) i_ci: c_int,
122}
123
124#[repr(C)]
125#[derive(Debug, Clone, Copy)]
126pub struct luaL_Reg {
127	pub name: *const c_char,
128	pub func: Option<lua_CFunction>,
129}
130
131/*#[repr(C)]
132pub struct luaL_Buffer {
133	p: *mut c_char,
134	lvl: c_int,
135	L: *mut lua_State,
136	buffer: [c_char; LUAL_BUFFERSIZE],
137}*/
138
139// --- Functions ---
140#[cfg_attr(windows, link(name = "lua51", kind = "raw-dylib"))]
141unsafe extern "C-unwind" {
142	pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State; // TODO: Expose this safely.
143	pub fn lua_close(L: *mut lua_State);
144	pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State; // TODO: Expose this safely.
145
146	pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction; // TODO: Expose this safely.
147
148	pub fn lua_gettop(L: *mut lua_State) -> c_int;
149	pub fn lua_settop(L: *mut lua_State, idx: c_int);
150	pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
151	pub fn lua_remove(L: *mut lua_State, idx: c_int);
152	pub fn lua_insert(L: *mut lua_State, idx: c_int);
153	pub fn lua_replace(L: *mut lua_State, idx: c_int);
154	pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
155
156	pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int); // TODO: Expose this safely.
157
158	pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
159	pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
160	pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
161	pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
162
163	pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
164	pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
165
166	pub fn lua_equal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
167	pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
168	pub fn lua_lessthan(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
169
170	pub fn lua_tonumber(L: *mut lua_State, idx: c_int) -> lua_Number;
171	pub fn lua_tointeger(L: *mut lua_State, idx: c_int) -> lua_Integer;
172	pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
173	pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
174	pub fn lua_objlen(L: *mut lua_State, idx: c_int) -> usize;
175	pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
176	pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
177	pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State; // TODO: Expose this safely.
178	pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
179
180	pub fn lua_pushnil(L: *mut lua_State);
181	pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
182	pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
183	pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, l: usize);
184	pub fn lua_pushstring(L: *mut lua_State, s: *const c_char);
185	// TODO: add lua_pushvfstring when VaList is stabilized.
186	pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char; // TODO: Expose this safely.
187	pub fn lua_pushcclosure(L: *mut lua_State, r#fn: lua_CFunction, n: c_int);
188	pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
189	pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
190	pub fn lua_pushthread(L: *mut lua_State) -> c_int;
191
192	pub fn lua_gettable(L: *mut lua_State, idx: c_int);
193	pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char);
194	pub fn lua_rawget(L: *mut lua_State, idx: c_int);
195	pub fn lua_rawgeti(L: *mut lua_State, idx: c_int, n: c_int);
196	pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
197	pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void;
198	pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
199	pub fn lua_getfenv(L: *mut lua_State, idx: c_int);
200
201	pub fn lua_settable(L: *mut lua_State, idx: c_int);
202	pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
203	pub fn lua_rawset(L: *mut lua_State, idx: c_int);
204	pub fn lua_rawseti(L: *mut lua_State, idx: c_int, n: c_int);
205	pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
206	pub fn lua_setfenv(L: *mut lua_State, idx: c_int) -> c_int;
207
208	pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
209	pub fn lua_pcall(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
210	pub fn lua_cpcall(L: *mut lua_State, func: lua_CFunction, ud: *mut c_void) -> c_int;
211	pub fn lua_load(L: *mut lua_State, reader: lua_Reader, dt: *mut c_void, chunkname: *const c_char) -> c_int; // TODO: Expose this safely.
212	pub fn lua_dump(L: *mut lua_State, writer: lua_Writer, dt: *mut c_void) -> c_int;
213
214	pub fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int; // TODO: Expose this safely.
215	pub fn lua_resume(L: *mut lua_State, narg: c_int) -> c_int; // TODO: Expose this safely.
216	pub fn lua_status(L: *mut lua_State) -> c_int; // TODO: Expose this safely.
217
218	pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
219	pub fn lua_error(L: *mut lua_State) -> c_int;
220	pub fn lua_next(L: *mut lua_State, index: c_int) -> c_int;
221	pub fn lua_concat(L: *mut lua_State, n: c_int);
222
223	pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc; // TODO: Expose this safely.
224	pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void); // TODO: Expose this safely.
225
226	pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
227	pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
228	pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
229	pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
230	pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
231	pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
232	pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int) -> c_int;
233	pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
234	pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
235	pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
236
237	pub fn lua_upvalueid(L: *mut lua_State, idx: c_int, n: c_int) -> *mut c_void;
238	pub fn lua_upvaluejoin(L: *mut lua_State, idx1: c_int, n1: c_int, idx2: c_int, n2: c_int); // TODO: Expose this safely.
239	pub fn lua_loadx(L: *mut lua_State, reader: lua_Reader, dt: *mut c_void, chunkname: *const c_char, mode: *const c_char) -> c_int; // TODO: Expose this safely.
240	pub safe fn lua_version(L: *mut lua_State) -> *const lua_Number;
241	pub fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int);
242	pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
243	pub fn lua_tointegerx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
244
245	pub fn lua_isyieldable(L: *mut lua_State) -> c_int; // TODO: Expose this safely.
246
247	pub fn luaL_openlib(L: *mut lua_State, libname: *const c_char, l: *const luaL_Reg, nup: c_int); // TODO: Expose this safely.
248	pub fn luaL_register(L: *mut lua_State, libname: *const c_char, l: *const luaL_Reg);
249	pub fn luaL_getmetafield(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int; // TODO: Expose this safely.
250	pub fn luaL_callmeta(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
251	pub fn luaL_typerror(L: *mut lua_State, narg: c_int, tname: *const c_char) -> c_int;
252	pub fn luaL_argerror(L: *mut lua_State, numarg: c_int, extramsg: *const c_char) -> c_int;
253
254	pub fn luaL_checklstring(L: *mut lua_State, numArg: c_int, l: *mut usize) -> *const c_char;
255	pub fn luaL_optlstring(L: *mut lua_State, numArg: c_int, def: *const c_char, l: *mut usize) -> *const c_char;
256	pub fn luaL_checknumber(L: *mut lua_State, numArg: c_int) -> lua_Number;
257	pub fn luaL_optnumber(L: *mut lua_State, nArg: c_int, def: lua_Number) -> lua_Number;
258	pub fn luaL_checkinteger(L: *mut lua_State, numArg: c_int) -> lua_Integer;
259	pub fn luaL_optinteger(L: *mut lua_State, nArg: c_int, def: lua_Integer) -> lua_Integer;
260
261	pub fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char);
262	pub fn luaL_checktype(L:*mut lua_State, narg: c_int, t: c_int);
263	pub fn luaL_checkany(L: *mut lua_State, narg: c_int);
264
265	pub fn luaL_newmetatable(L: *mut lua_State, tname: *const c_char) -> c_int;
266	pub fn luaL_checkudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void;
267
268	pub fn luaL_where(L: *mut lua_State, lvl: c_int);
269	pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> c_int;
270
271	pub fn luaL_checkoption(L: *mut lua_State, narg: c_int, def: *const c_char, lst: *const *const c_char) -> c_int; // TODO: Expose this safely.
272
273	pub fn luaL_ref(L: *mut lua_State, t: c_int) -> c_int; // TODO: Expose this safely.
274	pub fn luaL_unref(L: *mut lua_State, t: c_int, r#ref: c_int); // TODO: Expose this safely.
275
276	pub fn luaL_loadfile(L: *mut lua_State, filename: *const c_char) -> c_int;
277	pub fn luaL_loadbuffer(L: *mut lua_State, buff: *const c_char, sz: usize, name: *const c_char) -> c_int;
278	pub fn luaL_loadstring(L: *mut lua_State, s: *const c_char) -> c_int;
279
280	pub safe fn luaL_newstate() -> *mut lua_State; // TODO: Expose this safely.
281
282	pub fn luaL_gsub(L: *mut lua_State, s: *const c_char, p: *const c_char, r: *const c_char) -> *const c_char;
283
284	pub fn luaL_findtable(L: *mut lua_State, idx: c_int, fname: *const c_char, szhint: c_int) -> *const c_char; // TODO: Expose this safely.
285
286	pub fn luaL_fileresult(L: *mut lua_State, stat: c_int, fname: *const c_char) -> c_int; // TODO: Expose this safely.
287	pub fn luaL_execresult(L: *mut lua_State, stat: c_int) -> c_int; // TODO: Expose this safely.
288	pub fn luaL_loadfilex(L: *mut lua_State, filename: *const c_char, mode: *const c_char) -> c_int; // TODO: Expose this safely.
289	pub fn luaL_loadbufferx(L: *mut lua_State, buff: *const c_char, sz: usize, name: *const c_char, mode: *const c_char) -> c_int; // TODO: Expose this safely.
290	pub fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const c_char, level: c_int); // TODO: Expose this safely.
291	pub fn luaL_setfuncs(L: *mut lua_State, l: *const luaL_Reg, nup: c_int); // TODO: Expose this safely.
292	pub fn luaL_pushmodule(L: *mut lua_State, modname: *const c_char, sizehint: c_int); // TODO: Expose this safely.
293	pub fn luaL_testudata(L: *mut lua_State, ud: c_int, tname: *const c_char); // TODO: Expose this safely.
294	pub fn luaL_setmetatable(L: *mut lua_State, tname: *const c_char); // TODO: Expose this safely.
295
296	// add buffer stuff when I figure out how to have luaL_Buffer exist...
297	//pub fn luaL_addstring(B: *mut luaL_Buffer, s: *const c_char);
298	//pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Buffer);
299	//pub fn luaL_pushresult(B: *mut luaL_Buffer);
300
301	pub fn luaL_openlibs(L: *mut lua_State); // TODO: Expose this safely?
302
303	pub fn luaopen_base(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
304	pub fn luaopen_math(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
305	pub fn luaopen_string(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
306	pub fn luaopen_table(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
307	pub fn luaopen_io(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
308	pub fn luaopen_os(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
309	pub fn luaopen_package(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
310	pub fn luaopen_debug(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
311	pub fn luaopen_bit(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
312	pub fn luaopen_jit(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
313	pub fn luaopen_ffi(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
314	pub fn luaopen_string_buffer(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
315
316	pub fn luaJIT_setmode(L: *mut lua_State, idx: c_int, mode: c_int) -> c_int; // TODO: Expose this safely.
317	pub fn luaJIT_profile_start(L: *mut lua_State, mode: *const c_char, cb: luaJIT_profile_callback, data: *mut c_void); // TODO: Expose this safely.
318	pub fn luaJIT_profile_stop(L: *mut lua_State); // TODO: Expose this safely.
319	pub fn luaJIT_profile_dumpstack(L: *mut lua_State, fmt: *const c_char, depth: c_int, len: *mut usize) -> *const c_char; // TODO: Expose this safely.
320}
321
322// --- Macros ---
323#[inline(always)]
324pub const fn lua_upvalueindex(i: c_int) -> c_int {
325	LUA_GLOBALSINDEX - i as c_int
326}
327
328#[inline(always)]
329pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
330	unsafe { lua_settop(L, -(n)-1); }
331}
332
333#[inline(always)]
334pub unsafe fn lua_newtable(L: *mut lua_State) {
335	unsafe { lua_createtable(L, 0, 0); }
336}
337
338#[inline(always)]
339pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
340	unsafe { lua_pushcfunction(L, f); lua_setglobal(L, n); }
341}
342
343#[inline(always)]
344pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
345	unsafe { lua_pushcclosure(L, f, 0); }
346}
347
348#[inline(always)]
349pub unsafe fn lua_strlen(L: *mut lua_State, i: c_int) -> usize {
350	unsafe { lua_objlen(L, i) }
351}
352
353#[inline(always)]
354pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
355	unsafe { c_int::from(lua_type(L, n) == LUA_TFUNCTION) }
356}
357
358#[inline(always)]
359pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
360	unsafe { c_int::from(lua_type(L, n) == LUA_TTABLE) }
361}
362
363#[inline(always)]
364pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
365	unsafe { c_int::from(lua_type(L, n) == LUA_TLIGHTUSERDATA) }
366}
367
368#[inline(always)]
369pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
370	unsafe { c_int::from(lua_type(L, n) == LUA_TNIL) }
371}
372
373#[inline(always)]
374pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
375	unsafe { c_int::from(lua_type(L, n) == LUA_TBOOLEAN) }
376}
377
378#[inline(always)]
379pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
380	unsafe { c_int::from(lua_type(L, n) == LUA_TTHREAD) }
381}
382
383#[inline(always)]
384pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
385	unsafe { c_int::from(lua_type(L, n) == LUA_TNONE) }
386}
387
388#[inline(always)]
389pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
390	unsafe { c_int::from(lua_type(L, n) <= 0) }
391}
392
393#[inline(always)]
394pub unsafe fn lua_pushliteral(L: *mut lua_State, s: *const c_char) {
395	unsafe { lua_pushlstring(L, s, CStr::from_ptr(s).count_bytes()); }
396}
397
398#[inline(always)]
399pub unsafe fn lua_setglobal(L: *mut lua_State, s: *const c_char) {
400	unsafe { lua_setfield(L, LUA_GLOBALSINDEX, s); }
401}
402
403#[inline(always)]
404pub unsafe fn lua_getglobal(L: *mut lua_State, s: *const c_char) {
405	unsafe { lua_getfield(L, LUA_GLOBALSINDEX, s); }
406}
407
408#[inline(always)]
409pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
410	unsafe { lua_tolstring(L, i, ptr::null_mut()) }
411}
412
413#[inline(always)]
414pub unsafe fn luaL_argcheck(L: *mut lua_State, cond: c_int, numarg: c_int, extramsg: *const c_char) {
415	if cond != 0 { unsafe { luaL_argerror(L, numarg, extramsg); } }
416}
417
418#[inline(always)]
419pub unsafe fn luaL_checkstring(L: *mut lua_State, n: c_int) -> *const c_char {
420	unsafe { luaL_checklstring(L, n, ptr::null_mut()) }
421}
422
423#[inline(always)]
424pub unsafe fn luaL_optstring(L: *mut lua_State, n: c_int, d: *const c_char) -> *const c_char {
425	unsafe { luaL_optlstring(L, n, d, ptr::null_mut()) }
426}
427
428#[inline(always)]
429#[allow(clippy::cast_possible_truncation)]
430pub unsafe fn luaL_checkint(L: *mut lua_State, n: c_int) -> c_int {
431	unsafe { luaL_checkinteger(L, n) as c_int }
432}
433
434#[inline(always)]
435#[allow(clippy::cast_possible_truncation)]
436pub unsafe fn luaL_optint(L: *mut lua_State, n: c_int, d: lua_Integer) -> c_int {
437	unsafe { luaL_optinteger(L, n, d) as c_int }
438}
439
440#[inline(always)]
441pub unsafe fn luaL_checklong(L: *mut lua_State, n: c_int) -> c_long {
442	unsafe { luaL_checkinteger(L, n) as c_long }
443}
444
445#[inline(always)]
446pub unsafe fn luaL_optlong(L: *mut lua_State, n: c_int, d: lua_Integer) -> c_long {
447	unsafe { luaL_optinteger(L, n, d) as c_long }
448}
449
450#[inline(always)]
451pub unsafe fn luaL_typename(L: *mut lua_State, i: i32) -> *const c_char {
452	unsafe { lua_typename(L, lua_type(L, i)) }
453}
454
455// TODO: Expose this safely.
456#[inline(always)]
457pub unsafe fn luaL_dofile(L: *mut lua_State, r#fn: *const c_char) -> c_int {
458	unsafe {
459		let ok = luaL_loadfile(L, r#fn);
460		if ok != 0 { return ok; }
461		lua_pcall(L, 0, LUA_MULTRET, 0)
462	}
463}
464
465// TODO: Expose this safely.
466#[inline(always)]
467pub unsafe fn luaL_dostring(L: *mut lua_State, s: *const c_char) -> c_int {
468	unsafe {
469		let ok = luaL_loadstring(L, s);
470		if ok != 0 { return ok; }
471		lua_pcall(L, 0, LUA_MULTRET, 0)
472	}
473}
474
475// TODO: Expose this safely.
476#[inline(always)]
477pub unsafe fn luaL_getmetatable(L: *mut lua_State, n: *const c_char) {
478	unsafe { lua_getfield(L, LUA_REGISTRYINDEX, n) }
479}
480
481#[inline(always)]
482pub unsafe fn luaL_opt<T>(L: *mut lua_State, f: impl FnOnce(*mut lua_State, c_int) -> T, n: c_int, d: T) -> T {
483	if unsafe { lua_isnoneornil(L, n) != 0 } { d } else { f(L, n) }
484}
485
486#[inline(always)]
487pub unsafe fn luaL_newlibtable(L: *mut lua_State, l: *const luaL_Reg) {
488	const unsafe fn sizeof(mut l: *const luaL_Reg) -> c_int {
489		let mut size = 0;
490
491		while unsafe { !l.read().name.is_null() } {
492			size += 1;
493			l = unsafe { l.add(1) };
494		}
495
496		size
497	}
498
499	unsafe { lua_createtable(L, 0, sizeof(l)); }
500}
501
502#[inline(always)]
503pub unsafe fn luaL_newlib(L: *mut lua_State, l: *const luaL_Reg) {
504	unsafe { luaL_newlibtable(L, l); luaL_setfuncs(L, l, 0); }
505}