1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#![forbid(rust_2018_idioms, future_incompatible, elided_lifetimes_in_paths)]
#![warn(
missing_debug_implementations,
trivial_casts,
trivial_numeric_casts,
missing_docs,
unreachable_pub,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
variant_size_differences
)]
mod ptr;
use std::{cell::Cell, error::Error, fmt, mem};
pub use self::ptr::*;
pub use witx_gen_macros::witx_gen;
pub trait WasiValue: Sized {
type NativeType: Copy + fmt::Debug;
fn from_native(native: Self::NativeType) -> Result<Self, WasiValueError<Self>>;
fn to_native(self) -> Self::NativeType;
}
#[doc(hidden)]
pub trait WasiLeafValue: WasiValue {
fn get_error_message(value: Self::NativeType) -> String;
}
impl<T: WasiValue> WasiLeafValue for T
where
T::NativeType: fmt::Display,
{
fn get_error_message(native: Self::NativeType) -> String {
format!(
"Could not convert the native value {} to {}.",
native,
std::any::type_name::<T>()
)
}
}
pub struct WasiValueError<T: WasiValue>(T::NativeType, String);
impl<T: WasiLeafValue> WasiValueError<T> {
#[doc(hidden)]
pub fn from_native(native: T::NativeType) -> Self {
WasiValueError(native, T::get_error_message(native))
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! try_from_native {
($n:expr, $i:expr) => {
match $crate::WasiValue::from_native($i) {
Ok(value) => value,
Err(error) => return Err($crate::WasiValueError::from_inner($n, error)),
}
};
}
impl<T: WasiValue> WasiValueError<T> {
#[doc(hidden)]
pub fn from_inner<U: WasiValue>(native: T::NativeType, inner: WasiValueError<U>) -> Self {
WasiValueError(native, inner.1)
}
#[doc(hidden)]
pub fn from_unknown(native: T::NativeType) -> Self {
WasiValueError(native, "Cannot convert unknown union value.".into())
}
pub fn as_native(self) -> T::NativeType {
self.0
}
}
impl<T: WasiValue> Clone for WasiValueError<T> {
fn clone(&self) -> Self {
Self(self.0, self.1.clone())
}
}
impl<T: WasiValue> fmt::Debug for WasiValueError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple("WasiValueError").field(&self.0).finish()
}
}
impl<T: WasiValue> fmt::Display for WasiValueError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
fmt,
"Could not convert the native value {} to {}.",
self.1,
std::any::type_name::<T>()
)
}
}
impl<T: WasiValue> Error for WasiValueError<T> {}
pub mod reexports {
#[doc(no_inline)]
pub use bitflags::bitflags;
#[doc(no_inline)]
pub use wasmer_runtime_core::{func, import::ImportObject, imports, memory::Memory, vm::Ctx};
}
pub trait WasmValue: fmt::Debug + Copy {
const SIZE: u32;
const ARRAY_OFFSET: u32;
fn read(mem: &[Cell<u8>]) -> Self;
fn write(self, mem: &[Cell<u8>]);
}
macro_rules! primitive_wasmvalue_impl {
($t:ty) => {
impl WasmValue for $t {
const SIZE: u32 = mem::size_of::<$t>() as u32;
const ARRAY_OFFSET: u32 = mem::size_of::<$t>() as u32;
fn read(mem: &[Cell<u8>]) -> Self {
const SIZE: usize = mem::size_of::<$t>();
assert_eq!(mem.len(), SIZE);
let mut bytes = [0u8; SIZE];
for i in 0..SIZE {
bytes[i] = mem[i].get();
}
Self::from_le_bytes(bytes)
}
fn write(self, mem: &[Cell<u8>]) {
const SIZE: usize = mem::size_of::<$t>();
assert_eq!(mem.len(), SIZE);
let bytes = self.to_le_bytes();
for i in 0..SIZE {
mem[i].set(bytes[i])
}
}
}
};
}
primitive_wasmvalue_impl!(u8);
primitive_wasmvalue_impl!(i8);
primitive_wasmvalue_impl!(u16);
primitive_wasmvalue_impl!(i16);
primitive_wasmvalue_impl!(u32);
primitive_wasmvalue_impl!(i32);
primitive_wasmvalue_impl!(u64);
primitive_wasmvalue_impl!(i64);
primitive_wasmvalue_impl!(f32);
primitive_wasmvalue_impl!(f64);