Skip to main content

lua_stdlib/
state_stub.rs

1//! Phase-B reconcile shim: re-exports the canonical `LuaState` from
2//! `lua-vm` and provides an extension trait holding every method the
3//! Phase-A stdlib translation used to call on the Phase-A stub.
4//!
5//! TODO_ARCH(phase-b-reconcile): all extension-trait method bodies are
6//! `todo!("phase-b-reconcile: <name>")`. They must move to real
7//! implementations on `lua_vm::state::LuaState` itself; that work lives in
8//! `lua-vm`, not here. The shim exists only so stdlib code keeps compiling
9//! while the canonical `LuaState` API stabilises.
10//!
11//! Where a trait method's name collides with an inherent method on the
12//! canonical `LuaState`, Rust resolves to the inherent method. Most
13//! Phase-A call sites compile through the inherent method unchanged; the
14//! handful that depend on a different return shape (e.g. `state.push(...)?`
15//! against the canonical `pub fn push(&mut self, val: LuaValue)`) are
16//! patched at the call site.
17
18#![allow(dead_code, unused_variables, clippy::too_many_arguments)]
19
20use lua_types::{
21    arith::ArithOp,
22    closure::{LuaCFnPtr, LuaClosure},
23    error::LuaError,
24    gc::GcRef,
25    string::LuaString,
26    userdata::LuaUserData,
27    value::{LuaThread, LuaValue},
28    CallInfoIdx,
29    LuaType,
30    LuaStatus,
31};
32
33pub use lua_vm::state::LuaState;
34
35/// Bare function callable from Lua. C: `lua_CFunction`.
36#[allow(non_camel_case_types)]
37pub type lua_CFunction = fn(&mut LuaState) -> Result<usize, LuaError>;
38
39/// Pseudo-index for the `i`-th upvalue of a C function.
40/// C: `#define lua_upvalueindex(i)`
41pub fn upvalue_index(i: i32) -> i32 {
42    -1_001_000 - i
43}
44
45/// Comparison operations (eq, lt, le). C: `LUA_OP*`.
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum CompareOp {
48    Eq,
49    Lt,
50    Le,
51}
52
53/// Reader-callback type for `lua_load`. C: `lua_Reader`.
54pub type LuaReader<'a> = dyn FnMut() -> Option<Vec<u8>> + 'a;
55
56/// Writer-callback type for `lua_dump`. C: `lua_Writer`.
57pub type LuaWriter<'a> = dyn FnMut(&[u8]) -> Result<(), LuaError> + 'a;
58
59/// Debug introspection record. C: `lua_Debug`.
60#[derive(Debug, Default, Clone)]
61pub struct LuaDebug {
62    pub name: Option<Vec<u8>>,
63    pub namewhat: Vec<u8>,
64    pub what: u8,
65    pub source: Vec<u8>,
66    pub short_src: Vec<u8>,
67    pub linedefined: i32,
68    pub lastlinedefined: i32,
69    pub currentline: i32,
70    pub nups: u8,
71    pub nparams: u8,
72    pub isvararg: bool,
73    pub istailcall: bool,
74    pub ftransfer: u16,
75    pub ntransfer: u16,
76    /// Active CallInfo index, set by `get_stack`/`get_stack_level` and read by
77    /// `get_info`/`get_local_at`/`set_local_at`. Mirrors C's `lua_Debug.i_ci`
78    /// (a raw pointer in C; an index here).
79    pub(crate) i_ci_idx: Option<CallInfoIdx>,
80}
81
82impl LuaDebug {
83    pub fn name_bytes(&self) -> &[u8] { self.name.as_deref().unwrap_or(b"?") }
84    pub fn namewhat_bytes(&self) -> &[u8] { &self.namewhat }
85    pub fn what_bytes(&self) -> &[u8] { match self.what { b'L' => b"Lua", b'C' => b"C", b'm' => b"main", _ => b"?" } }
86    pub fn short_src_bytes(&self) -> &[u8] { &self.short_src }
87    pub fn source_bytes(&self) -> &[u8] { &self.source }
88}
89
90/// Extension trait wiring every Phase-A stub method onto the canonical
91/// `LuaState`. Bodies are `todo!("phase-b-reconcile: …")`. When the
92/// canonical type already defines a method with the same name, Rust's
93/// inherent-first resolution makes this trait method unreachable (the
94/// inherent one wins) — the trait is then only providing the *missing*
95/// methods. Conflicting call-sites whose shape no longer matches the
96/// inherent signature are patched in their respective stdlib modules.
97pub trait LuaStateStubExt {
98    fn push_value(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_value") }
99    fn push_copy(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_copy") }
100    fn push_string(&mut self, s: &[u8]) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_string") }
101    fn push_bytes(&mut self, s: &[u8]) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_bytes") }
102    fn push_fstring(&mut self, args: std::fmt::Arguments<'_>) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_fstring") }
103    fn push_c_function(&mut self, f: lua_CFunction) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_c_function") }
104    fn push_c_closure(&mut self, f: lua_CFunction, n: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_c_closure") }
105    fn push_where(&mut self, level: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_where") }
106    fn push_globals(&mut self) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_globals") }
107
108    fn pop_bytes(&mut self) -> Vec<u8> { todo!("phase-b-reconcile: pop_bytes") }
109
110    fn top(&mut self) -> i32 { todo!("phase-b-reconcile: top") }
111    fn top_count(&mut self) -> i32 { todo!("phase-b-reconcile: top_count") }
112
113    fn insert(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: insert") }
114    fn remove(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: remove") }
115    fn replace(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: replace") }
116    fn rotate(&mut self, idx: i32, n: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: rotate") }
117    fn copy_value(&mut self, from: i32, to: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: copy_value") }
118    fn abs_index(&mut self, idx: i32) -> i32 { todo!("phase-b-reconcile: abs_index") }
119    fn ensure_stack<S: AsRef<[u8]> + ?Sized>(&mut self, n: i32, msg: &S) -> Result<(), LuaError> { let _ = msg.as_ref(); todo!("phase-b-reconcile: ensure_stack") }
120    fn check_stack_space(&mut self, n: i32) -> bool { todo!("phase-b-reconcile: check_stack_space") }
121
122    fn type_at(&mut self, idx: i32) -> LuaType { todo!("phase-b-reconcile: type_at") }
123    fn type_name(&mut self, t: LuaType) -> &'static [u8] { todo!("phase-b-reconcile: type_name") }
124    fn type_name_at(&mut self, idx: i32) -> &'static [u8] { todo!("phase-b-reconcile: type_name_at") }
125    fn value_at(&mut self, idx: i32) -> LuaValue { todo!("phase-b-reconcile: value_at") }
126    fn is_none_or_nil(&mut self, idx: i32) -> bool { todo!("phase-b-reconcile: is_none_or_nil") }
127    fn is_integer(&mut self, idx: i32) -> bool { todo!("phase-b-reconcile: is_integer") }
128    fn is_number(&mut self, idx: i32) -> bool { todo!("phase-b-reconcile: is_number") }
129
130    fn to_lua_string(&mut self, idx: i32) -> Option<GcRef<LuaString>> { todo!("phase-b-reconcile: to_lua_string") }
131    fn to_lua_string_bytes(&mut self, idx: i32) -> Option<Vec<u8>> { todo!("phase-b-reconcile: to_lua_string_bytes") }
132    fn to_lua_string_len(&mut self, idx: i32) -> Option<usize> { todo!("phase-b-reconcile: to_lua_string_len") }
133    fn to_integer_x(&mut self, idx: i32) -> Option<i64> { todo!("phase-b-reconcile: to_integer_x") }
134    fn to_number_x(&mut self, idx: i32) -> Option<f64> { todo!("phase-b-reconcile: to_number_x") }
135    fn to_boolean(&mut self, idx: i32) -> bool { todo!("phase-b-reconcile: to_boolean") }
136    fn to_userdata(&mut self, idx: i32) -> Option<GcRef<LuaUserData>> { todo!("phase-b-reconcile: to_userdata") }
137    fn to_display_string(&mut self, idx: i32) -> Result<Vec<u8>, LuaError> { todo!("phase-b-reconcile: to_display_string") }
138
139    fn check_arg_any(&mut self, arg: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: check_arg_any") }
140    fn check_arg_integer(&mut self, arg: i32) -> Result<i64, LuaError> { todo!("phase-b-reconcile: check_arg_integer") }
141    fn check_arg_string(&mut self, arg: i32) -> Result<Vec<u8>, LuaError> { todo!("phase-b-reconcile: check_arg_string") }
142    fn check_arg_type(&mut self, arg: i32, t: LuaType) -> Result<(), LuaError> { todo!("phase-b-reconcile: check_arg_type") }
143    fn check_arg_option(&mut self, arg: i32, def: Option<&[u8]>, lst: &[&[u8]]) -> Result<usize, LuaError> { todo!("phase-b-reconcile: check_arg_option") }
144
145    fn opt_arg_integer(&mut self, arg: i32, def: i64) -> Result<i64, LuaError> { todo!("phase-b-reconcile: opt_arg_integer") }
146    fn opt_arg_string_bytes(&mut self, arg: i32) -> Result<Vec<u8>, LuaError> { todo!("phase-b-reconcile: opt_arg_string_bytes") }
147    fn opt_arg_string(&mut self, arg: i32, def: &[u8]) -> Result<Vec<u8>, LuaError> { todo!("phase-b-reconcile: opt_arg_string") }
148    fn arg_to_bool(&mut self, arg: i32) -> bool { todo!("phase-b-reconcile: arg_to_bool") }
149
150    fn get_field(&mut self, idx: i32, k: &[u8]) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: get_field") }
151    fn set_field(&mut self, idx: i32, k: &[u8]) -> Result<(), LuaError> { todo!("phase-b-reconcile: set_field") }
152    fn raw_get(&mut self, idx: i32) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: raw_get") }
153    fn raw_set(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: raw_set") }
154    fn raw_get_i(&mut self, idx: i32, n: i64) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: raw_get_i") }
155    fn raw_set_i(&mut self, idx: i32, n: i64) -> Result<(), LuaError> { todo!("phase-b-reconcile: raw_set_i") }
156    fn raw_equal(&mut self, idx1: i32, idx2: i32) -> Result<bool, LuaError> { todo!("phase-b-reconcile: raw_equal") }
157    fn raw_len(&mut self, idx: i32) -> i64 { todo!("phase-b-reconcile: raw_len") }
158    fn get_i(&mut self, idx: i32, n: i64) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: get_i") }
159    fn get_metafield(&mut self, idx: i32, name: &[u8]) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: get_metafield") }
160    fn get_meta_field(&mut self, idx: i32, name: &[u8]) -> Result<bool, LuaError> { todo!("phase-b-reconcile: get_meta_field") }
161    fn get_metatable(&mut self, idx: i32) -> Result<bool, LuaError> { todo!("phase-b-reconcile: get_metatable") }
162    fn set_metatable(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: set_metatable") }
163    fn table_next(&mut self, idx: i32) -> Result<bool, LuaError> { todo!("phase-b-reconcile: table_next") }
164    fn create_table(&mut self, narr: i32, nrec: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: create_table") }
165
166    fn gc_control_simple(&mut self, op: i32) -> Result<i32, LuaError> { todo!("phase-b-reconcile: gc_control_simple") }
167    fn gc_count(&mut self) -> Result<i32, LuaError> { todo!("phase-b-reconcile: gc_count") }
168    fn gc_count_b(&mut self) -> Result<i32, LuaError> { todo!("phase-b-reconcile: gc_count_b") }
169    fn gc_step(&mut self, data: i32) -> Result<i32, LuaError> { todo!("phase-b-reconcile: gc_step") }
170    fn gc_set_param(&mut self, op: i32, value: i32) -> Result<i32, LuaError> { todo!("phase-b-reconcile: gc_set_param") }
171    fn gc_is_running(&mut self) -> Result<bool, LuaError> { todo!("phase-b-reconcile: gc_is_running") }
172    fn gc_gen(&mut self, minor_mul: i32, major_mul: i32) -> Result<i32, LuaError> { todo!("phase-b-reconcile: gc_gen") }
173    fn gc_inc(&mut self, pause: i32, step_mul: i32, step_size: i32) -> Result<i32, LuaError> { todo!("phase-b-reconcile: gc_inc") }
174
175    fn call(&mut self, nargs: i32, nresults: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: call") }
176    fn call_k(
177        &mut self,
178        nargs: i32,
179        nresults: i32,
180        ctx: isize,
181        k: Option<fn(&mut LuaState, i32, isize) -> Result<usize, LuaError>>,
182    ) -> Result<(), LuaError> {
183        let _ = (nargs, nresults, ctx, k);
184        todo!("phase-b-reconcile: call_k")
185    }
186    fn protected_call(&mut self, nargs: i32, nresults: i32, msgh: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: protected_call") }
187    fn protected_call_k(
188        &mut self,
189        nargs: i32,
190        nresults: i32,
191        msgh: i32,
192        ctx: isize,
193        k: Option<fn(&mut LuaState, i32, isize) -> Result<usize, LuaError>>,
194    ) -> Result<(), LuaError> {
195        let _ = (nargs, nresults, msgh, ctx, k);
196        todo!("phase-b-reconcile: protected_call_k")
197    }
198    fn len_op(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: len_op") }
199    fn arith(&mut self, op: ArithOp) -> Result<(), LuaError> { todo!("phase-b-reconcile: arith") }
200
201    fn load(&mut self, chunk: &[u8], name: &[u8], mode: Option<&[u8]>) -> Result<bool, LuaError> { todo!("phase-b-reconcile: load") }
202    fn load_buffer_ex<M: ?Sized>(&mut self, buf: &[u8], name: &[u8], mode: &M) -> Result<bool, LuaError>
203    where
204        M: AsRef<[u8]>,
205    { let _ = (buf, name, mode); todo!("phase-b-reconcile: load_buffer_ex") }
206    fn load_file(&mut self, path: Option<&[u8]>) -> Result<bool, LuaError> { todo!("phase-b-reconcile: load_file") }
207    fn load_file_ex(&mut self, path: Option<&[u8]>, mode: Option<&[u8]>) -> Result<bool, LuaError> { todo!("phase-b-reconcile: load_file_ex") }
208    fn load_with_reader<F, M: ?Sized>(&mut self, reader: F, name: &[u8], mode: &M) -> Result<bool, LuaError>
209    where
210        F: FnMut(&mut LuaState) -> Result<Option<Vec<u8>>, LuaError>,
211        M: AsRef<[u8]>,
212    { let _ = (reader, name, mode); todo!("phase-b-reconcile: load_with_reader") }
213    fn dump_function(&mut self, strip: bool) -> Result<Vec<u8>, LuaError> { todo!("phase-b-reconcile: dump_function") }
214
215    fn warning(&mut self, msg: &[u8], to_cont: bool) -> Result<(), LuaError> { todo!("phase-b-reconcile: warning") }
216    fn write_output(&mut self, msg: &[u8]) -> Result<(), LuaError> { todo!("phase-b-reconcile: write_output") }
217    fn set_warn_fn(&mut self, f: Option<lua_CFunction>, ud: Option<LuaValue>) -> Result<(), LuaError> { todo!("phase-b-reconcile: set_warn_fn") }
218    fn set_funcs(&mut self, funcs: &[(&[u8], lua_CFunction)], nup: i32) -> Result<(), LuaError> {
219        let _ = (funcs, nup);
220        todo!("phase-b-reconcile: set_funcs")
221    }
222    fn set_global(&mut self, name: &[u8]) -> Result<(), LuaError> { todo!("phase-b-reconcile: set_global") }
223    fn set_upvalue(&mut self, fidx: i32, n: i32) -> Result<Option<Vec<u8>>, LuaError> { todo!("phase-b-reconcile: set_upvalue") }
224    fn get_info(&mut self, what: &[u8], ar: &mut LuaDebug) -> Result<(), LuaError> { todo!("phase-b-reconcile: get_info") }
225    fn get_stack(&mut self, level: i32, ar: &mut LuaDebug) -> bool { todo!("phase-b-reconcile: get_stack") }
226    fn lua_version(&mut self) -> f64 { todo!("phase-b-reconcile: lua_version") }
227    fn string_to_number(&mut self, idx: i32) -> Option<usize> { todo!("phase-b-reconcile: string_to_number") }
228    fn string_to_number_push<S: AsRef<[u8]> + ?Sized>(&mut self, s: &S) -> Result<usize, LuaError> { let _ = s.as_ref(); todo!("phase-b-reconcile: string_to_number_push") }
229    fn require_lib(&mut self, name: &[u8], openf: lua_CFunction, glb: bool) -> Result<(), LuaError> { todo!("phase-b-reconcile: require_lib") }
230    fn peek_bytes(&mut self, idx: i32) -> Option<Vec<u8>> { todo!("phase-b-reconcile: peek_bytes") }
231
232    fn check_number(&mut self, arg: i32) -> Result<f64, LuaError> { todo!("phase-b-reconcile: check_number") }
233    fn check_integer(&mut self, arg: i32) -> Result<i64, LuaError> { todo!("phase-b-reconcile: check_integer") }
234    fn check_any(&mut self, arg: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: check_any") }
235    fn check_arg_number(&mut self, arg: i32) -> Result<f64, LuaError> { todo!("phase-b-reconcile: check_arg_number") }
236    fn check_arg_userdata(&mut self, arg: i32, name: &[u8]) -> Result<GcRef<LuaUserData>, LuaError> { todo!("phase-b-reconcile: check_arg_userdata") }
237    fn check_stack_growth(&mut self, n: i32) -> bool { todo!("phase-b-reconcile: check_stack_growth") }
238    fn opt_integer(&mut self, arg: i32, def: i64) -> Result<i64, LuaError> { todo!("phase-b-reconcile: opt_integer") }
239    fn opt_number(&mut self, arg: i32, def: f64) -> Result<f64, LuaError> { todo!("phase-b-reconcile: opt_number") }
240    fn opt_arg_lstring(&mut self, arg: i32, def: Option<&[u8]>) -> Result<Option<Vec<u8>>, LuaError> { todo!("phase-b-reconcile: opt_arg_lstring") }
241
242    fn table_get_i(&mut self, idx: i32, n: i64) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: table_get_i") }
243    fn table_set_i(&mut self, idx: i32, n: i64) -> Result<(), LuaError> { todo!("phase-b-reconcile: table_set_i") }
244    fn table_get_i_value(&mut self, t: &LuaValue, n: i64) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: table_get_i_value") }
245    fn table_set_i_value(&mut self, t: &LuaValue, n: i64) -> Result<(), LuaError> { todo!("phase-b-reconcile: table_set_i_value") }
246    fn get_table(&mut self, idx: i32) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: get_table") }
247    fn raw_geti(&mut self, idx: i32, n: i64) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: raw_geti") }
248    fn raw_seti(&mut self, idx: i32, n: i64) -> Result<(), LuaError> { todo!("phase-b-reconcile: raw_seti") }
249    fn len_at(&mut self, idx: i32) -> i64 { todo!("phase-b-reconcile: len_at") }
250    fn length_at(&mut self, idx: i32) -> Result<i64, LuaError> { todo!("phase-b-reconcile: length_at") }
251    fn stack_top(&mut self) -> i32 { todo!("phase-b-reconcile: stack_top") }
252    fn get_top(&mut self) -> i32 { todo!("phase-b-reconcile: get_top") }
253
254    fn push_value_at(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_value_at") }
255    fn push_fail(&mut self) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_fail") }
256    fn push_lstring(&mut self, s: &[u8]) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_lstring") }
257    fn push_thread(&mut self) -> Result<bool, LuaError> { todo!("phase-b-reconcile: push_thread") }
258    fn push_cclosure(&mut self, f: lua_CFunction, n: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_cclosure") }
259    fn push_upvalue(&mut self, idx: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_upvalue") }
260    fn push_registry(&mut self) -> Result<(), LuaError> { todo!("phase-b-reconcile: push_registry") }
261
262    fn to_integer(&mut self, idx: i32) -> Option<i64> { todo!("phase-b-reconcile: to_integer") }
263    fn to_integer_opt(&mut self, idx: i32) -> Option<i64> { todo!("phase-b-reconcile: to_integer_opt") }
264    fn to_number(&mut self, idx: i32) -> Option<f64> { todo!("phase-b-reconcile: to_number") }
265    fn to_bytes(&mut self, idx: i32) -> Option<Vec<u8>> { todo!("phase-b-reconcile: to_bytes") }
266    fn to_bytes_at(&mut self, idx: i32) -> Option<Vec<u8>> { todo!("phase-b-reconcile: to_bytes_at") }
267    fn to_string_coerced(&mut self, idx: i32) -> Option<Vec<u8>> { todo!("phase-b-reconcile: to_string_coerced") }
268    fn to_light_userdata(&mut self, idx: i32) -> Option<*mut std::ffi::c_void> { todo!("phase-b-reconcile: to_light_userdata") }
269    fn to_thread(&mut self, idx: i32) -> Option<GcRef<LuaThread>> { todo!("phase-b-reconcile: to_thread") }
270    fn to_thread_at(&mut self, idx: i32) -> Option<GcRef<LuaThread>> { todo!("phase-b-reconcile: to_thread_at") }
271    fn type_name_str_at(&mut self, idx: i32) -> &'static [u8] { todo!("phase-b-reconcile: type_name_str_at") }
272    fn is_c_function_at(&mut self, idx: i32) -> bool { todo!("phase-b-reconcile: is_c_function_at") }
273
274    fn compare(&mut self, idx1: i32, idx2: i32, op: CompareOp) -> Result<bool, LuaError> { todo!("phase-b-reconcile: compare") }
275    fn compare_lt(&mut self, idx1: i32, idx2: i32) -> Result<bool, LuaError> { todo!("phase-b-reconcile: compare_lt") }
276
277    fn get_field_registry(&mut self, name: &[u8]) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: get_field_registry") }
278    fn get_registry_field(&mut self, name: &[u8]) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: get_registry_field") }
279    fn get_subtable_registry(&mut self, name: &[u8]) -> Result<bool, LuaError> { todo!("phase-b-reconcile: get_subtable_registry") }
280    fn get_or_create_registry_subtable(&mut self, name: &[u8]) -> Result<bool, LuaError> { todo!("phase-b-reconcile: get_or_create_registry_subtable") }
281    fn registry_get(&mut self, key: &[u8]) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: registry_get") }
282    fn registry_set(&mut self, key: &[u8]) -> Result<(), LuaError> { todo!("phase-b-reconcile: registry_set") }
283
284    fn new_lib<F: Copy>(&mut self, funcs: &[(&[u8], F)]) -> Result<(), LuaError> { todo!("phase-b-reconcile: new_lib") }
285    fn new_lib_table<F: Copy>(&mut self, funcs: &[(&[u8], F)]) -> Result<(), LuaError> { todo!("phase-b-reconcile: new_lib_table") }
286    fn new_metatable(&mut self, name: &[u8]) -> Result<bool, LuaError> { todo!("phase-b-reconcile: new_metatable") }
287    fn set_metatable_by_name(&mut self, name: &[u8]) -> Result<(), LuaError> { todo!("phase-b-reconcile: set_metatable_by_name") }
288    fn register_funcs<F: Copy>(&mut self, funcs: &[(&[u8], F)]) -> Result<(), LuaError> { todo!("phase-b-reconcile: register_funcs") }
289    fn register_lib<F: Copy>(&mut self, name: &[u8], funcs: &[(&[u8], F)]) -> Result<(), LuaError> { todo!("phase-b-reconcile: register_lib") }
290    fn set_funcs_with_upvalues<F: Copy>(&mut self, funcs: &[(&[u8], F)], nup: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: set_funcs_with_upvalues") }
291
292    fn new_userdata_typed(&mut self, name: &[u8], size: usize, nuvalue: i32) -> Result<GcRef<LuaUserData>, LuaError> { todo!("phase-b-reconcile: new_userdata_typed") }
293    fn get_iuservalue(&mut self, idx: i32, n: i32) -> Result<LuaType, LuaError> { todo!("phase-b-reconcile: get_iuservalue") }
294    fn set_iuservalue(&mut self, idx: i32, n: i32) -> Result<bool, LuaError> { todo!("phase-b-reconcile: set_iuservalue") }
295    fn test_arg_userdata(&mut self, arg: i32, name: &[u8]) -> Option<GcRef<LuaUserData>> { todo!("phase-b-reconcile: test_arg_userdata") }
296
297    fn get_upvalue(&mut self, fidx: i32, n: i32) -> Result<Option<Vec<u8>>, LuaError> { todo!("phase-b-reconcile: get_upvalue") }
298    fn upvalue_id(&mut self, fidx: i32, n: i32) -> Result<*mut std::ffi::c_void, LuaError> { todo!("phase-b-reconcile: upvalue_id") }
299    fn join_upvalues(&mut self, fidx1: i32, n1: i32, fidx2: i32, n2: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: join_upvalues") }
300    fn upvalue_index(&mut self, i: i32) -> i32 { upvalue_index(i) }
301    fn close(&mut self) { todo!("phase-b-reconcile: close") }
302    fn set_hook_full(&mut self, f: Option<lua_CFunction>, mask: u32, count: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: set_hook_full") }
303
304    fn get_local_at(&mut self, ar: &LuaDebug, n: i32) -> Result<Option<Vec<u8>>, LuaError> { todo!("phase-b-reconcile: get_local_at") }
305    fn set_local_at(&mut self, ar: &LuaDebug, n: i32) -> Result<Option<Vec<u8>>, LuaError> { todo!("phase-b-reconcile: set_local_at") }
306    fn get_param_name(&mut self, fidx: i32, n: i32) -> Result<Option<Vec<u8>>, LuaError> { todo!("phase-b-reconcile: get_param_name") }
307
308    fn get_debug_info(&mut self, what: &[u8], ar: &mut LuaDebug) -> Result<(), LuaError> { todo!("phase-b-reconcile: get_debug_info") }
309    fn get_stack_level(&mut self, level: i32, ar: &mut LuaDebug) -> bool { todo!("phase-b-reconcile: get_stack_level") }
310    fn has_frames(&mut self) -> bool { todo!("phase-b-reconcile: has_frames") }
311    fn lua_traceback(&mut self, other: &mut LuaState, msg: Option<&[u8]>, level: i32) -> Result<(), LuaError> { todo!("phase-b-reconcile: lua_traceback") }
312
313    fn get_hook_count(&mut self) -> i32 { todo!("phase-b-reconcile: get_hook_count") }
314    fn get_hook_mask(&mut self) -> u32 { todo!("phase-b-reconcile: get_hook_mask") }
315    fn hook_is_set(&mut self) -> bool { todo!("phase-b-reconcile: hook_is_set") }
316    fn hook_is_internal_lua_hook(&mut self) -> bool { todo!("phase-b-reconcile: hook_is_internal_lua_hook") }
317    fn set_c_stack_limit(&mut self, limit: i32) -> Result<i32, LuaError> { todo!("phase-b-reconcile: set_c_stack_limit") }
318
319    fn new_thread(&mut self, initial_body: Option<LuaValue>) -> Result<GcRef<LuaThread>, LuaError> {
320        let _ = initial_body;
321        todo!("phase-b-reconcile: new_thread")
322    }
323    fn is_same_thread(&mut self, other: &LuaState) -> bool { todo!("phase-b-reconcile: is_same_thread") }
324    fn thread_status(&mut self) -> LuaStatus { todo!("phase-b-reconcile: thread_status") }
325
326    fn load_buffer(&mut self, buf: &[u8], name: &[u8], mode: Option<&[u8]>) -> Result<LuaStatus, LuaError> { todo!("phase-b-reconcile: load_buffer") }
327    fn where_error(&mut self, level: i32, msg: &[u8]) -> LuaError { todo!("phase-b-reconcile: where_error") }
328    fn arg(&mut self, n: i32) -> LuaValue { todo!("phase-b-reconcile: arg") }
329    fn as_bytes_or_coerce(&mut self, idx: i32) -> Option<Vec<u8>> { todo!("phase-b-reconcile: as_bytes_or_coerce") }
330    fn as_bytes(&mut self, idx: i32) -> Option<Vec<u8>> { todo!("phase-b-reconcile: as_bytes") }
331}
332
333impl LuaStateStubExt for LuaState {
334    fn require_lib(&mut self, name: &[u8], openf: lua_CFunction, glb: bool) -> Result<(), LuaError> {
335        crate::auxlib::requiref(self, name, openf, glb)
336    }
337
338    fn get_field(&mut self, idx: i32, k: &[u8]) -> Result<LuaType, LuaError> {
339        lua_vm::api::get_field(self, idx, k)
340    }
341
342    fn abs_index(&mut self, idx: i32) -> i32 {
343        lua_vm::api::abs_index(self, idx)
344    }
345
346    fn push_value(&mut self, idx: i32) -> Result<(), LuaError> {
347        lua_vm::api::push_value(self, idx);
348        Ok(())
349    }
350
351    fn set_field(&mut self, idx: i32, k: &[u8]) -> Result<(), LuaError> {
352        lua_vm::api::set_field(self, idx, k)
353    }
354
355    fn set_global(&mut self, name: &[u8]) -> Result<(), LuaError> {
356        lua_vm::api::set_global(self, name)
357    }
358
359    fn to_boolean(&mut self, idx: i32) -> bool {
360        lua_vm::api::to_boolean(self, idx)
361    }
362
363    fn top(&mut self) -> i32 {
364        lua_vm::api::get_top(self)
365    }
366
367    fn push_c_function(&mut self, f: lua_CFunction) -> Result<(), LuaError> {
368        let idx: LuaCFnPtr = {
369            let mut g = self.global_mut();
370            match g.c_functions.iter().position(|&existing| existing == f) {
371                Some(i) => i,
372                None => {
373                    let i = g.c_functions.len();
374                    g.c_functions.push(f);
375                    i
376                }
377            }
378        };
379        self.push(LuaValue::Function(LuaClosure::LightC(idx)));
380        Ok(())
381    }
382
383    fn push_bytes(&mut self, s: &[u8]) -> Result<(), LuaError> {
384        lua_vm::api::push_lstring(self, s)?;
385        Ok(())
386    }
387
388    fn call(&mut self, nargs: i32, nresults: i32) -> Result<(), LuaError> {
389        lua_vm::api::call_k(self, nargs, nresults, 0, None)
390    }
391
392    fn call_k(
393        &mut self,
394        nargs: i32,
395        nresults: i32,
396        ctx: isize,
397        k: Option<fn(&mut LuaState, i32, isize) -> Result<usize, LuaError>>,
398    ) -> Result<(), LuaError> {
399        lua_vm::api::call_k(self, nargs, nresults, ctx, k)
400    }
401
402    fn remove(&mut self, idx: i32) -> Result<(), LuaError> {
403        lua_vm::api::rotate(self, idx, -1);
404        lua_vm::api::set_top(self, -2)
405    }
406
407    fn get_upvalue(&mut self, fidx: i32, n: i32) -> Result<Option<Vec<u8>>, LuaError> {
408        Ok(lua_vm::api::get_upvalue(self, fidx, n))
409    }
410
411    fn set_upvalue(&mut self, fidx: i32, n: i32) -> Result<Option<Vec<u8>>, LuaError> {
412        Ok(lua_vm::api::setup_value(self, fidx, n))
413    }
414
415    fn load(&mut self, chunk: &[u8], name: &[u8], mode: Option<&[u8]>) -> Result<bool, LuaError> {
416        let mut remaining = Some(chunk.to_vec());
417        let reader: Box<dyn FnMut() -> Option<Vec<u8>>> = Box::new(move || remaining.take());
418        let status = lua_vm::api::load(self, reader, Some(name), mode)?;
419        Ok(status == LuaStatus::Ok)
420    }
421
422    fn push_globals(&mut self) -> Result<(), LuaError> {
423        let g = self.global().globals.clone();
424        self.push(g);
425        Ok(())
426    }
427
428    fn set_funcs(&mut self, funcs: &[(&[u8], lua_CFunction)], nup: i32) -> Result<(), LuaError> {
429        lua_vm::api::check_stack(self, nup);
430        for (name, f) in funcs {
431            for _ in 0..nup {
432                lua_vm::api::push_value(self, -nup);
433            }
434            lua_vm::api::push_cclosure(self, *f, nup)?;
435            lua_vm::api::set_field(self, -(nup + 2), name)?;
436        }
437        self.pop_n(nup as usize);
438        Ok(())
439    }
440
441    fn arg_to_bool(&mut self, arg: i32) -> bool {
442        lua_vm::api::to_boolean(self, arg)
443    }
444
445    fn value_at(&mut self, idx: i32) -> LuaValue {
446        lua_vm::api::push_value(self, idx);
447        self.pop()
448    }
449
450    fn check_arg_type(&mut self, arg: i32, t: LuaType) -> Result<(), LuaError> {
451        if lua_vm::api::lua_type_at(self, arg) != t {
452            lua_vm::api::push_value(self, arg);
453            let got = self.pop();
454            let expected: &str = match t {
455                LuaType::None => "no value",
456                LuaType::Nil => "nil",
457                LuaType::Boolean => "boolean",
458                LuaType::LightUserData => "userdata",
459                LuaType::Number => "number",
460                LuaType::String => "string",
461                LuaType::Table => "table",
462                LuaType::Function => "function",
463                LuaType::UserData => "userdata",
464                LuaType::Thread => "thread",
465            };
466            let got_name = self.full_type_name(&got)?;
467            let extramsg = format!(
468                "{} expected, got {}",
469                expected, String::from_utf8_lossy(&got_name)
470            );
471            return Err(lua_vm::debug::arg_error_impl(self, arg, extramsg.as_bytes()));
472        }
473        Ok(())
474    }
475
476    fn opt_arg_string(&mut self, arg: i32, def: &[u8]) -> Result<Vec<u8>, LuaError> {
477        match lua_vm::api::lua_type_at(self, arg) {
478            LuaType::None | LuaType::Nil => Ok(def.to_vec()),
479            _ => self.check_arg_string(arg),
480        }
481    }
482
483    fn get_metafield(&mut self, idx: i32, name: &[u8]) -> Result<LuaType, LuaError> {
484        let abs = lua_vm::api::abs_index(self, idx);
485        if !lua_vm::api::get_metatable(self, abs) {
486            return Ok(LuaType::Nil);
487        }
488        lua_vm::api::push_lstring(self, name)?;
489        let tt = lua_vm::api::raw_get(self, -2);
490        if tt == LuaType::Nil {
491            self.pop_n(2);
492        } else {
493            self.remove(-2)?;
494        }
495        Ok(tt)
496    }
497
498    fn table_get_i(&mut self, idx: i32, n: i64) -> Result<LuaType, LuaError> {
499        lua_vm::api::get_i(self, idx, n)
500    }
501
502    fn table_get_i_value(&mut self, t: &LuaValue, n: i64) -> Result<LuaType, LuaError> {
503        lua_vm::api::get_i_value(self, t, n)
504    }
505
506    fn table_set_i_value(&mut self, t: &LuaValue, n: i64) -> Result<(), LuaError> {
507        lua_vm::api::set_i_value(self, t, n)
508    }
509
510    fn compare_lt(&mut self, idx1: i32, idx2: i32) -> Result<bool, LuaError> {
511        lua_vm::api::compare(self, idx1, idx2, 1)
512    }
513
514    fn check_arg_any(&mut self, arg: i32) -> Result<(), LuaError> {
515        if lua_vm::api::lua_type_at(self, arg) == LuaType::None {
516            return Err(LuaError::arg_error(arg, "value expected"));
517        }
518        Ok(())
519    }
520
521    fn check_arg_integer(&mut self, arg: i32) -> Result<i64, LuaError> {
522        match lua_vm::api::to_integer_x(self, arg) {
523            Some(d) => Ok(d),
524            None => {
525                if lua_vm::api::is_number(self, arg) {
526                    Err(LuaError::arg_error(
527                        arg,
528                        "number has no integer representation",
529                    ))
530                } else {
531                    let got = self.value_at(arg);
532                    let got_name = self.full_type_name(&got)?;
533                    let extramsg = format!(
534                        "number expected, got {}",
535                        String::from_utf8_lossy(&got_name)
536                    );
537                    Err(lua_vm::debug::arg_error_impl(self, arg, extramsg.as_bytes()))
538                }
539            }
540        }
541    }
542
543    fn check_arg_string(&mut self, arg: i32) -> Result<Vec<u8>, LuaError> {
544        match lua_vm::api::to_lua_string(self, arg)? {
545            Some(s) => Ok(s.as_bytes().to_vec()),
546            None => {
547                let got = self.value_at(arg);
548                let got_name = self.full_type_name(&got)?;
549                let extramsg = format!(
550                    "string expected, got {}",
551                    String::from_utf8_lossy(&got_name)
552                );
553                Err(lua_vm::debug::arg_error_impl(self, arg, extramsg.as_bytes()))
554            }
555        }
556    }
557
558    fn check_arg_number(&mut self, arg: i32) -> Result<f64, LuaError> {
559        match lua_vm::api::to_number_x(self, arg) {
560            Some(d) => Ok(d),
561            None => {
562                let got = self.value_at(arg);
563                let got_name = self.full_type_name(&got)?;
564                let extramsg = format!(
565                    "number expected, got {}",
566                    String::from_utf8_lossy(&got_name)
567                );
568                Err(lua_vm::debug::arg_error_impl(self, arg, extramsg.as_bytes()))
569            }
570        }
571    }
572
573    fn check_number(&mut self, arg: i32) -> Result<f64, LuaError> {
574        self.check_arg_number(arg)
575    }
576
577    fn check_integer(&mut self, arg: i32) -> Result<i64, LuaError> {
578        self.check_arg_integer(arg)
579    }
580
581    fn check_any(&mut self, arg: i32) -> Result<(), LuaError> {
582        self.check_arg_any(arg)
583    }
584
585    fn opt_arg_integer(&mut self, arg: i32, def: i64) -> Result<i64, LuaError> {
586        match lua_vm::api::lua_type_at(self, arg) {
587            LuaType::None | LuaType::Nil => Ok(def),
588            _ => self.check_arg_integer(arg),
589        }
590    }
591
592    fn opt_integer(&mut self, arg: i32, def: i64) -> Result<i64, LuaError> {
593        self.opt_arg_integer(arg, def)
594    }
595
596    fn opt_number(&mut self, arg: i32, def: f64) -> Result<f64, LuaError> {
597        match lua_vm::api::lua_type_at(self, arg) {
598            LuaType::None | LuaType::Nil => Ok(def),
599            _ => self.check_arg_number(arg),
600        }
601    }
602
603    fn opt_arg_string_bytes(&mut self, arg: i32) -> Result<Vec<u8>, LuaError> {
604        match lua_vm::api::lua_type_at(self, arg) {
605            LuaType::None | LuaType::Nil => Ok(Vec::new()),
606            _ => self.check_arg_string(arg),
607        }
608    }
609
610    fn opt_arg_lstring(&mut self, arg: i32, def: Option<&[u8]>) -> Result<Option<Vec<u8>>, LuaError> {
611        match lua_vm::api::lua_type_at(self, arg) {
612            LuaType::None | LuaType::Nil => Ok(def.map(|d| d.to_vec())),
613            _ => Ok(Some(self.check_arg_string(arg)?)),
614        }
615    }
616
617    fn check_arg_option(
618        &mut self,
619        arg: i32,
620        def: Option<&[u8]>,
621        lst: &[&[u8]],
622    ) -> Result<usize, LuaError> {
623        let name: Vec<u8> = match def {
624            Some(d) if matches!(
625                lua_vm::api::lua_type_at(self, arg),
626                LuaType::None | LuaType::Nil
627            ) =>
628            {
629                d.to_vec()
630            }
631            _ => self.check_arg_string(arg)?,
632        };
633        for (i, entry) in lst.iter().enumerate() {
634            if *entry == name.as_slice() {
635                return Ok(i);
636            }
637        }
638        Err(LuaError::arg_error(arg, "invalid option"))
639    }
640
641    fn arg(&mut self, n: i32) -> LuaValue {
642        self.value_at(n)
643    }
644
645    fn type_at(&mut self, idx: i32) -> LuaType {
646        lua_vm::api::lua_type_at(self, idx)
647    }
648
649    fn type_name(&mut self, t: LuaType) -> &'static [u8] {
650        lua_vm::api::type_name(self, t)
651    }
652
653    fn type_name_at(&mut self, idx: i32) -> &'static [u8] {
654        let t = lua_vm::api::lua_type_at(self, idx);
655        lua_vm::api::type_name(self, t)
656    }
657
658    fn is_integer(&mut self, idx: i32) -> bool {
659        lua_vm::api::is_integer(self, idx)
660    }
661
662    fn is_number(&mut self, idx: i32) -> bool {
663        lua_vm::api::is_number(self, idx)
664    }
665
666    fn is_none_or_nil(&mut self, idx: i32) -> bool {
667        matches!(
668            lua_vm::api::lua_type_at(self, idx),
669            LuaType::None | LuaType::Nil
670        )
671    }
672
673    fn to_integer_x(&mut self, idx: i32) -> Option<i64> {
674        lua_vm::api::to_integer_x(self, idx)
675    }
676
677    fn to_number_x(&mut self, idx: i32) -> Option<f64> {
678        lua_vm::api::to_number_x(self, idx)
679    }
680
681    fn to_integer(&mut self, idx: i32) -> Option<i64> {
682        lua_vm::api::to_integer_x(self, idx)
683    }
684
685    fn to_integer_opt(&mut self, idx: i32) -> Option<i64> {
686        lua_vm::api::to_integer_x(self, idx)
687    }
688
689    fn to_number(&mut self, idx: i32) -> Option<f64> {
690        lua_vm::api::to_number_x(self, idx)
691    }
692
693    fn to_lua_string(&mut self, idx: i32) -> Option<GcRef<LuaString>> {
694        lua_vm::api::to_lua_string(self, idx).ok().flatten()
695    }
696
697    fn to_lua_string_bytes(&mut self, idx: i32) -> Option<Vec<u8>> {
698        lua_vm::api::to_lua_string(self, idx)
699            .ok()
700            .flatten()
701            .map(|s| s.as_bytes().to_vec())
702    }
703
704    fn to_lua_string_len(&mut self, idx: i32) -> Option<usize> {
705        lua_vm::api::to_lua_string(self, idx)
706            .ok()
707            .flatten()
708            .map(|s| s.len())
709    }
710
711    fn to_bytes(&mut self, idx: i32) -> Option<Vec<u8>> {
712        self.to_lua_string_bytes(idx)
713    }
714
715    fn to_bytes_at(&mut self, idx: i32) -> Option<Vec<u8>> {
716        self.to_lua_string_bytes(idx)
717    }
718
719    fn raw_equal(&mut self, idx1: i32, idx2: i32) -> Result<bool, LuaError> {
720        Ok(lua_vm::api::raw_equal(self, idx1, idx2))
721    }
722
723    fn raw_geti(&mut self, idx: i32, n: i64) -> Result<LuaType, LuaError> {
724        Ok(lua_vm::api::raw_get_i(self, idx, n))
725    }
726
727    fn raw_get_i(&mut self, idx: i32, n: i64) -> Result<LuaType, LuaError> {
728        Ok(lua_vm::api::raw_get_i(self, idx, n))
729    }
730
731    fn raw_seti(&mut self, idx: i32, n: i64) -> Result<(), LuaError> {
732        lua_vm::api::raw_set_i(self, idx, n)
733    }
734
735    fn raw_set_i(&mut self, idx: i32, n: i64) -> Result<(), LuaError> {
736        lua_vm::api::raw_set_i(self, idx, n)
737    }
738
739    fn raw_len(&mut self, idx: i32) -> i64 {
740        lua_vm::api::raw_len(self, idx) as i64
741    }
742
743    fn get_i(&mut self, idx: i32, n: i64) -> Result<LuaType, LuaError> {
744        lua_vm::api::get_i(self, idx, n)
745    }
746
747    fn get_metatable(&mut self, idx: i32) -> Result<bool, LuaError> {
748        Ok(lua_vm::api::get_metatable(self, idx))
749    }
750
751    fn set_metatable(&mut self, idx: i32) -> Result<(), LuaError> {
752        lua_vm::api::set_metatable(self, idx)?;
753        Ok(())
754    }
755
756    fn compare(&mut self, idx1: i32, idx2: i32, op: CompareOp) -> Result<bool, LuaError> {
757        let op_i = match op {
758            CompareOp::Eq => 0,
759            CompareOp::Lt => 1,
760            CompareOp::Le => 2,
761        };
762        lua_vm::api::compare(self, idx1, idx2, op_i)
763    }
764
765    fn protected_call(&mut self, nargs: i32, nresults: i32, msgh: i32) -> Result<(), LuaError> {
766        lua_vm::api::pcall_k(self, nargs, nresults, msgh, 0, None)?;
767        Ok(())
768    }
769    fn protected_call_k(
770        &mut self,
771        nargs: i32,
772        nresults: i32,
773        msgh: i32,
774        ctx: isize,
775        k: Option<fn(&mut LuaState, i32, isize) -> Result<usize, LuaError>>,
776    ) -> Result<(), LuaError> {
777        lua_vm::api::pcall_k(self, nargs, nresults, msgh, ctx, k)?;
778        Ok(())
779    }
780
781    fn push_value_at(&mut self, idx: i32) -> Result<(), LuaError> {
782        lua_vm::api::push_value(self, idx);
783        Ok(())
784    }
785
786    fn push_thread(&mut self) -> Result<bool, LuaError> {
787        Ok(lua_vm::api::push_thread(self))
788    }
789
790    fn push_cclosure(&mut self, f: lua_CFunction, n: i32) -> Result<(), LuaError> {
791        lua_vm::api::push_cclosure(self, f, n)
792    }
793
794    fn push_c_closure(&mut self, f: lua_CFunction, n: i32) -> Result<(), LuaError> {
795        lua_vm::api::push_cclosure(self, f, n)
796    }
797
798    fn push_lstring(&mut self, s: &[u8]) -> Result<(), LuaError> {
799        lua_vm::api::push_lstring(self, s)?;
800        Ok(())
801    }
802
803    fn push_string(&mut self, s: &[u8]) -> Result<(), LuaError> {
804        lua_vm::api::push_lstring(self, s)?;
805        Ok(())
806    }
807
808    fn get_top(&mut self) -> i32 {
809        lua_vm::api::get_top(self)
810    }
811
812    fn stack_top(&mut self) -> i32 {
813        lua_vm::api::get_top(self)
814    }
815
816    fn top_count(&mut self) -> i32 {
817        lua_vm::api::get_top(self)
818    }
819
820    fn check_stack_space(&mut self, n: i32) -> bool {
821        lua_vm::api::check_stack(self, n)
822    }
823
824    fn rotate(&mut self, idx: i32, n: i32) -> Result<(), LuaError> {
825        lua_vm::api::rotate(self, idx, n);
826        Ok(())
827    }
828
829    fn insert(&mut self, idx: i32) -> Result<(), LuaError> {
830        lua_vm::api::rotate(self, idx, 1);
831        Ok(())
832    }
833
834    fn copy_value(&mut self, from: i32, to: i32) -> Result<(), LuaError> {
835        lua_vm::api::copy(self, from, to);
836        Ok(())
837    }
838
839    fn replace(&mut self, idx: i32) -> Result<(), LuaError> {
840        lua_vm::api::copy(self, -1, idx);
841        lua_vm::api::set_top(self, -2)
842    }
843
844    fn len_op(&mut self, idx: i32) -> Result<(), LuaError> {
845        lua_vm::api::len(self, idx)
846    }
847
848    fn table_next(&mut self, idx: i32) -> Result<bool, LuaError> {
849        lua_vm::api::next(self, idx)
850    }
851
852    fn create_table(&mut self, narr: i32, nrec: i32) -> Result<(), LuaError> {
853        lua_vm::api::create_table(self, narr, nrec)
854    }
855
856    fn to_userdata(&mut self, idx: i32) -> Option<GcRef<LuaUserData>> {
857        let v = self.value_at(idx);
858        if let LuaValue::UserData(u) = v { Some(u) } else { None }
859    }
860
861    fn to_light_userdata(&mut self, idx: i32) -> Option<*mut std::ffi::c_void> {
862        lua_vm::api::to_userdata(self, idx)
863    }
864
865    fn to_thread(&mut self, idx: i32) -> Option<GcRef<LuaThread>> {
866        lua_vm::api::to_thread(self, idx)
867    }
868
869    fn to_thread_at(&mut self, idx: i32) -> Option<GcRef<LuaThread>> {
870        lua_vm::api::to_thread(self, idx)
871    }
872
873    fn len_at(&mut self, idx: i32) -> i64 {
874        lua_vm::api::raw_len(self, idx) as i64
875    }
876
877    fn length_at(&mut self, idx: i32) -> Result<i64, LuaError> {
878        lua_vm::api::len(self, idx)?;
879        let v = lua_vm::api::to_integer_x(self, -1);
880        self.pop_n(1);
881        match v {
882            Some(l) => Ok(l),
883            None => Err(LuaError::runtime(format_args!(
884                "object length is not an integer"
885            ))),
886        }
887    }
888
889    fn peek_bytes(&mut self, idx: i32) -> Option<Vec<u8>> {
890        self.to_lua_string_bytes(idx)
891    }
892
893    fn to_string_coerced(&mut self, idx: i32) -> Option<Vec<u8>> {
894        self.to_lua_string_bytes(idx)
895    }
896
897    fn raw_get(&mut self, idx: i32) -> Result<LuaType, LuaError> {
898        Ok(lua_vm::api::raw_get(self, idx))
899    }
900
901    fn raw_set(&mut self, idx: i32) -> Result<(), LuaError> {
902        lua_vm::api::raw_set(self, idx)
903    }
904
905    fn is_c_function_at(&mut self, idx: i32) -> bool {
906        lua_vm::api::is_cfunction(self, idx)
907    }
908
909    fn type_name_str_at(&mut self, idx: i32) -> &'static [u8] {
910        let t = lua_vm::api::lua_type_at(self, idx);
911        lua_vm::api::type_name(self, t)
912    }
913
914    fn push_fstring(&mut self, args: std::fmt::Arguments<'_>) -> Result<(), LuaError> {
915        let formatted = std::fmt::format(args);
916        lua_vm::api::push_fstring(self, formatted.as_bytes())?;
917        Ok(())
918    }
919
920    fn arith(&mut self, op: ArithOp) -> Result<(), LuaError> {
921        lua_vm::api::arith(self, op as i32)
922    }
923
924    fn lua_version(&mut self) -> f64 {
925        504.0
926    }
927
928    fn push_fail(&mut self) -> Result<(), LuaError> {
929        self.push(LuaValue::Nil);
930        Ok(())
931    }
932
933    fn push_registry(&mut self) -> Result<(), LuaError> {
934        let r = self.registry_value();
935        self.push(r);
936        Ok(())
937    }
938
939    fn push_upvalue(&mut self, idx: i32) -> Result<(), LuaError> {
940        lua_vm::api::push_value(self, upvalue_index(idx));
941        Ok(())
942    }
943
944    fn pop_bytes(&mut self) -> Vec<u8> {
945        match self.pop() {
946            LuaValue::Str(s) => s.as_bytes().to_vec(),
947            _ => Vec::new(),
948        }
949    }
950
951    fn push_where(&mut self, level: i32) -> Result<(), LuaError> {
952        let mut ar = lua_vm::debug::LuaDebug::default();
953        if lua_vm::debug::get_stack(self, level, &mut ar) {
954            lua_vm::debug::get_info(self, b"Sl", &mut ar);
955            if ar.currentline > 0 {
956                let zero = ar
957                    .short_src
958                    .iter()
959                    .position(|&b| b == 0)
960                    .unwrap_or(ar.short_src.len());
961                let mut buf: Vec<u8> = ar.short_src[..zero].to_vec();
962                buf.push(b':');
963                buf.extend_from_slice(ar.currentline.to_string().as_bytes());
964                buf.extend_from_slice(b": ");
965                lua_vm::api::push_lstring(self, &buf)?;
966                return Ok(());
967            }
968        }
969        lua_vm::api::push_lstring(self, b"")?;
970        Ok(())
971    }
972
973    fn where_error(&mut self, level: i32, msg: &[u8]) -> LuaError {
974        if self.push_where(level).is_err() {
975            return LuaError::runtime(format_args!("{}", StubBStr(msg)));
976        }
977        let mut full = self.pop_bytes();
978        full.extend_from_slice(msg);
979        LuaError::runtime(format_args!("{}", StubBStr(&full)))
980    }
981
982    fn registry_get(&mut self, key: &[u8]) -> Result<LuaType, LuaError> {
983        lua_vm::api::get_field(self, STUB_LUA_REGISTRYINDEX, key)
984    }
985
986    fn get_field_registry(&mut self, name: &[u8]) -> Result<LuaType, LuaError> {
987        lua_vm::api::get_field(self, STUB_LUA_REGISTRYINDEX, name)
988    }
989
990    fn get_registry_field(&mut self, name: &[u8]) -> Result<LuaType, LuaError> {
991        lua_vm::api::get_field(self, STUB_LUA_REGISTRYINDEX, name)
992    }
993
994    fn get_or_create_registry_subtable(&mut self, name: &[u8]) -> Result<bool, LuaError> {
995        self.get_subtable_registry(name)
996    }
997
998    fn registry_set(&mut self, key: &[u8]) -> Result<(), LuaError> {
999        lua_vm::api::set_field(self, STUB_LUA_REGISTRYINDEX, key)
1000    }
1001
1002    fn check_stack_growth(&mut self, n: i32) -> bool {
1003        lua_vm::api::check_stack(self, n)
1004    }
1005
1006    fn ensure_stack<S: AsRef<[u8]> + ?Sized>(&mut self, n: i32, msg: &S) -> Result<(), LuaError> {
1007        if lua_vm::api::check_stack(self, n) {
1008            return Ok(());
1009        }
1010        let m = msg.as_ref();
1011        if m.is_empty() {
1012            Err(LuaError::runtime(format_args!("stack overflow")))
1013        } else {
1014            Err(LuaError::runtime(format_args!(
1015                "stack overflow ({})",
1016                StubBStr(m)
1017            )))
1018        }
1019    }
1020
1021    fn push_copy(&mut self, idx: i32) -> Result<(), LuaError> {
1022        lua_vm::api::push_value(self, idx);
1023        Ok(())
1024    }
1025
1026    fn as_bytes(&mut self, idx: i32) -> Option<Vec<u8>> {
1027        self.to_lua_string_bytes(idx)
1028    }
1029
1030    fn as_bytes_or_coerce(&mut self, idx: i32) -> Option<Vec<u8>> {
1031        self.to_lua_string_bytes(idx)
1032    }
1033
1034    fn thread_status(&mut self) -> LuaStatus {
1035        lua_vm::api::status(self)
1036    }
1037
1038    fn new_thread(&mut self, initial_body: Option<LuaValue>) -> Result<GcRef<LuaThread>, LuaError> {
1039        lua_vm::state::new_thread(self, initial_body)?;
1040        let th = lua_vm::api::to_thread(self, -1)
1041            .ok_or_else(|| LuaError::runtime(format_args!("new_thread: missing thread on top")))?;
1042        Ok(th)
1043    }
1044
1045    fn is_same_thread(&mut self, other: &LuaState) -> bool {
1046        std::ptr::eq(self as *const LuaState, other as *const LuaState)
1047    }
1048
1049    fn load_buffer(&mut self, buf: &[u8], name: &[u8], mode: Option<&[u8]>) -> Result<LuaStatus, LuaError> {
1050        let mut remaining = Some(buf.to_vec());
1051        let reader: Box<dyn FnMut() -> Option<Vec<u8>>> = Box::new(move || remaining.take());
1052        lua_vm::api::load(self, reader, Some(name), mode)
1053    }
1054
1055    fn load_buffer_ex<M: ?Sized>(&mut self, buf: &[u8], name: &[u8], mode: &M) -> Result<bool, LuaError>
1056    where
1057        M: AsRef<[u8]>,
1058    {
1059        let mut remaining = Some(buf.to_vec());
1060        let reader: Box<dyn FnMut() -> Option<Vec<u8>>> = Box::new(move || remaining.take());
1061        let mode_bytes = mode.as_ref();
1062        let status = lua_vm::api::load(self, reader, Some(name), Some(mode_bytes))?;
1063        Ok(status == LuaStatus::Ok)
1064    }
1065
1066    fn dump_function(&mut self, strip: bool) -> Result<Vec<u8>, LuaError> {
1067        let mut out: Vec<u8> = Vec::new();
1068        let mut writer = |chunk: &[u8]| -> Result<(), LuaError> {
1069            out.extend_from_slice(chunk);
1070            Ok(())
1071        };
1072        let ok = lua_vm::api::dump(self, &mut writer, strip)?;
1073        if !ok {
1074            return Err(LuaError::runtime(format_args!(
1075                "unable to dump given function"
1076            )));
1077        }
1078        Ok(out)
1079    }
1080
1081    fn warning(&mut self, msg: &[u8], to_cont: bool) -> Result<(), LuaError> {
1082        lua_vm::api::warning(self, msg, to_cont);
1083        Ok(())
1084    }
1085
1086    fn string_to_number(&mut self, idx: i32) -> Option<usize> {
1087        let bytes = lua_vm::api::to_lua_string(self, idx)
1088            .ok()
1089            .flatten()?
1090            .as_bytes()
1091            .to_vec();
1092        let consumed = lua_vm::api::string_to_number(self, &bytes);
1093        if consumed == 0 {
1094            None
1095        } else {
1096            Some(consumed)
1097        }
1098    }
1099
1100    fn string_to_number_push<S: AsRef<[u8]> + ?Sized>(&mut self, s: &S) -> Result<usize, LuaError> {
1101        Ok(lua_vm::api::string_to_number(self, s.as_ref()))
1102    }
1103
1104    fn gc_count(&mut self) -> Result<i32, LuaError> {
1105        Ok(lua_vm::api::gc(self, lua_vm::api::GcArgs::Count))
1106    }
1107
1108    fn gc_count_b(&mut self) -> Result<i32, LuaError> {
1109        Ok(lua_vm::api::gc(self, lua_vm::api::GcArgs::CountB))
1110    }
1111
1112    fn gc_step(&mut self, data: i32) -> Result<i32, LuaError> {
1113        Ok(lua_vm::api::gc(self, lua_vm::api::GcArgs::Step { data }))
1114    }
1115
1116    fn gc_is_running(&mut self) -> Result<bool, LuaError> {
1117        Ok(lua_vm::api::gc(self, lua_vm::api::GcArgs::IsRunning) != 0)
1118    }
1119
1120    fn gc_control_simple(&mut self, op: i32) -> Result<i32, LuaError> {
1121        let args = match op {
1122            0 => lua_vm::api::GcArgs::Stop,
1123            1 => lua_vm::api::GcArgs::Restart,
1124            2 => lua_vm::api::GcArgs::Collect,
1125            _ => return Err(LuaError::runtime(format_args!(
1126                "invalid GC option {}", op
1127            ))),
1128        };
1129        Ok(lua_vm::api::gc(self, args))
1130    }
1131
1132    fn gc_set_param(&mut self, op: i32, value: i32) -> Result<i32, LuaError> {
1133        let args = match op {
1134            6 => lua_vm::api::GcArgs::SetPause { value },
1135            7 => lua_vm::api::GcArgs::SetStepMul { value },
1136            _ => return Err(LuaError::runtime(format_args!(
1137                "invalid GC param option {}", op
1138            ))),
1139        };
1140        Ok(lua_vm::api::gc(self, args))
1141    }
1142
1143    fn gc_gen(&mut self, minor_mul: i32, major_mul: i32) -> Result<i32, LuaError> {
1144        Ok(lua_vm::api::gc(
1145            self,
1146            lua_vm::api::GcArgs::Gen { minormul: minor_mul, majormul: major_mul },
1147        ))
1148    }
1149
1150    fn gc_inc(&mut self, pause: i32, step_mul: i32, step_size: i32) -> Result<i32, LuaError> {
1151        Ok(lua_vm::api::gc(
1152            self,
1153            lua_vm::api::GcArgs::Inc { pause, stepmul: step_mul, stepsize: step_size },
1154        ))
1155    }
1156
1157    fn get_meta_field(&mut self, idx: i32, name: &[u8]) -> Result<bool, LuaError> {
1158        Ok(crate::auxlib::get_metafield(self, idx, name)? != LuaType::Nil)
1159    }
1160
1161    fn to_display_string(&mut self, idx: i32) -> Result<Vec<u8>, LuaError> {
1162        crate::auxlib::to_lua_string(self, idx)
1163    }
1164
1165    fn get_subtable_registry(&mut self, name: &[u8]) -> Result<bool, LuaError> {
1166        crate::auxlib::get_subtable(self, STUB_LUA_REGISTRYINDEX, name)
1167    }
1168
1169    fn new_metatable(&mut self, name: &[u8]) -> Result<bool, LuaError> {
1170        crate::auxlib::new_metatable(self, name)
1171    }
1172
1173    fn set_metatable_by_name(&mut self, name: &[u8]) -> Result<(), LuaError> {
1174        crate::auxlib::set_metatable(self, name)
1175    }
1176
1177    fn check_arg_userdata(&mut self, arg: i32, name: &[u8]) -> Result<GcRef<LuaUserData>, LuaError> {
1178        crate::auxlib::check_udata(self, arg, name)
1179    }
1180
1181    fn test_arg_userdata(&mut self, arg: i32, name: &[u8]) -> Option<GcRef<LuaUserData>> {
1182        crate::auxlib::test_udata(self, arg, name).ok().flatten()
1183    }
1184
1185    fn get_table(&mut self, idx: i32) -> Result<LuaType, LuaError> {
1186        lua_vm::api::get_table(self, idx)
1187    }
1188
1189    fn get_stack(&mut self, level: i32, ar: &mut LuaDebug) -> bool {
1190        let mut lvm_ar = lua_vm::debug::LuaDebug::default();
1191        let ok = lua_vm::debug::get_stack(self, level, &mut lvm_ar);
1192        if ok {
1193            ar.i_ci_idx = lvm_ar.i_ci;
1194        } else {
1195            ar.i_ci_idx = None;
1196        }
1197        ok
1198    }
1199
1200    fn get_stack_level(&mut self, level: i32, ar: &mut LuaDebug) -> bool {
1201        LuaStateStubExt::get_stack(self, level, ar)
1202    }
1203
1204    fn get_info(&mut self, what: &[u8], ar: &mut LuaDebug) -> Result<(), LuaError> {
1205        let mut lvm_ar = lua_vm::debug::LuaDebug::default();
1206        lvm_ar.i_ci = ar.i_ci_idx;
1207        let ok = lua_vm::debug::get_info(self, what, &mut lvm_ar);
1208        copy_lvm_debug_to_stub_selective(&lvm_ar, ar, what);
1209        if ok {
1210            Ok(())
1211        } else {
1212            Err(LuaError::runtime(format_args!("invalid option")))
1213        }
1214    }
1215
1216    fn get_debug_info(&mut self, what: &[u8], ar: &mut LuaDebug) -> Result<(), LuaError> {
1217        LuaStateStubExt::get_info(self, what, ar)
1218    }
1219
1220    fn get_local_at(&mut self, ar: &LuaDebug, n: i32) -> Result<Option<Vec<u8>>, LuaError> {
1221        let mut lvm_ar = lua_vm::debug::LuaDebug::default();
1222        lvm_ar.i_ci = ar.i_ci_idx;
1223        Ok(lua_vm::debug::get_local(self, Some(&lvm_ar), n))
1224    }
1225
1226    fn set_local_at(&mut self, ar: &LuaDebug, n: i32) -> Result<Option<Vec<u8>>, LuaError> {
1227        let mut lvm_ar = lua_vm::debug::LuaDebug::default();
1228        lvm_ar.i_ci = ar.i_ci_idx;
1229        Ok(lua_vm::debug::set_local(self, &lvm_ar, n))
1230    }
1231
1232    fn get_param_name(&mut self, fidx: i32, n: i32) -> Result<Option<Vec<u8>>, LuaError> {
1233        let _ = fidx;
1234        Ok(lua_vm::debug::get_local(self, None, n))
1235    }
1236
1237    fn has_frames(&mut self) -> bool {
1238        !self.is_base_ci(self.current_ci_idx())
1239    }
1240
1241    fn lua_traceback(
1242        &mut self,
1243        other: &mut LuaState,
1244        msg: Option<&[u8]>,
1245        level: i32,
1246    ) -> Result<(), LuaError> {
1247        crate::auxlib::traceback(self, Some(other), msg, level)
1248    }
1249
1250    fn upvalue_id(&mut self, fidx: i32, n: i32) -> Result<*mut std::ffi::c_void, LuaError> {
1251        match lua_vm::api::upvalue_id(self, fidx, n) {
1252            Some(id) => Ok(id as *mut std::ffi::c_void),
1253            None => Ok(std::ptr::null_mut()),
1254        }
1255    }
1256
1257    fn join_upvalues(&mut self, fidx1: i32, n1: i32, fidx2: i32, n2: i32) -> Result<(), LuaError> {
1258        lua_vm::api::upvalue_join(self, fidx1, n1, fidx2, n2);
1259        Ok(())
1260    }
1261
1262    /// Pre-collect the chunks supplied by `reader` (a state-aware callback)
1263    /// and forward the accumulated buffer to `lua_vm::api::load`. The streaming
1264    /// loader in `lua_vm::api::load` consumes a `Box<dyn FnMut() -> Option<Vec<u8>>>`
1265    /// that does not take a `&mut LuaState`, so the state-touching reader
1266    /// (e.g. `generic_reader`, which calls a Lua function to produce each
1267    /// chunk) is drained first. C-Lua streams chunks directly through
1268    /// `lua_load`; the materialised path here is observable only for very
1269    /// large source files and is intentional for the Phase-B shim.
1270    fn load_with_reader<F, M: ?Sized>(&mut self, mut reader: F, name: &[u8], mode: &M) -> Result<bool, LuaError>
1271    where
1272        F: FnMut(&mut LuaState) -> Result<Option<Vec<u8>>, LuaError>,
1273        M: AsRef<[u8]>,
1274    {
1275        let mut buf: Vec<u8> = Vec::new();
1276        let mut reader_err: Option<LuaError> = None;
1277        loop {
1278            match reader(self) {
1279                Err(e) => {
1280                    reader_err = Some(e);
1281                    break;
1282                }
1283                Ok(None) => break,
1284                Ok(Some(piece)) => {
1285                    if piece.is_empty() {
1286                        break;
1287                    }
1288                    buf.extend_from_slice(&piece);
1289                }
1290            }
1291        }
1292        if let Some(e) = reader_err {
1293            let msg_value = match e {
1294                LuaError::Runtime(v) | LuaError::Syntax(v) => v,
1295                LuaError::Memory => {
1296                    let s = self.intern_str(b"not enough memory")?;
1297                    LuaValue::Str(s)
1298                }
1299                _ => {
1300                    let s = self.intern_str(b"error in reader function")?;
1301                    LuaValue::Str(s)
1302                }
1303            };
1304            self.push(msg_value);
1305            return Ok(false);
1306        }
1307        let mut once = Some(buf);
1308        let boxed: Box<dyn FnMut() -> Option<Vec<u8>>> = Box::new(move || once.take());
1309        let mode_bytes = mode.as_ref();
1310        let status = lua_vm::api::load(self, boxed, Some(name), Some(mode_bytes))?;
1311        Ok(status == LuaStatus::Ok)
1312    }
1313
1314    fn load_file_ex(&mut self, path: Option<&[u8]>, mode: Option<&[u8]>) -> Result<bool, LuaError> {
1315        let status = crate::auxlib::load_filex(self, path, mode)?;
1316        Ok(status == 0)
1317    }
1318
1319    fn load_file(&mut self, path: Option<&[u8]>) -> Result<bool, LuaError> {
1320        LuaStateStubExt::load_file_ex(self, path, None)
1321    }
1322
1323    fn get_iuservalue(&mut self, idx: i32, n: i32) -> Result<LuaType, LuaError> {
1324        Ok(lua_vm::api::get_i_uservalue(self, idx, n))
1325    }
1326
1327    fn set_iuservalue(&mut self, idx: i32, n: i32) -> Result<bool, LuaError> {
1328        lua_vm::api::set_i_uservalue(self, idx, n)
1329    }
1330
1331    fn get_hook_mask(&mut self) -> u32 {
1332        lua_vm::debug::get_hook_mask(self) as u32
1333    }
1334
1335    fn get_hook_count(&mut self) -> i32 {
1336        lua_vm::debug::get_hook_count(self)
1337    }
1338
1339    /// Approximate "is a debug hook installed?" using the hook event mask.
1340    /// `lua_sethook` clears the mask whenever the hook is uninstalled, so a
1341    /// non-zero mask is equivalent to a non-NULL `L->hook` for the
1342    /// `debug.gethook` call site. Avoids invoking `state.hook()`, which is
1343    /// still a Phase-B `todo!` on `LuaState`.
1344    fn hook_is_set(&mut self) -> bool {
1345        lua_vm::debug::get_hook_mask(self) != 0
1346    }
1347
1348    /// Hooks installed through the debug library use the Lua hook trampoline
1349    /// and store the real Lua callback in registry[HOOKKEY].
1350    fn hook_is_internal_lua_hook(&mut self) -> bool {
1351        lua_vm::debug::get_hook_mask(self) != 0
1352    }
1353
1354    fn set_c_stack_limit(&mut self, limit: i32) -> Result<i32, LuaError> {
1355        let clamped = if limit < 0 { 0u32 } else { limit as u32 };
1356        Ok(lua_vm::state::set_c_stack_limit(self, clamped))
1357    }
1358
1359    /// `lua_close(L)` destroys a Lua state. In Rust the state's resources are
1360    /// released by `Drop` when the owning value goes out of scope, so the
1361    /// in-place `&mut self` form is a no-op. The consuming free function
1362    /// `lua_vm::state::close(state)` is reserved for the top-level shutdown
1363    /// path in `lua-cli`.
1364    fn close(&mut self) {
1365        let _ = self;
1366    }
1367
1368    /// Install (or clear) a debug hook on this thread.
1369    ///
1370    /// C: `LUA_API void lua_sethook (lua_State *L, lua_Hook func, int mask, int count)`
1371    /// (`ldebug.c`).
1372    ///
1373    /// The Phase-B `LuaStateStubExt` signature uses `lua_CFunction` (the
1374    /// stdlib C-function shape: `fn(&mut LuaState) -> Result<usize, LuaError>`)
1375    /// for `f`, whereas the canonical `lua_vm::debug::set_hook` takes a
1376    /// `Box<dyn FnMut(&mut LuaState, &LuaDebug)>` (a true Lua hook, which has
1377    /// access to the active `lua_Debug`). To bridge the two, an installed
1378    /// `lua_CFunction` is wrapped in a trampoline closure that calls it with
1379    /// `state` and discards both the activation record and the
1380    /// `Result<usize, LuaError>` (a hook's return value is ignored by C-Lua).
1381    fn set_hook_full(
1382        &mut self,
1383        f: Option<lua_CFunction>,
1384        mask: u32,
1385        count: i32,
1386    ) -> Result<(), LuaError> {
1387        let hook: Option<Box<dyn FnMut(&mut LuaState, &lua_vm::debug::LuaDebug)>> = match f {
1388            None => None,
1389            Some(func) => Some(Box::new(move |state, _ar| {
1390                let _ = func(state);
1391            })),
1392        };
1393        lua_vm::debug::set_hook(self, hook, mask as i32, count);
1394        Ok(())
1395    }
1396
1397    /// Write `msg` to the host's standard output stream.
1398    ///
1399    /// C: `lua_writestring(s, l)` macro from `lauxlib.h` (defaults to
1400    /// `fwrite(s, 1, l, stdout)`).
1401    ///
1402    /// Delegates to the canonical inherent `LuaState::write_output`. UFCS is
1403    /// used to disambiguate from the trait method (this method) which would
1404    /// otherwise recurse.
1405    fn write_output(&mut self, msg: &[u8]) -> Result<(), LuaError> {
1406        LuaState::write_output(self, msg)
1407    }
1408
1409    /// `t[n] = v`, where `t` is the value at `idx` and `v` is popped from the
1410    /// stack top. Honours `__newindex`.
1411    ///
1412    /// C: `LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n)`.
1413    fn table_set_i(&mut self, idx: i32, n: i64) -> Result<(), LuaError> {
1414        LuaState::table_set_i(self, idx, n)
1415    }
1416
1417    /// Allocate a fresh full-userdata, push it on the stack, and return a
1418    /// `GcRef` to it. `name` is advisory (callers typically follow up with
1419    /// `set_metatable_by_name(name)`).
1420    ///
1421    /// C-correspondent: `lua_newuserdatauv(L, size, nuvalue)` plus the
1422    /// auxiliary `luaL_setmetatable` pattern. The Rust signature carries
1423    /// `name` for caller convenience as documented on the inherent method.
1424    fn new_userdata_typed(
1425        &mut self,
1426        name: &[u8],
1427        size: usize,
1428        nuvalue: i32,
1429    ) -> Result<GcRef<LuaUserData>, LuaError> {
1430        LuaState::new_userdata_typed(self, name, size, nuvalue)
1431    }
1432}
1433
1434/// Copy populated fields from the canonical `lua_vm::debug::LuaDebug` into
1435/// the Phase-B stub `LuaDebug`. The two structs diverge on a few field types
1436/// (e.g. `what` is a single byte tag in the stub vs. `Option<&'static [u8]>`
1437/// in the canonical struct, `short_src` is `Vec<u8>` vs. fixed array).
1438/// Copy only the fields that `lua_getinfo`'s `what` string actually populates
1439/// in C. Mirrors `auxgetinfo` in `ldebug.c`: each option byte writes a disjoint
1440/// subset of `lua_Debug`. Calling `get_info` with one option string must not
1441/// clobber fields populated by an earlier call with a different option string
1442/// (a pattern the auxiliary library relies on — `pushglobalfuncname` calls
1443/// `lua_getinfo(L, "f", ar)` and expects the previously-set `namewhat`/`what`/
1444/// `short_src`/`linedefined` to survive).
1445fn copy_lvm_debug_to_stub_selective(
1446    src: &lua_vm::debug::LuaDebug,
1447    dst: &mut LuaDebug,
1448    what: &[u8],
1449) {
1450    dst.i_ci_idx = src.i_ci;
1451    for &ch in what {
1452        match ch {
1453            b'S' => {
1454                dst.what = match src.what {
1455                    Some(b"Lua") => b'L',
1456                    Some(b"C") => b'C',
1457                    Some(b"main") => b'm',
1458                    _ => 0,
1459                };
1460                dst.source = src.source.clone().unwrap_or_default();
1461                let zero = src
1462                    .short_src
1463                    .iter()
1464                    .position(|&b| b == 0)
1465                    .unwrap_or(src.short_src.len());
1466                dst.short_src = src.short_src[..zero].to_vec();
1467                dst.linedefined = src.linedefined;
1468                dst.lastlinedefined = src.lastlinedefined;
1469            }
1470            b'l' => {
1471                dst.currentline = src.currentline;
1472            }
1473            b'u' => {
1474                dst.nups = src.nups;
1475                dst.nparams = src.nparams;
1476                dst.isvararg = src.isvararg;
1477            }
1478            b't' => {
1479                dst.istailcall = src.istailcall;
1480            }
1481            b'n' => {
1482                dst.name = src.name.clone();
1483                dst.namewhat = src.namewhat.map(|s| s.to_vec()).unwrap_or_default();
1484            }
1485            b'r' => {
1486                dst.ftransfer = src.ftransfer;
1487                dst.ntransfer = src.ntransfer;
1488            }
1489            _ => {}
1490        }
1491    }
1492}
1493
1494const STUB_LUA_REGISTRYINDEX: i32 = -(1_000_000) - 1000;
1495
1496struct StubBStr<'a>(&'a [u8]);
1497
1498impl<'a> std::fmt::Display for StubBStr<'a> {
1499    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1500        use std::fmt::Write as _;
1501        for &b in self.0 {
1502            if b.is_ascii() {
1503                f.write_char(b as char)?;
1504            } else {
1505                write!(f, "\\x{:02x}", b)?;
1506            }
1507        }
1508        Ok(())
1509    }
1510}
1511
1512// ──────────────────────────────────────────────────────────────────────────
1513// PORT STATUS
1514//   source:        (Phase-B reconcile shim; no C source)
1515//   target_crate:  lua-stdlib
1516//   confidence:    high
1517//   todos:         0
1518//   port_notes:    3
1519//   unsafe_blocks: 0
1520//   notes:         Re-exports lua_vm::state::LuaState (canonical owner per
1521//                  harness/type-vocabulary.tsv); the LuaStateStubExt trait
1522//                  carries every Phase-A stub method as a
1523//                  todo!("phase-b-reconcile: …") body so the rest of
1524//                  lua-stdlib keeps compiling while the canonical API
1525//                  catches up.
1526// ──────────────────────────────────────────────────────────────────────────