Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Base _opcode
  • Loading branch information
ShaharNaveh committed Sep 17, 2025
commit f14e623ba8a910b0601d254b0c789fe834cbad91
6 changes: 6 additions & 0 deletions compiler/core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ impl OpArg {
}
}

impl From<u32> for OpArg {
fn from(raw: u32) -> Self {
Self(raw)
}
}

#[derive(Default, Copy, Clone)]
#[repr(transparent)]
pub struct OpArgState {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ tkinter = ["dep:tk-sys", "dep:tcl-sys"]
[dependencies]
# rustpython crates
rustpython-derive = { workspace = true }
rustpython-vm = { workspace = true, default-features = false }
rustpython-vm = { workspace = true, default-features = false, features = ["compiler"]}
rustpython-common = { workspace = true }

ahash = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ mod locale;
mod math;
#[cfg(unix)]
mod mmap;
mod opcode;
mod pyexpat;
mod pystruct;
mod random;
Expand Down Expand Up @@ -135,6 +136,7 @@ pub fn get_module_inits() -> impl Iterator<Item = (Cow<'static, str>, StdlibInit
"_json" => json::make_module,
"math" => math::make_module,
"pyexpat" => pyexpat::make_module,
"_opcode" => opcode::make_module,
"_random" => random::make_module,
"_statistics" => statistics::make_module,
"_struct" => pystruct::make_module,
Expand Down
83 changes: 83 additions & 0 deletions stdlib/src/opcode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
pub(crate) use opcode::make_module;

#[pymodule]
mod opcode {
use crate::vm::{
AsObject, PyObjectRef, PyResult, VirtualMachine,
builtins::{PyBool, PyInt, PyIntRef, PyNone},
bytecode::Instruction,
match_class,
};
use std::ops::Deref;

struct Opcode(Instruction);

impl Deref for Opcode {
type Target = Instruction;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Opcode {
#[must_use]
pub fn try_from_pyint(raw: PyIntRef, vm: &VirtualMachine) -> PyResult<Self> {
let instruction = raw
.try_to_primitive::<u8>(vm)
.and_then(|v| {
Instruction::try_from(v).map_err(|_| {
vm.new_exception_empty(vm.ctx.exceptions.value_error.to_owned())
})
})
.map_err(|_| vm.new_value_error("invalid opcode or oparg"))?;

Ok(Self(instruction))
}
}

#[derive(FromArgs)]
struct StackEffectArgs {
#[pyarg(positional)]
opcode: PyIntRef,
#[pyarg(positional, optional)]
oparg: Option<PyObjectRef>,
#[pyarg(named, optional)]
jump: Option<PyObjectRef>,
}

#[pyfunction]
fn stack_effect(args: StackEffectArgs, vm: &VirtualMachine) -> PyResult<i32> {
let oparg = args
.oparg
.map(|v| {
if !v.fast_isinstance(vm.ctx.types.int_type) {
return Err(vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
v.class().name()
)));
}
v.downcast_ref::<PyInt>()?.try_to_primitive::<u32>(vm)
})
.unwrap_or(Ok(0))?;

let jump = args
.jump
.map(|v| {
match_class!(match v {
b @ PyBool => Ok(b.is(&vm.ctx.true_value)),
_n @ PyNone => Ok(false),
_ => {
return Err(
vm.new_value_error("stack_effect: jump must be False, True or None")
);
}
})
})
.unwrap_or(Ok(-1))?;

let opcode = Opcode::try_from_pyint(args.opcode, vm)?;

Ok(opcode.stack_effect(oparg.into(), jump))
}
}