diff --git a/src/common/stack.rs b/src/common/stack.rs index 9479fc5..a96af35 100644 --- a/src/common/stack.rs +++ b/src/common/stack.rs @@ -1,7 +1,9 @@ +#[allow(unused_imports)] +use alloc::prelude::*; #[cfg(feature = "std")] use std::error; -use std::fmt; +use core::fmt; #[derive(Debug)] pub struct Error(String); diff --git a/src/func.rs b/src/func.rs index 10162fa..802f16c 100644 --- a/src/func.rs +++ b/src/func.rs @@ -1,5 +1,7 @@ -use std::rc::{Rc, Weak}; -use std::fmt; +#[allow(unused_imports)] +use alloc::prelude::*; +use alloc::rc::{Rc, Weak}; +use core::fmt; use parity_wasm::elements::Local; use {Trap, TrapKind, Signature}; use host::Externals; @@ -17,7 +19,7 @@ use isa; #[derive(Clone, Debug)] pub struct FuncRef(Rc); -impl ::std::ops::Deref for FuncRef { +impl ::core::ops::Deref for FuncRef { type Target = FuncInstance; fn deref(&self) -> &FuncInstance { &self.0 diff --git a/src/global.rs b/src/global.rs index aec71ed..fe7e276 100644 --- a/src/global.rs +++ b/src/global.rs @@ -1,5 +1,5 @@ -use std::rc::Rc; -use std::cell::Cell; +use alloc::rc::Rc; +use core::cell::Cell; use value::RuntimeValue; use Error; use types::ValueType; @@ -13,7 +13,7 @@ use parity_wasm::elements::{ValueType as EValueType}; #[derive(Clone, Debug)] pub struct GlobalRef(Rc); -impl ::std::ops::Deref for GlobalRef { +impl ::core::ops::Deref for GlobalRef { type Target = GlobalInstance; fn deref(&self) -> &GlobalInstance { &self.0 diff --git a/src/host.rs b/src/host.rs index 65393e8..745f207 100644 --- a/src/host.rs +++ b/src/host.rs @@ -1,4 +1,4 @@ -use std::any::TypeId; +use core::any::TypeId; use value::{RuntimeValue, FromRuntimeValue}; use {TrapKind, Trap}; @@ -98,7 +98,7 @@ impl<'a> RuntimeArgs<'a> { /// _ => panic!(), /// } /// ``` -pub trait HostError: 'static + ::std::fmt::Display + ::std::fmt::Debug + Send + Sync { +pub trait HostError: 'static + ::core::fmt::Display + ::core::fmt::Debug + Send + Sync { #[doc(hidden)] fn __private_get_type_id__(&self) -> TypeId { TypeId::of::() diff --git a/src/imports.rs b/src/imports.rs index 1459ff2..0ba8d2a 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -1,3 +1,5 @@ +#[allow(unused_imports)] +use alloc::prelude::*; use hashmap_core::HashMap; use global::GlobalRef; use memory::MemoryRef; diff --git a/src/isa.rs b/src/isa.rs index 39aaa9e..4af093c 100644 --- a/src/isa.rs +++ b/src/isa.rs @@ -67,6 +67,9 @@ //! - Reserved immediates are ignored for `call_indirect`, `current_memory`, `grow_memory`. //! +#[allow(unused_imports)] +use alloc::prelude::*; + /// Should we keep a value before "discarding" a stack frame? /// /// Note that this is a `enum` since Wasm doesn't support multiple return diff --git a/src/lib.rs b/src/lib.rs index 7c86318..ecf600c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -96,6 +96,21 @@ #![warn(missing_docs)] +#![cfg_attr(not(feature = "std"), no_std)] + +//// alloc is required in no_std +#![cfg_attr(not(feature = "std"), feature(alloc))] + +#[cfg(not(feature = "std"))] +#[macro_use] +extern crate alloc; +#[cfg(feature = "std")] +extern crate std as alloc; + +#[cfg(feature = "std")] +#[macro_use] +extern crate core; + #[cfg(test)] extern crate wabt; #[cfg(test)] @@ -109,7 +124,9 @@ extern crate memory_units as memory_units_crate; pub extern crate nan_preserving_float; -use std::fmt; +#[allow(unused_imports)] +use alloc::prelude::*; +use core::fmt; #[cfg(feature = "std")] use std::error; diff --git a/src/memory.rs b/src/memory.rs index 0854d2a..b126516 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -1,9 +1,11 @@ -use std::u32; -use std::ops::Range; -use std::cmp; -use std::fmt; -use std::rc::Rc; -use std::cell::{Cell, RefCell}; +#[allow(unused_imports)] +use alloc::prelude::*; +use alloc::rc::Rc; +use core::u32; +use core::ops::Range; +use core::cmp; +use core::fmt; +use core::cell::{Cell, RefCell}; use parity_wasm::elements::ResizableLimits; use Error; use memory_units::{RoundUpTo, Pages, Bytes}; @@ -28,7 +30,7 @@ const LINEAR_MEMORY_MAX_PAGES: Pages = Pages(65536); #[derive(Clone, Debug)] pub struct MemoryRef(Rc); -impl ::std::ops::Deref for MemoryRef { +impl ::core::ops::Deref for MemoryRef { type Target = MemoryInstance; fn deref(&self) -> &MemoryInstance { &self.0 @@ -172,7 +174,7 @@ impl MemoryInstance { /// Get value from memory at given offset. pub fn get_value(&self, offset: u32) -> Result { let mut buffer = self.buffer.borrow_mut(); - let region = self.checked_region(&mut buffer, offset as usize, ::std::mem::size_of::())?; + let region = self.checked_region(&mut buffer, offset as usize, ::core::mem::size_of::())?; Ok(T::from_little_endian(&buffer[region.range()]).expect("Slice size is checked")) } @@ -216,7 +218,7 @@ impl MemoryInstance { /// Copy value in the memory at given offset. pub fn set_value(&self, offset: u32, value: T) -> Result<(), Error> { let mut buffer = self.buffer.borrow_mut(); - let range = self.checked_region(&mut buffer, offset as usize, ::std::mem::size_of::())?.range(); + let range = self.checked_region(&mut buffer, offset as usize, ::core::mem::size_of::())?.range(); value.into_little_endian(&mut buffer[range]); Ok(()) } @@ -254,7 +256,7 @@ impl MemoryInstance { } fn checked_region(&self, buffer: &mut B, offset: usize, size: usize) -> Result - where B: ::std::ops::DerefMut> + where B: ::core::ops::DerefMut> { let end = offset.checked_add(size) .ok_or_else(|| Error::Memory(format!("trying to access memory block of size {} from offset {}", size, offset)))?; @@ -275,7 +277,7 @@ impl MemoryInstance { fn checked_region_pair(&self, buffer: &mut B, offset1: usize, size1: usize, offset2: usize, size2: usize) -> Result<(CheckedRegion, CheckedRegion), Error> - where B: ::std::ops::DerefMut> + where B: ::core::ops::DerefMut> { let end1 = offset1.checked_add(size1) .ok_or_else(|| Error::Memory(format!("trying to access memory block of size {} from offset {}", size1, offset1)))?; @@ -314,7 +316,7 @@ impl MemoryInstance { let (read_region, write_region) = self.checked_region_pair(&mut buffer, src_offset, len, dst_offset, len)?; - unsafe { ::std::ptr::copy( + unsafe { ::core::ptr::copy( buffer[read_region.range()].as_ptr(), buffer[write_region.range()].as_ptr() as *mut _, len, @@ -343,7 +345,7 @@ impl MemoryInstance { return Err(Error::Memory(format!("non-overlapping copy is used for overlapping regions"))) } - unsafe { ::std::ptr::copy_nonoverlapping( + unsafe { ::core::ptr::copy_nonoverlapping( buffer[read_region.range()].as_ptr(), buffer[write_region.range()].as_ptr() as *mut _, len, diff --git a/src/module.rs b/src/module.rs index af56bb3..94c2ec2 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,8 +1,10 @@ +#[allow(unused_imports)] +use alloc::prelude::*; +use alloc::rc::Rc; use runner::check_function_args; use Trap; -use std::rc::Rc; -use std::cell::RefCell; -use std::fmt; +use core::cell::RefCell; +use core::fmt; use hashmap_core::HashMap; use parity_wasm::elements::{External, InitExpr, Internal, Instruction, ResizableLimits, Type}; use {Module, Error, Signature, MemoryInstance, RuntimeValue, TableInstance}; @@ -32,7 +34,7 @@ use memory_units::Pages; #[derive(Clone, Debug)] pub struct ModuleRef(pub(crate) Rc); -impl ::std::ops::Deref for ModuleRef { +impl ::core::ops::Deref for ModuleRef { type Target = ModuleInstance; fn deref(&self) -> &ModuleInstance { &self.0 diff --git a/src/runner.rs b/src/runner.rs index 5a9ec84..3f4d809 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -1,7 +1,9 @@ -use std::ops; -use std::{u32, usize}; -use std::fmt; -use std::iter::repeat; +#[allow(unused_imports)] +use alloc::prelude::*; +use core::ops; +use core::{u32, usize}; +use core::fmt; +use core::iter::repeat; use parity_wasm::elements::Local; use {Error, Trap, TrapKind, Signature}; use module::ModuleRef; @@ -19,7 +21,7 @@ use nan_preserving_float::{F32, F64}; use isa; /// Maximum number of entries in value stack. -pub const DEFAULT_VALUE_STACK_LIMIT: usize = (1024 * 1024) / ::std::mem::size_of::(); +pub const DEFAULT_VALUE_STACK_LIMIT: usize = (1024 * 1024) / ::core::mem::size_of::(); // TODO: Make these parameters changeble. pub const DEFAULT_CALL_STACK_LIMIT: usize = 64 * 1024; @@ -122,7 +124,7 @@ impl Interpreter { } pub fn resume_execution<'a, E: Externals + 'a>(&mut self, return_val: Option, externals: &'a mut E) -> Result, Trap> { - use std::mem::swap; + use core::mem::swap; // Ensure that the VM is resumable. This is checked in `FuncInvocation::resume_execution`. assert!(self.state.is_resumable()); diff --git a/src/table.rs b/src/table.rs index 53dcc10..0e42196 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1,7 +1,9 @@ -use std::u32; -use std::fmt; -use std::cell::RefCell; -use std::rc::Rc; +#[allow(unused_imports)] +use alloc::prelude::*; +use alloc::rc::Rc; +use core::u32; +use core::fmt; +use core::cell::RefCell; use parity_wasm::elements::ResizableLimits; use Error; use func::FuncRef; @@ -16,7 +18,7 @@ use module::check_limits; #[derive(Clone, Debug)] pub struct TableRef(Rc); -impl ::std::ops::Deref for TableRef { +impl ::core::ops::Deref for TableRef { type Target = TableInstance; fn deref(&self) -> &TableInstance { &self.0 diff --git a/src/tests/host.rs b/src/tests/host.rs index c0b5916..4ba09d2 100644 --- a/src/tests/host.rs +++ b/src/tests/host.rs @@ -12,8 +12,8 @@ struct HostErrorWithCode { error_code: u32, } -impl ::std::fmt::Display for HostErrorWithCode { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> { +impl ::core::fmt::Display for HostErrorWithCode { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> { write!(f, "{}", self.error_code) } } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 3ad8ba8..193e274 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -23,10 +23,10 @@ fn assert_error_properties() { fn unsigned_to_runtime_value() { use super::RuntimeValue; - let overflow_i32: u32 = ::std::i32::MAX as u32 + 1; + let overflow_i32: u32 = ::core::i32::MAX as u32 + 1; assert_eq!(RuntimeValue::from(overflow_i32).try_into::().unwrap(), overflow_i32); - let overflow_i64: u64 = ::std::i64::MAX as u64 + 1; + let overflow_i64: u64 = ::core::i64::MAX as u64 + 1; assert_eq!(RuntimeValue::from(overflow_i64).try_into::().unwrap(), overflow_i64); } diff --git a/src/types.rs b/src/types.rs index de5e673..6cf139a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,4 +1,4 @@ -use std::borrow::Cow; +use alloc::borrow::Cow; use parity_wasm::elements::{ FunctionType, ValueType as EValueType, GlobalType, TableType, MemoryType}; diff --git a/src/validation/context.rs b/src/validation/context.rs index 145ac18..19805e5 100644 --- a/src/validation/context.rs +++ b/src/validation/context.rs @@ -1,3 +1,5 @@ +#[allow(unused_imports)] +use alloc::prelude::*; use parity_wasm::elements::{MemoryType, TableType, GlobalType, BlockType, ValueType, FunctionType}; use validation::Error; diff --git a/src/validation/func.rs b/src/validation/func.rs index 7107e05..7c43bf7 100644 --- a/src/validation/func.rs +++ b/src/validation/func.rs @@ -1,4 +1,6 @@ -use std::u32; +#[allow(unused_imports)] +use alloc::prelude::*; +use core::u32; use parity_wasm::elements::{Instruction, BlockType, ValueType, TableElementType, Func, FuncBody}; use common::{DEFAULT_MEMORY_INDEX, DEFAULT_TABLE_INDEX}; use validation::context::ModuleContext; @@ -1726,7 +1728,7 @@ impl Sink { } fn emit_br_table(&mut self, targets: &[Target], default: Target) { - use std::iter; + use core::iter; let pc = self.cur_pc(); let mut isa_targets = Vec::new(); @@ -1757,7 +1759,7 @@ impl Sink { /// /// Panics if the label is already resolved. fn resolve_label(&mut self, label: LabelId) { - use std::mem; + use core::mem; if let (Label::Resolved(_), _) = self.labels[label.0] { panic!("Trying to resolve already resolved label"); diff --git a/src/validation/mod.rs b/src/validation/mod.rs index ed6f8ee..73794d8 100644 --- a/src/validation/mod.rs +++ b/src/validation/mod.rs @@ -1,6 +1,8 @@ +#[allow(unused_imports)] +use alloc::prelude::*; #[cfg(feature = "std")] use std::error; -use std::fmt; +use core::fmt; use hashmap_core::HashSet; use parity_wasm::elements::{ BlockType, External, GlobalEntry, GlobalType, Internal, MemoryType, Module, Instruction, @@ -47,7 +49,7 @@ pub struct ValidatedModule { pub module: Module, } -impl ::std::ops::Deref for ValidatedModule { +impl ::core::ops::Deref for ValidatedModule { type Target = Module; fn deref(&self) -> &Module { &self.module diff --git a/src/validation/util.rs b/src/validation/util.rs index acdec1b..69d70ac 100644 --- a/src/validation/util.rs +++ b/src/validation/util.rs @@ -1,3 +1,5 @@ +#[allow(unused_imports)] +use alloc::prelude::*; use parity_wasm::elements::{Local, ValueType}; use validation::Error; diff --git a/src/value.rs b/src/value.rs index bddc9f6..18253ac 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,7 +1,7 @@ use byteorder::{ByteOrder, LittleEndian, WriteBytesExt}; use nan_preserving_float::{F32, F64}; -use std::mem::transmute; -use std::{f32, i32, i64, u32, u64}; +use core::mem::transmute; +use core::{f32, i32, i64, u32, u64}; use TrapKind; #[derive(Debug)] @@ -780,7 +780,7 @@ macro_rules! impl_float { return round; } - use std::ops::Rem; + use core::ops::Rem; if round.rem(2.0) == 1.0 { self.floor() } else if round.rem(2.0) == -1.0 { @@ -795,7 +795,7 @@ macro_rules! impl_float { // This instruction corresponds to what is sometimes called "minNaN" in other languages. fn min(self, other: $type) -> $type { if self.is_nan() || other.is_nan() { - return ::std::$intermediate::NAN.into(); + return ::core::$intermediate::NAN.into(); } self.min(other) @@ -803,13 +803,13 @@ macro_rules! impl_float { // This instruction corresponds to what is sometimes called "maxNaN" in other languages. fn max(self, other: $type) -> $type { if self.is_nan() || other.is_nan() { - return ::std::$intermediate::NAN.into(); + return ::core::$intermediate::NAN.into(); } self.max(other) } fn copysign(self, other: $type) -> $type { - use std::mem::size_of; + use core::mem::size_of; if self.is_nan() { return self;