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
use context::Context;
use holochain_core_types::error::{
HcResult, HolochainError, RibosomeErrorCode, RibosomeReturnCode,
};
use holochain_wasm_utils::memory_allocation::decode_encoded_allocation;
use nucleus::{
ribosome::{api::ZomeApiFunction, memory::SinglePageManager, Runtime},
ZomeFnCall, ZomeFnResult,
};
use std::{str::FromStr, sync::Arc};
use wasmi::{
self, Error as InterpreterError, FuncInstance, FuncRef, ImportsBuilder, ModuleImportResolver,
ModuleInstance, NopExternals, RuntimeValue, Signature, ValueType,
};
pub fn run_dna(
dna_name: &str,
context: Arc<Context>,
wasm: Vec<u8>,
zome_call: &ZomeFnCall,
parameters: Option<Vec<u8>>,
) -> ZomeFnResult {
let module = wasmi::Module::from_buffer(wasm).expect("wasm binary should be valid");
struct RuntimeModuleImportResolver;
impl ModuleImportResolver for RuntimeModuleImportResolver {
fn resolve_func(
&self,
field_name: &str,
_signature: &Signature,
) -> Result<FuncRef, InterpreterError> {
let api_fn = match ZomeApiFunction::from_str(&field_name) {
Ok(api_fn) => api_fn,
Err(_) => {
return Err(InterpreterError::Function(format!(
"host module doesn't export function with name {}",
field_name
)));
}
};
match api_fn {
ZomeApiFunction::Abort => Ok(FuncInstance::alloc_host(
Signature::new(
&[
ValueType::I32,
ValueType::I32,
ValueType::I32,
ValueType::I32,
][..],
None,
),
api_fn as usize,
)),
_ => Ok(FuncInstance::alloc_host(
Signature::new(&[ValueType::I32][..], Some(ValueType::I32)),
api_fn as usize,
)),
}
}
}
let mut imports = ImportsBuilder::new();
imports.push_resolver("env", &RuntimeModuleImportResolver);
let wasm_instance = ModuleInstance::new(&module, &imports)
.expect("Failed to instantiate module")
.run_start(&mut NopExternals)
.map_err(|_| HolochainError::RibosomeFailed("Module failed to start".to_string()))?;
let input_parameters: Vec<_> = parameters.unwrap_or_default();
let mut runtime = Runtime {
memory_manager: SinglePageManager::new(&wasm_instance),
context,
zome_call: zome_call.clone(),
dna_name: dna_name.to_string(),
};
let encoded_allocation_of_input: u32;
{
let mut_runtime = &mut runtime;
let maybe_allocation_of_input = mut_runtime.memory_manager.write(&input_parameters);
encoded_allocation_of_input = match maybe_allocation_of_input {
Err(RibosomeErrorCode::ZeroSizedAllocation) => 0,
Err(err) => {
return Err(HolochainError::RibosomeFailed(err.to_string()));
}
Ok(allocation_of_input) => allocation_of_input.encode(),
}
}
let returned_encoded_allocation: u32;
{
let mut_runtime = &mut runtime;
returned_encoded_allocation = wasm_instance
.invoke_export(
zome_call.fn_name.clone().as_str(),
&[RuntimeValue::I32(encoded_allocation_of_input as i32)],
mut_runtime,
)
.map_err(|err| HolochainError::RibosomeFailed(err.to_string()))?
.unwrap()
.try_into()
.unwrap();
}
let maybe_allocation = decode_encoded_allocation(returned_encoded_allocation);
let return_log_msg: String;
let return_result: HcResult<String>;
match maybe_allocation {
Err(return_code) => {
return_log_msg = return_code.to_string();
return_result = match return_code {
RibosomeReturnCode::Success => Ok(String::new()),
RibosomeReturnCode::Failure(err_code) => {
Err(HolochainError::RibosomeFailed(err_code.to_string()))
}
};
}
Ok(valid_allocation) => {
let result = runtime.memory_manager.read(valid_allocation);
let maybe_zome_result = String::from_utf8(result);
match maybe_zome_result {
Err(err) => {
return_log_msg = err.to_string();
return_result = Err(HolochainError::RibosomeFailed(err.to_string()));
}
Ok(json_str) => {
return_log_msg = json_str.clone();
return_result = Ok(json_str);
}
}
}
};
runtime
.context
.log(&format!(
"Zome Function '{}' returned: {}",
zome_call.fn_name, return_log_msg,
))
.expect("Logger should work");
return return_result;
}