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

feat: Add hexadecimal notation support for integer arguments #9989

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a88b8b8
feat: Add hexadecimal notation support for integer arguments
VolodymyrBg Jan 12, 2025
b76c18d
A new WAT file hex_args.wat
VolodymyrBg Jan 13, 2025
59e274c
A new test function hex_integer_args()
VolodymyrBg Jan 13, 2025
60b1e2b
Update cli_tests.rs
VolodymyrBg Jan 13, 2025
6596a70
Update cli_tests.rs
VolodymyrBg Jan 13, 2025
4276ade
Update hex_args.wat
VolodymyrBg Jan 13, 2025
baf60c3
Update cli_tests.rs
VolodymyrBg Jan 13, 2025
e801981
Update cli_tests.rs
VolodymyrBg Jan 13, 2025
f949657
Update cli_tests.rs
VolodymyrBg Jan 13, 2025
32db4a8
Update cli_tests.rs
VolodymyrBg Jan 13, 2025
6a7e7bc
Update cli_tests.rs
VolodymyrBg Jan 13, 2025
4d6760f
Update cli_tests.rs
VolodymyrBg Jan 13, 2025
232fb04
Update hex_args.wat
VolodymyrBg Jan 13, 2025
4d33077
Update run.rs
VolodymyrBg Jan 13, 2025
2ea8539
Update run.rs
VolodymyrBg Jan 13, 2025
f002a86
Update run.rs
VolodymyrBg Jan 13, 2025
8ced349
Update run.rs
VolodymyrBg Jan 13, 2025
c892ca4
Update run.rs
VolodymyrBg Jan 14, 2025
cf89ca6
Update run.rs
VolodymyrBg Jan 14, 2025
c7ff06e
Update run.rs
VolodymyrBg Jan 14, 2025
34a648d
Update run.rs
VolodymyrBg Jan 14, 2025
6f8848e
Update run.rs
VolodymyrBg Jan 14, 2025
3c09131
Update run.rs
VolodymyrBg Jan 14, 2025
7929fd4
Update run.rs
VolodymyrBg Jan 14, 2025
a2e3881
Update run.rs
VolodymyrBg Jan 14, 2025
e2f7f46
Update run.rs
VolodymyrBg Jan 14, 2025
64f3333
Update run.rs
VolodymyrBg Jan 14, 2025
8224789
Update run.rs
VolodymyrBg Jan 14, 2025
216494a
Update run.rs
VolodymyrBg Jan 14, 2025
312dcaa
Update run.rs
VolodymyrBg Jan 14, 2025
69ab6cc
Update run.rs
VolodymyrBg Jan 14, 2025
169b103
Update run.rs
VolodymyrBg Jan 14, 2025
d9a8b35
Delete tests/all/cli_tests/hex_args.wat
VolodymyrBg Jan 14, 2025
529d966
Create numeric_args.wat
VolodymyrBg Jan 14, 2025
fdc8aae
Update numeric_args.wat
VolodymyrBg Jan 14, 2025
7316dc7
Update run.rs
VolodymyrBg Jan 14, 2025
0addd96
Update cli_tests.rs
VolodymyrBg Jan 14, 2025
205b254
Update run.rs
VolodymyrBg Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,11 +523,16 @@ impl RunCommand {
.to_str()
.ok_or_else(|| anyhow!("argument is not valid utf-8: {val:?}"))?;
values.push(match ty {
// TODO: integer parsing here should handle hexadecimal notation
// like `0x0...`, but the Rust standard library currently only
// parses base-10 representations.
ValType::I32 => Val::I32(val.parse()?),
ValType::I64 => Val::I64(val.parse()?),
ValType::I32 => Val::I32(if val.starts_with("0x") || val.starts_with("0X") {
i32::from_str_radix(&val[2..], 16)?
} else {
val.parse()?
}),
ValType::I64 => Val::I64(if val.starts_with("0x") || val.starts_with("0X") {
i64::from_str_radix(&val[2..], 16)?
} else {
val.parse()?
}),
ValType::F32 => Val::F32(val.parse::<f32>()?.to_bits()),
ValType::F64 => Val::F64(val.parse::<f64>()?.to_bits()),
t => bail!("unsupported argument type {:?}", t),
Expand Down
19 changes: 19 additions & 0 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2124,3 +2124,22 @@ fn unreachable_without_wasi() -> Result<()> {
assert_trap_code(&output.status);
Ok(())
}

#[test]
fn hex_integer_args() -> Result<()> {
let wasm = build_wasm("tests/all/cli_tests/hex_args.wat")?;
let output = run_wasmtime_for_output(
&[
wasm.path().to_str().unwrap(),
"--invoke",
"sum",
"0x2A", // 42 in hex
"0xFF", // 255 in hex
],
None,
)?;
assert!(output.status.success());
let stdout = String::from_utf8(output.stdout)?;
assert_eq!(stdout.trim(), "297"); // 42 + 255 = 297
Ok(())
}
6 changes: 6 additions & 0 deletions tests/all/cli_tests/hex_args.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(module
(func $sum (export "sum") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add)
)
Loading