Add memory data function

This commit is contained in:
Brandon Fish 2019-02-10 15:20:35 -06:00
parent a502da58e3
commit 66bf13c9dd
4 changed files with 23 additions and 1 deletions

View File

@ -439,6 +439,14 @@ pub extern "C" fn wasmer_instance_context_memory(
memory as *const Memory as *const wasmer_memory_t
}
#[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;
use std::cell::Cell;
unsafe { ((*memory).view::<u8>()[..]).as_ptr() as *mut Cell<u8> as *mut u8 }
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_memory_data_length(mem: *mut wasmer_memory_t) -> uint32_t {

View File

@ -5,13 +5,21 @@
static print_str_called = false;
static memory_len = 0;
static ptr_len = 0;
static char actual_str[14] = {};
void print_str(int32_t ptr, int32_t len, wasmer_instance_context_t *ctx) {
wasmer_memory_t *memory = wasmer_instance_context_memory(ctx, 0);
uint32_t mem_len = wasmer_memory_length(memory);
printf("In print_str, memory len: %d\n", mem_len);
uint8_t *mem_bytes = wasmer_memory_data(memory);
for(int32_t idx = 0; idx < len; idx++){
actual_str[idx] = mem_bytes[ptr + idx];
}
actual_str[13] = '\0';
printf("In print_str, memory len: %d, ptr_len: %d\n, str %s", mem_len, len, actual_str);
print_str_called = true;
memory_len = mem_len;
ptr_len = len;
}
int main()
@ -43,6 +51,8 @@ int main()
assert(print_str_called);
assert(memory_len == 17);
assert(ptr_len == 13);
assert(0 == strcmp(actual_str, "Hello, World!"));
printf("Destroy instance\n");
wasmer_instance_destroy(instance);

View File

@ -114,6 +114,8 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
uint32_t wasm_bytes_len,
wasmer_import_object_t *import_object);
uint8_t *wasmer_memory_data(wasmer_memory_t *mem);
uint32_t wasmer_memory_data_length(wasmer_memory_t *mem);
void wasmer_memory_destroy(wasmer_memory_t *memory);

View File

@ -114,6 +114,8 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
uint32_t wasm_bytes_len,
wasmer_import_object_t *import_object);
uint8_t *wasmer_memory_data(wasmer_memory_t *mem);
uint32_t wasmer_memory_data_length(wasmer_memory_t *mem);
void wasmer_memory_destroy(wasmer_memory_t *memory);