Added 'add_fuel' command line option#3792
Conversation
|
You don't need to modify the clif interpreter at all here. You want to add a call to https://docs.rs/wasmtime/latest/wasmtime/struct.Store.html#method.add_fuel somewhere around here: https://github.com/bytecodealliance/wasmtime/blob/main/src/commands/run.rs#L165 |
|
@fitzgen Thank you so much! That specific instantiation eluded me! Thank you so much for being so patient and understanding, as well as holding my hand through this! |
a1phyr
left a comment
There was a problem hiding this comment.
Maybe we could warn the user if --add-fuel is used without --consume-fuel ?
| // if consume fuel has been flagged, we want to instantiate the | ||
| // store with the set amount of fuel from the 'add_fuel' option | ||
| if self.common.consume_fuel { | ||
| store.add_fuel(self.common.add_fuel).unwrap(); |
There was a problem hiding this comment.
I don't think you want to panic here, even though this should never fail.
| store.add_fuel(self.common.add_fuel).unwrap(); | |
| store.add_fuel(self.common.add_fuel)?; |
fitzgen
left a comment
There was a problem hiding this comment.
Thanks!
We can use clap's requires functionality to require that both --consume-fuel and --add-fuel are given if either one is given. This avoids having to check and warn (or at least pushes that into clap rather than here).
But backing up a bit, the fact that users always have to path both is a hint that maybe this is one knob, not two different knobs.
What if instead we removed --consume-fuel and --add-fuel <N> and instead had just --fuel <N>.
#[structopt(long)]
fuel: Option<u64>,and then when configuring the config and store we would do this:
// When configuring the config...
if self.common.fuel.is_some() {
config.consume_fuel(true);
}
// ...
// When configuring the store...
if let Some(fuel) = self.common.fuel {
store.add_fuel(fuel)?;
}Does that make sense?
| #[structopt(long, default_value = "0")] | ||
| add_fuel: u64, |
There was a problem hiding this comment.
| #[structopt(long, default_value = "0")] | |
| add_fuel: u64, | |
| #[structopt(long, requires = "consume_fuel")] | |
| add_fuel: Option<u64>, |
| // if consume fuel has been flagged, we want to instantiate the | ||
| // store with the set amount of fuel from the 'add_fuel' option | ||
| if self.common.consume_fuel { | ||
| store.add_fuel(self.common.add_fuel)?; | ||
| } | ||
|
|
||
| // warn the user if 'add_fuel' is set - is not 0 - and 'consume_fuel' is not | ||
| if self.common.add_fuel != 0 && !self.common.consume_fuel { | ||
| eprintln!( | ||
| "warning: using `--add-fuel` without '--consume-fuel' flag\ | ||
| being set. This currently won't do anything." | ||
| ); | ||
| } |
There was a problem hiding this comment.
| // if consume fuel has been flagged, we want to instantiate the | |
| // store with the set amount of fuel from the 'add_fuel' option | |
| if self.common.consume_fuel { | |
| store.add_fuel(self.common.add_fuel)?; | |
| } | |
| // warn the user if 'add_fuel' is set - is not 0 - and 'consume_fuel' is not | |
| if self.common.add_fuel != 0 && !self.common.consume_fuel { | |
| eprintln!( | |
| "warning: using `--add-fuel` without '--consume-fuel' flag\ | |
| being set. This currently won't do anything." | |
| ); | |
| } | |
| if self.common.consume_fuel { | |
| let fuel = self.common.add_fuel.expect("both add_fuel and consume_fuel must be given"); | |
| store.add_fuel(fuel)?; | |
| } |
| #[structopt(long)] | ||
| consume_fuel: bool, |
There was a problem hiding this comment.
| #[structopt(long)] | |
| consume_fuel: bool, | |
| #[structopt(long, requires = "add_fuel")] | |
| consume_fuel: bool, |
|
The only concern I could see from I am not sure if there is a use case, but just thought I would mention it. |
In that case they would be able to do |
fitzgen
left a comment
There was a problem hiding this comment.
Thanks! This looks great, I just have a couple tiny nitpicks on the wording of the comments. In general, we try to use capitalization and punctuation in our code comments.
Once you commit these suggestions, I'll hit the merge button.
Thanks again!
Or I guess github lets me add those suggestions as commits to your branch :) |
|
Thanks @fitzgen! Learnt a lot from this. Eager to try some more once I get a better understanding of compiler architecture. Thanks again for all the input, you are the best! |
* Added 'add_fuel' command line option * Added default value to 'add_fuel' config option * Added 'add_fuel' to run command Store instantiation * Added comment * Added warning for add-fuel without consume-fuel * Formatting * Changed out --add-fuel and --consume-fuel to --fuel * Formatting * Update src/lib.rs * Update src/commands/run.rs Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
Relatively new to both Rust and Wasmtime, so trying to just pick up a small first issue here.
PR to try a solution to Issue #3717
Currently just adds a "add_fuel" config option to the wasmtime API. I was taking a look and I think I might need to update this default value when the interpreter is instantiated:
to
Don't currently have any test cases for the specific command line option just yet. Just wanted to check if I was on the right track.
CC: @fitzgen