Skip to content

Commit

Permalink
Merge pull request #5042 from wasmerio/fix-import-capi
Browse files Browse the repository at this point in the history
Expose memory and wasi generic imports
  • Loading branch information
syrusakbary authored Aug 29, 2024
2 parents 158c8da + 880f29e commit b8e61d7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
26 changes: 24 additions & 2 deletions lib/c-api/src/wasm_c_api/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,32 @@ unsafe fn wasi_get_imports_inner(
) -> Option<()> {
let wasi_env = wasi_env?;
let store = &mut wasi_env.store;
let mut store_mut = store.store_mut();
let module = module?;

let import_object = c_try!(wasi_env.inner.import_object(&mut store_mut, &module.inner));
let mut import_object = c_try!(wasi_env
.inner
.import_object(&mut store.store_mut(), &module.inner));

let shared_memory = module.inner.imports().memories().next().map(|a| *a.ty());

let spawn_type = match shared_memory {
Some(ty) => wasmer_wasix::runtime::SpawnMemoryType::CreateMemoryOfType(ty),
None => wasmer_wasix::runtime::SpawnMemoryType::CreateMemory,
};

let tasks = wasi_env
.inner
.data(&store.store())
.runtime
.task_manager()
.clone();
let memory = tasks
.build_memory(&mut store.store_mut(), spawn_type)
.unwrap();

if let Some(memory) = memory {
import_object.define("env", "memory", memory);
}

imports_set_buffer(store, &module.inner, import_object, imports)?;

Expand Down
15 changes: 13 additions & 2 deletions lib/wasix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,25 @@ pub fn generate_import_object_from_env(
ctx: &FunctionEnv<WasiEnv>,
version: WasiVersion,
) -> Imports {
match version {
let mut imports = match version {
WasiVersion::Snapshot0 => generate_import_object_snapshot0(store, ctx),
WasiVersion::Snapshot1 | WasiVersion::Latest => {
generate_import_object_snapshot1(store, ctx)
}
WasiVersion::Wasix32v1 => generate_import_object_wasix32_v1(store, ctx),
WasiVersion::Wasix64v1 => generate_import_object_wasix64_v1(store, ctx),
}
};

let exports_wasi_generic = wasi_exports_generic(store, ctx);

#[allow(unused_mut)]
let mut imports_wasi_generic = imports! {
"wasi" => exports_wasi_generic,
};

imports.extend(&imports_wasi_generic);

imports
}

fn wasi_exports_generic(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>) -> Exports {
Expand Down

0 comments on commit b8e61d7

Please sign in to comment.