mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-18 06:19:12 +00:00
Update c-api examples to Context API
This commit is contained in:
@@ -21,6 +21,8 @@ int main(int argc, const char *argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
own wasm_engine_t* engine = wasm_engine_new();
|
own wasm_engine_t* engine = wasm_engine_new();
|
||||||
own wasm_store_t* store = wasm_store_new(engine);
|
own wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// =====================
|
// =====================
|
||||||
wasm_limits_t limits1 = {
|
wasm_limits_t limits1 = {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ void print_frame(wasm_frame_t* frame) {
|
|||||||
|
|
||||||
wasm_store_t *store = NULL;
|
wasm_store_t *store = NULL;
|
||||||
|
|
||||||
own wasm_trap_t* early_exit(const wasm_val_vec_t* args, wasm_val_vec_t* results) {
|
own wasm_trap_t* early_exit(wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results) {
|
||||||
own wasm_message_t trap_message;
|
own wasm_message_t trap_message;
|
||||||
wasm_name_new_from_string_nt(&trap_message, "trapping from a host import");
|
wasm_name_new_from_string_nt(&trap_message, "trapping from a host import");
|
||||||
own wasm_trap_t *trap = wasm_trap_new(store, &trap_message);
|
own wasm_trap_t *trap = wasm_trap_new(store, &trap_message);
|
||||||
@@ -42,6 +42,8 @@ int main(int argc, const char *argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t *engine = wasm_engine_new();
|
wasm_engine_t *engine = wasm_engine_new();
|
||||||
store = wasm_store_new(engine);
|
store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Creating the store...\n");
|
printf("Creating the store...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
printf("Compiling module...\n");
|
printf("Compiling module...\n");
|
||||||
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Creating the store...\n");
|
printf("Creating the store...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
printf("Compiling module...\n");
|
printf("Compiling module...\n");
|
||||||
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Creating the store...\n");
|
printf("Creating the store...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new_with_config(config);
|
wasm_engine_t* engine = wasm_engine_new_with_config(config);
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
printf("Compiling module...\n");
|
printf("Compiling module...\n");
|
||||||
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "wasmer.h"
|
#include "wasmer.h"
|
||||||
|
|
||||||
wasm_trap_t* host_func_callback(const wasm_val_vec_t* args, wasm_val_vec_t* results) {
|
wasm_trap_t* host_func_callback(wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results) {
|
||||||
printf("Calling back...\n> ");
|
printf("Calling back...\n> ");
|
||||||
|
|
||||||
wasm_val_t val = WASM_I32_VAL(42);
|
wasm_val_t val = WASM_I32_VAL(42);
|
||||||
@@ -31,6 +31,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Creating the store...\n");
|
printf("Creating the store...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
printf("Compiling module...\n");
|
printf("Compiling module...\n");
|
||||||
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Creating the store...\n");
|
printf("Creating the store...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
printf("Compiling module...\n");
|
printf("Compiling module...\n");
|
||||||
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Creating the store...\n");
|
printf("Creating the store...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
printf("Compiling module...\n");
|
printf("Compiling module...\n");
|
||||||
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ int main(int argc, const char *argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
own wasm_engine_t* engine = wasm_engine_new();
|
own wasm_engine_t* engine = wasm_engine_new();
|
||||||
own wasm_store_t* store = wasm_store_new(engine);
|
own wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// =====================
|
// =====================
|
||||||
wasm_limits_t limits1 = {
|
wasm_limits_t limits1 = {
|
||||||
|
|||||||
@@ -28,6 +28,24 @@ int main(int argc, const char* argv[]) {
|
|||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
|
||||||
|
printf("Setting up WASI...\n");
|
||||||
|
wasi_config_t* config = wasi_config_new("example_program");
|
||||||
|
// TODO: error checking
|
||||||
|
const char* 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(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.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
FILE* file = fopen("assets/qjs.wasm", "r");
|
FILE* file = fopen("assets/qjs.wasm", "r");
|
||||||
@@ -55,26 +73,10 @@ int main(int argc, const char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wasm_byte_vec_delete(&binary);
|
wasm_byte_vec_delete(&binary);
|
||||||
|
|
||||||
printf("Setting up WASI...\n");
|
|
||||||
wasi_config_t* config = wasi_config_new("example_program");
|
|
||||||
// TODO: error checking
|
|
||||||
const char* 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(config);
|
|
||||||
if (!wasi_env) {
|
|
||||||
printf("> Error building WASI env!\n");
|
|
||||||
print_wasmer_error();
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instantiate.
|
// Instantiate.
|
||||||
printf("Instantiating module...\n");
|
printf("Instantiating module...\n");
|
||||||
wasm_extern_vec_t imports;
|
wasm_extern_vec_t imports;
|
||||||
bool get_imports_result = wasi_get_imports(store, module, wasi_env, &imports);
|
bool get_imports_result = wasi_get_imports(store, module, &imports);
|
||||||
|
|
||||||
if (!get_imports_result) {
|
if (!get_imports_result) {
|
||||||
printf("> Error getting WASI imports!\n");
|
printf("> Error getting WASI imports!\n");
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::wasm_c_api::store::wasm_store_t;
|
use crate::wasm_c_api::store::wasm_store_t;
|
||||||
use libc::c_void;
|
use libc::c_void;
|
||||||
use wasmer_api::Context;
|
use wasmer_api::{Context, ContextMut};
|
||||||
|
|
||||||
/// Opaque type representing a WebAssembly context.
|
/// Opaque type representing a WebAssembly context.
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
@@ -38,3 +38,36 @@ pub unsafe extern "C" fn wasm_context_new(
|
|||||||
/// See the module's documentation.
|
/// See the module's documentation.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn wasm_context_delete(_context: Option<Box<wasm_context_t>>) {}
|
pub unsafe extern "C" fn wasm_context_delete(_context: Option<Box<wasm_context_t>>) {}
|
||||||
|
|
||||||
|
/// Opaque type representing a mut ref of a WebAssembly context.
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
pub struct wasm_context_ref_mut_t<'a> {
|
||||||
|
pub(crate) inner: ContextMut<'a, *mut c_void>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the value of `wasm_context_ref_mut_t` data.
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn wasm_context_ref_mut_get(ctx: &wasm_context_ref_mut_t) -> *mut c_void {
|
||||||
|
*ctx.inner.data()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the value of [`ContextMut`] data.
|
||||||
|
///
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn wasm_context_ref_mut_set(
|
||||||
|
ctx: &mut wasm_context_ref_mut_t,
|
||||||
|
new_val: *mut c_void,
|
||||||
|
) {
|
||||||
|
*ctx.inner.data_mut() = new_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deletes a WebAssembly context.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// See the module's documentation.
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn wasm_context_ref_mut_delete(
|
||||||
|
_context: Option<&mut wasm_context_ref_mut_t>,
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|||||||
21
lib/c-api/src/wasm_c_api/externals/function.rs
vendored
21
lib/c-api/src/wasm_c_api/externals/function.rs
vendored
@@ -1,4 +1,4 @@
|
|||||||
use super::super::context::wasm_context_t;
|
use super::super::context::{wasm_context_ref_mut_t, wasm_context_t};
|
||||||
use super::super::store::wasm_store_t;
|
use super::super::store::wasm_store_t;
|
||||||
use super::super::trap::wasm_trap_t;
|
use super::super::trap::wasm_trap_t;
|
||||||
use super::super::types::{wasm_functype_t, wasm_valkind_enum};
|
use super::super::types::{wasm_functype_t, wasm_valkind_enum};
|
||||||
@@ -32,20 +32,11 @@ impl wasm_func_t {
|
|||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub type wasm_func_callback_t = unsafe extern "C" fn(
|
pub type wasm_func_callback_t = unsafe extern "C" fn(
|
||||||
|
context: &mut wasm_context_ref_mut_t,
|
||||||
args: &wasm_val_vec_t,
|
args: &wasm_val_vec_t,
|
||||||
results: &mut wasm_val_vec_t,
|
results: &mut wasm_val_vec_t,
|
||||||
) -> Option<Box<wasm_trap_t>>;
|
) -> Option<Box<wasm_trap_t>>;
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub type wasm_func_callback_with_env_t = unsafe extern "C" fn(
|
|
||||||
env: *mut c_void,
|
|
||||||
args: &wasm_val_vec_t,
|
|
||||||
results: &mut wasm_val_vec_t,
|
|
||||||
) -> Option<Box<wasm_trap_t>>;
|
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub type wasm_env_finalizer_t = unsafe extern "C" fn(*mut c_void);
|
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn wasm_func_new(
|
pub unsafe extern "C" fn wasm_func_new(
|
||||||
store: Option<&wasm_store_t>,
|
store: Option<&wasm_store_t>,
|
||||||
@@ -62,7 +53,7 @@ pub unsafe extern "C" fn wasm_func_new(
|
|||||||
|
|
||||||
let func_sig = &function_type.inner().function_type;
|
let func_sig = &function_type.inner().function_type;
|
||||||
let num_rets = func_sig.results().len();
|
let num_rets = func_sig.results().len();
|
||||||
let inner_callback = move |_ctx: wasmer_api::ContextMut<'_, *mut c_void>,
|
let inner_callback = move |ctx: wasmer_api::ContextMut<'_, *mut c_void>,
|
||||||
args: &[Value]|
|
args: &[Value]|
|
||||||
-> Result<Vec<Value>, RuntimeError> {
|
-> Result<Vec<Value>, RuntimeError> {
|
||||||
let processed_args: wasm_val_vec_t = args
|
let processed_args: wasm_val_vec_t = args
|
||||||
@@ -81,7 +72,11 @@ pub unsafe extern "C" fn wasm_func_new(
|
|||||||
]
|
]
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
let trap = callback(&processed_args, &mut results);
|
let trap = callback(
|
||||||
|
&mut wasm_context_ref_mut_t { inner: ctx },
|
||||||
|
&processed_args,
|
||||||
|
&mut results,
|
||||||
|
);
|
||||||
|
|
||||||
if let Some(trap) = trap {
|
if let Some(trap) = trap {
|
||||||
return Err(trap.inner);
|
return Err(trap.inner);
|
||||||
|
|||||||
@@ -224,9 +224,11 @@ mod tests {
|
|||||||
|
|
||||||
// The `sum` host function implementation.
|
// The `sum` host function implementation.
|
||||||
wasm_trap_t* sum_callback(
|
wasm_trap_t* sum_callback(
|
||||||
|
wasm_context_ref_mut_t* ctx_mut,
|
||||||
const wasm_val_vec_t* arguments,
|
const wasm_val_vec_t* arguments,
|
||||||
wasm_val_vec_t* results
|
wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
|
(void) ctx_mut;
|
||||||
wasm_val_t sum = {
|
wasm_val_t sum = {
|
||||||
.kind = WASM_I32,
|
.kind = WASM_I32,
|
||||||
.of = { arguments->data[0].of.i32 + arguments->data[1].of.i32 },
|
.of = { arguments->data[0].of.i32 + arguments->data[1].of.i32 },
|
||||||
|
|||||||
@@ -7,8 +7,6 @@
|
|||||||
|
|
||||||
#define own
|
#define own
|
||||||
|
|
||||||
wasm_store_t* global_store = NULL;
|
|
||||||
|
|
||||||
// Print a Wasm value
|
// Print a Wasm value
|
||||||
void wasm_val_print(wasm_val_t val) {
|
void wasm_val_print(wasm_val_t val) {
|
||||||
switch (val.kind) {
|
switch (val.kind) {
|
||||||
@@ -37,7 +35,7 @@ void wasm_val_print(wasm_val_t val) {
|
|||||||
|
|
||||||
// A function to be called from Wasm code.
|
// A function to be called from Wasm code.
|
||||||
own wasm_trap_t* print_callback(
|
own wasm_trap_t* print_callback(
|
||||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
printf("Calling back...\n> ");
|
printf("Calling back...\n> ");
|
||||||
wasm_val_print(args->data[0]);
|
wasm_val_print(args->data[0]);
|
||||||
@@ -50,9 +48,9 @@ own wasm_trap_t* print_callback(
|
|||||||
|
|
||||||
// A function closure.
|
// A function closure.
|
||||||
own wasm_trap_t* closure_callback(
|
own wasm_trap_t* closure_callback(
|
||||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
int i = *(int*) wasm_store_data_get(global_store);
|
int i = *(int*) wasm_context_ref_mut_get(ctx_mut);
|
||||||
printf("Calling back closure...\n");
|
printf("Calling back closure...\n");
|
||||||
printf("> %d\n", i);
|
printf("> %d\n", i);
|
||||||
|
|
||||||
@@ -66,10 +64,10 @@ int main(int argc, const char* argv[]) {
|
|||||||
// Initialize.
|
// Initialize.
|
||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
global_store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
int i = 42;
|
int i = 42;
|
||||||
wasm_context_t* ctx = wasm_context_new(global_store, &i);
|
wasm_context_t* ctx = wasm_context_new(store, &i);
|
||||||
wasm_store_context_set(global_store, ctx);
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
@@ -91,7 +89,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
|
|
||||||
// Compile.
|
// Compile.
|
||||||
printf("Compiling module...\n");
|
printf("Compiling module...\n");
|
||||||
own wasm_module_t* module = wasm_module_new(global_store, &binary);
|
own wasm_module_t* module = wasm_module_new(store, &binary);
|
||||||
if (!module) {
|
if (!module) {
|
||||||
printf("> Error compiling module!\n");
|
printf("> Error compiling module!\n");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -102,10 +100,10 @@ int main(int argc, const char* argv[]) {
|
|||||||
// Create external print functions.
|
// Create external print functions.
|
||||||
printf("Creating callback...\n");
|
printf("Creating callback...\n");
|
||||||
own wasm_functype_t* print_type = wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32());
|
own wasm_functype_t* print_type = wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32());
|
||||||
own wasm_func_t* print_func = wasm_func_new(global_store, print_type, print_callback);
|
own wasm_func_t* print_func = wasm_func_new(store, print_type, print_callback);
|
||||||
|
|
||||||
own wasm_functype_t* closure_type = wasm_functype_new_0_1(wasm_valtype_new_i32());
|
own wasm_functype_t* closure_type = wasm_functype_new_0_1(wasm_valtype_new_i32());
|
||||||
own wasm_func_t* closure_func = wasm_func_new(global_store, closure_type, closure_callback);
|
own wasm_func_t* closure_func = wasm_func_new(store, closure_type, closure_callback);
|
||||||
|
|
||||||
wasm_functype_delete(print_type);
|
wasm_functype_delete(print_type);
|
||||||
wasm_functype_delete(closure_type);
|
wasm_functype_delete(closure_type);
|
||||||
@@ -117,7 +115,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
};
|
};
|
||||||
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
|
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
|
||||||
own wasm_instance_t* instance =
|
own wasm_instance_t* instance =
|
||||||
wasm_instance_new(global_store, module, &imports, NULL);
|
wasm_instance_new(store, module, &imports, NULL);
|
||||||
if (!instance) {
|
if (!instance) {
|
||||||
printf("> Error instantiating module!\n");
|
printf("> Error instantiating module!\n");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -162,7 +160,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
|
|
||||||
// Shut down.
|
// Shut down.
|
||||||
printf("Shutting down...\n");
|
printf("Shutting down...\n");
|
||||||
wasm_store_delete(global_store);
|
wasm_store_delete(store);
|
||||||
wasm_engine_delete(engine);
|
wasm_engine_delete(engine);
|
||||||
|
|
||||||
// All done.
|
// All done.
|
||||||
|
|||||||
@@ -74,6 +74,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Live count %d\n", live_count);
|
printf("Live count %d\n", live_count);
|
||||||
printf("Creating store 1...\n");
|
printf("Creating store 1...\n");
|
||||||
wasm_store_t* store1 = wasm_store_new(engine);
|
wasm_store_t* store1 = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx1 = wasm_context_new(store1, 0);
|
||||||
|
wasm_store_context_set(store1, ctx1);
|
||||||
|
|
||||||
printf("Running in store 1...\n");
|
printf("Running in store 1...\n");
|
||||||
run_in_store(store1);
|
run_in_store(store1);
|
||||||
@@ -81,6 +83,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
|
|
||||||
printf("Creating store 2...\n");
|
printf("Creating store 2...\n");
|
||||||
wasm_store_t* store2 = wasm_store_new(engine);
|
wasm_store_t* store2 = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx2 = wasm_context_new(store2, 0);
|
||||||
|
wasm_store_context_set(store2, ctx2);
|
||||||
|
|
||||||
printf("Running in store 2...\n");
|
printf("Running in store 2...\n");
|
||||||
run_in_store(store2);
|
run_in_store(store2);
|
||||||
|
|||||||
@@ -52,6 +52,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
// A function to be called from Wasm code.
|
// A function to be called from Wasm code.
|
||||||
own wasm_trap_t* hello_callback(
|
own wasm_trap_t* hello_callback(
|
||||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
printf("Calling back...\n");
|
printf("Calling back...\n");
|
||||||
printf("> Hello World!\n");
|
printf("> Hello World!\n");
|
||||||
@@ -22,6 +22,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
// A function to be called from Wasm code.
|
// A function to be called from Wasm code.
|
||||||
own wasm_trap_t* callback(
|
own wasm_trap_t* callback(
|
||||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
printf("Calling back...\n> ");
|
printf("Calling back...\n> ");
|
||||||
printf("> %p\n",
|
printf("> %p\n",
|
||||||
@@ -127,6 +127,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -98,6 +98,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
// A function to be called from Wasm code.
|
// A function to be called from Wasm code.
|
||||||
own wasm_trap_t* callback(
|
own wasm_trap_t* callback(
|
||||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
printf("Calling back...\n> ");
|
printf("Calling back...\n> ");
|
||||||
printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
|
printf("> %"PRIu32" %"PRIu64" %"PRIu64" %"PRIu32"\n",
|
||||||
@@ -44,6 +44,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -87,6 +87,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
// A function to be called from Wasm code.
|
// A function to be called from Wasm code.
|
||||||
own wasm_trap_t* hello_callback(
|
own wasm_trap_t* hello_callback(
|
||||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
printf("Calling back...\n");
|
printf("Calling back...\n");
|
||||||
printf("> Hello World!\n");
|
printf("> Hello World!\n");
|
||||||
@@ -22,6 +22,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
// A function to be called from Wasm code.
|
// A function to be called from Wasm code.
|
||||||
own wasm_trap_t* neg_callback(
|
own wasm_trap_t* neg_callback(
|
||||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
wasm_context_ref_mut_t* ctx_mut, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
printf("Calling back...\n");
|
printf("Calling back...\n");
|
||||||
results->data[0].kind = WASM_I32;
|
results->data[0].kind = WASM_I32;
|
||||||
@@ -78,6 +78,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
wasm_store_t* store = wasm_store_new(engine);
|
wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ const int N_THREADS = 10;
|
|||||||
const int N_REPS = 3;
|
const int N_REPS = 3;
|
||||||
|
|
||||||
// A function to be called from Wasm code.
|
// A function to be called from Wasm code.
|
||||||
own wasm_trap_t* callback(const wasm_val_vec_t* args, wasm_val_vec_t* results) {
|
own wasm_trap_t* callback(wasm_context_ref_mut_t* ctx, const wasm_val_vec_t* args, wasm_val_vec_t* results) {
|
||||||
assert(args->data[0].kind == WASM_I32);
|
assert(args->data[0].kind == WASM_I32);
|
||||||
printf("> Thread %d running\n", args->data[0].of.i32);
|
printf("> Thread %d running\n", args->data[0].of.i32);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -31,6 +31,8 @@ void* run(void* args_abs) {
|
|||||||
|
|
||||||
// Rereate store and module.
|
// Rereate store and module.
|
||||||
own wasm_store_t* store = wasm_store_new(args->engine);
|
own wasm_store_t* store = wasm_store_new(args->engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
own wasm_module_t* module = wasm_module_obtain(store, args->module);
|
own wasm_module_t* module = wasm_module_obtain(store, args->module);
|
||||||
|
|
||||||
// Run the example N times.
|
// Run the example N times.
|
||||||
@@ -119,6 +121,9 @@ int main(int argc, const char *argv[]) {
|
|||||||
|
|
||||||
// Compile and share.
|
// Compile and share.
|
||||||
own wasm_store_t* store = wasm_store_new(engine);
|
own wasm_store_t* store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
own wasm_module_t* module = wasm_module_new(store, &binary);
|
own wasm_module_t* module = wasm_module_new(store, &binary);
|
||||||
if (!module) {
|
if (!module) {
|
||||||
printf("> Error compiling module!\n");
|
printf("> Error compiling module!\n");
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ wasm_store_t* store = NULL;
|
|||||||
|
|
||||||
// A function to be called from Wasm code.
|
// A function to be called from Wasm code.
|
||||||
own wasm_trap_t* fail_callback(
|
own wasm_trap_t* fail_callback(
|
||||||
const wasm_val_vec_t* args, wasm_val_vec_t* results
|
wasm_context_ref_mut_t* ctx, const wasm_val_vec_t* args, wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
printf("Calling back...\n");
|
printf("Calling back...\n");
|
||||||
own wasm_name_t message;
|
own wasm_name_t message;
|
||||||
@@ -37,6 +37,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
printf("Initializing...\n");
|
printf("Initializing...\n");
|
||||||
wasm_engine_t* engine = wasm_engine_new();
|
wasm_engine_t* engine = wasm_engine_new();
|
||||||
store = wasm_store_new(engine);
|
store = wasm_store_new(engine);
|
||||||
|
wasm_context_t* ctx = wasm_context_new(store, 0);
|
||||||
|
wasm_store_context_set(store, ctx);
|
||||||
|
|
||||||
// Load binary.
|
// Load binary.
|
||||||
printf("Loading binary...\n");
|
printf("Loading binary...\n");
|
||||||
|
|||||||
@@ -146,6 +146,7 @@ WASM_API_EXTERN own wasm_store_t* wasm_store_new(wasm_engine_t*);
|
|||||||
// Context
|
// Context
|
||||||
|
|
||||||
WASM_DECLARE_OWN(context)
|
WASM_DECLARE_OWN(context)
|
||||||
|
WASM_DECLARE_OWN(context_ref_mut)
|
||||||
|
|
||||||
WASM_API_EXTERN own void wasm_store_context_set(own wasm_store_t*, own wasm_context_t*);
|
WASM_API_EXTERN own void wasm_store_context_set(own wasm_store_t*, own wasm_context_t*);
|
||||||
WASM_API_EXTERN own void wasm_store_data_set(own wasm_store_t*, own void*);
|
WASM_API_EXTERN own void wasm_store_data_set(own wasm_store_t*, own void*);
|
||||||
@@ -421,9 +422,7 @@ WASM_API_EXTERN own wasm_module_t* wasm_module_deserialize(wasm_store_t*, const
|
|||||||
WASM_DECLARE_REF(func)
|
WASM_DECLARE_REF(func)
|
||||||
|
|
||||||
typedef own wasm_trap_t* (*wasm_func_callback_t)(
|
typedef own wasm_trap_t* (*wasm_func_callback_t)(
|
||||||
const wasm_val_vec_t* args, own wasm_val_vec_t* results);
|
wasm_context_ref_mut_t*, const wasm_val_vec_t* args, own wasm_val_vec_t* results);
|
||||||
typedef own wasm_trap_t* (*wasm_func_callback_with_env_t)(
|
|
||||||
void* env, const wasm_val_vec_t* args, wasm_val_vec_t* results);
|
|
||||||
|
|
||||||
WASM_API_EXTERN own wasm_func_t* wasm_func_new(
|
WASM_API_EXTERN own wasm_func_t* wasm_func_new(
|
||||||
wasm_store_t*, const wasm_functype_t*, wasm_func_callback_t);
|
wasm_store_t*, const wasm_functype_t*, wasm_func_callback_t);
|
||||||
|
|||||||
Reference in New Issue
Block a user