diff --git a/JS/wasm/crates/cli/Cargo.toml b/JS/wasm/crates/cli/Cargo.toml index de325a491..28e1403e7 100644 --- a/JS/wasm/crates/cli/Cargo.toml +++ b/JS/wasm/crates/cli/Cargo.toml @@ -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" diff --git a/JS/wasm/crates/cli/build.rs b/JS/wasm/crates/cli/build.rs index 9d2752189..7f21a279d 100644 --- a/JS/wasm/crates/cli/build.rs +++ b/JS/wasm/crates/cli/build.rs @@ -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 { diff --git a/JS/wasm/crates/cli/src/main.rs b/JS/wasm/crates/cli/src/main.rs index 37816c94c..83972ca8d 100644 --- a/JS/wasm/crates/cli/src/main.rs +++ b/JS/wasm/crates/cli/src/main.rs @@ -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"); @@ -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(()); } diff --git a/JS/wasi_snapshot_preview1.reactor.wasm b/JS/wasm/crates/cli/wasi_snapshot_preview1.reactor.wasm similarity index 100% rename from JS/wasi_snapshot_preview1.reactor.wasm rename to JS/wasm/crates/cli/wasi_snapshot_preview1.reactor.wasm