feat(runtime-c-api) Extract the table module.

This commit is contained in:
Ivan Enderlin 2019-03-29 15:02:50 +01:00
parent 4239975240
commit 8bdb458ea7
2 changed files with 89 additions and 81 deletions

View File

@ -11,16 +11,18 @@ use wasmer_runtime::{Ctx, Global, ImportObject, Instance, Memory, Module, Table,
use wasmer_runtime_core::export::{Context, Export, FuncPointer};
use wasmer_runtime_core::import::Namespace;
use wasmer_runtime_core::module::{ExportIndex, ImportName};
use wasmer_runtime_core::types::{ElementType, FuncSig, TableDescriptor, Type};
use wasmer_runtime_core::types::{FuncSig, Type};
pub mod error;
pub mod memory;
pub mod module;
pub mod table;
pub mod value;
use error::{update_last_error, CApiError};
use memory::wasmer_memory_t;
use module::wasmer_module_t;
use table::wasmer_table_t;
use value::{wasmer_value, wasmer_value_t, wasmer_value_tag};
#[repr(C)]
@ -43,10 +45,6 @@ pub struct wasmer_global_descriptor_t {
kind: wasmer_value_tag,
}
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_table_t;
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_import_func_t;
@ -128,82 +126,6 @@ pub struct wasmer_byte_array {
bytes_len: uint32_t,
}
/// Creates a new Table for the given descriptor and initializes the given
/// pointer to pointer to a pointer to the new Table.
///
/// The caller owns the object and should call `wasmer_table_destroy` to free it.
///
/// Returns `wasmer_result_t::WASMER_OK` upon success.
///
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
/// and `wasmer_last_error_message` to get an error message.
#[no_mangle]
pub unsafe extern "C" fn wasmer_table_new(
table: *mut *mut wasmer_table_t,
limits: wasmer_limits_t,
) -> wasmer_result_t {
let max = if limits.max.has_some {
Some(limits.max.some)
} else {
None
};
let desc = TableDescriptor {
element: ElementType::Anyfunc,
minimum: limits.min,
maximum: max,
};
let result = Table::new(desc);
let new_table = match result {
Ok(table) => table,
Err(error) => {
update_last_error(error);
return wasmer_result_t::WASMER_ERROR;
}
};
*table = Box::into_raw(Box::new(new_table)) as *mut wasmer_table_t;
wasmer_result_t::WASMER_OK
}
/// Grows a Table by the given number of elements.
///
/// Returns `wasmer_result_t::WASMER_OK` upon success.
///
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
/// and `wasmer_last_error_message` to get an error message.
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_table_grow(
table: *mut wasmer_table_t,
delta: uint32_t,
) -> wasmer_result_t {
let table = unsafe { &*(table as *mut Table) };
let delta_result = table.grow(delta);
match delta_result {
Ok(_) => wasmer_result_t::WASMER_OK,
Err(grow_error) => {
update_last_error(grow_error);
wasmer_result_t::WASMER_ERROR
}
}
}
/// Returns the current length of the given Table
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_table_length(table: *mut wasmer_table_t) -> uint32_t {
let table = unsafe { &*(table as *mut Table) };
table.size()
}
/// Frees memory for the given Table
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_table_destroy(table: *mut wasmer_table_t) {
if !table.is_null() {
unsafe { Box::from_raw(table as *mut Table) };
}
}
/// Creates a new Global and returns a pointer to it.
/// The caller owns the object and should call `wasmer_global_destroy` to free it.
#[no_mangle]

View File

@ -0,0 +1,86 @@
//! Wasm tables.
use crate::{error::update_last_error, wasmer_limits_t, wasmer_result_t};
use libc::uint32_t;
use wasmer_runtime::Table;
use wasmer_runtime_core::types::{ElementType, TableDescriptor};
#[repr(C)]
#[derive(Clone)]
pub struct wasmer_table_t;
/// Creates a new Table for the given descriptor and initializes the given
/// pointer to pointer to a pointer to the new Table.
///
/// The caller owns the object and should call `wasmer_table_destroy` to free it.
///
/// Returns `wasmer_result_t::WASMER_OK` upon success.
///
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
/// and `wasmer_last_error_message` to get an error message.
#[no_mangle]
pub unsafe extern "C" fn wasmer_table_new(
table: *mut *mut wasmer_table_t,
limits: wasmer_limits_t,
) -> wasmer_result_t {
let max = if limits.max.has_some {
Some(limits.max.some)
} else {
None
};
let desc = TableDescriptor {
element: ElementType::Anyfunc,
minimum: limits.min,
maximum: max,
};
let result = Table::new(desc);
let new_table = match result {
Ok(table) => table,
Err(error) => {
update_last_error(error);
return wasmer_result_t::WASMER_ERROR;
}
};
*table = Box::into_raw(Box::new(new_table)) as *mut wasmer_table_t;
wasmer_result_t::WASMER_OK
}
/// Grows a Table by the given number of elements.
///
/// Returns `wasmer_result_t::WASMER_OK` upon success.
///
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
/// and `wasmer_last_error_message` to get an error message.
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_table_grow(
table: *mut wasmer_table_t,
delta: uint32_t,
) -> wasmer_result_t {
let table = unsafe { &*(table as *mut Table) };
let delta_result = table.grow(delta);
match delta_result {
Ok(_) => wasmer_result_t::WASMER_OK,
Err(grow_error) => {
update_last_error(grow_error);
wasmer_result_t::WASMER_ERROR
}
}
}
/// Returns the current length of the given Table
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_table_length(table: *mut wasmer_table_t) -> uint32_t {
let table = unsafe { &*(table as *mut Table) };
table.size()
}
/// Frees memory for the given Table
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub extern "C" fn wasmer_table_destroy(table: *mut wasmer_table_t) {
if !table.is_null() {
unsafe { Box::from_raw(table as *mut Table) };
}
}