Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make memoryView lifetime linked to StoreRef instead of Memory #3580

Merged
merged 11 commits into from
Mar 15, 2023
2 changes: 1 addition & 1 deletion lib/api/src/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Memory {

/// Creates a view into the memory that then allows for
/// read and write
pub fn view<'a>(&'a self, store: &impl AsStoreRef) -> MemoryView<'a> {
pub fn view<'a>(&self, store: &'a impl AsStoreRef) -> MemoryView<'a> {
MemoryView::new(self, store)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::sys::externals::memory_view as memory_view_impl;
pub struct MemoryView<'a>(pub(crate) memory_view_impl::MemoryView<'a>);

impl<'a> MemoryView<'a> {
pub(crate) fn new(memory: &'a Memory, store: &impl AsStoreRef) -> Self {
pub(crate) fn new(memory: &Memory, store: &'a impl AsStoreRef) -> Self {
MemoryView(memory_view_impl::MemoryView::new(&memory.0, store))
}

Expand Down
9 changes: 5 additions & 4 deletions lib/api/src/js/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ impl Memory {
self.handle.ty
}

pub fn view(&self, store: &impl AsStoreRef) -> MemoryView {
/// Creates a view into the memory that then allows for
/// read and write
pub fn view<'a>(&self, store: &'a impl AsStoreRef) -> MemoryView<'a> {
MemoryView::new(self, store)
}

Expand Down Expand Up @@ -145,14 +147,13 @@ impl Memory {
let amount = view.data_size() as usize;

let new_memory = Self::new(new_store, ty)?;
let mut new_view = new_memory.view(&new_store);
let new_view_size = new_view.data_size() as usize;
let new_view_size = new_memory.view(&new_store).data_size() as usize;
if amount > new_view_size {
let delta = amount - new_view_size;
let pages = ((delta - 1) / WASM_PAGE_SIZE) + 1;
new_memory.grow(new_store, Pages(pages as u32))?;
new_view = new_memory.view(&new_store);
}
let new_view = new_memory.view(&new_store);

// Copy the bytes
view.copy_to_memory(amount as u64, &new_view)
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/js/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct MemoryView<'a> {
}

impl<'a> MemoryView<'a> {
pub(crate) fn new(memory: &Memory, _store: &impl AsStoreRef) -> Self {
pub(crate) fn new(memory: &Memory, _store: &'a impl AsStoreRef) -> Self {
Self::new_raw(&memory.handle.memory)
}

Expand Down
4 changes: 3 additions & 1 deletion lib/api/src/sys/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ impl Memory {
self.handle.get(store.as_store_ref().objects()).ty()
}

pub fn view<'a>(&'a self, store: &impl AsStoreRef) -> MemoryView<'a> {
/// Creates a view into the memory that then allows for
/// read and write
pub fn view<'a>(&self, store: &'a impl AsStoreRef) -> MemoryView<'a> {
MemoryView::new(self, store)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/sys/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct MemoryView<'a> {
}

impl<'a> MemoryView<'a> {
pub(crate) fn new(memory: &'a Memory, store: &impl AsStoreRef) -> Self {
pub(crate) fn new(memory: &Memory, store: &'a impl AsStoreRef) -> Self {
let size = memory.handle.get(store.as_store_ref().objects()).size();

let definition = memory.handle.get(store.as_store_ref().objects()).vmmemory();
Expand Down
20 changes: 9 additions & 11 deletions lib/wasi/src/bindings/wasix_http_client_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,16 @@ where
);
match result {
Ok(e) => {
let val = wai_bindgen_wasmer::rt::as_i32({
let data_mut = store.data_mut();
let mut tables = data_mut.tables.borrow_mut();
tables.client_table.insert(e) as i32
});
let _memory_view = _memory.view(&store);
let caller_memory = unsafe { _memory_view.data_unchecked_mut() };
caller_memory
.store(arg0 + 0, wai_bindgen_wasmer::rt::as_i32(0i32) as u8)?;
caller_memory.store(
arg0 + 4,
wai_bindgen_wasmer::rt::as_i32({
let data_mut = store.data_mut();
let mut tables = data_mut.tables.borrow_mut();
tables.client_table.insert(e) as i32
}),
)?;
caller_memory.store(arg0 + 4, val)?;
}
Err(e) => {
let _memory_view = _memory.view(&store);
Expand Down Expand Up @@ -312,12 +310,12 @@ where
.unwrap()
.func_canonical_abi_realloc
.clone();
let _memory: wasmer::Memory = store.data().lazy.get().unwrap().memory.clone();
let _memory_view = _memory.view(&store);
let (data_mut, alt_store) = store.data_and_store_mut();
let _memory: wasmer::Memory = data_mut.lazy.get().unwrap().memory.clone();
let _memory_view = _memory.view(&alt_store);
let mut _bc = wai_bindgen_wasmer::BorrowChecker::new(unsafe {
_memory_view.data_unchecked_mut()
});
let data_mut = store.data_mut();
let tables = data_mut.tables.borrow_mut();
let load0 = _bc.load::<i32>(arg0 + 0)?;
let load1 = _bc.load::<i32>(arg0 + 4)?;
Expand Down