Skip to content

Commit a2428f3

Browse files
committed
update
1 parent 8173ba0 commit a2428f3

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

crates/luars/src/lua_vm/execute/control_instructions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ pub fn exec_call(
865865
arg_count,
866866
a,
867867
nresults,
868+
max_stack_size,
868869
);
869870

870871
*frame_ptr_ptr = vm.push_frame(new_frame);
@@ -1015,6 +1016,7 @@ fn exec_call_lua_vararg(
10151016
arg_count,
10161017
a,
10171018
nresults,
1019+
max_stack_size,
10181020
);
10191021

10201022
*frame_ptr_ptr = vm.push_frame(new_frame);
@@ -1130,6 +1132,7 @@ fn exec_call_lua_function(
11301132
arg_count, // top = number of arguments
11311133
a, // result_reg
11321134
nresults,
1135+
max_stack_size,
11331136
);
11341137

11351138
*frame_ptr_ptr = vm.push_frame(new_frame);
@@ -1199,6 +1202,7 @@ fn exec_call_lua_function(
11991202
actual_arg_count, // top = number of arguments
12001203
a, // result_reg
12011204
nresults,
1205+
max_stack_size,
12021206
);
12031207

12041208
*frame_ptr_ptr = vm.push_frame(new_frame);
@@ -1427,6 +1431,7 @@ pub fn exec_tailcall(
14271431
arg_count, // top = number of arguments passed
14281432
result_reg, // result_reg from the CALLER (not 0!)
14291433
nresults,
1434+
max_stack_size,
14301435
);
14311436

14321437
*frame_ptr_ptr = vm.push_frame(new_frame);

crates/luars/src/lua_vm/execute/load_instructions.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@ pub fn exec_varargprep(
2020
) {
2121
let a = get_a!(instr); // number of fixed params
2222

23-
let frame = vm.current_frame();
23+
let frame = unsafe { &*frame_ptr };
2424
let frame_base = frame.base_ptr as usize;
2525
let top = frame.top as usize;
2626

27-
// Get max_stack_size from the function using new ObjectPool API
28-
let Some(func_id) = frame.get_function_id() else {
29-
return; // Invalid function - should not happen
30-
};
31-
let Some(func_ref) = vm.object_pool.get_function(func_id) else {
32-
return; // Invalid function ID - should not happen
33-
};
34-
let max_stack_size = func_ref.chunk.max_stack_size;
27+
// OPTIMIZED: Use cached max_stack from frame instead of object_pool lookup
28+
let max_stack_size = frame.max_stack as usize;
3529

3630
// Arguments were placed starting at frame_base by CALL instruction
3731
// Fixed parameters are at frame_base + 0 to frame_base + a - 1
@@ -69,11 +63,10 @@ pub fn exec_varargprep(
6963
}
7064

7165
// Set vararg info in frame
72-
vm.current_frame_mut().set_vararg(vararg_dest, vararg_count);
66+
unsafe { &mut *frame_ptr }.set_vararg(vararg_dest, vararg_count);
7367
} else {
7468
// No varargs passed
75-
vm.current_frame_mut()
76-
.set_vararg(frame_base + max_stack_size, 0);
69+
unsafe { &mut *frame_ptr }.set_vararg(frame_base + max_stack_size, 0);
7770
}
7871

7972
// Initialize local variables (registers from a to max_stack_size) with nil

crates/luars/src/lua_vm/execute/loop_instructions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ pub fn exec_tforcall(
382382
2, // top = 2 (we have 2 arguments)
383383
a + 4, // result goes to R[A+4] relative to caller's base
384384
nresults, // expecting c results
385+
max_stack_size,
385386
);
386387

387388
vm.push_frame(new_frame);

crates/luars/src/lua_vm/lua_call_frame.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct LuaCallFrame {
5454
vararg_start: u32, // 4 bytes - vararg start position (absolute index)
5555
nresults: i16, // 2 bytes - expected return count (-1 = LUA_MULTRET)
5656
vararg_count: u16, // 2 bytes - number of vararg arguments
57+
pub max_stack: u16, // 2 bytes - max stack size (for VARARGPREP optimization)
5758
}
5859

5960
// ============================================================================
@@ -92,6 +93,7 @@ impl Default for LuaCallFrame {
9293
vararg_start: 0,
9394
nresults: 0,
9495
vararg_count: 0,
96+
max_stack: 0,
9597
}
9698
}
9799
}
@@ -142,6 +144,7 @@ impl LuaCallFrame {
142144
top: usize,
143145
result_reg: usize,
144146
nresults: i16,
147+
max_stack: usize,
145148
) -> Self {
146149
LuaCallFrame {
147150
func_id_ptr: Self::pack_lua_function(func_id),
@@ -155,6 +158,7 @@ impl LuaCallFrame {
155158
vararg_start: 0,
156159
nresults,
157160
vararg_count: 0,
161+
max_stack: max_stack as u16,
158162
}
159163
}
160164

@@ -173,6 +177,7 @@ impl LuaCallFrame {
173177
vararg_start: 0,
174178
nresults: 0,
175179
vararg_count: 0,
180+
max_stack: 0,
176181
}
177182
}
178183

@@ -335,8 +340,8 @@ mod tests {
335340

336341
#[test]
337342
fn test_lua_call_frame_size() {
338-
// Verify frame size is compact
339-
assert_eq!(std::mem::size_of::<LuaCallFrame>(), 56);
343+
// Verify frame size is compact (added max_stack u16 + _padding u16)
344+
assert_eq!(std::mem::size_of::<LuaCallFrame>(), 64);
340345
}
341346

342347
#[test]
@@ -351,6 +356,7 @@ mod tests {
351356
0,
352357
0,
353358
0,
359+
16,
354360
);
355361

356362
assert!(frame.is_lua());
@@ -382,6 +388,7 @@ mod tests {
382388
0,
383389
0,
384390
0,
391+
16,
385392
);
386393

387394
// Test fresh flag
@@ -420,6 +427,7 @@ mod tests {
420427
0,
421428
0,
422429
0,
430+
16,
423431
);
424432

425433
// Set all flags

crates/luars/src/lua_vm/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ impl LuaVM {
280280
chunk.max_stack_size, // top
281281
0, // result_reg
282282
0, // nresults
283+
chunk.max_stack_size, // max_stack_size
283284
);
284285

285286
self.push_frame(frame);
@@ -356,6 +357,7 @@ impl LuaVM {
356357
max_stack,
357358
0,
358359
0,
360+
max_stack,
359361
);
360362

361363
self.push_frame(frame);
@@ -1217,6 +1219,7 @@ impl LuaVM {
12171219
max_stack_size,
12181220
0,
12191221
-1,
1222+
max_stack_size,
12201223
);
12211224
self.push_frame(new_frame);
12221225

@@ -1286,6 +1289,7 @@ impl LuaVM {
12861289
max_stack_size,
12871290
0,
12881291
-1,
1292+
max_stack_size,
12891293
);
12901294
self.push_frame(new_frame);
12911295

@@ -2355,6 +2359,7 @@ impl LuaVM {
23552359
max_stack_size, // top
23562360
result_reg,
23572361
1, // expect 1 result
2362+
max_stack_size,
23582363
);
23592364

23602365
self.push_frame(temp_frame);
@@ -2721,6 +2726,7 @@ impl LuaVM {
27212726
max_stack_size,
27222727
0,
27232728
-1,
2729+
max_stack_size,
27242730
);
27252731
self.push_frame(new_frame);
27262732

@@ -2901,6 +2907,7 @@ impl LuaVM {
29012907
max_stack_size,
29022908
0, // result_reg unused
29032909
-1, // LUA_MULTRET
2910+
max_stack_size,
29042911
);
29052912
self.push_frame(new_frame);
29062913

0 commit comments

Comments
 (0)