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

Add fn for splitting borrow of memory & data in Ctx, use in WASI #1069

Merged
merged 4 commits into from
Dec 17, 2019
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
15 changes: 15 additions & 0 deletions lib/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,21 @@ impl Ctx {
}
}

/// Splits the borrow, getting both Memory and a mutable reference to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Splits the borrow" sounds like a function that takes a borrow and splits it. Instead, this is a variation on memory that returns a Memory and a mutable reference to the Ctx.data.

Also, I'd replace "a hostcall" with simply "the host".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's required by the Rust borrow checker; it's a split borrow in that one borrow comes in and 2 come out that otherwise is not possible with safe Rust. There's a function for splitting borrows over slices I believe &mut [u8] -> (&mut [u8], &mut [u8]) which is where I got the terminology from

/// the data type provided.
///
/// This method is required to access both at the same time.
/// This is useful if the data stores information about locations in Wasm
/// memory that a hostcall needs to access.
///
/// # Safety
///
/// This function must be called with the same type, `T`, that the `data`
/// was set to.
pub unsafe fn memory_and_data_mut<T>(&mut self, mem_index: u32) -> (&Memory, &mut T) {
(self.memory(mem_index), &mut *(self.data as *mut T))
}

/// Gives access to the emscripten symbol map, used for debugging
pub unsafe fn borrow_symbol_map(&self) -> &Option<HashMap<u32, String>> {
&(*self.module).info.em_symbol_map
Expand Down
Loading