Add Memory Grow C API

This commit is contained in:
Brandon Fish 2019-02-09 17:53:03 -06:00
parent a8dcc0ee87
commit a0288c87ac
4 changed files with 32 additions and 1 deletions

View File

@ -146,6 +146,22 @@ pub unsafe extern "C" fn wasmer_memory_new(
wasmer_memory_result_t::WASMER_MEMORY_OK
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_memory_grow(
memory: *mut wasmer_memory_t,
delta: uint32_t,
) -> wasmer_memory_result_t {
let memory = unsafe { Box::from_raw(memory as *mut Memory) };
let maybe_delta = memory.grow(Pages(delta));
Box::into_raw(memory);
if let Some(_delta) = maybe_delta {
wasmer_memory_result_t::WASMER_MEMORY_OK
} else {
wasmer_memory_result_t::WASMER_MEMORY_ERROR
}
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_memory_length(memory: *mut wasmer_memory_t) -> uint32_t {

View File

@ -8,7 +8,7 @@ int main()
wasmer_memory_t *memory = NULL;
wasmer_limits_t descriptor;
descriptor.min = 10;
descriptor.max = 10;
descriptor.max = 15;
wasmer_memory_result_t memory_result = wasmer_memory_new(&memory, descriptor);
printf("Memory result: %d\n", memory_result);
assert(memory_result == WASMER_MEMORY_OK);
@ -17,6 +17,17 @@ int main()
printf("Memory pages length: %d\n", len);
assert(len == 10);
wasmer_memory_result_t grow_result = wasmer_memory_grow(memory, 2);
assert(grow_result == WASMER_MEMORY_OK);
uint32_t new_len = wasmer_memory_length(memory);
printf("Memory pages length: %d\n", new_len);
assert(new_len == 12);
// Err, grow beyond max
wasmer_memory_result_t grow_result2 = wasmer_memory_grow(memory, 10);
assert(grow_result2 == WASMER_MEMORY_ERROR);
printf("Destroy memory\n");
wasmer_memory_destroy(memory);
return 0;

View File

@ -112,6 +112,8 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
void wasmer_memory_destroy(wasmer_memory_t *memory);
wasmer_memory_result_t wasmer_memory_grow(wasmer_memory_t *memory, uint32_t delta);
uint32_t wasmer_memory_length(wasmer_memory_t *memory);
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);

View File

@ -112,6 +112,8 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
void wasmer_memory_destroy(wasmer_memory_t *memory);
wasmer_memory_result_t wasmer_memory_grow(wasmer_memory_t *memory, uint32_t delta);
uint32_t wasmer_memory_length(wasmer_memory_t *memory);
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);