lu_sys/
lua.rs

1#![allow(clippy::missing_safety_doc)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4
5use std::marker::{PhantomData, PhantomPinned};
6use std::{
7    ffi::{c_char, c_double, c_float, c_int, c_uchar, c_uint, c_void},
8    ptr::null_mut,
9};
10
11use super::*;
12
13/// Indicates that a call should return all values to the caller.
14///
15/// This is used with [`lua_call`] and [`lua_pcall`] to specify that all returned
16/// values should be pushed onto the stack.
17pub const LUA_MULTRET: c_int = -1;
18
19/// The pseudo-index for the registry.
20///
21/// The registry is a pre-defined table that is only accessible from the C API.
22/// Integer keys in this table should not be used, as they are reserved for
23/// Luau's ref system.
24///
25/// Care should be taken that keys do not conflict. A great way to avoid this is
26/// to use a light userdata key pointing to a static variable, as this will
27/// always be unique.
28pub const LUA_REGISTRYINDEX: c_int = -LUAI_MAXCSTACK - 2000;
29
30/// The pseudo-index for the environment table of the running function.
31pub const LUA_ENVIRONINDEX: c_int = -LUAI_MAXCSTACK - 2001;
32
33/// The pseudo-index for the global environment table.
34pub const LUA_GLOBALSINDEX: c_int = -LUAI_MAXCSTACK - 2002;
35
36/// Produces the pseudo-indicies for upvalues of C functions.
37///
38/// A C function that captures upvalues can get a pseudo-index for each upvalue
39/// using this function. The first upvalue is at `lua_upvalueindex(1)`, and the
40/// n-th upvalue is at `lua_upvalueindex(n)`.
41///
42/// This function will produce an acceptable but invalid pseudo-index for any
43/// input.
44pub fn lua_upvalueindex(i: c_int) -> c_int {
45    LUA_GLOBALSINDEX - i
46}
47
48/// Checks if the given index is a pseudo-index.
49///
50/// This function checks if the given index is a pseudo-index, which includes
51/// the registry, environment, and global environment indices.
52pub fn lua_ispseudo(i: c_int) -> bool {
53    i <= LUA_REGISTRYINDEX
54}
55
56/// The status codes returned by the Luau VM.
57///
58/// These codes indicate the result of a function call or operation in the Luau
59/// VM. As an example [`lua_pcall`] might return [`LUA_OK`] if the call was
60/// successful, or [`LUA_YIELD`] if the call yielded execution, or
61/// [`LUA_ERRRUN`] if there was a runtime error.
62#[repr(C)]
63#[derive(Clone, Copy, PartialEq, Eq)]
64pub enum lua_Status {
65    /// The call was successful and the function returned normally.
66    LUA_OK = 0,
67
68    /// The call yielded execution using either [`lua_yield`] or `coroutine.yield`.
69    LUA_YIELD,
70
71    /// The call encountered a runtime error.
72    LUA_ERRRUN,
73
74    /// This variant exists only for backwards compatibility. It is not used.
75    LUA_ERRSYNTAX,
76
77    /// The call encountered a memory allocation error.
78    LUA_ERRMEM,
79
80    /// The call encountered an error while running the error handler.
81    LUA_ERRERR,
82
83    /// The call was interrupted by a break.
84    LUA_BREAK,
85}
86
87pub use lua_Status::*;
88
89/// The status codes of a coroutine.
90///
91/// This is only returned by [`lua_costatus`].
92#[repr(C)]
93#[derive(Clone, Copy, PartialEq, Eq)]
94pub enum lua_CoStatus {
95    LUA_CORUN = 0,
96    LUA_COSUS,
97    LUA_CONOR,
98    LUA_COFIN,
99    LUA_COERR,
100}
101
102pub use lua_CoStatus::*;
103
104/// Represents a Luau thread, stack, and environment. The main structure for
105/// interacting with the Luau VM.
106///
107/// This structure is opaque and should never exist, except as a pointer.
108#[repr(C)]
109pub struct lua_State {
110    _data: (),
111    _marker: PhantomData<(*mut u8, PhantomPinned)>,
112}
113
114/// The type of a C function that can be called from Luau.
115///
116/// This function takes a pointer to a [`lua_State`] and returns the number of
117/// results it pushes onto the stack. It can also yield execution, in which case
118/// it should return the result from [`lua_yield`].
119pub type lua_CFunction = extern "C-unwind" fn(L: *mut lua_State) -> c_int;
120
121/// The type of a continuation function that can be called from Luau.
122///
123/// When a C function yields, either by yielding directly or by calling a
124/// function that yields, it's continuation will be run when the coroutine is
125/// resumed.
126///
127/// The following Luau and C code segments are equivalent:
128///
129/// ```lua
130/// local function foo()
131///     print("foo")
132///     coroutine.yield()
133///     print("bar")
134/// end
135/// ```
136///
137/// ```c
138/// int foo(lua_State* L) {
139///     printf("foo\n");
140///     return lua_yield(L, 0);
141/// }
142///
143/// int foo_cont(lua_State* L, int status) {
144///     printf("bar\n");
145///     return 0;
146/// }
147/// ```
148pub type lua_Continuation = extern "C-unwind" fn(L: *mut lua_State, status: lua_Status) -> c_int;
149
150/// The type of the memory allocator function used by Luau's VM.
151///
152/// * `ud` is the same userdata pointer as was passed to [`lua_newstate`].
153/// * `ptr` is the pointer to the memory block to be reallocated, or null if
154///   osize is zero.
155/// * `osize` is the size of the memory block pointed to by `ptr`.
156/// * `nsize` is the new size of the memory block to be allocated.
157///
158/// If `osize` is zero, `ptr` will be null and the function should allocate a
159/// new memory block of size `nsize`.
160///
161/// If `nsize` is zero, the function should free the memory block pointed to be
162/// `ptr`.
163///
164/// If `osize` and `nsize` are both non-zero, the function should reallocate the
165/// memory block pointed to by `ptr` to the new size `nsize`.
166///
167/// # Example
168///
169/// ```
170/// extern "C-unwind" fn alloc(
171///     ud: *mut c_void,
172///     ptr: *mut c_void,
173///     osize: usize,
174///     nsize: usize
175/// ) -> *mut c_void {
176///     if nsize == 0 {
177///         unsafe { libc::free(ptr) };
178///         std::ptr::null_mut()    
179///     } else {
180///         unsafe { libc::realloc(ptr, nsize) }
181///     }
182/// }
183/// ```
184pub type lua_Alloc = extern "C-unwind" fn(
185    ud: *mut c_void,
186    ptr: *mut c_void,
187    osize: usize,
188    nsize: usize,
189) -> *mut c_void;
190
191/// The different types of Luau values.
192///
193/// This is returned by [`lua_type`] and is taken as an argument by
194/// [`lua_typename`] and [`luaL_checktype`].
195#[repr(C)]
196#[derive(Clone, Copy, PartialEq, Eq)]
197pub enum lua_Type {
198    LUA_TNONE = -1,
199    LUA_TNIL = 0,
200    LUA_TBOOLEAN = 1,
201
202    LUA_TLIGHTUSERDATA,
203    LUA_TNUMBER,
204    LUA_TVECTOR,
205
206    LUA_TSTRING,
207
208    LUA_TTABLE,
209    LUA_TFUNCTION,
210    LUA_TUSERDATA,
211    LUA_TTHREAD,
212    LUA_TBUFFER,
213}
214
215pub use lua_Type::*;
216
217/// The type of Luau numbers.
218pub type lua_Number = c_double;
219
220/// The type of Luau integers.
221pub type lua_Integer = c_int;
222
223/// The type of Luau unsigned integers.
224pub type lua_Unsigned = c_uint;
225
226unsafe extern "C-unwind" {
227    /// Creates a new [`lua_State`].
228    ///
229    /// This function takes an allocator function and a userdata pointer.
230    pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
231
232    /// Closes the passed [`lua_State`].
233    ///
234    /// This function will free all memory associated with the state and
235    /// invalidate the pointer.
236    pub fn lua_close(L: *mut lua_State);
237
238    /// Creates a new thread.
239    ///
240    /// This function creates a new thread, pushes it onto the stack, and
241    /// returns the pointer to the new thread. Threads, like all other Luau
242    /// objects, are subject to garbage collection.
243    pub fn lua_newthread(L: *mut lua_State) -> *mut lua_State;
244
245    /// Returns the main thread of the given state.
246    ///
247    /// This function returns the main thread of the given state, or the pointer
248    /// initially returned by [`lua_newstate`].
249    pub fn lua_mainthread(L: *mut lua_State) -> *mut lua_State;
250
251    /// TODO: Document this function.
252    pub fn lua_resetthread(L: *mut lua_State);
253
254    /// TODO: Document this function.
255    pub fn lua_isthreadreset(L: *mut lua_State) -> c_int;
256
257    /// Transforms a stack index into an index that is not relative to the top
258    /// of the stack.
259    ///
260    /// Passing psuedo-indices or positive indices will return the same
261    /// index, while negative indices will be transformed to a positive index.
262    pub fn lua_absindex(L: *mut lua_State, idx: c_int) -> c_int;
263
264    /// Returns the number of items on the stack.
265    ///
266    /// This function returns the top index of the stack, or the number of items
267    /// on the stack. A return of `0` indicates that the stack is empty.
268    pub fn lua_gettop(L: *mut lua_State) -> c_int;
269
270    /// Sets the top of the stack to the given index.
271    ///
272    /// Accepts any index, including `0`. If the index is greater than the
273    /// current top, the stack will be extended with `nil` values.
274    pub fn lua_settop(L: *mut lua_State, idx: c_int);
275
276    /// Pushes a value onto the stack.
277    ///
278    /// This function copies the value at the given index, and pushes the copy
279    /// onto the stack.
280    pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);
281
282    /// Removes the value at the given index from the stack.
283    ///
284    /// This function removes the value at the given index from the stack,
285    /// shifting all values above it down by one.
286    pub fn lua_remove(L: *mut lua_State, idx: c_int);
287
288    /// Moves the value at the given index to the top of the stack.
289    ///
290    /// This function moves the value at the given index to the top of the
291    /// stack, shifting all values above it down by one.
292    pub fn lua_insert(L: *mut lua_State, idx: c_int);
293
294    /// Replaces the given index with the value at the top of the stack.
295    ///
296    /// This function replaces the value at the given index with the value at
297    /// the top of the stack, and then pops the top value off the stack.
298    pub fn lua_replace(L: *mut lua_State, idx: c_int);
299
300    /// Attempts to make space on the stack for at least `n` more values.
301    ///
302    /// This function will attempt to make space on the stack for at least `sz`
303    /// more values. If the stack is already large enough, this function does
304    /// nothing. If the stack is not large enough, it will attempt to grow the
305    /// stack to accommodate the new values.
306    ///
307    /// If the stack cannot be grown, this function will return `0`, otherwise
308    /// it will return `1`.
309    pub fn lua_checkstack(L: *mut lua_State, sz: c_int) -> c_int;
310
311    /// TODO: Document this function.
312    pub fn lua_rawcheckstack(L: *mut lua_State, sz: c_int);
313
314    /// Moves values from the top of one stack to another stack.
315    ///
316    /// This function moves `n` values from the top of the `from` stack to the
317    /// top of the `to` stack. This can be thought of as a "slide" between the
318    /// two stacks.
319    pub fn lua_xmove(from: *mut lua_State, to: *mut lua_State, n: c_int);
320
321    /// Pushes a value from an index of one stack onto another stack.
322    ///
323    /// This function pushes a copy of the value at the given index of the
324    /// `from` stack onto the top of the `to` stack.
325    pub fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int);
326
327    /// Does the given index contain a value of type number.
328    pub fn lua_isnumber(L: *mut lua_State, idx: c_int) -> c_int;
329
330    /// Does the given index contain a value of type string.
331    pub fn lua_isstring(L: *mut lua_State, idx: c_int) -> c_int;
332
333    /// Does the given index contain a value of type function which is a C
334    /// function.
335    pub fn lua_iscfunction(L: *mut lua_State, idx: c_int) -> c_int;
336
337    /// Does the given index contain a value of type function which is a Luau
338    /// function.
339    pub fn lua_isLfunction(L: *mut lua_State, idx: c_int) -> c_int;
340
341    /// Does the given index contain a value of type userdata.
342    pub fn lua_isuserdata(L: *mut lua_State, idx: c_int) -> c_int;
343
344    /// Returns the type of the value at the given index.
345    pub fn lua_type(L: *mut lua_State, idx: c_int) -> lua_Type;
346
347    /// Returns the name of the given type.
348    pub fn lua_typename(L: *mut lua_State, tp: lua_Type) -> *const c_char;
349
350    /// Compares the values at the two indices for equality.
351    ///
352    /// This function compares the values at the two indices for equality, using
353    /// Luau's equality rules (`==`).
354    pub fn lua_equal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
355
356    /// Compares the values at the two indices for raw equality.
357    ///
358    /// This function compares the values at the two indices for raw equality,
359    /// using Luau's raw equality rules (does not hit metamethods).
360    pub fn lua_rawequal(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
361
362    /// Compares the values at the two indices for less than.
363    ///
364    /// This function compares the values at the two indices for less than,
365    /// using Luau's less than rules (`<`).
366    ///
367    /// Greater than comparison can be done by swapping the indices.
368    pub fn lua_lessthan(L: *mut lua_State, idx1: c_int, idx2: c_int) -> c_int;
369
370    /// Attempts to convert the value at the given index to a number.
371    ///
372    /// This function attempts to convert the value at the given index to a
373    /// number, returning the value if successful, or `0.0` if the conversion
374    /// failed.
375    ///
376    /// As `0.0` is a valid Luau number and cannot be used to indicate failure,
377    /// the function also allows for an optional pointer which it will set to
378    /// `0` if the conversion failed, or `1` if it succeeded.
379    ///
380    /// This function will leave numbers as is, and will transform strings which
381    /// can be parsed as numbers into numbers. This will modify the stack.
382    pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
383
384    /// Attempts to convert the value at the given index to an integer.
385    ///
386    /// This function operates similarly to [`lua_tonumberx`], but adds the step
387    /// of flooring the result to an integer. While a string may be turned into
388    /// a number on the stack, the value on the stack will not be floored to an
389    /// integer.
390    pub fn lua_tointegerx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
391
392    /// Attempts to convert the value at the given index to an unsigned integer.
393    ///
394    /// This function operates in the exact same way as [`lua_tointegerx`], but
395    /// returns a [`lua_Unsigned`] instead of a [`lua_Integer`]. If the value
396    /// is negative the output is undefined.
397    pub fn lua_tounsignedx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Unsigned;
398
399    /// Returns the value of the vector at the given index.
400    ///
401    /// If the value at the given index is not a vector, this function will
402    /// return null. Otherwise, it will return a pointer to the vector's
403    /// floats.
404    pub fn lua_tovector(L: *mut lua_State, idx: c_int) -> *const c_float;
405
406    /// Returns the truthiness of the value at the given index.
407    ///
408    /// This function returns `0` if the value at the given index is `nil` or
409    /// `false`, and `1` for all other values.
410    pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
411
412    /// Attempts to convert the value at the given index to a string and returns
413    /// its value and length.
414    ///
415    /// This function attempts to convert the value at the given index to a
416    /// string, returning a pointer to the string if successful, or null if the
417    /// conversion failed.
418    ///
419    /// If `len` is not null, it will be set to the length of the string if the
420    /// conversion was successful.
421    ///
422    /// If the value is a string, it will be returned as is. If the value is a
423    /// number, it will be converted into a string and the stack will be
424    /// modified.
425    pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
426
427    /// Returns the value of the string at the given index as well as its atom.
428    ///
429    /// If the value at the given index is not a string, this function will
430    /// return null. Otherwise, it will return a pointer to the string. Unlike
431    /// [`lua_tolstring`], this function does not do any conversions or modify
432    /// the stack.
433    ///
434    /// If `atom` is not null, it will be set to the atom of the string.
435    pub fn lua_tostringatom(L: *mut lua_State, idx: c_int, atom: *mut c_int) -> *const c_char;
436
437    /// Returns the value of the string at the given index as well as its atom
438    /// and length.
439    ///
440    /// If the value at the given index is not a string, this function will
441    /// return null. Otherwise, it will return a pointer to the string. Unlike
442    /// [`lua_tolstring`], this function does not do any conversions or modify
443    /// the stack.
444    ///
445    /// If `atom` is not null, it will be set to the atom of the string. If
446    /// `len` is not null, it will be set to the length of the string.
447    pub fn lua_tolstringatom(
448        L: *mut lua_State,
449        idx: c_int,
450        len: *mut usize,
451        atom: *mut c_int,
452    ) -> *const c_char;
453
454    /// Returns the value of the namecall method string as well as its atom.
455    ///
456    /// If not within a namecall context, this function will return null.
457    /// Otherwise, it will return a pointer to the string.
458    ///
459    /// If `atom` is not null, it will be set to the atom of the string.
460    pub fn lua_namecallatom(L: *mut lua_State, atom: *mut c_int) -> *const c_char;
461
462    /// Returns the length of the value at the given index.
463    ///
464    /// Length is defined differently for different types:
465    ///
466    /// * For strings, it returns the length of the string in bytes.
467    /// * For userdata, it returns the size of the userdata in bytes.
468    /// * For buffers, it returns the length of the buffer in bytes.
469    /// * For tables, it returns the same value that the length operator (`#`)
470    ///   would return.
471    ///
472    /// If the value is not one of these types, this function will return `0`.
473    pub fn lua_objlen(L: *mut lua_State, idx: c_int) -> c_int;
474
475    /// Returns a pointer to the C function at the given index.
476    ///
477    /// If the value at the given index is not a C function, this function will
478    /// return null. Otherwise, it will return a pointer to the C function.
479    pub fn lua_tocfunction(L: *mut lua_State, idx: c_int) -> Option<lua_CFunction>;
480
481    /// Returns the pointer of the light userdata at the given index.
482    ///
483    /// If the value at the given index is not a light userdata, this function
484    /// will return null. Otherwise, it will return the pointer that the light
485    /// userdata contains.
486    pub fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
487
488    /// Returns the pointer to the light userdata at the given index if it has the
489    /// given tag.
490    ///
491    /// If the value at the given index is not a light userdata, or if it does
492    /// not have the given tag, this function will return null. Otherwise,
493    /// it will return the pointer that the light userdata contains.
494    pub fn lua_tolightuserdatatagged(L: *mut lua_State, idx: c_int, tag: c_int) -> *mut c_void;
495
496    /// Returns the pointer to the userdata at the given index.
497    ///
498    /// If the value at the given index is not a userdata, this function will
499    /// return null. Otherwise, it will return a pointer to the userdata.
500    pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;
501
502    /// Returns the pointer to the userdata at the given index if it has the
503    /// given tag.
504    ///
505    /// If the value at the given index is not a userdata, or if it does not
506    /// have the given tag, this function will return null. Otherwise, it will
507    /// return a pointer to the userdata.
508    pub fn lua_touserdatatagged(L: *mut lua_State, idx: c_int, tag: c_int) -> *mut c_void;
509
510    /// Returns the tag of the userdata at the given index.
511    ///
512    /// If the value at the given index is not a userdata, this function will
513    /// return `-1`. Otherwise, it will return the tag of the userdata.
514    pub fn lua_userdatatag(L: *mut lua_State, idx: c_int) -> c_int;
515
516    /// Returns the tag of the light userdata at the given index.
517    ///
518    /// If the value at the given index is not a light userdata, this function
519    /// will return `-1`. Otherwise, it will return the tag of the light
520    /// userdata.
521    pub fn lua_lightuserdatatag(L: *mut lua_State, idx: c_int) -> c_int;
522
523    /// Returns the thread at the given index.
524    ///
525    /// If the value at the given index is not a thread, this function will
526    /// return null. Otherwise, it will return a pointer to the thread's
527    /// [`lua_State`].
528    pub fn lua_tothread(L: *mut lua_State, idx: c_int) -> *mut lua_State;
529
530    /// Returns the buffer at the given index.
531    ///
532    /// If the value at the given index is not a buffer, this function will
533    /// return null. Otherwise, it will return a pointer to the buffer's data.
534    ///
535    /// If `len` is not null, it will be set to the length of the buffer.
536    pub fn lua_tobuffer(L: *mut lua_State, idx: c_int, len: *mut usize) -> *mut c_void;
537
538    /// Returns the pointer of the value at the given index.
539    ///
540    /// The "pointer of the value" is defined differently for different types:
541    ///
542    /// * For userdata, it returns the pointer to the userdata.
543    /// * For light userdata, it returns the pointer that the light userdata
544    ///   contains.
545    /// * For strings, tables, functions, threads, and buffers, it returns the
546    ///   pointer to the value's data on the heap. The contents of this pointer
547    ///   are undefined.
548    ///
549    /// For all other types, this function will return null.
550    pub fn lua_topointer(L: *mut lua_State, idx: c_int) -> *const c_void;
551
552    /// Pushes a nil value onto the top of the stack.
553    pub fn lua_pushnil(L: *mut lua_State);
554
555    /// Pushes a number onto the top of the stack.
556    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
557
558    /// Pushes an integer onto the top of the stack.
559    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
560
561    /// Pushes an unsigned integer onto the top of the stack.
562    pub fn lua_pushunsigned(L: *mut lua_State, n: lua_Unsigned);
563
564    /// Pushes a vector onto the top of the stack.
565    pub fn lua_pushvector(L: *mut lua_State, x: c_float, y: c_float, z: c_float);
566
567    /// Pushes a string with a length onto the top of the stack.
568    pub fn lua_pushlstring(L: *mut lua_State, s: *const c_char, l: usize);
569
570    /// Pushes a string onto the top of the stack.
571    pub fn lua_pushstring(L: *mut lua_State, s: *const c_char);
572
573    /// Pushes a C function with upvalues and continuation onto the top of the stack.
574    pub fn lua_pushcclosurek(
575        L: *mut lua_State,
576        r#fn: lua_CFunction,
577        debugname: *const c_char,
578        nup: c_int,
579        cont: Option<lua_Continuation>,
580    );
581
582    /// Pushes a boolean onto the top of the stack.
583    pub fn lua_pushboolean(L: *mut lua_State, b: c_int);
584
585    /// Pushes the thread onto the top of its own stack.
586    ///
587    /// This function will return a 1 is this thread is the main thread of its
588    /// state.
589    pub fn lua_pushthread(L: *mut lua_State) -> c_int;
590
591    /// Pushes a light userdata with a tag onto the top of the stack.
592    pub fn lua_pushlightuserdatatagged(L: *mut lua_State, p: *mut c_void, tag: c_int);
593
594    /// Pushes a new userdata with a tag onto the top of the stack.
595    ///
596    /// This function will allocate a new userdata of the given size, and push
597    /// it onto the top of the stack. It will return a pointer to the allocated
598    /// userdata.
599    pub fn lua_newuserdatatagged(L: *mut lua_State, size: usize, tag: c_int) -> *mut c_void;
600
601    /// Pushes a new userdata with a tag and a metatable onto the top of the
602    /// stack.
603    ///
604    /// This function will allocate a new userdata of the given size, push it
605    /// onto the top of the stack, and set its metatable to the table registered
606    /// with the given tag. It will return a pointer to the allocated userdata.
607    pub fn lua_newuserdatataggedwithmetatable(
608        L: *mut lua_State,
609        size: usize,
610        tag: c_int,
611    ) -> *mut c_void;
612
613    /// Pushes a new userdata with a destructor onto the top of the stack.
614    ///
615    /// This function will allocate a new userdata of the given size, push it
616    /// onto the top of the stack, and set its destructor to the given function.
617    /// It will return a pointer to the allocated userdata.
618    pub fn lua_newuserdatadtor(
619        L: *mut lua_State,
620        size: usize,
621        dtor: Option<extern "C-unwind" fn(*mut c_void)>,
622    ) -> *mut c_void;
623
624    /// Pushes a new buffer onto the top of the stack.
625    ///
626    /// This function will allocate a new buffer of the given size, push it onto
627    /// the top of the stack, and return a pointer to the allocated buffer.
628    pub fn lua_newbuffer(L: *mut lua_State, size: usize) -> *mut c_void;
629
630    /// Indexes a table at the given index with the key at the top of the stack
631    /// and pushes the value onto the stack.
632    ///
633    /// This function is equivalent to the Luau code `t[k]` where `t` is at the
634    /// given index, and `k` is at the top of the stack. The key will be popped
635    /// from the stack, and the resulting value will be pushed onto the stack.
636    ///
637    /// The type of the resulting value is returned.
638    pub fn lua_gettable(L: *mut lua_State, idx: c_int) -> lua_Type;
639
640    /// Indexes a table at the given index with the given string key and pushes
641    /// the value onto the stack.
642    ///
643    /// This function is equivalent to the Luau code `t[k]` where `t` is at the
644    /// given index, and `k` is the given string key. The resulting value will
645    /// be pushed onto the stack.
646    ///
647    /// The type of the resulting value is returned.
648    pub fn lua_getfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> lua_Type;
649
650    /// Raw-indexes a table at the given index with the given string key and
651    /// pushes the value onto the stack.
652    ///
653    /// This function is equivalent to the Luau code `rawget(t, k)` where `t` is
654    /// at the given index, and `k` is the given string key. The resulting
655    /// value will be pushed onto the stack.
656    ///
657    /// The type of the resulting value is returned.
658    pub fn lua_rawgetfield(L: *mut lua_State, idx: c_int, k: *const c_char) -> lua_Type;
659
660    /// Raw-indexes a table at the given index with the key at the top of the
661    /// stack and pushes the value onto the stack.
662    ///
663    /// This function is equivalent to the Luau code `rawget(t, k)` where `t` is
664    /// at the given index, and `k` is at the top of the stack. The key will be
665    /// popped from the stack, and the resulting value will be pushed onto the
666    /// stack.
667    ///
668    /// The type of the resulting value is returned.
669    pub fn lua_rawget(L: *mut lua_State, idx: c_int) -> lua_Type;
670
671    /// Raw-indexes a table at the given index with the given integer key and
672    /// pushes the value onto the stack.
673    ///
674    /// This function is equivalent to the Luau code `rawget(t, k)` where `t` is
675    /// at the given index, and `k` is the given integer key. The resulting
676    /// value will be pushed onto the stack.
677    ///
678    /// The type of the resulting value is returned.
679    pub fn lua_rawgeti(L: *mut lua_State, idx: c_int, k: c_int) -> lua_Type;
680
681    /// Creates a new table of the given size and pushes it onto the stack.
682    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
683
684    /// Sets the readonly status of the table at the given index.
685    pub fn lua_setreadonly(L: *mut lua_State, idx: c_int, enabled: c_int);
686
687    /// Gets the readonly status of the table at the given index.
688    pub fn lua_getreadonly(L: *mut lua_State, idx: c_int) -> c_int;
689
690    /// Sets the safe environment status of the table at the given index.
691    pub fn lua_setsafeenv(L: *mut lua_State, idx: c_int, enabled: c_int);
692
693    /// Gets and pushes the metatable of the value at the given index onto the
694    /// top of the stack.
695    ///
696    /// If the value at the given index does not have a metatable, this function
697    /// will not push anything onto the stack and will return `0`. If the value
698    /// does have a metatable, it will be pushed onto the stack and `1` will be
699    /// returned.
700    pub fn lua_getmetatable(L: *mut lua_State, idx: c_int) -> c_int;
701
702    /// Gets the environment of the value at the given index and pushes it onto
703    /// the top of the stack.
704    ///
705    /// If the value at the given index does not have an environment, this
706    /// function will push `nil` onto the stack. If the value does have an
707    /// environment, it will be pushed onto the stack.
708    ///
709    /// Only functions and threads have environments.
710    pub fn lua_getfenv(L: *mut lua_State, idx: c_int);
711
712    /// Sets a value in a table at the given index with the key at the top of
713    /// stack.
714    ///
715    /// This function is equivalent to the Luau code `t[k] = v` where `t` is at
716    /// the given index, `v` is at the top of the stack, and `k` is right below
717    /// `v`.
718    ///
719    /// Both the key and value will be popped from the stack.
720    pub fn lua_settable(L: *mut lua_State, idx: c_int);
721
722    /// Sets a value in a table at the given index with the given string key.
723    ///
724    /// This function is equivalent to the Luau code `t[k] = v` where `t` is at
725    /// the given index, `v` is at the top of the stack, and `k` is the given
726    /// string key.
727    ///
728    /// The value will be popped from the stack.
729    pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);
730
731    /// Raw-sets a value in a table at the given index with the given string
732    /// key.
733    ///
734    /// This function is equivalent to the Luau code `rawset(t, k, v)` where `t`
735    /// is at the given index, `v` is at the top of the stack, and `k` is the
736    /// given string key.
737    ///
738    /// The value will be popped from the stack.
739    pub fn lua_rawsetfield(L: *mut lua_State, idx: c_int, k: *const c_char);
740
741    /// Raw-sets a value in a table at the given index with the key at the top
742    /// of the stack.
743    ///
744    /// This function is equivalent to the Luau code `rawset(t, k, v)` where `t`
745    /// is at the given index, `v` is at the top of the stack, and `k` is right
746    /// below `v`.
747    ///
748    /// Both the key and value will be popped from the stack.
749    pub fn lua_rawset(L: *mut lua_State, idx: c_int);
750
751    /// Raw-sets a value in a table at the given index with the given integer
752    /// key.
753    ///
754    /// This function is equivalent to the Luau code `rawset(t, k, v)` where `t`
755    /// is at the given index, `v` is at the top of the stack, and `k` is the
756    /// given integer key.
757    ///
758    /// The value will be popped from the stack.
759    pub fn lua_rawseti(L: *mut lua_State, idx: c_int, k: c_int) -> c_int;
760
761    /// Sets the metatable of the value at the given index to the value at the
762    /// top of the stack.
763    ///
764    /// The metatable of tables and userdata can be set using this, but also the
765    /// metatable of any other primitive type can be set.
766    ///
767    /// This function will set the metatable of the value at the given index
768    /// to the value at the top of the stack, and pop the value from the stack.
769    ///
770    /// This function will always return `1`.
771    pub fn lua_setmetatable(L: *mut lua_State, idx: c_int) -> c_int;
772
773    /// Sets the environment of the value at the given index to the value at the
774    /// top of the stack.
775    ///
776    /// This function will set the environment of the value at the given index
777    /// to the value at the top of the stack, and pop the value from the stack.
778    ///
779    /// Only functions and threads have environments, so this function will only
780    /// work for those types.
781    ///
782    /// This function will return `1` if the environment was set successfully,
783    /// and `0` if the value at the given index does not have an environment.
784    pub fn lua_setfenv(L: *mut lua_State, idx: c_int) -> c_int;
785
786    /// Pushes a function constructed from bytecode onto the top of the stack.
787    ///
788    /// This function will push a function onto the top of the stack, which is
789    /// constructed from the given bytecode. The bytecode is expected to be in
790    /// the format produced by the Luau compiler.
791    ///
792    /// The `env` argument is the index of the environment table to use for the
793    /// function. If `env` is `0`, the function will use the current environment
794    /// of the state.
795    pub fn luau_load(
796        L: *mut lua_State,
797        chunkname: *const c_char,
798        data: *const c_char,
799        size: usize,
800        env: c_int,
801    ) -> c_int;
802
803    /// Calls a function on the stack, with the given number of arguments, and
804    /// the given number of results.
805    ///
806    /// The function should be pushed onto the stack, and then the arguments
807    /// should be pushed onto the stack above the function. The arguments and
808    /// the function will be popped from the stack, and the results will be
809    /// pushed onto the stack.
810    ///
811    /// The `nargs` argument is the number of arguments to pass to the function,
812    /// and the `nresults` argument is the number of results to expect from the
813    /// function. If there are more results than `nresults`, the additional
814    /// results will be discarded. If there are fewer results than `nresults`,
815    /// the missing results will be filled with `nil` values.
816    ///
817    /// If `nresults` is [`LUA_MULTRET`], all results will be pushed onto the
818    /// stack.
819    ///
820    /// Any errors that occur during the call will be propagated and this
821    /// function will not return. If the function yields, this function will
822    /// return.
823    pub fn lua_call(L: *mut lua_State, nargs: c_int, nresults: c_int);
824
825    /// Calls a protected function on the stack, with the given number of
826    /// arguments, and the given number of results.
827    ///
828    /// This function is similar to [`lua_call`], but in the case of an error,
829    /// the error will be caught, the error value will be pushed onto the stack,
830    /// and an error code will be returned.
831    ///
832    /// If `errfunc` is not `0`, it will be used as the index of the error
833    /// handler function, which will be called with the error value on the stack
834    /// and should return an error value. This function is usually used to add
835    /// stack traces to errors, as the error handler function is called before
836    /// the stack is unwound.
837    ///
838    /// When the call is successful, the results will be pushed onto the stack,
839    /// and the function will return [`LUA_OK`].
840    ///
841    /// When the call errors, the error handler, if it exists, will be called,
842    /// the error value will be pushed to the stack, and the function will
843    /// return [`LUA_ERRRUN`], or if the error handler errored, [`LUA_ERRERR`],
844    /// or if there was an allocation error [`LUA_ERRMEM`] will be returned.
845    ///
846    /// When the call yields, the function will return [`LUA_OK`]
847    pub fn lua_pcall(
848        L: *mut lua_State,
849        nargs: c_int,
850        nresults: c_int,
851        errfunc: c_int,
852    ) -> lua_Status;
853
854    /// Calls a protected C function with a single light userdata argument.
855    ///
856    /// The function is executed with only one element, the light userdata `ud`
857    /// on its stack. In the case of an error, this function returns the same
858    /// error codes as [`lua_pcall`] and with the same error object on the
859    /// stack.
860    ///
861    /// When the call is successful, this function returns [`LUA_OK`] and the
862    /// stack is left unchanged.
863    ///
864    /// Values returned by the C function are discarded.
865    pub fn lua_cpcall(L: *mut lua_State, func: lua_CFunction, ud: *mut c_void) -> lua_Status;
866
867    /// Yields the thread with the given number of results.
868    ///
869    /// This function will yield the thread, yielding the given number of
870    /// results to the resumer. This function should only be called from return
871    /// positions of C functions.
872    pub fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int;
873
874    /// TODO: Document this function.
875    pub fn lua_break(L: *mut lua_State) -> c_int;
876
877    /// Resumes a thread with the given number of arguments.
878    ///
879    /// This function will resume the thread with the given number of arguments.
880    /// The arguments should be pushed onto the stack before calling resume.
881    ///
882    /// The `from` argument is used to preserve the C call depth and limit. It
883    /// may be null, but this is bad practice.
884    ///
885    /// This function returns the status of the resumed thread after it stops
886    /// executing.
887    pub fn lua_resume(L: *mut lua_State, from: *mut lua_State, nargs: c_int) -> lua_Status;
888
889    /// Resumes a thread with an error.
890    ///
891    /// This function is similar to [`lua_resume`], but it will resume the
892    /// thread with an error. The error value is whatever is at the top of the
893    /// stack.
894    ///
895    /// The `from` argument is used to preserve the C call depth and limit. It
896    /// may be null, but this is bad practice.
897    ///
898    /// This function returns the status of the resumed thread after it stops
899    /// executing.
900    pub fn lua_resumeerror(L: *mut lua_State, from: *mut lua_State) -> lua_Status;
901
902    /// Returns the status of the thread.
903    pub fn lua_status(L: *mut lua_State) -> lua_Status;
904
905    /// Returns if the thread is yieldable or not.
906    ///
907    /// Threads may not yield when doing so would yield across a C call without
908    /// a continuation, such as during a metamethod call.
909    pub fn lua_isyieldable(L: *mut lua_State) -> c_int;
910
911    /// Returns the thread-data pointer of the given thread.
912    ///
913    /// This function returns the thread-data pointer of the given thread, which
914    /// is null if the thread-data is unset, or the pointer previously set by
915    /// [`lua_setthreaddata`].
916    pub fn lua_getthreaddata(L: *mut lua_State) -> *mut c_void;
917
918    /// Sets the thread-data pointer of the given thread.
919    ///
920    /// This function sets the thread-data pointer of the given thread to the
921    /// given pointer. The thread-data pointer is a pointer that can be used to
922    /// store arbitrary data associated with the thread. It is not used by Luau
923    /// itself, and is intended for use by C extensions.
924    pub fn lua_setthreaddata(L: *mut lua_State, data: *mut c_void);
925
926    /// Returns the status of the given coroutine.
927    ///
928    /// This is equivalent to the Luau function `coroutine.status`.
929    pub fn lua_costatus(L: *mut lua_State) -> lua_CoStatus;
930}
931
932/// Garbage collection operations that can be performed.
933#[repr(C)]
934#[derive(Clone, Copy, PartialEq, Eq)]
935pub enum lua_GCOp {
936    /// Stops the incremental garbage collector.
937    LUA_GCSTOP,
938
939    /// Restarts the incremental garbage collector after a stop.
940    LUA_GCRESTART,
941
942    /// Runs a full garbage collection cycle.
943    LUA_GCCOLLECT,
944
945    /// Counts the heap size in kilobytes.
946    LUA_GCCOUNT,
947
948    /// Counts the remainder of the heap size in bytes, after the kilobytes.
949    LUA_GCCOUNTB,
950
951    /// Returns if the GC is running. This may still return true even if the GC
952    /// is not actively collecting.
953    LUA_GCISRUNNING,
954
955    /// Performs an explicit GC step with the given size in kilobytes.
956    LUA_GCSTEP,
957
958    /// Sets the goal of the incremental garbage collector. The goal is given
959    /// as a percentage of live data to all data, so 200% (the default) means
960    /// that the heap is allowed to grow to twice the size of the live data.
961    LUA_GCSETGOAL,
962
963    /// Sets the step multiplier of the incremental garbage collector. The GC
964    /// will try to collect at least this percentage of the heap size each step.
965    LUA_GCSETSTEPMUL,
966
967    /// Sets the step size of the incremental garbage collector. The GC will
968    /// run a step after this many bytes are allocated.
969    LUA_GCSETSTEPSIZE,
970}
971
972unsafe extern "C-unwind" {
973    /// Performs a garbage collection operation.
974    ///
975    /// These operations are defined and documented in [`lua_GCOp`].
976    pub fn lua_gc(L: *mut lua_State, what: lua_GCOp, data: c_int) -> c_int;
977
978    /// Sets the memory category of the thread.
979    ///
980    /// All memory allocated by the thread will be counted towards this
981    /// category, which can be used to track memory usage of different parts of
982    /// the program.
983    pub fn lua_setmemcat(L: *mut lua_State, category: c_int);
984
985    /// Reads the number of bytes allocated to a given memory category.
986    pub fn lua_totalbytes(L: *mut lua_State, category: c_int) -> usize;
987
988    /// Throws an error.
989    ///
990    /// This function will throw an error with the top value on the stack being
991    /// the error value. This function will not return.
992    pub fn lua_error(L: *mut lua_State) -> !;
993
994    /// Produces the next key and value pair from a table given the previous
995    /// key.
996    ///
997    /// This function is equivalent to the Luau code `next(t, k)` where `t` is
998    /// the table at the given index, and `k` is the value at the top of the
999    /// stack. The key will be popped from the stack, and the resulting key and
1000    /// value will be pushed onto the stack.
1001    ///
1002    /// If the table has no more key-value pairs, this function will return `0`
1003    /// and will not push a key and value onto the stack, but the previous key
1004    /// will still be popped from the stack.
1005    ///
1006    /// A typical traversal of a table using this function would look like this:
1007    ///
1008    /// ```c
1009    /// // table is at index t
1010    /// lua_pushnil(L); // push nil as the first key
1011    /// while (lua_next(L, t) != 0) {
1012    ///     // key is at index -2, value is at index -1
1013    ///     lua_pop(L, 1); // remove value, keep key for next iteration
1014    /// }
1015    /// ```
1016    ///
1017    /// This function, while still usable, is not recommended for use. Instead,
1018    /// use [`lua_rawiter`].
1019    pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
1020
1021    /// Produces the next key and value pair from a table given the previous
1022    /// iteration index.
1023    ///
1024    /// This function will iterate over the table at the given index, starting
1025    /// from the array section, then iterating over the hashmap section. After
1026    /// each call, the function returns an iteration index that should be passed
1027    /// to the next call to continue iterating, and it will return `-1` when
1028    /// iteration is complete.
1029    ///
1030    /// The function will push the key and value onto the stack.
1031    ///
1032    /// A typical traversal of a table using this function would look like this:
1033    ///
1034    /// ```c
1035    /// // table is at index t
1036    /// int iteration_index = 0; // start iteration with 0
1037    /// while ((iteration_index = lua_rawiter(L, t, iteration_index)) != -1) {
1038    ///     // key is at index -2, value is at index -1
1039    ///     lua_pop(L, 2); // remove key and value
1040    /// }
1041    /// ```
1042    pub fn lua_rawiter(L: *mut lua_State, idx: c_int, iteration_index: c_int) -> c_int;
1043
1044    /// Concatenates `n` values from the stack into a string.
1045    ///
1046    /// This function pops `n` values from the top of the stack, concatenates
1047    /// them into a string, and pushes the resulting string onto the top of the
1048    /// stack. When `n` is `0` an empty string is pushed onto the stack.
1049    pub fn lua_concat(L: *mut lua_State, n: c_int);
1050
1051    /// Encodes a pointer such that it remains unique but no longer points to
1052    /// the original location.
1053    ///
1054    /// This is useful for sandboxing pointers exposed to Luau.
1055    pub fn lua_encodepointer(L: *mut lua_State, p: usize) -> usize;
1056
1057    /// Returns a high-precision timestamp in seconds.
1058    ///
1059    /// The returned timestamp doesn't have a defined epoch, but can be used to
1060    /// measure duration with sub-microsecond precision.
1061    pub fn lua_clock() -> c_double;
1062
1063    /// Sets the tag of a userdata at the given index.
1064    pub fn lua_setuserdatatag(L: *mut lua_State, idx: c_int, tag: c_int) -> c_int;
1065}
1066
1067/// The type of destructor functions for userdata.
1068pub type lua_Destructor = extern "C-unwind" fn(L: *mut lua_State, userdata: *mut c_void);
1069
1070unsafe extern "C-unwind" {
1071    /// Sets the destructor of a userdata tag.
1072    ///
1073    /// For all userdata with the same tag, the destructor will be called when
1074    /// the userdata is collected by the garbage collector.
1075    pub fn lua_setuserdatadtor(L: *mut lua_State, tag: c_int, dtor: Option<lua_Destructor>);
1076
1077    /// Gets the destructor of a userdata tag.
1078    ///
1079    /// For all userdata with the same tag, the destructor will be called when
1080    /// the userdata is collected by the garbage collector.
1081    pub fn lua_getuserdatadtor(L: *mut lua_State, tag: c_int) -> Option<lua_Destructor>;
1082
1083    /// Sets the metatable that userdata with the given tag will have when
1084    /// created with [`lua_newuserdatataggedwithmetatable`].
1085    ///
1086    /// The metatable will be popped from the top of the stack.
1087    pub fn lua_setuserdatametatable(L: *mut lua_State, tag: c_int);
1088
1089    /// Gets the metatable that userdata with the given tag will have when
1090    /// created with [`lua_newuserdatataggedwithmetatable`].
1091    ///
1092    /// The metatable will be pushed onto the top of the stack.
1093    pub fn lua_getuserdatametatable(L: *mut lua_State, tag: c_int) -> c_int;
1094
1095    /// Sets the name of a light userdata tag.
1096    pub fn lua_setlightuserdataname(L: *mut lua_State, tag: c_int, name: *const c_char);
1097
1098    /// Gets the name of a light userdata tag.
1099    pub fn lua_getlightuserdataname(L: *mut lua_State, tag: c_int) -> *const c_char;
1100
1101    /// Clones the function at the given index and pushes it onto the stack.
1102    ///
1103    /// The function must be a Luau function, it cannot be a C function. All
1104    /// of the function's upvalues will be copied (they reference the same
1105    /// upvalues). The function will have the environment of the current
1106    /// thread.
1107    pub fn lua_clonefunction(L: *mut lua_State, idx: c_int);
1108
1109    /// Clears the table at the given index.
1110    ///
1111    /// The table will be cleared of all values, but it will retain its
1112    /// allocated size and metatable.
1113    pub fn lua_cleartable(L: *mut lua_State, idx: c_int);
1114
1115    /// Clones the table at the given index and pushes it onto the stack.
1116    ///
1117    /// All the table's keys and values will be copied, the cloned table will
1118    /// have the same metatable and will not be readonly. The copy is shallow.
1119    pub fn lua_clonetable(L: *mut lua_State, idx: c_int);
1120
1121    /// Returns the memory allocation function of the state.
1122    pub fn lua_getallocf(L: *mut lua_State) -> Option<lua_Alloc>;
1123}
1124
1125/// A placeholder value that is unique from any created reference.
1126pub const LUA_NOREF: c_int = -1;
1127
1128/// A reference that is used to refer to a nil value.
1129pub const LUA_REFNIL: c_int = 0;
1130
1131unsafe extern "C-unwind" {
1132    /// Creates a reference to the value at the given index.
1133    ///
1134    /// This function returns an integer reference to the value at the given
1135    /// index that can be used to push the value onto the stack later. While
1136    /// the value is referenced, it will not be collected by the garbage
1137    /// collector.
1138    ///
1139    /// References can be pushed onto a stack using [`lua_getref`] and must be
1140    /// released using [`lua_unref`].
1141    pub fn lua_ref(L: *mut lua_State, idx: c_int) -> c_int;
1142
1143    /// Releases a reference to a value.
1144    ///
1145    /// This function will release the given reference created by [`lua_ref`].
1146    /// If the reference is [`LUA_NOREF`] or [`LUA_REFNIL`] this function will
1147    /// do nothing.
1148    pub fn lua_unref(L: *mut lua_State, ref_: c_int);
1149}
1150
1151/// Pushes the value of the given reference onto the stack.
1152///
1153/// This function will push the value of the given reference onto the stack and
1154/// return the type of the value. If the reference is [`LUA_NOREF`] or
1155/// [`LUA_REFNIL`], this function will push `nil` onto the stack and return
1156/// [`LUA_TNIL`].
1157pub unsafe fn lua_getref(L: *mut lua_State, r#ref: c_int) -> lua_Type {
1158    unsafe { lua_rawgeti(L, LUA_REGISTRYINDEX, r#ref) }
1159}
1160
1161/// Attempts to convert the value at the given index to a number, and returns
1162/// it.
1163///
1164/// If the value at the given index cannot be converted to a number, this
1165/// function will return `0.0`.
1166///
1167/// This function will leave numbers as is, and will transform strings which
1168/// can be parsed as numbers into numbers. This will modify the stack.
1169pub unsafe fn lua_tonumber(L: *mut lua_State, idx: c_int) -> lua_Number {
1170    unsafe { lua_tonumberx(L, idx, null_mut()) }
1171}
1172
1173/// Attempts to convert the value at the given index to an integer, and returns
1174/// it.
1175///
1176/// This function operates in a similar way as [`lua_tonumber`], but it has the
1177/// final result floored to an integer. While it will convert a string into a
1178/// number on the stack, it will not modify the stack value into an integer.
1179pub unsafe fn lua_tointeger(L: *mut lua_State, idx: c_int) -> lua_Integer {
1180    unsafe { lua_tointegerx(L, idx, null_mut()) }
1181}
1182
1183/// Attempts to convert the value at the given index to an unsigned integer, and
1184/// returns it.
1185///
1186/// This function operates in the same way as [`lua_tointeger`], but it will
1187/// return an unsigned integer instead. If the input is negative, the output
1188/// is undefined.
1189pub unsafe fn lua_tounsigned(L: *mut lua_State, idx: c_int) -> lua_Unsigned {
1190    unsafe { lua_tounsignedx(L, idx, null_mut()) }
1191}
1192
1193/// Pops `n` number of values from the stack.
1194pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
1195    unsafe { lua_settop(L, -(n) - 1) }
1196}
1197
1198/// Creates a new table and pushes it onto the stack.
1199pub unsafe fn lua_newtable(L: *mut lua_State) {
1200    unsafe { lua_createtable(L, 0, 0) }
1201}
1202
1203/// Creates a new userdata of the given size and pushes it onto the stack.
1204pub unsafe fn lua_newuserdata(L: *mut lua_State, size: usize) -> *mut c_void {
1205    unsafe { lua_newuserdatatagged(L, size, 0) }
1206}
1207
1208/// Returns the length of the string at the given index in bytes.
1209pub unsafe fn lua_strlen(L: *mut lua_State, idx: c_int) -> c_int {
1210    unsafe { lua_objlen(L, idx) }
1211}
1212
1213/// Returns if the value at the given index is a function.
1214pub unsafe fn lua_isfunction(L: *mut lua_State, idx: c_int) -> bool {
1215    unsafe { lua_type(L, idx) == LUA_TFUNCTION }
1216}
1217
1218/// Returns if the value at the given index is a table.
1219pub unsafe fn lua_istable(L: *mut lua_State, idx: c_int) -> bool {
1220    unsafe { lua_type(L, idx) == LUA_TTABLE }
1221}
1222
1223/// Returns if the value at the given index is a light userdata.
1224pub unsafe fn lua_islightuserdata(L: *mut lua_State, idx: c_int) -> bool {
1225    unsafe { lua_type(L, idx) == LUA_TLIGHTUSERDATA }
1226}
1227
1228/// Returns if the value at the given index is nil.
1229pub unsafe fn lua_isnil(L: *mut lua_State, idx: c_int) -> bool {
1230    unsafe { lua_type(L, idx) == LUA_TNIL }
1231}
1232
1233/// Returns if the value at the given index is a boolean.
1234pub unsafe fn lua_isboolean(L: *mut lua_State, idx: c_int) -> bool {
1235    unsafe { lua_type(L, idx) == LUA_TBOOLEAN }
1236}
1237
1238/// Returns if the value at the given index is a vector.
1239pub unsafe fn lua_isvector(L: *mut lua_State, idx: c_int) -> bool {
1240    unsafe { lua_type(L, idx) == LUA_TVECTOR }
1241}
1242
1243/// Returns if the value at the given index is a thread.
1244pub unsafe fn lua_isthread(L: *mut lua_State, idx: c_int) -> bool {
1245    unsafe { lua_type(L, idx) == LUA_TTHREAD }
1246}
1247
1248/// Returns if the value at the given index is a buffer.
1249pub unsafe fn lua_isbuffer(L: *mut lua_State, idx: c_int) -> bool {
1250    unsafe { lua_type(L, idx) == LUA_TBUFFER }
1251}
1252
1253/// Returns if the value at the given index is none.
1254pub unsafe fn lua_isnone(L: *mut lua_State, idx: c_int) -> bool {
1255    unsafe { lua_type(L, idx) == LUA_TNONE }
1256}
1257
1258/// Returns if the value at the given index is none or nil.
1259pub unsafe fn lua_isnoneornil(L: *mut lua_State, idx: c_int) -> bool {
1260    unsafe { lua_type(L, idx) as c_int <= LUA_TNIL as c_int }
1261}
1262
1263/// Pushes a literal string onto the stack.
1264pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static [u8]) {
1265    unsafe { lua_pushlstring(L, s.as_ptr() as _, s.len()) }
1266}
1267
1268/// Pushes a C function onto the stack.
1269pub unsafe fn lua_pushcfunction(L: *mut lua_State, r#fn: lua_CFunction, debugname: *const c_char) {
1270    unsafe { lua_pushcclosurek(L, r#fn, debugname, 0, None) }
1271}
1272
1273/// Pushes a C closure onto the stack.
1274pub unsafe fn lua_pushcclosure(
1275    L: *mut lua_State,
1276    r#fn: lua_CFunction,
1277    debugname: *const c_char,
1278    nup: c_int,
1279) {
1280    unsafe { lua_pushcclosurek(L, r#fn, debugname, nup, None) }
1281}
1282
1283/// Pushes a light userdata onto the stack.
1284pub unsafe fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void) {
1285    unsafe { lua_pushlightuserdatatagged(L, p, 0) }
1286}
1287
1288/// Sets a global variable with the given name.
1289pub unsafe fn lua_setglobal(L: *mut lua_State, name: *const c_char) {
1290    unsafe { lua_setfield(L, LUA_GLOBALSINDEX, name) }
1291}
1292
1293/// Gets a global variable with the given name and pushes it onto the stack.
1294pub unsafe fn lua_getglobal(L: *mut lua_State, name: *const c_char) -> lua_Type {
1295    unsafe { lua_getfield(L, LUA_GLOBALSINDEX, name) }
1296}
1297
1298/// Attempts to convert the value at the given index to a string, and return it.
1299///
1300/// This function attempts to convert the value at the given index to a
1301/// string, returning a pointer to the string if successful, or null if the
1302/// conversion failed.
1303///
1304/// If the value is a string, it will be returned as is. If the value is a
1305/// number, it will be converted into a string and the stack will be
1306/// modified.
1307pub unsafe fn lua_tostring(L: *mut lua_State, idx: c_int) -> *const c_char {
1308    unsafe { lua_tolstring(L, idx, null_mut()) }
1309}
1310
1311/// TODO: Document this struct.
1312#[repr(C)]
1313pub struct lua_Debug {
1314    pub name: *const c_char,
1315    pub what: *const c_char,
1316    pub source: *const c_char,
1317    pub short_src: *const c_char,
1318    pub linedefined: c_int,
1319    pub currentline: c_int,
1320    pub nupvals: c_uchar,
1321    pub nparams: c_uchar,
1322    pub isvararg: c_char,
1323    pub userdata: *mut c_void,
1324
1325    pub ssbuf: [c_char; LUA_IDSIZE as usize],
1326}
1327
1328/// TODO: Document this type.
1329pub type lua_Hook = extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug);
1330
1331unsafe extern "C-unwind" {
1332    /// Returns the stack depth, or the number of calls on the stack.
1333    pub fn lua_stackdepth(L: *mut lua_State) -> c_int;
1334
1335    /// TODO: Document this function.
1336    pub fn lua_getinfo(
1337        L: *mut lua_State,
1338        level: c_int,
1339        what: *const c_char,
1340        ar: *mut lua_Debug,
1341    ) -> c_int;
1342
1343    /// TODO: Document this function.
1344    pub fn lua_getargument(L: *mut lua_State, level: c_int, n: c_int) -> c_int;
1345
1346    /// TODO: Document this function.
1347    pub fn lua_getlocal(L: *mut lua_State, level: c_int, n: c_int) -> *const c_char;
1348
1349    /// TODO: Document this function.
1350    pub fn lua_setlocal(L: *mut lua_State, level: c_int, n: c_int) -> *const c_char;
1351
1352    /// TODO: Document this function.
1353    pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
1354
1355    /// TODO: Document this function.
1356    pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
1357
1358    /// TODO: Document this function.
1359    pub fn lua_singlestep(L: *mut lua_State, enabled: c_int);
1360
1361    /// TODO: Document this function.
1362    pub fn lua_breakpoint(
1363        L: *mut lua_State,
1364        funcindex: c_int,
1365        line: c_int,
1366        enabled: c_int,
1367    ) -> c_int;
1368}
1369
1370/// TODO: Document this type.
1371pub type lua_Coverage = extern "C-unwind" fn(
1372    context: *mut c_void,
1373    function: *const c_char,
1374    linedefined: c_int,
1375    depth: c_int,
1376    hits: *const c_int,
1377    size: usize,
1378);
1379
1380unsafe extern "C-unwind" {
1381    /// TODO: Document this function.
1382    pub fn lua_getcoverage(
1383        L: *mut lua_State,
1384        funcindex: c_int,
1385        context: *mut c_void,
1386        callback: lua_Coverage,
1387    );
1388
1389    /// TODO: Document this function.
1390    pub fn lua_debugtrace(L: *mut lua_State) -> *const c_char;
1391}
1392
1393/// TODO: Document this struct.
1394#[repr(C)]
1395pub struct lua_Callbacks {
1396    pub userdata: *mut c_void,
1397
1398    pub interrupt: Option<extern "C-unwind" fn(L: *mut lua_State, gc: c_int)>,
1399
1400    pub panic: Option<extern "C-unwind" fn(L: *mut lua_State, errcode: c_int)>,
1401
1402    pub userthread: Option<extern "C-unwind" fn(LP: *mut lua_State, L: *mut lua_State)>,
1403
1404    pub useratom: Option<extern "C-unwind" fn(s: *const c_char, l: usize) -> i16>,
1405
1406    pub debugbreak: Option<lua_Hook>,
1407
1408    pub debugstep: Option<lua_Hook>,
1409
1410    pub debuginterrupt: Option<lua_Hook>,
1411
1412    pub debugprotectederror: Option<lua_Hook>,
1413
1414    pub onallocate: Option<extern "C-unwind" fn(L: *mut lua_State, osize: usize, nsize: usize)>,
1415}
1416
1417unsafe extern "C-unwind" {
1418    /// TODO: Document this function.
1419    pub fn lua_callbacks(L: *mut lua_State) -> *mut lua_Callbacks;
1420}