Skip to main content

lua_stdlib/
table_lib.rs

1//! Rust port of `ltablib.c` — Lua `table` standard library.
2//!
3//! Provides: `table.concat`, `table.insert`, `table.move`, `table.pack`,
4//! `table.remove`, `table.sort`, `table.unpack`.
5//!
6//! C source: `reference/lua-5.4.7/src/ltablib.c` (430 lines, 14 functions)
7
8use lua_types::{GcRef, LuaError, LuaTable, LuaType, LuaValue};
9use crate::state_stub::{LuaState, LuaStateStubExt as _, CompareOp};
10use lua_vm::state::LuaTableRefExt as _;
11
12// ─── Operation flags ──────────────────────────────────────────────────────────
13const TAB_R: u32 = 1;
14const TAB_W: u32 = 2;
15const TAB_L: u32 = 4;
16const TAB_RW: u32 = TAB_R | TAB_W;
17
18const RANLIMIT: u32 = 100;
19
20type IdxT = u32;
21
22// ─── Internal helpers ─────────────────────────────────────────────────────────
23
24/// currently sitting at stack depth `n`; returns `true` if the result is not nil.
25///
26/// ```c
27/// static int checkfield (lua_State *L, const char *key, int n) {
28///   lua_pushstring(L, key);
29///   return (lua_rawget(L, -n) != LUA_TNIL);
30/// }
31/// ```
32fn check_field(state: &mut LuaState, key: &[u8], n: i32) -> Result<bool, LuaError> {
33    // TODO(port): state.push_string pushes a Lua string from &[u8]; verify method name
34    state.push_string(key)?;
35    // raw_get(-n): looks up MT[key] (MT is at -n after the key push), replaces key with value
36    let ty = state.raw_get(-n)?;
37    Ok(ty != LuaType::Nil)
38}
39
40/// metatable with the metamethods required by `what`
41/// (`TAB_R` → `__index`, `TAB_W` → `__newindex`, `TAB_L` → `__len`).
42///
43/// ```c
44/// static void checktab (lua_State *L, int arg, int what) {
45///   if (lua_type(L, arg) != LUA_TTABLE) {
46///     int n = 1;
47///     if (lua_getmetatable(L, arg) &&
48///         (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
49///         (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
50///         (!(what & TAB_L) || checkfield(L, "__len", ++n))) {
51///       lua_pop(L, n);
52///     }
53///     else
54///       luaL_checktype(L, arg, LUA_TTABLE);
55///   }
56/// }
57/// ```
58///
59/// PORT NOTE: stack cleanup on the error path is elided here (in C, `longjmp`
60/// unwinds automatically). Phase B should add cleanup before the `check_arg_type`
61/// call to leave the stack consistent.
62fn check_tab(state: &mut LuaState, arg: i32, what: u32) -> Result<(), LuaError> {
63    if state.type_at(arg) == LuaType::Table {
64        return Ok(());
65    }
66    // `n` tracks how many items have been pushed (MT + checked field values).
67    let mut n: i32 = 1;
68    // TODO(port): state.get_metatable returns bool (pushes MT if found); verify method name
69    let has_mt = state.get_metatable(arg)?;
70    let mut ok = has_mt;
71
72    // Short-circuit: each field is only checked if all previous checks passed.
73    if ok && (what & TAB_R) != 0 {
74        n += 1;
75        ok = check_field(state, b"__index", n)?;
76    }
77    if ok && (what & TAB_W) != 0 {
78        n += 1;
79        ok = check_field(state, b"__newindex", n)?;
80    }
81    if ok && (what & TAB_L) != 0 {
82        n += 1;
83        ok = check_field(state, b"__len", n)?;
84    }
85
86    if ok {
87        state.pop_n(n as usize);
88        Ok(())
89    } else {
90        state.check_arg_type(arg, LuaType::Table)
91    }
92}
93
94///
95/// Check that argument `n` is a table (or table-like per `w`) and return its length.
96fn aux_getn(state: &mut LuaState, n: i32, w: u32) -> Result<i64, LuaError> {
97    check_tab(state, n, w | TAB_L)?;
98    // TODO(port): state.length_at applies the `#` operator and returns i64; verify method name
99    state.length_at(n)
100}
101
102#[inline]
103fn plain_table_at(state: &mut LuaState, idx: i32) -> Option<GcRef<LuaTable>> {
104    match state.value_at(idx) {
105        LuaValue::Table(tbl) if tbl.metatable().is_none() => Some(tbl),
106        _ => None,
107    }
108}
109
110#[inline]
111fn raw_set_int(
112    state: &mut LuaState,
113    tbl: GcRef<LuaTable>,
114    key: i64,
115    value: LuaValue,
116) -> Result<(), LuaError> {
117    state.gc_table_barrier_back(&tbl, &value);
118    tbl.raw_set_int(state, key, value)
119}
120
121// ─── table.insert ─────────────────────────────────────────────────────────────
122
123///
124/// ```c
125/// static int tinsert (lua_State *L) {
126///   lua_Integer pos;
127///   lua_Integer e = aux_getn(L, 1, TAB_RW);
128///   e = luaL_intop(+, e, 1);
129///   switch (lua_gettop(L)) {
130///     case 2: { pos = e; break; }
131///     case 3: {
132///       lua_Integer i;
133///       pos = luaL_checkinteger(L, 2);
134///       luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2,
135///                        "position out of bounds");
136///       for (i = e; i > pos; i--) {
137///         lua_geti(L, 1, i - 1);
138///         lua_seti(L, 1, i);
139///       }
140///       break;
141///     }
142///     default:
143///       return luaL_error(L, "wrong number of arguments to 'insert'");
144///   }
145///   lua_seti(L, 1, pos);
146///   return 0;
147/// }
148/// ```
149pub fn insert(state: &mut LuaState) -> Result<usize, LuaError> {
150    let mut e = aux_getn(state, 1, TAB_RW)?;
151    e = (e as u64).wrapping_add(1) as i64;
152    let plain_table = plain_table_at(state, 1);
153
154    let pos: i64 = match state.get_top() {
155        2 => {
156            if let Some(tbl) = plain_table {
157                let value = state.value_at(2);
158                raw_set_int(state, tbl, e, value)?;
159                state.pop_n(1);
160                return Ok(0);
161            }
162            e
163        }
164        3 => {
165            let pos = state.check_arg_integer(2)?;
166            // Checks 1 <= pos <= e (wrapping subtraction catches pos <= 0)
167            if !((pos as u64).wrapping_sub(1) < (e as u64)) {
168                return Err(lua_vm::debug::arg_error_impl(state, 2, b"position out of bounds"));
169            }
170            if let Some(tbl) = plain_table {
171                let value = state.value_at(3);
172                let mut i = e;
173                while i > pos {
174                    let shifted = tbl.get_int(i - 1);
175                    raw_set_int(state, tbl, i, shifted)?;
176                    i -= 1;
177                }
178                raw_set_int(state, tbl, pos, value)?;
179                state.pop_n(1);
180                return Ok(0);
181            }
182            // Cache the table once to avoid re-resolving stack slot 1 on every
183            // iteration of the shift loop. C's lua_geti is a single pointer
184            // arithmetic operation; our index_to_value is a function call with
185            // branches, so this saves ~2N index resolutions for shift count N.
186            let tbl = state.value_at(1);
187            let mut i = e;
188            while i > pos {
189                state.table_get_i_value(&tbl, i - 1)?;
190                state.table_set_i_value(&tbl, i)?;
191                i -= 1;
192            }
193            pos
194        }
195        _ => {
196            return Err(LuaError::runtime(format_args!(
197                "wrong number of arguments to 'insert'"
198            )));
199        }
200    };
201    state.table_set_i(1, pos)?;
202    Ok(0)
203}
204
205// ─── table.remove ─────────────────────────────────────────────────────────────
206
207///
208/// ```c
209/// static int tremove (lua_State *L) {
210///   lua_Integer size = aux_getn(L, 1, TAB_RW);
211///   lua_Integer pos = luaL_optinteger(L, 2, size);
212///   if (pos != size)
213///     luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2,
214///                      "position out of bounds");
215///   lua_geti(L, 1, pos);
216///   for ( ; pos < size; pos++) {
217///     lua_geti(L, 1, pos + 1);
218///     lua_seti(L, 1, pos);
219///   }
220///   lua_pushnil(L);
221///   lua_seti(L, 1, pos);
222///   return 1;
223/// }
224/// ```
225pub fn remove(state: &mut LuaState) -> Result<usize, LuaError> {
226    let size = aux_getn(state, 1, TAB_RW)?;
227    let mut pos = state.opt_arg_integer(2, size)?;
228    if pos != size {
229        if !((pos as u64).wrapping_sub(1) <= (size as u64)) {
230            let argn = if state.global().lua_version == lua_types::LuaVersion::V53 { 1 } else { 2 };
231            return Err(lua_vm::debug::arg_error_impl(state, argn, b"position out of bounds"));
232        }
233    }
234    // Cache the table once to avoid re-resolving stack slot 1 on every
235    // iteration of the shift loop. C's lua_geti is a single pointer
236    // arithmetic operation; our index_to_value is a function call with
237    // branches, so this saves ~2N index resolutions for shift count N.
238    if let Some(tbl) = plain_table_at(state, 1) {
239        let result = tbl.get_int(pos);
240        state.push(result);
241        while pos < size {
242            let shifted = tbl.get_int(pos + 1);
243            raw_set_int(state, tbl, pos, shifted)?;
244            pos += 1;
245        }
246        raw_set_int(state, tbl, pos, LuaValue::Nil)?;
247        return Ok(1);
248    }
249    let tbl = state.value_at(1);
250    state.table_get_i_value(&tbl, pos)?;   // push element to be returned
251    while pos < size {
252        state.table_get_i_value(&tbl, pos + 1)?;
253        state.table_set_i_value(&tbl, pos)?;
254        pos += 1;
255    }
256    state.push(LuaValue::Nil);
257    state.table_set_i_value(&tbl, pos)?;   // remove last slot (table[pos] = nil)
258    Ok(1)
259}
260
261// ─── table.move ───────────────────────────────────────────────────────────────
262
263///
264/// Copies elements `a1[f..e]` into `a2[t..]` (or `a1[t..]` if `a2` is absent).
265/// Copies in increasing order when safe, decreasing when ranges overlap.
266///
267/// ```c
268/// static int tmove (lua_State *L) {
269///   lua_Integer f = luaL_checkinteger(L, 2);
270///   lua_Integer e = luaL_checkinteger(L, 3);
271///   lua_Integer t = luaL_checkinteger(L, 4);
272///   int tt = !lua_isnoneornil(L, 5) ? 5 : 1;
273///   checktab(L, 1, TAB_R);
274///   checktab(L, tt, TAB_W);
275///   if (e >= f) {
276///     lua_Integer n, i;
277///     luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, "too many elements to move");
278///     n = e - f + 1;
279///     luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, "destination wrap around");
280///     if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
281///       for (i = 0; i < n; i++) { lua_geti(L, 1, f + i); lua_seti(L, tt, t + i); }
282///     } else {
283///       for (i = n - 1; i >= 0; i--) { lua_geti(L, 1, f + i); lua_seti(L, tt, t + i); }
284///     }
285///   }
286///   lua_pushvalue(L, tt);
287///   return 1;
288/// }
289/// ```
290pub fn tmove(state: &mut LuaState) -> Result<usize, LuaError> {
291    let f = state.check_arg_integer(2)?;
292    let e = state.check_arg_integer(3)?;
293    let t = state.check_arg_integer(4)?;
294    let tt: i32 = if !matches!(state.type_at(5), LuaType::None | LuaType::Nil) {
295        5
296    } else {
297        1
298    };
299    check_tab(state, 1, TAB_R)?;
300    check_tab(state, tt, TAB_W)?;
301
302    if e >= f {
303        if !(f > 0 || e < i64::MAX + f) {
304            return Err(lua_vm::debug::arg_error_impl(state, 3, b"too many elements to move"));
305        }
306        let n = e - f + 1;
307        if !(t <= i64::MAX - n + 1) {
308            return Err(lua_vm::debug::arg_error_impl(state, 4, b"destination wrap around"));
309        }
310        // Copy forward (increasing) when safe to do so; backward when ranges overlap.
311        // TODO(port): state.compare(a, b, CompareOp::Eq) → lua_compare LUA_OPEQ; verify method
312        let copy_forward = t > e
313            || t <= f
314            || (tt != 1 && !state.compare(1, tt, CompareOp::Eq)?);
315        if copy_forward {
316            for i in 0..n {
317                state.table_get_i(1, f + i)?;
318                state.table_set_i(tt, t + i)?;
319            }
320        } else {
321            for i in (0..n).rev() {
322                state.table_get_i(1, f + i)?;
323                state.table_set_i(tt, t + i)?;
324            }
325        }
326    }
327    // TODO(port): state.push_value_at → lua_pushvalue; verify method name
328    state.push_value_at(tt)?;
329    Ok(1)
330}
331
332// ─── table.concat ─────────────────────────────────────────────────────────────
333
334/// a string-or-number, add its string representation to `buf`, then pop it.
335///
336/// ```c
337/// static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
338///   lua_geti(L, 1, i);
339///   if (l_unlikely(!lua_isstring(L, -1)))
340///     luaL_error(L, "invalid value (%s) at index %I in table for 'concat'",
341///                   luaL_typename(L, -1), (LUAI_UACINT)i);
342///   luaL_addvalue(b);
343/// }
344/// ```
345///
346/// PORT NOTE: `luaL_Buffer` in C accumulates bytes and then pushes the result;
347/// Rust uses a `Vec<u8>` accumulator passed by mutable reference instead.
348fn add_field(state: &mut LuaState, buf: &mut Vec<u8>, idx: i64) -> Result<(), LuaError> {
349    state.table_get_i(1, idx)?;
350    if !matches!(state.type_at(-1), LuaType::String | LuaType::Number) {
351        let type_name = state.type_name_str_at(-1);
352        let msg = format!(
353            "invalid value ({}) at index {} in table for 'concat'",
354            String::from_utf8_lossy(type_name),
355            idx
356        );
357        return crate::auxlib::lua_error(state, msg.as_bytes()).map(|_| ());
358    }
359    // TODO(port): state.to_bytes_at(-1) converts via Lua's tostring coercion; verify method name
360    let bytes = state.to_bytes_at(-1).ok_or_else(|| LuaError::runtime(format_args!("invalid value at index {}", idx)))?;
361    buf.extend_from_slice(&bytes);
362    state.pop_n(1);
363    Ok(())
364}
365
366///
367/// ```c
368/// static int tconcat (lua_State *L) {
369///   luaL_Buffer b;
370///   lua_Integer last = aux_getn(L, 1, TAB_R);
371///   size_t lsep;
372///   const char *sep = luaL_optlstring(L, 2, "", &lsep);
373///   lua_Integer i = luaL_optinteger(L, 3, 1);
374///   last = luaL_optinteger(L, 4, last);
375///   luaL_buffinit(L, &b);
376///   for (; i < last; i++) { addfield(L, &b, i); luaL_addlstring(&b, sep, lsep); }
377///   if (i == last) addfield(L, &b, i);
378///   luaL_pushresult(&b);
379///   return 1;
380/// }
381/// ```
382pub fn concat(state: &mut LuaState) -> Result<usize, LuaError> {
383    let last = aux_getn(state, 1, TAB_R)?;
384    // TODO(port): state.opt_arg_lstring(n, default) → luaL_optlstring; verify method name
385    // Clone the separator before any stack-mutating calls that might invalidate it.
386    let sep: Vec<u8> = state.opt_arg_lstring(2, Some(b""))?.unwrap_or_default();
387    let mut i = state.opt_arg_integer(3, 1)?;
388    let last = state.opt_arg_integer(4, last)?;
389
390    // PORT NOTE: C uses luaL_Buffer (which may back-patch the stack);
391    // Rust uses a plain Vec<u8> accumulator and pushes the result at the end.
392    let mut buf: Vec<u8> = Vec::new();
393    while i < last {
394        add_field(state, &mut buf, i)?;
395        buf.extend_from_slice(&sep);
396        i += 1;
397    }
398    if i == last {
399        add_field(state, &mut buf, i)?;
400    }
401    // TODO(port): state.push_lstring pushes a Lua string from &[u8]; verify method name
402    state.push_lstring(&buf)?;
403    Ok(1)
404}
405
406// ─── table.pack / table.unpack ────────────────────────────────────────────────
407
408///
409/// Creates a new table `t` with all arguments as integer keys and `t.n` set
410/// to the argument count.
411///
412/// ```c
413/// static int tpack (lua_State *L) {
414///   int i;
415///   int n = lua_gettop(L);
416///   lua_createtable(L, n, 1);
417///   lua_insert(L, 1);
418///   for (i = n; i >= 1; i--)
419///     lua_seti(L, 1, i);
420///   lua_pushinteger(L, n);
421///   lua_setfield(L, 1, "n");
422///   return 1;
423/// }
424/// ```
425pub fn pack(state: &mut LuaState) -> Result<usize, LuaError> {
426    let n = state.get_top();
427    // TODO(port): state.create_table(narr, nrec) → lua_createtable; verify method name
428    state.create_table(n, 1)?;
429    state.insert(1)?;
430    // table_set_i pops the top; args shift from n+1..=2 down to 1..=n as we pop
431    for i in (1..=n).rev() {
432        state.table_set_i(1, i as i64)?;
433    }
434    state.push(LuaValue::Int(n as i64));
435    // TODO(port): state.set_field(stack_pos, key_bytes) → lua_setfield; verify method name
436    state.set_field(1, b"n")?;
437    Ok(1)
438}
439
440///
441/// Pushes `t[i], t[i+1], …, t[j]` and returns the count.
442///
443/// ```c
444/// static int tunpack (lua_State *L) {
445///   lua_Unsigned n;
446///   lua_Integer i = luaL_optinteger(L, 2, 1);
447///   lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
448///   if (i > e) return 0;
449///   n = (lua_Unsigned)e - i;
450///   if (l_unlikely(n >= (unsigned int)INT_MAX ||
451///                  !lua_checkstack(L, (int)(++n))))
452///     return luaL_error(L, "too many results to unpack");
453///   for (; i < e; i++) lua_geti(L, 1, i);
454///   lua_geti(L, 1, e);
455///   return (int)n;
456/// }
457/// ```
458pub fn unpack(state: &mut LuaState) -> Result<usize, LuaError> {
459    let i = state.opt_arg_integer(2, 1)?;
460    let e = if matches!(state.type_at(3), LuaType::None | LuaType::Nil) {
461        state.length_at(1)?
462    } else {
463        state.check_arg_integer(3)?
464    };
465    if i > e {
466        return Ok(0); // empty range
467    }
468    let n = (e as u64).wrapping_sub(i as u64);
469    // The size check uses the pre-increment value so that a wrapped-to-0 result
470    // (e.g. i=minI, e=maxI yields n = 2^64-1 pre-inc, 0 post-inc) still trips
471    // the error rather than silently entering a 2^64-iteration loop.
472    if n >= i32::MAX as u64 {
473        return Err(LuaError::runtime(format_args!("too many results to unpack")));
474    }
475    let n = n + 1;
476    if !state.check_stack_growth(n as i32) {
477        return Err(LuaError::runtime(format_args!("too many results to unpack")));
478    }
479    let n = n as i64;
480    let mut k = i;
481    while k < e {
482        state.table_get_i(1, k)?;
483        k += 1;
484    }
485    state.table_get_i(1, e)?; // push last element
486    Ok(n as usize)
487}
488
489// ─── Quicksort ────────────────────────────────────────────────────────────────
490
491/// selection when a partition is severely imbalanced.
492///
493/// `unsigned int` array whose elements are summed.
494///
495/// PORT NOTE: C uses a small randomised pivot guard to avoid pathological sort
496/// partitions. The Rust port asks the host for entropy when available and falls
497/// back to a deterministic pivot value in sandboxed/bare-WASM hosts.
498fn randomize_pivot(state: &LuaState) -> u32 {
499    let entropy = state.global().entropy_hook.map(|hook| hook()).unwrap_or(0);
500    let mixed = entropy ^ entropy.wrapping_shr(32);
501    (mixed as u32) ^ (mixed as u32).wrapping_shr(16)
502}
503
504/// `table[i]` and `table[j]` respectively (table is at stack position 1).
505///
506/// ```c
507/// static void set2 (lua_State *L, IdxT i, IdxT j) {
508///   lua_seti(L, 1, i);
509///   lua_seti(L, 1, j);
510/// }
511/// ```
512fn set2(state: &mut LuaState, i: IdxT, j: IdxT) -> Result<(), LuaError> {
513    // First seti pops the stack top; second seti pops the new top.
514    state.table_set_i(1, i as i64)?;
515    state.table_set_i(1, j as i64)?;
516    Ok(())
517}
518
519/// sort order: either the `<` operator (if arg 2 is nil) or the user's
520/// comparison function at stack position 2.
521///
522/// ```c
523/// static int sort_comp (lua_State *L, int a, int b) {
524///   if (lua_isnil(L, 2))
525///     return lua_compare(L, a, b, LUA_OPLT);
526///   else {
527///     int res;
528///     lua_pushvalue(L, 2);
529///     lua_pushvalue(L, a-1);
530///     lua_pushvalue(L, b-2);
531///     lua_call(L, 2, 1);
532///     res = lua_toboolean(L, -1);
533///     lua_pop(L, 1);
534///     return res;
535///   }
536/// }
537/// ```
538///
539/// The offsets `a-1` and `b-2` compensate for the function and first-argument
540/// copies pushed before the respective values: `a-1` accounts for the function
541/// push; `b-2` accounts for both the function push and the copy of `a`.
542fn sort_comp(state: &mut LuaState, a: i32, b: i32) -> Result<bool, LuaError> {
543    if state.type_at(2) == LuaType::Nil {
544        // No user comparator: use the default `<` operator.
545        return state.compare(a, b, CompareOp::Lt);
546    }
547    // User comparator at stack position 2.
548    state.push_value_at(2)?;       // push function
549    state.push_value_at(a - 1)?;   // push copy of a (compensate for function push)
550    state.push_value_at(b - 2)?;   // push copy of b (compensate for function + a copy)
551    state.call(2, 1)?;
552    // TODO(port): state.to_boolean(-1) → lua_toboolean (never fails); verify method name
553    let res = state.to_boolean(-1);
554    state.pop_n(1);
555    Ok(res)
556}
557
558/// is already on the top of the Lua stack.
559///
560/// Precondition: `a[lo] <= P == a[up-1] <= a[up]` and `P` is at stack top.
561/// Postcondition: `a[lo..i-1] <= a[i] == P <= a[i+1..up]`; stack is clean.
562/// Returns the final pivot index `i`.
563///
564/// ```c
565/// static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
566///   IdxT i = lo;
567///   IdxT j = up - 1;
568///   for (;;) {
569///     while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
570///       if (l_unlikely(i == up - 1))
571///         luaL_error(L, "invalid order function for sorting");
572///       lua_pop(L, 1);
573///     }
574///     while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
575///       if (l_unlikely(j < i))
576///         luaL_error(L, "invalid order function for sorting");
577///       lua_pop(L, 1);
578///     }
579///     if (j < i) {
580///       lua_pop(L, 1);
581///       set2(L, up - 1, i);
582///       return i;
583///     }
584///     set2(L, i, j);
585///   }
586/// }
587/// ```
588fn partition(state: &mut LuaState, lo: IdxT, up: IdxT) -> Result<IdxT, LuaError> {
589    let mut i: IdxT = lo;
590    let mut j: IdxT = up - 1;
591    // Entry: stack top is P (pivot value).
592    loop {
593        // Advance i: find first a[i] >= P.
594        // Stack during i-loop body: P(-2), a[i](-1)
595        loop {
596            i += 1;
597            state.table_get_i(1, i as i64)?; // push a[i]
598            if !sort_comp(state, -1, -2)? {
599                // a[i] >= P: leave a[i] on stack and exit
600                break;
601            }
602            // a[i] < P; check for invalid comparator
603            if i == up - 1 {
604                return Err(LuaError::runtime(format_args!(
605                    "invalid order function for sorting"
606                )));
607            }
608            state.pop_n(1); // remove a[i]
609        }
610        // Retreat j: find last a[j] <= P.
611        // Stack during j-loop body: P(-3), a[i](-2), a[j](-1)
612        loop {
613            // PERF(port): wrapping_sub mirrors C unsigned IdxT behaviour for edge cases
614            j = j.wrapping_sub(1);
615            state.table_get_i(1, j as i64)?; // push a[j]
616            if !sort_comp(state, -3, -1)? {
617                // P >= a[j]: leave a[j] on stack and exit
618                break;
619            }
620            // P < a[j]; check for invalid comparator
621            if j < i {
622                return Err(LuaError::runtime(format_args!(
623                    "invalid order function for sorting"
624                )));
625            }
626            state.pop_n(1); // remove a[j]
627        }
628        // Stack: P(-3), a[i](-2), a[j](-1)
629        if j < i {
630            // No out-of-place elements; finalize: place pivot at position i.
631            state.pop_n(1); // pop a[j]; stack: P(-2), a[i](-1)
632            set2(state, up - 1, i)?; // table[up-1] = a[i], table[i] = P; stack clean
633            return Ok(i);
634        }
635        // Swap a[i] and a[j] to restore loop invariant.
636        // set2: table[i] = a[j] (pops -1), table[j] = a[i] (pops new -1); stack: P(-1)
637        set2(state, i, j)?;
638    }
639}
640
641/// `[lo, up]`, randomised by `rnd`.
642///
643/// ```c
644/// static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
645///   IdxT r4 = (up - lo) / 4;
646///   IdxT p = rnd % (r4 * 2) + (lo + r4);
647///   lua_assert(lo + r4 <= p && p <= up - r4);
648///   return p;
649/// }
650/// ```
651fn choose_pivot(lo: IdxT, up: IdxT, rnd: u32) -> IdxT {
652    let r4 = (up - lo) / 4; // range / 4
653    let p = rnd % (r4 * 2) + (lo + r4);
654    debug_assert!(lo + r4 <= p && p <= up - r4);
655    p
656}
657
658///
659/// Sorts `table[lo..=up]` in place, recursing on the smaller partition and
660/// tail-looping on the larger (to bound Rust's call stack). Randomises pivot
661/// selection when a partition is badly imbalanced.
662///
663/// ```c
664/// static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned int rnd) {
665///   while (lo < up) {
666///     IdxT p, n;
667///     lua_geti(L, 1, lo); lua_geti(L, 1, up);
668///     if (sort_comp(L, -1, -2)) set2(L, lo, up); else lua_pop(L, 2);
669///     if (up - lo == 1) return;
670///     if (up - lo < RANLIMIT || rnd == 0) p = (lo + up)/2;
671///     else p = choosePivot(lo, up, rnd);
672///     lua_geti(L, 1, p); lua_geti(L, 1, lo);
673///     if (sort_comp(L, -2, -1)) set2(L, p, lo);
674///     else {
675///       lua_pop(L, 1); lua_geti(L, 1, up);
676///       if (sort_comp(L, -1, -2)) set2(L, p, up); else lua_pop(L, 2);
677///     }
678///     if (up - lo == 2) return;
679///     lua_geti(L, 1, p); lua_pushvalue(L, -1); lua_geti(L, 1, up - 1);
680///     set2(L, p, up - 1);
681///     p = partition(L, lo, up);
682///     if (p - lo < up - p) {
683///       auxsort(L, lo, p - 1, rnd); n = p - lo; lo = p + 1;
684///     } else {
685///       auxsort(L, p + 1, up, rnd); n = up - p; up = p - 1;
686///     }
687///     if ((up - lo) / 128 > n) rnd = l_randomizePivot();
688///   }
689/// }
690/// ```
691fn aux_sort(state: &mut LuaState, mut lo: IdxT, mut up: IdxT, mut rnd: u32) -> Result<(), LuaError> {
692    while lo < up {
693        // Step 1: ensure a[lo] <= a[up] (cheap two-element sort)
694        state.table_get_i(1, lo as i64)?; // push a[lo]
695        state.table_get_i(1, up as i64)?; // push a[up]
696        if sort_comp(state, -1, -2)? {
697            set2(state, lo, up)?; // swap so a[lo] <= a[up]
698        } else {
699            state.pop_n(2);
700        }
701        if up - lo == 1 {
702            return Ok(()); // only 2 elements, now sorted
703        }
704
705        // Step 2: choose pivot index
706        let mut p: IdxT = if up - lo < RANLIMIT || rnd == 0 {
707            (lo + up) / 2 // midpoint pivot for small/non-random runs
708        } else {
709            choose_pivot(lo, up, rnd)
710        };
711
712        // Step 3: median-of-three: sort a[lo], a[p], a[up]
713        state.table_get_i(1, p as i64)?;  // push a[p]
714        state.table_get_i(1, lo as i64)?; // push a[lo]
715        if sort_comp(state, -2, -1)? {
716            set2(state, p, lo)?; // swap a[p] ↔ a[lo]; stack clean
717        } else {
718            state.pop_n(1); // remove a[lo]; stack: a[p]
719            state.table_get_i(1, up as i64)?; // push a[up]; stack: a[p], a[up]
720            if sort_comp(state, -1, -2)? {
721                set2(state, p, up)?; // swap a[p] ↔ a[up]; stack clean
722            } else {
723                state.pop_n(2); // remove a[up] and a[p]; stack clean
724            }
725        }
726        // Stack is clean at this point.
727        if up - lo == 2 {
728            return Ok(()); // only 3 elements, now sorted
729        }
730
731        // Step 4: move pivot to a[up-1] and call partition.
732        //
733        // Stack evolution:
734        //   table_get_i(p):    a[p]  (-1)
735        //   push_value_at(-1): a[p]  (-2), a[p]_copy  (-1)
736        //   table_get_i(up-1): a[p]  (-3), a[p]_copy  (-2), a[up-1]  (-1)
737        //   set2(p, up-1):     table[p] = a[up-1], table[up-1] = a[p]_copy;
738        //                      stack: a[p] (-1)  ← pivot for partition
739        state.table_get_i(1, p as i64)?;
740        state.push_value_at(-1)?; // duplicate: two copies of pivot on stack
741        state.table_get_i(1, (up - 1) as i64)?;
742        set2(state, p, up - 1)?;
743        // One copy of the pivot value remains at the stack top for partition.
744
745        p = partition(state, lo, up)?;
746        // Stack is clean after partition returns.
747
748        // Step 5: recurse on smaller partition; tail-loop on larger.
749        let n: IdxT;
750        if p - lo < up - p {
751            aux_sort(state, lo, p - 1, rnd)?;
752            n = p - lo;
753            lo = p + 1; // tail: sort [p+1 .. up]
754        } else {
755            aux_sort(state, p + 1, up, rnd)?;
756            n = up - p;
757            up = p - 1; // tail: sort [lo .. p-1]
758        }
759
760        // Re-randomise if the partition was severely imbalanced.
761        if (up - lo) / 128 > n {
762            rnd = randomize_pivot(state);
763        }
764    }
765    Ok(())
766}
767
768///
769/// ```c
770/// static int sort (lua_State *L) {
771///   lua_Integer n = aux_getn(L, 1, TAB_RW);
772///   if (n > 1) {
773///     luaL_argcheck(L, n < INT_MAX, 1, "array too big");
774///     if (!lua_isnoneornil(L, 2))
775///       luaL_checktype(L, 2, LUA_TFUNCTION);
776///     lua_settop(L, 2);
777///     auxsort(L, 1, (IdxT)n, 0);
778///   }
779///   return 0;
780/// }
781/// ```
782pub fn sort(state: &mut LuaState) -> Result<usize, LuaError> {
783    let n = aux_getn(state, 1, TAB_RW)?;
784    if n > 1 {
785        if !(n < i32::MAX as i64) {
786            return Err(lua_vm::debug::arg_error_impl(state, 1, b"array too big"));
787        }
788        if !matches!(state.type_at(2), LuaType::None | LuaType::Nil) {
789            state.check_arg_type(2, LuaType::Function)?;
790        }
791        // Must go through the public C-API set_top (relative to the call
792        // frame); the inherent LuaState::set_top treats its argument as
793        // an absolute stack slot and would corrupt the frame.
794        lua_vm::api::set_top(state, 2)?;
795        aux_sort(state, 1, n as IdxT, 0)?;
796    }
797    Ok(0)
798}
799
800// ─── Registration ─────────────────────────────────────────────────────────────
801
802///
803/// ```c
804/// static const luaL_Reg tab_funcs[] = {
805///   {"concat", tconcat}, {"insert", tinsert}, {"pack", tpack},
806///   {"unpack", tunpack}, {"remove", tremove}, {"move", tmove},
807///   {"sort", sort}, {NULL, NULL}
808/// };
809/// ```
810///
811/// PORT NOTE: In Rust we represent this as a slice of `(&[u8], fn-ptr)` pairs;
812/// the sentinel `{NULL, NULL}` is implicit (the slice has a known length).
813pub const TABLE_FUNCS: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)] = &[
814    (b"concat", concat),
815    (b"insert", insert),
816    (b"pack", pack),
817    (b"unpack", unpack),
818    (b"remove", remove),
819    (b"move", tmove),
820    (b"sort", sort),
821];
822
823/// `table.create(nseq [, nrec])` — Lua 5.5 addition
824/// (`specs/research/5.5-upstream-delta.md` §5, `ltablib.c`).
825///
826/// Preallocates a table with `nseq` array (sequence) slots and `nrec` hash
827/// (record) slots, returning the empty table. Preallocation is purely a
828/// capacity hint; the returned table is observably empty (length 0, no keys),
829/// so this implementation is behaviorally faithful even though our
830/// `create_table` may treat the sizes as advisory.
831///
832/// Registered into the `table` roster only under [`lua_types::LuaVersion::V55`]
833/// (see [`open_table`]); absent under 5.1-5.4, matching upstream.
834pub fn create(state: &mut LuaState) -> Result<usize, LuaError> {
835    let nseq = state.check_arg_integer(1)?;
836    let nrec = state.opt_arg_integer(2, 0)?;
837    if nseq < 0 || nseq > i32::MAX as i64 {
838        return Err(LuaError::runtime(format_args!(
839            "bad argument #1 to 'create' (size out of range)"
840        )));
841    }
842    if nrec < 0 || nrec > i32::MAX as i64 {
843        return Err(LuaError::runtime(format_args!(
844            "bad argument #2 to 'create' (size out of range)"
845        )));
846    }
847    state.create_table(nseq as i32, nrec as i32)?;
848    Ok(1)
849}
850
851// ─── Lua 5.1 legacy compat functions (`getn`/`setn`/`maxn`/`foreach`/`foreachi`) ──
852//
853// These predate the `#` operator and the 5.2 roster cleanup; they ship only in
854// the default lua5.1.5 build (`ltablib.c`) and are registered under the V51
855// backend by `open_table`. Verified against lua5.1.5; see
856// specs/followup/5.1-roster-syntax.md §1.
857
858/// `table.getn(t)` — the "size" of a sequence, i.e. the border `#t` reports.
859///
860/// In 5.1 `aux_getn` is `luaL_checktype(TABLE)` followed by `luaL_getn`, which
861/// resolves to the primitive length. Mirrors `getn` in 5.1 `ltablib.c`.
862fn getn(state: &mut LuaState) -> Result<usize, LuaError> {
863    state.check_arg_type(1, LuaType::Table)?;
864    let n = state.length_at(1)?;
865    state.push(LuaValue::Int(n));
866    Ok(1)
867}
868
869/// `table.setn(t, n)` — obsolete gravestone. In 5.1 the default build defines
870/// `luaL_setn` as a no-op, so `setn` raises `'setn' is obsolete`. Verified
871/// against lua5.1.5 (`pcall`-able to that exact message).
872fn setn(state: &mut LuaState) -> Result<usize, LuaError> {
873    state.check_arg_type(1, LuaType::Table)?;
874    Err(LuaError::runtime(format_args!("'setn' is obsolete")))
875}
876
877/// `table.maxn(t)` — the largest positive numeric key (0 if none). Iterates the
878/// raw table via `next`, tracking the max numeric key. Mirrors `maxn` in 5.1
879/// `ltablib.c`.
880fn maxn(state: &mut LuaState) -> Result<usize, LuaError> {
881    state.check_arg_type(1, LuaType::Table)?;
882    let mut max: f64 = 0.0;
883    state.push(LuaValue::Nil);
884    while state.table_next(1)? {
885        // Stack: ..., key, value. Drop the value, inspect the key.
886        state.pop_n(1);
887        if matches!(state.type_at(-1), LuaType::Number) {
888            if let Some(v) = state.to_number(-1) {
889                if v > max {
890                    max = v;
891                }
892            }
893        }
894    }
895    state.push(LuaValue::Float(max));
896    Ok(1)
897}
898
899/// `table.foreachi(t, f)` — call `f(i, t[i])` for `i` in `1..#t`, stopping early
900/// if `f` returns a non-nil value (which is then returned). Mirrors `foreachi`
901/// in 5.1 `ltablib.c`.
902fn foreachi(state: &mut LuaState) -> Result<usize, LuaError> {
903    state.check_arg_type(1, LuaType::Table)?;
904    state.check_arg_type(2, LuaType::Function)?;
905    let n = state.length_at(1)?;
906    let mut i: i64 = 1;
907    while i <= n {
908        state.push_value_at(2)?;
909        state.push(LuaValue::Int(i));
910        state.table_get_i(1, i)?;
911        state.call(2, 1)?;
912        if !matches!(state.type_at(-1), LuaType::Nil) {
913            return Ok(1);
914        }
915        state.pop_n(1);
916        i += 1;
917    }
918    Ok(0)
919}
920
921/// `table.foreach(t, f)` — call `f(k, v)` for every pair, stopping early if `f`
922/// returns a non-nil value (which is then returned). Mirrors `foreach` in 5.1
923/// `ltablib.c`.
924fn foreach(state: &mut LuaState) -> Result<usize, LuaError> {
925    state.check_arg_type(1, LuaType::Table)?;
926    state.check_arg_type(2, LuaType::Function)?;
927    state.push(LuaValue::Nil);
928    while state.table_next(1)? {
929        // Stack: ..., key, value.
930        state.push_value_at(2)?; // function
931        state.push_value_at(-3)?; // key copy
932        state.push_value_at(-3)?; // value copy
933        state.call(2, 1)?;
934        if !matches!(state.type_at(-1), LuaType::Nil) {
935            return Ok(1);
936        }
937        state.pop_n(2); // remove value and result, leaving key for next()
938    }
939    Ok(0)
940}
941
942// ─── Module opener ────────────────────────────────────────────────────────────
943
944///
945/// ```c
946/// LUAMOD_API int luaopen_table (lua_State *L) {
947///   luaL_newlib(L, tab_funcs);
948///   return 1;
949/// }
950/// ```
951pub fn open_table(state: &mut LuaState) -> Result<usize, LuaError> {
952    // TODO(port): state.new_lib → luaL_newlib; creates a new table and registers functions;
953    //             verify method name and signature
954    //
955    // Per-version roster deltas:
956    //  - `table.move` is a Lua 5.3 addition, absent in 5.1/5.2 (verified against
957    //    lua5.2.4: `type(table.move)` == "nil").
958    //  - `table.pack`/`table.unpack` are Lua 5.2 additions; in 5.1 `unpack` is a
959    //    *global* and there is no `table.pack` (verified against lua5.1.5: both
960    //    `table.unpack` and `table.pack` are nil). 5.1 instead carries the legacy
961    //    `getn`/`setn`/`maxn`/`foreach`/`foreachi` roster.
962    if matches!(state.global().lua_version, lua_types::LuaVersion::V51) {
963        let legacy: Vec<(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)> = TABLE_FUNCS
964            .iter()
965            .filter(|(name, _)| {
966                *name != b"move".as_slice()
967                    && *name != b"pack".as_slice()
968                    && *name != b"unpack".as_slice()
969            })
970            .copied()
971            .collect();
972        state.new_lib(&legacy)?;
973        const LEGACY_FUNCS: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)] = &[
974            (b"getn", getn),
975            (b"setn", setn),
976            (b"maxn", maxn),
977            (b"foreach", foreach),
978            (b"foreachi", foreachi),
979        ];
980        state.set_funcs_with_upvalues(LEGACY_FUNCS, 0)?;
981    } else if matches!(state.global().lua_version, lua_types::LuaVersion::V52) {
982        let without_move: Vec<(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)> = TABLE_FUNCS
983            .iter()
984            .filter(|(name, _)| *name != b"move".as_slice())
985            .copied()
986            .collect();
987        state.new_lib(&without_move)?;
988    } else {
989        state.new_lib(TABLE_FUNCS)?;
990    }
991    // Per-version roster delta: `table.create` is a Lua 5.5 addition
992    // (`specs/research/5.5-upstream-delta.md` §5), absent in 5.1-5.4. Register
993    // it only on the V55 backend so the version seam carries a real,
994    // script-observable stdlib difference. `new_lib` leaves the new table on
995    // the stack top, so we register `create` into it directly.
996    if matches!(state.global().lua_version, lua_types::LuaVersion::V55) {
997        const CREATE_FUNCS: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)] =
998            &[(b"create", create)];
999        state.set_funcs_with_upvalues(CREATE_FUNCS, 0)?;
1000    }
1001    Ok(1)
1002}
1003
1004// ──────────────────────────────────────────────────────────────────────────────
1005// PORT STATUS
1006//   source:        src/ltablib.c  (430 lines, 14 functions)
1007//   target_crate:  lua-stdlib
1008//   confidence:    medium
1009//   todos:         17
1010//   port_notes:    5
1011//   unsafe_blocks: 0
1012//   notes:         Logic is faithfully translated. All TODOs are method-name
1013//                  uncertainties for LuaState API calls (table_get_i, table_set_i,
1014//                  opt_arg_integer, opt_arg_lstring, get_metatable, to_bytes_at,
1015//                  push_lstring, push_value_at, compare, new_lib, set_top,
1016//                  check_stack_growth, type_name_str_at, create_table) — Phase B
1017//                  maps these to the real method names once lua-vm is drafted.
1018//                  Stack cleanup on error paths is elided (C uses longjmp);
1019//                  needs Phase B attention in check_tab and add_field.
1020//                  PERF: remove() and insert() shift loops now cache the table
1021//                  value once (via value_at) and call table_get_i_value /
1022//                  table_set_i_value, bypassing the per-iteration index_to_value
1023//                  call. This shrank the index_to_value hot frame and improved
1024//                  table_ops_long from ~4.76x to ~4.02x vs reference.
1025// ──────────────────────────────────────────────────────────────────────────────