Skip to content

Commit ddbe25c

Browse files
authored
Fix the compatibility issue with loading WATM-Go in WATER-rs (#37)
* call _start when init if there is one, specifically for WATM-go
1 parent 6ef3d79 commit ddbe25c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

crates/water/src/runtime/core.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::runtime::*;
99
#[derive(Default, Clone)]
1010
pub struct Host {
1111
pub preview1_ctx: Option<wasmtime_wasi::WasiCtx>,
12+
#[cfg(feature = "multithread")]
1213
pub wasi_threads: Option<Arc<WasiThreadsCtx<Host>>>,
1314
}
1415

@@ -37,10 +38,13 @@ impl H2O<Host> {
3738
}
3839

3940
let engine = Engine::new(&wasm_config)?;
40-
let linker: Linker<Host> = Linker::new(&engine);
4141

4242
let module = Module::from_file(&engine, &conf.filepath)?;
4343

44+
let linker: Linker<Host> = Linker::new(&engine);
45+
46+
// linker.allow_unknown_exports(true);
47+
4448
let host = Host::default();
4549
let store = Store::new(&engine, host);
4650

@@ -143,8 +147,26 @@ impl H2O<Host> {
143147
version_common::funcs::export_config(&mut linker, conf.config_wasm.clone())?;
144148
}
145149

150+
// linker.define_unknown_imports_as_traps(&module)?;
151+
146152
let instance = linker.instantiate(&mut store, &module)?;
147153

154+
// call _start function explicitly if there is one exported from the WATM module
155+
let func = instance.get_func(&mut store, "_start");
156+
157+
if let Some(func) = func {
158+
let mut res = vec![Val::null(); func.ty(&store).results().len()];
159+
match func.call(&mut store, &[], &mut res) {
160+
Ok(_) => {}
161+
Err(e) => {
162+
return Err(anyhow::Error::msg(format!(
163+
"failed to call _start function: {}",
164+
e
165+
)))
166+
}
167+
}
168+
}
169+
148170
Ok(H2O {
149171
version: match version {
150172
Some(v) => v,

crates/water/src/runtime/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use tracing::{debug, info};
3434
use wasi_common::{file::FileAccessMode, WasiCtx, WasiFile};
3535
use wasmtime::*;
3636
use wasmtime_wasi::sync::{Dir, WasiCtxBuilder};
37-
use wasmtime_wasi_threads::WasiThreadsCtx;
3837

3938
// =================== CURRENT CRATE IMPORTS ===================
4039
use crate::{

0 commit comments

Comments
 (0)