diff --git a/.gitmodules b/.gitmodules index a926325f..77ce20c9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "preview2-prototyping"] path = preview2-prototyping url = git@github.com:bytecodealliance/preview2-prototyping +[submodule "component-init"] + path = crates/component-init + url = git@github.com:dicej/component-init diff --git a/Cargo.lock b/Cargo.lock index 2b57ab6d..612c8a37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,17 @@ version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854" +[[package]] +name = "async-trait" +version = "0.1.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6dde6e4ed435a4c1ee4e73592f5ba9da2151af10076cc04858746af9352d09" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.28", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -49,6 +60,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "component-init" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "futures", + "wasm-convert", + "wasm-encoder 0.29.0", + "wasmparser 0.107.0", +] + [[package]] name = "cranelift-entity" version = "0.99.0" @@ -87,6 +110,95 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" + +[[package]] +name = "futures-executor" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" + +[[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.28", +] + +[[package]] +name = "futures-sink" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" + +[[package]] +name = "futures-task" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" + +[[package]] +name = "futures-util" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + [[package]] name = "gimli" version = "0.26.2" @@ -248,6 +360,18 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" +[[package]] +name = "pin-project-lite" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cc1b0bf1727a77a54b6654e7b5f1af8604923edc8b81885f8ec92f9e3f0a05" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "proc-macro2" version = "1.0.66" @@ -320,6 +444,15 @@ dependencies = [ "serde", ] +[[package]] +name = "slab" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6528351c9bc8ab22353f9d776db39a20288e8d6c37ef8cfe3317cf875eecfc2d" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.11.0" @@ -512,6 +645,15 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "wasm-convert" +version = "0.1.0" +source = "git+https://github.com/dicej/wasm-convert#cd61fa7c3d1e35d0dc1700cdf5e4ade0f342c59b" +dependencies = [ + "wasm-encoder 0.29.0", + "wasmparser 0.107.0", +] + [[package]] name = "wasm-encoder" version = "0.29.0" @@ -551,6 +693,16 @@ version = "0.80.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "449167e2832691a1bff24cde28d2804e90e09586a448c8e76984792c44334a6b" +[[package]] +name = "wasmparser" +version = "0.107.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29e3ac9b780c7dda0cac7a52a5d6d2d6707cc6e3451c9db209b6c758f40d7acb" +dependencies = [ + "indexmap 1.9.3", + "semver", +] + [[package]] name = "wasmparser" version = "0.110.0" diff --git a/Cargo.toml b/Cargo.toml index 97f9c1c5..41421a85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,8 @@ [workspace] -members = ["crates/spidermonkey-embedding-splicer"] +members = [ + "crates/spidermonkey-embedding-splicer", + "crates/component-init" +] exclude = [ "deps/js-compute-runtime/runtime/js-compute-runtime/rust-url", "deps/js-compute-runtime/runtime/js-compute-runtime/rust-encoding" diff --git a/crates/component-init b/crates/component-init new file mode 160000 index 00000000..665e61b5 --- /dev/null +++ b/crates/component-init @@ -0,0 +1 @@ +Subproject commit 665e61b5a1adb8d1e73751d23a72cc849ed1666d diff --git a/crates/spidermonkey-embedding-splicer/src/stub_wasi.rs b/crates/spidermonkey-embedding-splicer/src/stub_wasi.rs index 6a9594b6..0f2a29bc 100644 --- a/crates/spidermonkey-embedding-splicer/src/stub_wasi.rs +++ b/crates/spidermonkey-embedding-splicer/src/stub_wasi.rs @@ -1,6 +1,7 @@ use anyhow::{bail, Result}; use walrus::{ + ir::{MemArg, StoreKind}, Function, FunctionBuilder, FunctionKind, ImportKind, ImportedFunction, InstrSeqBuilder, LocalId, Module, ValType, }; @@ -64,15 +65,28 @@ pub fn stub_wasi(wasm: Vec, stdout: bool) -> Result> { stub_import(&mut module, WASI, "proc_exit", unreachable_stub)?; stub_import(&mut module, WASI, "random_get", unreachable_stub)?; + let mem = module.memories.iter().next().unwrap().id(); + // (func (param i32 i32 i32 i32) (result i32))) + let ptr_local = module.locals.add(ValType::I32); stub_import(&mut module, WASI, "clock_time_get", |body| { - body.i32_const(0); + body.local_get(ptr_local) + .i64_const(0) + .store( + mem, + StoreKind::I64 { atomic: false }, + MemArg { + align: 3, + offset: 0, + }, + ) + .i32_const(0); Ok(vec![]) })?; // (func (param i32 i32) (result i32))) stub_import(&mut module, WASI, "fd_fdstat_get", |body| { - body.i32_const(0); + body.i32_const(8); Ok(vec![]) })?; diff --git a/lib/spidermonkey-embedding-splicer.core.wasm b/lib/spidermonkey-embedding-splicer.core.wasm index 3a235994..f881d5e0 100644 Binary files a/lib/spidermonkey-embedding-splicer.core.wasm and b/lib/spidermonkey-embedding-splicer.core.wasm differ diff --git a/lib/spidermonkey-embedding-splicer.d.ts b/lib/spidermonkey-embedding-splicer.d.ts index 7d8b1464..d904c125 100644 --- a/lib/spidermonkey-embedding-splicer.d.ts +++ b/lib/spidermonkey-embedding-splicer.d.ts @@ -24,15 +24,15 @@ export interface SpliceResult { importWrappers: [string, string][], imports: [string, string, number][], } -import { Environment } from './interfaces/environment'; -import { Exit } from './interfaces/exit'; -import { Preopens } from './interfaces/preopens'; -import { Stderr } from './interfaces/stderr'; -import { Stdin } from './interfaces/stdin'; -import { Stdout } from './interfaces/stdout'; -import { WallClock } from './interfaces/wall-clock'; -import { Filesystem } from './interfaces/filesystem'; -import { Streams } from './interfaces/streams'; -import { Random } from './interfaces/random'; +import { WasiCliBaseEnvironment } from './imports/wasi-cli-base-environment'; +import { WasiCliBaseExit } from './imports/wasi-cli-base-exit'; +import { WasiCliBasePreopens } from './imports/wasi-cli-base-preopens'; +import { WasiCliBaseStderr } from './imports/wasi-cli-base-stderr'; +import { WasiCliBaseStdin } from './imports/wasi-cli-base-stdin'; +import { WasiCliBaseStdout } from './imports/wasi-cli-base-stdout'; +import { WasiClocksWallClock } from './imports/wasi-clocks-wall-clock'; +import { WasiFilesystemFilesystem } from './imports/wasi-filesystem-filesystem'; +import { WasiIoStreams } from './imports/wasi-io-streams'; +import { WasiRandomRandom } from './imports/wasi-random-random'; export function stubWasi(engine: Uint8Array | ArrayBuffer, stdout: boolean): Uint8Array; export function spliceBindings(sourceName: string | null, spidermonkeyEngine: Uint8Array | ArrayBuffer, witWorld: string | null, witPath: string | null, worldName: string | null): SpliceResult;