1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! Public API traits for Lua operations.
//!
//! Provides `LuaApi` for immutable operations and `LuaApiMut` for mutable operations.
use std::any::Any;
use crate::conversion::{FromLua, IntoLua};
use crate::error::{LuaError, LuaResult, RuntimeError};
use crate::handles::{AnyUserData, Function, Table};
use crate::vm::closure::{Closure, LuaClosure, RustClosure, RustFn};
use crate::vm::proto::ProtoRef;
use crate::vm::state::LuaState;
use crate::vm::value::Val;
/// Read-only API trait for Lua operations.
///
/// Provides immutable access to Lua state for operations that don't modify state.
pub trait LuaApi {
/// Returns an immutable reference to the underlying `LuaState`.
fn state(&self) -> &LuaState;
/// Returns the total memory in use by Lua (in bytes).
fn gc_count(&self) -> usize {
self.state().gc.gc_state.total_bytes
}
/// Raw get on a table handle via the public API.
fn table_raw_get(&self, table: &Table, key: Val) -> LuaResult<Val> {
table.raw_get(self.state(), key)
}
/// Returns the raw length of a table (no `__len` metamethod).
fn table_raw_len(&self, table: &Table) -> i64 {
table.raw_len(self.state())
}
}
/// Mutable API trait for Lua operations.
///
/// Extends `LuaApi` with operations that require mutable access to state.
pub trait LuaApiMut: LuaApi {
/// Returns a mutable reference to the underlying `LuaState`.
fn state_mut(&mut self) -> &mut LuaState;
// -----------------------------------------------------------------------
// Globals
// -----------------------------------------------------------------------
/// Gets a global variable, converting it to the requested Rust type.
fn global<V: FromLua>(&mut self, name: &str) -> LuaResult<V>
where
Self: Sized,
{
let val = self.get_global_val(name);
V::from_lua(val, self)
}
/// Sets a global variable from a Rust value.
fn set_global<V: IntoLua>(&mut self, name: &str, value: V) -> LuaResult<()>
where
Self: Sized,
{
let val = value.into_lua(self)?;
self.set_global_val(name, val)
}
/// Reads a value from the global table by name.
fn get_global_val(&mut self, name: &str) -> Val {
let state = self.state_mut();
let key_ref = state.gc.intern_string(name.as_bytes());
let Some(global_table) = state.gc.tables.get(state.global) else {
return Val::Nil;
};
global_table.get_str(key_ref, &state.gc.string_arena)
}
/// Sets a value in the global table by name.
fn set_global_val(&mut self, name: &str, val: Val) -> LuaResult<()> {
let state = self.state_mut();
let key_ref = state.gc.intern_string(name.as_bytes());
let key = Val::Str(key_ref);
let global = state.global;
let table = state.gc.tables.get_mut(global).ok_or_else(|| {
LuaError::Runtime(RuntimeError {
message: "global table not found".into(),
level: 0,
traceback: vec![],
})
})?;
table.raw_set(key, val, &state.gc.string_arena)
}
// -----------------------------------------------------------------------
// Table creation
// -----------------------------------------------------------------------
/// Allocates a new empty table and returns a handle.
fn create_table(&mut self) -> Table {
let state = self.state_mut();
let r = state.gc.alloc_table(crate::vm::table::Table::new());
Table(r)
}
// -----------------------------------------------------------------------
// Userdata creation
// -----------------------------------------------------------------------
/// Creates a new userdata containing `data` with no metatable.
///
/// With the `send` feature enabled, `T` must also implement `Send`.
#[cfg(not(feature = "send"))]
fn create_userdata<T: Any>(&mut self, data: T) -> AnyUserData {
let state = self.state_mut();
let ud = crate::vm::value::Userdata::new(Box::new(data));
let r = state.gc.alloc_userdata(ud);
AnyUserData(r)
}
/// Creates a new userdata containing `data` with no metatable.
///
/// With the `send` feature, `T` must implement `Send` so the `Lua`
/// instance remains thread-safe.
#[cfg(feature = "send")]
fn create_userdata<T: Any + Send>(&mut self, data: T) -> AnyUserData {
let state = self.state_mut();
let ud = crate::vm::value::Userdata::new(Box::new(data));
let r = state.gc.alloc_userdata(ud);
AnyUserData(r)
}
/// Creates a new userdata with a named, registry-cached metatable.
#[cfg(not(feature = "send"))]
fn create_typed_userdata<T: Any>(
&mut self,
data: T,
type_name: &str,
) -> LuaResult<AnyUserData> {
let state = self.state_mut();
let mt = crate::stdlib::new_metatable(state, type_name)?;
let ud = crate::vm::value::Userdata::with_metatable(Box::new(data), mt);
let r = state.gc.alloc_userdata(ud);
Ok(AnyUserData(r))
}
/// Creates a new userdata with a named, registry-cached metatable
/// (thread-safe variant).
#[cfg(feature = "send")]
fn create_typed_userdata<T: Any + Send>(
&mut self,
data: T,
type_name: &str,
) -> LuaResult<AnyUserData> {
let state = self.state_mut();
let mt = crate::stdlib::new_metatable(state, type_name)?;
let ud = crate::vm::value::Userdata::with_metatable(Box::new(data), mt);
let r = state.gc.alloc_userdata(ud);
Ok(AnyUserData(r))
}
/// Creates or retrieves a named metatable for a userdata type.
fn create_userdata_metatable(&mut self, type_name: &str) -> LuaResult<Table> {
let state = self.state_mut();
let mt = crate::stdlib::new_metatable(state, type_name)?;
Ok(Table(mt))
}
// -----------------------------------------------------------------------
// Function registration
// -----------------------------------------------------------------------
/// Registers a Rust function as a global Lua function.
fn register_function(&mut self, name: &str, func: RustFn) -> LuaResult<()> {
let state = self.state_mut();
let closure = Closure::Rust(RustClosure::new(func, name));
let closure_ref = state.gc.alloc_closure(closure);
self.set_global_val(name, Val::Function(closure_ref))
}
// -----------------------------------------------------------------------
// GC control
// -----------------------------------------------------------------------
/// Runs a full garbage collection cycle.
fn gc_collect(&mut self) -> LuaResult<()> {
self.state_mut().full_gc()
}
/// Stops the garbage collector.
fn gc_stop(&mut self) {
self.state_mut().gc.gc_state.gc_threshold = usize::MAX;
}
/// Restarts the garbage collector.
fn gc_restart(&mut self) {
let state = self.state_mut();
state.gc.gc_state.gc_threshold = state.gc.gc_state.total_bytes;
}
/// Performs an incremental GC step.
fn gc_step(&mut self, step_size: i64) -> LuaResult<bool> {
self.state_mut().gc_step(step_size)
}
/// Sets the GC pause parameter (percentage). Returns the previous value.
fn gc_set_pause(&mut self, pause: u32) -> u32 {
let state = self.state_mut();
let old = state.gc.gc_state.gc_pause;
state.gc.gc_state.gc_pause = pause;
old
}
/// Sets the GC step multiplier. Returns the previous value.
fn gc_set_step_multiplier(&mut self, stepmul: u32) -> u32 {
let state = self.state_mut();
let old = state.gc.gc_state.gc_stepmul;
state.gc.gc_state.gc_stepmul = stepmul;
old
}
// -----------------------------------------------------------------------
// String creation
// -----------------------------------------------------------------------
/// Interns a byte string via the GC string table, returning `Val::Str`.
fn create_string(&mut self, s: &[u8]) -> Val {
let state = self.state_mut();
let str_ref = state.gc.intern_string(s);
Val::Str(str_ref)
}
// -----------------------------------------------------------------------
// Table operations
// -----------------------------------------------------------------------
/// Raw set on a table handle via the public API.
fn table_raw_set(&mut self, table: &Table, key: Val, value: Val) -> LuaResult<()> {
table.raw_set(self.state_mut(), key, value)
}
/// Sets a named Rust function on a table.
fn table_set_function(&mut self, table: &Table, name: &str, func: RustFn) -> LuaResult<()> {
let state = self.state_mut();
let key = Val::Str(state.gc.intern_string(name.as_bytes()));
let closure = Closure::Rust(RustClosure::new(func, name));
let closure_ref = state.gc.alloc_closure(closure);
table.raw_set(state, key, Val::Function(closure_ref))
}
// -----------------------------------------------------------------------
// Compilation and loading
// -----------------------------------------------------------------------
/// Compiles Lua source bytes (or loads a binary chunk) and returns a function handle.
fn load_bytes(&mut self, source: &[u8], name: &str) -> LuaResult<Function> {
let proto = crate::compile_or_undump(source, name)?;
let mut proto = ProtoRef::try_unwrap(proto).unwrap_or_else(|rc| (*rc).clone());
let state = self.state_mut();
crate::patch_string_constants(&mut proto, &mut state.gc);
let proto = ProtoRef::new(proto);
let num_upvalues = proto.num_upvalues as usize;
let mut lua_cl = LuaClosure::new(proto, state.global);
for _ in 0..num_upvalues {
let uv = crate::vm::closure::Upvalue::new_closed(Val::Nil);
let uv_ref = state.gc.alloc_upvalue(uv);
lua_cl.upvalues.push(uv_ref);
}
let closure_ref = state.gc.alloc_closure(Closure::Lua(lua_cl));
Ok(Function(closure_ref))
}
/// Compiles a Lua source string and returns a function handle.
fn load(&mut self, source: &str) -> LuaResult<Function> {
self.load_bytes(source.as_bytes(), "=(string)")
}
}