Use wasi_env_set_instance instead of wasi_env_set_memory

This commit is contained in:
Mark McCaskey
2020-08-12 17:32:28 -07:00
parent 892fb4efcf
commit cba35df3f1
3 changed files with 15 additions and 14 deletions

View File

@@ -4,7 +4,7 @@
mod capture_files;
use super::{wasm_extern_t, wasm_memory_t, wasm_module_t, wasm_store_t};
use super::{wasm_extern_t, wasm_instance_t, wasm_module_t, wasm_store_t};
// required due to really weird Rust resolution rules for macros
// https://github.com/rust-lang/rust/issues/57966
use crate::c_try;
@@ -115,8 +115,15 @@ pub extern "C" fn wasi_env_new(mut config: Box<wasi_config_t>) -> Option<Box<was
pub extern "C" fn wasi_env_delete(_state: Option<Box<wasi_env_t>>) {}
#[no_mangle]
pub extern "C" fn wasi_env_set_memory(env: &mut wasi_env_t, memory: &wasm_memory_t) {
env.inner.set_memory(memory.inner.clone());
pub extern "C" fn wasi_env_set_instance(env: &mut wasi_env_t, instance: &wasm_instance_t) -> bool {
let memory = if let Ok(memory) = instance.inner.exports.get_memory("memory") {
memory
} else {
return false;
};
env.inner.set_memory(memory.clone());
true
}
#[no_mangle]

View File

@@ -100,13 +100,7 @@ int main(int argc, const char* argv[]) {
}
fprintf(stderr, "found %zu exports\n", exports.size);
printf("Getting memory...\n");
const wasm_memory_t* memory = wasm_extern_as_memory(exports.data[0]);
if (! memory) {
printf("Could not get memory!\n");
return 1;
}
wasi_env_set_memory(wasi_env, memory);
wasi_env_set_instance(wasi_env, instance);
const wasm_func_t* run_func = wasm_extern_as_func(exports.data[1]);
if (run_func == NULL) {
printf("> Error accessing export!\n");

View File

@@ -64,13 +64,13 @@ void wasi_env_delete(own wasi_env_t*);
// Get an array of imports that can be used to instantiate the given module.
bool wasi_get_imports(wasm_store_t* store,
wasm_module_t* module,
const wasm_module_t* module,
wasi_env_t* wasi_env,
wasm_extern_t** imports);
// TODO: investigate removing this part of the API
// Set the memory in the `wasi_env_t` so that the WASI host functions can access WASI's memory.
void wasi_env_set_memory(wasi_env_t*, const wasm_memory_t*);
// Set up the `wasi_env_t` so that the WASI host functions can access WASI's memory.
// Returns whether or not it succeeded.
bool wasi_env_set_instance(wasi_env_t*, const wasm_instance_t*);
// Read from WASI's buffered stdout if stdout has not been inherited with
// `wasi_config_inherit_stdout`.