Skip to content

Commit 386b5b6

Browse files
committed
feat(lua_ffi): add luaL_ref related stuff
1 parent e61b471 commit 386b5b6

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

hlua/ffi/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ use std::ptr::null_mut;
88
/// 2. lauxlib
99
/// 3. Lua utitlites, implemented in Tarantool
1010
11+
/// Lua provides a registry, a pre-defined table that can be used by any C code
12+
/// to store whatever Lua value it needs to store. This table is always located
13+
/// at pseudo-index `LUA_REGISTRYINDEX`. Any C library can store data into this
14+
/// table, but it should take care to choose keys different from those used by
15+
/// other libraries, to avoid collisions. Typically, you should use as key a
16+
/// string containing your library name or a light userdata with the address of
17+
/// a C object in your code.
18+
///
19+
/// The integer keys in the registry are used by the reference mechanism,
20+
/// implemented by the auxiliary library, and therefore should not be used for
21+
/// other purposes.
1122
pub const LUA_REGISTRYINDEX: c_int = -10000;
1223
pub const LUA_ENVIRONINDEX: c_int = -10001;
1324
pub const LUA_GLOBALSINDEX: c_int = -10002;
@@ -33,6 +44,9 @@ pub const LUA_TTHREAD: c_int = 8;
3344

3445
pub const LUA_MINSTACK: c_int = 20;
3546

47+
pub const LUA_NOREF: c_int = -2;
48+
pub const LUA_REFNIL: c_int = -1;
49+
3650
#[repr(C)]
3751
#[derive(Debug, Copy, Clone)]
3852
pub struct lua_State {
@@ -321,6 +335,29 @@ extern "C" {
321335
/// as return `luaL_error(args)`.
322336
pub fn luaL_error(l: *mut lua_State, fmt: *const c_schar, ...) -> c_int;
323337
pub fn luaL_openlibs(L: *mut lua_State);
338+
339+
/// Creates and returns a reference, in the table at index `t`, for the
340+
/// object at the top of the stack (and pops the object).
341+
/// *[-1, +0, m]*
342+
///
343+
/// A reference is a unique integer key. As long as you do not manually add
344+
/// integer keys into table t, `luaL_ref` ensures the uniqueness of the key
345+
/// it returns. You can retrieve an object referred by reference r by
346+
/// calling [`lua_rawgeti`]`(L, t, r)`. Function [`luaL_unref`] frees a
347+
/// reference and its associated object.
348+
///
349+
/// If the object at the top of the stack is nil, `luaL_ref` returns the
350+
/// constant [`LUA_REFNIL`]. The constant [`LUA_NOREF`] is guaranteed to be
351+
/// different from any reference returned by `luaL_ref`.
352+
pub fn luaL_ref(l: *mut lua_State, t: c_int) -> c_int;
353+
354+
/// Releases reference `r` from the table at index `t` (see [`luaL_ref`]).
355+
/// The entry is removed from the table, so that the referred object can be
356+
/// collected. The reference `r` is also freed to be used again.
357+
/// *[-0, +0, -]*
358+
///
359+
/// If ref is [`LUA_NOREF`] or [`LUA_REFNIL`], `luaL_unref` does nothing.
360+
pub fn luaL_unref(l: *mut lua_State, t: c_int, r: c_int);
324361
}
325362

326363
#[inline(always)]

0 commit comments

Comments
 (0)