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
use super::Runtime;
use crate::exec::Ext;
use pwasm_utils::parity_wasm::elements::{FunctionType, ValueType};
use sp_sandbox::Value;
#[macro_use]
pub mod macros;
pub trait ConvertibleToWasm: Sized {
const VALUE_TYPE: ValueType;
type NativeType;
fn to_typed_value(self) -> Value;
fn from_typed_value(_: Value) -> Option<Self>;
}
impl ConvertibleToWasm for i32 {
type NativeType = i32;
const VALUE_TYPE: ValueType = ValueType::I32;
fn to_typed_value(self) -> Value {
Value::I32(self)
}
fn from_typed_value(v: Value) -> Option<Self> {
v.as_i32()
}
}
impl ConvertibleToWasm for u32 {
type NativeType = u32;
const VALUE_TYPE: ValueType = ValueType::I32;
fn to_typed_value(self) -> Value {
Value::I32(self as i32)
}
fn from_typed_value(v: Value) -> Option<Self> {
match v {
Value::I32(v) => Some(v as u32),
_ => None,
}
}
}
impl ConvertibleToWasm for u64 {
type NativeType = u64;
const VALUE_TYPE: ValueType = ValueType::I64;
fn to_typed_value(self) -> Value {
Value::I64(self as i64)
}
fn from_typed_value(v: Value) -> Option<Self> {
match v {
Value::I64(v) => Some(v as u64),
_ => None,
}
}
}
pub type HostFunc<E> = fn(
&mut Runtime<E>,
&[sp_sandbox::Value],
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError>;
pub trait FunctionImplProvider<E: Ext> {
fn impls<F: FnMut(&[u8], &[u8], HostFunc<E>)>(f: &mut F);
}
pub trait ImportSatisfyCheck {
fn can_satisfy(module: &[u8], name: &[u8], func_type: &FunctionType) -> bool;
}