Skip to content

Commit f70b8bf

Browse files
authored
Refactor ExecutingFrame (RustPython#4308)
* Add jump_if function * Add jump_if_or_pop function
1 parent 5fed6a3 commit f70b8bf

File tree

1 file changed

+31
-35
lines changed

1 file changed

+31
-35
lines changed

vm/src/frame.rs

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -980,44 +980,13 @@ impl ExecutingFrame<'_> {
980980
self.jump(*target);
981981
Ok(None)
982982
}
983-
bytecode::Instruction::JumpIfTrue { target } => {
984-
let obj = self.pop_value();
985-
let value = obj.try_to_bool(vm)?;
986-
if value {
987-
self.jump(*target);
988-
}
989-
Ok(None)
990-
}
991-
992-
bytecode::Instruction::JumpIfFalse { target } => {
993-
let obj = self.pop_value();
994-
let value = obj.try_to_bool(vm)?;
995-
if !value {
996-
self.jump(*target);
997-
}
998-
Ok(None)
999-
}
1000-
983+
bytecode::Instruction::JumpIfTrue { target } => self.jump_if(vm, *target, true),
984+
bytecode::Instruction::JumpIfFalse { target } => self.jump_if(vm, *target, false),
1001985
bytecode::Instruction::JumpIfTrueOrPop { target } => {
1002-
let obj = self.last_value();
1003-
let value = obj.try_to_bool(vm)?;
1004-
if value {
1005-
self.jump(*target);
1006-
} else {
1007-
self.pop_value();
1008-
}
1009-
Ok(None)
986+
self.jump_if_or_pop(vm, *target, true)
1010987
}
1011-
1012988
bytecode::Instruction::JumpIfFalseOrPop { target } => {
1013-
let obj = self.last_value();
1014-
let value = obj.try_to_bool(vm)?;
1015-
if !value {
1016-
self.jump(*target);
1017-
} else {
1018-
self.pop_value();
1019-
}
1020-
Ok(None)
989+
self.jump_if_or_pop(vm, *target, false)
1021990
}
1022991

1023992
bytecode::Instruction::Raise { kind } => self.execute_raise(vm, *kind),
@@ -1484,6 +1453,33 @@ impl ExecutingFrame<'_> {
14841453
self.update_lasti(|i| *i = target_pc);
14851454
}
14861455

1456+
#[inline]
1457+
fn jump_if(&mut self, vm: &VirtualMachine, target: bytecode::Label, flag: bool) -> FrameResult {
1458+
let obj = self.pop_value();
1459+
let value = obj.try_to_bool(vm)?;
1460+
if value == flag {
1461+
self.jump(target);
1462+
}
1463+
Ok(None)
1464+
}
1465+
1466+
#[inline]
1467+
fn jump_if_or_pop(
1468+
&mut self,
1469+
vm: &VirtualMachine,
1470+
target: bytecode::Label,
1471+
flag: bool,
1472+
) -> FrameResult {
1473+
let obj = self.last_value();
1474+
let value = obj.try_to_bool(vm)?;
1475+
if value == flag {
1476+
self.jump(target);
1477+
} else {
1478+
self.pop_value();
1479+
}
1480+
Ok(None)
1481+
}
1482+
14871483
/// The top of stack contains the iterator, lets push it forward
14881484
fn execute_for_iter(&mut self, vm: &VirtualMachine, target: bytecode::Label) -> FrameResult {
14891485
let top_of_stack = PyIter::new(self.last_value());

0 commit comments

Comments
 (0)