tlua/ffi.rs
1//! Module provides FFI bindings for the following constants,
2//! types and functions, realted to Lua C API:
3//! 1. Plain lua C API
4//! 2. lauxlib
5//! 3. Lua utitlites, implemented in Tarantool
6
7#![allow(non_camel_case_types)]
8#![allow(clippy::missing_safety_doc)]
9use std::os::raw::{c_char, c_double, c_int, c_void};
10use std::ptr::null_mut;
11
12/// Lua provides a registry, a pre-defined table that can be used by any C code
13/// to store whatever Lua value it needs to store. This table is always located
14/// at pseudo-index `LUA_REGISTRYINDEX`. Any C library can store data into this
15/// table, but it should take care to choose keys different from those used by
16/// other libraries, to avoid collisions. Typically, you should use as key a
17/// string containing your library name or a light userdata with the address of
18/// a C object in your code.
19///
20/// The integer keys in the registry are used by the reference mechanism,
21/// implemented by the auxiliary library, and therefore should not be used for
22/// other purposes.
23pub const LUA_REGISTRYINDEX: c_int = -10000;
24pub const LUA_ENVIRONINDEX: c_int = -10001;
25pub const LUA_GLOBALSINDEX: c_int = -10002;
26
27pub fn is_relative_index(index: c_int) -> bool {
28 index < 0 && index > LUA_REGISTRYINDEX
29}
30
31pub const LUA_OK: c_int = 0;
32pub const LUA_YIELD: c_int = 1;
33pub const LUA_ERRRUN: c_int = 2;
34pub const LUA_ERRSYNTAX: c_int = 3;
35pub const LUA_ERRMEM: c_int = 4;
36pub const LUA_ERRERR: c_int = 5;
37
38pub const LUA_TNONE: c_int = -1;
39
40pub const LUA_TNIL: c_int = 0;
41pub const LUA_TBOOLEAN: c_int = 1;
42pub const LUA_TLIGHTUSERDATA: c_int = 2;
43pub const LUA_TNUMBER: c_int = 3;
44pub const LUA_TSTRING: c_int = 4;
45pub const LUA_TTABLE: c_int = 5;
46pub const LUA_TFUNCTION: c_int = 6;
47pub const LUA_TUSERDATA: c_int = 7;
48pub const LUA_TTHREAD: c_int = 8;
49pub const LUA_TCDATA: c_int = 10;
50
51pub const LUA_MINSTACK: c_int = 20;
52
53pub const LUA_NOREF: c_int = -2;
54pub const LUA_REFNIL: c_int = -1;
55
56pub const LUA_MULTRET: c_int = -1;
57
58#[repr(C)]
59#[derive(Debug, Copy, Clone)]
60pub struct lua_State {
61 pub _unused: [u8; 0],
62}
63
64#[repr(C)]
65pub struct luaL_Reg {
66 pub name: *const c_char,
67 pub func: lua_CFunction,
68}
69
70pub type lua_Number = libc::c_double;
71pub type lua_Integer = libc::ptrdiff_t;
72
73/// Type for C functions.
74///
75/// In order to communicate properly with Lua, a C function must use the
76/// following protocol, which defines the way parameters and results are passed:
77/// a C function receives its arguments from Lua in its stack in direct order
78/// (the first argument is pushed first). So, when the function starts,
79/// [`lua_gettop`]`(l)` returns the number of arguments received by the function.
80/// The first argument (if any) is at index 1 and its last argument is at index
81/// [`lua_gettop`]`(l)`. To return values to Lua, a C function just pushes them
82/// onto the stack, in direct order (the first result is pushed first), and
83/// returns the number of results. Any other value in the stack below the
84/// results will be properly discarded by Lua. Like a Lua function, a C function
85/// called by Lua can also return many results.
86///
87/// As an example, the following function receives a variable number of
88/// numerical arguments and returns their average and sum:
89///
90/// ```
91/// use tlua::c_ptr;
92/// use tlua::ffi::{
93/// lua_gettop, lua_isnumber, lua_pushstring, lua_error, lua_tonumber,
94/// lua_pushnumber, lua_State, lua_Number,
95/// };
96/// unsafe extern "C" fn foo(l: *mut lua_State) -> i32 {
97/// let n = lua_gettop(l); /* number of arguments */
98/// let mut sum: lua_Number = 0.;
99/// let i: i32;
100/// for i in 1..=n {
101/// if !lua_isnumber(l, i) {
102/// lua_pushstring(l, c_ptr!("incorrect argument"));
103/// lua_error(l);
104/// }
105/// sum += lua_tonumber(l, i);
106/// }
107/// lua_pushnumber(l, sum / n as f64); /* first result */
108/// lua_pushnumber(l, sum); /* second result */
109/// return 2; /* number of results */
110/// }
111/// ```
112pub type lua_CFunction = unsafe extern "C-unwind" fn(l: *mut lua_State) -> c_int;
113
114pub type lua_Alloc = extern "C" fn(
115 ud: *mut libc::c_void,
116 ptr: *mut libc::c_void,
117 osize: libc::size_t,
118 nsize: libc::size_t,
119) -> *mut libc::c_void;
120
121/// The reader function used by [`lua_load`]. Every time it needs another piece
122/// of the chunk, [`lua_load`] calls the reader, passing along its `data`
123/// parameter. The reader must return a pointer to a block of memory with a new
124/// piece of the chunk and set `size` to the block size. The block must exist
125/// until the reader function is called again. To signal the end of the chunk,
126/// the reader must return `NULL` or set `size` to zero. The reader function may
127/// return pieces of any size greater than zero.
128pub type lua_Reader = extern "C" fn(
129 l: *mut lua_State,
130 data: *mut libc::c_void,
131 size: *mut libc::size_t,
132) -> *const libc::c_char;
133
134pub type lua_Writer = extern "C" fn(
135 l: *mut lua_State,
136 p: *const libc::c_void,
137 sz: libc::size_t,
138 ud: *mut libc::c_void,
139) -> libc::c_int;
140
141extern "C-unwind" {
142 // Lua C API functions.
143 pub fn lua_newstate(f: lua_Alloc, ud: *mut libc::c_void) -> *mut lua_State;
144 pub fn lua_close(l: *mut lua_State);
145
146 /// Creates a new thread, pushes it on the stack, and returns a pointer to a
147 /// `lua_State` that represents this new thread. The new state returned by
148 /// this function shares with the original state all global objects (such as
149 /// tables), but has an independent execution stack.
150 /// *[-0, +1, m]*
151 ///
152 /// There is no explicit function to close or to destroy a thread. Threads
153 /// are subject to garbage collection, like any Lua object.
154 pub fn lua_newthread(l: *mut lua_State) -> *mut lua_State;
155
156 pub fn lua_atpanic(l: *mut lua_State, panicf: lua_CFunction) -> lua_CFunction;
157
158 pub fn lua_version(l: *mut lua_State) -> *const lua_Number;
159
160 /// Returns the index of the top element in the stack. Because indices start
161 /// at 1, this result is equal to the number of elements in the stack (and
162 /// so 0 means an empty stack).
163 /// *[-0, +0, -]*
164 pub fn lua_gettop(l: *mut lua_State) -> c_int;
165 pub fn lua_settop(l: *mut lua_State, index: c_int);
166 pub fn lua_pushboolean(l: *mut lua_State, n: c_int);
167 pub fn lua_pushlstring(l: *mut lua_State, s: *const libc::c_char, l: libc::size_t);
168
169 /// Pushes the zero-terminated string pointed to by `s` onto the stack. Lua
170 /// makes (or reuses) an internal copy of the given string, so the memory at
171 /// s can be freed or reused immediately after the function returns. The
172 /// string cannot contain embedded zeros; it is assumed to end at the first
173 /// zero.
174 /// *[-0, +1, m]*
175 pub fn lua_pushstring(l: *mut lua_State, s: *const c_char) -> *const c_char;
176
177 /// Pushes a number with value `n` onto the stack.
178 /// *[-0, +1, -]*
179 pub fn lua_pushinteger(l: *mut lua_State, n: isize);
180
181 /// Pushes a number with value `n` onto the stack.
182 /// *[-0, +1, -]*
183 pub fn lua_pushnumber(l: *mut lua_State, n: c_double);
184
185 /// Pushes a new C closure onto the stack.
186 /// *[-n, +1, m]*
187 ///
188 /// When a C function is created, it is possible to associate some values
189 /// with it, thus creating a C closure; these values are then accessible to
190 /// the function whenever it is called. To associate values with a C
191 /// function, first these values should be pushed onto the stack (when there
192 /// are multiple values, the first value is pushed first). Then
193 /// lua_pushcclosure is called to create and push the C function onto the
194 /// stack, with the argument `n` telling how many values should be
195 /// associated with the function. lua_pushcclosure also pops these values
196 /// from the stack.
197 ///
198 /// The maximum value for `n` is 255.
199 pub fn lua_pushcclosure(l: *mut lua_State, fun: lua_CFunction, n: c_int);
200 pub fn lua_pushnil(l: *mut lua_State);
201
202 /// Pushes a copy of the element at the given valid `index` onto the stack.
203 /// *[-0, +1, -]*
204 pub fn lua_pushvalue(l: *mut lua_State, index: c_int);
205 pub fn lua_tointeger(l: *mut lua_State, index: c_int) -> isize;
206 pub fn lua_toboolean(l: *mut lua_State, index: c_int) -> c_int;
207
208 /// Converts the Lua value at the given acceptable `index` to a C string. If
209 /// `len` is not NULL, it also sets `*len` with the string length. The Lua
210 /// value must be a string or a number; otherwise, the function returns
211 /// NULL. If the value is a number, then `lua_tolstring` also changes the
212 /// actual value in the stack to a string. (This change confuses
213 /// [`lua_next`] when `lua_tolstring` is applied to keys during a table
214 /// traversal.)
215 /// *[-0, +0, m]*
216 ///
217 /// `lua_tolstring` returns a fully aligned pointer to a string inside the
218 /// Lua state. This string always has a zero ('\0') after its last character
219 /// (as in C), but can contain other zeros in its body. Because Lua has
220 /// garbage collection, there is no guarantee that the pointer returned by
221 /// `lua_tolstring` will be valid after the corresponding value is removed
222 /// from the stack.
223 pub fn lua_tolstring(l: *mut lua_State, index: c_int, len: *mut usize) -> *const c_char;
224
225 /// If the value at the given acceptable `index` is a full userdata, returns
226 /// its block address. If the value is a light userdata, returns its
227 /// pointer. Otherwise, returns `NULL`.
228 /// *[-0, +0, -]*
229 pub fn lua_touserdata(l: *mut lua_State, index: c_int) -> *mut libc::c_void;
230
231 /// Does the equivalent to `t[k] = v`, where `t` is the value at the given
232 /// valid index and `v` is the value at the top of the stack.
233 /// *[-1, +0, e]*
234 ///
235 /// This function pops the value from the stack. As in Lua, this function
236 /// may trigger a metamethod for the "newindex" event
237 pub fn lua_setfield(l: *mut lua_State, index: c_int, k: *const c_char);
238
239 /// Pushes onto the stack the value `t[k]`, where `t` is the value at the
240 /// given valid `index`. As in Lua, this function may trigger a metamethod
241 /// for the "index" event
242 /// *[-0, +1, e]*
243 pub fn lua_getfield(l: *mut lua_State, index: c_int, k: *const c_char);
244
245 /// Creates a new empty table and pushes it onto the stack. The new table
246 /// has space pre-allocated for `narr` array elements and `nrec` non-array
247 /// elements. This pre-allocation is useful when you know exactly how many
248 /// elements the table will have. Otherwise you can use the function
249 /// [`lua_newtable`].
250 /// *[-0, +1, m]*
251 pub fn lua_createtable(l: *mut lua_State, narr: c_int, nrec: c_int);
252
253 /// This function allocates a new block of memory with the given size,
254 /// pushes onto the stack a new full userdata with the block address, and
255 /// returns this address.
256 /// *[-0, +1, m]*
257 ///
258 /// Userdata represent C values in Lua. A full userdata represents a block
259 /// of memory. It is an object (like a table): you must create it, it can
260 /// have its own metatable, and you can detect when it is being collected. A
261 /// full userdata is only equal to itself (under raw equality).
262 ///
263 /// When Lua collects a full userdata with a gc metamethod, Lua calls the
264 /// metamethod and marks the userdata as finalized. When this userdata is
265 /// collected again then Lua frees its corresponding memory.
266 pub fn lua_newuserdata(l: *mut lua_State, sz: libc::size_t) -> *mut libc::c_void;
267
268 /// Pushes a light userdata onto the stack.
269 /// *[-0, +1, -]*
270 ///
271 /// Userdata represent C values in Lua. A light userdata represents a
272 /// pointer. It is a value (like a number): you do not create it, it has no
273 /// individual metatable, and it is not collected (as it was never created).
274 /// A light userdata is equal to "any" light userdata with the same C
275 /// address.
276 pub fn lua_pushlightuserdata(l: *mut lua_State, ud: *mut c_void);
277
278 /// Pushes onto the stack the value `t[k]`, where `t` is the value at the
279 /// given valid `index` and `k` is the value at the top of the stack.
280 /// *[-1, +1, e]*
281 ///
282 /// This function pops the key from the stack (putting the resulting value
283 /// in its place). As in Lua, this function may trigger a metamethod for the
284 /// "index" event
285 pub fn lua_gettable(l: *mut lua_State, index: c_int);
286
287 /// Similar to [`lua_gettable`], but does a raw access (i.e., without
288 /// metamethods).
289 /// *[-1, +1, -]*
290 pub fn lua_rawget(l: *mut lua_State, index: c_int);
291
292 /// Pushes onto the stack the value `t[n]`, where `t` is the value at the
293 /// given valid `index`. The access is *raw*; that is, it does not invoke
294 /// metamethods.
295 /// *[-0, +1, -]*
296 pub fn lua_rawgeti(l: *mut lua_State, index: c_int, n: c_int);
297
298 /// Does the equivalent to `t[k] = v`, where `t` is the value at the given
299 /// valid `index`, `v` is the value at the top of the stack, and `k` is the
300 /// value just below the top.
301 /// *[-2, +0, e]*
302 ///
303 /// This function pops both the key and the value from the stack. As in Lua,
304 /// this function may trigger a metamethod for the "newindex" event.
305 pub fn lua_settable(l: *mut lua_State, index: c_int);
306
307 /// Similar to [`lua_settable`], but does a raw assignment (i.e., without
308 /// metamethods).
309 /// *[-2, +0, m]*
310 pub fn lua_rawset(l: *mut lua_State, index: c_int);
311
312 /// Does the equivalent of `t[n] = v`, where `t` is the value at the given
313 /// valid `index` and `v` is the value at the top of the stack.
314 /// *[-1, +0, m]*
315 ///
316 /// This function pops the value from the stack. The assignment is raw; that
317 /// is, it does not invoke metamethods.
318 pub fn lua_rawseti(l: *mut lua_State, index: c_int, n: c_int);
319
320 /// Returns the type of the value in the given acceptable `index`, or
321 /// [`LUA_TNONE`] for a non-valid index (that is, an index to an "empty"
322 /// stack position). The types returned by lua_type are coded by the
323 /// following constants: [`LUA_TNIL`], [`LUA_TNUMBER`], [`LUA_TBOOLEAN`],
324 /// [`LUA_TSTRING`], [`LUA_TTABLE`], [`LUA_TFUNCTION`], [`LUA_TUSERDATA`],
325 /// [`LUA_TTHREAD`], and [`LUA_TLIGHTUSERDATA`].
326 /// *[-0, +0, -]*
327 pub fn lua_type(state: *mut lua_State, index: c_int) -> c_int;
328
329 /// Returns the name of the type encoded by the value `tp`, which must be
330 /// one the values returned by [`lua_type`].
331 /// *[-0, +0, -]*
332 pub fn lua_typename(state: *mut lua_State, tp: c_int) -> *mut c_char;
333
334 /// Pops a table from the stack and sets it as the new metatable for the
335 /// value at the given acceptable `index`.
336 /// *[-1, +0, -]*
337 pub fn lua_setmetatable(l: *mut lua_State, index: c_int) -> c_int;
338 pub fn lua_getmetatable(l: *mut lua_State, index: c_int) -> c_int;
339
340 pub fn lua_tonumber(l: *mut lua_State, index: c_int) -> lua_Number;
341 pub fn lua_tonumberx(l: *mut lua_State, index: c_int, isnum: *mut c_int) -> lua_Number;
342 pub fn lua_tointegerx(l: *mut lua_State, index: c_int, isnum: *mut c_int) -> lua_Integer;
343
344 /// Calls a function in protected mode.
345 /// *[-(nargs + 1), +(nresults|1), -]*
346 ///
347 /// Both `nargs` and `nresults` have the same meaning as in `lua_call`. If
348 /// there are no errors during the call, `lua_pcall` behaves exactly like
349 /// `lua_call`. However, if there is any error, `lua_pcall` catches it,
350 /// pushes a single value on the stack (the error message), and returns an
351 /// error code. Like lua_call, `lua_pcall` always removes the function and
352 /// its arguments from the stack.
353 ///
354 /// If `errfunc` is 0, then the error message returned on the stack is
355 /// exactly the original error message. Otherwise, `errfunc` is the stack
356 /// index of an error handler function. (In the current implementation, this
357 /// index cannot be a pseudo-index.) In case of runtime errors, this
358 /// function will be called with the error message and its return value will
359 /// be the message returned on the stack by `lua_pcall`.
360 ///
361 /// Typically, the error handler function is used to add more debug
362 /// information to the error message, such as a stack traceback. Such
363 /// information cannot be gathered after the return of `lua_pcall`, since by
364 /// then the stack has unwound.
365 ///
366 /// The `lua_pcall` function returns 0 in case of success or one of the
367 /// following error codes:
368 /// - [`LUA_ERRRUN`]: a runtime error.
369 ///
370 /// - [`LUA_ERRMEM`]: memory allocation error. For such errors, Lua does not
371 /// call the error handler function.
372 ///
373 /// - [`LUA_ERRERR`]: error while running the error handler function.
374 pub fn lua_pcall(l: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int) -> c_int;
375
376 /// Calls the C function `func` in protected mode. `func` starts with only
377 /// one element in its stack, a light userdata containing `ud`. In case of
378 /// errors, lua_cpcall returns the same error codes as lua_pcall, plus the
379 /// error object on the top of the stack; otherwise, it returns zero, and
380 /// does not change the stack. All values returned by `func` are discarded.
381 /// *[-0, +(0|1), -]*
382 pub fn lua_cpcall(l: *mut lua_State, func: lua_CFunction, ud: *mut c_void) -> c_int;
383
384 /// [-0, +1, -]
385 /// Loads a Lua chunk. If there are no errors, `lua_load` pushes the
386 /// compiled chunk as a Lua function on top of the stack. Otherwise, it
387 /// pushes an error message. The return values of `lua_load` are:
388 ///
389 /// - `0`: no errors;
390 /// - [`LUA_ERRSYNTAX`]: syntax error during pre-compilation;
391 /// - [`LUA_ERRMEM`]: memory allocation error.
392 ///
393 /// This function only loads a chunk; it does not run it.
394 ///
395 /// `lua_load` automatically detects whether the chunk is text or binary,
396 /// and loads it accordingly (see program `luac`).
397 ///
398 /// The `lua_load` function uses a user-supplied `reader` function to read
399 /// the chunk (see [`lua_Reader`]). The `data` argument is an opaque value
400 /// passed to the reader function.
401 ///
402 /// The `chunkname` argument gives a name to the chunk, which is used for
403 /// error messages and in debug information
404 pub fn lua_load(
405 l: *mut lua_State,
406 reader: lua_Reader,
407 dt: *mut libc::c_void,
408 chunkname: *const libc::c_char,
409 ) -> c_int;
410 pub fn lua_loadx(
411 l: *mut lua_State,
412 reader: lua_Reader,
413 dt: *mut libc::c_void,
414 chunkname: *const libc::c_char,
415 mode: *const libc::c_char,
416 ) -> c_int;
417 pub fn lua_dump(l: *mut lua_State, writer: lua_Writer, data: *mut libc::c_void) -> c_int;
418
419 /// Generates a Lua error. The error message (which can actually be a Lua
420 /// value of any type) must be on the stack top. This function does a long
421 /// jump, and therefore never returns. (see [`luaL_error`]).
422 /// *[-1, +0, v]*
423 pub fn lua_error(l: *mut lua_State) -> c_int;
424
425 /// Pops a key from the stack, and pushes a key-value pair from the table at
426 /// the given `index` (the "next" pair after the given key). If there are no
427 /// more elements in the table, then `lua_next` returns 0 (and pushes
428 /// nothing).
429 /// *[-1, +(2|0), e]*
430 ///
431 /// A typical traversal looks like this:
432 ///
433 /// ```
434 /// use std::ffi::CStr;
435 /// use tlua::ffi::{
436 /// lua_pushnil, lua_next, lua_typename, lua_type, lua_pop, lua_State,
437 /// };
438 /// unsafe fn print_values(l: *mut lua_State, t: i32) {
439 /// // table is in the stack at index 't'
440 /// lua_pushnil(l); // first key
441 /// while lua_next(l, t) != 0 {
442 /// // uses 'key' (at index -2) and 'value' (at index -1)
443 /// println!("{} - {}",
444 /// CStr::from_ptr(lua_typename(l, lua_type(l, -2))).to_str().unwrap(),
445 /// CStr::from_ptr(lua_typename(l, lua_type(l, -1))).to_str().unwrap(),
446 /// );
447 /// // removes 'value'; keeps 'key' for next iteration
448 /// lua_pop(l, 1);
449 /// }
450 /// }
451 /// ```
452 /// While traversing a table, do not call [`lua_tolstring`] directly on a
453 /// key, unless you know that the key is actually a string. Recall that
454 /// `lua_tolstring` changes the value at the given index; this confuses the
455 /// next call to lua_next.
456 pub fn lua_next(l: *mut lua_State, index: c_int) -> c_int;
457 pub fn lua_concat(l: *mut lua_State, n: c_int);
458 pub fn lua_len(l: *mut lua_State, index: c_int);
459
460 /// Moves the top element into the given valid `index`, shifting up the
461 /// elements above this `index` to open space. Cannot be called with a
462 /// pseudo-index, because a pseudo-index is not an actual stack position.
463 /// **[-1, +1, -]**
464 pub fn lua_insert(l: *mut lua_State, index: c_int);
465
466 /// Removes the element at the given valid index, shifting down the elements
467 /// above this index to fill the gap. Cannot be called with a pseudo-index,
468 /// because a pseudo-index is not an actual stack position.
469 /// **[-1, +0, -]**
470 pub fn lua_remove(l: *mut lua_State, index: c_int);
471
472 pub fn luaopen_base(l: *mut lua_State);
473 pub fn luaopen_bit(l: *mut lua_State);
474 pub fn luaopen_debug(l: *mut lua_State);
475 pub fn luaopen_io(l: *mut lua_State);
476 pub fn luaopen_math(l: *mut lua_State);
477 pub fn luaopen_os(l: *mut lua_State);
478 pub fn luaopen_package(l: *mut lua_State);
479 pub fn luaopen_string(l: *mut lua_State);
480 pub fn luaopen_table(l: *mut lua_State);
481
482 // lauxlib functions.
483 pub fn luaL_newstate() -> *mut lua_State;
484 pub fn luaL_register(l: *mut lua_State, libname: *const c_char, lr: *const luaL_Reg);
485
486 /// Raises an error. The error message format is given by `fmt` plus any
487 /// extra arguments, following the same rules of `lua_pushfstring`. It also
488 /// adds at the beginning of the message the file name and the line number
489 /// where the error occurred, if this information is available.
490 /// *[-0, +0, v]*
491 ///
492 /// This function never returns, but it is an idiom to use it in C functions
493 /// as return `luaL_error(args)`.
494 pub fn luaL_error(l: *mut lua_State, fmt: *const c_char, ...) -> c_int;
495 pub fn luaL_openlibs(l: *mut lua_State);
496
497 /// Creates and returns a reference, in the table at index `t`, for the
498 /// object at the top of the stack (and pops the object).
499 /// *[-1, +0, m]*
500 ///
501 /// A reference is a unique integer key. As long as you do not manually add
502 /// integer keys into table t, `luaL_ref` ensures the uniqueness of the key
503 /// it returns. You can retrieve an object referred by reference r by
504 /// calling [`lua_rawgeti`]`(l, t, r)`. Function [`luaL_unref`] frees a
505 /// reference and its associated object.
506 ///
507 /// If the object at the top of the stack is nil, `luaL_ref` returns the
508 /// constant [`LUA_REFNIL`]. The constant [`LUA_NOREF`] is guaranteed to be
509 /// different from any reference returned by `luaL_ref`.
510 pub fn luaL_ref(l: *mut lua_State, t: c_int) -> c_int;
511
512 /// Releases reference `r` from the table at index `t` (see [`luaL_ref`]).
513 /// The entry is removed from the table, so that the referred object can be
514 /// collected. The reference `r` is also freed to be used again.
515 /// *[-0, +0, -]*
516 ///
517 /// If ref is [`LUA_NOREF`] or [`LUA_REFNIL`], `luaL_unref` does nothing.
518 pub fn luaL_unref(l: *mut lua_State, t: c_int, r: c_int);
519}
520
521#[inline(always)]
522/// Pushes onto the stack the value of the global `name`.
523/// *[-0, +1, e]*
524pub unsafe fn lua_getglobal(state: *mut lua_State, name: *const c_char) {
525 lua_getfield(state, LUA_GLOBALSINDEX, name);
526}
527
528#[inline(always)]
529/// Pops a value from the stack and sets it as the new value of global `name`.
530/// *[-1, +0, e]*
531pub unsafe fn lua_setglobal(state: *mut lua_State, name: *const c_char) {
532 lua_setfield(state, LUA_GLOBALSINDEX, name);
533}
534
535#[inline(always)]
536pub unsafe fn lua_pop(state: *mut lua_State, n: c_int) {
537 lua_settop(state, -n - 1);
538}
539
540#[inline(always)]
541/// Pushes a C function onto the stack. This function receives a pointer to a C
542/// function and pushes onto the stack a Lua value of type function that, when
543/// called, invokes the corresponding C function.
544/// *[-0, +1, m]*
545///
546/// Any function to be registered in Lua must follow the correct protocol to
547/// receive its parameters and return its results (see [`lua_CFunction`]).
548pub unsafe fn lua_pushcfunction(state: *mut lua_State, f: lua_CFunction) {
549 lua_pushcclosure(state, f, 0);
550}
551
552#[inline(always)]
553pub unsafe fn lua_tostring(state: *mut lua_State, i: c_int) -> *const c_char {
554 lua_tolstring(state, i, null_mut())
555}
556
557#[inline(always)]
558/// Creates a new empty table and pushes it onto the stack. It is equivalent to
559/// [`lua_createtable`]`(l, 0, 0)`.
560/// *[-0, +1, m]*
561pub unsafe fn lua_newtable(state: *mut lua_State) {
562 lua_createtable(state, 0, 0);
563}
564
565#[inline(always)]
566/// When a C function is created, it is possible to associate some values with
567/// it, thus creating a C closure; these values are called upvalues and are
568/// accessible to the function whenever it is called (see [`lua_pushcclosure`]).
569///
570/// Whenever a C function is called, its **upvalues** are located at specific
571/// pseudo-indices. These pseudo-indices are produced by the function
572/// `lua_upvalueindex`. The first value associated with a function is at
573/// position `lua_upvalueindex(1)`, and so on. Any access to
574/// `lua_upvalueindex(n)`, where n is greater than the number of upvalues of the
575/// current function (but not greater than 256), produces an acceptable (but
576/// invalid) index.
577pub fn lua_upvalueindex(i: c_int) -> c_int {
578 LUA_GLOBALSINDEX - i
579}
580
581#[inline(always)]
582pub unsafe fn lua_isfunction(state: *mut lua_State, index: c_int) -> bool {
583 lua_type(state, index) == LUA_TFUNCTION
584}
585
586#[inline(always)]
587pub unsafe fn lua_istable(state: *mut lua_State, index: c_int) -> bool {
588 lua_type(state, index) == LUA_TTABLE
589}
590
591#[inline(always)]
592pub unsafe fn lua_islightuserdata(state: *mut lua_State, index: c_int) -> bool {
593 lua_type(state, index) == LUA_TLIGHTUSERDATA
594}
595
596#[inline(always)]
597pub unsafe fn lua_isnil(state: *mut lua_State, index: c_int) -> bool {
598 lua_type(state, index) == LUA_TNIL
599}
600
601#[inline(always)]
602pub unsafe fn lua_isboolean(state: *mut lua_State, index: c_int) -> bool {
603 lua_type(state, index) == LUA_TBOOLEAN
604}
605
606#[inline(always)]
607pub unsafe fn lua_isthread(state: *mut lua_State, index: c_int) -> bool {
608 lua_type(state, index) == LUA_TTHREAD
609}
610
611#[allow(non_snake_case)]
612#[inline(always)]
613pub unsafe fn luaL_iscdata(state: *mut lua_State, index: c_int) -> bool {
614 lua_type(state, index) == LUA_TCDATA
615}
616
617#[inline(always)]
618pub unsafe fn lua_isnumber(state: *mut lua_State, index: c_int) -> bool {
619 lua_type(state, index) == LUA_TNUMBER
620}
621
622#[inline(always)]
623pub unsafe fn lua_isnone(state: *mut lua_State, index: c_int) -> bool {
624 lua_type(state, index) == LUA_TNONE
625}
626
627#[inline(always)]
628pub unsafe fn lua_isnoneornil(state: *mut lua_State, index: c_int) -> bool {
629 lua_type(state, index) <= 0
630}
631
632#[inline(always)]
633pub unsafe fn lua_pushglobaltable(state: *mut lua_State) {
634 lua_pushvalue(state, LUA_GLOBALSINDEX)
635}
636
637pub type CTypeID = u32;
638pub const CTID_NONE: CTypeID = 0;
639pub const CTID_VOID: CTypeID = 1;
640pub const CTID_CVOID: CTypeID = 2;
641pub const CTID_BOOL: CTypeID = 3;
642pub const CTID_CCHAR: CTypeID = 4;
643pub const CTID_INT8: CTypeID = 5;
644pub const CTID_UINT8: CTypeID = 6;
645pub const CTID_INT16: CTypeID = 7;
646pub const CTID_UINT16: CTypeID = 8;
647pub const CTID_INT32: CTypeID = 9;
648pub const CTID_UINT32: CTypeID = 10;
649pub const CTID_INT64: CTypeID = 11;
650pub const CTID_UINT64: CTypeID = 12;
651pub const CTID_FLOAT: CTypeID = 13;
652pub const CTID_DOUBLE: CTypeID = 14;
653pub const CTID_COMPLEX_FLOAT: CTypeID = 15;
654pub const CTID_COMPLEX_DOUBLE: CTypeID = 16;
655pub const CTID_P_VOID: CTypeID = 17;
656pub const CTID_P_CVOID: CTypeID = 18;
657pub const CTID_P_CCHAR: CTypeID = 19;
658pub const CTID_A_CCHAR: CTypeID = 20;
659pub const CTID_CTYPEID: CTypeID = 21;
660
661extern "C-unwind" {
662 /// Push `u64` onto the stack
663 /// *[-0, +1, -]*
664 pub fn luaL_pushuint64(l: *mut lua_State, val: u64);
665
666 /// Push `i64` onto the stack
667 /// *[-0, +1, -]*
668 pub fn luaL_pushint64(l: *mut lua_State, val: i64);
669
670 /// Checks whether the argument `idx` is a `u64` or a convertable string and
671 /// returns this number.
672 /// *[-0, +0, -]*
673 ///
674 /// **Return** the converted number or 0 of argument can't be converted.
675 pub fn luaL_touint64(l: *mut lua_State, idx: c_int) -> u64;
676
677 /// Checks whether the argument `idx` is a `i64` or a convertable string and
678 /// returns this number.
679 /// *[-0, +0, -]*
680 ///
681 /// **Return** the converted number or 0 of argument can't be converted.
682 pub fn luaL_toint64(l: *mut lua_State, idx: c_int) -> i64;
683
684 /// Push cdata of given `ctypeid` onto the stack.
685 /// CTypeID must be used from FFI at least once. Allocated memory returned
686 /// uninitialized. Only numbers and pointers are supported.
687 /// - `l`: Lua State
688 /// - `ctypeid`: FFI's CTypeID of this cdata
689 ///
690 /// See also: [`luaL_checkcdata`]
691 ///
692 /// **Returns** memory associated with this cdata
693 pub fn luaL_pushcdata(l: *mut lua_State, ctypeid: CTypeID) -> *mut c_void;
694
695 /// Checks whether the function argument `idx` is a cdata
696 /// * `l`: Lua State
697 /// * `idx`: stack index
698 /// * `ctypeid`: FFI's CTypeID of this cdata
699 ///
700 /// See also: [`luaL_pushcdata`]
701 ///
702 /// **Returns** memory associated with this cdata
703 pub fn luaL_checkcdata(l: *mut lua_State, idx: c_int, ctypeid: *mut CTypeID) -> *mut c_void;
704
705 /// Return CTypeID (FFI) of given CDATA type
706 /// `ctypename` is a C type name as string (e.g. "struct request",
707 /// "uint32_t", etc.).
708 ///
709 /// See also: [`luaL_pushcdata`], [`luaL_checkcdata`]
710 pub fn luaL_ctypeid(l: *mut lua_State, ctypename: *const c_char) -> CTypeID;
711
712 /// Check if value at `index` is a lua function, a lua value with a `__call`
713 /// metamethod or a callable `cdata`.
714 pub fn luaL_iscallable(l: *mut lua_State, index: i32) -> i32;
715
716 /// Push the metafield `field` of the value at given `index` onto the stack.
717 /// Returns `1` if the value was pushed, `0` otherwise.
718 /// *[-0, +(1|0), -]*
719 pub fn luaL_getmetafield(l: *mut lua_State, index: i32, field: *const c_char) -> i32;
720}
721
722/// Check if value at given `index` has metafield `field`.
723/// *[-0, +0, -]*
724#[inline(always)]
725#[allow(non_snake_case)]
726pub unsafe fn luaL_hasmetafield(l: *mut lua_State, index: i32, field: *const c_char) -> bool {
727 luaL_getmetafield(l, index, field) != 0 && {
728 lua_pop(l, 1);
729 true
730 }
731}
732
733extern "C-unwind" {
734 /// Convert the value at `idx` to string using `__tostring` metamethod if
735 /// other measures didn't work and return it. Sets the `len` if it's not
736 /// `NULL`. The newly created string is left on top of the stack.
737 /// *[-0, +1, m]*
738 pub fn luaT_tolstring(l: *mut lua_State, idx: c_int, len: *mut usize) -> *const c_char;
739}