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;
25
26// ── Top-level re-exports (most consumers use these flat names) ──────────
27pub use closure::{LuaClosure, LuaLClosure};
28pub use error::{LuaError, LuaExit};
29pub use filehandle::LuaFileHandle;
30pub use gc::GcRef;
31pub use proto::{AbsLineInfo, LocalVar, LuaProto, UpvalDesc};
32pub use status::LuaStatus;
33pub use string::LuaString;
34pub use table::LuaTable;
35pub use upval::{UpVal, UpValState};
36pub use userdata::LuaUserData;
37pub use value::{F2Imod, LuaValue};
38
39// ── Top-level newtypes ──────────────────────────────────────────────────
40
41/// Index into the Lua value stack. **Never a pointer or borrow.** Stack
42/// reallocates; only indices are stable across mutations.
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
44pub struct StackIdx(pub u32);
45
46impl StackIdx {
47    pub const ZERO: Self = StackIdx(0);
48    #[inline(always)]
49    pub fn get(self) -> u32 { self.0 }
50    #[inline(always)]
51    pub fn as_usize(self) -> usize { self.0 as usize }
52}
53
54impl std::ops::Add<i32> for StackIdx {
55    type Output = Self;
56    #[inline(always)]
57    fn add(self, rhs: i32) -> Self { StackIdx((self.0 as i32 + rhs) as u32) }
58}
59impl std::ops::Sub<i32> for StackIdx {
60    type Output = Self;
61    #[inline(always)]
62    fn sub(self, rhs: i32) -> Self { StackIdx((self.0 as i32 - rhs) as u32) }
63}
64impl std::ops::Sub<StackIdx> for StackIdx {
65    type Output = i32;
66    #[inline(always)]
67    fn sub(self, rhs: StackIdx) -> i32 { self.0 as i32 - rhs.0 as i32 }
68}
69
70/// Index into the call-info stack.
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
72pub struct CallInfoIdx(pub u32);
73
74impl CallInfoIdx {
75    pub const ZERO: Self = CallInfoIdx(0);
76    #[inline(always)]
77    pub fn get(self) -> u32 { self.0 }
78    #[inline(always)]
79    pub fn as_usize(self) -> usize { self.0 as usize }
80}
81
82/// The base type tag for a Lua value. Matches C-Lua's LUA_T* constants.
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
84#[repr(i8)]
85pub enum LuaType {
86    None          = -1,
87    Nil           = 0,
88    Boolean       = 1,
89    LightUserData = 2,
90    Number        = 3,
91    String        = 4,
92    Table         = 5,
93    Function      = 6,
94    UserData      = 7,
95    Thread        = 8,
96}
97
98impl LuaType {
99    pub const NUM_TYPES: usize = 9;
100}
101
102// ──────────────────────────────────────────────────────────────────────────
103// PORT STATUS
104//   source:        (foundation — designed in Phase B, not ported from .c)
105//   target_crate:  lua-types
106//   confidence:    high
107//   todos:         many — submodule impls are stubs
108//   port_notes:    0
109//   unsafe_blocks: 0
110//   notes:         shapes match what Phase A files reference. Method bodies
111//                  panic with todo!() / unimplemented!(); compile-fixer
112//                  iterations will land real impls.
113// ──────────────────────────────────────────────────────────────────────────