Add basic Table functions

This commit is contained in:
Brandon Fish 2019-02-09 13:37:07 -06:00
parent c3707efa08
commit 4e5e525626
6 changed files with 110 additions and 8 deletions

View File

@ -7,10 +7,10 @@ use std::slice;
use std::str;
use std::sync::Arc;
use std::{ffi::c_void, mem, ptr};
use wasmer_runtime::{ImportObject, Instance, Memory, Value};
use wasmer_runtime::{ImportObject, Instance, Memory, Table, Value};
use wasmer_runtime_core::export::{Context, Export, FuncPointer};
use wasmer_runtime_core::import::{LikeNamespace, Namespace};
use wasmer_runtime_core::types::{FuncSig, MemoryDescriptor, Type};
use wasmer_runtime_core::types::{ElementType, FuncSig, MemoryDescriptor, TableDescriptor, Type};
use wasmer_runtime_core::units::Pages;
#[allow(non_camel_case_types)]
@ -46,6 +46,14 @@ pub enum wasmer_memory_result_t {
WASMER_MEMORY_ERROR = 2,
}
#[allow(non_camel_case_types)]
#[no_mangle]
#[repr(C)]
pub enum wasmer_table_result_t {
WASMER_TABLE_OK = 1,
WASMER_TABLE_ERROR = 2,
}
#[repr(u32)]
#[derive(Clone)]
pub enum wasmer_value_tag {
@ -74,10 +82,10 @@ pub struct wasmer_value_t {
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_memory_t();
//{
// pub ptr: *mut uint8_t,
// pub len: uint32_t,
//}
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_table_t();
#[repr(C)]
pub struct wasmer_limits_t {
@ -118,7 +126,6 @@ pub unsafe extern "C" fn wasmer_memory_new(
let new_memory = match result {
Ok(memory) => memory,
Err(error) => {
println!("Err: {:?}", error);
return wasmer_memory_result_t::WASMER_MEMORY_ERROR;
}
};
@ -135,6 +142,44 @@ pub extern "C" fn wasmer_memory_length(memory: *mut wasmer_memory_t) -> uint32_t
len
}
#[no_mangle]
pub unsafe extern "C" fn wasmer_table_new(
mut table: *mut *mut wasmer_table_t,
limits: wasmer_limits_t,
) -> wasmer_table_result_t {
let desc = TableDescriptor {
element: ElementType::Anyfunc,
minimum: limits.min,
maximum: Some(limits.max),
};
let result = Table::new(desc);
let new_table = match result {
Ok(table) => table,
Err(error) => {
return wasmer_table_result_t::WASMER_TABLE_ERROR;
}
};
unsafe { *table = Box::into_raw(Box::new(new_table)) as *mut wasmer_table_t };
wasmer_table_result_t::WASMER_TABLE_OK
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_table_length(table: *mut wasmer_table_t) -> uint32_t {
let table = unsafe { Box::from_raw(table as *mut Table) };
let len = table.size();
Box::into_raw(table);
len
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_table_destroy(table: *mut wasmer_table_t) {
if !table.is_null() {
drop(unsafe { Box::from_raw(table as *mut Table) });
}
}
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import_object_t) {
@ -169,7 +214,6 @@ pub unsafe extern "C" fn wasmer_instantiate(
let new_instance = match result {
Ok(instance) => instance,
Err(error) => {
println!("Err: {:?}", error);
return wasmer_compile_result_t::WASMER_COMPILE_ERROR;
}
};

View File

@ -12,5 +12,6 @@ _deps
test-instantiate
test-import-function
test-memory
test-tables
test-validate
rust-build

View File

@ -5,6 +5,7 @@ add_executable(test-instantiate test-instantiate.c)
add_executable(test-import-function test-import-function.c)
add_executable(test-memory test-memory.c)
add_executable(test-validate test-validate.c)
add_executable(test-tables test-tables.c)
find_library(
WASMER_LIB NAMES libwasmer_runtime_c_api.dylib libwasmer_runtime_c_api.so libwasmer_runtime_c_api.dll
@ -23,10 +24,13 @@ target_link_libraries(test-memory
general ${WASMER_LIB})
target_link_libraries(test-validate
general ${WASMER_LIB})
target_link_libraries(test-tables
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)
add_test(test-validate test-validate)
add_test(test-tables test-tables)

View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include "../wasmer.h"
#include <assert.h>
#include <stdint.h>
int main()
{
wasmer_table_t *table = NULL;
wasmer_limits_t descriptor;
descriptor.min = 10;
descriptor.max = 10;
wasmer_table_result_t table_result = wasmer_table_new(&table, descriptor);
printf("Table result: %d\n", table_result);
assert(table_result == WASMER_TABLE_OK);
uint32_t len = wasmer_table_length(table);
printf("Table length: %d\n", len);
assert(len == 10);
printf("Destroy table\n");
wasmer_table_destroy(table);
return 0;
}

View File

@ -18,6 +18,11 @@ typedef enum {
WASMER_MEMORY_ERROR = 2,
} wasmer_memory_result_t;
typedef enum {
WASMER_TABLE_OK = 1,
WASMER_TABLE_ERROR = 2,
} wasmer_table_result_t;
enum wasmer_value_tag {
WASM_I32,
WASM_I64,
@ -53,6 +58,10 @@ typedef struct {
uint32_t max;
} wasmer_limits_t;
typedef struct {
} wasmer_table_t;
void wasmer_import_object_destroy(wasmer_import_object_t *import_object);
wasmer_import_object_t *wasmer_import_object_new(void);
@ -88,4 +97,10 @@ uint32_t wasmer_memory_length(wasmer_memory_t *memory);
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);
void wasmer_table_destroy(wasmer_table_t *table);
uint32_t wasmer_table_length(wasmer_table_t *table);
wasmer_table_result_t wasmer_table_new(wasmer_table_t **table, wasmer_limits_t limits);
bool wasmer_validate(uint8_t *wasm_bytes, uint32_t wasm_bytes_len);

View File

@ -17,6 +17,11 @@ enum class wasmer_memory_result_t {
WASMER_MEMORY_ERROR = 2,
};
enum class wasmer_table_result_t {
WASMER_TABLE_OK = 1,
WASMER_TABLE_ERROR = 2,
};
enum class wasmer_value_tag : uint32_t {
WASM_I32,
WASM_I64,
@ -51,6 +56,10 @@ struct wasmer_limits_t {
uint32_t max;
};
struct wasmer_table_t {
};
extern "C" {
void wasmer_import_object_destroy(wasmer_import_object_t *import_object);
@ -88,6 +97,12 @@ uint32_t wasmer_memory_length(wasmer_memory_t *memory);
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);
void wasmer_table_destroy(wasmer_table_t *table);
uint32_t wasmer_table_length(wasmer_table_t *table);
wasmer_table_result_t wasmer_table_new(wasmer_table_t **table, wasmer_limits_t limits);
bool wasmer_validate(uint8_t *wasm_bytes, uint32_t wasm_bytes_len);
} // extern "C"