Skip to content

Commit

Permalink
Update c-api examples to Context API
Browse files Browse the repository at this point in the history
  • Loading branch information
epilys committed Jul 19, 2022
1 parent 62f07ef commit eed0327
Show file tree
Hide file tree
Showing 27 changed files with 132 additions and 56 deletions.
2 changes: 2 additions & 0 deletions lib/c-api/examples/deprecated-header.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ int main(int argc, const char *argv[]) {
printf("Initializing...\n");
own wasm_engine_t* engine = wasm_engine_new();
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 = {
Expand Down
4 changes: 3 additions & 1 deletion lib/c-api/examples/early-exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void print_frame(wasm_frame_t* frame) {

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;
wasm_name_new_from_string_nt(&trap_message, "trapping from a host import");
own wasm_trap_t *trap = wasm_trap_new(store, &trap_message);
Expand All @@ -42,6 +42,8 @@ int main(int argc, const char *argv[]) {
printf("Initializing...\n");
wasm_engine_t *engine = wasm_engine_new();
store = wasm_store_new(engine);
wasm_context_t* ctx = wasm_context_new(store, 0);
wasm_store_context_set(store, ctx);

// Load binary.
printf("Loading binary...\n");
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/examples/exports-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ int main(int argc, const char* argv[]) {
printf("Creating the store...\n");
wasm_engine_t* engine = wasm_engine_new();
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");
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/examples/exports-global.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ int main(int argc, const char* argv[]) {
printf("Creating the store...\n");
wasm_engine_t* engine = wasm_engine_new();
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");
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/examples/features.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ int main(int argc, const char* argv[]) {
printf("Creating the store...\n");
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);

printf("Compiling module...\n");
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
Expand Down
4 changes: 3 additions & 1 deletion lib/c-api/examples/imports-exports.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdio.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> ");

wasm_val_t val = WASM_I32_VAL(42);
Expand Down Expand Up @@ -31,6 +31,8 @@ int main(int argc, const char* argv[]) {
printf("Creating the store...\n");
wasm_engine_t* engine = wasm_engine_new();
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");
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/examples/instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ int main(int argc, const char* argv[]) {
printf("Creating the store...\n");
wasm_engine_t* engine = wasm_engine_new();
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");
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/examples/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ int main(int argc, const char* argv[]) {
printf("Creating the store...\n");
wasm_engine_t* engine = wasm_engine_new();
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");
wasm_module_t* module = wasm_module_new(store, &wasm_bytes);
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/examples/memory2.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ int main(int argc, const char *argv[]) {
printf("Initializing...\n");
own wasm_engine_t* engine = wasm_engine_new();
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 = {
Expand Down
36 changes: 19 additions & 17 deletions lib/c-api/examples/wasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ int main(int argc, const char* argv[]) {
wasm_engine_t* engine = wasm_engine_new();
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.
printf("Loading binary...\n");
FILE* file = fopen("assets/qjs.wasm", "r");
Expand Down Expand Up @@ -55,26 +73,10 @@ int main(int argc, const char* argv[]) {
}

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.
printf("Instantiating module...\n");
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) {
printf("> Error getting WASI imports!\n");
Expand Down
35 changes: 34 additions & 1 deletion lib/c-api/src/wasm_c_api/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::wasm_c_api::store::wasm_store_t;
use libc::c_void;
use wasmer_api::Context;
use wasmer_api::{Context, ContextMut};

/// Opaque type representing a WebAssembly context.
#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -38,3 +38,36 @@ pub unsafe extern "C" fn wasm_context_new(
/// See the module's documentation.
#[no_mangle]
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 changes: 8 additions & 13 deletions lib/c-api/src/wasm_c_api/externals/function.rs
Original file line number Diff line number Diff line change
@@ -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::trap::wasm_trap_t;
use super::super::types::{wasm_functype_t, wasm_valkind_enum};
Expand Down Expand Up @@ -32,20 +32,11 @@ impl wasm_func_t {

#[allow(non_camel_case_types)]
pub type wasm_func_callback_t = unsafe extern "C" fn(
context: &mut wasm_context_ref_mut_t,
args: &wasm_val_vec_t,
results: &mut wasm_val_vec_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]
pub unsafe extern "C" fn wasm_func_new(
store: Option<&wasm_store_t>,
Expand All @@ -62,7 +53,7 @@ pub unsafe extern "C" fn wasm_func_new(

let func_sig = &function_type.inner().function_type;
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]|
-> Result<Vec<Value>, RuntimeError> {
let processed_args: wasm_val_vec_t = args
Expand All @@ -81,7 +72,11 @@ pub unsafe extern "C" fn wasm_func_new(
]
.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 {
return Err(trap.inner);
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/src/wasm_c_api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,11 @@ mod tests {

// The `sum` host function implementation.
wasm_trap_t* sum_callback(
wasm_context_ref_mut_t* ctx_mut,
const wasm_val_vec_t* arguments,
wasm_val_vec_t* results
) {
(void) ctx_mut;
wasm_val_t sum = {
.kind = WASM_I32,
.of = { arguments->data[0].of.i32 + arguments->data[1].of.i32 },
Expand Down
24 changes: 11 additions & 13 deletions lib/c-api/tests/wasm-c-api/example/callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#define own

wasm_store_t* global_store = NULL;

// Print a Wasm value
void wasm_val_print(wasm_val_t val) {
switch (val.kind) {
Expand Down Expand Up @@ -37,7 +35,7 @@ void wasm_val_print(wasm_val_t val) {

// A function to be called from Wasm code.
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> ");
wasm_val_print(args->data[0]);
Expand All @@ -50,9 +48,9 @@ own wasm_trap_t* print_callback(

// A function closure.
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("> %d\n", i);

Expand All @@ -66,10 +64,10 @@ int main(int argc, const char* argv[]) {
// Initialize.
printf("Initializing...\n");
wasm_engine_t* engine = wasm_engine_new();
global_store = wasm_store_new(engine);
wasm_store_t* store = wasm_store_new(engine);
int i = 42;
wasm_context_t* ctx = wasm_context_new(global_store, &i);
wasm_store_context_set(global_store, ctx);
wasm_context_t* ctx = wasm_context_new(store, &i);
wasm_store_context_set(store, ctx);

// Load binary.
printf("Loading binary...\n");
Expand All @@ -91,7 +89,7 @@ int main(int argc, const char* argv[]) {

// Compile.
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) {
printf("> Error compiling module!\n");
return 1;
Expand All @@ -102,10 +100,10 @@ int main(int argc, const char* argv[]) {
// Create external print functions.
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_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_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(closure_type);
Expand All @@ -117,7 +115,7 @@ int main(int argc, const char* argv[]) {
};
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
own wasm_instance_t* instance =
wasm_instance_new(global_store, module, &imports, NULL);
wasm_instance_new(store, module, &imports, NULL);
if (!instance) {
printf("> Error instantiating module!\n");
return 1;
Expand Down Expand Up @@ -162,7 +160,7 @@ int main(int argc, const char* argv[]) {

// Shut down.
printf("Shutting down...\n");
wasm_store_delete(global_store);
wasm_store_delete(store);
wasm_engine_delete(engine);

// All done.
Expand Down
4 changes: 4 additions & 0 deletions lib/c-api/tests/wasm-c-api/example/finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ int main(int argc, const char* argv[]) {
printf("Live count %d\n", live_count);
printf("Creating store 1...\n");
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");
run_in_store(store1);
printf("Live count %d\n", live_count);

printf("Creating store 2...\n");
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");
run_in_store(store2);
Expand Down
2 changes: 2 additions & 0 deletions lib/c-api/tests/wasm-c-api/example/global.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ int main(int argc, const char* argv[]) {
printf("Initializing...\n");
wasm_engine_t* engine = wasm_engine_new();
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.
printf("Loading binary...\n");
Expand Down
4 changes: 3 additions & 1 deletion lib/c-api/tests/wasm-c-api/example/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// A function to be called from Wasm code.
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("> Hello World!\n");
Expand All @@ -22,6 +22,8 @@ int main(int argc, const char* argv[]) {
printf("Initializing...\n");
wasm_engine_t* engine = wasm_engine_new();
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.
printf("Loading binary...\n");
Expand Down
Loading

0 comments on commit eed0327

Please sign in to comment.