feat(plugin/runnner): Support wasm32-wasi targets (#3271)

This commit is contained in:
OJ Kwon 2022-01-14 20:40:00 -08:00 committed by GitHub
parent 6ad418339e
commit a4c497464d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 216 additions and 14 deletions

View File

@ -194,7 +194,7 @@ jobs:
uses: actions-rs/toolchain@v1 uses: actions-rs/toolchain@v1
with: with:
profile: minimal profile: minimal
target: wasm32-unknown-unknown target: wasm32-wasi
- uses: denoland/setup-deno@v1 - uses: denoland/setup-deno@v1
if: matrix.crate == 'swc_bundler' if: matrix.crate == 'swc_bundler'

179
Cargo.lock generated
View File

@ -147,6 +147,12 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "base-x"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b"
[[package]] [[package]]
name = "base64" name = "base64"
version = "0.11.0" version = "0.11.0"
@ -245,6 +251,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]] [[package]]
name = "bytes" name = "bytes"
version = "1.1.0" version = "1.1.0"
@ -279,7 +291,7 @@ dependencies = [
"libc", "libc",
"num-integer", "num-integer",
"num-traits", "num-traits",
"time", "time 0.1.43",
"wasm-bindgen", "wasm-bindgen",
"winapi", "winapi",
] ]
@ -340,6 +352,12 @@ dependencies = [
"tiny-keccak", "tiny-keccak",
] ]
[[package]]
name = "const_fn"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbdcdcb6d86f71c5e97409ad45898af11cbc995b4ee8112d59095a28d376c935"
[[package]] [[package]]
name = "constant_time_eq" name = "constant_time_eq"
version = "0.1.5" version = "0.1.5"
@ -672,6 +690,12 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "discard"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
[[package]] [[package]]
name = "either" name = "either"
version = "1.6.1" version = "1.6.1"
@ -819,6 +843,15 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "generational-arena"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e1d3b771574f62d0548cee0ad9057857e9fc25d7a3335f140c84f6acd0bf601"
dependencies = [
"cfg-if 0.1.10",
]
[[package]] [[package]]
name = "generic-array" name = "generic-array"
version = "0.14.5" version = "0.14.5"
@ -2385,6 +2418,12 @@ dependencies = [
"opaque-debug", "opaque-debug",
] ]
[[package]]
name = "sha1"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d"
[[package]] [[package]]
name = "sharded-slab" name = "sharded-slab"
version = "0.1.4" version = "0.1.4"
@ -2454,6 +2493,15 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "standback"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e113fb6f3de07a243d434a56ec6f186dfd51cb08448239fe7bcae73f87ff28ff"
dependencies = [
"version_check",
]
[[package]] [[package]]
name = "static-map-macro" name = "static-map-macro"
version = "0.2.1" version = "0.2.1"
@ -2472,6 +2520,55 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "stdweb"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d022496b16281348b52d0e30ae99e01a73d737b2f45d38fed4edf79f9325a1d5"
dependencies = [
"discard",
"rustc_version",
"stdweb-derive",
"stdweb-internal-macros",
"stdweb-internal-runtime",
"wasm-bindgen",
]
[[package]]
name = "stdweb-derive"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef"
dependencies = [
"proc-macro2",
"quote",
"serde",
"serde_derive",
"syn",
]
[[package]]
name = "stdweb-internal-macros"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58fa5ff6ad0d98d1ffa8cb115892b6e69d67799f6763e162a1c9db421dc22e11"
dependencies = [
"base-x",
"proc-macro2",
"quote",
"serde",
"serde_derive",
"serde_json",
"sha1",
"syn",
]
[[package]]
name = "stdweb-internal-runtime"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213701ba3370744dcd1a12960caa4843b3d68b4d1c0a5d575e0d65b2ee9d16c0"
[[package]] [[package]]
name = "string_cache" name = "string_cache"
version = "0.8.2" version = "0.8.2"
@ -3441,6 +3538,7 @@ dependencies = [
"testing", "testing",
"wasmer", "wasmer",
"wasmer-cache", "wasmer-cache",
"wasmer-wasi",
] ]
[[package]] [[package]]
@ -3631,6 +3729,44 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "time"
version = "0.2.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4752a97f8eebd6854ff91f1c1824cd6160626ac4bd44287f7f4ea2035a02a242"
dependencies = [
"const_fn",
"libc",
"standback",
"stdweb",
"time-macros",
"version_check",
"winapi",
]
[[package]]
name = "time-macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "957e9c6e26f12cb6d0dd7fc776bb67a706312e7299aed74c8dd5b17ebb27e2f1"
dependencies = [
"proc-macro-hack",
"time-macros-impl",
]
[[package]]
name = "time-macros-impl"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd3c141a1b43194f3f56a1411225df8646c55781d5f26db825b3d98507eb482f"
dependencies = [
"proc-macro-hack",
"proc-macro2",
"quote",
"standback",
"syn",
]
[[package]] [[package]]
name = "tiny-keccak" name = "tiny-keccak"
version = "2.0.2" version = "2.0.2"
@ -4165,6 +4301,17 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "wasmer-vfs"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a3a58a3700781aa4f5344915ea082086e75ba7ebe294f60ae499614db92dd00"
dependencies = [
"libc",
"thiserror",
"tracing",
]
[[package]] [[package]]
name = "wasmer-vm" name = "wasmer-vm"
version = "2.1.1" version = "2.1.1"
@ -4187,6 +4334,36 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "wasmer-wasi"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2b1d981ad312dac6e74a41a35b9bca41a6d1157c3e6a575fb1041e4b516610"
dependencies = [
"cfg-if 1.0.0",
"generational-arena",
"getrandom 0.2.3",
"libc",
"thiserror",
"tracing",
"wasm-bindgen",
"wasmer",
"wasmer-vfs",
"wasmer-wasi-types",
"winapi",
]
[[package]]
name = "wasmer-wasi-types"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7731240c0ae536623414beb73091dddf68d1a080f49086fc31ec916536b1af98"
dependencies = [
"byteorder",
"time 0.2.27",
"wasmer-types",
]
[[package]] [[package]]
name = "wasmparser" name = "wasmparser"
version = "0.78.2" version = "0.78.2"

View File

@ -21,6 +21,7 @@ swc_ecma_ast = {version = "0.65.0", path = "../swc_ecma_ast", features = ["rkyv-
swc_ecma_parser = {version = "0.87.0", path = "../swc_ecma_parser"} swc_ecma_parser = {version = "0.87.0", path = "../swc_ecma_parser"}
wasmer = "2.1.1" wasmer = "2.1.1"
wasmer-cache = "2.1.1" wasmer-cache = "2.1.1"
wasmer-wasi = "2.1.1"
[dev-dependencies] [dev-dependencies]
testing = {version = "0.18.0", path = "../testing"} testing = {version = "0.18.0", path = "../testing"}

View File

@ -8,8 +8,9 @@ use once_cell::sync::Lazy;
use parking_lot::Mutex; use parking_lot::Mutex;
use resolve::PluginCache; use resolve::PluginCache;
use swc_common::{collections::AHashMap, plugin::Serialized}; use swc_common::{collections::AHashMap, plugin::Serialized};
use wasmer::{imports, Array, Instance, Memory, MemoryType, Module, Store, WasmPtr}; use wasmer::{imports, Array, Exports, Instance, Memory, MemoryType, Module, Store, WasmPtr};
use wasmer_cache::{Cache, Hash}; use wasmer_cache::{Cache, Hash};
use wasmer_wasi::{get_wasi_version, WasiState};
pub mod resolve; pub mod resolve;
@ -96,9 +97,36 @@ fn load_plugin(plugin_path: &Path, cache: &mut Option<PluginCache>) -> Result<In
return match module { return match module {
Ok(module) => { Ok(module) => {
let memory = Memory::new(&wasmer_store, MemoryType::new(1, None, false))?; let memory = Memory::new(&wasmer_store, MemoryType::new(1, None, false))?;
let import_object = imports! { let wasi_version = get_wasi_version(&module, true);
"env" => {
"memory" => memory // Assume plugin binary targets wasm32-wasi if wasi version can be found
let import_object = if wasi_version.is_some() {
// Create the `WasiEnv`.
let mut wasi_env = WasiState::new(
plugin_path
.file_name()
.and_then(|f| f.to_str())
.expect("Plugin path missing file name"),
)
.finalize()?;
// Generate an `ImportObject` from wasi_env
let mut import_object = wasi_env.import_object(&module)?;
// Inject `memory` into generated import_object, this is required to allocate
// guest (plugin)'s memory space from the host (SWC).
let mut env = Exports::new();
env.insert("memory", memory);
import_object.register("env", env);
import_object
}
// Not able to detect wasi version in binary - assume plugin targets
// wasm32-unknown-unknown
else {
imports! {
"env" => {
"memory" => memory
}
} }
}; };

View File

@ -26,11 +26,7 @@ fn build_plugin(dir: &Path) -> Result<PathBuf, Error> {
cmd.stderr(Stdio::inherit()).output()?; cmd.stderr(Stdio::inherit()).output()?;
} }
for entry in fs::read_dir( for entry in fs::read_dir(&dir.join("target").join("wasm32-wasi").join("debug"))? {
&dir.join("target")
.join("wasm32-unknown-unknown")
.join("debug"),
)? {
let entry = entry?; let entry = entry?;
let s = entry.file_name().to_string_lossy().into_owned(); let s = entry.file_name().to_string_lossy().into_owned();

View File

@ -9,7 +9,7 @@ FOR %%A in (%*) DO (
) )
REM Build wasm binary REM Build wasm binary
cargo build %* --target=wasm32-unknown-unknown cargo build %* --target=wasm32-wasi
REM Build polyfill cli, run polyfill over generated wasm binary REM Build polyfill cli, run polyfill over generated wasm binary
PUSHD multivalue-polyfill PUSHD multivalue-polyfill

View File

@ -10,7 +10,7 @@ do
done done
# Build wasm binary # Build wasm binary
cargo build $@ --target=wasm32-unknown-unknown cargo build $@ --target=wasm32-wasi
# Build polyfill cli, run polyfill over generated wasm binary # Build polyfill cli, run polyfill over generated wasm binary
pushd multivalue-polyfill pushd multivalue-polyfill

View File

@ -28,7 +28,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let target_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?) let target_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?)
.join("..") .join("..")
.join("target") .join("target")
.join("wasm32-unknown-unknown") .join("wasm32-wasi")
.join(target[1].clone()); .join(target[1].clone());
let input_path = fs::read_dir(target_path)? let input_path = fs::read_dir(target_path)?