mlua_sys/luau/
lua.rs

1//! Contains definitions from `lua.h`.
2
3use std::marker::{PhantomData, PhantomPinned};
4use std::os::raw::{c_char, c_double, c_float, c_int, c_uint, c_void};
5use std::{mem, ptr};
6
7// Option for multiple returns in 'lua_pcall' and 'lua_call'
8pub const LUA_MULTRET: c_int = -1;
9
10// Max number of Lua stack slots
11const LUAI_MAXCSTACK: c_int = 1000000;
12
13// Number of valid Lua userdata tags
14const LUA_UTAG_LIMIT: c_int = 128;
15
16// Number of valid Lua lightuserdata tags
17const LUA_LUTAG_LIMIT: c_int = 128;
18
19//
20// Pseudo-indices
21//
22pub const LUA_REGISTRYINDEX: c_int = -LUAI_MAXCSTACK - 2000;
23pub const LUA_ENVIRONINDEX: c_int = -LUAI_MAXCSTACK - 2001;
24pub const LUA_GLOBALSINDEX: c_int = -LUAI_MAXCSTACK - 2002;
25
26pub const fn lua_upvalueindex(i: c_int) -> c_int {
27    LUA_GLOBALSINDEX - i
28}
29
30//
31// Thread status
32//
33pub 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/// A raw Lua state associated with a thread.
41#[repr(C)]
42pub struct lua_State {
43    _data: [u8; 0],
44    _marker: PhantomData<(*mut u8, PhantomPinned)>,
45}
46
47//
48// Basic types
49//
50pub const LUA_TNONE: c_int = -1;
51
52pub const LUA_TNIL: c_int = 0;
53pub const LUA_TBOOLEAN: c_int = 1;
54
55pub const LUA_TLIGHTUSERDATA: c_int = 2;
56pub const LUA_TNUMBER: c_int = 3;
57pub const LUA_TVECTOR: c_int = 4;
58
59pub const LUA_TSTRING: c_int = 5;
60pub const LUA_TTABLE: c_int = 6;
61pub const LUA_TFUNCTION: c_int = 7;
62pub const LUA_TUSERDATA: c_int = 8;
63pub const LUA_TTHREAD: c_int = 9;
64pub const LUA_TBUFFER: c_int = 10;
65
66/// Guaranteed number of Lua stack slots available to a C function.
67pub const LUA_MINSTACK: c_int = 20;
68
69/// A Lua number, usually equivalent to `f64`.
70pub type lua_Number = c_double;
71
72/// A Lua integer, equivalent to `i32`.
73pub type lua_Integer = c_int;
74
75/// A Lua unsigned integer, equivalent to `u32`.
76pub type lua_Unsigned = c_uint;
77
78/// Type for native C functions that can be passed to Lua.
79pub type lua_CFunction = unsafe extern "C-unwind" fn(L: *mut lua_State) -> c_int;
80pub type lua_Continuation = unsafe extern "C-unwind" fn(L: *mut lua_State, status: c_int) -> c_int;
81
82/// Type for userdata destructor functions.
83pub type lua_Udestructor = unsafe extern "C-unwind" fn(*mut c_void);
84pub type lua_Destructor = unsafe extern "C-unwind" fn(L: *mut lua_State, *mut c_void);
85
86/// Type for memory-allocation functions.
87pub type lua_Alloc =
88    unsafe extern "C-unwind" fn(ud: *mut c_void, ptr: *mut c_void, osize: usize, nsize: usize) -> *mut c_void;
89
90/// Returns Luau release version (eg. `0.xxx`).
91pub const fn luau_version() -> Option<&'static str> {
92    option_env!("LUAU_VERSION")
93}
94
95extern "C-unwind" {
96    //
97    // State manipulation
98    //
99    pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
100    pub fn lua_close(L: *mut lua_State);
101    pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
102    pub fn lua_mainthread(L: *mut lua_State) -> *mut lua_State;
103    pub fn lua_resetthread(L: *mut lua_State);
104    pub fn lua_isthreadreset(L: *mut lua_State) -> c_int;
105
106    //
107    // Basic stack manipulation
108    //
109    pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
110    pub fn lua_gettop(L: *mut lua_State) -> c_int;
111    pub fn lua_settop(L: *mut lua_State, idx: c_int);
112    pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
113    pub fn lua_remove(L: *mut lua_State, idx: c_int);
114    pub fn lua_insert(L: *mut lua_State, idx: c_int);
115    pub fn lua_replace(L: *mut lua_State, idx: c_int);
116    pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
117    pub fn lua_rawcheckstack(L: *mut lua_State, sz: c_int);
118
119    pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
120    pub fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int);
121
122    //
123    // Access functions (stack -> C)
124    //
125    pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
126    pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
127    pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
128    pub fn lua_isLfunction(L: *mut lua_State, idx: c_int) -> c_int;
129    pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
130    pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
131    pub fn lua_typename(L: *mut lua_State, tp: c_int) -> *const c_char;
132
133    pub fn lua_equal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
134    pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
135    pub fn lua_lessthan(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
136
137    pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
138    #[link_name = "lua_tointegerx"]
139    pub fn lua_tointegerx_(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
140    pub fn lua_tounsignedx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Unsigned;
141    pub fn lua_tovector(L: *mut lua_State, idx: c_int) -> *const c_float;
142    pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
143    pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
144    pub fn lua_tostringatom(L: *mut lua_State, idx: c_int, atom: *mut c_int) -> *const c_char;
145    pub fn lua_namecallatom(L: *mut lua_State, atom: *mut c_int) -> *const c_char;
146    pub fn lua_objlen(L: *mut lua_State, idx: c_int) -> usize;
147    pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
148    pub fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
149    pub fn lua_tolightuserdatatagged(L: *mut lua_State, idx: c_int, tag: c_int) -> *mut c_void;
150    pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
151    pub fn lua_touserdatatagged(L: *mut lua_State, idx: c_int, tag: c_int) -> *mut c_void;
152    pub fn lua_userdatatag(L: *mut lua_State, idx: c_int) -> c_int;
153    pub fn lua_lightuserdatatag(L: *mut lua_State, idx: c_int) -> c_int;
154    pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
155    pub fn lua_tobuffer(L: *mut lua_State, idx: c_int, len: *mut usize) -> *mut c_void;
156    pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
157
158    //
159    // Push functions (C -> stack)
160    //
161    pub fn lua_pushnil(L: *mut lua_State);
162    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
163    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
164    pub fn lua_pushunsigned(L: *mut lua_State, n: lua_Unsigned);
165    #[cfg(not(feature = "luau-vector4"))]
166    pub fn lua_pushvector(L: *mut lua_State, x: c_float, y: c_float, z: c_float);
167    #[cfg(feature = "luau-vector4")]
168    pub fn lua_pushvector(L: *mut lua_State, x: c_float, y: c_float, z: c_float, w: c_float);
169    #[link_name = "lua_pushlstring"]
170    pub fn lua_pushlstring_(L: *mut lua_State, s: *const c_char, l: usize);
171    #[link_name = "lua_pushstring"]
172    pub fn lua_pushstring_(L: *mut lua_State, s: *const c_char);
173    // lua_pushvfstring
174    #[link_name = "lua_pushfstringL"]
175    pub fn lua_pushfstring(L: *mut lua_State, fmt: *const c_char, ...) -> *const c_char;
176    pub fn lua_pushcclosurek(
177        L: *mut lua_State,
178        f: lua_CFunction,
179        debugname: *const c_char,
180        nup: c_int,
181        cont: Option<lua_Continuation>,
182    );
183    pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
184    pub fn lua_pushthread(L: *mut lua_State) -> c_int;
185
186    pub fn lua_pushlightuserdatatagged(L: *mut lua_State, p: *mut c_void, tag: c_int);
187    pub fn lua_newuserdatatagged(L: *mut lua_State, sz: usize, tag: c_int) -> *mut c_void;
188    pub fn lua_newuserdatataggedwithmetatable(L: *mut lua_State, sz: usize, tag: c_int) -> *mut c_void;
189    pub fn lua_newuserdatadtor(L: *mut lua_State, sz: usize, dtor: lua_Udestructor) -> *mut c_void;
190
191    pub fn lua_newbuffer(L: *mut lua_State, sz: usize) -> *mut c_void;
192
193    //
194    // Get functions (Lua -> stack)
195    //
196    pub fn lua_gettable(L: *mut lua_State, idx: c_int) -> c_int;
197    pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int;
198    pub fn lua_rawgetfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> c_int;
199    pub fn lua_rawget(L: *mut lua_State, idx: c_int) -> c_int;
200    #[link_name = "lua_rawgeti"]
201    pub fn lua_rawgeti_(L: *mut lua_State, idx: c_int, n: c_int) -> c_int;
202    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
203
204    pub fn lua_setreadonly(L: *mut lua_State, idx: c_int, enabled: c_int);
205    pub fn lua_getreadonly(L: *mut lua_State, idx: c_int) -> c_int;
206    pub fn lua_setsafeenv(L: *mut lua_State, idx: c_int, enabled: c_int);
207
208    pub fn lua_getmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
209    pub fn lua_getfenv(L: *mut lua_State, idx: c_int);
210
211    //
212    // Set functions (stack -> Lua)
213    //
214    pub fn lua_settable(L: *mut lua_State, idx: c_int);
215    pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
216    pub fn lua_rawset(L: *mut lua_State, idx: c_int);
217    #[link_name = "lua_rawseti"]
218    pub fn lua_rawseti_(L: *mut lua_State, idx: c_int, n: c_int);
219    pub fn lua_setmetatable(L: *mut lua_State, objindex: c_int) -> c_int;
220    pub fn lua_setfenv(L: *mut lua_State, idx: c_int) -> c_int;
221
222    //
223    // `load' and `call' functions (load and run Luau bytecode)
224    //
225    pub fn luau_load(
226        L: *mut lua_State,
227        chunkname: *const c_char,
228        data: *const c_char,
229        size: usize,
230        env: c_int,
231    ) -> c_int;
232    pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
233    pub fn lua_pcall(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
234
235    //
236    // Coroutine functions
237    //
238    pub fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int;
239    pub fn lua_break(L: *mut lua_State) -> c_int;
240    #[link_name = "lua_resume"]
241    pub fn lua_resume_(L: *mut lua_State, from: *mut lua_State, narg: c_int) -> c_int;
242    pub fn lua_resumeerror(L: *mut lua_State, from: *mut lua_State) -> c_int;
243    pub fn lua_status(L: *mut lua_State) -> c_int;
244    pub fn lua_isyieldable(L: *mut lua_State) -> c_int;
245    pub fn lua_getthreaddata(L: *mut lua_State) -> *mut c_void;
246    pub fn lua_setthreaddata(L: *mut lua_State, data: *mut c_void);
247}
248
249//
250// Garbage-collection function and options
251//
252pub const LUA_GCSTOP: c_int = 0;
253pub const LUA_GCRESTART: c_int = 1;
254pub const LUA_GCCOLLECT: c_int = 2;
255pub const LUA_GCCOUNT: c_int = 3;
256pub const LUA_GCCOUNTB: c_int = 4;
257pub const LUA_GCISRUNNING: c_int = 5;
258pub const LUA_GCSTEP: c_int = 6;
259pub const LUA_GCSETGOAL: c_int = 7;
260pub const LUA_GCSETSTEPMUL: c_int = 8;
261pub const LUA_GCSETSTEPSIZE: c_int = 9;
262
263extern "C-unwind" {
264    pub fn lua_gc(L: *mut lua_State, what: c_int, data: c_int) -> c_int;
265}
266
267//
268// Memory statistics
269//
270extern "C-unwind" {
271    pub fn lua_setmemcat(L: *mut lua_State, category: c_int);
272    pub fn lua_totalbytes(L: *mut lua_State, category: c_int) -> usize;
273}
274
275//
276// Miscellaneous functions
277//
278extern "C-unwind" {
279    pub fn lua_error(L: *mut lua_State) -> !;
280    pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
281    pub fn lua_rawiter(L: *mut lua_State, idx: c_int, iter: c_int) -> c_int;
282    pub fn lua_concat(L: *mut lua_State, n: c_int);
283    // TODO: lua_encodepointer
284    pub fn lua_clock() -> c_double;
285    pub fn lua_setuserdatatag(L: *mut lua_State, idx: c_int, tag: c_int);
286    pub fn lua_setuserdatadtor(L: *mut lua_State, tag: c_int, dtor: Option<lua_Destructor>);
287    pub fn lua_getuserdatadtor(L: *mut lua_State, tag: c_int) -> Option<lua_Destructor>;
288    pub fn lua_setuserdatametatable(L: *mut lua_State, tag: c_int, idx: c_int);
289    pub fn lua_getuserdatametatable(L: *mut lua_State, tag: c_int);
290    pub fn lua_setlightuserdataname(L: *mut lua_State, tag: c_int, name: *const c_char);
291    pub fn lua_getlightuserdataname(L: *mut lua_State, tag: c_int) -> *const c_char;
292    pub fn lua_clonefunction(L: *mut lua_State, idx: c_int);
293    pub fn lua_cleartable(L: *mut lua_State, idx: c_int);
294    pub fn lua_getallocf(L: *mut lua_State, ud: *mut *mut c_void) -> lua_Alloc;
295}
296
297//
298// Reference system, can be used to pin objects
299//
300pub const LUA_NOREF: c_int = -1;
301pub const LUA_REFNIL: c_int = 0;
302
303extern "C-unwind" {
304    pub fn lua_ref(L: *mut lua_State, idx: c_int) -> c_int;
305    pub fn lua_unref(L: *mut lua_State, r#ref: c_int);
306}
307
308//
309// Some useful macros (implemented as Rust functions)
310//
311
312#[inline(always)]
313pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
314    lua_tonumberx(L, i, ptr::null_mut())
315}
316
317#[inline(always)]
318pub unsafe fn lua_tointeger_(L: *mut lua_State, i: c_int) -> lua_Integer {
319    lua_tointegerx_(L, i, ptr::null_mut())
320}
321
322#[inline(always)]
323pub unsafe fn lua_tounsigned(L: *mut lua_State, i: c_int) -> lua_Unsigned {
324    lua_tounsignedx(L, i, ptr::null_mut())
325}
326
327#[inline(always)]
328pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
329    lua_settop(L, -n - 1)
330}
331
332#[inline(always)]
333pub unsafe fn lua_newtable(L: *mut lua_State) {
334    lua_createtable(L, 0, 0)
335}
336
337#[inline(always)]
338pub unsafe fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void {
339    lua_newuserdatatagged(L, sz, 0)
340}
341
342#[inline(always)]
343pub unsafe fn lua_newuserdata_t<T>(L: *mut lua_State) -> *mut T {
344    unsafe extern "C-unwind" fn destructor<T>(ud: *mut c_void) {
345        ptr::drop_in_place(ud as *mut T);
346    }
347
348    lua_newuserdatadtor(L, mem::size_of::<T>(), destructor::<T>) as *mut T
349}
350
351// TODO: lua_strlen
352
353#[inline(always)]
354pub unsafe fn lua_isfunction(L: *mut lua_State, n: c_int) -> c_int {
355    (lua_type(L, n) == LUA_TFUNCTION) as c_int
356}
357
358#[inline(always)]
359pub unsafe fn lua_istable(L: *mut lua_State, n: c_int) -> c_int {
360    (lua_type(L, n) == LUA_TTABLE) as c_int
361}
362
363#[inline(always)]
364pub unsafe fn lua_islightuserdata(L: *mut lua_State, n: c_int) -> c_int {
365    (lua_type(L, n) == LUA_TLIGHTUSERDATA) as c_int
366}
367
368#[inline(always)]
369pub unsafe fn lua_isnil(L: *mut lua_State, n: c_int) -> c_int {
370    (lua_type(L, n) == LUA_TNIL) as c_int
371}
372
373#[inline(always)]
374pub unsafe fn lua_isboolean(L: *mut lua_State, n: c_int) -> c_int {
375    (lua_type(L, n) == LUA_TBOOLEAN) as c_int
376}
377
378#[inline(always)]
379pub unsafe fn lua_isvector(L: *mut lua_State, n: c_int) -> c_int {
380    (lua_type(L, n) == LUA_TVECTOR) as c_int
381}
382
383#[inline(always)]
384pub unsafe fn lua_isthread(L: *mut lua_State, n: c_int) -> c_int {
385    (lua_type(L, n) == LUA_TTHREAD) as c_int
386}
387
388#[inline(always)]
389pub unsafe fn lua_isbuffer(L: *mut lua_State, n: c_int) -> c_int {
390    (lua_type(L, n) == LUA_TBUFFER) as c_int
391}
392
393#[inline(always)]
394pub unsafe fn lua_isnone(L: *mut lua_State, n: c_int) -> c_int {
395    (lua_type(L, n) == LUA_TNONE) as c_int
396}
397
398#[inline(always)]
399pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
400    (lua_type(L, n) <= LUA_TNIL) as c_int
401}
402
403#[inline(always)]
404pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) {
405    use std::ffi::CString;
406    let c_str = CString::new(s).unwrap();
407    lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
408}
409
410#[inline(always)]
411pub unsafe fn lua_pushcfunction(L: *mut lua_State, f: lua_CFunction) {
412    lua_pushcclosurek(L, f, ptr::null(), 0, None)
413}
414
415#[inline(always)]
416pub unsafe fn lua_pushcfunctiond(L: *mut lua_State, f: lua_CFunction, debugname: *const c_char) {
417    lua_pushcclosurek(L, f, debugname, 0, None)
418}
419
420#[inline(always)]
421pub unsafe fn lua_pushcclosure(L: *mut lua_State, f: lua_CFunction, nup: c_int) {
422    lua_pushcclosurek(L, f, ptr::null(), nup, None)
423}
424
425#[inline(always)]
426pub unsafe fn lua_pushcclosured(L: *mut lua_State, f: lua_CFunction, debugname: *const c_char, nup: c_int) {
427    lua_pushcclosurek(L, f, debugname, nup, None)
428}
429
430#[inline(always)]
431pub unsafe fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void) {
432    lua_pushlightuserdatatagged(L, p, 0)
433}
434
435#[inline(always)]
436pub unsafe fn lua_setglobal(L: *mut lua_State, var: *const c_char) {
437    lua_setfield(L, LUA_GLOBALSINDEX, var)
438}
439
440#[inline(always)]
441pub unsafe fn lua_getglobal(L: *mut lua_State, var: *const c_char) -> c_int {
442    lua_getfield(L, LUA_GLOBALSINDEX, var)
443}
444
445#[inline(always)]
446pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
447    lua_tolstring(L, i, ptr::null_mut())
448}
449
450//
451// Debug API
452//
453
454// Maximum size for the description of the source of a function in debug information.
455const LUA_IDSIZE: usize = 256;
456
457/// Type for functions to be called on debug events.
458pub type lua_Hook = unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug);
459
460pub type lua_Coverage = unsafe extern "C-unwind" fn(
461    context: *mut c_void,
462    function: *const c_char,
463    linedefined: c_int,
464    depth: c_int,
465    hits: *const c_int,
466    size: usize,
467);
468
469extern "C-unwind" {
470    pub fn lua_stackdepth(L: *mut lua_State) -> c_int;
471    pub fn lua_getinfo(L: *mut lua_State, level: c_int, what: *const c_char, ar: *mut lua_Debug) -> c_int;
472    pub fn lua_getargument(L: *mut lua_State, level: c_int, n: c_int) -> c_int;
473    pub fn lua_getlocal(L: *mut lua_State, level: c_int, n: c_int) -> *const c_char;
474    pub fn lua_setlocal(L: *mut lua_State, level: c_int, n: c_int) -> *const c_char;
475    pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
476    pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
477
478    pub fn lua_singlestep(L: *mut lua_State, enabled: c_int);
479    pub fn lua_breakpoint(L: *mut lua_State, funcindex: c_int, line: c_int, enabled: c_int) -> c_int;
480
481    pub fn lua_getcoverage(L: *mut lua_State, funcindex: c_int, context: *mut c_void, callback: lua_Coverage);
482
483    pub fn lua_debugtrace(L: *mut lua_State) -> *const c_char;
484}
485
486#[repr(C)]
487pub struct lua_Debug {
488    pub name: *const c_char,
489    pub what: *const c_char,
490    pub source: *const c_char,
491    pub short_src: *const c_char,
492    pub linedefined: c_int,
493    pub currentline: c_int,
494    pub nupvals: u8,
495    pub nparams: u8,
496    pub isvararg: c_char,
497    pub userdata: *mut c_void,
498    pub ssbuf: [c_char; LUA_IDSIZE],
499}
500
501//
502// Callbacks that can be used to reconfigure behavior of the VM dynamically.
503// These are shared between all coroutines.
504//
505
506#[repr(C)]
507#[non_exhaustive]
508pub struct lua_Callbacks {
509    /// arbitrary userdata pointer that is never overwritten by Luau
510    pub userdata: *mut c_void,
511
512    /// gets called at safepoints (loop back edges, call/ret, gc) if set
513    pub interrupt: Option<unsafe extern "C-unwind" fn(L: *mut lua_State, gc: c_int)>,
514    /// gets called when an unprotected error is raised (if longjmp is used)
515    pub panic: Option<unsafe extern "C-unwind" fn(L: *mut lua_State, errcode: c_int)>,
516
517    /// gets called when L is created (LP == parent) or destroyed (LP == NULL)
518    pub userthread: Option<unsafe extern "C-unwind" fn(LP: *mut lua_State, L: *mut lua_State)>,
519    /// gets called when a string is created; returned atom can be retrieved via tostringatom
520    pub useratom: Option<unsafe extern "C-unwind" fn(s: *const c_char, l: usize) -> i16>,
521
522    /// gets called when BREAK instruction is encountered
523    pub debugbreak: Option<unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug)>,
524    /// gets called after each instruction in single step mode
525    pub debugstep: Option<unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug)>,
526    /// gets called when thread execution is interrupted by break in another thread
527    pub debuginterrupt: Option<unsafe extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug)>,
528    /// gets called when protected call results in an error
529    pub debugprotectederror: Option<unsafe extern "C-unwind" fn(L: *mut lua_State)>,
530
531    /// gets called when memory is allocated
532    pub onallocate: Option<unsafe extern "C-unwind" fn(L: *mut lua_State, osize: usize, nsize: usize)>,
533}
534
535extern "C" {
536    pub fn lua_callbacks(L: *mut lua_State) -> *mut lua_Callbacks;
537}
538
539// Functions from customization lib
540extern "C" {
541    pub fn luau_setfflag(name: *const c_char, value: c_int) -> c_int;
542    pub fn lua_getmetatablepointer(L: *mut lua_State, idx: c_int) -> *const c_void;
543}