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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! Table operations for the Lua VM.
//!
//! This module contains methods for creating and manipulating Lua tables,
//! including metamethod-aware operations.
use super::lua_val::Val;
use super::{Result, State, TypeError};
use crate::instr::{ArgCount, RetCount};
impl State {
/// Creates a new empty table and pushes it onto the stack.
#[hotpath::measure]
pub fn new_table(&mut self) {
let val = self.alloc_table();
self.stack.push(val);
}
/// Pushes onto the stack the value `t[k]`, where `t` is the value at the given
/// valid index and `k` is the value at the top of the stack.
///
/// This function pops the key from the stack (putting the resulting value in
/// its place). As in Lua, this function may trigger a metamethod for the
/// "index" event.
#[hotpath::measure]
pub fn get_table(&mut self, i: isize) -> Result<()> {
let idx = self.convert_idx(i)?;
assert!(idx != self.stack.len() - 1);
let key = self.pop_val();
self.get_table_with_key(idx, key)
}
/// Gets `t[k]` without invoking metamethods.
/// `t` is at the given index, `k` is at the top of the stack.
/// Pops the key and pushes the result.
#[hotpath::measure]
pub fn get_table_raw(&mut self, i: isize) -> Result<()> {
let idx = self.convert_idx(i)?;
let key = self.pop_val();
// Get the ObjectPtr and type for error reporting
let obj_ptr = self.stack[idx].as_object_ptr();
let typ = self.stack[idx].typ_simple();
match obj_ptr.and_then(|ptr| self.heap.as_table_ref(ptr)) {
Some(t) => {
let val = t.get(&key);
self.stack.push(val);
Ok(())
}
None => Err(self.type_error(TypeError::TableIndex(typ))),
}
}
/// Does the equivalent of `t[k] = v`, where `t` is the value at the given
/// valid index, `k` is the value at the top of the stack minus 1, and `v`
/// is the value at the top of the stack.
///
/// This function pops both the key and the value from the stack.
#[hotpath::measure]
pub fn set_table_raw(&mut self, i: isize) -> Result<()> {
let idx = self.convert_idx(i)?;
let key = self.pop_val();
let val = self.pop_val();
// Get the ObjectPtr and type for error reporting
let obj_ptr = self.stack[idx].as_object_ptr();
let typ = self.stack[idx].typ_simple();
match obj_ptr.and_then(|ptr| self.heap.as_table(ptr)) {
Some(t) => {
t.insert(key, val)?;
Ok(())
}
None => Err(self.type_error(TypeError::TableIndex(typ))),
}
}
/// Returns the next key-value pair from a table, for use with `pairs`.
/// Takes the table index and pops the key from the stack.
/// Pushes the next key and value onto the stack (or just nil if done).
#[hotpath::measure]
pub fn table_next(&mut self, table_idx: isize) -> Result<bool> {
let idx = self.convert_idx(table_idx)?;
let key = self.pop_val();
let obj_ptr = self.stack[idx].as_object_ptr();
let typ = self.stack[idx].typ_simple();
match obj_ptr.and_then(|ptr| self.heap.as_table_ref(ptr)) {
Some(t) => {
let (next_key, next_val) = t.next(&key);
if matches!(next_key, Val::Nil) {
self.stack.push(Val::Nil);
Ok(false)
} else {
self.stack.push(next_key);
self.stack.push(next_val);
Ok(true)
}
}
None => Err(self.type_error(TypeError::TableIndex(typ))),
}
}
/// Returns the array length of the table at the given index.
#[hotpath::measure]
pub fn table_len(&self, idx: isize) -> usize {
let i = match self.convert_idx(idx) {
Ok(i) => i,
Err(_) => return 0,
};
self.stack[i]
.as_object_ptr()
.and_then(|ptr| self.heap.as_table_ref(ptr))
.map_or(0, super::table::Table::array_len)
}
/// Gets the metatable of the value at the given index.
/// For tables, returns the table's metatable.
/// For other types, returns nil (we don't support type metatables yet).
#[hotpath::measure]
pub fn get_metatable_of(&mut self, idx: isize) -> Result<()> {
let i = self.convert_idx(idx)?;
let metatable = self.stack[i]
.as_object_ptr()
.and_then(|ptr| self.heap.as_table_ref(ptr))
.and_then(super::table::Table::get_metatable);
match metatable {
Some(mt) => self.stack.push(Val::Obj(mt)),
None => self.push_nil(),
}
Ok(())
}
/// Sets the metatable of the table at the given index.
/// The metatable should be at the top of the stack (or nil to remove).
/// Pops the metatable from the stack.
#[hotpath::measure]
pub fn set_metatable_of(&mut self, table_idx: isize) -> Result<()> {
let mt_val = self.pop_val();
let idx = self.convert_idx(table_idx)?;
let typ = self.stack[idx].typ_simple();
let mt = match mt_val {
Val::Nil => None,
Val::Obj(ptr) if self.heap.as_table_ref(ptr).is_some() => Some(ptr),
other => return Err(self.type_error(TypeError::TableIndex(other.typ_simple()))),
};
match self.stack[idx]
.as_object_ptr()
.and_then(|ptr| self.heap.as_table(ptr))
{
Some(t) => {
t.set_metatable(mt);
Ok(())
}
None => Err(self.type_error(TypeError::TableIndex(typ))),
}
}
/// Inserts a value into a table at a position, shifting elements.
/// Stack: [t, pos, value] -> []
/// The pos and value are popped from the stack.
#[hotpath::measure]
pub fn table_insert_at(&mut self, table_idx: isize) -> Result<()> {
let value = self.pop_val();
let pos_val = self.pop_val();
let pos = pos_val
.as_num()
.ok_or_else(|| self.type_error(TypeError::Arithmetic(pos_val.typ_simple())))?
as usize;
let idx = self.convert_idx(table_idx)?;
let obj_ptr = self.stack[idx].as_object_ptr();
let typ = self.stack[idx].typ_simple();
match obj_ptr.and_then(|ptr| self.heap.as_table(ptr)) {
Some(t) => {
t.array_insert(pos, value);
Ok(())
}
None => Err(self.type_error(TypeError::TableIndex(typ))),
}
}
/// Removes a value from a table at a position, shifting elements.
/// Pushes the removed value onto the stack.
#[hotpath::measure]
pub fn table_remove_at(&mut self, table_idx: isize, pos: usize) -> Result<()> {
let idx = self.convert_idx(table_idx)?;
let obj_ptr = self.stack[idx].as_object_ptr();
let typ = self.stack[idx].typ_simple();
let removed = match obj_ptr.and_then(|ptr| self.heap.as_table(ptr)) {
Some(t) => t.array_remove(pos),
None => return Err(self.type_error(TypeError::TableIndex(typ))),
};
self.stack.push(removed);
Ok(())
}
/// Sorts the array portion of a table in place.
/// If has_comp is true, uses the function at stack index 2 as comparator.
/// Sort a table's array portion. Returns the array length for cost charging.
#[hotpath::measure]
pub fn table_sort(&mut self, table_idx: isize, has_comp: bool) -> Result<usize> {
let idx = self.convert_idx(table_idx)?;
// Get the array values
let obj_ptr = self.stack[idx].as_object_ptr();
let typ = self.stack[idx].typ_simple();
let mut arr = match obj_ptr.and_then(|ptr| self.heap.as_table_ref(ptr)) {
Some(t) => t.get_array(),
None => return Err(self.type_error(TypeError::TableIndex(typ))),
};
if arr.is_empty() {
return Ok(0);
}
let n = arr.len();
if has_comp {
// Use the comparator function at stack index 2
// We need to do a stable sort with the comparator
let comp_idx = self.convert_idx(2)?;
// Bubble sort to keep it simple (not efficient but works)
for i in 0..n {
for j in 0..n - 1 - i {
// Call comp(arr[j], arr[j+1])
let a = arr[j];
let b = arr[j + 1];
// Push comp function
self.stack.push(self.stack[comp_idx]);
// Push args
self.stack.push(a);
self.stack.push(b);
// Call
self.call(ArgCount::Fixed(2), RetCount::Fixed(1))?;
// Get result
let result = self.pop_val();
// If comp(a, b) is false, swap
if !result.truthy() {
arr.swap(j, j + 1);
}
}
}
} else {
// Default: sort by < operator (numbers first, then strings)
let heap = &self.heap;
arr.sort_by(|a, b| {
match (a.as_num(), b.as_num()) {
(Some(na), Some(nb)) => {
na.partial_cmp(&nb).unwrap_or(std::cmp::Ordering::Equal)
}
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => {
// Try string comparison
match (a.as_string(heap), b.as_string(heap)) {
(Some(sa), Some(sb)) => sa.cmp(sb),
_ => std::cmp::Ordering::Equal,
}
}
}
});
}
// Put sorted array back - need to look up the table again since we may have
// mutated self during comparator calls
let obj_ptr = self.stack[idx].as_object_ptr();
match obj_ptr.and_then(|ptr| self.heap.as_table(ptr)) {
Some(t) => {
t.set_array(arr);
Ok(n)
}
None => Err(self.type_error(TypeError::TableIndex(typ))),
}
}
/// Converts the value at the given index to a string, checking for __tostring metamethod.
/// If the value is a table with a __tostring metamethod, calls it and returns the result.
#[hotpath::measure]
pub fn to_string_with_meta(&mut self, idx: isize) -> Result<String> {
let i = self.convert_idx(idx)?;
let val = self.stack[i];
// Check if it's a table with __tostring
let metatable_ptr = val
.as_object_ptr()
.and_then(|ptr| self.heap.as_table_ref(ptr))
.and_then(super::table::Table::get_metatable);
if let Some(mt_ptr) = metatable_ptr {
let tostring_key = self.alloc_string("__tostring");
let tostring_handler = self
.heap
.as_table_ref(mt_ptr)
.map_or(Val::Nil, |mt| mt.get(&tostring_key));
if !matches!(tostring_handler, Val::Nil) {
// Call the __tostring metamethod
self.stack.push(tostring_handler);
self.stack.push(val);
self.call(ArgCount::Fixed(1), RetCount::Fixed(1))?;
let result = self.pop_val();
return Ok(result.to_string_with_heap(&self.heap));
}
}
// No __tostring, use default
Ok(val.to_string_with_heap(&self.heap))
}
/// Allocates a new table on the heap.
#[hotpath::measure]
pub(super) fn alloc_table(&mut self) -> Val {
// Check if GC is needed before allocating
if self.heap.is_full() {
self.gc_collect();
}
let obj = self.heap.alloc_table();
Val::Obj(obj)
}
}