diff --git a/lib/runtime-c-api/src/lib.rs b/lib/runtime-c-api/src/lib.rs index 8055fc445..3d571533d 100644 --- a/lib/runtime-c-api/src/lib.rs +++ b/lib/runtime-c-api/src/lib.rs @@ -947,6 +947,17 @@ pub unsafe extern "C" fn wasmer_instance_exports( *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. +#[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); /// Frees the memory for the given exports @@ -1350,6 +1361,15 @@ pub extern "C" fn wasmer_instance_context_memory( memory as *const Memory as *const wasmer_memory_t } +/// Gets the `data` field within the context. +#[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 #[allow(clippy::cast_ptr_alignment)] #[no_mangle] diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index c9cb61709..43d97ecb8 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -95,11 +95,11 @@ typedef struct { typedef struct { -} wasmer_memory_t; +} wasmer_instance_context_t; typedef struct { -} wasmer_instance_context_t; +} wasmer_memory_t; typedef struct { @@ -382,6 +382,17 @@ wasmer_result_t wasmer_instance_call(wasmer_instance_t *instance, wasmer_value_t *results, 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`. * The index is always 0 until multiple memories are supported. diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index 4578eae49..76c27d46e 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -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, 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`. /// The index is always 0 until multiple memories are supported. const wasmer_memory_t *wasmer_instance_context_memory(const wasmer_instance_context_t *ctx,