From c3523137b320a9d38d31a1984fbcbe75c1692566 Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 1 Apr 2025 11:52:21 +0100 Subject: [PATCH 1/5] dev: make Memory::get `pub` --- vm/src/vm/vm_memory/memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/vm_memory/memory.rs b/vm/src/vm/vm_memory/memory.rs index 7d108ce2ff..34a43fb856 100644 --- a/vm/src/vm/vm_memory/memory.rs +++ b/vm/src/vm/vm_memory/memory.rs @@ -238,7 +238,7 @@ impl Memory { } /// Retrieve a value from memory (either normal or temporary) and apply relocation rules - pub(crate) fn get<'a, 'b: 'a, K: 'a>(&'b self, key: &'a K) -> Option> + pub fn get<'a, 'b: 'a, K: 'a>(&'b self, key: &'a K) -> Option> where Relocatable: TryFrom<&'a K>, { From 6ee09ec5dfeb8b2125740c4c37ad2a2dd9624c9d Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 1 Apr 2025 11:54:02 +0100 Subject: [PATCH 2/5] edit changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e1539ba38..8724213dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* dev: add Memory::get_maybe_relocatable [#2039](https://github.com/lambdaclass/cairo-vm/pull/2039) + * refactor: remove duplicated get_val function [#2065](https://github.com/lambdaclass/cairo-vm/pull/2065) * fix: Always use a normal segment in first SegmentArena segment [#1845](https://github.com/lambdaclass/cairo-vm/pull/1845) From 70d85da8e090b6661f5f774897783b4529f21f38 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 14 Apr 2025 22:45:02 +0100 Subject: [PATCH 3/5] dev: expose get_maybe_relocatable instead --- vm/src/vm/vm_memory/memory.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/vm/src/vm/vm_memory/memory.rs b/vm/src/vm/vm_memory/memory.rs index 34a43fb856..b3be507975 100644 --- a/vm/src/vm/vm_memory/memory.rs +++ b/vm/src/vm/vm_memory/memory.rs @@ -238,7 +238,7 @@ impl Memory { } /// Retrieve a value from memory (either normal or temporary) and apply relocation rules - pub fn get<'a, 'b: 'a, K: 'a>(&'b self, key: &'a K) -> Option> + pub(crate) fn get<'a, 'b: 'a, K: 'a>(&'b self, key: &'a K) -> Option> where Relocatable: TryFrom<&'a K>, { @@ -448,6 +448,18 @@ impl Memory { } } + /// Gets the value from memory address as a MaybeRelocatable value. + /// Returns an Error if the value at the memory address is missing or not a MaybeRelocatable. + pub fn get_maybe_relocatable(&self, key: Relocatable) -> Result { + match self + .get(&key) + .ok_or_else(|| MemoryError::UnknownMemoryCell(Box::new(key)))? + { + Cow::Borrowed(maybe_rel) => Ok(maybe_rel.clone()), + Cow::Owned(maybe_rel) => Ok(maybe_rel), + } + } + /// Inserts a value into memory /// Returns an error if the memory cell asignment is invalid pub fn insert_value>( From 8d7360eb40d478faa4cac99681204017d3b44a65 Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 15 Apr 2025 14:32:06 +0100 Subject: [PATCH 4/5] add comment on borrowed variant --- vm/src/vm/vm_memory/memory.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/vm/src/vm/vm_memory/memory.rs b/vm/src/vm/vm_memory/memory.rs index b3be507975..50b06a92c5 100644 --- a/vm/src/vm/vm_memory/memory.rs +++ b/vm/src/vm/vm_memory/memory.rs @@ -455,6 +455,7 @@ impl Memory { .get(&key) .ok_or_else(|| MemoryError::UnknownMemoryCell(Box::new(key)))? { + // Note: the `Borrowed` variant will never occur. Cow::Borrowed(maybe_rel) => Ok(maybe_rel.clone()), Cow::Owned(maybe_rel) => Ok(maybe_rel), } From 5804e0c38434b384c28e0e9474f414ed92c4adc4 Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 15 Apr 2025 17:30:10 +0100 Subject: [PATCH 5/5] add tests --- vm/src/vm/vm_memory/memory.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/vm/src/vm/vm_memory/memory.rs b/vm/src/vm/vm_memory/memory.rs index 50b06a92c5..4bcd463935 100644 --- a/vm/src/vm/vm_memory/memory.rs +++ b/vm/src/vm/vm_memory/memory.rs @@ -1207,6 +1207,29 @@ mod memory_tests { ); } + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_maybe_relocatable_valid_relocatable() { + let memory = memory![((0, 0), (1, 0))]; + assert_eq!( + memory + .get_maybe_relocatable(Relocatable::from((0, 0))) + .unwrap(), + Relocatable::from((1, 0)).into() + ); + } + + #[test] + fn get_maybe_relocatable_valid_integer() { + let memory = memory![((0, 0), 10)]; + assert_eq!( + memory + .get_maybe_relocatable(Relocatable::from((0, 0))) + .unwrap(), + 10.into() + ); + } + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn default_memory() {