Skip to content

Commit

Permalink
programmatically adapt module to component in compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
redoC-A2k committed May 26, 2024
1 parent 2e15379 commit 7ad5a92
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion JS/wasm/crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ path = "src/main.rs"
[dependencies]
wizer = { workspace = true }
anyhow = { workspace = true }
clap = { version = "4.1.4", features = [ "derive" ] }
clap = { version = "4.1.4", features = ["derive"] }
binaryen = { git = "https://github.com/pepyakin/binaryen-rs" }
wit-component = { version = "0.208.1" }

[build-dependencies]
anyhow = "1.0.79"
Expand Down
5 changes: 5 additions & 0 deletions JS/wasm/crates/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ use std::path::{Path, PathBuf};
use anyhow::Result;

fn main() -> Result<()> {
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
// copy wasi_snapshot_preview1.reactor.wasm file from current directory to OUT_DIR
fs::copy("wasi_snapshot_preview1.reactor.wasm", out_dir.join("adapter.wasm"))?;
println!("cargo:rerun-if-changed=wasi_snapshot_preview1.reactor.wasm");
println!("cargo:warning=copied wasi_snapshot_preview1.reactor.wasm to adapter.wasm in OUT_DIR");
if let Ok("cargo-clippy") = env::var("CARGO_CFG_FEATURE").as_ref().map(String::as_str) {
stub_javy_core_for_clippy()
} else {
Expand Down
9 changes: 9 additions & 0 deletions JS/wasm/crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::io::Read;
use std::process::Command;
use std::{env, fs::File, path::PathBuf};
use wizer::Wizer;
use wit_component::ComponentEncoder;

const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -63,6 +64,14 @@ fn main() -> Result<()> {
bail!("Unable to read wasm binary for wasm-opt optimizations");
}

println!("Adapting module for component model");
let adapter_path = concat!(env!("OUT_DIR"), "/adapter.wasm");
wasm = ComponentEncoder::default()
.validate(true)
.module(&wasm)?
.adapter("wasi_snapshot_preview1", &fs::read(adapter_path).expect("Unable to read adapter"))?
.encode()?;

fs::write(&opts.output, wasm)?;
return Ok(());
}
Expand Down
File renamed without changes.

0 comments on commit 7ad5a92

Please sign in to comment.