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 lightuserdata 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: c_int) -> 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 `from` stack
324 /// onto the top of the `to` stack.
325 pub fn lua_xpush(from: *mut lua_State, to: *mut lua_State, idx: c_int) -> 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 /// Yields the thread with the given number of results.
855 ///
856 /// This function will yield the thread, yielding the given number of
857 /// results to the resumer. This function should only be called from return
858 /// positions of C functions.
859 pub fn lua_yield(L: *mut lua_State, nresults: c_int) -> c_int;
860
861 /// TODO: Document this function.
862 pub fn lua_break(L: *mut lua_State) -> c_int;
863
864 /// Resumes a thread with the given number of arguments.
865 ///
866 /// This function will resume the thread with the given number of arguments.
867 /// The arguments should be pushed onto the stack before calling resume.
868 ///
869 /// The `from` argument is used to preserve the C call depth and limit. It
870 /// may be null, but this is bad practice.
871 ///
872 /// This function returns the status of the resumed thread after it stops
873 /// executing.
874 pub fn lua_resume(L: *mut lua_State, from: *mut lua_State, nargs: c_int) -> lua_Status;
875
876 /// Resumes a thread with an error.
877 ///
878 /// This function is similar to [`lua_resume`], but it will resume the
879 /// thread with an error. The error value is whatever is at the top of the
880 /// stack.
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_resumeerror(L: *mut lua_State, from: *mut lua_State) -> lua_Status;
888
889 /// Returns the status of the thread.
890 pub fn lua_status(L: *mut lua_State) -> lua_Status;
891
892 /// Returns if the thread is yieldable or not.
893 ///
894 /// Threads may not yield when doing so would yield across a C call without
895 /// a continuation, such as during a metamethod call.
896 pub fn lua_isyieldable(L: *mut lua_State) -> c_int;
897
898 /// Returns the thread-data pointer of the given thread.
899 ///
900 /// This function returns the thread-data pointer of the given thread, which
901 /// is null if the thread-data is unset, or the pointer previously set by
902 /// [`lua_setthreaddata`].
903 pub fn lua_getthreaddata(L: *mut lua_State) -> *mut c_void;
904
905 /// Sets the thread-data pointer of the given thread.
906 ///
907 /// This function sets the thread-data pointer of the given thread to the
908 /// given pointer. The thread-data pointer is a pointer that can be used to
909 /// store arbitrary data associated with the thread. It is not used by Luau
910 /// itself, and is intended for use by C extensions.
911 pub fn lua_setthreaddata(L: *mut lua_State, data: *mut c_void);
912
913 /// Returns the status of the given coroutine.
914 ///
915 /// This is equivalent to the Luau function `coroutine.status`.
916 pub fn lua_costatus(L: *mut lua_State) -> lua_CoStatus;
917}
918
919/// Garbage collection operations that can be performed.
920#[repr(C)]
921#[derive(Clone, Copy, PartialEq, Eq)]
922pub enum lua_GCOp {
923 /// Stops the incremental garbage collector.
924 LUA_GCSTOP,
925
926 /// Restarts the incremental garbage collector after a stop.
927 LUA_GCRESTART,
928
929 /// Runs a full garbage collection cycle.
930 LUA_GCCOLLECT,
931
932 /// Counts the heap size in kilobytes.
933 LUA_GCCOUNT,
934
935 /// Counts the remainder of the heap size in bytes, after the kilobytes.
936 LUA_GCCOUNTB,
937
938 /// Returns if the GC is running. This may still return true even if the GC
939 /// is not actively collecting.
940 LUA_GCISRUNNING,
941
942 /// Performs an explicit GC step with the given size in kilobytes.
943 LUA_GCSTEP,
944
945 /// Sets the goal of the incremental garbage collector. The goal is given
946 /// as a percentage of live data to all data, so 200% (the default) means
947 /// that the heap is allowed to grow to twice the size of the live data.
948 LUA_GCSETGOAL,
949
950 /// Sets the step multiplier of the incremental garbage collector. The GC
951 /// will try to collect at least this percentage of the heap size each step.
952 LUA_GCSETSTEPMUL,
953
954 /// Sets the step size of the incremental garbage collector. The GC will
955 /// run a step after this many bytes are allocated.
956 LUA_GCSETSTEPSIZE,
957}
958
959unsafe extern "C-unwind" {
960 /// Performs a garbage collection operation.
961 ///
962 /// These operations are defined and documented in [`lua_GCOp`].
963 pub fn lua_gc(L: *mut lua_State, what: lua_GCOp, data: c_int) -> c_int;
964
965 /// Sets the memory category of the thread.
966 ///
967 /// All memory allocated by the thread will be counted towards this
968 /// category, which can be used to track memory usage of different parts of
969 /// the program.
970 pub fn lua_setmemcat(L: *mut lua_State, category: c_int);
971
972 /// Reads the number of bytes allocated to a given memory category.
973 pub fn lua_totalbytes(L: *mut lua_State, category: c_int) -> usize;
974
975 /// Throws an error.
976 ///
977 /// This function will throw an error with the top value on the stack being
978 /// the error value. This function will not return.
979 pub fn lua_error(L: *mut lua_State) -> !;
980
981 /// Produces the next key and value pair from a table given the previous
982 /// key.
983 ///
984 /// This function is equivalent to the Luau code `next(t, k)` where `t` is
985 /// the table at the given index, and `k` is the value at the top of the
986 /// stack. The key will be popped from the stack, and the resulting key and
987 /// value will be pushed onto the stack.
988 ///
989 /// If the table has no more key-value pairs, this function will return `0`
990 /// and will not push a key and value onto the stack, but the previous key
991 /// will still be popped from the stack.
992 ///
993 /// A typical traversal of a table using this function would look like this:
994 ///
995 /// ```c
996 /// // table is at index t
997 /// lua_pushnil(L); // push nil as the first key
998 /// while (lua_next(L, t) != 0) {
999 /// // key is at index -2, value is at index -1
1000 /// lua_pop(L, 1); // remove value, keep key for next iteration
1001 /// }
1002 /// ```
1003 ///
1004 /// This function, while still usable, is not recommended for use. Instead,
1005 /// use [`lua_rawiter`].
1006 pub fn lua_next(L: *mut lua_State, idx: c_int) -> c_int;
1007
1008 /// Produces the next key and value pair from a table given the previous
1009 /// iteration index.
1010 ///
1011 /// This function will iterate over the table at the given index, starting
1012 /// from the array section, then iterating over the hashmap section. After
1013 /// each call, the function returns an iteration index that should be passed
1014 /// to the next call to continue iterating, and it will return `-1` when
1015 /// iteration is complete.
1016 ///
1017 /// The function will push the key and value onto the stack.
1018 ///
1019 /// A typical traversal of a table using this function would look like this:
1020 ///
1021 /// ```c
1022 /// // table is at index t
1023 /// int iteration_index = 0; // start iteration with 0
1024 /// while ((iteration_index = lua_rawiter(L, t, iteration_index)) != -1) {
1025 /// // key is at index -2, value is at index -1
1026 /// lua_pop(L, 2); // remove key and value
1027 /// }
1028 /// ```
1029 pub fn lua_rawiter(L: *mut lua_State, idx: c_int, iteration_index: c_int) -> c_int;
1030
1031 /// Concatenates `n` values from the stack into a string.
1032 ///
1033 /// This function pops `n` values from the top of the stack, concatenates
1034 /// them into a string, and pushes the resulting string onto the top of the
1035 /// stack. When `n` is `0` an empty string is pushed onto the stack.
1036 pub fn lua_concat(L: *mut lua_State, n: c_int);
1037
1038 /// Encodes a pointer such that it remains unique but no longer points to
1039 /// the original location.
1040 ///
1041 /// This is useful for sandboxing pointers exposed to Luau.
1042 pub fn lua_encodepointer(L: *mut lua_State, p: usize) -> usize;
1043
1044 /// Returns a high-precision timestamp in seconds.
1045 ///
1046 /// The returned timestamp doesn't have a defined epoch, but can be used to
1047 /// measure duration with sub-microsecond precision.
1048 pub fn lua_clock() -> c_double;
1049
1050 /// Sets the tag of a userdata at the given index.
1051 pub fn lua_setuserdatatag(L: *mut lua_State, idx: c_int, tag: c_int) -> c_int;
1052}
1053
1054/// The type of destructor functions for userdata.
1055pub type lua_Destructor = extern "C-unwind" fn(L: *mut lua_State, userdata: *mut c_void);
1056
1057unsafe extern "C-unwind" {
1058 /// Sets the destructor of a userdata tag.
1059 ///
1060 /// For all userdata with the same tag, the destructor will be called when
1061 /// the userdata is collected by the garbage collector.
1062 pub fn lua_setuserdatadtor(L: *mut lua_State, tag: c_int, dtor: Option<lua_Destructor>);
1063
1064 /// Gets the destructor of a userdata tag.
1065 ///
1066 /// For all userdata with the same tag, the destructor will be called when
1067 /// the userdata is collected by the garbage collector.
1068 pub fn lua_getuserdatadtor(L: *mut lua_State, tag: c_int) -> Option<lua_Destructor>;
1069
1070 /// Sets the metatable that userdata with the given tag will have when
1071 /// created with [`lua_newuserdatataggedwithmetatable`].
1072 ///
1073 /// The metatable will be popped from the top of the stack.
1074 pub fn lua_setuserdatametatable(L: *mut lua_State, tag: c_int);
1075
1076 /// Gets the metatable that userdata with the given tag will have when
1077 /// created with [`lua_newuserdatataggedwithmetatable`].
1078 ///
1079 /// The metatable will be pushed onto the top of the stack.
1080 pub fn lua_getuserdatametatable(L: *mut lua_State, tag: c_int) -> c_int;
1081
1082 /// Sets the name of a light userdata tag.
1083 pub fn lua_setlightuserdataname(L: *mut lua_State, tag: c_int, name: *const c_char);
1084
1085 /// Gets the name of a light userdata tag.
1086 pub fn lua_getlightuserdataname(L: *mut lua_State, tag: c_int) -> *const c_char;
1087
1088 /// Clones the function at the given index and pushes it onto the stack.
1089 ///
1090 /// The function must be a Luau function, it cannot be a C function. All
1091 /// of the function's upvalues will be copied (they reference the same
1092 /// upvalues). The function will have the environment of the current
1093 /// thread.
1094 pub fn lua_clonefunction(L: *mut lua_State, idx: c_int);
1095
1096 /// Clears the table at the given index.
1097 ///
1098 /// The table will be cleared of all values, but it will retain its
1099 /// allocated size and metatable.
1100 pub fn lua_cleartable(L: *mut lua_State, idx: c_int);
1101
1102 /// Clones the table at the given index and pushes it onto the stack.
1103 ///
1104 /// All the table's keys and values will be copied, the cloned table will
1105 /// have the same metatable and will not be readonly. The copy is shallow.
1106 pub fn lua_clonetable(L: *mut lua_State, idx: c_int);
1107
1108 /// Returns the memory allocation function of the state.
1109 pub fn lua_getallocf(L: *mut lua_State) -> Option<lua_Alloc>;
1110}
1111
1112/// A placeholder value that is unique from any created reference.
1113pub const LUA_NOREF: c_int = -1;
1114
1115/// A reference that is used to refer to a nil value.
1116pub const LUA_REFNIL: c_int = 0;
1117
1118unsafe extern "C-unwind" {
1119 /// Creates a reference to the value at the given index.
1120 ///
1121 /// This function returns an integer reference to the value at the given
1122 /// index that can be used to push the value onto the stack later. While
1123 /// the value is referenced, it will not be collected by the garbage
1124 /// collector.
1125 ///
1126 /// References can be pushed onto a stack using [`lua_getref`] and must be
1127 /// released using [`lua_unref`].
1128 pub fn lua_ref(L: *mut lua_State, idx: c_int) -> c_int;
1129
1130 /// Releases a reference to a value.
1131 ///
1132 /// This function will release the given reference created by [`lua_ref`].
1133 /// If the reference is [`LUA_NOREF`] or [`LUA_REFNIL`] this function will
1134 /// do nothing.
1135 pub fn lua_unref(L: *mut lua_State, ref_: c_int);
1136}
1137
1138/// Pushes the value of the given reference onto the stack.
1139///
1140/// This function will push the value of the given reference onto the stack and
1141/// return the type of the value. If the reference is [`LUA_NOREF`] or
1142/// [`LUA_REFNIL`], this function will push `nil` onto the stack and return
1143/// [`LUA_TNIL`].
1144pub unsafe fn lua_getref(L: *mut lua_State, r#ref: c_int) -> lua_Type {
1145 unsafe { lua_rawgeti(L, LUA_REGISTRYINDEX, r#ref) }
1146}
1147
1148/// Attempts to convert the value at the given index to a number, and returns
1149/// it.
1150///
1151/// If the value at the given index cannot be converted to a number, this
1152/// function will return `0.0`.
1153///
1154/// This function will leave numbers as is, and will transform strings which
1155/// can be parsed as numbers into numbers. This will modify the stack.
1156pub unsafe fn lua_tonumber(L: *mut lua_State, idx: c_int) -> lua_Number {
1157 unsafe { lua_tonumberx(L, idx, null_mut()) }
1158}
1159
1160/// Attempts to convert the value at the given index to an integer, and returns
1161/// it.
1162///
1163/// This function operates in a similar way as [`lua_tonumber`], but it has the
1164/// final result floored to an integer. While it will convert a string into a
1165/// number on the stack, it will not modify the stack value into an integer.
1166pub unsafe fn lua_tointeger(L: *mut lua_State, idx: c_int) -> lua_Integer {
1167 unsafe { lua_tointegerx(L, idx, null_mut()) }
1168}
1169
1170/// Attempts to convert the value at the given index to an unsigned integer, and
1171/// returns it.
1172///
1173/// This function operates in the same way as [`lua_tointeger`], but it will
1174/// return an unsigned integer instead. If the input is negative, the output
1175/// is undefined.
1176pub unsafe fn lua_tounsigned(L: *mut lua_State, idx: c_int) -> lua_Unsigned {
1177 unsafe { lua_tounsignedx(L, idx, null_mut()) }
1178}
1179
1180/// Pops `n` number of values from the stack.
1181pub unsafe fn lua_pop(L: *mut lua_State, n: c_int) {
1182 unsafe { lua_settop(L, -(n) - 1) }
1183}
1184
1185/// Creates a new table and pushes it onto the stack.
1186pub unsafe fn lua_newtable(L: *mut lua_State) {
1187 unsafe { lua_createtable(L, 0, 0) }
1188}
1189
1190/// Creates a new userdata of the given size and pushes it onto the stack.
1191pub unsafe fn lua_newuserdata(L: *mut lua_State, size: usize) -> *mut c_void {
1192 unsafe { lua_newuserdatatagged(L, size, 0) }
1193}
1194
1195/// Returns the length of the string at the given index in bytes.
1196pub unsafe fn lua_strlen(L: *mut lua_State, idx: c_int) -> c_int {
1197 unsafe { lua_objlen(L, idx) }
1198}
1199
1200/// Returns if the value at the given index is a function.
1201pub unsafe fn lua_isfunction(L: *mut lua_State, idx: c_int) -> bool {
1202 unsafe { lua_type(L, idx) == LUA_TFUNCTION }
1203}
1204
1205/// Returns if the value at the given index is a table.
1206pub unsafe fn lua_istable(L: *mut lua_State, idx: c_int) -> bool {
1207 unsafe { lua_type(L, idx) == LUA_TTABLE }
1208}
1209
1210/// Returns if the value at the given index is a light userdata.
1211pub unsafe fn lua_islightuserdata(L: *mut lua_State, idx: c_int) -> bool {
1212 unsafe { lua_type(L, idx) == LUA_TLIGHTUSERDATA }
1213}
1214
1215/// Returns if the value at the given index is nil.
1216pub unsafe fn lua_isnil(L: *mut lua_State, idx: c_int) -> bool {
1217 unsafe { lua_type(L, idx) == LUA_TNIL }
1218}
1219
1220/// Returns if the value at the given index is a boolean.
1221pub unsafe fn lua_isboolean(L: *mut lua_State, idx: c_int) -> bool {
1222 unsafe { lua_type(L, idx) == LUA_TBOOLEAN }
1223}
1224
1225/// Returns if the value at the given index is a vector.
1226pub unsafe fn lua_isvector(L: *mut lua_State, idx: c_int) -> bool {
1227 unsafe { lua_type(L, idx) == LUA_TVECTOR }
1228}
1229
1230/// Returns if the value at the given index is a thread.
1231pub unsafe fn lua_isthread(L: *mut lua_State, idx: c_int) -> bool {
1232 unsafe { lua_type(L, idx) == LUA_TTHREAD }
1233}
1234
1235/// Returns if the value at the given index is a buffer.
1236pub unsafe fn lua_isbuffer(L: *mut lua_State, idx: c_int) -> bool {
1237 unsafe { lua_type(L, idx) == LUA_TBUFFER }
1238}
1239
1240/// Returns if the value at the given index is none.
1241pub unsafe fn lua_isnone(L: *mut lua_State, idx: c_int) -> bool {
1242 unsafe { lua_type(L, idx) == LUA_TNONE }
1243}
1244
1245/// Returns if the value at the given index is none or nil.
1246pub unsafe fn lua_isnoneornil(L: *mut lua_State, idx: c_int) -> bool {
1247 unsafe { lua_type(L, idx) as c_int <= LUA_TNIL as c_int }
1248}
1249
1250/// Pushes a literal string onto the stack.
1251pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static [u8]) {
1252 unsafe { lua_pushlstring(L, s.as_ptr() as _, s.len()) }
1253}
1254
1255/// Pushes a C function onto the stack.
1256pub unsafe fn lua_pushcfunction(L: *mut lua_State, r#fn: lua_CFunction, debugname: *const c_char) {
1257 unsafe { lua_pushcclosurek(L, r#fn, debugname, 0, None) }
1258}
1259
1260/// Pushes a C closure onto the stack.
1261pub unsafe fn lua_pushcclosure(
1262 L: *mut lua_State,
1263 r#fn: lua_CFunction,
1264 debugname: *const c_char,
1265 nup: c_int,
1266) {
1267 unsafe { lua_pushcclosurek(L, r#fn, debugname, nup, None) }
1268}
1269
1270/// Pushes a light userdata onto the stack.
1271pub unsafe fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void) {
1272 unsafe { lua_pushlightuserdatatagged(L, p, 0) }
1273}
1274
1275/// Sets a global variable with the given name.
1276pub unsafe fn lua_setglobal(L: *mut lua_State, name: *const c_char) {
1277 unsafe { lua_setfield(L, LUA_GLOBALSINDEX, name) }
1278}
1279
1280/// Gets a global variable with the given name and pushes it onto the stack.
1281pub unsafe fn lua_getglobal(L: *mut lua_State, name: *const c_char) -> lua_Type {
1282 unsafe { lua_getfield(L, LUA_GLOBALSINDEX, name) }
1283}
1284
1285/// Attempts to convert the value at the given index to a string, and return it.
1286///
1287/// This function attempts to convert the value at the given index to a
1288/// string, returning a pointer to the string if successful, or null if the
1289/// conversion failed.
1290///
1291/// If the value is a string, it will be returned as is. If the value is a
1292/// number, it will be converted into a string and the stack will be
1293/// modified.
1294pub unsafe fn lua_tostring(L: *mut lua_State, idx: c_int) -> *const c_char {
1295 unsafe { lua_tolstring(L, idx, null_mut()) }
1296}
1297
1298/// TODO: Document this struct.
1299#[repr(C)]
1300pub struct lua_Debug {
1301 pub name: *const c_char,
1302 pub what: *const c_char,
1303 pub source: *const c_char,
1304 pub short_src: *const c_char,
1305 pub linedefined: c_int,
1306 pub currentline: c_int,
1307 pub nupvals: c_uchar,
1308 pub nparams: c_uchar,
1309 pub isvararg: c_char,
1310 pub userdata: *mut c_void,
1311
1312 pub ssbuf: [c_char; LUA_IDSIZE as usize],
1313}
1314
1315/// TODO: Document this type.
1316pub type lua_Hook = extern "C-unwind" fn(L: *mut lua_State, ar: *mut lua_Debug);
1317
1318unsafe extern "C-unwind" {
1319 /// Returns the stack depth, or the number of calls on the stack.
1320 pub fn lua_stackdepth(L: *mut lua_State) -> c_int;
1321
1322 /// TODO: Document this function.
1323 pub fn lua_getinfo(
1324 L: *mut lua_State,
1325 level: c_int,
1326 what: *const c_char,
1327 ar: *mut lua_Debug,
1328 ) -> c_int;
1329
1330 /// TODO: Document this function.
1331 pub fn lua_getargument(L: *mut lua_State, level: c_int, n: c_int) -> c_int;
1332
1333 /// TODO: Document this function.
1334 pub fn lua_getlocal(L: *mut lua_State, level: c_int, n: c_int) -> *const c_char;
1335
1336 /// TODO: Document this function.
1337 pub fn lua_setlocal(L: *mut lua_State, level: c_int, n: c_int) -> *const c_char;
1338
1339 /// TODO: Document this function.
1340 pub fn lua_getupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
1341
1342 /// TODO: Document this function.
1343 pub fn lua_setupvalue(L: *mut lua_State, funcindex: c_int, n: c_int) -> *const c_char;
1344
1345 /// TODO: Document this function.
1346 pub fn lua_singlestep(L: *mut lua_State, enabled: c_int);
1347
1348 /// TODO: Document this function.
1349 pub fn lua_breakpoint(
1350 L: *mut lua_State,
1351 funcindex: c_int,
1352 line: c_int,
1353 enabled: c_int,
1354 ) -> c_int;
1355}
1356
1357/// TODO: Document this type.
1358pub type lua_Coverage = extern "C-unwind" fn(
1359 context: *mut c_void,
1360 function: *const c_char,
1361 linedefined: c_int,
1362 depth: c_int,
1363 hits: *const c_int,
1364 size: usize,
1365);
1366
1367unsafe extern "C-unwind" {
1368 /// TODO: Document this function.
1369 pub fn lua_getcoverage(
1370 L: *mut lua_State,
1371 funcindex: c_int,
1372 context: *mut c_void,
1373 callback: lua_Coverage,
1374 );
1375
1376 /// TODO: Document this function.
1377 pub fn lua_debugtrace(L: *mut lua_State) -> *const c_char;
1378}
1379
1380/// TODO: Document this struct.
1381#[repr(C)]
1382pub struct lua_Callbacks {
1383 pub userdata: *mut c_void,
1384
1385 pub interrupt: Option<extern "C-unwind" fn(L: *mut lua_State, gc: c_int)>,
1386
1387 pub panic: Option<extern "C-unwind" fn(L: *mut lua_State, errcode: c_int)>,
1388
1389 pub userthread: Option<extern "C-unwind" fn(LP: *mut lua_State, L: *mut lua_State)>,
1390
1391 pub useratom: Option<extern "C-unwind" fn(s: *const c_char, l: usize) -> i16>,
1392
1393 pub debugbreak: Option<lua_Hook>,
1394
1395 pub debugstep: Option<lua_Hook>,
1396
1397 pub debuginterrupt: Option<lua_Hook>,
1398
1399 pub debugprotectederror: Option<lua_Hook>,
1400
1401 pub onallocate: Option<extern "C-unwind" fn(L: *mut lua_State, osize: usize, nsize: usize)>,
1402}
1403
1404unsafe extern "C-unwind" {
1405 /// TODO: Document this function.
1406 pub fn lua_callbacks(L: *mut lua_State) -> *mut lua_Callbacks;
1407}