Skip to main content

lua_types/
userdata.rs

1//! `LuaUserData` — Lua's heap-allocated userdata. Carries a typed byte
2//! buffer plus optional user values (a Vec of TValues).
3
4use std::cell::RefCell;
5
6use crate::gc::GcRef;
7use crate::table::LuaTable;
8use crate::value::LuaValue;
9
10#[derive(Debug)]
11pub struct LuaUserData {
12    pub data: Box<[u8]>,
13    pub uv: Vec<LuaValue>,
14    pub metatable: RefCell<Option<GcRef<LuaTable>>>,
15}
16
17impl LuaUserData {
18    pub fn placeholder() -> Self {
19        LuaUserData {
20            data: Box::new([]),
21            uv: Vec::new(),
22            metatable: RefCell::new(None),
23        }
24    }
25
26    pub fn metatable(&self) -> Option<GcRef<LuaTable>> {
27        self.metatable.borrow().clone()
28    }
29
30    pub fn set_metatable(&self, mt: Option<GcRef<LuaTable>>) {
31        *self.metatable.borrow_mut() = mt;
32    }
33}
34
35// ──────────────────────────────────────────────────────────────────────────────
36// PORT STATUS
37//   source:        src/lobject.h (Udata + Udata0)
38//   target_crate:  lua-types
39//   confidence:    high
40//   todos:         0
41//   port_notes:    0
42//   unsafe_blocks: 0
43//   notes:         LuaUserData type, including metatable slot and uservalues. C uses a
44//                  flexible-array trailing payload; we use a typed Vec / Box of the
45//                  user payload + uservalues vector.
46// ──────────────────────────────────────────────────────────────────────────────