Skip to content

Commit 6a52df4

Browse files
v0.6.0 release
1 parent ea19c33 commit 6a52df4

File tree

6 files changed

+67
-37
lines changed

6 files changed

+67
-37
lines changed

Cargo.lock

Lines changed: 13 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pyembive"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -9,6 +9,6 @@ name = "pyembive"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
embive = { version = "0.5.0", features = ["alloc"] }
12+
embive = { version = "0.6.0", features = ["alloc"] }
1313
ouroboros = "0.18.5"
14-
pyo3 = "0.25.1"
14+
pyo3 = "0.26.0"

src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ pub enum ProgramError {
1515
impl Display for ProgramError {
1616
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1717
match self {
18-
ProgramError::Interpreter(err) => write!(f, "Interpreter error: {}", err),
19-
ProgramError::Transpiler(err) => write!(f, "Transpiler error: {}", err),
20-
ProgramError::Python(err) => write!(f, "Python error: {}", err),
18+
ProgramError::Interpreter(err) => write!(f, "Interpreter error: {err}"),
19+
ProgramError::Transpiler(err) => write!(f, "Transpiler error: {err}"),
20+
ProgramError::Python(err) => write!(f, "Python error: {err}"),
2121
}
2222
}
2323
}

src/lib.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Program {
123123
}
124124

125125
/// Trigger an interrupt.
126-
///
126+
///
127127
/// Arguments:
128128
/// - `value`: 32-bit signed integer value to be passed to the interrupt handler.
129129
fn interrupt(&mut self, value: i32) -> PyResult<()> {
@@ -139,7 +139,7 @@ impl Program {
139139
///
140140
/// Arguments:
141141
/// - `syscall_fn`: Python function to call for the syscall.
142-
/// - Example: `def syscall(nr: int, args: List[int], memory: MemoryPy) -> SyscallResult`
142+
/// - Example: `def syscall(nr: int, args: List[int], memory: Memory) -> SyscallResult`
143143
fn syscall(&mut self, syscall_fn: Bound<'_, PyFunction>) -> PyResult<()> {
144144
let result = self.interpreter.with_interpreter_mut(|interpreter| {
145145
interpreter.syscall(&mut |nr,
@@ -215,10 +215,28 @@ impl Program {
215215
.with_interpreter_mut(|interpreter| interpreter.program_counter = pc);
216216
Ok(())
217217
}
218+
219+
/// Run a function with access to the interpreter memory.
220+
///
221+
/// Arguments:
222+
/// - `memory_fn`: Python function to call with the memory.
223+
/// - Example: `def memory_fn(memory: Memory) -> None`
224+
fn with_memory(&mut self, memory_fn: Bound<'_, PyFunction>) -> PyResult<()> {
225+
let result = self.interpreter.with_interpreter_mut(|interpreter| {
226+
let fn_scope = |memory: &Py<Memory>| {
227+
memory_fn.call1((memory,))?;
228+
Ok(())
229+
};
230+
231+
memory_scope(interpreter.memory, fn_scope).map_err(ProgramError::from)
232+
});
233+
234+
result.map_err(|e| e.into())
235+
}
218236
}
219237

220238
/// Embive Python module
221-
///
239+
///
222240
/// This module provides the Python bindings for the Embive interpreter and transpiler.
223241
#[pymodule]
224242
fn pyembive(m: &Bound<'_, PyModule>) -> PyResult<()> {

src/memory.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn memory_scope<R, F: FnOnce(&Py<Memory>) -> PyResult<R>>(
1313
memory_wrapper: &mut MemoryWrapper,
1414
scope: F,
1515
) -> PyResult<R> {
16-
Python::with_gil(|py| {
16+
Python::attach(|py| {
1717
let memory = Memory {
1818
inner: Some(AtomicPtr::new(memory_wrapper as *mut MemoryWrapper)),
1919
};
@@ -45,7 +45,7 @@ impl Memory {
4545
///
4646
/// Returns:
4747
/// - `Vec<u8>`: Data loaded from memory.
48-
pub fn load(&mut self, address: i32, len: u32) -> PyResult<Vec<u8>> {
48+
pub fn load(&mut self, address: i32, len: usize) -> PyResult<Vec<u8>> {
4949
let address = address as u32;
5050
let inner = self.inner.as_mut().ok_or_else(|| {
5151
PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
@@ -55,7 +55,7 @@ impl Memory {
5555

5656
// Safety: Pointer is valid for as long as the struct is valid, borrow is guarded by pyo3 (this function is mutable).
5757
let memory = unsafe { &mut **inner.get_mut() };
58-
let result = memory.load(address, len).map_err(ProgramError::from);
58+
let result = memory.load_bytes(address, len).map_err(ProgramError::from);
5959

6060
result.map(|data| data.to_vec()).map_err(|e| e.into())
6161
}
@@ -75,7 +75,9 @@ impl Memory {
7575

7676
// Safety: Pointer is valid for as long as the struct is valid, borrow is guarded by pyo3 (this function is mutable).
7777
let memory = unsafe { &mut **inner.get_mut() };
78-
let result = memory.store(address, &data).map_err(ProgramError::from);
78+
let result = memory
79+
.store_bytes(address, &data)
80+
.map_err(ProgramError::from);
7981

8082
result.map_err(|e| e.into())
8183
}

src/wrappers.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,27 @@ impl MemoryWrapper {
2727
}
2828

2929
impl Memory for MemoryWrapper {
30-
fn load(&mut self, address: u32, len: u32) -> Result<&[u8], embive::interpreter::Error> {
31-
self.0.with_memory_mut(|memory| memory.load(address, len))
30+
fn load_bytes(
31+
&mut self,
32+
address: u32,
33+
len: usize,
34+
) -> Result<&[u8], embive::interpreter::Error> {
35+
self.0
36+
.with_memory_mut(|memory| memory.load_bytes(address, len))
3237
}
3338

34-
fn store(&mut self, address: u32, data: &[u8]) -> Result<(), embive::interpreter::Error> {
35-
self.0.with_memory_mut(|memory| memory.store(address, data))
39+
fn mut_bytes(
40+
&mut self,
41+
address: u32,
42+
len: usize,
43+
) -> Result<&mut [u8], embive::interpreter::Error> {
44+
self.0
45+
.with_memory_mut(|memory| memory.mut_bytes(address, len))
46+
}
47+
48+
fn store_bytes(&mut self, address: u32, data: &[u8]) -> Result<(), embive::interpreter::Error> {
49+
self.0
50+
.with_memory_mut(|memory| memory.store_bytes(address, data))
3651
}
3752
}
3853

@@ -48,12 +63,8 @@ pub struct InterpreterWrapper(InterpreterInner);
4863

4964
impl InterpreterWrapper {
5065
pub fn new(memory: MemoryWrapper, instruction_limit: u32) -> Self {
51-
let interpreter = InterpreterInner::new(memory, |memory| {
52-
Interpreter::new(
53-
memory,
54-
instruction_limit,
55-
)
56-
});
66+
let interpreter =
67+
InterpreterInner::new(memory, |memory| Interpreter::new(memory, instruction_limit));
5768
InterpreterWrapper(interpreter)
5869
}
5970

0 commit comments

Comments
 (0)