-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cli now has options to specify arguments and function to run
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
- Loading branch information
1 parent
5f928b7
commit 7d167c2
Showing
3 changed files
with
97 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::str::FromStr; | ||
use tinywasm::WasmValue; | ||
|
||
#[derive(Debug)] | ||
pub struct WasmArg(WasmValue); | ||
|
||
pub fn to_wasm_args(args: Vec<WasmArg>) -> Vec<WasmValue> { | ||
args.into_iter().map(|a| a.into()).collect() | ||
} | ||
|
||
impl From<WasmArg> for WasmValue { | ||
fn from(value: WasmArg) -> Self { | ||
value.0 | ||
} | ||
} | ||
|
||
impl FromStr for WasmArg { | ||
type Err = String; | ||
fn from_str(s: &str) -> std::prelude::v1::Result<Self, Self::Err> { | ||
let [ty, val]: [&str; 2] = s | ||
.split(':') | ||
.collect::<Vec<_>>() | ||
.try_into() | ||
.map_err(|e| format!("invalid arguments: {:?}", e))?; | ||
|
||
let arg: WasmValue = match ty { | ||
"i32" => val | ||
.parse::<i32>() | ||
.map_err(|e| format!("invalid argument value for i32: {e:?}"))? | ||
.into(), | ||
"i64" => val | ||
.parse::<i64>() | ||
.map_err(|e| format!("invalid argument value for i64: {e:?}"))? | ||
.into(), | ||
"f32" => val | ||
.parse::<f32>() | ||
.map_err(|e| format!("invalid argument value for f32: {e:?}"))? | ||
.into(), | ||
"f64" => val | ||
.parse::<f64>() | ||
.map_err(|e| format!("invalid argument value for f64: {e:?}"))? | ||
.into(), | ||
t => return Err(format!("Invalid arg type: {}", t)), | ||
}; | ||
|
||
Ok(WasmArg(arg)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
(module | ||
(func $loopExample (export "loopExample") | ||
(local i32) ;; Declare a local i32 variable, let's call it 'i' | ||
(i32.const 0) ;; Initialize 'i' to 0 | ||
(local.set 0) | ||
|
||
(loop $loopStart ;; Start of the loop | ||
(local.get 0) ;; Get the current value of 'i' | ||
(i32.const 1) ;; Push 1 onto the stack | ||
(i32.add) ;; Add 'i' and 1 | ||
(local.set 0) ;; Update 'i' with the new value | ||
|
||
(local.get 0) ;; Push the current value of 'i' to check the condition | ||
(i32.const 10) ;; Push 10 onto the stack | ||
(i32.lt_s) ;; Check if 'i' is less than 10 | ||
(br_if $loopStart) ;; If 'i' < 10, continue the loop | ||
) | ||
) | ||
) | ||
|