mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-09 14:18:20 +00:00
Move Webassembly objects to Store and remove Context
Co-authored-by: ptitSeb <sebastien.chev@gmail.com> Co-authored-by: Manos Pitsidianakis <manos@wasmer.io>
This commit is contained in:
committed by
Manos Pitsidianakis
parent
b5ae6399ce
commit
a419ccdf52
@@ -36,16 +36,6 @@ int main(int argc, const char* argv[]) {
|
||||
wasi_config_arg(config, js_string);
|
||||
wasi_config_capture_stdout(config);
|
||||
|
||||
wasi_env_t* wasi_env = wasi_env_new(config);
|
||||
if (!wasi_env) {
|
||||
printf("> Error building WASI env!\n");
|
||||
print_wasmer_error();
|
||||
return 1;
|
||||
}
|
||||
|
||||
wasm_context_t* ctx = wasm_context_new(store, wasi_env);
|
||||
wasm_store_context_set(store, ctx);
|
||||
|
||||
// Load binary.
|
||||
printf("Loading binary...\n");
|
||||
FILE* file = fopen("assets/qjs.wasm", "r");
|
||||
@@ -73,10 +63,27 @@ int main(int argc, const char* argv[]) {
|
||||
}
|
||||
|
||||
wasm_byte_vec_delete(&binary);
|
||||
|
||||
printf("Setting up WASI...\n");
|
||||
config = wasi_config_new("example_program");
|
||||
// TODO: error checking
|
||||
js_string = "function greet(name) { return JSON.stringify('Hello, ' + name); }; print(greet('World'));";
|
||||
wasi_config_arg(config, "--eval");
|
||||
wasi_config_arg(config, js_string);
|
||||
wasi_config_capture_stdout(config);
|
||||
|
||||
wasi_env_t* wasi_env = wasi_env_new(store, config);
|
||||
|
||||
if (!wasi_env) {
|
||||
printf("> Error building WASI env!\n");
|
||||
print_wasmer_error();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Instantiate.
|
||||
printf("Instantiating module...\n");
|
||||
wasm_extern_vec_t imports;
|
||||
bool get_imports_result = wasi_get_imports(store, module, &imports);
|
||||
bool get_imports_result = wasi_get_imports(store, wasi_env,module,&imports);
|
||||
|
||||
if (!get_imports_result) {
|
||||
printf("> Error getting WASI imports!\n");
|
||||
@@ -93,6 +100,12 @@ int main(int argc, const char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!wasi_env_initialize_instance(wasi_env, store, instance)) {
|
||||
printf("> Error initializing wasi env memory!\n");
|
||||
print_wasmer_error();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// Extract export.
|
||||
printf("Extracting export...\n");
|
||||
@@ -111,9 +124,6 @@ int main(int argc, const char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
wasm_module_delete(module);
|
||||
wasm_instance_delete(instance);
|
||||
|
||||
// Call.
|
||||
printf("Calling export...\n");
|
||||
printf("Evaluating \"%s\"\n", js_string);
|
||||
@@ -170,6 +180,8 @@ int main(int argc, const char* argv[]) {
|
||||
printf("Shutting down...\n");
|
||||
wasm_func_delete(run_func);
|
||||
wasi_env_delete(wasi_env);
|
||||
wasm_module_delete(module);
|
||||
wasm_instance_delete(instance);
|
||||
wasm_store_delete(store);
|
||||
wasm_engine_delete(engine);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user