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 new_with_user_state #2282

Closed

Conversation

zhiqiangxu
Copy link
Contributor

@zhiqiangxu zhiqiangxu commented Oct 8, 2020

Allow user to specify custom user state when instantiate a wasm instance.

Copy link
Contributor

@bjorn3 bjorn3 left a comment

Choose a reason for hiding this comment

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

How do you get access to the user state?

@@ -156,7 +163,12 @@ impl Instance {
/// [inst]: https://webassembly.github.io/spec/core/exec/modules.html#exec-instantiation
/// [issue]: https://github.com/bytecodealliance/wasmtime/issues/727
/// [`ExternType`]: crate::ExternType
pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result<Instance, Error> {
fn _new(
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn _new(
pub fn new_with_user_state(

Comment on lines 198 to 209
pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result<Instance, Error> {
return _new(store, module, imports, None);
}

pub fn new_with_user_state(
store: &Store,
module: &Module,
imports: &[Extern],
user_state: Option<dyn Any>,
) -> Result<Instance, Error> {
return _new(store, module, imports, user_state);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result<Instance, Error> {
return _new(store, module, imports, None);
}
pub fn new_with_user_state(
store: &Store,
module: &Module,
imports: &[Extern],
user_state: Option<dyn Any>,
) -> Result<Instance, Error> {
return _new(store, module, imports, user_state);
}
pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result<Instance, Error> {
Instance::new_with_user_state(store, module, imports, None)
}

This function is also missing documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, _new is removed.

One can access the user state by instance.host_state() then cast it to HostState

@@ -179,6 +195,10 @@ impl Instance {
})
}

pub fn new(store: &Store, module: &Module, imports: &[Extern]) -> Result<Instance, Error> {
return new_with_user_state(store, module, imports, None);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return new_with_user_state(store, module, imports, None);
Instance::new_with_user_state(store, module, imports, None)
  1. You can't use new_with_user_state directly as it isn't imported in the current module.
  2. Please prefer implicit return at the end of a block/function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -104,14 +104,15 @@ pub struct Instance {
module: Module,
}

#[allow(dead_code)]
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Otherwise it will report: warning: field is never read: frame_info_registration.

@@ -102,7 +104,47 @@ pub struct Instance {
module: Module,
}

#[allow(dead_code)]
pub struct HostState {
frame_info_registration: Option<Arc<GlobalFrameInfoRegistration>>,
Copy link
Contributor

@bjorn3 bjorn3 Oct 8, 2020

Choose a reason for hiding this comment

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

Suggested change
frame_info_registration: Option<Arc<GlobalFrameInfoRegistration>>,
_frame_info_registration: Option<Arc<GlobalFrameInfoRegistration>>,

This suppresses the dead code warning for this field only.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved #[allow(dead_code)] to above the frame_info_registration field

@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Oct 8, 2020
@github-actions
Copy link

github-actions bot commented Oct 8, 2020

Subscribe to Label Action

cc @peterhuene

This issue or pull request has been labeled: "wasmtime:api"

Thus the following users have been cc'd because of the following labels:

  • peterhuene: wasmtime:api

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@alexcrichton
Copy link
Member

Thanks for the PR! Can you describe in a bit more detail how you see this being used though? As-is this seems like it's only useful in that you can get a destructor called when the instance itself is deallocated, but I'm not sure if that's the intention or if there's another use case you'd like to unlock. (I'm also not entirely sure how that ability would be used itself)

@zhiqiangxu
Copy link
Contributor Author

zhiqiangxu commented Oct 8, 2020

Thanks for the PR! Can you describe in a bit more detail how you see this being used though? As-is this seems like it's only useful in that you can get a destructor called when the instance itself is deallocated, but I'm not sure if that's the intention or if there's another use case you'd like to unlock. (I'm also not entirely sure how that ability would be used itself)

This is useful if you want to add extra logic such as gas limit for blockchain, the limit can be stored in user state, and checked in FunctionEnvironment::before_translate_operator(extra built in function is required to do that).I will try to submit code for customize that in another PR.

@alexcrichton
Copy link
Member

Hm ok, although the wasmtime crate is not intended to be used where you're also using lots of cranelift internals. At that point it may be best to add a first-class feature to Wasmtime.

@zhiqiangxu
Copy link
Contributor Author

I see. Gas limiting is a fundamental feature for blockchain,so such feature will be very handy for whoever implements vm layer based on wasmtime.

@alexcrichton
Copy link
Member

Oh sure yeah I don't mean to imply that we're not interested in this feature, just that this probably isn't the best way to implement it in Wasmtime.

@zhiqiangxu
Copy link
Contributor Author

What way do you recommend ? I don’t mind if there is a better solution:)

@alexcrichton
Copy link
Member

This sort of limiting execution seems like it would probably be implemented similar to how timeouts are implemented right now, which is to say that there's a configuration option in Config which allows you to tweak an object from Store. The configuration option would then get plumbed through to the codegen backend to affect the generated code.

@bjorn3
Copy link
Contributor

bjorn3 commented Jun 12, 2021

Does this still make sense after #2897?

@alexcrichton
Copy link
Member

I'm going to close this since this has been inactive for some time and I agree that after #2897 this is less relevant than before. If you'd still like to pursue this though feel free to send a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:api Related to the API of the `wasmtime` crate itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants