diff --git a/lib/runtime-c-api/src/lib.rs b/lib/runtime-c-api/src/lib.rs index 1bebd8336..d7d513604 100644 --- a/lib/runtime-c-api/src/lib.rs +++ b/lib/runtime-c-api/src/lib.rs @@ -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 { diff --git a/lib/runtime-c-api/tests/test-memory.c b/lib/runtime-c-api/tests/test-memory.c index 0201a36c1..631a6a983 100644 --- a/lib/runtime-c-api/tests/test-memory.c +++ b/lib/runtime-c-api/tests/test-memory.c @@ -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; diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index c6e649bd4..4df5001ef 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -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); diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index 7ac6b1e19..5fb292cb1 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -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);