1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use crate::{
CallInfo,
lua_value::{Chunk, LuaValue},
lua_vm::{
LuaResult, LuaState,
execute::helper::{ivalue, setivalue, ttisinteger},
lua_limits::EXTRA_STACK,
},
};
use super::helper::{buildhiddenargs, setnilvalue};
/// Get the number of vararg arguments from the vararg table's "n" field.
/// Validates that "n" is a non-negative integer not larger than INT_MAX/2.
/// Equivalent to C Lua 5.5's `getnumargs` when a vararg table exists.
fn get_vatab_len(lua_state: &mut LuaState, base: usize, vatab_reg: usize) -> LuaResult<usize> {
let table_val = {
let stack = lua_state.stack_mut();
unsafe { *stack.get_unchecked(base + vatab_reg) }
};
if let Some(table) = table_val.as_table_mut() {
// Read the "n" field from the table
let n_key = lua_state.create_string("n")?;
let n_val = table.raw_get(&n_key);
match n_val {
Some(val) if val.is_integer() => {
let n = val.as_integer().unwrap();
// l_castS2U(n) > cast_uint(INT_MAX/2) — treat as unsigned, must be <= i32::MAX/2
if (n as u64) > (i32::MAX as u64 / 2) {
return Err(lua_state.error("vararg table has no proper 'n'".to_string()));
}
Ok(n as usize)
}
_ => Err(lua_state.error("vararg table has no proper 'n'".to_string())),
}
} else {
Err(lua_state.error("vararg table has no proper 'n'".to_string()))
}
}
/// VARARG: R[A], ..., R[A+C-2] = varargs
///
/// Lua 5.5: When k flag is set, B is the register of the vararg table.
/// The number of varargs is read from the table's "n" field (which may
/// have been modified by user code), not from CallInfo.nextraargs.
pub fn get_varargs(
lua_state: &mut LuaState,
ci: &mut CallInfo,
base: usize,
a: usize,
b: usize,
vatab: i32,
wanted: i32,
chunk: &Chunk,
) -> LuaResult<()> {
// Get the number of vararg arguments.
// If vatab mode, read "n" from the vararg table (user may have modified it).
// Otherwise, use nextraargs from CallInfo.
let nargs: usize = if vatab >= 0 {
get_vatab_len(lua_state, base, vatab as usize)?
} else {
ci.nextraargs as usize
};
// Calculate how many to copy
let touse = if wanted < 0 {
nargs // Get all
} else if (wanted as usize) > nargs {
nargs
} else {
wanted as usize
};
// Always update stack_top to accommodate the results
// NOTE: Only update L->top (stack_top), NOT ci->top.
// ci->top must remain at base + max_stack_size to protect ALL registers.
// C Lua's luaT_getvarargs only sets L->top.p = where + nvar, never ci->top.
let new_top = base + a + touse;
lua_state.set_top(new_top)?;
let ra_pos = base + a;
if vatab < 0 {
// No vararg table - get from stack
let nfixparams = chunk.param_count;
let totalargs = nfixparams + nargs;
let new_func_pos = base - 1;
let old_func_pos = if totalargs > 0 && new_func_pos > totalargs {
new_func_pos - totalargs - 1
} else {
base + nfixparams
};
let vararg_start = old_func_pos + 1 + nfixparams;
// Varargs are stored below base, ra is at base+a, so ranges never overlap.
// Use copy_within to avoid heap allocation.
let stack = lua_state.stack_mut();
stack.copy_within(vararg_start..vararg_start + touse, ra_pos);
} else {
// Get from vararg table at R[B]
let table_val = { unsafe { *lua_state.stack().get_unchecked(base + b) } };
if let Some(table) = table_val.as_table_mut() {
let stack = lua_state.stack_mut();
for i in 0..touse {
unsafe {
*stack.get_unchecked_mut(ra_pos + i) =
table.raw_geti((i + 1) as i64).unwrap_or(LuaValue::nil())
};
}
} else {
// Not a table, fill with nil
let stack = lua_state.stack_mut();
for i in 0..touse {
setnilvalue(unsafe { stack.get_unchecked_mut(ra_pos + i) });
}
}
}
// Fill remaining with nil
if wanted >= 0 {
let stack = lua_state.stack_mut();
for i in touse..(wanted as usize) {
setnilvalue(unsafe { stack.get_unchecked_mut(ra_pos + i) });
}
}
Ok(())
}
pub fn get_vararg(
lua_state: &mut LuaState,
ci: &mut CallInfo,
base: usize,
ra_pos: usize,
rc_pos: usize,
) -> LuaResult<()> {
let nextra = ci.nextraargs as usize;
// Ensure stack is large enough for rc_pos access
if rc_pos >= lua_state.stack_len() {
lua_state.grow_stack(rc_pos + 1)?;
}
let stack = lua_state.stack_mut();
let rc = stack[rc_pos];
if let Some(s) = rc.as_str() {
if s == "n" {
let stack = lua_state.stack_mut();
setivalue(unsafe { stack.get_unchecked_mut(ra_pos) }, nextra as i64);
} else {
let stack = lua_state.stack_mut();
setnilvalue(unsafe { stack.get_unchecked_mut(ra_pos) });
}
} else if ttisinteger(&rc) {
let n = ivalue(&rc);
let stack = lua_state.stack_mut();
if nextra > 0 && n >= 1 && (n as usize) <= nextra {
let slot = (base - 1) - nextra + (n as usize) - 1;
stack[ra_pos] = stack[slot];
} else {
setnilvalue(unsafe { stack.get_unchecked_mut(ra_pos) });
}
} else if rc.is_float() {
// Lua 5.5: tointegerns - convert integer-valued float to integer
let f = rc.as_float().unwrap();
let n = f as i64;
if (n as f64) == f {
// Float is integer-valued
let stack = lua_state.stack_mut();
if nextra > 0 && n >= 1 && (n as usize) <= nextra {
let slot = (base - 1) - nextra + (n as usize) - 1;
stack[ra_pos] = stack[slot];
} else {
setnilvalue(unsafe { stack.get_unchecked_mut(ra_pos) });
}
} else {
let stack = lua_state.stack_mut();
setnilvalue(unsafe { stack.get_unchecked_mut(ra_pos) });
}
} else {
let stack = lua_state.stack_mut();
setnilvalue(unsafe { stack.get_unchecked_mut(ra_pos) });
}
Ok(())
}
/// VARARGPREP: Adjust varargs (prepare vararg function)
pub fn exec_varargprep(
lua_state: &mut LuaState,
ci: &mut CallInfo,
chunk: &Chunk,
base: &mut usize,
) -> LuaResult<()> {
// Use the nextraargs already computed correctly by push_frame,
// which knows the actual argument count from the CALL instruction.
// We must NOT recalculate from stack_top because push_frame inflates
// stack_top to frame_top (base + maxstacksize) for GC safety,
// which would give a wrong totalargs.
let nextra = ci.nextraargs as usize;
let func_pos = ci.base - 1;
let nfixparams = chunk.param_count;
let totalargs = nfixparams + nextra;
// Handle Lua 5.5 named varargs (requires table)
if chunk.needs_vararg_table {
// Create table with size 'nextra'
// Collect arguments first to avoid borrow conflicts
let mut args = Vec::with_capacity(nextra);
{
let stack = lua_state.stack();
let args_start = func_pos + 1 + nfixparams;
for i in 0..nextra {
if args_start + i < stack.len() {
args.push(stack[args_start + i]);
} else {
args.push(LuaValue::nil());
}
}
}
// Create table
// Use 0 for hash part to encourage ValueArray creation for efficient named varargs
// ValueArray now supports "n" key natively
let table_val = lua_state.create_table(nextra, 0)?;
let n_str = lua_state.create_string("n")?;
let n_val = LuaValue::integer(nextra as i64);
// Populate table
{
if let Some(table_ref) = table_val.as_table_mut() {
// Populate array first (ValueArray will push)
for (i, val) in args.into_iter().enumerate() {
table_ref.raw_seti((i + 1) as i64, val);
}
// Set "n" last (ValueArray will confirm matching length or resize)
table_ref.raw_set(&n_str, n_val);
}
}
// Place table at base + nfixparams (overwriting first extra arg or empty slot)
// Ensure stack is large enough
let target_idx = func_pos + 1 + nfixparams;
// If target_idx is beyond current stack, we need to push
if target_idx >= lua_state.stack_len() {
lua_state.grow_stack(target_idx + 1)?;
}
let stack = lua_state.stack_mut();
unsafe { *stack.get_unchecked_mut(target_idx) = table_val };
// In Lua 5.5 C implementation "luaT_adjustvarargs", the table is placed
// at the slot after fixed parameters. L->top is adjusted to include the table.
// The remaining extra args on the stack are not explicitly cleared but are effectively ignored.
// However, for safety in Rust VM, we might want to clear them or just leave them.
// We will leave them be, as they are "above" the relevant stack usage for this function frame.
}
// Implement buildhiddenargs if there are extra args (and no table needed)
else if nextra > 0 {
// Ensure stack has enough space
let required_size =
func_pos + totalargs + 1 + nfixparams + chunk.max_stack_size + EXTRA_STACK;
if lua_state.stack_len() < required_size {
lua_state.grow_stack(required_size)?;
}
let new_base = buildhiddenargs(lua_state, ci, chunk, totalargs, nfixparams, nextra)?;
*base = new_base;
// Lua 5.5: set vararg parameter register to nil (ltm.c:288)
// The named vararg param (e.g., 't' in '...t') is at register nfixparams
// It should be nil when using hidden args mode (GETVARG accesses hidden area directly)
let stack = lua_state.stack_mut();
setnilvalue(unsafe { stack.get_unchecked_mut(new_base + nfixparams) });
}
// No vararg table needed and no extra args: still need to nil the vararg register
// so it doesn't contain stale stack values
else if chunk.is_vararg {
let current_base = ci.base;
let stack = lua_state.stack_mut();
setnilvalue(unsafe { stack.get_unchecked_mut(current_base + nfixparams) });
}
Ok(())
}