test(runtime-c-api) Fix compilation errors in test-imports.c.

This commit is contained in:
Ivan Enderlin 2019-03-06 12:21:50 +01:00
parent c658224f0c
commit dcb4032e9d
4 changed files with 30 additions and 29 deletions

View File

@ -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::<u8>()[..]).as_ptr() as *mut Cell<u8> as *mut u8 }
}

View File

@ -2,14 +2,15 @@
#include "../wasmer.h"
#include <assert.h>
#include <stdint.h>
#include <string.h>
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;

View File

@ -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

View File

@ -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.