Merge pull request #184 from wasmerio/fix-compiler-no-std

fix(compiler) Make it work without `std`
This commit is contained in:
Syrus Akbary
2020-07-23 08:47:56 -07:00
committed by GitHub
4 changed files with 39 additions and 30 deletions

View File

@@ -1,7 +1,11 @@
use crate::lib::std::string::String;
#[cfg(feature = "std")]
use thiserror::Error;
// Compilation Errors
//
// If `std` feature is enable, we can't use `thiserror` until
// https://github.com/dtolnay/thiserror/pull/64 is merged.
/// The WebAssembly.CompileError object indicates an error during
/// WebAssembly decoding or validation.
@@ -9,26 +13,27 @@ use thiserror::Error;
/// This is based on the [Wasm Compile Error][compile-error] API.
///
/// [compiler-error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/CompileError
#[derive(Error, Debug)]
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum CompileError {
/// A Wasm translation error occured.
#[error("WebAssembly translation error: {0}")]
Wasm(#[from] WasmError),
#[cfg_attr(feature = "std", error("WebAssembly translation error: {0}"))]
Wasm(#[cfg_attr(feature = "std", from)] WasmError),
/// A compilation error occured.
#[error("Compilation error: {0}")]
#[cfg_attr(feature = "std", error("Compilation error: {0}"))]
Codegen(String),
/// The module did not pass validation.
#[error("Validation error: {0}")]
#[cfg_attr(feature = "std", error("Validation error: {0}"))]
Validate(String),
/// The compiler doesn't support a Wasm feature
#[error("Feature {0} is not yet supported")]
#[cfg_attr(feature = "std", error("Feature {0} is not yet supported"))]
UnsupportedFeature(String),
/// Insufficient resources available for execution.
#[error("Insufficient resources: {0}")]
#[cfg_attr(feature = "std", error("Insufficient resources: {0}"))]
Resource(String),
}
@@ -36,13 +41,17 @@ pub enum CompileError {
///
/// When a WebAssembly function can't be translated, one of these error codes will be returned
/// to describe the failure.
#[derive(Error, Debug)]
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum WasmError {
/// The input WebAssembly code is invalid.
///
/// This error code is used by a WebAssembly translator when it encounters invalid WebAssembly
/// code. This should never happen for validated WebAssembly code.
#[error("Invalid input WebAssembly code at offset {offset}: {message}")]
#[cfg_attr(
feature = "std",
error("Invalid input WebAssembly code at offset {offset}: {message}")
)]
InvalidWebAssembly {
/// A string describing the validation error.
message: String,
@@ -53,17 +62,27 @@ pub enum WasmError {
/// A feature used by the WebAssembly code is not supported by the embedding environment.
///
/// Embedding environments may have their own limitations and feature restrictions.
#[error("Unsupported feature: {0}")]
#[cfg_attr(feature = "std", error("Unsupported feature: {0}"))]
Unsupported(String),
/// An implementation limit was exceeded.
#[error("Implementation limit exceeded")]
#[cfg_attr(feature = "std", error("Implementation limit exceeded"))]
ImplLimitExceeded,
/// A generic error.
#[error("{0}")]
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
}
/// The error that can happen while parsing a `str`
/// to retrieve a [`CpuFeature`].
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum ParseCpuFeatureError {
/// The provided string feature doesn't exist
#[cfg_attr(feature = "std", error("CpuFeature {0} not recognized"))]
Missing(String),
}
/// A convenient alias for a `Result` that uses `WasmError` as the error type.
pub type WasmResult<T> = Result<T, WasmError>;

View File

@@ -38,15 +38,14 @@ extern crate alloc;
mod lib {
#[cfg(feature = "core")]
pub mod std {
#[macro_use]
pub use alloc::{borrow, boxed, string, vec, sync};
pub use alloc::{borrow, boxed, str, string, sync, vec};
pub use core::fmt;
pub use hashbrown as collections;
}
#[cfg(feature = "std")]
pub mod std {
pub use std::{borrow, boxed, collections, fmt, string, sync, vec};
pub use std::{borrow, boxed, collections, fmt, str, string, sync, vec};
}
}
@@ -70,7 +69,7 @@ mod sourceloc;
pub use crate::address_map::{FunctionAddressMap, InstructionAddressMap};
#[cfg(feature = "translator")]
pub use crate::compiler::{Compiler, CompilerConfig};
pub use crate::error::{CompileError, WasmError, WasmResult};
pub use crate::error::{CompileError, ParseCpuFeatureError, WasmError, WasmResult};
pub use crate::function::{
Compilation, CompiledFunction, CompiledFunctionFrameInfo, CustomSections, Dwarf, FunctionBody,
Functions,
@@ -82,7 +81,7 @@ pub use crate::section::{CustomSection, CustomSectionProtection, SectionBody, Se
pub use crate::sourceloc::SourceLoc;
pub use crate::target::{
Architecture, BinaryFormat, CallingConvention, CpuFeature, Endianness, OperatingSystem,
ParseCpuFeatureError, PointerWidth, Target, Triple,
PointerWidth, Target, Triple,
};
#[cfg(feature = "translator")]
pub use crate::translator::{

View File

@@ -1,6 +1,6 @@
use crate::lib::std::sync::Arc;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use wasm_common::entity::PrimaryMap;
use wasm_common::{Features, MemoryIndex, TableIndex};
use wasmer_vm::{MemoryStyle, ModuleInfo, TableStyle};

View File

@@ -1,12 +1,12 @@
//! Target configuration
use crate::error::ParseCpuFeatureError;
use crate::lib::std::str::FromStr;
use crate::lib::std::string::{String, ToString};
use enumset::{EnumSet, EnumSetType};
use std::str::FromStr;
use std::string::ToString;
pub use target_lexicon::{
Architecture, BinaryFormat, CallingConvention, Endianness, OperatingSystem, PointerWidth,
Triple,
};
use thiserror::Error;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use raw_cpuid::CpuId;
@@ -112,15 +112,6 @@ impl CpuFeature {
}
}
/// The error that can happen while parsing a `str`
/// to retrieve a [`CpuFeature`].
#[derive(Error, Debug)]
pub enum ParseCpuFeatureError {
/// The provided string feature doesn't exist
#[error("CpuFeature {0} not recognized")]
Missing(String),
}
// This options should map exactly the GCC options indicated
// here by architectures:
//