Implement basic C API memory functions

This commit is contained in:
Brandon Fish 2019-02-04 21:46:47 -06:00
parent 2defd27fac
commit 8d8dea7ec8
6 changed files with 128 additions and 5 deletions

View File

@ -7,10 +7,11 @@ use std::slice;
use std::str;
use std::sync::Arc;
use std::{ffi::c_void, mem, ptr};
use wasmer_runtime::{ImportObject, Instance, Value};
use wasmer_runtime::{ImportObject, Instance, Memory, Value};
use wasmer_runtime_core::export::{Context, Export, FuncPointer};
use wasmer_runtime_core::import::{LikeNamespace, Namespace};
use wasmer_runtime_core::types::{FuncSig, Type};
use wasmer_runtime_core::types::{FuncSig, MemoryDescriptor, Type};
use wasmer_runtime_core::units::Pages;
#[allow(non_camel_case_types)]
pub struct wasmer_import_object_t();
@ -37,6 +38,14 @@ pub enum wasmer_call_result_t {
WASMER_CALL_ERROR = 2,
}
#[allow(non_camel_case_types)]
#[no_mangle]
#[repr(C)]
pub enum wasmer_memory_result_t {
WASMER_MEMORY_OK = 1,
WASMER_MEMORY_ERROR = 2,
}
#[repr(u32)]
#[derive(Clone)]
pub enum wasmer_value_tag {
@ -64,9 +73,16 @@ pub struct wasmer_value_t {
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_memory_t {
pub ptr: *mut uint8_t,
pub len: uint32_t,
pub struct wasmer_memory_t();
//{
// pub ptr: *mut uint8_t,
// pub len: uint32_t,
//}
#[repr(C)]
pub struct wasmer_limits_t {
pub min: uint32_t,
pub max: uint32_t,
}
#[no_mangle]
@ -74,6 +90,37 @@ pub extern "C" fn wasmer_import_object_new() -> *mut wasmer_import_object_t {
Box::into_raw(Box::new(ImportObject::new())) as *mut wasmer_import_object_t
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_memory_new(
mut memory: *mut *mut wasmer_memory_t,
limits: wasmer_limits_t,
) -> wasmer_memory_result_t {
let desc = MemoryDescriptor {
minimum: Pages(limits.min),
maximum: Some(Pages(limits.max)),
shared: false,
};
let result = Memory::new(desc);
let new_memory = match result {
Ok(memory) => memory,
Err(error) => {
println!("Err: {:?}", error);
return wasmer_memory_result_t::WASMER_MEMORY_ERROR;
}
};
unsafe { *memory = Box::into_raw(Box::new(new_memory)) as *mut wasmer_memory_t };
wasmer_memory_result_t::WASMER_MEMORY_OK
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_memory_length(memory: *mut wasmer_memory_t) -> uint32_t {
let memory = unsafe { Box::from_raw(memory as *mut Memory) };
let Pages(len) = memory.size();
Box::into_raw(memory);
len
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import_object_t) {
@ -82,6 +129,14 @@ pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import
}
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_memory_destroy(memory: *mut wasmer_memory_t) {
if !memory.is_null() {
drop(unsafe { Box::from_raw(memory as *mut Memory) });
}
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_instantiate(

View File

@ -11,4 +11,5 @@ CTestTestfile.cmake
_deps
test-instantiate
test-import-function
test-memory
rust-build

View File

@ -3,6 +3,7 @@ project (WasmerCApiTests)
add_executable(test-instantiate test-instantiate.c)
add_executable(test-import-function test-import-function.c)
add_executable(test-memory test-memory.c)
find_library(
WASMER_LIB NAMES libwasmer_runtime_c_api.dylib libwasmer_runtime_c_api.so libwasmer_runtime_c_api.dll
@ -17,8 +18,11 @@ target_link_libraries(test-instantiate
general ${WASMER_LIB})
target_link_libraries(test-import-function
general ${WASMER_LIB})
target_link_libraries(test-memory
general ${WASMER_LIB})
enable_testing()
add_test(test-instantiate test-instantiate)
add_test(test-import-function test-import-function)
add_test(test-memory test-memory)

View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include "../wasmer.h"
#include <assert.h>
#include <stdint.h>
int main()
{
wasmer_memory_t *memory = NULL;
wasmer_limits_t descriptor;
descriptor.min = 10;
descriptor.max = 10;
wasmer_memory_result_t memory_result = wasmer_memory_new(&memory, descriptor);
printf("Memory result: %d\n", memory_result);
assert(memory_result == WASMER_MEMORY_OK);
uint32_t len = wasmer_memory_length(memory);
printf("Memory pages length: %d\n", len);
assert(len == 10);
printf("Destroy memory\n");
wasmer_memory_destroy(memory);
return 0;
}

View File

@ -13,6 +13,11 @@ typedef enum {
WASMER_COMPILE_ERROR = 2,
} wasmer_compile_result_t;
typedef enum {
WASMER_MEMORY_OK = 1,
WASMER_MEMORY_ERROR = 2,
} wasmer_memory_result_t;
enum wasmer_value_tag {
WASM_I32,
WASM_I64,
@ -39,6 +44,15 @@ typedef struct {
wasmer_value value;
} wasmer_value_t;
typedef struct {
} wasmer_memory_t;
typedef struct {
uint32_t min;
uint32_t max;
} wasmer_limits_t;
void wasmer_import_object_destroy(wasmer_import_object_t *import_object);
wasmer_import_object_t *wasmer_import_object_new(void);
@ -67,3 +81,9 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
uint8_t *wasm_bytes,
uint32_t wasm_bytes_len,
wasmer_import_object_t *import_object);
void wasmer_memory_destroy(wasmer_memory_t *memory);
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

@ -12,6 +12,11 @@ enum class wasmer_compile_result_t {
WASMER_COMPILE_ERROR = 2,
};
enum class wasmer_memory_result_t {
WASMER_MEMORY_OK = 1,
WASMER_MEMORY_ERROR = 2,
};
enum class wasmer_value_tag : uint32_t {
WASM_I32,
WASM_I64,
@ -37,6 +42,15 @@ struct wasmer_value_t {
wasmer_value value;
};
struct wasmer_memory_t {
};
struct wasmer_limits_t {
uint32_t min;
uint32_t max;
};
extern "C" {
void wasmer_import_object_destroy(wasmer_import_object_t *import_object);
@ -68,4 +82,10 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
uint32_t wasm_bytes_len,
wasmer_import_object_t *import_object);
void wasmer_memory_destroy(wasmer_memory_t *memory);
uint32_t wasmer_memory_length(wasmer_memory_t *memory);
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);
} // extern "C"