From d0dd8303060ed834c944996c5562108462f5fb3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Tue, 23 Aug 2022 13:38:08 +0200 Subject: [PATCH 1/4] Initial update for migration-to-3.0.0 for MemoryView --- docs/migration_to_3.0.0.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/migration_to_3.0.0.md b/docs/migration_to_3.0.0.md index 2a87147af62..68e116187b2 100644 --- a/docs/migration_to_3.0.0.md +++ b/docs/migration_to_3.0.0.md @@ -117,6 +117,9 @@ env_mut.memory = Some(instance.exports.get_memory("memory")); env_mut.alloc_guest_memory = Some(instance.exports.get_typed_function("__alloc")); ``` +### New `MemoryView` API + +Adding the new memory view API (and show how people can update from previous usage) ### Managing imports From 36f4c44c6c83110440ee40364a53e2966c8f301f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Tue, 23 Aug 2022 15:32:18 +0200 Subject: [PATCH 2/4] Fix typo --- examples/exports_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/exports_memory.rs b/examples/exports_memory.rs index 5f8b0a9d217..8cb29e2e6dc 100644 --- a/examples/exports_memory.rs +++ b/examples/exports_memory.rs @@ -75,7 +75,7 @@ fn main() -> Result<(), Box> { println!("String offset: {:?}", ptr.offset()); println!("String length: {:?}", length); - // We now know where to fin our string, let's read it. + // We now know where to find our string, let's read it. // // We will get bytes out of the memory so we need to // decode them into a string. From b6a6897ef52efaff2b54d458e897762f2d04b421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Tue, 23 Aug 2022 15:52:50 +0200 Subject: [PATCH 3/4] Add proper description of MemoryView API --- docs/migration_to_3.0.0.md | 57 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/docs/migration_to_3.0.0.md b/docs/migration_to_3.0.0.md index 68e116187b2..c115578cd5c 100644 --- a/docs/migration_to_3.0.0.md +++ b/docs/migration_to_3.0.0.md @@ -117,9 +117,62 @@ env_mut.memory = Some(instance.exports.get_memory("memory")); env_mut.alloc_guest_memory = Some(instance.exports.get_typed_function("__alloc")); ``` -### New `MemoryView` API +### New `MemoryView` API (preparation for shared memory) -Adding the new memory view API (and show how people can update from previous usage) +Reading from memory has slightly changed compared to 2.x: + +```rust +// 2.x +let memory = instance.exports.get_memory("mem")?; +println!("Memory size (pages) {:?}", memory.size()); +println!("Memory size (bytes) {:?}", memory.data_size()); + +let load = instance + .exports + .get_native_function::<(), (WasmPtr, i32)>("load")?; + +let (ptr, length) = load.call(&mut store)?; +let str = ptr.get_utf8_string(memory, length as u32).unwrap(); +println!("Memory contents: {:?}", str); +``` + +```rust +// 3.x +let memory = instance.exports.get_memory("mem")?; +let memory_view = memory.view(&store); +println!("Memory size (pages) {:?}", memory_view.size()); +println!("Memory size (bytes) {:?}", memory_view.data_size()); + +let load: TypedFunction<(), (WasmPtr, i32)> = + instance.exports.get_typed_function(&mut store, "load")?; + +let (ptr, length) = load.call(&mut store)?; +let memory_view = memory.view(&store); +let str = ptr.read_utf8_string(&memory_view, length as u32).unwrap(); +println!("Memory contents: {:?}", str); +``` + +The reason for this change is that in the future this will enable +safely sharing memory across threads. The same thing goes for reading slices: + +```rust +// 2.x +let new_str = b"Hello, Wasmer!"; +let values = ptr.deref(memory, 0, new_str.len() as u32).unwrap(); +for i in 0..new_str.len() { + values[i].set(new_str[i]); +} +``` + +```rust +// 3.x +let memory_view = memory.view(&store); // (can be reused) +let new_str = b"Hello, Wasmer!"; +let values = ptr.slice(&memory_view, new_str.len() as u32).unwrap(); +for i in 0..new_str.len() { + values.index(i as u64).write(new_str[i]).unwrap(); +} +``` ### Managing imports From c1205e39be28582cb287d5b4c52a478cca04f3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Tue, 23 Aug 2022 15:59:11 +0200 Subject: [PATCH 4/4] Add update-migration to CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7756a471e61..e2822cc821f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C - #[3096](https://github.com/wasmerio/wasmer/pull/3096) create-exe: use cached wasmer tarballs for network fetches ### Changed +- #[3131](https://github.com/wasmerio/wasmer/pull/3131) Update migration docs for MemoryView changes ### Fixed