mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-09 22:28:21 +00:00
cli: initialize wasienv memory in create-exe
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
pub use super::unstable::wasi::wasi_get_unordered_imports;
|
||||
use super::{
|
||||
externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t},
|
||||
externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t, wasm_memory_t},
|
||||
instance::wasm_instance_t,
|
||||
module::wasm_module_t,
|
||||
store::{wasm_store_t, StoreRef},
|
||||
@@ -197,6 +197,14 @@ pub unsafe extern "C" fn wasi_env_new(
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasi_env_delete(_state: Option<Box<wasi_env_t>>) {}
|
||||
|
||||
/// Set the memory on a [`wasi_env_t`].
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasi_env_set_memory(env: &mut wasi_env_t, memory: &wasm_memory_t) {
|
||||
let mut store_mut = env.store.store_mut();
|
||||
let wasi_env = env.inner.data_mut(&mut store_mut);
|
||||
wasi_env.set_memory(memory.extern_.memory());
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasi_env_read_stdout(
|
||||
env: &mut wasi_env_t,
|
||||
|
||||
@@ -242,6 +242,14 @@ impl Default for LinkCode {
|
||||
|
||||
impl LinkCode {
|
||||
fn run(&self) -> anyhow::Result<()> {
|
||||
let libwasmer_path = self
|
||||
.libwasmer_path
|
||||
.canonicalize()
|
||||
.context("Failed to find libwasmer")?;
|
||||
println!(
|
||||
"Using path `{}` as libwasmer path.",
|
||||
libwasmer_path.display()
|
||||
);
|
||||
let mut command = Command::new(&self.linker_path);
|
||||
let command = command
|
||||
.arg(&self.optimization_flag)
|
||||
@@ -250,12 +258,7 @@ impl LinkCode {
|
||||
.iter()
|
||||
.map(|path| path.canonicalize().unwrap()),
|
||||
)
|
||||
.arg(
|
||||
&self
|
||||
.libwasmer_path
|
||||
.canonicalize()
|
||||
.context("Failed to find libwasmer")?,
|
||||
);
|
||||
.arg(&libwasmer_path);
|
||||
let command = if let Some(target) = &self.target {
|
||||
command.arg("-target").arg(format!("{}", target))
|
||||
} else {
|
||||
@@ -272,11 +275,11 @@ impl LinkCode {
|
||||
// On unix we need dlopen-related symbols, libmath for a few things, and pthreads.
|
||||
#[cfg(not(windows))]
|
||||
let command = command.arg("-ldl").arg("-lm").arg("-pthread");
|
||||
let link_aganist_extra_libs = self
|
||||
let link_against_extra_libs = self
|
||||
.additional_libraries
|
||||
.iter()
|
||||
.map(|lib| format!("-l{}", lib));
|
||||
let command = command.args(link_aganist_extra_libs);
|
||||
let command = command.args(link_against_extra_libs);
|
||||
let output = command.arg("-o").arg(&self.output_path).output()?;
|
||||
|
||||
if !output.status.success() {
|
||||
|
||||
@@ -149,6 +149,24 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
#ifdef WASI
|
||||
// Read the exports.
|
||||
wasm_extern_vec_t exports;
|
||||
wasm_instance_exports(instance, &exports);
|
||||
wasm_memory_t* mem = NULL;
|
||||
for (size_t i = 0; i < exports.size; i++) {
|
||||
mem = wasm_extern_as_memory(exports.data[i]);
|
||||
if (mem) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!mem) {
|
||||
fprintf(stderr, "Failed to create instance: Could not find memory in exports\n");
|
||||
print_wasmer_error();
|
||||
return -1;
|
||||
}
|
||||
wasi_env_set_memory(wasi_env, mem);
|
||||
|
||||
own wasm_func_t *start_function = wasi_get_start_function(instance);
|
||||
if (!start_function) {
|
||||
fprintf(stderr, "`_start` function not found\n");
|
||||
@@ -163,11 +181,14 @@ int main(int argc, char *argv[]) {
|
||||
fprintf(stderr, "Trap is not NULL: TODO:\n");
|
||||
return -1;
|
||||
}
|
||||
wasi_env_delete(wasi_env);
|
||||
#endif
|
||||
|
||||
// TODO: handle non-WASI start (maybe with invoke?)
|
||||
|
||||
#ifdef WASI
|
||||
wasi_env_delete(wasi_env);
|
||||
wasm_extern_vec_delete(&exports);
|
||||
#endif
|
||||
wasm_instance_delete(instance);
|
||||
wasm_module_delete(module);
|
||||
wasm_store_delete(store);
|
||||
|
||||
Reference in New Issue
Block a user