Skip to content

Commit

Permalink
Use anyhow::Error in instantiation errors.
Browse files Browse the repository at this point in the history
This commit updates the error enums used in instantiation errors to encapsulate
an `anyhow::Error` rather than a string.
  • Loading branch information
peterhuene committed Mar 8, 2021
1 parent a4ab0db commit fdc1b09
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions crates/jit/src/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)))
Expand Down
4 changes: 3 additions & 1 deletion crates/jit/src/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
}
Expand Down
10 changes: 4 additions & 6 deletions crates/runtime/src/instance/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/runtime/src/instance/allocator/pooling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl InstancePool {
max_pages,
commit_memory_pages,
)
.map_err(|e| InstantiationError::Resource(e.to_string()))?,
.map_err(InstantiationError::Resource)?,
);
}

Expand All @@ -511,7 +511,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
Expand Down Expand Up @@ -787,7 +787,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))
Expand Down

0 comments on commit fdc1b09

Please sign in to comment.