-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
add new_with_user_state #2282
Conversation
There was a problem hiding this 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?
crates/wasmtime/src/instance.rs
Outdated
@@ -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( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn _new( | |
pub fn new_with_user_state( |
crates/wasmtime/src/instance.rs
Outdated
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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
crates/wasmtime/src/instance.rs
Outdated
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return new_with_user_state(store, module, imports, None); | |
Instance::new_with_user_state(store, module, imports, None) |
- You can't use
new_with_user_state
directly as it isn't imported in the current module. - Please prefer implicit return at the end of a block/function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
crates/wasmtime/src/instance.rs
Outdated
@@ -104,14 +104,15 @@ pub struct Instance { | |||
module: Module, | |||
} | |||
|
|||
#[allow(dead_code)] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
frame_info_registration: Option<Arc<GlobalFrameInfoRegistration>>, | |
_frame_info_registration: Option<Arc<GlobalFrameInfoRegistration>>, |
This suppresses the dead code warning for this field only.
There was a problem hiding this comment.
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
Subscribe to Label Actioncc @peterhuene
This issue or pull request has been labeled: "wasmtime:api"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
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. |
Hm ok, although the |
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. |
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. |
What way do you recommend ? I don’t mind if there is a better solution:) |
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 |
Does this still make sense after #2897? |
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! |
Allow user to specify custom user state when instantiate a wasm instance.