Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context capi ci errors #3001

Merged
merged 2 commits into from
Jul 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/api/src/sys/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ impl Global {
}
}

impl std::cmp::PartialEq for Global {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}

impl std::cmp::Eq for Global {}

impl<'a> Exportable<'a> for Global {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Expand Down
8 changes: 8 additions & 0 deletions lib/api/src/sys/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ impl Memory {
}
}

impl std::cmp::PartialEq for Memory {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}

impl std::cmp::Eq for Memory {}

impl<'a> Exportable<'a> for Memory {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Expand Down
8 changes: 8 additions & 0 deletions lib/api/src/sys/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ impl Table {
}
}

impl std::cmp::PartialEq for Table {
fn eq(&self, other: &Self) -> bool {
self.handle == other.handle
}
}

impl std::cmp::Eq for Table {}

impl<'a> Exportable<'a> for Table {
fn get_self_from_extern(_extern: &'a Extern) -> Result<&'a Self, ExportError> {
match _extern {
Expand Down
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
8 changes: 8 additions & 0 deletions lib/c-api/src/wasm_c_api/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ pub unsafe extern "C" fn wasm_global_set(global: &mut wasm_global_t, val: &wasm_
}
}

#[no_mangle]
pub unsafe extern "C" fn wasm_global_same(
wasm_global1: &wasm_global_t,
wasm_global2: &wasm_global_t,
) -> bool {
wasm_global1.inner == wasm_global2.inner
}

#[no_mangle]
pub extern "C" fn wasm_global_type(
global: Option<&wasm_global_t>,
Expand Down
8 changes: 8 additions & 0 deletions lib/c-api/src/wasm_c_api/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub unsafe extern "C" fn wasm_memory_copy(memory: &wasm_memory_t) -> Box<wasm_me
Box::new(wasm_memory_t::new((&*memory.inner).clone()))
}

#[no_mangle]
pub unsafe extern "C" fn wasm_memory_same(
wasm_memory1: &wasm_memory_t,
wasm_memory2: &wasm_memory_t,
) -> bool {
wasm_memory1.inner == wasm_memory2.inner
}

#[no_mangle]
pub unsafe extern "C" fn wasm_memory_type(
memory: Option<&wasm_memory_t>,
Expand Down
8 changes: 8 additions & 0 deletions lib/c-api/src/wasm_c_api/externals/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub unsafe extern "C" fn wasm_table_size(table: &wasm_table_t) -> usize {
table.inner.size(&ctx.inner) as _
}

#[no_mangle]
pub unsafe extern "C" fn wasm_table_same(
wasm_table1: &wasm_table_t,
wasm_table2: &wasm_table_t,
) -> bool {
wasm_table1.inner == wasm_table2.inner
}

#[no_mangle]
pub unsafe extern "C" fn wasm_table_grow(
_table: &mut wasm_table_t,
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
Loading