From 3ce5d2057dcb3ace77e2585e16234488098e62e7 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Mon, 29 Apr 2019 11:34:45 +0200 Subject: [PATCH] [wasm] Add the ability to provide a user-defined error; --- cranelift/wasm/Cargo.toml | 2 +- cranelift/wasm/src/environ/spec.rs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cranelift/wasm/Cargo.toml b/cranelift/wasm/Cargo.toml index 2309322acb..7d9504aa54 100644 --- a/cranelift/wasm/Cargo.toml +++ b/cranelift/wasm/Cargo.toml @@ -27,7 +27,7 @@ target-lexicon = "0.4.0" [features] default = ["std"] -std = ["cranelift-codegen/std", "cranelift-frontend/std", "wasmparser/std"] +std = ["cranelift-codegen/std", "cranelift-frontend/std", "wasmparser/std", "failure/std"] core = ["hashmap_core", "cranelift-codegen/core", "cranelift-frontend/core", "wasmparser/core"] [badges] diff --git a/cranelift/wasm/src/environ/spec.rs b/cranelift/wasm/src/environ/spec.rs index 3173a07886..ac58fd7215 100644 --- a/cranelift/wasm/src/environ/spec.rs +++ b/cranelift/wasm/src/environ/spec.rs @@ -39,7 +39,7 @@ pub enum GlobalVariable { /// /// When a WebAssembly function can't be translated, one of these error codes will be returned /// to describe the failure. -#[derive(Fail, Debug, PartialEq, Eq)] +#[derive(Fail, Debug)] pub enum WasmError { /// The input WebAssembly code is invalid. /// @@ -67,6 +67,18 @@ pub enum WasmError { /// [limits]: https://cranelift.readthedocs.io/en/latest/ir.html#implementation-limits #[fail(display = "Implementation limit exceeded")] ImplLimitExceeded, + + /// Any user-defined error. Requires an std build, where failure::Error is defined. + #[cfg(feature = "std")] + #[fail(display = "User error: {}", _0)] + User(failure::Error), +} + +#[cfg(feature = "std")] +impl From for WasmError { + fn from(err: failure::Error) -> Self { + WasmError::User(err) + } } impl From for WasmError {