mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-13 22:25:40 +00:00
Add NativeWasmType trait and modify WasmExternType
This commit is contained in:
parent
6478500b8f
commit
85d9ca9ad2
@ -2,7 +2,7 @@ use crate::{
|
|||||||
error::RuntimeError,
|
error::RuntimeError,
|
||||||
export::{Context, Export, FuncPointer},
|
export::{Context, Export, FuncPointer},
|
||||||
import::IsExport,
|
import::IsExport,
|
||||||
types::{FuncSig, Type, WasmExternType},
|
types::{FuncSig, NativeWasmType, Type, WasmExternType},
|
||||||
vm::{self, Ctx},
|
vm::{self, Ctx},
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
@ -135,10 +135,10 @@ where
|
|||||||
impl<Rets, E> TrapEarly<Rets> for Result<Rets, E>
|
impl<Rets, E> TrapEarly<Rets> for Result<Rets, E>
|
||||||
where
|
where
|
||||||
Rets: WasmTypeList,
|
Rets: WasmTypeList,
|
||||||
E: Any,
|
E: Into<Box<Any>> + 'static,
|
||||||
{
|
{
|
||||||
fn report(self) -> Result<Rets, Box<dyn Any>> {
|
fn report(self) -> Result<Rets, Box<dyn Any>> {
|
||||||
self.map_err(|err| Box::new(err) as Box<dyn Any>)
|
self.map_err(|err| err.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,22 +213,24 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
|
|||||||
type CStruct = S1<A>;
|
type CStruct = S1<A>;
|
||||||
type RetArray = [u64; 1];
|
type RetArray = [u64; 1];
|
||||||
fn from_ret_array(array: Self::RetArray) -> Self {
|
fn from_ret_array(array: Self::RetArray) -> Self {
|
||||||
(WasmExternType::from_bits(array[0]),)
|
(WasmExternType::from_native(NativeWasmType::from_bits(
|
||||||
|
array[0],
|
||||||
|
)),)
|
||||||
}
|
}
|
||||||
fn empty_ret_array() -> Self::RetArray {
|
fn empty_ret_array() -> Self::RetArray {
|
||||||
[0u64]
|
[0u64]
|
||||||
}
|
}
|
||||||
fn from_c_struct(c_struct: Self::CStruct) -> Self {
|
fn from_c_struct(c_struct: Self::CStruct) -> Self {
|
||||||
let S1(a) = c_struct;
|
let S1(a) = c_struct;
|
||||||
(a,)
|
(WasmExternType::from_native(a),)
|
||||||
}
|
}
|
||||||
fn into_c_struct(self) -> Self::CStruct {
|
fn into_c_struct(self) -> Self::CStruct {
|
||||||
#[allow(unused_parens, non_snake_case)]
|
#[allow(unused_parens, non_snake_case)]
|
||||||
let (a,) = self;
|
let (a,) = self;
|
||||||
S1(a)
|
S1(WasmExternType::to_native(a))
|
||||||
}
|
}
|
||||||
fn types() -> &'static [Type] {
|
fn types() -> &'static [Type] {
|
||||||
&[A::TYPE]
|
&[A::Native::TYPE]
|
||||||
}
|
}
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
unsafe fn call<Rets: WasmTypeList>(
|
unsafe fn call<Rets: WasmTypeList>(
|
||||||
@ -237,11 +239,8 @@ impl<A: WasmExternType> WasmTypeList for (A,) {
|
|||||||
wasm: Wasm,
|
wasm: Wasm,
|
||||||
ctx: *mut Ctx,
|
ctx: *mut Ctx,
|
||||||
) -> Result<Rets, WasmTrapInfo> {
|
) -> Result<Rets, WasmTrapInfo> {
|
||||||
// type Trampoline = extern "C" fn(*mut Ctx, *const c_void, *const u64, *mut u64);
|
|
||||||
// type Invoke = extern "C" fn(Trampoline, *mut Ctx, *const c_void, *const u64, *mut u64, &mut WasmTrapInfo) -> bool;
|
|
||||||
|
|
||||||
let (a,) = self;
|
let (a,) = self;
|
||||||
let args = [a.to_bits()];
|
let args = [a.to_native().to_bits()];
|
||||||
let mut rets = Rets::empty_ret_array();
|
let mut rets = Rets::empty_ret_array();
|
||||||
let mut trap = WasmTrapInfo::Unknown;
|
let mut trap = WasmTrapInfo::Unknown;
|
||||||
|
|
||||||
@ -277,7 +276,7 @@ where
|
|||||||
macro_rules! impl_traits {
|
macro_rules! impl_traits {
|
||||||
( [$repr:ident] $struct_name:ident, $( $x:ident ),* ) => {
|
( [$repr:ident] $struct_name:ident, $( $x:ident ),* ) => {
|
||||||
#[repr($repr)]
|
#[repr($repr)]
|
||||||
pub struct $struct_name <$( $x ),*> ( $( $x ),* );
|
pub struct $struct_name <$( $x: WasmExternType ),*> ( $( <$x as WasmExternType>::Native ),* );
|
||||||
|
|
||||||
impl< $( $x: WasmExternType, )* > WasmTypeList for ( $( $x ),* ) {
|
impl< $( $x: WasmExternType, )* > WasmTypeList for ( $( $x ),* ) {
|
||||||
type CStruct = $struct_name<$( $x ),*>;
|
type CStruct = $struct_name<$( $x ),*>;
|
||||||
@ -285,7 +284,7 @@ macro_rules! impl_traits {
|
|||||||
fn from_ret_array(array: Self::RetArray) -> Self {
|
fn from_ret_array(array: Self::RetArray) -> Self {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
let [ $( $x ),* ] = array;
|
let [ $( $x ),* ] = array;
|
||||||
( $( WasmExternType::from_bits($x) ),* )
|
( $( WasmExternType::from_native(NativeWasmType::from_bits($x)) ),* )
|
||||||
}
|
}
|
||||||
fn empty_ret_array() -> Self::RetArray {
|
fn empty_ret_array() -> Self::RetArray {
|
||||||
[0; count_idents!( $( $x ),* )]
|
[0; count_idents!( $( $x ),* )]
|
||||||
@ -293,24 +292,21 @@ macro_rules! impl_traits {
|
|||||||
fn from_c_struct(c_struct: Self::CStruct) -> Self {
|
fn from_c_struct(c_struct: Self::CStruct) -> Self {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
let $struct_name ( $( $x ),* ) = c_struct;
|
let $struct_name ( $( $x ),* ) = c_struct;
|
||||||
( $( $x ),* )
|
( $( WasmExternType::from_native($x) ),* )
|
||||||
}
|
}
|
||||||
fn into_c_struct(self) -> Self::CStruct {
|
fn into_c_struct(self) -> Self::CStruct {
|
||||||
#[allow(unused_parens, non_snake_case)]
|
#[allow(unused_parens, non_snake_case)]
|
||||||
let ( $( $x ),* ) = self;
|
let ( $( $x ),* ) = self;
|
||||||
$struct_name ( $( $x ),* )
|
$struct_name ( $( WasmExternType::to_native($x) ),* )
|
||||||
}
|
}
|
||||||
fn types() -> &'static [Type] {
|
fn types() -> &'static [Type] {
|
||||||
&[$( $x::TYPE, )*]
|
&[$( $x::Native::TYPE, )*]
|
||||||
}
|
}
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
unsafe fn call<Rets: WasmTypeList>(self, f: NonNull<vm::Func>, wasm: Wasm, ctx: *mut Ctx) -> Result<Rets, WasmTrapInfo> {
|
unsafe fn call<Rets: WasmTypeList>(self, f: NonNull<vm::Func>, wasm: Wasm, ctx: *mut Ctx) -> Result<Rets, WasmTrapInfo> {
|
||||||
// type Trampoline = extern "C" fn(*mut Ctx, *const c_void, *const u64, *mut u64);
|
|
||||||
// type Invoke = extern "C" fn(Trampoline, *mut Ctx, *const c_void, *const u64, *mut u64, &mut WasmTrapInfo) -> bool;
|
|
||||||
|
|
||||||
#[allow(unused_parens)]
|
#[allow(unused_parens)]
|
||||||
let ( $( $x ),* ) = self;
|
let ( $( $x ),* ) = self;
|
||||||
let args = [ $( $x.to_bits() ),* ];
|
let args = [ $( $x.to_native().to_bits() ),* ];
|
||||||
let mut rets = Rets::empty_ret_array();
|
let mut rets = Rets::empty_ret_array();
|
||||||
let mut trap = WasmTrapInfo::Unknown;
|
let mut trap = WasmTrapInfo::Unknown;
|
||||||
|
|
||||||
@ -319,12 +315,6 @@ macro_rules! impl_traits {
|
|||||||
} else {
|
} else {
|
||||||
Err(trap)
|
Err(trap)
|
||||||
}
|
}
|
||||||
|
|
||||||
// let f: extern fn(*mut Ctx $( ,$x )*) -> Rets::CStruct = mem::transmute(f);
|
|
||||||
// #[allow(unused_parens)]
|
|
||||||
// let ( $( $x ),* ) = self;
|
|
||||||
// let c_struct = f(ctx $( ,$x )*);
|
|
||||||
// Rets::from_c_struct(c_struct)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -335,11 +325,11 @@ macro_rules! impl_traits {
|
|||||||
|
|
||||||
/// This is required for the llvm backend to be able to unwind through this function.
|
/// This is required for the llvm backend to be able to unwind through this function.
|
||||||
#[cfg_attr(nightly, unwind(allowed))]
|
#[cfg_attr(nightly, unwind(allowed))]
|
||||||
extern fn wrap<$( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly<Rets>, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap>( ctx: &mut Ctx $( ,$x: $x )* ) -> Rets::CStruct {
|
extern fn wrap<$( $x: WasmExternType, )* Rets: WasmTypeList, Trap: TrapEarly<Rets>, FN: Fn( &mut Ctx $( ,$x )* ) -> Trap>( ctx: &mut Ctx $( ,$x: <$x as WasmExternType>::Native )* ) -> Rets::CStruct {
|
||||||
let f: FN = unsafe { mem::transmute_copy(&()) };
|
let f: FN = unsafe { mem::transmute_copy(&()) };
|
||||||
|
|
||||||
let err = match panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
let err = match panic::catch_unwind(panic::AssertUnwindSafe(|| {
|
||||||
let res = f( ctx $( ,$x )* ).report();
|
let res = f( ctx $( ,WasmExternType::from_native($x) )* ).report();
|
||||||
res
|
res
|
||||||
})) {
|
})) {
|
||||||
Ok(Ok(returns)) => return returns.into_c_struct(),
|
Ok(Ok(returns)) => return returns.into_c_struct(),
|
||||||
|
@ -71,103 +71,149 @@ impl From<f64> for Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe trait WasmExternType: Copy + Clone
|
pub unsafe trait NativeWasmType: Copy + Into<Value>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
const TYPE: Type;
|
const TYPE: Type;
|
||||||
|
fn from_bits(bits: u64) -> Self;
|
||||||
fn to_bits(self) -> u64;
|
fn to_bits(self) -> u64;
|
||||||
fn from_bits(n: u64) -> Self;
|
}
|
||||||
|
|
||||||
|
unsafe impl NativeWasmType for i32 {
|
||||||
|
const TYPE: Type = Type::I32;
|
||||||
|
fn from_bits(bits: u64) -> Self {
|
||||||
|
bits as _
|
||||||
|
}
|
||||||
|
fn to_bits(self) -> u64 {
|
||||||
|
self as _
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unsafe impl NativeWasmType for i64 {
|
||||||
|
const TYPE: Type = Type::I64;
|
||||||
|
fn from_bits(bits: u64) -> Self {
|
||||||
|
bits as _
|
||||||
|
}
|
||||||
|
fn to_bits(self) -> u64 {
|
||||||
|
self as _
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unsafe impl NativeWasmType for f32 {
|
||||||
|
const TYPE: Type = Type::F32;
|
||||||
|
fn from_bits(bits: u64) -> Self {
|
||||||
|
bits as _
|
||||||
|
}
|
||||||
|
fn to_bits(self) -> u64 {
|
||||||
|
self as _
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unsafe impl NativeWasmType for f64 {
|
||||||
|
const TYPE: Type = Type::F64;
|
||||||
|
fn from_bits(bits: u64) -> Self {
|
||||||
|
bits as _
|
||||||
|
}
|
||||||
|
fn to_bits(self) -> u64 {
|
||||||
|
self as _
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe trait WasmExternType: Copy
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
type Native: NativeWasmType;
|
||||||
|
fn from_native(native: Self::Native) -> Self;
|
||||||
|
fn to_native(self) -> Self::Native;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl WasmExternType for i8 {
|
unsafe impl WasmExternType for i8 {
|
||||||
const TYPE: Type = Type::I32;
|
type Native = i32;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self as u64
|
native as _
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
n as _
|
self as _
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl WasmExternType for u8 {
|
unsafe impl WasmExternType for u8 {
|
||||||
const TYPE: Type = Type::I32;
|
type Native = i32;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self as u64
|
native as _
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
n as _
|
self as _
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl WasmExternType for i16 {
|
unsafe impl WasmExternType for i16 {
|
||||||
const TYPE: Type = Type::I32;
|
type Native = i32;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self as u64
|
native as _
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
n as _
|
self as _
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl WasmExternType for u16 {
|
unsafe impl WasmExternType for u16 {
|
||||||
const TYPE: Type = Type::I32;
|
type Native = i32;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self as u64
|
native as _
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
n as _
|
self as _
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl WasmExternType for i32 {
|
unsafe impl WasmExternType for i32 {
|
||||||
const TYPE: Type = Type::I32;
|
type Native = i32;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self as u64
|
native
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
n as _
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl WasmExternType for u32 {
|
unsafe impl WasmExternType for u32 {
|
||||||
const TYPE: Type = Type::I32;
|
type Native = i32;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self as u64
|
native as _
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
n as _
|
self as _
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl WasmExternType for i64 {
|
unsafe impl WasmExternType for i64 {
|
||||||
const TYPE: Type = Type::I64;
|
type Native = i64;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self as u64
|
native
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
n as _
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl WasmExternType for u64 {
|
unsafe impl WasmExternType for u64 {
|
||||||
const TYPE: Type = Type::I64;
|
type Native = i64;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self
|
native as _
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
n
|
self as _
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl WasmExternType for f32 {
|
unsafe impl WasmExternType for f32 {
|
||||||
const TYPE: Type = Type::F32;
|
type Native = f32;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self.to_bits() as u64
|
native
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
f32::from_bits(n as u32)
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl WasmExternType for f64 {
|
unsafe impl WasmExternType for f64 {
|
||||||
const TYPE: Type = Type::F64;
|
type Native = f64;
|
||||||
fn to_bits(self) -> u64 {
|
fn from_native(native: Self::Native) -> Self {
|
||||||
self.to_bits()
|
native
|
||||||
}
|
}
|
||||||
fn from_bits(n: u64) -> Self {
|
fn to_native(self) -> Self::Native {
|
||||||
f64::from_bits(n)
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user