Skip to main content

lua_types/
lib.rs

1//! Lua value types, error types, and shared newtypes.
2//!
3//! Phase B foundation: this crate defines the types referenced by all Phase A
4//! files. Implementations are stubs (`todo!()`) where they exist; the goal is
5//! that `use lua_types::...` imports resolve so `cargo check` can surface real
6//! errors instead of name-resolution noise.
7//!
8//! See `PORT_STRATEGY.md` §3 for the design decisions encoded here.
9
10pub mod arith;
11pub mod closure;
12pub mod error;
13pub mod filehandle;
14pub mod gc;
15pub mod opcode;
16pub mod proto;
17pub mod status;
18pub mod string;
19pub mod table;
20pub mod tagmethod;
21pub mod trace_impls;
22pub mod upval;
23pub mod userdata;
24pub mod value;
25pub mod version;
26
27// ── Top-level re-exports (most consumers use these flat names) ──────────
28pub use closure::{LuaClosure, LuaLClosure};
29pub use error::{LuaError, LuaExit, LuaThreadClose};
30pub use filehandle::LuaFileHandle;
31pub use gc::GcRef;
32pub use proto::{AbsLineInfo, LocalVar, LuaProto, UpvalDesc};
33pub use status::LuaStatus;
34pub use string::LuaString;
35pub use table::LuaTable;
36pub use upval::{UpVal, UpValState};
37pub use userdata::LuaUserData;
38pub use value::{F2Imod, LuaValue};
39pub use version::{LuaVersion, NumberModel};
40
41// ── Top-level newtypes ──────────────────────────────────────────────────
42
43/// Index into the Lua value stack. **Never a pointer or borrow.** Stack
44/// reallocates; only indices are stable across mutations.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
46pub struct StackIdx(pub u32);
47
48impl StackIdx {
49    pub const ZERO: Self = StackIdx(0);
50    #[inline(always)]
51    pub fn get(self) -> u32 {
52        self.0
53    }
54    #[inline(always)]
55    pub fn as_usize(self) -> usize {
56        self.0 as usize
57    }
58}
59
60impl std::ops::Add<i32> for StackIdx {
61    type Output = Self;
62    #[inline(always)]
63    fn add(self, rhs: i32) -> Self {
64        StackIdx((self.0 as i32 + rhs) as u32)
65    }
66}
67impl std::ops::Sub<i32> for StackIdx {
68    type Output = Self;
69    #[inline(always)]
70    fn sub(self, rhs: i32) -> Self {
71        StackIdx((self.0 as i32 - rhs) as u32)
72    }
73}
74impl std::ops::Sub<StackIdx> for StackIdx {
75    type Output = i32;
76    #[inline(always)]
77    fn sub(self, rhs: StackIdx) -> i32 {
78        self.0 as i32 - rhs.0 as i32
79    }
80}
81
82/// Index into the call-info stack.
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
84pub struct CallInfoIdx(pub u32);
85
86impl CallInfoIdx {
87    pub const ZERO: Self = CallInfoIdx(0);
88    #[inline(always)]
89    pub fn get(self) -> u32 {
90        self.0
91    }
92    #[inline(always)]
93    pub fn as_usize(self) -> usize {
94        self.0 as usize
95    }
96}
97
98/// The base type tag for a Lua value. Matches C-Lua's LUA_T* constants.
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
100#[repr(i8)]
101pub enum LuaType {
102    None = -1,
103    Nil = 0,
104    Boolean = 1,
105    LightUserData = 2,
106    Number = 3,
107    String = 4,
108    Table = 5,
109    Function = 6,
110    UserData = 7,
111    Thread = 8,
112}
113
114impl LuaType {
115    pub const NUM_TYPES: usize = 9;
116}
117
118// ──────────────────────────────────────────────────────────────────────────
119// PORT STATUS
120//   source:        (foundation — designed in Phase B, not ported from .c)
121//   target_crate:  lua-types
122//   confidence:    high
123//   todos:         many — submodule impls are stubs
124//   port_notes:    0
125//   unsafe_blocks: 0
126//   notes:         shapes match what Phase A files reference. Method bodies
127//                  panic with todo!() / unimplemented!(); compile-fixer
128//                  iterations will land real impls.
129// ──────────────────────────────────────────────────────────────────────────