Skip to content

Commit

Permalink
[Rust][feature](types, function, global, memory, table): add types mo…
Browse files Browse the repository at this point in the history
…dule and refactor relevant code

Signed-off-by: Xin Liu <[email protected]>
  • Loading branch information
apepkuss authored and hydai committed Dec 7, 2021
1 parent 4aa79f3 commit 279c015
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ autoconf/autom4te.cache
/.idea
# (Rust) Cargo lockfile, not published with libraries.
Cargo.lock
bindings/rust/target
bindings/rust/wasmedge-sdk/target
bindings/rust/wasmedge-sys/target

Expand Down
16 changes: 8 additions & 8 deletions bindings/rust/wasmedge-sys/src/instance/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ extern "C" fn wraper_fn(
#[derive(Debug)]
pub struct Function {
pub(crate) ctx: *mut wasmedge::WasmEdge_FunctionInstanceContext,
pub(crate) registed: bool,
ty: Option<Type>,
pub(crate) registered: bool,
pub(crate) ty: Option<FuncType>,
}

impl Function {
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Function {
});

let key_ptr = key as *const usize as *mut c_void;
let ty = Type::create(I::parameters(), O::parameters());
let ty = FuncType::create(I::parameters(), O::parameters());

let ctx = unsafe {
wasmedge::WasmEdge_FunctionInstanceCreateBinding(
Expand All @@ -113,26 +113,26 @@ impl Function {
Self {
ctx,
ty: Some(ty),
registed: false,
registered: false,
}
}
}

impl Drop for Function {
fn drop(&mut self) {
self.ty = None;
if !self.registed {
if !self.registered && !self.ctx.is_null() {
unsafe { wasmedge::WasmEdge_FunctionInstanceDelete(self.ctx) };
}
}
}

#[derive(Debug)]
pub struct Type {
pub struct FuncType {
pub(crate) ctx: *mut wasmedge::WasmEdge_FunctionTypeContext,
}

impl Type {
impl FuncType {
pub(crate) fn create(input: Vec<Value>, output: Vec<Value>) -> Self {
let raw_input = {
let mut head = vec![wasmedge::WasmEdge_ValType_ExternRef];
Expand Down Expand Up @@ -160,7 +160,7 @@ impl Type {
}
}

impl Drop for Type {
impl Drop for FuncType {
fn drop(&mut self) {
unsafe { wasmedge::WasmEdge_FunctionTypeDelete(self.ctx) };
}
Expand Down
67 changes: 65 additions & 2 deletions bindings/rust/wasmedge-sys/src/instance/global.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,74 @@
use crate::wasmedge;
use crate::{
types::{Mutability, ValType},
Value,
};

#[derive(Debug)]
pub struct Global {
ctx: *mut wasmedge::WasmEdge_GlobalInstanceContext,
pub(crate) ctx: *mut wasmedge::WasmEdge_GlobalInstanceContext,
pub(crate) registered: bool,
}
impl Global {
pub fn create(ty: &mut GlobalType, val: Value) -> Option<Self> {
let ctx = unsafe {
wasmedge::WasmEdge_GlobalInstanceCreate(ty.ctx, wasmedge::WasmEdge_Value::from(val))
};
ty.registered = true;
match ctx.is_null() {
true => None,
false => Some(Self {
ctx,
registered: false,
}),
}
}

pub fn get_value(&self) -> Value {
let val = unsafe { wasmedge::WasmEdge_GlobalInstanceGetValue(self.ctx) };
Value::from(val)
}

pub fn set_value(&mut self, val: Value) {
unsafe {
wasmedge::WasmEdge_GlobalInstanceSetValue(self.ctx, wasmedge::WasmEdge_Value::from(val))
}
}
}
impl Drop for Global {
fn drop(&mut self) {
unsafe { wasmedge::WasmEdge_GlobalInstanceDelete(self.ctx) };
if !self.registered && !self.ctx.is_null() {
unsafe { wasmedge::WasmEdge_GlobalInstanceDelete(self.ctx) };
}
}
}

#[derive(Debug)]
pub struct GlobalType {
pub(crate) ctx: *mut wasmedge::WasmEdge_GlobalTypeContext,
pub(crate) registered: bool,
}
impl GlobalType {
pub fn create(val_ty: ValType, mutable: Mutability) -> Option<Self> {
let ctx = unsafe {
wasmedge::WasmEdge_GlobalTypeCreate(
wasmedge::WasmEdge_ValType::from(val_ty),
wasmedge::WasmEdge_Mutability::from(mutable),
)
};
match ctx.is_null() {
true => None,
false => Some(Self {
ctx,
registered: false,
}),
}
}
}
impl Drop for GlobalType {
fn drop(&mut self) {
if !self.registered && !self.ctx.is_null() {
unsafe { wasmedge::WasmEdge_GlobalTypeDelete(self.ctx) }
}
}
}
55 changes: 52 additions & 3 deletions bindings/rust/wasmedge-sys/src/instance/memory.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
use crate::types::Limit;
use crate::wasmedge;

#[derive(Debug)]
pub struct Memory {
ctx: *mut wasmedge::WasmEdge_MemoryInstanceContext,
pub(crate) ctx: *mut wasmedge::WasmEdge_MemoryInstanceContext,
pub(crate) registered: bool,
}
impl Memory {
pub fn create(limit: Limit) -> Option<Self> {
let mut mem_ty = MemType::create(limit)?;
let ctx = unsafe { wasmedge::WasmEdge_MemoryInstanceCreate(mem_ty.ctx) };
mem_ty.ctx = std::ptr::null_mut();
match ctx.is_null() {
true => None,
false => Some(Memory {
ctx,
registered: false,
}),
}
}
}

impl Drop for Memory {
fn drop(&mut self) {
unsafe { wasmedge::WasmEdge_MemoryInstanceDelete(self.ctx) };
if !self.registered && !self.ctx.is_null() {
unsafe { wasmedge::WasmEdge_MemoryInstanceDelete(self.ctx) };
}
}
}

#[derive(Debug)]
pub struct MemType {
pub(crate) ctx: *mut wasmedge::WasmEdge_MemoryTypeContext,
pub(crate) registered: bool,
}
impl MemType {
pub fn create(limit: Limit) -> Option<Self> {
let ctx =
unsafe { wasmedge::WasmEdge_MemoryTypeCreate(wasmedge::WasmEdge_Limit::from(limit)) };
match ctx.is_null() {
true => None,
false => Some(Self {
ctx,
registered: false,
}),
}
}

pub fn limit(&self) -> Limit {
let limit = unsafe { wasmedge::WasmEdge_MemoryTypeGetLimit(self.ctx) };
Limit::from(limit)
}
}
impl Drop for MemType {
fn drop(&mut self) {
if !self.registered && !self.ctx.is_null() {
unsafe { wasmedge::WasmEdge_MemoryTypeDelete(self.ctx) }
}
}
}
2 changes: 1 addition & 1 deletion bindings/rust/wasmedge-sys/src/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ pub mod memory;
pub mod table;

pub use function::Function;
pub use global::Global;
pub use global::{Global, GlobalType};
pub use memory::Memory;
pub use table::Table;
28 changes: 25 additions & 3 deletions bindings/rust/wasmedge-sys/src/instance/table.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
use crate::types::{Limit, WasmEdgeRefType};
use crate::wasmedge;

#[derive(Debug)]
pub struct Table {
ctx: *mut wasmedge::WasmEdge_TableInstanceContext,
pub(crate) ctx: *mut wasmedge::WasmEdge_TableInstanceContext,
pub(crate) registered: bool,
}
impl Table {
pub fn create(ref_type: WasmEdgeRefType, limit: Limit) -> Option<Self> {
let ctx = unsafe {
let table_ty = wasmedge::WasmEdge_TableTypeCreate(
wasmedge::WasmEdge_RefType::from(ref_type),
wasmedge::WasmEdge_Limit::from(limit),
);
wasmedge::WasmEdge_TableInstanceCreate(table_ty)
};
match ctx.is_null() {
true => None,
false => Some(Table {
ctx,
registered: false,
}),
}
}
}

impl Drop for Table {
fn drop(&mut self) {
unsafe { wasmedge::WasmEdge_TableInstanceDelete(self.ctx) };
if !self.registered && !self.ctx.is_null() {
unsafe { wasmedge::WasmEdge_TableInstanceDelete(self.ctx) };
}
}
}
1 change: 1 addition & 0 deletions bindings/rust/wasmedge-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod io;
pub mod module;
pub mod raw_result;
pub mod string;
pub mod types;
pub mod value;
pub mod version;
pub mod vm;
Expand Down
117 changes: 117 additions & 0 deletions bindings/rust/wasmedge-sys/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
use super::wasmedge;
pub type WasmEdgeProposal = wasmedge::WasmEdge_Proposal;
pub type HostRegistration = wasmedge::WasmEdge_HostRegistration;
pub type CompilerOptimizationLevel = wasmedge::WasmEdge_CompilerOptimizationLevel;
pub type HostFunc = wasmedge::WasmEdge_HostFunc_t;
pub type WrapFunc = wasmedge::WasmEdge_WrapFunc_t;

#[derive(Debug)]
pub enum WasmEdgeRefType {
FuncRef,
ExternRef,
}
impl From<WasmEdgeRefType> for wasmedge::WasmEdge_RefType {
fn from(ty: WasmEdgeRefType) -> Self {
match ty {
WasmEdgeRefType::FuncRef => wasmedge::WasmEdge_RefType_FuncRef,
WasmEdgeRefType::ExternRef => wasmedge::WasmEdge_RefType_ExternRef,
}
}
}

#[derive(Debug)]
pub struct Limit {
pub(crate) min: u32,
pub(crate) max: Option<u32>,
}
impl Limit {
pub fn create(min: u32, max: Option<u32>) -> Self {
Self { min, max }
}
}
impl From<Limit> for wasmedge::WasmEdge_Limit {
fn from(limit: Limit) -> Self {
match limit.max {
None => Self {
Min: limit.min,
HasMax: false,
Max: u32::MAX,
},
Some(max) => Self {
Min: limit.min,
HasMax: true,
Max: max,
},
}
}
}
impl From<wasmedge::WasmEdge_Limit> for Limit {
fn from(limit: wasmedge::WasmEdge_Limit) -> Self {
let max = match limit.HasMax {
true => Some(limit.Max),
false => None,
};
Self {
min: limit.Min,
max,
}
}
}

#[derive(Debug)]
pub enum ValType {
I32,
I64,
F32,
F64,
FuncRef,
ExternRef,
}
impl From<ValType> for wasmedge::WasmEdge_ValType {
fn from(ty: ValType) -> Self {
match ty {
ValType::I32 => wasmedge::WasmEdge_ValType_I32,
ValType::I64 => wasmedge::WasmEdge_ValType_I64,
ValType::F32 => wasmedge::WasmEdge_ValType_F32,
ValType::F64 => wasmedge::WasmEdge_ValType_F64,
ValType::FuncRef => wasmedge::WasmEdge_ValType_FuncRef,
ValType::ExternRef => wasmedge::WasmEdge_ValType_ExternRef,
}
}
}
impl From<wasmedge::WasmEdge_ValType> for ValType {
fn from(ty: wasmedge::WasmEdge_ValType) -> Self {
match ty {
wasmedge::WasmEdge_ValType_I32 => ValType::I32,
wasmedge::WasmEdge_ValType_I64 => ValType::I64,
wasmedge::WasmEdge_ValType_F32 => ValType::F32,
wasmedge::WasmEdge_ValType_F64 => ValType::F64,
wasmedge::WasmEdge_ValType_FuncRef => ValType::FuncRef,
wasmedge::WasmEdge_ValType_ExternRef => ValType::ExternRef,
_ => panic!("unknown WasmEdge_ValType `{}`", ty),
}
}
}

#[derive(Debug)]
pub enum Mutability {
Const,
Var,
}
impl From<Mutability> for wasmedge::WasmEdge_Mutability {
fn from(mutable: Mutability) -> Self {
match mutable {
Mutability::Const => wasmedge::WasmEdge_Mutability_Const,
Mutability::Var => wasmedge::WasmEdge_Mutability_Var,
}
}
}
impl From<wasmedge::WasmEdge_Mutability> for Mutability {
fn from(mutable: wasmedge::WasmEdge_Mutability) -> Self {
match mutable {
wasmedge::WasmEdge_Mutability_Const => Mutability::Const,
wasmedge::WasmEdge_Mutability_Var => Mutability::Var,
_ => panic!("unknown Mutability value `{}`", mutable),
}
}
}

0 comments on commit 279c015

Please sign in to comment.