lua_vm/tagmethods.rs
1//! Tag methods (metamethods) — ported from `ltm.c` / `ltm.h`.
2//!
3//! Every metamethod name (`__index`, `__add`, …) is interned on `GlobalState`
4//! during `init()`. Lookup helpers (`get_tm`, `get_tm_by_obj`) return
5//! `LuaValue::Nil` when no metamethod is present; callers check with
6//! `value.is_nil()` (the `notm` macro in C).
7
8use std::borrow::Cow;
9
10use crate::state::LuaState;
11#[allow(unused_imports)] use crate::prelude::*;
12use lua_types::{CallInfoIdx, GcRef, LuaError, LuaType, LuaValue, StackIdx};
13
14// ── TagMethod enum (from ltm.h `TMS`) ────────────────────────────────────────
15
16/// Metamethod selector; one variant per `__xxx` event, in ORDER TM.
17///
18/// The discriminant values are load-bearing: they index into
19/// `GlobalState.tmname` and are used as bit positions in `Table.flags`.
20/// Do **not** reorder without grepping ORDER TM / ORDER OP.
21///
22#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
23#[repr(u8)]
24pub(crate) enum TagMethod {
25 Index = 0,
26 NewIndex,
27 Gc,
28 Mode,
29 Len,
30 Eq,
31 Add,
32 Sub,
33 Mul,
34 Mod,
35 Pow,
36 Div,
37 IDiv,
38 BAnd,
39 BOr,
40 BXor,
41 Shl,
42 Shr,
43 Unm,
44 BNot,
45 Lt,
46 Le,
47 Concat,
48 Call,
49 Close,
50 N,
51}
52
53impl TagMethod {
54 /// Convert a raw u8 discriminant to a `TagMethod`.
55 /// Returns `TagMethod::N` (sentinel) if `v >= TM_N`.
56 ///
57 /// PORT NOTE: reshaped for borrowck — C casts freely; Rust requires an explicit map.
58 pub(crate) fn from_u8(v: u8) -> Self {
59 match v {
60 0 => TagMethod::Index,
61 1 => TagMethod::NewIndex,
62 2 => TagMethod::Gc,
63 3 => TagMethod::Mode,
64 4 => TagMethod::Len,
65 5 => TagMethod::Eq,
66 6 => TagMethod::Add,
67 7 => TagMethod::Sub,
68 8 => TagMethod::Mul,
69 9 => TagMethod::Mod,
70 10 => TagMethod::Pow,
71 11 => TagMethod::Div,
72 12 => TagMethod::IDiv,
73 13 => TagMethod::BAnd,
74 14 => TagMethod::BOr,
75 15 => TagMethod::BXor,
76 16 => TagMethod::Shl,
77 17 => TagMethod::Shr,
78 18 => TagMethod::Unm,
79 19 => TagMethod::BNot,
80 20 => TagMethod::Lt,
81 21 => TagMethod::Le,
82 22 => TagMethod::Concat,
83 23 => TagMethod::Call,
84 24 => TagMethod::Close,
85 _ => TagMethod::N,
86 }
87 }
88}
89
90/// Number of real metamethods (= `TagMethod::N as usize`).
91///
92pub(crate) const TM_N: usize = TagMethod::N as usize;
93
94// ── Type-name table (from ltm.h / ltm.c `luaT_typenames_`) ──────────────────
95
96//
97// "no value",
98// "nil", "boolean", udatatypename, "number",
99// "string", "table", "function", udatatypename, "thread",
100// "upvalue", "proto"
101// };
102//
103// Indexed as `luaT_typenames_[(x) + 1]` where x is the raw LuaType integer
104// (LUA_TNONE = -1, LUA_TNIL = 0, …, LUA_TPROTO = 10).
105// LUA_TOTALTYPES = LUA_TPROTO + 2 = 12 entries.
106//
107// PORT NOTE: C uses `const char*`; Rust uses `&'static [u8]` throughout.
108pub(crate) static TYPE_NAMES: &[&[u8]] = &[
109 b"no value", // index 0 → LUA_TNONE (-1 + 1)
110 b"nil", // index 1 → LUA_TNIL ( 0 + 1)
111 b"boolean", // index 2 → LUA_TBOOLEAN
112 b"userdata", // index 3 → LUA_TLIGHTUSERDATA
113 b"number", // index 4 → LUA_TNUMBER
114 b"string", // index 5 → LUA_TSTRING
115 b"table", // index 6 → LUA_TTABLE
116 b"function", // index 7 → LUA_TFUNCTION
117 b"userdata", // index 8 → LUA_TUSERDATA
118 b"thread", // index 9 → LUA_TTHREAD
119 b"upvalue", // index 10 → LUA_TUPVAL
120 b"proto", // index 11 → LUA_TPROTO
121];
122
123/// Return the human-readable type name for a `LuaType`.
124///
125///
126/// Panics in debug builds if `t` is out of the expected range (shouldn't
127/// happen with a well-formed `LuaType`).
128pub(crate) fn type_name(t: LuaType) -> &'static [u8] {
129 let idx = (t as i32 + 1) as usize;
130 TYPE_NAMES.get(idx).copied().unwrap_or(b"?")
131}
132
133// ── luaT_init ────────────────────────────────────────────────────────────────
134
135/// Intern all metamethod name strings and pin them in the GC.
136///
137/// Must be called exactly once during `LuaState` initialization, before any
138/// metamethod lookup. After this call, `GlobalState.tmname[i]` holds the
139/// interned `LuaString` for metamethod `i`.
140pub(crate) fn init(state: &mut LuaState) -> Result<(), LuaError> {
141 // "__index", "__newindex",
142 // "__gc", "__mode", "__len", "__eq",
143 // "__add", "__sub", "__mul", "__mod", "__pow",
144 // "__div", "__idiv",
145 // "__band", "__bor", "__bxor", "__shl", "__shr",
146 // "__unm", "__bnot", "__lt", "__le",
147 // "__concat", "__call", "__close"
148 // };
149 const EVENT_NAMES: &[&[u8]] = &[
150 b"__index",
151 b"__newindex",
152 b"__gc",
153 b"__mode",
154 b"__len",
155 b"__eq",
156 b"__add",
157 b"__sub",
158 b"__mul",
159 b"__mod",
160 b"__pow",
161 b"__div",
162 b"__idiv",
163 b"__band",
164 b"__bor",
165 b"__bxor",
166 b"__shl",
167 b"__shr",
168 b"__unm",
169 b"__bnot",
170 b"__lt",
171 b"__le",
172 b"__concat",
173 b"__call",
174 b"__close",
175 ];
176 debug_assert!(EVENT_NAMES.len() == TM_N);
177
178 // PORT NOTE: The C `tmname[TM_N]` is a fixed-size array on `global_State`;
179 // the Rust port uses `Vec<GcRef<LuaString>>` initialized empty in
180 // `lua_state_init`, so we must grow it to `TM_N` before indexed assignment.
181 if state.global().tmname.len() < TM_N {
182 let pad = state.intern_str(b"")?;
183 state.global_mut().tmname.resize(TM_N, pad);
184 }
185
186 // G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);
187 // luaC_fix(L, obj2gco(G(L)->tmname[i])); /* never collect these names */
188 // }
189 for (i, &name) in EVENT_NAMES.iter().enumerate() {
190 let interned = state.intern_str(name)?;
191 state.global_mut().tmname[i] = interned.clone();
192 // Pin the string so the GC never collects it.
193 // TODO(port): luaC_fix API on gc() is TBD; no-op in Phase A–C (Rc keeps it alive)
194 state.gc().fix_object(&interned);
195 }
196 Ok(())
197}
198
199// ── luaT_gettmbyobj ──────────────────────────────────────────────────────────
200
201/// Look up a metamethod for any Lua value by dispatching on its type.
202///
203/// Tables and full userdata have per-object metatables; all other types use
204/// the per-type metatables on `GlobalState`. Returns `LuaValue::Nil` when
205/// neither the object nor its type has a metatable, or when the metatable
206/// does not contain the requested metamethod.
207pub(crate) fn get_tm_by_obj(
208 state: &mut LuaState,
209 o: &LuaValue,
210 event: TagMethod,
211) -> LuaValue {
212 // case LUA_TTABLE: mt = hvalue(o)->metatable; break;
213 // case LUA_TUSERDATA: mt = uvalue(o)->metatable; break;
214 // default: mt = G(L)->mt[ttype(o)];
215 // }
216 //
217 // TODO(port): `GcRef<LuaTable>` access pattern (direct field vs borrow) is
218 // TBD pending the GcRef/RefCell decision in Phase B; using `.metatable()`
219 // accessor here as a placeholder.
220 let mt: Option<GcRef<lua_types::value::LuaTable>> = match o {
221 LuaValue::Table(t) => t.metatable(),
222 LuaValue::UserData(u) => u.metatable(),
223 _ => {
224 let type_idx = o.base_type() as usize;
225 state.global().mt[type_idx].clone()
226 }
227 };
228
229 match mt {
230 Some(mt_ref) => {
231 // Clone the name string before the table lookup to avoid borrow conflict.
232 let ename = state.global().tmname[event as usize].clone();
233 mt_ref.get_short_str(&ename)
234 }
235 None => LuaValue::Nil,
236 }
237}
238
239// ── luaT_objtypename ─────────────────────────────────────────────────────────
240
241/// Return the human-readable type name for a Lua value without any heap
242/// allocation in the common case.
243///
244/// For tables and full userdata whose metatable defines `__name` as a string,
245/// returns `Cow::Owned` with the custom name bytes. For every other case
246/// returns `Cow::Borrowed` pointing into the static `TYPE_NAMES` table —
247/// no allocation, no interning, no `LuaState` access required.
248///
249/// PORT NOTE: C returns `const char*` — either a pointer into a GC-managed
250/// `LuaString` or a pointer into the static `luaT_typenames_` array. Rust
251/// models this as `Cow<'static, [u8]>`: `Borrowed` for static names,
252/// `Owned` only when the metatable `__name` field overrides the default.
253/// Uses `LuaTable::get_str_bytes` (linear byte-scan) instead of
254/// `intern_str` + `get_short_str` so the lookup is infallible and requires
255/// no mutable state access.
256pub(crate) fn obj_type_name_cow(o: &LuaValue) -> Cow<'static, [u8]> {
257 if matches!(o, LuaValue::LightUserData(_)) {
258 return Cow::Borrowed(b"light userdata");
259 }
260 // (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL))
261 let mt: Option<GcRef<lua_types::value::LuaTable>> = match o {
262 LuaValue::Table(t) => t.metatable(),
263 LuaValue::UserData(u) => u.metatable(),
264 _ => None,
265 };
266 if let Some(mt_ref) = mt {
267 // Uses get_str_bytes (raw byte scan) rather than intern_str + get_short_str
268 // so no mutable state is needed and no error can propagate.
269 let name_val = mt_ref.get_str_bytes(b"__name");
270 if let LuaValue::Str(s) = name_val {
271 return Cow::Owned(s.as_bytes().to_vec());
272 }
273 }
274 Cow::Borrowed(type_name(o.base_type()))
275}
276
277/// Compatibility wrapper returning `Vec<u8>` for callers that have not yet
278/// migrated to `obj_type_name_cow`. Always allocates; prefer
279/// `obj_type_name_cow` for allocation-free lookup in error-path code.
280///
281/// PORT NOTE: `state` parameter retained for API compatibility; it is no
282/// longer used since the implementation delegates to `obj_type_name_cow`.
283/// Fallibility (`Result`) is also retained for the same reason.
284pub(crate) fn obj_type_name(_state: &mut LuaState, o: &LuaValue) -> Result<Vec<u8>, LuaError> {
285 Ok(obj_type_name_cow(o).into_owned())
286}
287
288// ── luaT_callTM ──────────────────────────────────────────────────────────────
289
290// const TValue *p2, const TValue *p3)
291/// Call tag method `f` with three arguments, discarding any return values.
292///
293/// Used for metamethods like `__gc` and `__close` that take three operands
294/// and whose return value is irrelevant.
295///
296/// If the current call frame is Lua bytecode (`isLuacode`), the metamethod
297/// may yield; otherwise yielding is suppressed (`callnoyield`).
298pub(crate) fn call_tm(
299 state: &mut LuaState,
300 f: LuaValue,
301 p1: LuaValue,
302 p2: LuaValue,
303 p3: LuaValue,
304) -> Result<(), LuaError> {
305 let func = state.top_idx();
306 //
307 // PORT NOTE: In C these are direct writes into the EXTRA_STACK reserve
308 // area above the official top. In Rust we use push() which manages
309 // capacity; the semantic result is identical.
310 state.push(f);
311 state.push(p1);
312 state.push(p2);
313 state.push(p3);
314 // luaD_call(L, func, 0);
315 // else
316 // luaD_callnoyield(L, func, 0);
317 //
318 // TODO(port): `do_call(func, nresults)` vs `call_from(func, nresults)` —
319 // exact Rust API name TBD; nargs is implicit as `top - func - 1`.
320 if state.current_ci().is_lua_code() {
321 state.do_call(func, 0)?;
322 } else {
323 state.do_call_no_yield(func, 0)?;
324 }
325 Ok(())
326}
327
328// ── luaT_callTMres ───────────────────────────────────────────────────────────
329
330// const TValue *p2, StkId res)
331/// Call tag method `f` with two arguments, writing the single result into
332/// the stack slot at index `res`.
333///
334/// `res` is a `StackIdx` (index-stable across stack reallocation) that must
335/// refer to a pre-existing or scratch slot. After return, the stack top is
336/// back to what it was before the call (i.e. `top == res`).
337///
338/// C uses `savestack`/`restorestack` (byte-offset from base) to preserve `res`
339/// across potential stack reallocations inside `luaD_call`. In Rust, `StackIdx`
340/// is already an index and needs no save/restore.
341pub(crate) fn call_tm_res(
342 state: &mut LuaState,
343 f: LuaValue,
344 p1: LuaValue,
345 p2: LuaValue,
346 res: StackIdx,
347) -> Result<(), LuaError> {
348 // savestack → StackIdx is already the stable byte-offset analogue; no-op.
349
350 let func = state.top_idx();
351 state.push(f);
352 state.push(p1);
353 state.push(p2);
354
355 // luaD_call(L, func, 1);
356 // else
357 // luaD_callnoyield(L, func, 1);
358 //
359 // TODO(port): same `do_call` API question as in call_tm above.
360 if state.current_ci().is_lua_code() {
361 state.do_call(func, 1)?;
362 } else {
363 state.do_call_no_yield(func, 1)?;
364 }
365
366 // restorestack → StackIdx is already stable; `res` is unchanged.
367
368 // Pre-decrement top, then copy that slot to res.
369 let result_val = state.pop();
370 state.set_at(res, result_val);
371 Ok(())
372}
373
374// ── callbinTM (static) ────────────────────────────────────────────────────────
375
376// StkId res, TMS event)
377/// Try to find and call a binary tag method for `event`.
378///
379/// Checks `p1` first, then `p2`, for a metamethod. If neither has one,
380/// returns `false` and leaves `res` unmodified. If found, calls it with
381/// `(p1, p2)` as arguments, writes the result to slot `res`, and returns `true`.
382fn call_bin_tm(
383 state: &mut LuaState,
384 p1: &LuaValue,
385 p2: &LuaValue,
386 res: StackIdx,
387 event: TagMethod,
388) -> Result<bool, LuaError> {
389 let tm = get_tm_by_obj(state, p1, event);
390 // tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
391 let tm = if tm.is_nil() {
392 get_tm_by_obj(state, p2, event)
393 } else {
394 tm
395 };
396 if tm.is_nil() {
397 return Ok(false);
398 }
399 // Clone p1/p2 before the mutable borrow of `state` via call_tm_res.
400 call_tm_res(state, tm, p1.clone(), p2.clone(), res)?;
401 Ok(true)
402}
403
404/// Lua 5.3 core string→integer coercion for bitwise ops.
405///
406/// Returns:
407/// - `Some(Ok(()))` — both operands coerced to integers; the op was computed
408/// and written to `res`.
409/// - `Some(Err(..))` — an operand is a numeric-but-non-integral string
410/// (e.g. `"3.5"`, `"0xff..ff.0"`); raises "number has no integer
411/// representation" (`luaG_tointerror`), matching lua5.3.6.
412/// - `None` — coercion is impossible (an operand is a non-numeric string such
413/// as `"abc"`); the caller falls through to its normal error path, which
414/// raises "perform bitwise operation on".
415///
416/// The 5.3-only gate and the bitwise-event filter are applied by the caller.
417fn try_bitwise_strconv_53(
418 state: &mut LuaState,
419 p1: &LuaValue,
420 p1_idx: Option<StackIdx>,
421 p2: &LuaValue,
422 p2_idx: Option<StackIdx>,
423 res: StackIdx,
424 event: TagMethod,
425) -> Option<Result<(), LuaError>> {
426 // Both operands must be number-ish (integer, float, or a string that
427 // parses as a number). If either is genuinely non-numeric, bail to the
428 // caller's "perform bitwise operation on" path.
429 let n1 = p1.to_number_with_strconv();
430 let n2 = p2.to_number_with_strconv();
431 if n1.is_none() || n2.is_none() {
432 return None;
433 }
434 // Both are number-ish. Now require integer representations. If a number-ish
435 // operand has no integer representation (non-integral numeric string or
436 // float), 5.3 raises "number has no integer representation".
437 let i1 = p1.to_integer_with_strconv();
438 let i2 = p2.to_integer_with_strconv();
439 let (i1, i2) = match (i1, i2) {
440 (Some(a), Some(b)) => (a, b),
441 _ => {
442 let p1_idx = p1_idx.unwrap_or(StackIdx(0));
443 let p2_idx = p2_idx.unwrap_or(StackIdx(0));
444 return Some(Err(crate::debug::to_int_error(
445 state,
446 p1,
447 Some(p1_idx),
448 p2,
449 Some(p2_idx),
450 )));
451 }
452 };
453 let result = match event {
454 TagMethod::BAnd => i1 & i2,
455 TagMethod::BOr => i1 | i2,
456 TagMethod::BXor => i1 ^ i2,
457 TagMethod::Shl => crate::vm::shiftl(i1, i2),
458 TagMethod::Shr => crate::vm::shiftl(i1, i2.wrapping_neg()),
459 // Unary `~x` arrives here as a binary event with p1 == p2; `~i1`.
460 TagMethod::BNot => !i1,
461 _ => return None,
462 };
463 state.set_at(res, LuaValue::Int(result));
464 Some(Ok(()))
465}
466
467// ── luaT_trybinTM ────────────────────────────────────────────────────────────
468
469// StkId res, TMS event)
470/// Attempt a binary metamethod call; raise a type error if no metamethod exists.
471///
472/// For bitwise operations, further distinguishes between:
473/// - Both operands are numbers but not integers → `LuaError::int_overflow`
474/// (`luaG_tointerror` — "number has no integer representation")
475/// - At least one operand is not a number → `LuaError::arith_error` with
476/// "perform bitwise operation on"
477///
478/// All other missing metamethods raise `LuaError::arith_error` with
479/// "perform arithmetic on".
480pub(crate) fn try_bin_tm(
481 state: &mut LuaState,
482 p1: &LuaValue,
483 p1_idx: Option<StackIdx>,
484 p2: &LuaValue,
485 p2_idx: Option<StackIdx>,
486 res: StackIdx,
487 event: TagMethod,
488) -> Result<(), LuaError> {
489 // Lua 5.3 arith error wording with string operands. In the shared (5.4)
490 // model arithmetic metamethods (`__add`/`__sub`/…) are installed on the
491 // string metatable, so a failed arith fast path that has a string operand
492 // dispatches to the string-library `trymt`, which raises `attempt to <op> a
493 // '<t>' with a '<t>'`, adds a spurious `[C]: in metamethod '<op>'` frame, and
494 // cannot produce operand varinfo (it runs as a C function with no access to
495 // the calling bytecode registers). 5.3 instead owns arithmetic string
496 // coercion in the core: when the operation cannot succeed it raises `attempt
497 // to perform arithmetic on a <type> value (<varinfo>)`, blaming the operand
498 // that does not coerce to a number.
499 //
500 // The intercept is narrow: it fires ONLY when a string operand cannot be
501 // coerced to a number AND the other operand carries no genuine arith
502 // metamethod of its own. So `t + "5"` (t has `__add`) still dispatches to
503 // t's metamethod via `call_bin_tm`, and the coercible success path
504 // (`"3" + 2`) still flows through the string metamethod below, preserving
505 // 5.3 float-promotion semantics. See specs/followup/5.3-coerce-err.md.
506 if matches!(state.global().lua_version, lua_types::LuaVersion::V53)
507 && matches!(
508 event,
509 TagMethod::Add
510 | TagMethod::Sub
511 | TagMethod::Mul
512 | TagMethod::Mod
513 | TagMethod::Pow
514 | TagMethod::Div
515 | TagMethod::IDiv
516 | TagMethod::Unm
517 )
518 && (matches!(p1, LuaValue::Str(_)) || matches!(p2, LuaValue::Str(_)))
519 {
520 use crate::state::LuaValueExt;
521 let p1_num = p1.to_number_with_strconv().is_some();
522 let p2_num = p2.to_number_with_strconv().is_some();
523 if !(p1_num && p2_num) {
524 // A string operand did not coerce. Only raise the core error here if
525 // the non-string operand has no genuine arith metamethod; otherwise
526 // fall through so `call_bin_tm` dispatches to that real metamethod
527 // (the string metatable's synthetic arith mm is ignored for this
528 // decision — it never produces a useful result for a non-coercible
529 // pairing, it only raises the wrong-version wording).
530 //
531 // Unary minus arrives as a binary event with `p1 == p2`, so there is
532 // no genuine "other operand" — the only metamethod available is the
533 // synthetic string one. Treat it as absent so `-"x"` takes the core
534 // path.
535 let unary = matches!(event, TagMethod::Unm);
536 let other_has_mm = !unary
537 && if matches!(p1, LuaValue::Str(_)) {
538 !get_tm_by_obj(state, p2, event).is_nil()
539 } else {
540 !get_tm_by_obj(state, p1, event).is_nil()
541 };
542 if !other_has_mm {
543 // Point varinfo at the operand that does not coerce, matching C
544 // `luaG_opinterror`. A coercible numeric string counts as a
545 // number, so `'2' * nil` blames `nil`, not `'2'`.
546 let (bad, bad_idx) = if !p1_num {
547 (p1, p1_idx.unwrap_or(StackIdx(0)))
548 } else {
549 (p2, p2_idx.unwrap_or(StackIdx(0)))
550 };
551 return Err(crate::debug::type_error(
552 state, bad, bad_idx, b"perform arithmetic on",
553 ));
554 }
555 }
556 }
557 if !call_bin_tm(state, p1, p2, res, event)? {
558 // Lua 5.3 coerces numeric strings to integers in the *core* bitwise
559 // ops (`& | ~ << >>` and unary `~`), where 5.4/5.5 require a real
560 // number operand and delegate string handling to a (non-existent)
561 // string metamethod. On the 5.3 path, after the metamethod lookup
562 // fails, retry the operation with string→integer coercion before
563 // raising. The boundary semantics (non-integral numeric string →
564 // "no integer representation"; non-numeric string → "perform bitwise
565 // operation") fall out of `to_integer_with_strconv` /
566 // `to_number_with_strconv`. See specs/followup/5.3-coerce-err.md.
567 if matches!(state.global().lua_version, lua_types::LuaVersion::V53)
568 && matches!(
569 event,
570 TagMethod::BAnd
571 | TagMethod::BOr
572 | TagMethod::BXor
573 | TagMethod::Shl
574 | TagMethod::Shr
575 | TagMethod::BNot
576 )
577 && (matches!(p1, LuaValue::Str(_)) || matches!(p2, LuaValue::Str(_)))
578 {
579 if let Some(result) = try_bitwise_strconv_53(state, p1, p1_idx, p2, p2_idx, res, event)
580 {
581 return result;
582 }
583 }
584 // case TM_BAND: case TM_BOR: case TM_BXOR:
585 // case TM_SHL: case TM_SHR: case TM_BNOT: {
586 // if (ttisnumber(p1) && ttisnumber(p2))
587 // luaG_tointerror(L, p1, p2);
588 // else
589 // luaG_opinterror(L, p1, p2, "perform bitwise operation on");
590 // }
591 // /* calls never return, but to avoid warnings: *//* FALLTHROUGH */
592 // default:
593 // luaG_opinterror(L, p1, p2, "perform arithmetic on");
594 // }
595 //
596 // PORT NOTE: the C switch has a dead "FALLTHROUGH" for bitwise cases
597 // because both branches of the inner if/else call noreturn functions.
598 // In Rust `match` has no implicit fallthrough; each arm is self-contained
599 // and explicitly returns `Err(...)`.
600 match event {
601 TagMethod::BAnd
602 | TagMethod::BOr
603 | TagMethod::BXor
604 | TagMethod::Shl
605 | TagMethod::Shr
606 | TagMethod::BNot => {
607 if matches!(p1, LuaValue::Int(_) | LuaValue::Float(_))
608 && matches!(p2, LuaValue::Int(_) | LuaValue::Float(_))
609 {
610 // "(field 'huge')" / "(local 'x')" etc. based on the bytecode that
611 // produced the bad operand.
612 return Err(crate::debug::to_int_error(state, p1, p1_idx, p2, p2_idx));
613 } else {
614 // varinfo on the non-number operand.
615 let p1_idx = p1_idx.unwrap_or(StackIdx(0));
616 let p2_idx = p2_idx.unwrap_or(StackIdx(0));
617 return Err(crate::debug::op_int_error(
618 state, p1, p1_idx, p2, p2_idx, b"perform bitwise operation on",
619 ));
620 }
621 }
622 _ => {
623 // varinfo enriches with "(global 'aaa')" etc.
624 let p1_idx = p1_idx.unwrap_or(StackIdx(0));
625 let p2_idx = p2_idx.unwrap_or(StackIdx(0));
626 return Err(crate::debug::op_int_error(
627 state, p1, p1_idx, p2, p2_idx, b"perform arithmetic on",
628 ));
629 }
630 }
631 }
632 Ok(())
633}
634
635// ── luaT_tryconcatTM ─────────────────────────────────────────────────────────
636
637/// Attempt the `__concat` metamethod on the two values at the top of the stack.
638///
639/// Reads `stack[top-2]` and `stack[top-1]`, searches both for `__concat`,
640/// calls it with `(stack[top-2], stack[top-1])` writing the result back to
641/// `stack[top-2]`, or raises `LuaError::concat_error` if no metamethod exists.
642pub(crate) fn try_concat_tm(state: &mut LuaState) -> Result<(), LuaError> {
643 let top = state.top_idx();
644 // luaG_concaterror(L, s2v(top - 2), s2v(top - 1));
645 //
646 // Clone the operands before any call that might mutate the stack.
647 let p1 = state.get_at(top - 2).clone();
648 let p2 = state.get_at(top - 1).clone();
649 if !call_bin_tm(state, &p1, &p2, top - 2, TagMethod::Concat)? {
650 let p1_ok = matches!(p1, LuaValue::Str(_) | LuaValue::Int(_) | LuaValue::Float(_));
651 let (bad, bad_idx) = if p1_ok { (&p2, top - 1) } else { (&p1, top - 2) };
652 return Err(crate::debug::type_error(state, bad, bad_idx, b"concatenate"));
653 }
654 Ok(())
655}
656
657// ── luaT_trybinassocTM ───────────────────────────────────────────────────────
658
659// int flip, StkId res, TMS event)
660/// Try a binary associative metamethod, optionally swapping the operands.
661///
662/// When `flip` is `true`, operands are exchanged before dispatch. This
663/// implements Lua's symmetry rule: if `a OP b` finds no metamethod on `a`,
664/// the VM retries with `b OP a` (setting flip=true to restore the original
665/// argument order for the call).
666pub(crate) fn try_bin_assoc_tm(
667 state: &mut LuaState,
668 p1: &LuaValue,
669 p1_idx: Option<StackIdx>,
670 p2: &LuaValue,
671 p2_idx: Option<StackIdx>,
672 flip: bool,
673 res: StackIdx,
674 event: TagMethod,
675) -> Result<(), LuaError> {
676 // luaT_trybinTM(L, p2, p1, res, event);
677 // else
678 // luaT_trybinTM(L, p1, p2, res, event);
679 if flip {
680 try_bin_tm(state, p2, p2_idx, p1, p1_idx, res, event)
681 } else {
682 try_bin_tm(state, p1, p1_idx, p2, p2_idx, res, event)
683 }
684}
685
686// ── luaT_trybiniTM ───────────────────────────────────────────────────────────
687
688// int flip, StkId res, TMS event)
689/// Try a binary metamethod where the second operand is an integer constant.
690///
691/// Boxes `i2` as `LuaValue::Int` and delegates to `try_bin_assoc_tm`.
692pub(crate) fn try_bini_tm(
693 state: &mut LuaState,
694 p1: &LuaValue,
695 p1_idx: Option<StackIdx>,
696 i2: i64,
697 flip: bool,
698 res: StackIdx,
699 event: TagMethod,
700) -> Result<(), LuaError> {
701 let aux = LuaValue::Int(i2);
702 // The immediate operand has no stack location, so it gets `None`.
703 try_bin_assoc_tm(state, p1, p1_idx, &aux, None, flip, res, event)
704}
705
706// ── luaT_callorderTM ─────────────────────────────────────────────────────────
707
708// TMS event)
709/// Call an order metamethod (`__lt` or `__le`) and return its boolean result.
710///
711/// Returns `true` if the metamethod returned a truthy value.
712/// Raises `LuaError::order_error` if neither operand has the metamethod.
713///
714/// PORT NOTE: `LUA_COMPAT_LT_LE` (deriving `__le` from `__lt`) is ON by default
715/// in the reference `make macosx` builds of 5.1–5.4 and removed in 5.5. We match
716/// the default-built reference (the pinned oracle, per specs/oracle/CONTRACT.md),
717/// so the fallback is implemented and version-gated: derive for 5.1–5.4, raise
718/// for 5.5.
719pub(crate) fn call_order_tm(
720 state: &mut LuaState,
721 p1: &LuaValue,
722 p2: &LuaValue,
723 event: TagMethod,
724) -> Result<bool, LuaError> {
725 // return !l_isfalse(s2v(L->top.p));
726 //
727 // PORT NOTE: In C, `L->top.p` is used as a scratch slot (written by
728 // callTMres then immediately read) in the EXTRA_STACK reserved area above
729 // the official stack top — the stack top is NOT officially advanced.
730 // In Rust we pass `state.top_idx()` as `res`; call_bin_tm → call_tm_res
731 // pushes 3 values, calls, pops the result back to res, and leaves top ==
732 // res (i.e. top unchanged relative to entry). Reading get_at(res_idx)
733 // after this is safe because the slot was just written and top == res_idx.
734 //
735 // TODO(port): Verify in Phase B that no call path between call_bin_tm
736 // returning and the get_at read can disturb the scratch slot or reset top
737 // below res_idx. The invariant holds as long as do_call with 1 result
738 // leaves exactly one value on the stack above func.
739 let res_idx = state.top_idx();
740 if call_bin_tm(state, p1, p2, res_idx, event)? {
741 // l_isfalse(o) → matches!(o, LuaValue::Nil | LuaValue::Bool(false))
742 let result = state.get_at(res_idx).clone();
743 return Ok(!matches!(result, LuaValue::Nil | LuaValue::Bool(false)));
744 }
745
746 // LUA_COMPAT_LT_LE: in 5.1–5.4 a missing `__le` falls back to `not (b < a)`
747 // via `__lt` with the operands swapped; 5.5 removed this and raises.
748 if event == TagMethod::Le
749 && matches!(
750 state.global().lua_version,
751 lua_types::LuaVersion::V51
752 | lua_types::LuaVersion::V52
753 | lua_types::LuaVersion::V53
754 | lua_types::LuaVersion::V54
755 )
756 {
757 let res_idx = state.top_idx();
758 if call_bin_tm(state, p2, p1, res_idx, TagMethod::Lt)? {
759 // l_isfalse(result): a <= b == not (b < a)
760 let result = state.get_at(res_idx).clone();
761 return Ok(matches!(result, LuaValue::Nil | LuaValue::Bool(false)));
762 }
763 }
764
765 Err(crate::debug::order_error(state, p1, p2))
766}
767
768// ── luaT_callorderiTM ────────────────────────────────────────────────────────
769
770// int flip, int isfloat, TMS event)
771/// Call an order metamethod where the second operand is a primitive int or float.
772///
773/// `v2` is a C `int`; `isfloat` selects whether it is coerced to
774/// `LuaValue::Float` (via `cast_num`) or kept as `LuaValue::Int`.
775/// When `flip` is true the operands are swapped so that `p1` was originally
776/// on the right-hand side.
777pub(crate) fn call_orderi_tm(
778 state: &mut LuaState,
779 p1: &LuaValue,
780 v2: i32,
781 flip: bool,
782 isfloat: bool,
783 event: TagMethod,
784) -> Result<bool, LuaError> {
785 // setfltvalue(&aux, cast_num(v2));
786 // }
787 // else
788 // setivalue(&aux, v2);
789 let aux = if isfloat {
790 LuaValue::Float(v2 as f64)
791 } else {
792 LuaValue::Int(v2 as i64)
793 };
794
795 // p2 = p1; p1 = &aux; /* correct them */
796 // }
797 // else
798 // p2 = &aux;
799 if flip {
800 call_order_tm(state, &aux, p1, event)
801 } else {
802 call_order_tm(state, p1, &aux, event)
803 }
804}
805
806// ── luaT_adjustvarargs ───────────────────────────────────────────────────────
807
808// const Proto *p)
809/// Adjust the stack layout for a vararg function at call entry.
810///
811/// Moves the fixed parameters above the extra (vararg) arguments and copies
812/// the function object alongside them so the function body sees its registers
813/// at the expected offsets. Records the extra-argument count in the CallInfo
814/// for `OP_VARARG` use.
815///
816/// Before call: `[func | fixed... | extra...]`
817/// After call: `[func | nil... | extra... | func′ | fixed...]`
818/// (`ci.func` and `ci.top` are advanced by `actual + 1`.)
819pub(crate) fn adjust_varargs(
820 state: &mut LuaState,
821 nfixparams: i32,
822 ci_idx: CallInfoIdx,
823 proto: &GcRef<lua_types::LuaProto>,
824) -> Result<(), LuaError> {
825 let ci_func: StackIdx = state.call_info[ci_idx.as_usize()].func;
826 let actual = (state.top_idx().0 as i32) - (ci_func.0 as i32) - 1;
827 let nextra = actual - nfixparams;
828 // TODO(phase-b): nextraargs lives inside CallInfoFrame::Lua; needs proper
829 // pattern-match write through state.call_info[..].u.
830 if let crate::state::CallInfoFrame::Lua { ref mut nextraargs, .. } = state.call_info[ci_idx.as_usize()].u {
831 *nextraargs = nextra;
832 }
833
834 let maxstacksize = proto.maxstacksize as i32;
835 state.check_stack(maxstacksize + 1)?;
836
837 // Re-read ci_func after check_stack (stack may have reallocated, but
838 // StackIdx is index-based so the value is still correct).
839 let ci_func: StackIdx = state.call_info[ci_idx.as_usize()].func;
840
841 let func_val = state.get_at(ci_func).clone();
842 state.push(func_val);
843
844 // setobjs2s(L, L->top.p++, ci->func.p + i);
845 // setnilvalue(s2v(ci->func.p + i)); /* erase original parameter (for GC) */
846 // }
847 for i in 1..=nfixparams {
848 // TODO(port): StackIdx is u32; if ci_func + i overflows the u32 range
849 // this panics in debug. In practice `i` is small (≤ 255 params), but
850 // add a saturating or checked add in Phase B.
851 let src: StackIdx = ci_func + i as i32;
852 let param_val = state.get_at(src).clone();
853 state.push(param_val);
854 state.set_at(src, LuaValue::Nil);
855 }
856
857 // TODO(port): `actual + 1` may be negative if `actual < -1` (malformed call);
858 // casting to StackIdx (u32) would underflow. In practice Lua guarantees
859 // actual >= 0 at this point, but add a debug_assert in Phase B.
860 let offset = (actual + 1) as i32;
861 state.call_info[ci_idx.as_usize()].func = state.call_info[ci_idx.as_usize()].func + offset;
862 state.call_info[ci_idx.as_usize()].top = state.call_info[ci_idx.as_usize()].top + offset;
863
864 debug_assert!(state.top_idx().0 <= state.call_info[ci_idx.as_usize()].top.0);
865 Ok(())
866}
867
868// ── luaT_getvarargs ──────────────────────────────────────────────────────────
869
870/// Copy vararg values into the stack starting at `where_idx`.
871///
872/// `wanted` specifies how many values to copy. Pass `wanted < 0` (the
873/// `LUA_MULTRET` convention) to request all available extra arguments; the
874/// stack top is then set to `where_idx + nextra`.
875///
876/// Slots beyond `nextra` but within `wanted` are filled with `LuaValue::Nil`.
877pub(crate) fn get_varargs(
878 state: &mut LuaState,
879 ci_idx: CallInfoIdx,
880 where_idx: StackIdx,
881 wanted: i32,
882) -> Result<(), LuaError> {
883 let nextra: i32 = if let crate::state::CallInfoFrame::Lua { nextraargs, .. } = state.call_info[ci_idx.as_usize()].u { nextraargs } else { 0 };
884
885 // wanted = nextra; /* get all extra arguments available */
886 // checkstackGCp(L, nextra, where); /* ensure stack space */
887 // L->top.p = where + nextra; /* next instruction will need top */
888 // }
889 let wanted: i32 = if wanted < 0 {
890 state.check_stack(nextra)?;
891 state.gc().check_step();
892 // TODO(port): `where_idx + nextra as i32` may overflow if nextra
893 // is very large; checked add in Phase B.
894 state.set_top(where_idx + nextra as i32);
895 nextra
896 } else {
897 wanted
898 };
899
900 // setobjs2s(L, where + i, ci->func.p - nextra + i);
901 //
902 // After adjustvarargs, the extra args live at positions
903 // ci->func - nextra .. ci->func - 1.
904 let ci_func: StackIdx = state.call_info[ci_idx.as_usize()].func;
905 let copy_count = wanted.min(nextra);
906 for i in 0..copy_count {
907 // TODO(port): subtraction on StackIdx (u32) underflows if nextra > ci_func.
908 // Invariant: ci_func >= nextra (enforced by adjustvarargs), but add
909 // a debug_assert in Phase B.
910 let src: StackIdx = ci_func - nextra as i32 + i as i32;
911 let val = state.get_at(src).clone();
912 state.set_at(where_idx + i as i32, val);
913 }
914
915 // setnilvalue(s2v(where + i));
916 for i in copy_count..wanted {
917 state.set_at(where_idx + i as i32, LuaValue::Nil);
918 }
919 Ok(())
920}
921
922// ──────────────────────────────────────────────────────────────────────────────
923// PORT STATUS
924// source: src/ltm.c (271 lines, 15 functions)
925// src/ltm.h (104 lines; merged into this file)
926// target_crate: lua-vm
927// confidence: medium
928// todos: 10
929// port_notes: 7
930// unsafe_blocks: 0 (must be 0 outside explicit unsafe-budget crates)
931// notes:
932// Logic translation is faithful; the main uncertainties are:
933// (1) GcRef<LuaTable> accessor pattern (.metatable() / .borrow().metatable)
934// is TBD — annotated TODO(port) in get_tm_by_obj.
935// (2) call_tm / call_tm_res use a `do_call` / `do_call_no_yield` naming
936// convention that must be confirmed against the LuaState API in Phase B.
937// (3) call_order_tm uses `top_idx()` as a scratch slot matching C's
938// EXTRA_STACK convention — annotated with TODO(port) for Phase B review.
939// (4) StackIdx (u32) arithmetic in adjust_varargs / get_varargs can
940// underflow — annotated TODO(port); add checked arithmetic in Phase B.
941// (5) PERF(port) callout for obj_type_name Vec alloc retired: the
942// allocation-free `obj_type_name_cow` is now the canonical
943// implementation; `obj_type_name` is a compat wrapper. Existing
944// callers can migrate to `obj_type_name_cow` to avoid the
945// `.into_owned()` allocation.
946// (6) LUA_COMPAT_LT_LE block in call_order_tm omitted per PORTING.md §13.
947// (7) intern_str fallibility in obj_type_name resolved: obj_type_name_cow
948// uses get_str_bytes (infallible) and obj_type_name wraps it with
949// Ok(...), so no OOM propagation risk remains.
950// (8) luaC_fix in init() is stubbed as gc().fix_object() — no-op Phase A-C.
951// ──────────────────────────────────────────────────────────────────────────────