Skip to content

Commit

Permalink
cli: initialize wasienv memory in create-exe
Browse files Browse the repository at this point in the history
  • Loading branch information
epilys committed Jul 21, 2022
1 parent 0f93232 commit f70c44b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
19 changes: 11 additions & 8 deletions lib/cli/src/commands/create_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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() {
Expand Down
18 changes: 9 additions & 9 deletions lib/cli/src/commands/wasmer_create_exe_main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

#include "wasmer.h"
//#include "my_wasm.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -94,10 +91,9 @@ int main(int argc, char *argv[]) {
wasm_config_t *config = wasm_config_new();
wasm_engine_t *engine = wasm_engine_new_with_config(config);
wasm_store_t *store = wasm_store_new(engine);
wasm_context_t* ctx = wasm_context_new(store, 0);
wasm_store_context_set(store, ctx);

wasm_module_t *module = wasmer_module_new(store, WASMER_MODULE_LENGTH, (const char*)&WASMER_MODULE_DATA);
wasm_module_t *module = wasmer_module_new(store, WASMER_MODULE_LENGTH,
(const char *)&WASMER_MODULE_DATA);

if (!module) {
fprintf(stderr, "Failed to create module\n");
Expand All @@ -112,7 +108,7 @@ int main(int argc, char *argv[]) {
wasi_config_t *wasi_config = wasi_config_new(argv[0]);
handle_arguments(wasi_config, argc, argv);

wasi_env_t *wasi_env = wasi_env_new(wasi_config);
wasi_env_t *wasi_env = wasi_env_new(store, wasi_config);
if (!wasi_env) {
fprintf(stderr, "Error building WASI env!\n");
print_wasmer_error();
Expand All @@ -128,8 +124,7 @@ int main(int argc, char *argv[]) {
wasm_importtype_vec_delete(&import_types);

#ifdef WASI
bool get_imports_result = wasi_get_imports(store, module, &imports);
wasi_env_delete(wasi_env);
bool get_imports_result = wasi_get_imports(store, wasi_env, module, &imports);

if (!get_imports_result) {
fprintf(stderr, "Error getting WASI imports!\n");
Expand All @@ -148,6 +143,8 @@ int main(int argc, char *argv[]) {
}

#ifdef WASI
wasi_env_initialize_instance(wasi_env, store, instance);

own wasm_func_t *start_function = wasi_get_start_function(instance);
if (!start_function) {
fprintf(stderr, "`_start` function not found\n");
Expand All @@ -166,6 +163,9 @@ int main(int argc, char *argv[]) {

// TODO: handle non-WASI start (maybe with invoke?)

#ifdef WASI
wasi_env_delete(wasi_env);
#endif
wasm_instance_delete(instance);
wasm_module_delete(module);
wasm_store_delete(store);
Expand Down

0 comments on commit f70c44b

Please sign in to comment.