Skip to content

Commit dad564f

Browse files
committed
clean code
1 parent 9771a57 commit dad564f

4 files changed

Lines changed: 86 additions & 224 deletions

File tree

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
///
33
/// Implements CALL and TAILCALL opcodes via `precall` / `pretailcall`.
44
use crate::{
5-
LUA_MASKCALL, LUA_MASKRET, LuaProto, LuaValue,
5+
CallInfo, LUA_MASKCALL, LUA_MASKRET, LuaProto, LuaValue,
66
gc::UpvaluePtr,
77
lua_vm::{
88
CFunction, LUA_HOOKCALL, LUA_HOOKRET, LuaResult, LuaState, StkId, TmKind,
@@ -572,21 +572,19 @@ fn pretailcall_meta(lua_state: &mut LuaState, func_idx: usize, narg1: usize) ->
572572
///
573573
/// Caller must set `lua_state.top = ra + nres` before calling (results
574574
/// are at `top - nres .. top - 1`).
575-
pub fn poscall(lua_state: &mut LuaState, nres: usize, pc: usize) -> LuaResult<()> {
576-
let nresults = lua_state
577-
.current_frame()
578-
.expect("return handling requires an active call frame")
579-
.nresults();
575+
pub fn poscall(
576+
lua_state: &mut LuaState,
577+
ci: &mut CallInfo,
578+
nres: usize,
579+
pc: usize,
580+
) -> LuaResult<()> {
581+
let nresults = ci.nresults();
580582

581583
// Return hook (cold path — almost never fires)
582584
if lua_state.hook_mask & LUA_MASKRET != 0 && lua_state.allow_hook {
583585
hook_on_return(lua_state, pc, nres as i32)?;
584586
}
585587

586-
// res = ci->func.p (destination for results)
587-
let ci = lua_state
588-
.current_frame()
589-
.expect("return handling requires an active call frame");
590588
let res = ci.base - ci.func_offset as usize;
591589

592590
// moveresults: move nres values from top-nres to res, adjusted for wanted count

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

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -320,35 +320,19 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
320320
continue;
321321
}
322322
OpCode::GetTable => {
323-
table_ops::op_get_table(
324-
lua_state,
325-
ci,
326-
&mut base_stk,
327-
&mut pc,
328-
&mut trap,
329-
instr,
330-
constants,
331-
)?;
323+
table_ops::op_get_table(lua_state, ci, &mut base_stk, pc, &mut trap, instr)?;
332324
continue;
333325
}
334326
OpCode::GetI => {
335-
table_ops::op_get_i(
336-
lua_state,
337-
ci,
338-
&mut base_stk,
339-
&mut pc,
340-
&mut trap,
341-
instr,
342-
constants,
343-
)?;
327+
table_ops::op_get_i(lua_state, ci, &mut base_stk, pc, &mut trap, instr)?;
344328
continue;
345329
}
346330
OpCode::GetField => {
347331
table_ops::op_get_field(
348332
lua_state,
349333
ci,
350334
&mut base_stk,
351-
&mut pc,
335+
pc,
352336
&mut trap,
353337
instr,
354338
constants,
@@ -360,7 +344,7 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
360344
lua_state,
361345
ci,
362346
&mut base_stk,
363-
&mut pc,
347+
pc,
364348
&mut trap,
365349
instr,
366350
constants,
@@ -372,7 +356,7 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
372356
lua_state,
373357
ci,
374358
&mut base_stk,
375-
&mut pc,
359+
pc,
376360
&mut trap,
377361
instr,
378362
constants,
@@ -384,7 +368,7 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
384368
lua_state,
385369
ci,
386370
&mut base_stk,
387-
&mut pc,
371+
pc,
388372
&mut trap,
389373
instr,
390374
constants,
@@ -396,7 +380,7 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
396380
lua_state,
397381
ci,
398382
&mut base_stk,
399-
&mut pc,
383+
pc,
400384
&mut trap,
401385
instr,
402386
constants,
@@ -419,7 +403,7 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
419403
lua_state,
420404
ci,
421405
&mut base_stk,
422-
&mut pc,
406+
pc,
423407
&mut trap,
424408
instr,
425409
constants,
@@ -1292,7 +1276,7 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
12921276

12931277
lua_state.set_top_raw(a_pos + n as usize);
12941278
ci.save_pc(pc);
1295-
poscall(lua_state, n as usize, pc)?;
1279+
poscall(lua_state, ci, n as usize, pc)?;
12961280
// Reload caller frame and continue dispatch (avoid outer loop roundtrip)
12971281
reload_after_return!();
12981282
continue;
@@ -1301,7 +1285,7 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
13011285
// return (no values)
13021286
if lua_state.hook_mask & (LUA_MASKRET | LUA_MASKLINE) != 0 {
13031287
ci.save_pc(pc);
1304-
return0_with_hook(lua_state, ci.base + instr.get_a() as usize, pc)?;
1288+
return0_with_hook(lua_state, ci, ci.base + instr.get_a() as usize, pc)?;
13051289
break;
13061290
}
13071291

@@ -1329,7 +1313,7 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
13291313
// return R[A] (single value)
13301314
if lua_state.hook_mask & (LUA_MASKRET | LUA_MASKLINE) != 0 {
13311315
ci.save_pc(pc);
1332-
return1_with_hook(lua_state, ci.base + instr.get_a() as usize, pc)?;
1316+
return1_with_hook(lua_state, ci, ci.base + instr.get_a() as usize, pc)?;
13331317
break;
13341318
}
13351319

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,24 +1480,28 @@ pub fn eq_fallback(
14801480

14811481
/// Cold path: Return0 with active hooks — delegates to generic poscall
14821482
#[inline(never)]
1483-
pub fn return0_with_hook(lua_state: &mut LuaState, a_pos: usize, pc: usize) -> LuaResult<()> {
1483+
pub fn return0_with_hook(
1484+
lua_state: &mut LuaState,
1485+
ci: &mut CallInfo,
1486+
a_pos: usize,
1487+
pc: usize,
1488+
) -> LuaResult<()> {
14841489
lua_state.set_top_raw(a_pos);
1485-
lua_state
1486-
.current_frame_mut()
1487-
.expect("saving pc requires an active call frame")
1488-
.save_pc(pc);
1489-
poscall(lua_state, 0, pc)
1490+
ci.save_pc(pc);
1491+
poscall(lua_state, ci, 0, pc)
14901492
}
14911493

14921494
/// Cold path: Return1 with active hooks — delegates to generic poscall
14931495
#[inline(never)]
1494-
pub fn return1_with_hook(lua_state: &mut LuaState, a_pos: usize, pc: usize) -> LuaResult<()> {
1496+
pub fn return1_with_hook(
1497+
lua_state: &mut LuaState,
1498+
ci: &mut CallInfo,
1499+
a_pos: usize,
1500+
pc: usize,
1501+
) -> LuaResult<()> {
14951502
lua_state.set_top_raw(a_pos + 1);
1496-
lua_state
1497-
.current_frame_mut()
1498-
.expect("saving pc requires an active call frame")
1499-
.save_pc(pc);
1500-
poscall(lua_state, 1, pc)
1503+
ci.save_pc(pc);
1504+
poscall(lua_state, ci, 1, pc)
15011505
}
15021506

15031507
#[inline]

0 commit comments

Comments
 (0)