mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 05:51:02 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -21,8 +21,8 @@ use sc_executor_common::error::WasmError;
|
||||
use sp_wasm_interface::{Function, ValueType};
|
||||
use std::any::Any;
|
||||
use wasmtime::{
|
||||
Extern, ExternType, Func, FuncType, ImportType, Limits, Memory, MemoryType, Module,
|
||||
Trap, Val, Store,
|
||||
Extern, ExternType, Func, FuncType, ImportType, Limits, Memory, MemoryType, Module, Store,
|
||||
Trap, Val,
|
||||
};
|
||||
|
||||
pub struct Imports {
|
||||
@@ -51,36 +51,29 @@ pub fn resolve_imports(
|
||||
"host doesn't provide any imports from non-env module: {}:{}",
|
||||
import_ty.module(),
|
||||
name,
|
||||
)));
|
||||
)))
|
||||
}
|
||||
|
||||
let resolved = match name {
|
||||
"memory" => {
|
||||
memory_import_index = Some(externs.len());
|
||||
resolve_memory_import(store, &import_ty, heap_pages)?
|
||||
}
|
||||
_ => resolve_func_import(
|
||||
store,
|
||||
&import_ty,
|
||||
host_functions,
|
||||
allow_missing_func_imports,
|
||||
)?,
|
||||
},
|
||||
_ =>
|
||||
resolve_func_import(store, &import_ty, host_functions, allow_missing_func_imports)?,
|
||||
};
|
||||
externs.push(resolved);
|
||||
}
|
||||
Ok(Imports {
|
||||
memory_import_index,
|
||||
externs,
|
||||
})
|
||||
Ok(Imports { memory_import_index, externs })
|
||||
}
|
||||
|
||||
/// When the module linking proposal is supported the import's name can be `None`.
|
||||
/// Because we are not using this proposal we could safely unwrap the name.
|
||||
/// However, we opt for an error in order to avoid panics at all costs.
|
||||
fn import_name<'a, 'b: 'a>(import: &'a ImportType<'b>) -> Result<&'a str, WasmError> {
|
||||
let name = import.name().ok_or_else(||
|
||||
let name = import.name().ok_or_else(|| {
|
||||
WasmError::Other("The module linking proposal is not supported.".to_owned())
|
||||
)?;
|
||||
})?;
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
@@ -91,21 +84,17 @@ fn resolve_memory_import(
|
||||
) -> Result<Extern, WasmError> {
|
||||
let requested_memory_ty = match import_ty.ty() {
|
||||
ExternType::Memory(memory_ty) => memory_ty,
|
||||
_ => {
|
||||
_ =>
|
||||
return Err(WasmError::Other(format!(
|
||||
"this import must be of memory type: {}:{}",
|
||||
import_ty.module(),
|
||||
import_name(&import_ty)?,
|
||||
)))
|
||||
}
|
||||
))),
|
||||
};
|
||||
|
||||
// Increment the min (a.k.a initial) number of pages by `heap_pages` and check if it exceeds the
|
||||
// maximum specified by the import.
|
||||
let initial = requested_memory_ty
|
||||
.limits()
|
||||
.min()
|
||||
.saturating_add(heap_pages);
|
||||
let initial = requested_memory_ty.limits().min().saturating_add(heap_pages);
|
||||
if let Some(max) = requested_memory_ty.limits().max() {
|
||||
if initial > max {
|
||||
return Err(WasmError::Other(format!(
|
||||
@@ -113,7 +102,7 @@ fn resolve_memory_import(
|
||||
by the runtime wasm module {}",
|
||||
initial,
|
||||
max,
|
||||
)));
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,37 +126,31 @@ fn resolve_func_import(
|
||||
|
||||
let func_ty = match import_ty.ty() {
|
||||
ExternType::Func(func_ty) => func_ty,
|
||||
_ => {
|
||||
_ =>
|
||||
return Err(WasmError::Other(format!(
|
||||
"host doesn't provide any non function imports besides 'memory': {}:{}",
|
||||
import_ty.module(),
|
||||
name,
|
||||
)));
|
||||
}
|
||||
))),
|
||||
};
|
||||
|
||||
let host_func = match host_functions
|
||||
.iter()
|
||||
.find(|host_func| host_func.name() == name)
|
||||
{
|
||||
let host_func = match host_functions.iter().find(|host_func| host_func.name() == name) {
|
||||
Some(host_func) => host_func,
|
||||
None if allow_missing_func_imports => {
|
||||
return Ok(MissingHostFuncHandler::new(import_ty)?.into_extern(store, &func_ty));
|
||||
}
|
||||
None => {
|
||||
None if allow_missing_func_imports =>
|
||||
return Ok(MissingHostFuncHandler::new(import_ty)?.into_extern(store, &func_ty)),
|
||||
None =>
|
||||
return Err(WasmError::Other(format!(
|
||||
"host doesn't provide such function: {}:{}",
|
||||
import_ty.module(),
|
||||
name,
|
||||
)));
|
||||
}
|
||||
))),
|
||||
};
|
||||
if &func_ty != &wasmtime_func_sig(*host_func) {
|
||||
return Err(WasmError::Other(format!(
|
||||
"signature mismatch for: {}:{}",
|
||||
import_ty.module(),
|
||||
name,
|
||||
)));
|
||||
)))
|
||||
}
|
||||
|
||||
Ok(HostFuncHandler::new(*host_func).into_extern(store))
|
||||
@@ -218,7 +201,7 @@ fn call_static(
|
||||
);
|
||||
wasmtime_results[0] = util::into_wasmtime_val(ret_val);
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
Ok(None) => {
|
||||
debug_assert!(
|
||||
wasmtime_results.len() == 0,
|
||||
@@ -226,26 +209,22 @@ fn call_static(
|
||||
correspond to the number of results returned by the host function",
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
Err(msg) => Err(Trap::new(msg)),
|
||||
}
|
||||
}
|
||||
|
||||
impl HostFuncHandler {
|
||||
fn new(host_func: &'static dyn Function) -> Self {
|
||||
Self {
|
||||
host_func,
|
||||
}
|
||||
Self { host_func }
|
||||
}
|
||||
|
||||
fn into_extern(self, store: &Store) -> Extern {
|
||||
let host_func = self.host_func;
|
||||
let func_ty = wasmtime_func_sig(self.host_func);
|
||||
let func = Func::new(store, func_ty,
|
||||
move |_, params, result| {
|
||||
call_static(host_func, params, result)
|
||||
}
|
||||
);
|
||||
let func = Func::new(store, func_ty, move |_, params, result| {
|
||||
call_static(host_func, params, result)
|
||||
});
|
||||
Extern::Func(func)
|
||||
}
|
||||
}
|
||||
@@ -266,28 +245,17 @@ impl MissingHostFuncHandler {
|
||||
|
||||
fn into_extern(self, store: &Store, func_ty: &FuncType) -> Extern {
|
||||
let Self { module, name } = self;
|
||||
let func = Func::new(store, func_ty.clone(),
|
||||
move |_, _, _| Err(Trap::new(format!(
|
||||
"call to a missing function {}:{}",
|
||||
module, name
|
||||
)))
|
||||
);
|
||||
let func = Func::new(store, func_ty.clone(), move |_, _, _| {
|
||||
Err(Trap::new(format!("call to a missing function {}:{}", module, name)))
|
||||
});
|
||||
Extern::Func(func)
|
||||
}
|
||||
}
|
||||
|
||||
fn wasmtime_func_sig(func: &dyn Function) -> wasmtime::FuncType {
|
||||
let signature = func.signature();
|
||||
let params = signature
|
||||
.args
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(into_wasmtime_val_type);
|
||||
let results = signature
|
||||
.return_value
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(into_wasmtime_val_type);
|
||||
let params = signature.args.iter().cloned().map(into_wasmtime_val_type);
|
||||
let results = signature.return_value.iter().cloned().map(into_wasmtime_val_type);
|
||||
wasmtime::FuncType::new(params, results)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user