Browse Source

Use `anyhow::Error` in instantiation errors.

This commit updates the error enums used in instantiation errors to encapsulate
an `anyhow::Error` rather than a string.
pull/2518/head
Peter Huene 4 years ago
parent
commit
623290d42e
No known key found for this signature in database GPG Key ID: 1DD33E128C1F90D2
  1. 8
      crates/jit/src/instantiate.rs
  2. 4
      crates/jit/src/trampoline.rs
  3. 10
      crates/runtime/src/instance/allocator.rs
  4. 6
      crates/runtime/src/instance/allocator/pooling.rs

8
crates/jit/src/instantiate.rs

@ -133,9 +133,9 @@ impl CompilationArtifacts {
}
let obj = obj.write().map_err(|_| {
SetupError::Instantiate(InstantiationError::Resource(
"failed to create image memory".to_string(),
))
SetupError::Instantiate(InstantiationError::Resource(anyhow::anyhow!(
"failed to create image memory"
)))
})?;
Ok(CompilationArtifacts {
@ -236,7 +236,7 @@ impl CompiledModule {
&artifacts.unwind_info,
)
.map_err(|message| {
SetupError::Instantiate(InstantiationError::Resource(format!(
SetupError::Instantiate(InstantiationError::Resource(anyhow::anyhow!(
"failed to build code memory for functions: {}",
message
)))

4
crates/jit/src/trampoline.rs

@ -38,7 +38,9 @@ pub fn make_trampoline(
assert!(compiled_function.relocations.is_empty());
let ptr = code_memory
.allocate_for_function(&compiled_function)
.map_err(|message| SetupError::Instantiate(InstantiationError::Resource(message)))?
.map_err(|message| {
SetupError::Instantiate(InstantiationError::Resource(anyhow::anyhow!(message)))
})?
.as_ptr();
Ok(unsafe { std::mem::transmute::<*const VMFunctionBody, VMTrampoline>(ptr) })
}

10
crates/runtime/src/instance/allocator.rs

@ -71,7 +71,7 @@ pub struct LinkError(pub String);
pub enum InstantiationError {
/// Insufficient resources available for execution.
#[error("Insufficient resources: {0}")]
Resource(String),
Resource(anyhow::Error),
/// A wasm link error occured.
#[error("Failed to link module")]
@ -91,7 +91,7 @@ pub enum InstantiationError {
pub enum FiberStackError {
/// Insufficient resources available for the request.
#[error("Insufficient resources: {0}")]
Resource(String),
Resource(anyhow::Error),
/// An error for when the allocator doesn't support custom fiber stacks.
#[error("Custom fiber stacks are not supported by the allocator")]
NotSupported,
@ -569,10 +569,8 @@ impl OnDemandInstanceAllocator {
let mut memories: PrimaryMap<DefinedMemoryIndex, _> =
PrimaryMap::with_capacity(module.memory_plans.len() - num_imports);
for plan in &module.memory_plans.values().as_slice()[num_imports..] {
memories.push(
Memory::new_dynamic(plan, creator)
.map_err(|e| InstantiationError::Resource(e.to_string()))?,
);
memories
.push(Memory::new_dynamic(plan, creator).map_err(InstantiationError::Resource)?);
}
Ok(memories)
}

6
crates/runtime/src/instance/allocator/pooling.rs

@ -485,7 +485,7 @@ impl InstancePool {
max_pages,
commit_memory_pages,
)
.map_err(|e| InstantiationError::Resource(e.to_string()))?,
.map_err(InstantiationError::Resource)?,
);
}
@ -509,7 +509,7 @@ impl InstancePool {
let base = tables.next().unwrap();
commit_table_pages(base, max_elements as usize * mem::size_of::<*mut u8>())
.map_err(|e| InstantiationError::Resource(e.to_string()))?;
.map_err(InstantiationError::Resource)?;
instance
.tables
@ -785,7 +785,7 @@ impl StackPool {
.add((index * self.stack_size) + self.page_size);
commit_stack_pages(bottom_of_stack, size_without_guard)
.map_err(|e| FiberStackError::Resource(e.to_string()))?;
.map_err(FiberStackError::Resource)?;
// The top of the stack should be returned
Ok(bottom_of_stack.add(size_without_guard))

Loading…
Cancel
Save