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

Added conveniance function FunctionEnvMut::data_and_store_mut #3612

Merged
merged 5 commits into from
Feb 28, 2023

Conversation

ptitSeb
Copy link
Contributor

@ptitSeb ptitSeb commented Feb 23, 2023

This allows to borrows mutable ref of both staore and data env from a FunctionEnvMut, making the writting of complex function s easier.

Should address #3592

Comment on lines +120 to +130

/// Borrows a new mutable reference of both the attached Store and host state
pub fn data_and_store_mut(&mut self) -> (&mut T, StoreMut) {
let data = self.func_env.as_mut(&mut self.store_mut) as *mut T;
// telling the borrow check to close his eyes here
// this is still relatively safe to do as func_env are
// stored in a specific vec of Store, separate from the other objects
// and not really directly accessible with the StoreMut
let data = unsafe { &mut *data };
(data, self.store_mut.as_store_mut())
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we add it on the js version as well and add a #[universal_test] verifying the desired behavior? (that way, we test that it also works in js)

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 is on js (in the 2nd commit).
I'll find something for a test.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I missed the js impl. Let's just have an universal test

// The Env is then accessed using `data()` or `data_mut()` method.
#[derive(Clone)]
struct Env {
counter: Arc<Mutex<i32>>,
Copy link
Member

Choose a reason for hiding this comment

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

Why this do need to be a mutex?

Copy link
Member

@syrusakbary syrusakbary Feb 23, 2023

Choose a reason for hiding this comment

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

I just read the comment

Copy link
Member

Choose a reason for hiding this comment

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

But I'm not sure if I agree with it, we can discuss in a call

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 based on the import_function_env example, it's really mostly copy/paste. But we can have a call about the examples for sure.

/// Borrows a new mutable reference of both the attached Store and host state
pub fn data_and_store_mut(&mut self) -> (&mut T, StoreMut) {
let data = self.func_env.as_mut(&mut self.store_mut) as *mut T;
// telling the borrow check to close his eyes here
Copy link
Contributor

Choose a reason for hiding this comment

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

After reviewing the API, I don't see any other (public) way to get access to the FunctionEnv in StoreInner, so this should be safe from a public API standpoint.

Of course internally we could still violate this.

A thing I thought about yesterday : we could put the FunctionEnvs behind a RefCell to make this a bit more ssafer, but that would come with some overhead.

But otherwise I think this is a not ideal, but acceptable use of unsafe.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, agreed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants