Skip to main content

lua_types/
proto.rs

1//! `LuaProto` — compiled function prototype. Mirrors C-Lua's `Proto` struct
2//! but uses Rust idioms (Vec instead of pointer+size pairs).
3
4use crate::closure::LuaLClosure;
5use crate::gc::GcRef;
6use crate::opcode::Instruction;
7use crate::string::LuaString;
8use crate::value::LuaValue;
9use core::cell::RefCell;
10
11#[derive(Debug)]
12pub struct LuaProto {
13    pub numparams: u8,
14    pub is_vararg: bool,
15    pub maxstacksize: u8,
16    pub upvalues: Vec<UpvalDesc>,
17    pub k: Vec<LuaValue>,
18    pub code: Vec<Instruction>,
19    pub p: Vec<GcRef<LuaProto>>,
20    pub lineinfo: Vec<i8>,
21    pub abslineinfo: Vec<AbsLineInfo>,
22    pub locvars: Vec<LocalVar>,
23    pub linedefined: i32,
24    pub lastlinedefined: i32,
25    pub source: Option<GcRef<LuaString>>,
26    /// Last closure instantiated from this proto, reused by `OP_CLOSURE` when a
27    /// new instantiation would capture the identical upvalues. Mirrors C-Lua's
28    /// `Proto.cache` (5.2/5.3 only — added in 5.2, removed in 5.4), which is why
29    /// loop-built closures with shared upvalues compare `==` on those versions.
30    /// Populated only under 5.2/5.3 in `push_closure`; `None` otherwise. Traced
31    /// (so it cannot dangle); unlike C's GC-cleared weak cache this pins the one
32    /// cached closure to the proto's lifetime, which is bounded and safe.
33    pub cache: RefCell<Option<GcRef<LuaLClosure>>>,
34    /// Lua 5.5 named varargs (`function f(...t)`): the register holding the
35    /// packed vararg table `t`. When set, `...` unpacks live from that table
36    /// (count = its `n` field) rather than the frame's extra-arg slots, so
37    /// mutating `t` is observable through a later `...` (shared storage). `None`
38    /// for ordinary `...` and all pre-5.5 functions. Mirrors upstream's
39    /// `needvatab` proto flag + the vararg-table register.
40    pub vararg_table_reg: Option<u8>,
41}
42
43impl LuaProto {
44    pub fn placeholder() -> Self {
45        LuaProto {
46            numparams: 0,
47            is_vararg: false,
48            maxstacksize: 2,
49            upvalues: Vec::new(),
50            k: Vec::new(),
51            code: Vec::new(),
52            p: Vec::new(),
53            lineinfo: Vec::new(),
54            abslineinfo: Vec::new(),
55            locvars: Vec::new(),
56            linedefined: 0,
57            lastlinedefined: 0,
58            source: None,
59            cache: RefCell::new(None),
60            vararg_table_reg: None,
61        }
62    }
63}
64
65#[derive(Debug, Clone)]
66pub struct UpvalDesc {
67    pub name: Option<GcRef<LuaString>>,
68    pub instack: bool,
69    pub idx: u8,
70    pub kind: u8,
71}
72
73#[derive(Debug, Clone)]
74pub struct LocalVar {
75    pub varname: GcRef<LuaString>,
76    pub startpc: i32,
77    pub endpc: i32,
78}
79
80#[derive(Debug, Clone, Copy)]
81pub struct AbsLineInfo {
82    pub pc: i32,
83    pub line: i32,
84}
85
86// ──────────────────────────────────────────────────────────────────────────────
87// PORT STATUS
88//   source:        src/lobject.h (Proto struct)
89//   target_crate:  lua-types
90//   confidence:    high
91//   todos:         0
92//   port_notes:    0
93//   unsafe_blocks: 0
94//   notes:         Function prototype: bytecode, constants, line info, debug info,
95//                  upvalue descriptors. Faithful layout of C's Proto struct using
96//                  Vec<T> in place of T*+size pairs.
97// ──────────────────────────────────────────────────────────────────────────────