From dcb4032e9d2cfbf3ddd7b8c494bdd9431143f19e Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 6 Mar 2019 12:21:50 +0100 Subject: [PATCH] test(runtime-c-api) Fix compilation errors in `test-imports.c`. --- lib/runtime-c-api/src/lib.rs | 14 ++++++------- lib/runtime-c-api/tests/test-imports.c | 29 +++++++++++++------------- lib/runtime-c-api/wasmer.h | 8 +++---- lib/runtime-c-api/wasmer.hh | 8 +++---- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/runtime-c-api/src/lib.rs b/lib/runtime-c-api/src/lib.rs index 591a03a54..3d078cbb7 100644 --- a/lib/runtime-c-api/src/lib.rs +++ b/lib/runtime-c-api/src/lib.rs @@ -232,8 +232,8 @@ pub extern "C" fn wasmer_memory_grow( /// Returns the current length in pages of the given memory #[allow(clippy::cast_ptr_alignment)] #[no_mangle] -pub extern "C" fn wasmer_memory_length(memory: *mut wasmer_memory_t) -> uint32_t { - let memory = unsafe { &*(memory as *mut Memory) }; +pub extern "C" fn wasmer_memory_length(memory: *const wasmer_memory_t) -> uint32_t { + let memory = unsafe { &*(memory as *const Memory) }; let Pages(len) = memory.size(); len } @@ -1153,7 +1153,7 @@ pub unsafe extern "C" fn wasmer_import_func_new( ctx: Context::Internal, signature: Arc::new(FuncSig::new(params, returns)), }); - Box::into_raw(export) as *mut wasmer_import_func_t + Box::into_raw(export) as *const wasmer_import_func_t } /// Sets the params buffer to the parameter types of the given wasmer_import_func_t @@ -1241,7 +1241,7 @@ pub unsafe extern "C" fn wasmer_import_func_returns_arity( /// Frees memory for the given Func #[allow(clippy::cast_ptr_alignment)] #[no_mangle] -pub extern "C" fn wasmer_import_func_destroy(func: *mut wasmer_import_func_t) { +pub extern "C" fn wasmer_import_func_destroy(func: *const wasmer_import_func_t) { if !func.is_null() { drop(unsafe { Box::from_raw(func as *mut Export) }); } @@ -1342,7 +1342,7 @@ pub unsafe extern "C" fn wasmer_export_func_call( #[allow(clippy::cast_ptr_alignment)] #[no_mangle] pub extern "C" fn wasmer_instance_context_memory( - ctx: *mut wasmer_instance_context_t, + ctx: *const wasmer_instance_context_t, _memory_idx: uint32_t, ) -> *const wasmer_memory_t { let ctx = unsafe { &*(ctx as *const Ctx) }; @@ -1353,8 +1353,8 @@ pub extern "C" fn wasmer_instance_context_memory( /// Gets the start pointer to the bytes within a Memory #[allow(clippy::cast_ptr_alignment)] #[no_mangle] -pub extern "C" fn wasmer_memory_data(mem: *mut wasmer_memory_t) -> *mut uint8_t { - let memory = mem as *mut Memory; +pub extern "C" fn wasmer_memory_data(mem: *const wasmer_memory_t) -> *mut uint8_t { + let memory = mem as *const Memory; use std::cell::Cell; unsafe { ((*memory).view::()[..]).as_ptr() as *mut Cell as *mut u8 } } diff --git a/lib/runtime-c-api/tests/test-imports.c b/lib/runtime-c-api/tests/test-imports.c index 4410c4dd6..26734e17b 100644 --- a/lib/runtime-c-api/tests/test-imports.c +++ b/lib/runtime-c-api/tests/test-imports.c @@ -2,14 +2,15 @@ #include "../wasmer.h" #include #include +#include -static print_str_called = false; +bool static print_str_called = false; // Host function that will be imported into the Web Assembly Instance -void print_str(wasmer_instance_context_t *ctx, int32_t ptr, int32_t len) +void print_str(const wasmer_instance_context_t *ctx, int32_t ptr, int32_t len) { print_str_called = true; - wasmer_memory_t *memory = wasmer_instance_context_memory(ctx, 0); + const wasmer_memory_t *memory = wasmer_instance_context_memory(ctx, 0); uint32_t mem_len = wasmer_memory_length(memory); uint8_t *mem_bytes = wasmer_memory_data(memory); printf("%.*s", len, mem_bytes + ptr); @@ -31,19 +32,19 @@ int main() // of our `print_str` host function wasmer_value_tag params_sig[] = {WASM_I32, WASM_I32}; wasmer_value_tag returns_sig[] = {}; - wasmer_import_func_t *func = wasmer_import_func_new(print_str, params_sig, 2, returns_sig, 0); + const wasmer_import_func_t *func = wasmer_import_func_new((void (*)(void *)) print_str, params_sig, 2, returns_sig, 0); // Create module name for our imports // represented in bytes for UTF-8 compatability - char *module_name = "env"; + const char *module_name = "env"; wasmer_byte_array module_name_bytes; - module_name_bytes.bytes = module_name; + module_name_bytes.bytes = (const uint8_t *) module_name; module_name_bytes.bytes_len = strlen(module_name); // Define a function import - char *import_name = "_print_str"; + const char *import_name = "_print_str"; wasmer_byte_array import_name_bytes; - import_name_bytes.bytes = import_name; + import_name_bytes.bytes = (const uint8_t *) import_name; import_name_bytes.bytes_len = strlen(import_name); wasmer_import_t func_import; func_import.module_name = module_name_bytes; @@ -52,9 +53,9 @@ int main() func_import.value.func = func; // Define a memory import - char *import_memory_name = "memory"; + const char *import_memory_name = "memory"; wasmer_byte_array import_memory_name_bytes; - import_memory_name_bytes.bytes = import_memory_name; + import_memory_name_bytes.bytes = (const uint8_t *) import_memory_name; import_memory_name_bytes.bytes_len = strlen(import_memory_name); wasmer_import_t memory_import; memory_import.module_name = module_name_bytes; @@ -75,9 +76,9 @@ int main() memory_import.value.memory = memory; // Define a global import - char *import_global_name = "__memory_base"; + const char *import_global_name = "__memory_base"; wasmer_byte_array import_global_name_bytes; - import_global_name_bytes.bytes = import_global_name; + import_global_name_bytes.bytes = (const uint8_t *) import_global_name; import_global_name_bytes.bytes_len = strlen(import_global_name); wasmer_import_t global_import; global_import.module_name = module_name_bytes; @@ -90,9 +91,9 @@ int main() global_import.value.global = global; // Define a table import - char *import_table_name = "table"; + const char *import_table_name = "table"; wasmer_byte_array import_table_name_bytes; - import_table_name_bytes.bytes = import_table_name; + import_table_name_bytes.bytes = (const uint8_t *) import_table_name; import_table_name_bytes.bytes_len = strlen(import_table_name); wasmer_import_t table_import; table_import.module_name = module_name_bytes; diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index 8ecdd1d7d..02265c2ff 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -319,7 +319,7 @@ int wasmer_import_descriptors_len(wasmer_import_descriptors_t *exports); /** * Frees memory for the given Func */ -void wasmer_import_func_destroy(wasmer_import_func_t *func); +void wasmer_import_func_destroy(const wasmer_import_func_t *func); /** * Creates new func @@ -386,7 +386,7 @@ wasmer_result_t wasmer_instance_call(wasmer_instance_t *instance, * 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(wasmer_instance_context_t *ctx, +const wasmer_memory_t *wasmer_instance_context_memory(const wasmer_instance_context_t *ctx, uint32_t _memory_idx); /** @@ -442,7 +442,7 @@ int wasmer_last_error_message(char *buffer, int length); /** * Gets the start pointer to the bytes within a Memory */ -uint8_t *wasmer_memory_data(wasmer_memory_t *mem); +uint8_t *wasmer_memory_data(const wasmer_memory_t *mem); /** * Gets the size in bytes of a Memory @@ -465,7 +465,7 @@ wasmer_result_t wasmer_memory_grow(wasmer_memory_t *memory, uint32_t delta); /** * Returns the current length in pages of the given memory */ -uint32_t wasmer_memory_length(wasmer_memory_t *memory); +uint32_t wasmer_memory_length(const wasmer_memory_t *memory); /** * Creates a new Memory for the given descriptor and initializes the given diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index b093b2b64..ce3bcc660 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -256,7 +256,7 @@ wasmer_import_descriptor_t *wasmer_import_descriptors_get(wasmer_import_descript int wasmer_import_descriptors_len(wasmer_import_descriptors_t *exports); /// Frees memory for the given Func -void wasmer_import_func_destroy(wasmer_import_func_t *func); +void wasmer_import_func_destroy(const wasmer_import_func_t *func); /// Creates new func /// The caller owns the object and should call `wasmer_import_func_destroy` to free it. @@ -309,7 +309,7 @@ wasmer_result_t wasmer_instance_call(wasmer_instance_t *instance, /// 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(wasmer_instance_context_t *ctx, +const wasmer_memory_t *wasmer_instance_context_memory(const wasmer_instance_context_t *ctx, uint32_t _memory_idx); /// Frees memory for the given Instance @@ -353,7 +353,7 @@ int wasmer_last_error_length(); int wasmer_last_error_message(char *buffer, int length); /// Gets the start pointer to the bytes within a Memory -uint8_t *wasmer_memory_data(wasmer_memory_t *mem); +uint8_t *wasmer_memory_data(const wasmer_memory_t *mem); /// Gets the size in bytes of a Memory uint32_t wasmer_memory_data_length(wasmer_memory_t *mem); @@ -368,7 +368,7 @@ void wasmer_memory_destroy(wasmer_memory_t *memory); wasmer_result_t wasmer_memory_grow(wasmer_memory_t *memory, uint32_t delta); /// Returns the current length in pages of the given memory -uint32_t wasmer_memory_length(wasmer_memory_t *memory); +uint32_t wasmer_memory_length(const wasmer_memory_t *memory); /// Creates a new Memory for the given descriptor and initializes the given /// pointer to pointer to a pointer to the new memory.