Skip to main content

mlua_sys/luau/
lua.rs

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