Skip to content

Commit

Permalink
update wasmtime (#57)
Browse files Browse the repository at this point in the history
* fix(wasmtime-provider): build also when wasi feature is disabled

Ensure the code compiles also when the `wasi` feature is disabled.

Note well: some **internal** APIs have been changed. Since these
changes are not visible to the consumers of the library there's
no need to do a major/minor version bump.

Signed-off-by: Flavio Castelli <[email protected]>

* chore(deps): update wasmtime

Update the wasmtime-provider crate to consume latest stable release of
wasmtime.

Signed-off-by: Flavio Castelli <[email protected]>

* tests: extend GH workflows

Prior to this commit, the GH workflow was running the tests using
`cargo test --workspace`. This however didn't run the wasmtime-provider
test with the `wasi` feature disabled.

Because of that we didn't notice that building without the `wasi`
feature enabled didn't work.

Signed-off-by: Flavio Castelli <[email protected]>

---------

Signed-off-by: Flavio Castelli <[email protected]>
  • Loading branch information
flavio authored Jul 24, 2023
1 parent 415b914 commit fef792d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 39 deletions.
29 changes: 26 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ on:
workflow_call:

jobs:
build:
linter:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
Expand All @@ -28,5 +27,29 @@ jobs:
target: wasm32-wasi
- name: lint
run: make check
- name: test

workspace-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
components: rustfmt
target: wasm32-wasi
- name: lint
run: make test

wasmtime-provider:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
components: rustfmt
target: wasm32-wasi
- name: test
run: |
cd crates/wasmtime-provider
make test
8 changes: 4 additions & 4 deletions crates/wasmtime-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ wasi = ["wasi-common", "wasi-cap-std-sync", "wasmtime-wasi"]
[dependencies]
wapc = { path = "../wapc", version = "1.1.0" }
log = "0.4"
wasmtime = "10.0"
wasmtime = "11.0"
anyhow = "1.0"
thiserror = "1.0"
cfg-if = "1.0.0"
parking_lot = "0.12"
serde = { version = "1.0", features = ["derive"] }
# feature = wasi
wasmtime-wasi = { version = "10.0", optional = true }
wasi-common = { version = "10.0", optional = true }
wasi-cap-std-sync = { version = "10.0", optional = true }
wasmtime-wasi = { version = "11.0", optional = true }
wasi-common = { version = "11.0", optional = true }
wasi-cap-std-sync = { version = "11.0", optional = true }

[dev-dependencies]
wapc-codec = { path = "../wapc-codec" }
Expand Down
55 changes: 23 additions & 32 deletions crates/wasmtime-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,40 +119,19 @@ struct WapcStore {
}

impl WapcStore {
#[cfg(feature = "wasi")]
fn new(wasi_params: &WasiParams, host: Option<Arc<ModuleState>>) -> Result<WapcStore> {
cfg_if::cfg_if! {
if #[cfg(feature = "wasi")] {
let preopened_dirs = wasi::compute_preopen_dirs(&wasi_params.preopened_dirs, &wasi_params.map_dirs)
.map_err(|e| errors::Error::WasiInitCtxError(format!("Cannot compute preopened dirs: {:?}", e)))?;
let wasi_ctx = wasi::init_ctx(&preopened_dirs, &wasi_params.argv, &wasi_params.env_vars)
.map_err(|e| errors::Error::WasiInitCtxError(e.to_string()))?;

let preopened_dirs = wasi::compute_preopen_dirs(
&wasi_params.preopened_dirs,
&wasi_params.map_dirs
).map_err(|e|
errors::Error::WasiInitCtxError(format!("Cannot compute preopened dirs: {:?}", e)))?;
let wasi_ctx = wasi::init_ctx(
&preopened_dirs,
&wasi_params.argv,
&wasi_params.env_vars,
).map_err(|e| errors::Error::WasiInitCtxError(e.to_string()))?;

Ok(WapcStore{
wasi_ctx,
host,
})
} else {
if wasi.is_some() {
// this check is required because otherwise the `wasi` parameter
// would not be used when the feature `wasi` is not enabled.
// That would cause a compilation error because we do not allow unused
// code.
Err(errors::Error::WasiDisabled);
} else {
Ok(WapcStore{
wasi_ctx,
host,
})
}
}
}
Ok(WapcStore { wasi_ctx, host })
}

#[cfg(not(feature = "wasi"))]
fn new(host: Option<Arc<ModuleState>>) -> WapcStore {
WapcStore { host }
}
}

Expand Down Expand Up @@ -221,7 +200,11 @@ impl WasmtimeEngineProviderPre {
pub(crate) fn rehydrate(&self) -> Result<WasmtimeEngineProvider> {
let engine = self.engine.clone();

#[cfg(feature = "wasi")]
let wapc_store = WapcStore::new(&self.wasi_params, None)?;
#[cfg(not(feature = "wasi"))]
let wapc_store = WapcStore::new(None);

let store = Store::new(&engine, wapc_store);

Ok(WasmtimeEngineProvider {
Expand Down Expand Up @@ -254,7 +237,11 @@ impl Clone for WasmtimeEngineProvider {
fn clone(&self) -> Self {
let engine = self.engine.clone();

#[cfg(feature = "wasi")]
let wapc_store = WapcStore::new(&self.wasi_params, None).unwrap();
#[cfg(not(feature = "wasi"))]
let wapc_store = WapcStore::new(None);

let store = Store::new(&engine, wapc_store);

match &self.inner {
Expand Down Expand Up @@ -339,7 +326,11 @@ impl WebAssemblyEngineProvider for WasmtimeEngineProvider {
host: Arc<ModuleState>,
) -> std::result::Result<(), Box<(dyn std::error::Error + Send + Sync + 'static)>> {
// create the proper store, now we have a value for `host`
#[cfg(feature = "wasi")]
let wapc_store = WapcStore::new(&self.wasi_params, Some(host.clone()))?;
#[cfg(not(feature = "wasi"))]
let wapc_store = WapcStore::new(Some(host.clone()));

self.store = Store::new(&self.engine, wapc_store);

let instance = self.instance_pre.instantiate(&mut self.store)?;
Expand Down

0 comments on commit fef792d

Please sign in to comment.