From 1716f84c89af3a489ceef7412d15a831f99bacbc Mon Sep 17 00:00:00 2001 From: Paul Osborne Date: Mon, 22 Sep 2025 14:23:04 -0500 Subject: [PATCH] Support using alternate standard adapters This also attempts to change the stub impl to allow for only stubbing missing wasi bits expected by the runtime and missing from the selected adapter. --- Cargo.lock | 169 +++++++++++++++++++++++++----------------------- Cargo.toml | 1 + build.rs | 8 --- src/command.rs | 26 ++++++++ src/lib.rs | 4 +- src/link.rs | 24 +++---- src/stubwasi.rs | 23 +++++-- 7 files changed, 147 insertions(+), 108 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1bac962..5650b08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,6 +508,7 @@ dependencies = [ "test-generator", "tokio", "toml", + "wasi-preview1-component-adapter-provider", "wasm-encoder 0.235.0", "wasmparser 0.235.0", "wasmtime", @@ -570,36 +571,36 @@ dependencies = [ [[package]] name = "cranelift-assembler-x64" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "226b7077389885873ffad5d778e8512742580a6e11b0f723072f41f305d3652f" +checksum = "2ce81edaca6167d1f78da026afa92d7ff957a80aa82a79076e11cd34cde20165" dependencies = [ "cranelift-assembler-x64-meta", ] [[package]] name = "cranelift-assembler-x64-meta" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9cfeae5a23c8cf9c43381f49211f3ce6dc1da1d46f1c5d06966e6258cc483fa" +checksum = "4d0d51e12f958551165969c6e8767e1e461729f6c1ccae923b0ba1d5cbcbbbf8" dependencies = [ "cranelift-srcgen", ] [[package]] name = "cranelift-bforest" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c88c577c6af92b550cb83455c331cf8e1bc89fe0ccc3e7eb0fa617ed1d63056" +checksum = "41294c755094d2c8a514cea903039742474423f2e91601332eab5f4094f76333" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-bitset" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "370f0aa7f1816bf0f838048d69b72d6cf12ef2fc3b37f6997fe494ffb9feb3ad" +checksum = "ebb6f5d0df5bd0d02c63ec48e8f2e38a176b123f59e084f22caf89a0d0593e7e" dependencies = [ "serde", "serde_derive", @@ -607,9 +608,9 @@ dependencies = [ [[package]] name = "cranelift-codegen" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d1a10a8a2958b68ecd261e565eef285249e242a8447ac959978319eabbb4a55" +checksum = "e543cdb278b7c15f739021cf880ee1808c68fa2402febb87edb9307f552c8fec" dependencies = [ "bumpalo", "cranelift-assembler-x64", @@ -634,9 +635,9 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f319986d5ae1386cfec625c70f8c01e52dc1f910aa6aaee7740bf8842d4e19c7" +checksum = "f979c75cfd712dbc754799dfe4a4d0db7a51defc2e36d006b27a8a63e018eece" dependencies = [ "cranelift-assembler-x64-meta", "cranelift-codegen-shared", @@ -646,24 +647,24 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed52f5660397039c3c741c3acf18746445f4e20629b7280d9f2ccfe57e2b1efd" +checksum = "d2f36e74ba4033490587a47952f74390cb7d4f1fc1fa28ace50564e491f1e38f" [[package]] name = "cranelift-control" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79bde8d48e1840702574e28c5d7d4499441435af71e6c47450881f84ce2b60a5" +checksum = "f6671962c7d65b9a7ad038cd92da6784744d8a9ecf8ded8bb9a1f7046dbe2ccf" dependencies = [ "arbitrary", ] [[package]] name = "cranelift-entity" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0335ac187211ac94c254826b6e78d23b8654ae09ebf0830506a827a2647162f" +checksum = "ee832f8329fa87c5df6c1d64a8506a58031e6f8a190d9b21b1900272a4dbb47d" dependencies = [ "cranelift-bitset", "serde", @@ -672,9 +673,9 @@ dependencies = [ [[package]] name = "cranelift-frontend" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4fce5fcf93c1fece95d0175b15fbaf0808b187430bc06c8ecde80db0ed58c5e" +checksum = "4f7bc17aa3277214eab4b63a03544b1b46962154012b751c9f14c2a5419c6471" dependencies = [ "cranelift-codegen", "log", @@ -684,15 +685,15 @@ dependencies = [ [[package]] name = "cranelift-isle" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13fc8d838a2bf28438dbaf6ccdbc34531b6a972054f43fd23be7f124121ce6e0" +checksum = "cff02dcecae2e7e9c61b713f1fb46eabecdca9f55b49f99859ceb1a3e7f4a9cb" [[package]] name = "cranelift-native" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0975ce66adcf2e0729d06b1d3efea0398d793d1f39c2e0a6f52a347537836693" +checksum = "90f76fd681f35bdf17be9c3e516b9acc0c7bd61b81faf95496decd8e0000979c" dependencies = [ "cranelift-codegen", "libc", @@ -701,9 +702,9 @@ dependencies = [ [[package]] name = "cranelift-srcgen" -version = "0.121.1" +version = "0.121.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4493a9b500bb02837ea2fb7d4b58c1c21c37a470ae33c92659f4e637aad14c9" +checksum = "8c3d9071bc5ee5573e723d9d84a45b7025a29e8f2c5ad81b3b9d0293129541d9" [[package]] name = "crc32fast" @@ -1822,9 +1823,9 @@ dependencies = [ [[package]] name = "pulley-interpreter" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe0e8f39bc99694ce6fc8df7df7ed258d38d255a9268e2ff964f67f4a6588cdb" +checksum = "be14280b69a9cbb6ada02a7aa5f7b3f1b72d1043b5bc9336990b700525dea6e3" dependencies = [ "cranelift-bitset", "log", @@ -1834,9 +1835,9 @@ dependencies = [ [[package]] name = "pulley-macros" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9276d404009cc49f3b8befeb8ffc1d868c5ea732bd9d72ab3e64231187f908c5" +checksum = "076f1be746801280af4c96c4407b5fd1d09cfa53ab27ba0ac7dd8f207e7bbf83" dependencies = [ "proc-macro2", "quote", @@ -2518,11 +2519,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.12" +version = "2.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +checksum = "3467d614147380f2e4e374161426ff399c91084acd2363eaf549172b3d5e60c0" dependencies = [ - "thiserror-impl 2.0.12", + "thiserror-impl 2.0.16", ] [[package]] @@ -2538,9 +2539,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.12" +version = "2.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +checksum = "6c5e1be1c48b9172ee610da68fd9cd2770e7a4056cb3fc98710ee6906f0c7960" dependencies = [ "proc-macro2", "quote", @@ -2853,6 +2854,12 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasi-preview1-component-adapter-provider" +version = "37.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d165ef21411cf53448af5aac9ba944b936c0a1d2eb93f0d34b6211a765dd53da" + [[package]] name = "wasm-bindgen" version = "0.2.100" @@ -3036,9 +3043,9 @@ dependencies = [ [[package]] name = "wasmtime" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2523d3347356a74e9c312c2c96e709c82d998dcafdca97f6d620e69c032fd043" +checksum = "ec10e50038f22ab407fdd8708120b8feed3450a02618efcf26ca47e82122927d" dependencies = [ "addr2line", "anyhow", @@ -3092,18 +3099,18 @@ dependencies = [ [[package]] name = "wasmtime-asm-macros" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c45ecc343d3ad4629d5882e94f3b0f0fac22a043c07e64373381168ae00c259" +checksum = "4d379cda46d6fd18619e282a75fbb09b70b3d0f166b605f45b4059dfaf9dc6ce" dependencies = [ "cfg-if", ] [[package]] name = "wasmtime-cache" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eb1976337108c8b9f80b05e9b909bf603f85c4ea97e31c112876a36d3cdcb98" +checksum = "f421723a7736c0767ceb422afef69b41526864bd0f026e0f49bb2bde7168f9a6" dependencies = [ "anyhow", "base64", @@ -3121,9 +3128,9 @@ dependencies = [ [[package]] name = "wasmtime-component-macro" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3491c0f2511be561a92ac9b086351abc3a0f48c5f5f7d14f3975e246c13838be" +checksum = "6b08be093e0a876da45f79070c2ada4656f2785eb77c01b86ce60be3153920a5" dependencies = [ "anyhow", "proc-macro2", @@ -3136,15 +3143,15 @@ dependencies = [ [[package]] name = "wasmtime-component-util" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26bc084e249f74e61c79077d8937c34fb0af223752b9b1725e3d7ed94b006f23" +checksum = "f0451ce0dd94a33d0dbd57934ce666a04c2753a5262ca2bc84cf6a67cf5303dc" [[package]] name = "wasmtime-cranelift" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0010bd93362c634837e6bb13e213c2d83673b28dc12208b64ddd821fa55f7d33" +checksum = "15aa836683d7398f13f2f26bbe74c404ceaba66b6bbb96700d6b7f91bec90e03" dependencies = [ "anyhow", "cfg-if", @@ -3160,7 +3167,7 @@ dependencies = [ "pulley-interpreter", "smallvec", "target-lexicon", - "thiserror 2.0.12", + "thiserror 2.0.16", "wasmparser 0.233.0", "wasmtime-environ", "wasmtime-math", @@ -3169,9 +3176,9 @@ dependencies = [ [[package]] name = "wasmtime-environ" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36a035dc308ff6be3d790dafdc2e41a128415e20ad864580da49470073e21dc1" +checksum = "317081a0cbbb1f749d348b262575608fc082d47ab11b6247bbe9163eeb955777" dependencies = [ "anyhow", "cpp_demangle", @@ -3196,9 +3203,9 @@ dependencies = [ [[package]] name = "wasmtime-fiber" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdc3c1e4e70cdd3a4572dff79062caa48988f7f1ccf6850d98a4e4c41bf3cfc8" +checksum = "6763b33eceefc443f6477d84dc8751df5f23d280d7e01f28339fa3ec4b00ff13" dependencies = [ "anyhow", "cc", @@ -3212,9 +3219,9 @@ dependencies = [ [[package]] name = "wasmtime-jit-debug" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5d83fa2dea686f76b5437b66045aae6351d359ee11cc4124f9842de63837b81" +checksum = "f935b198c58d3f85b6f8d2fedcbaf71e6f41dee3a8278d60cbe9326b82ac91aa" dependencies = [ "cc", "object", @@ -3224,9 +3231,9 @@ dependencies = [ [[package]] name = "wasmtime-jit-icache-coherence" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44c71d64e8ebe132cd45e9d299a4d0daf261d66bd05cf50a204a1bf8cf96ff1f" +checksum = "8ea6b740d1a35f2cebfe88e013ac8a4a84ff8dabc3a392df920abf554e871cf2" dependencies = [ "anyhow", "cfg-if", @@ -3236,24 +3243,24 @@ dependencies = [ [[package]] name = "wasmtime-math" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "222bfa4769c6931c985711eb49a92748ea0acc4ca85fcd24e945a2f1bacda0c1" +checksum = "62fa317691aedc64aae3a86b3d786e4b2b0007bc0b56e0b6098b8b5a85ab2134" dependencies = [ "libm", ] [[package]] name = "wasmtime-slab" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ac42c7fb0639f7c3e0c1ed0c984050245c55410f3fae334dd5b102e0edfab14" +checksum = "60a06819d24370273021054b50589e3078e7f5cfac15515e58b3fbbebf5e5b39" [[package]] name = "wasmtime-versioned-export-macros" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e052e1d9c30b8f31aff64380caaaff492a9890a412658bcc8866fe626b8e91f" +checksum = "9ca100ed168ffc9b37aefc07a5be440645eab612a2ff6e2ff884e8cc3740e666" dependencies = [ "proc-macro2", "quote", @@ -3262,9 +3269,9 @@ dependencies = [ [[package]] name = "wasmtime-wasi" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8392e2256e2b56167a69c4d5ea5505fc3cd164f088ce7009824ee0abd1671dc" +checksum = "fb691e92f81f339a215c6fb78f10fb9c03886a34995eba84f14d6aa846b72161" dependencies = [ "anyhow", "async-trait", @@ -3281,7 +3288,7 @@ dependencies = [ "io-lifetimes", "rustix 1.0.7", "system-interface", - "thiserror 2.0.12", + "thiserror 2.0.16", "tokio", "tracing", "url", @@ -3293,9 +3300,9 @@ dependencies = [ [[package]] name = "wasmtime-wasi-io" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92a8348338594ee5b46c2decdb921a54fabaaed4cb448f6effb97c49d09e44e7" +checksum = "5703fe2988cf503431c11989bec85bda910077925c232aeaf8d52c408fec48c5" dependencies = [ "anyhow", "async-trait", @@ -3306,9 +3313,9 @@ dependencies = [ [[package]] name = "wasmtime-winch" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2d71e002033124221f6633a462c26067280519fdd7527ba2751f585db779cc6" +checksum = "595f51430606a7b5578f34e0d7c73dca52a22ed24756f2ba9d4d0c1bde8631af" dependencies = [ "anyhow", "cranelift-codegen", @@ -3323,9 +3330,9 @@ dependencies = [ [[package]] name = "wasmtime-wit-bindgen" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f967f5efaaac7694e6bd0d67542a5a036830860e4adf95684260181e85a5d299" +checksum = "233fdcb96f9097be697319ba647ef42bdbdb40e89f04c8ae3713103813b5b793" dependencies = [ "anyhow", "heck", @@ -3376,14 +3383,14 @@ dependencies = [ [[package]] name = "wiggle" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab89466227933ce3d44f2b60eedd2cc46ba3dfd350cf1e938b6422bea18aa422" +checksum = "b61d3bcb6a981233e8b774738f4495ad2c724a0d73ac626f373944e9ba1e3101" dependencies = [ "anyhow", "async-trait", "bitflags", - "thiserror 2.0.12", + "thiserror 2.0.16", "tracing", "wasmtime", "wiggle-macro", @@ -3391,9 +3398,9 @@ dependencies = [ [[package]] name = "wiggle-generate" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f650b2d5981c3359778c49eada0796fcc98e9135bc6f7bb894cab2e2bc4fd04d" +checksum = "6e7f9816357d0d2bd1c0af68b6e302013152dfdeef4b6a488de87dc2026ed6fa" dependencies = [ "anyhow", "heck", @@ -3405,9 +3412,9 @@ dependencies = [ [[package]] name = "wiggle-macro" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4e5f692091b53dbb0835f74cfc03cbd8f384fd3fb493bde6bdc96426e105e84" +checksum = "1e215597862a78dc57fb6988bd09b76af8e632ca4cb74bc26fe3ab129b8cb3b9" dependencies = [ "proc-macro2", "quote", @@ -3448,9 +3455,9 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "winch-codegen" -version = "34.0.1" +version = "34.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d2bf456780101aff8950642fdf984f182816d7f555d5375699200242be78762" +checksum = "cdf007d7940f62127ce4f33a8aa92dadedfdc78c3860a057e06c8c24e26e180d" dependencies = [ "anyhow", "cranelift-assembler-x64", @@ -3459,7 +3466,7 @@ dependencies = [ "regalloc2", "smallvec", "target-lexicon", - "thiserror 2.0.12", + "thiserror 2.0.16", "wasmparser 0.233.0", "wasmtime-cranelift", "wasmtime-environ", diff --git a/Cargo.toml b/Cargo.toml index f102d47..40c1d0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ im-rc = "15.1.0" serde = { version = "1.0.213", features = ["derive"] } toml = "0.8.19" semver = "1.0.23" +wasi-preview1-component-adapter-provider = "37.0.0" [dev-dependencies] assert_cmd = "2.0.16" diff --git a/build.rs b/build.rs index fb91dc1..296219b 100644 --- a/build.rs +++ b/build.rs @@ -61,7 +61,6 @@ fn stubs_for_clippy(out_dir: &Path) -> Result<()> { "libwasi-emulated-signal.so.zst", "libc++.so.zst", "libc++abi.so.zst", - "wasi_snapshot_preview1.reactor.wasm.zst", ]; for file in files { @@ -211,13 +210,6 @@ fn package_all_the_things(out_dir: &Path) -> Result<()> { bail!("no such directory: {}", path.display()) } - compress( - &repo_dir.join("adapters/ab5a4484"), - "wasi_snapshot_preview1.reactor.wasm", - out_dir, - false, - )?; - Ok(()) } diff --git a/src/command.rs b/src/command.rs index fe75668..89cb3bd 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,6 +1,7 @@ use { anyhow::{Context, Result}, clap::Parser as _, + serde::Serialize, std::{ env, ffi::OsString, @@ -83,6 +84,26 @@ pub enum Command { Bindings(Bindings), } +#[derive(clap::ValueEnum, Clone, Default, Debug, Serialize)] +#[serde(rename_all = "kebab-case")] +pub enum WasiAdapter { + /// The "reactor" adapter provides the default adaptation from preview1 to preview2. + /// + /// This adapter implements the wasi:cli/imports world. + #[default] + Reactor, + /// The "command" adapter extends the “reactor” adapter and additionally exports a run function entrypoint. + /// + /// This adapter implements the wasi:cli/command world. + Command, + /// The “proxy” adapter provides implements a HTTP proxy which is more restricted than the "reactor" adapter + /// adapter, as it lacks filesystem, socket, environment, exit, and terminal support, but includes HTTP + /// handlers for incoming and outgoing requests. + /// + /// This adapter implements the wasi:http/proxy world. + Proxy, +} + #[derive(clap::Args, Debug)] pub struct Componentize { /// The name of a Python module containing the app to wrap. @@ -117,6 +138,10 @@ pub struct Componentize { #[arg(short = 'o', long, default_value = "index.wasm")] pub output: PathBuf, + /// Adapter to use + #[arg(short = 'a', long, default_value = "reactor")] + pub adapter: WasiAdapter, + /// If set, replace all WASI imports with trapping stubs. /// /// PLEASE NOTE: This has the effect of baking whatever PRNG seed is generated at build time into the @@ -199,6 +224,7 @@ fn componentize(common: Common, componentize: Componentize) -> Result<()> { &componentize.app_name, &componentize.output, None, + componentize.adapter, componentize.stub_wasi, &common .import_interface_name diff --git a/src/lib.rs b/src/lib.rs index 5a8685b..14fef3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ #![deny(warnings)] use { + crate::command::WasiAdapter, anyhow::{anyhow, bail, ensure, Context, Error, Result}, async_trait::async_trait, bytes::Bytes, @@ -227,6 +228,7 @@ pub async fn componentize( app_name: &str, output_path: &Path, add_to_linker: Option<&dyn Fn(&mut Linker) -> Result<()>>, + adapter: WasiAdapter, stub_wasi: bool, import_interface_names: &HashMap<&str, &str>, export_interface_names: &HashMap<&str, &str>, @@ -343,7 +345,7 @@ pub async fn componentize( dl_openable: false, }); - let component = link::link_libraries(&libraries)?; + let component = link::link_libraries(&libraries, adapter)?; let stubbed_component = if stub_wasi { stubwasi::link_stub_modules(libraries)? diff --git a/src/link.rs b/src/link.rs index b2286a0..27de0c9 100644 --- a/src/link.rs +++ b/src/link.rs @@ -1,10 +1,9 @@ -use std::io::Cursor; - use anyhow::Result; +use wasi_preview1_component_adapter_provider; -use crate::Library; +use crate::{command::WasiAdapter, Library}; -pub fn link_libraries(libraries: &[Library]) -> Result> { +pub fn link_libraries(libraries: &[Library], adapter: WasiAdapter) -> Result> { let mut linker = wit_component::Linker::default() .validate(true) .use_built_in_libdl(true); @@ -18,13 +17,16 @@ pub fn link_libraries(libraries: &[Library]) -> Result> { linker = linker.library(name, module, *dl_openable)?; } - linker = linker.adapter( - "wasi_snapshot_preview1", - &zstd::decode_all(Cursor::new(include_bytes!(concat!( - env!("OUT_DIR"), - "/wasi_snapshot_preview1.reactor.wasm.zst" - ))))?, - )?; + let adapter_module = match adapter { + WasiAdapter::Proxy => { + wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_PROXY_ADAPTER + } + WasiAdapter::Reactor => { + wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_REACTOR_ADAPTER + } + _ => panic!("Adapater not supported"), + }; + linker = linker.adapter("wasi_snapshot_preview1", adapter_module)?; linker.encode().map_err(|e| anyhow::anyhow!(e)) } diff --git a/src/stubwasi.rs b/src/stubwasi.rs index 613a101..e6c042b 100644 --- a/src/stubwasi.rs +++ b/src/stubwasi.rs @@ -12,7 +12,7 @@ use crate::Library; type LinkedStubModules = Option<(Vec, Box u32>)>; pub fn link_stub_modules(libraries: Vec) -> Result { - let mut wasi_imports = HashMap::new(); + let mut imports_to_stub = HashMap::new(); let mut linker = wit_component::Linker::default() .validate(true) .use_built_in_libdl(true); @@ -23,11 +23,11 @@ pub fn link_stub_modules(libraries: Vec) -> Result) -> Result= old_adapter_count); Ok(Some(( @@ -66,6 +66,7 @@ pub fn link_stub_modules(libraries: Vec) -> Result( module: &'a [u8], imports: &mut HashMap<&'a str, HashMap<&'a str, FuncType>>, + only_missing: bool, ) -> Result<(), Error> { let mut types = Vec::new(); for payload in Parser::new(0).parse_all(module) { @@ -80,9 +81,17 @@ fn add_wasi_imports<'a>( for import in reader { let import = import?; - if import.module == "wasi_snapshot_preview1" - || import.module.starts_with("wasi:") - { + // if `only_missing`, we should only stub wasi modules that are + // not present in the selected wasi adapter. + let is_adapter = import.module == "wasi_snapshot1_preview1"; + let has_wasi_prefix = import.module.starts_with("wasi:"); + let should_stub = if only_missing { + !is_adapter && has_wasi_prefix + } else { + is_adapter || has_wasi_prefix + }; + + if should_stub { if let TypeRef::Func(ty) = import.ty { imports .entry(import.module)