forked from WasmEdge/WasmEdge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rust][feature](types, function, global, memory, table): add types mo…
…dule and refactor relevant code Signed-off-by: Xin Liu <[email protected]>
- Loading branch information
Showing
8 changed files
with
270 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} | ||
} |