261: feat(runtime-c-api) Add an API to update `vm::Ctx.data` r=Hywan a=Hywan

This patch adds 2 functions for the runtime C API, respectively
`wasmer_instance_context_data_set` and
`wasmer_instance_context_data_get`. The goal is to modify the
`vm::Ctx.data` field in the `runtime-core` library.

Since all imported functions receive this context, that's the
simplest way to pass dynamic data to them.

Co-authored-by: Ivan Enderlin <ivan.enderlin@hoa-project.net>
Co-authored-by: Syrus Akbary <me@syrusakbary.com>
This commit is contained in:
bors[bot] 2019-03-12 08:30:24 +00:00
commit 97187f6d21
4 changed files with 58 additions and 4 deletions

View File

@ -947,6 +947,18 @@ pub unsafe extern "C" fn wasmer_instance_exports(
*exports = Box::into_raw(named_exports) as *mut wasmer_exports_t; *exports = Box::into_raw(named_exports) as *mut wasmer_exports_t;
} }
/// Sets the `data` field of the instance context. This context will be
/// passed to all imported function for instance.
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_instance_context_data_set(
instance: *mut wasmer_instance_t,
data_ptr: *mut c_void,
) {
let instance_ref = unsafe { &mut *(instance as *mut Instance) };
instance_ref.context_mut().data = data_ptr;
}
pub struct NamedExports(Vec<NamedExport>); pub struct NamedExports(Vec<NamedExport>);
/// Frees the memory for the given exports /// Frees the memory for the given exports
@ -1350,6 +1362,16 @@ pub extern "C" fn wasmer_instance_context_memory(
memory as *const Memory as *const wasmer_memory_t memory as *const Memory as *const wasmer_memory_t
} }
/// Gets the `data` field within the context.
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_instance_context_data_get(
ctx: *const wasmer_instance_context_t,
) -> *mut c_void {
let ctx = unsafe { &*(ctx as *const Ctx) };
ctx.data
}
/// Gets the start pointer to the bytes within a Memory /// Gets the start pointer to the bytes within a Memory
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
#[no_mangle] #[no_mangle]

View File

@ -8,6 +8,11 @@ static bool print_str_called = false;
static int memory_len = 0; static int memory_len = 0;
static int ptr_len = 0; static int ptr_len = 0;
static char actual_str[14] = {}; static char actual_str[14] = {};
static int actual_context_data_value = 0;
typedef struct {
int value;
} context_data;
void print_str(wasmer_instance_context_t *ctx, int32_t ptr, int32_t len) void print_str(wasmer_instance_context_t *ctx, int32_t ptr, int32_t len)
{ {
@ -23,6 +28,8 @@ void print_str(wasmer_instance_context_t *ctx, int32_t ptr, int32_t len)
print_str_called = true; print_str_called = true;
memory_len = mem_len; memory_len = mem_len;
ptr_len = len; ptr_len = len;
actual_context_data_value = ((context_data *) wasmer_instance_context_data_get(ctx))->value;
} }
int main() int main()
@ -65,6 +72,11 @@ int main()
assert(compile_result == WASMER_OK); assert(compile_result == WASMER_OK);
context_data* context_data = malloc(sizeof(context_data));
int context_data_value = 42;
context_data->value = context_data_value;
wasmer_instance_context_data_set(instance, context_data);
wasmer_value_t params[] = {}; wasmer_value_t params[] = {};
wasmer_value_t results[] = {}; wasmer_value_t results[] = {};
wasmer_result_t call_result = wasmer_instance_call(instance, "hello_wasm", params, 0, results, 0); wasmer_result_t call_result = wasmer_instance_call(instance, "hello_wasm", params, 0, results, 0);
@ -82,10 +94,12 @@ int main()
assert(memory_len == 17); assert(memory_len == 17);
assert(ptr_len == 13); assert(ptr_len == 13);
assert(0 == strcmp(actual_str, "Hello, World!")); assert(0 == strcmp(actual_str, "Hello, World!"));
assert(context_data_value == actual_context_data_value);
printf("Destroying func\n"); printf("Destroying func\n");
wasmer_import_func_destroy(func); wasmer_import_func_destroy(func);
printf("Destroy instance\n"); printf("Destroy instance\n");
wasmer_instance_destroy(instance); wasmer_instance_destroy(instance);
free(context_data);
return 0; return 0;
} }

View File

@ -95,11 +95,11 @@ typedef struct {
typedef struct { typedef struct {
} wasmer_memory_t; } wasmer_instance_context_t;
typedef struct { typedef struct {
} wasmer_instance_context_t; } wasmer_memory_t;
typedef struct { typedef struct {
@ -382,6 +382,17 @@ wasmer_result_t wasmer_instance_call(wasmer_instance_t *instance,
wasmer_value_t *results, wasmer_value_t *results,
int results_len); int results_len);
/**
* Gets the `data` field within the context.
*/
void *wasmer_instance_context_data_get(const wasmer_instance_context_t *ctx);
/**
* Sets the `data` field of the instance context. This context will be
* passed to all imported function for instance.
*/
void wasmer_instance_context_data_set(wasmer_instance_t *instance, void *data_ptr);
/** /**
* Gets the memory within the context at the index `memory_idx`. * Gets the memory within the context at the index `memory_idx`.
* The index is always 0 until multiple memories are supported. * The index is always 0 until multiple memories are supported.

View File

@ -90,11 +90,11 @@ struct wasmer_instance_t {
}; };
struct wasmer_memory_t { struct wasmer_instance_context_t {
}; };
struct wasmer_instance_context_t { struct wasmer_memory_t {
}; };
@ -307,6 +307,13 @@ wasmer_result_t wasmer_instance_call(wasmer_instance_t *instance,
wasmer_value_t *results, wasmer_value_t *results,
int results_len); int results_len);
/// Gets the `data` field within the context.
void *wasmer_instance_context_data_get(const wasmer_instance_context_t *ctx);
/// Sets the `data` field of the instance context. This context will be
/// passed to all imported function for instance.
void wasmer_instance_context_data_set(wasmer_instance_t *instance, void *data_ptr);
/// Gets the memory within the context at the index `memory_idx`. /// Gets the memory within the context at the index `memory_idx`.
/// The index is always 0 until multiple memories are supported. /// The index is always 0 until multiple memories are supported.
const wasmer_memory_t *wasmer_instance_context_memory(const wasmer_instance_context_t *ctx, const wasmer_memory_t *wasmer_instance_context_memory(const wasmer_instance_context_t *ctx,