Skip to content

Commit

Permalink
feat(EXC): Name Section for Metadata Command (#69)
Browse files Browse the repository at this point in the history
Add support for keeping the name section in the Wasm module when running
the `metadata` command. Currently that command will always drop the name
section when mutating the custom sections.
  • Loading branch information
adambratschikaye committed Sep 5, 2024
1 parent 9293dfc commit 085bbbf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ enum SubCommand {
/// Visibility of metadata
#[clap(short, long, value_parser = ["public", "private"], default_value = "private")]
visibility: String,
#[clap(short, long)]
keep_name_section: bool,
},
/// Limit resource usage
Resource {
Expand Down Expand Up @@ -94,6 +96,9 @@ fn main() -> anyhow::Result<()> {
SubCommand::Optimize {
keep_name_section, ..
} => keep_name_section,
SubCommand::Metadata {
keep_name_section, ..
} => keep_name_section,
_ => false,
};
let mut m = ic_wasm::utils::parse_wasm_file(opts.input, keep_name_section)?;
Expand Down Expand Up @@ -155,6 +160,7 @@ fn main() -> anyhow::Result<()> {
data,
file,
visibility,
keep_name_section: _,
} => {
use ic_wasm::metadata::*;
if let Some(name) = name {
Expand Down
36 changes: 36 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use assert_cmd::Command;

use std::fs;
use std::path::Path;

Expand Down Expand Up @@ -34,6 +35,21 @@ fn assert_wasm(expected: &str) {
}
}

fn assert_functions_are_named() {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests");
let out = path.join("out.wasm");

let module = walrus::Module::from_file(out).unwrap();
let name_count = module.funcs.iter().filter(|f| f.name.is_some()).count();
let total = module.funcs.iter().count();
// Walrus doesn't give direct access to the name section, but as a proxy
// just check that moste functions have names.
assert!(
name_count > total / 2,
"Module has {total} functions but only {name_count} have names."
)
}

#[test]
fn instrumentation() {
wasm_input("motoko.wasm", true)
Expand Down Expand Up @@ -352,3 +368,23 @@ icp:public whatever
)
.success();
}

#[test]
fn metadata_keep_name_section() {
for file in [
"motoko.wasm",
"classes.wasm",
"motoko-region.wasm",
"rust.wasm",
] {
wasm_input(file, true)
.arg("metadata")
.arg("foo")
.arg("-d")
.arg("hello")
.arg("--keep-name-section")
.assert()
.success();
assert_functions_are_named();
}
}

0 comments on commit 085bbbf

Please sign in to comment.