feat(runtime-c-api) Add the wasmer_serialized_module_from_bytes function.

This function is required to transform a `wasmer_byte_array` into a
`wasmer_serialized_module_t`. This is the complementary function of
`wasmer_serialized_module_bytes`.
This commit is contained in:
Ivan Enderlin
2019-03-19 10:51:43 +01:00
parent 5e31a1b6d1
commit 46edd20725
4 changed files with 61 additions and 0 deletions

View File

@ -627,6 +627,36 @@ pub unsafe extern "C" fn wasmer_serialized_module_bytes(
} }
} }
/// Transform a sequence of bytes into a serialized module.
///
/// The caller owns the object and should call `wasmer_serialized_module_destroy` to free it.
///
/// Returns `wasmer_result_t::WASMER_OK` upon success.
///
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
/// and `wasmer_last_error_message` to get an error message.
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_serialized_module_from_bytes(
serialized_module: *mut *mut wasmer_serialized_module_t,
serialized_module_bytes: *const wasmer_byte_array,
) -> wasmer_result_t {
if serialized_module.is_null() {
update_last_error(CApiError {
msg: "`serialized_module_bytes` pointer is null".to_string(),
});
return wasmer_result_t::WASMER_ERROR;
}
let serialized_module_bytes: &[u8] = slice::from_raw_parts(
(*serialized_module_bytes).bytes,
(*serialized_module_bytes).bytes_len as usize,
);
*serialized_module = Box::into_raw(Box::new(serialized_module_bytes)) as _;
wasmer_result_t::WASMER_OK
}
/// Deserialize the given serialized module. /// Deserialize the given serialized module.
/// ///
/// Returns `wasmer_result_t::WASMER_OK` upon success. /// Returns `wasmer_result_t::WASMER_OK` upon success.

View File

@ -63,8 +63,21 @@ int main()
assert(results[0].value.I32 == 15); assert(results[0].value.I32 == 15);
assert(call_result == WASMER_OK); assert(call_result == WASMER_OK);
wasmer_serialized_module_t *serialized_module_two = NULL;
wasmer_result_t serialized_module_from_bytes_result = wasmer_serialized_module_from_bytes(&serialized_module_two, &serialized_module_bytes);
assert(serialized_module_from_bytes_result == WASMER_OK);
wasmer_module_t *module_three = NULL;
wasmer_result_t unserialized_result_two = wasmer_module_deserialize(&module_three, serialized_module_two);
assert(unserialized_result_two == WASMER_OK);
wasmer_instance_t *instance_two = NULL;
wasmer_result_t instantiate_result_two = wasmer_module_instantiate(module_three, &instance, imports, 0);
assert(instantiate_result_two == WASMER_OK);
printf("Destroy the serialized module\n"); printf("Destroy the serialized module\n");
wasmer_serialized_module_destroy(serialized_module); wasmer_serialized_module_destroy(serialized_module);
wasmer_serialized_module_destroy(serialized_module_two);
printf("Destroy instance\n"); printf("Destroy instance\n");
wasmer_instance_destroy(instance); wasmer_instance_destroy(instance);

View File

@ -537,6 +537,16 @@ wasmer_byte_array wasmer_serialized_module_bytes(const wasmer_serialized_module_
*/ */
void wasmer_serialized_module_destroy(wasmer_serialized_module_t *serialized_module); void wasmer_serialized_module_destroy(wasmer_serialized_module_t *serialized_module);
/**
* Transform a sequence of bytes into a serialized module.
* The caller owns the object and should call `wasmer_serialized_module_destroy` to free it.
* Returns `wasmer_result_t::WASMER_OK` upon success.
* Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
* and `wasmer_last_error_message` to get an error message.
*/
wasmer_result_t wasmer_serialized_module_from_bytes(wasmer_serialized_module_t **serialized_module,
const wasmer_byte_array *serialized_module_bytes);
/** /**
* Frees memory for the given Table * Frees memory for the given Table
*/ */

View File

@ -422,6 +422,14 @@ wasmer_byte_array wasmer_serialized_module_bytes(const wasmer_serialized_module_
/// Frees memory for the given serialized Module. /// Frees memory for the given serialized Module.
void wasmer_serialized_module_destroy(wasmer_serialized_module_t *serialized_module); void wasmer_serialized_module_destroy(wasmer_serialized_module_t *serialized_module);
/// Transform a sequence of bytes into a serialized module.
/// The caller owns the object and should call `wasmer_serialized_module_destroy` to free it.
/// Returns `wasmer_result_t::WASMER_OK` upon success.
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
/// and `wasmer_last_error_message` to get an error message.
wasmer_result_t wasmer_serialized_module_from_bytes(wasmer_serialized_module_t **serialized_module,
const wasmer_byte_array *serialized_module_bytes);
/// Frees memory for the given Table /// Frees memory for the given Table
void wasmer_table_destroy(wasmer_table_t *table); void wasmer_table_destroy(wasmer_table_t *table);