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
84pub const LUAL_BUFFERSIZE: usize = if cfg!(windows) {
85	512
86} else if cfg!(unix) {
87	if cfg!(any(all(target_os = "linux", target_env = "gnu"), target_os = "haiku")) {
88		8192
89	} else if cfg!(any(all(target_os = "linux", target_env = "uclibc"), target_os = "aix")) {
90		4096
91	} else if cfg!(all(target_os = "l4re", target_env = "uclibc")) {
92		256
93	} else {
94		// Any other supported unix target (like musl, android, or apple) should not be bigger than this.
95		1024
96	}
97} else {
98	// Unknown, overcompensate the size (extra space will just be ignored).
99	8192
100};
101
102pub const LUAJIT_MODE_MASK: c_int = 0x00FF;
103
104pub const LUAJIT_MODE_ENGINE: c_int = 0;
105pub const LUAJIT_MODE_DEBUG: c_int = 1;
106pub const LUAJIT_MODE_FUNC: c_int = 2;
107pub const LUAJIT_MODE_ALLFUNC: c_int = 3;
108pub const LUAJIT_MODE_ALLSUBFUNC: c_int = 4;
109pub const LUAJIT_MODE_TRACE: c_int = 5;
110pub const LUAJIT_MODE_WRAPCFUNC: c_int = 0x10;
111pub const LUAJIT_MODE_MAX: c_int = 0x11;
112
113pub const LUAJIT_MODE_OFF: c_int = 0x0000;
114pub const LUAJIT_MODE_ON: c_int = 0x0100;
115pub const LUAJIT_MODE_FLUSH: c_int = 0x0200;
116
117// --- Structs ---
118#[repr(C)]
119pub struct lua_State((), PhantomData<(*mut c_void, PhantomPinned)>);
120
121#[repr(C)]
122#[derive(Debug, Clone)]
123pub struct lua_Debug {
124	pub event: c_int,
125	pub name: MaybeUninit<*const c_char>,
126	pub namewhat: MaybeUninit<*const c_char>,
127	pub what: MaybeUninit<*const c_char>,
128	pub source: MaybeUninit<*const c_char>,
129	pub currentline: c_int,
130	pub nups: MaybeUninit<c_int>,
131	pub linedefined: MaybeUninit<c_int>,
132	pub lastlinedefined: MaybeUninit<c_int>,
133	pub short_src: MaybeUninit<[c_char; LUA_IDSIZE]>,
134	pub(crate) i_ci: c_int,
135}
136
137#[repr(C)]
138#[derive(Debug, Clone, Copy)]
139pub struct luaL_Reg {
140	pub name: *const c_char,
141	pub func: Option<lua_CFunction>,
142}
143
144#[repr(C)]
145#[derive(Debug)]
146pub struct luaL_Buffer {
147	pub(crate) p: *mut c_char,
148	pub(crate) lvl: c_int,
149	pub(crate) L: *mut lua_State,
150	pub(crate) buffer: [MaybeUninit<c_char>; LUAL_BUFFERSIZE],
151	_marker: PhantomData<PhantomPinned>,
152}
153
154// --- Functions ---
155#[cfg_attr(windows, link(name = "lua51", kind = "raw-dylib"))]
156unsafe extern "C-unwind" {
157	pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State; // TODO: Expose this safely.
158	pub fn lua_close(L: *mut lua_State);
159	pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State; // TODO: Expose this safely.
160
161	pub fn lua_atpanic(L: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction; // TODO: Expose this safely.
162
163	pub fn lua_gettop(L: *mut lua_State) -> c_int;
164	pub fn lua_settop(L: *mut lua_State, idx: c_int);
165	pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
166	pub fn lua_remove(L: *mut lua_State, idx: c_int);
167	pub fn lua_insert(L: *mut lua_State, idx: c_int);
168	pub fn lua_replace(L: *mut lua_State, idx: c_int);
169	pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
170
171	pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int); // TODO: Expose this safely.
172
173	pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
174	pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
175	pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
176	pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
177
178	pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
179	pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
180
181	pub fn lua_equal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
182	pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
183	pub fn lua_lessthan(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
184
185	pub fn lua_tonumber(L: *mut lua_State, idx: c_int) -> lua_Number;
186	pub fn lua_tointeger(L: *mut lua_State, idx: c_int) -> lua_Integer;
187	pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
188	pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
189	pub fn lua_objlen(L: *mut lua_State, idx: c_int) -> usize;
190	pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
191	pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
192	pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State; // TODO: Expose this safely.
193	pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
194
195	pub fn lua_pushnil(L: *mut lua_State);
196	pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
197	pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
198	pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, l: usize);
199	pub fn lua_pushstring(L: *mut lua_State, s: *const c_char);
200	#[cfg(feature = "no_msrv")]
201	#[clippy::msrv = "1.99.0"]
202	pub fn lua_pushvfstring(L: *mut lua_State, fmt: *const c_char, argp: core::ffi::VaList) -> *const c_char; // TODO: Expose this (safely?).
203	pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char; // TODO: Expose this (safely?).
204	pub fn lua_pushcclosure(L: *mut lua_State, r#fn: lua_CFunction, n: c_int);
205	pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
206	pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
207	pub fn lua_pushthread(L: *mut lua_State) -> c_int;
208
209	pub fn lua_gettable(L: *mut lua_State, idx: c_int);
210	pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char);
211	pub fn lua_rawget(L: *mut lua_State, idx: c_int);
212	pub fn lua_rawgeti(L: *mut lua_State, idx: c_int, n: c_int);
213	pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
214	pub fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void;
215	pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
216	pub fn lua_getfenv(L: *mut lua_State, idx: c_int);
217
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_rawset(L: *mut lua_State, idx: c_int);
221	pub fn lua_rawseti(L: *mut lua_State, idx: c_int, n: c_int);
222	pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
223	pub fn lua_setfenv(L: *mut lua_State, idx: c_int) -> c_int;
224
225	pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
226	pub fn lua_pcall(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
227	pub fn lua_cpcall(L: *mut lua_State, func: lua_CFunction, ud: *mut c_void) -> c_int;
228	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.
229	pub fn lua_dump(L: *mut lua_State, writer: lua_Writer, dt: *mut c_void) -> c_int;
230
231	pub fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int; // TODO: Expose this safely.
232	pub fn lua_resume(L: *mut lua_State, narg: c_int) -> c_int; // TODO: Expose this safely.
233	pub fn lua_status(L: *mut lua_State) -> c_int; // TODO: Expose this safely.
234
235	pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
236	pub fn lua_error(L: *mut lua_State) -> c_int;
237	pub fn lua_next(L: *mut lua_State, index: c_int) -> c_int;
238	pub fn lua_concat(L: *mut lua_State, n: c_int);
239
240	pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc; // TODO: Expose this safely.
241	pub fn lua_setallocf(L: *mut lua_State, f: lua_Alloc, ud: *mut c_void); // TODO: Expose this safely.
242
243	pub fn lua_getstack(L: *mut lua_State, level: c_int, ar: *mut lua_Debug) -> c_int;
244	pub fn lua_getinfo(L: *mut lua_State, what: *const c_char, ar: *mut lua_Debug) -> c_int;
245	pub fn lua_getlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
246	pub fn lua_setlocal(L: *mut lua_State, ar: *const lua_Debug, n: c_int) -> *const c_char;
247	pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
248	pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
249	pub fn lua_sethook(L: *mut lua_State, func: Option<lua_Hook>, mask: c_int, count: c_int) -> c_int;
250	pub fn lua_gethook(L: *mut lua_State) -> Option<lua_Hook>;
251	pub fn lua_gethookmask(L: *mut lua_State) -> c_int;
252	pub fn lua_gethookcount(L: *mut lua_State) -> c_int;
253
254	pub fn lua_upvalueid(L: *mut lua_State, idx: c_int, n: c_int) -> *mut c_void;
255	pub fn lua_upvaluejoin(L: *mut lua_State, idx1: c_int, n1: c_int, idx2: c_int, n2: c_int); // TODO: Expose this safely.
256	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.
257	pub safe fn lua_version(L: *mut lua_State) -> *const lua_Number;
258	pub fn lua_copy(L: *mut lua_State, fromidx: c_int, toidx: c_int);
259	pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
260	pub fn lua_tointegerx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
261
262	pub fn lua_isyieldable(L: *mut lua_State) -> c_int; // TODO: Expose this safely.
263
264	pub fn luaL_openlib(L: *mut lua_State, libname: *const c_char, l: *const luaL_Reg, nup: c_int); // TODO: Expose this safely.
265	pub fn luaL_register(L: *mut lua_State, libname: *const c_char, l: *const luaL_Reg);
266	pub fn luaL_getmetafield(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int; // TODO: Expose this safely.
267	pub fn luaL_callmeta(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int;
268	pub fn luaL_typerror(L: *mut lua_State, narg: c_int, tname: *const c_char) -> c_int;
269	pub fn luaL_argerror(L: *mut lua_State, numarg: c_int, extramsg: *const c_char) -> c_int;
270
271	pub fn luaL_checklstring(L: *mut lua_State, numArg: c_int, l: *mut usize) -> *const c_char;
272	pub fn luaL_optlstring(L: *mut lua_State, numArg: c_int, def: *const c_char, l: *mut usize) -> *const c_char;
273	pub fn luaL_checknumber(L: *mut lua_State, numArg: c_int) -> lua_Number;
274	pub fn luaL_optnumber(L: *mut lua_State, nArg: c_int, def: lua_Number) -> lua_Number;
275	pub fn luaL_checkinteger(L: *mut lua_State, numArg: c_int) -> lua_Integer;
276	pub fn luaL_optinteger(L: *mut lua_State, nArg: c_int, def: lua_Integer) -> lua_Integer;
277
278	pub fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char);
279	pub fn luaL_checktype(L:*mut lua_State, narg: c_int, t: c_int);
280	pub fn luaL_checkany(L: *mut lua_State, narg: c_int);
281
282	pub fn luaL_newmetatable(L: *mut lua_State, tname: *const c_char) -> c_int;
283	pub fn luaL_checkudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void;
284
285	pub fn luaL_where(L: *mut lua_State, lvl: c_int);
286	pub fn luaL_error(L: *mut lua_State, fmt: *const c_char, ...) -> c_int;
287
288	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.
289
290	pub fn luaL_ref(L: *mut lua_State, t: c_int) -> c_int; // TODO: Expose this safely.
291	pub fn luaL_unref(L: *mut lua_State, t: c_int, r#ref: c_int); // TODO: Expose this safely.
292
293	pub fn luaL_loadfile(L: *mut lua_State, filename: *const c_char) -> c_int;
294	pub fn luaL_loadbuffer(L: *mut lua_State, buff: *const c_char, sz: usize, name: *const c_char) -> c_int;
295	pub fn luaL_loadstring(L: *mut lua_State, s: *const c_char) -> c_int;
296
297	pub safe fn luaL_newstate() -> *mut lua_State; // TODO: Expose this safely.
298
299	pub fn luaL_gsub(L: *mut lua_State, s: *const c_char, p: *const c_char, r: *const c_char) -> *const c_char;
300
301	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.
302
303	pub fn luaL_fileresult(L: *mut lua_State, stat: c_int, fname: *const c_char) -> c_int; // TODO: Expose this safely.
304	pub fn luaL_execresult(L: *mut lua_State, stat: c_int) -> c_int; // TODO: Expose this safely.
305	pub fn luaL_loadfilex(L: *mut lua_State, filename: *const c_char, mode: *const c_char) -> c_int; // TODO: Expose this safely.
306	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.
307	pub fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const c_char, level: c_int); // TODO: Expose this safely.
308	pub fn luaL_setfuncs(L: *mut lua_State, l: *const luaL_Reg, nup: c_int); // TODO: Expose this safely.
309	pub fn luaL_pushmodule(L: *mut lua_State, modname: *const c_char, sizehint: c_int); // TODO: Expose this safely.
310	pub fn luaL_testudata(L: *mut lua_State, ud: c_int, tname: *const c_char) -> *mut c_void;
311	pub fn luaL_setmetatable(L: *mut lua_State, tname: *const c_char); // TODO: Expose this safely.
312
313	pub fn luaL_buffinit(L: *mut lua_State, B: *mut luaL_Buffer);
314	pub fn luaL_prepbuffer(B: *mut luaL_Buffer) -> *mut c_char;
315	pub fn luaL_addlstring(B: *mut luaL_Buffer, s: *const c_char, l: usize);
316	pub fn luaL_addstring(B: *mut luaL_Buffer, s: *const c_char);
317	pub fn luaL_addvalue(B: *mut luaL_Buffer);
318	pub fn luaL_pushresult(B: *mut luaL_Buffer);
319
320	pub fn luaL_openlibs(L: *mut lua_State); // TODO: Expose this safely?
321
322	pub fn luaopen_base(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
323	pub fn luaopen_math(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
324	pub fn luaopen_string(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
325	pub fn luaopen_table(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
326	pub fn luaopen_io(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
327	pub fn luaopen_os(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
328	pub fn luaopen_package(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
329	pub fn luaopen_debug(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
330	pub fn luaopen_bit(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
331	pub fn luaopen_jit(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
332	pub fn luaopen_ffi(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
333	pub fn luaopen_string_buffer(L: *mut lua_State) -> c_int; // TODO: Expose this safely?
334
335	pub fn luaJIT_setmode(L: *mut lua_State, idx: c_int, mode: c_int) -> c_int; // TODO: Expose this safely.
336	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.
337	pub fn luaJIT_profile_stop(L: *mut lua_State); // TODO: Expose this safely.
338	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.
339}
340
341// --- Macros ---
342#[inline(always)]
343pub const fn lua_upvalueindex(i: c_int) -> c_int {
344	LUA_GLOBALSINDEX - i as c_int
345}
346
347#[inline(always)]
348pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
349	unsafe { lua_settop(L, -(n)-1); }
350}
351
352#[inline(always)]
353pub unsafe fn lua_newtable(L: *mut lua_State) {
354	unsafe { lua_createtable(L, 0, 0); }
355}
356
357#[inline(always)]
358pub unsafe fn lua_register(L: *mut lua_State, n: *const c_char, f: lua_CFunction) {
359	unsafe { lua_pushcfunction(L, f); lua_setglobal(L, n); }
360}
361
362#[inline(always)]
363pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
364	unsafe { lua_pushcclosure(L, f, 0); }
365}
366
367#[inline(always)]
368pub unsafe fn lua_strlen(L: *mut lua_State, i: c_int) -> usize {
369	unsafe { lua_objlen(L, i) }
370}
371
372#[inline(always)]
373pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
374	unsafe { c_int::from(lua_type(L, n) == LUA_TFUNCTION) }
375}
376
377#[inline(always)]
378pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
379	unsafe { c_int::from(lua_type(L, n) == LUA_TTABLE) }
380}
381
382#[inline(always)]
383pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
384	unsafe { c_int::from(lua_type(L, n) == LUA_TLIGHTUSERDATA) }
385}
386
387#[inline(always)]
388pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
389	unsafe { c_int::from(lua_type(L, n) == LUA_TNIL) }
390}
391
392#[inline(always)]
393pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
394	unsafe { c_int::from(lua_type(L, n) == LUA_TBOOLEAN) }
395}
396
397#[inline(always)]
398pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
399	unsafe { c_int::from(lua_type(L, n) == LUA_TTHREAD) }
400}
401
402#[inline(always)]
403pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
404	unsafe { c_int::from(lua_type(L, n) == LUA_TNONE) }
405}
406
407#[inline(always)]
408pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
409	unsafe { c_int::from(lua_type(L, n) <= 0) }
410}
411
412#[inline(always)]
413pub unsafe fn lua_pushliteral(L: *mut lua_State, s: *const c_char) {
414	unsafe { lua_pushlstring(L, s, CStr::from_ptr(s).count_bytes()); }
415}
416
417#[inline(always)]
418pub unsafe fn lua_setglobal(L: *mut lua_State, s: *const c_char) {
419	unsafe { lua_setfield(L, LUA_GLOBALSINDEX, s); }
420}
421
422#[inline(always)]
423pub unsafe fn lua_getglobal(L: *mut lua_State, s: *const c_char) {
424	unsafe { lua_getfield(L, LUA_GLOBALSINDEX, s); }
425}
426
427#[inline(always)]
428pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
429	unsafe { lua_tolstring(L, i, ptr::null_mut()) }
430}
431
432#[inline(always)]
433pub unsafe fn luaL_argcheck(L: *mut lua_State, cond: c_int, numarg: c_int, extramsg: *const c_char) {
434	if cond != 0 { unsafe { luaL_argerror(L, numarg, extramsg); } }
435}
436
437#[inline(always)]
438pub unsafe fn luaL_checkstring(L: *mut lua_State, n: c_int) -> *const c_char {
439	unsafe { luaL_checklstring(L, n, ptr::null_mut()) }
440}
441
442#[inline(always)]
443pub unsafe fn luaL_optstring(L: *mut lua_State, n: c_int, d: *const c_char) -> *const c_char {
444	unsafe { luaL_optlstring(L, n, d, ptr::null_mut()) }
445}
446
447#[inline(always)]
448#[allow(clippy::cast_possible_truncation)]
449pub unsafe fn luaL_checkint(L: *mut lua_State, n: c_int) -> c_int {
450	unsafe { luaL_checkinteger(L, n) as c_int }
451}
452
453#[inline(always)]
454#[allow(clippy::cast_possible_truncation)]
455pub unsafe fn luaL_optint(L: *mut lua_State, n: c_int, d: lua_Integer) -> c_int {
456	unsafe { luaL_optinteger(L, n, d) as c_int }
457}
458
459#[inline(always)]
460pub unsafe fn luaL_checklong(L: *mut lua_State, n: c_int) -> c_long {
461	unsafe { luaL_checkinteger(L, n) as c_long }
462}
463
464#[inline(always)]
465pub unsafe fn luaL_optlong(L: *mut lua_State, n: c_int, d: lua_Integer) -> c_long {
466	unsafe { luaL_optinteger(L, n, d) as c_long }
467}
468
469#[inline(always)]
470pub unsafe fn luaL_typename(L: *mut lua_State, i: i32) -> *const c_char {
471	unsafe { lua_typename(L, lua_type(L, i)) }
472}
473
474// TODO: Expose this safely.
475#[inline(always)]
476pub unsafe fn luaL_dofile(L: *mut lua_State, r#fn: *const c_char) -> c_int {
477	unsafe {
478		let ok = luaL_loadfile(L, r#fn);
479		if ok != 0 { return ok; }
480		lua_pcall(L, 0, LUA_MULTRET, 0)
481	}
482}
483
484// TODO: Expose this safely.
485#[inline(always)]
486pub unsafe fn luaL_dostring(L: *mut lua_State, s: *const c_char) -> c_int {
487	unsafe {
488		let ok = luaL_loadstring(L, s);
489		if ok != 0 { return ok; }
490		lua_pcall(L, 0, LUA_MULTRET, 0)
491	}
492}
493
494#[inline(always)]
495pub unsafe fn luaL_getmetatable(L: *mut lua_State, n: *const c_char) {
496	unsafe { lua_getfield(L, LUA_REGISTRYINDEX, n); }
497}
498
499#[inline(always)]
500pub unsafe fn luaL_opt<T>(L: *mut lua_State, f: impl FnOnce(*mut lua_State, c_int) -> T, n: c_int, d: T) -> T {
501	if unsafe { lua_isnoneornil(L, n) != 0 } { d } else { f(L, n) }
502}
503
504#[inline(always)]
505pub unsafe fn luaL_newlibtable(L: *mut lua_State, l: *const luaL_Reg) {
506	const unsafe fn sizeof(mut l: *const luaL_Reg) -> c_int {
507		let mut size = 0;
508
509		while unsafe { !l.read().name.is_null() } {
510			size += 1;
511			l = unsafe { l.add(1) };
512		}
513
514		size
515	}
516
517	unsafe { lua_createtable(L, 0, sizeof(l)); }
518}
519
520#[inline(always)]
521pub unsafe fn luaL_newlib(L: *mut lua_State, l: *const luaL_Reg) {
522	unsafe { luaL_newlibtable(L, l); luaL_setfuncs(L, l, 0); }
523}
524
525#[inline(always)]
526pub unsafe fn luaL_addchar(B: *mut luaL_Buffer, c: c_char) {
527	unsafe {
528		if (*B).p >= (*B).buffer.as_mut_ptr().add(LUAL_BUFFERSIZE).cast() { luaL_prepbuffer(B); }
529		(*B).p.write(c);
530		(*B).p = (*B).p.add(1);
531	}
532}
533
534#[inline(always)]
535pub const unsafe fn luaL_addsize(B: *mut luaL_Buffer, n: usize) {
536	unsafe { (*B).p = (*B).p.add(n); }
537}