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
use super::WType;
use crate::IType;
use wasmer_core::backend::SigRegistry;
use wasmer_core::module::ExportIndex;
use wasmer_core::vm::Ctx;
use wasmer_core::typed_func::WasmTypeList;
use wasmer_runtime::Func;
use wasmer_runtime::error::ResolveError;
use wasmer_runtime::types::LocalOrImport;
pub(super) unsafe fn get_export_func_by_name<'a, Args, Rets>(
ctx: &'a mut Ctx,
name: &str,
) -> Result<Func<'a, Args, Rets>, ResolveError>
where
Args: WasmTypeList,
Rets: WasmTypeList,
{
let module_inner = &(*ctx.module);
let export_index =
module_inner
.info
.exports
.get(name)
.ok_or_else(|| ResolveError::ExportNotFound {
name: name.to_string(),
})?;
let export_func_index = match export_index {
ExportIndex::Func(func_index) => func_index,
_ => {
return Err(ResolveError::ExportWrongType {
name: name.to_string(),
})
}
};
let export_func_signature_idx = *module_inner
.info
.func_assoc
.get(*export_func_index)
.expect("broken invariant, incorrect func index");
let export_func_signature = &module_inner.info.signatures[export_func_signature_idx];
let export_func_signature_ref = SigRegistry.lookup_signature_ref(export_func_signature);
if export_func_signature_ref.params() != Args::types()
|| export_func_signature_ref.returns() != Rets::types()
{
return Err(ResolveError::Signature {
expected: (*export_func_signature).clone(),
found: Args::types().to_vec(),
});
}
let func_wasm_inner = module_inner
.runnable_module
.get_trampoline(&module_inner.info, export_func_signature_idx)
.unwrap();
let export_func_ptr = match export_func_index.local_or_import(&module_inner.info) {
LocalOrImport::Local(local_func_index) => module_inner
.runnable_module
.get_func(&module_inner.info, local_func_index)
.unwrap(),
_ => {
return Err(ResolveError::ExportNotFound {
name: name.to_string(),
})
}
};
let typed_func: Func<'_, Args, Rets, wasmer_core::typed_func::Wasm> =
Func::from_raw_parts(func_wasm_inner, export_func_ptr, None, ctx as _);
Ok(typed_func)
}
pub(super) fn write_to_wasm_mem(context: &mut Ctx, address: usize, value: &[u8]) {
let memory = context.memory(0);
memory.view::<u8>()[address..(address + value.len())]
.iter()
.zip(value.iter())
.for_each(|(cell, byte)| cell.set(*byte));
}
pub(super) fn itypes_args_to_wtypes(itypes: &[IType]) -> Vec<WType> {
itypes
.iter()
.map(|itype| match itype {
IType::F32 => vec![WType::F32],
IType::F64 => vec![WType::F64],
IType::I64 | IType::U64 => vec![WType::I64],
IType::String | IType::Array(_) => vec![WType::I32, WType::I32],
_ => vec![WType::I32],
})
.flatten()
.collect::<Vec<_>>()
}
pub(super) fn itypes_output_to_wtypes(itypes: &[IType]) -> Vec<WType> {
itypes
.iter()
.map(|itype| match itype {
IType::F32 => vec![WType::F32],
IType::F64 => vec![WType::F64],
IType::I64 | IType::U64 => vec![WType::I64],
IType::String | IType::Array(_) | IType::Record(_) => vec![],
_ => vec![WType::I32],
})
.flatten()
.collect::<Vec<_>>()
}
#[macro_export]
macro_rules! init_wasm_func_once {
($func:ident, $ctx:ident, $args:ty, $rets:ty, $func_name:ident, $ret_error_code: expr) => {
if $func.borrow().is_none() {
let raw_func = match unsafe {
super::utils::get_export_func_by_name::<$args, $rets>($ctx, $func_name)
} {
Ok(func) => func,
Err(_) => return vec![WValue::I32($ret_error_code)],
};
unsafe {
let raw_func = std::mem::transmute::<Func<'_, _, _>, Func<'static, _, _>>(raw_func);
*$func.borrow_mut() = Some(raw_func);
}
}
};
}
#[macro_export]
macro_rules! call_wasm_func {
($func:ident, $arg:expr) => {
$func.borrow().as_ref().unwrap().call($arg).unwrap()
};
}