Migrate back to std:: stylistically (#554)
* Migrate back to `std::` stylistically This commit moves away from idioms such as `alloc::` and `core::` as imports of standard data structures and types. Instead it migrates all crates to uniformly use `std::` for importing standard data structures and types. This also removes the `std` and `core` features from all crates to and removes any conditional checking for `feature = "std"` All of this support was previously added in #407 in an effort to make wasmtime/cranelift "`no_std` compatible". Unfortunately though this change comes at a cost: * The usage of `alloc` and `core` isn't idiomatic. Especially trying to dual between types like `HashMap` from `std` as well as from `hashbrown` causes imports to be surprising in some cases. * Unfortunately there was no CI check that crates were `no_std`, so none of them actually were. Many crates still imported from `std` or depended on crates that used `std`. It's important to note, however, that **this does not mean that wasmtime will not run in embedded environments**. The style of the code today and idioms aren't ready in Rust to support this degree of multiplexing and makes it somewhat difficult to keep up with the style of `wasmtime`. Instead it's intended that embedded runtime support will be added as necessary. Currently only `std` is necessary to build `wasmtime`, and platforms that natively need to execute `wasmtime` will need to use a Rust target that supports `std`. Note though that not all of `std` needs to be supported, but instead much of it could be configured off to return errors, and `wasmtime` would be configured to gracefully handle errors. The goal of this PR is to move `wasmtime` back to idiomatic usage of features/`std`/imports/etc and help development in the short-term. Long-term when platform concerns arise (if any) they can be addressed by moving back to `no_std` crates (but fixing the issues mentioned above) or ensuring that the target in Rust has `std` available. * Start filling out platform support doc
This commit is contained in:
committed by
Dan Gohman
parent
c423a1c2f0
commit
39e57e3e9a
@@ -1,8 +1,8 @@
|
||||
use crate::instance::InstanceHandle;
|
||||
use crate::vmcontext::{VMFunctionImport, VMGlobalImport, VMMemoryImport, VMTableImport};
|
||||
use crate::HashSet;
|
||||
use cranelift_entity::{BoxedSlice, PrimaryMap};
|
||||
use cranelift_wasm::{FuncIndex, GlobalIndex, MemoryIndex, TableIndex};
|
||||
use std::collections::HashSet;
|
||||
|
||||
/// Resolved import pointers.
|
||||
#[derive(Clone)]
|
||||
|
||||
@@ -15,16 +15,6 @@ use crate::vmcontext::{
|
||||
VMGlobalDefinition, VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMSharedSignatureIndex,
|
||||
VMTableDefinition, VMTableImport,
|
||||
};
|
||||
use crate::{HashMap, HashSet};
|
||||
use alloc::borrow::ToOwned;
|
||||
use alloc::boxed::Box;
|
||||
use alloc::rc::Rc;
|
||||
use alloc::string::{String, ToString};
|
||||
use core::any::Any;
|
||||
use core::borrow::Borrow;
|
||||
use core::cell::RefCell;
|
||||
use core::convert::TryFrom;
|
||||
use core::{mem, ptr, slice};
|
||||
use cranelift_entity::{BoxedSlice, EntityRef, PrimaryMap};
|
||||
use cranelift_wasm::{
|
||||
DefinedFuncIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex,
|
||||
@@ -32,6 +22,13 @@ use cranelift_wasm::{
|
||||
};
|
||||
use memoffset::offset_of;
|
||||
use more_asserts::assert_lt;
|
||||
use std::any::Any;
|
||||
use std::borrow::Borrow;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::convert::TryFrom;
|
||||
use std::rc::Rc;
|
||||
use std::{mem, ptr, slice};
|
||||
use thiserror::Error;
|
||||
use wasmtime_environ::{DataInitializer, Module, TableElements, VMOffsets};
|
||||
|
||||
@@ -613,10 +610,8 @@ impl Instance {
|
||||
}
|
||||
|
||||
pub(crate) fn lookup_global_export(&self, field: &str) -> Option<Export> {
|
||||
let cell: &RefCell<HashMap<alloc::string::String, core::option::Option<Export>>> =
|
||||
self.global_exports.borrow();
|
||||
let map: &mut HashMap<alloc::string::String, core::option::Option<Export>> =
|
||||
&mut cell.borrow_mut();
|
||||
let cell: &RefCell<HashMap<String, Option<Export>>> = self.global_exports.borrow();
|
||||
let map: &mut HashMap<String, Option<Export>> = &mut cell.borrow_mut();
|
||||
if let Some(Some(export)) = map.get(field) {
|
||||
return Some(export.clone());
|
||||
}
|
||||
@@ -790,11 +785,9 @@ impl InstanceHandle {
|
||||
|
||||
// Collect the exports for the global export map.
|
||||
for (field, decl) in &instance.module.exports {
|
||||
use crate::hash_map::Entry::*;
|
||||
let cell: &RefCell<HashMap<alloc::string::String, core::option::Option<Export>>> =
|
||||
instance.global_exports.borrow();
|
||||
let map: &mut HashMap<alloc::string::String, core::option::Option<Export>> =
|
||||
&mut cell.borrow_mut();
|
||||
use std::collections::hash_map::Entry::*;
|
||||
let cell: &RefCell<HashMap<String, Option<Export>>> = instance.global_exports.borrow();
|
||||
let map: &mut HashMap<String, Option<Export>> = &mut cell.borrow_mut();
|
||||
match map.entry(field.to_string()) {
|
||||
Vacant(entry) => {
|
||||
entry.insert(Some(lookup_by_declaration(
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
//! the __jit_debug_register_code() and __jit_debug_descriptor to register
|
||||
//! or unregister generated object images with debuggers.
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use alloc::vec::Vec;
|
||||
use core::ptr;
|
||||
use std::ptr;
|
||||
|
||||
#[repr(C)]
|
||||
struct JITCodeEntry {
|
||||
@@ -41,7 +39,7 @@ extern "C" fn __jit_debug_register_code() {
|
||||
// Hack to not allow inlining even when Rust wants to do it in release mode.
|
||||
let x = 3;
|
||||
unsafe {
|
||||
core::ptr::read_volatile(&x);
|
||||
std::ptr::read_volatile(&x);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#![allow(improper_ctypes)]
|
||||
#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates)]
|
||||
#![warn(unused_import_braces)]
|
||||
#![cfg_attr(feature = "std", deny(unstable_features))]
|
||||
#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))]
|
||||
#![cfg_attr(
|
||||
feature = "cargo-clippy",
|
||||
@@ -22,9 +21,6 @@
|
||||
clippy::use_self
|
||||
)
|
||||
)]
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
mod export;
|
||||
mod imports;
|
||||
@@ -56,15 +52,5 @@ pub use crate::vmcontext::{
|
||||
VMTableDefinition, VMTableImport,
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use hashbrown::{hash_map, HashMap, HashSet};
|
||||
#[cfg(feature = "std")]
|
||||
use std::collections::{hash_map, HashMap, HashSet};
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use spin::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
#[cfg(feature = "std")]
|
||||
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
/// Version number of this crate.
|
||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
@@ -4,9 +4,8 @@
|
||||
|
||||
use crate::mmap::Mmap;
|
||||
use crate::vmcontext::VMMemoryDefinition;
|
||||
use alloc::string::String;
|
||||
use core::convert::TryFrom;
|
||||
use more_asserts::{assert_ge, assert_le};
|
||||
use std::convert::TryFrom;
|
||||
use wasmtime_environ::{MemoryPlan, MemoryStyle, WASM_MAX_PAGES, WASM_PAGE_SIZE};
|
||||
|
||||
/// A linear memory instance.
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
//! Low-level abstraction for allocating and managing zero-filled pages
|
||||
//! of memory.
|
||||
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::vec::Vec;
|
||||
use core::ptr;
|
||||
use core::slice;
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
use libc;
|
||||
use more_asserts::assert_le;
|
||||
use more_asserts::assert_lt;
|
||||
use region;
|
||||
use std::io;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
|
||||
/// Round `size` up to the nearest multiple of `page_size`.
|
||||
fn round_up_to_page_size(size: usize, page_size: usize) -> usize {
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
//! signature checking.
|
||||
|
||||
use crate::vmcontext::VMSharedSignatureIndex;
|
||||
use crate::{hash_map, HashMap};
|
||||
use core::convert::TryFrom;
|
||||
use cranelift_codegen::ir;
|
||||
use more_asserts::{assert_lt, debug_assert_lt};
|
||||
use std::collections::{hash_map, HashMap};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
/// WebAssembly requires that the caller and callee signatures in an indirect
|
||||
/// call must match. To implement this efficiently, keep a registry of all
|
||||
@@ -30,12 +30,12 @@ impl SignatureRegistry {
|
||||
match self.signature_hash.entry(sig.clone()) {
|
||||
hash_map::Entry::Occupied(entry) => *entry.get(),
|
||||
hash_map::Entry::Vacant(entry) => {
|
||||
// Keep `signature_hash` len under 2**32 -- VMSharedSignatureIndex::new(core::u32::MAX)
|
||||
// Keep `signature_hash` len under 2**32 -- VMSharedSignatureIndex::new(std::u32::MAX)
|
||||
// is reserved for VMSharedSignatureIndex::default().
|
||||
debug_assert_lt!(
|
||||
len,
|
||||
core::u32::MAX as usize,
|
||||
"Invariant check: signature_hash.len() < core::u32::MAX"
|
||||
std::u32::MAX as usize,
|
||||
"Invariant check: signature_hash.len() < std::u32::MAX"
|
||||
);
|
||||
let sig_id = VMSharedSignatureIndex::new(u32::try_from(len).unwrap());
|
||||
entry.insert(sig_id);
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use crate::vmcontext::VMContext;
|
||||
use crate::RwLock;
|
||||
use core::borrow::{Borrow, BorrowMut};
|
||||
use core::cell::Cell;
|
||||
use lazy_static::lazy_static;
|
||||
use std::borrow::{Borrow, BorrowMut};
|
||||
use std::cell::Cell;
|
||||
use std::sync::RwLock;
|
||||
|
||||
#[derive(Default)]
|
||||
struct TrapContext {
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
//! `Table` is to WebAssembly tables what `LinearMemory` is to WebAssembly linear memories.
|
||||
|
||||
use crate::vmcontext::{VMCallerCheckedAnyfunc, VMTableDefinition};
|
||||
use alloc::vec::Vec;
|
||||
use core::convert::{TryFrom, TryInto};
|
||||
use cranelift_wasm::TableElementType;
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use wasmtime_environ::{TablePlan, TableStyle};
|
||||
|
||||
/// A table instance.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::HashMap;
|
||||
use crate::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use cranelift_codegen::ir;
|
||||
use lazy_static::lazy_static;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
lazy_static! {
|
||||
static ref REGISTRY: RwLock<TrapRegistry> = RwLock::new(TrapRegistry::default());
|
||||
|
||||
@@ -4,10 +4,9 @@
|
||||
use crate::trap_registry::get_trap_registry;
|
||||
use crate::trap_registry::TrapDescription;
|
||||
use crate::vmcontext::{VMContext, VMFunctionBody};
|
||||
use alloc::string::{String, ToString};
|
||||
use core::cell::Cell;
|
||||
use core::ptr;
|
||||
use cranelift_codegen::ir;
|
||||
use std::cell::Cell;
|
||||
use std::ptr;
|
||||
|
||||
extern "C" {
|
||||
fn WasmtimeCallTrampoline(
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
//! fields that compiled wasm code accesses directly.
|
||||
|
||||
use crate::instance::Instance;
|
||||
use core::any::Any;
|
||||
use core::{ptr, u32};
|
||||
use std::any::Any;
|
||||
use std::{ptr, u32};
|
||||
use wasmtime_environ::BuiltinFunctionIndex;
|
||||
|
||||
/// An imported function.
|
||||
@@ -20,8 +20,8 @@ pub struct VMFunctionImport {
|
||||
#[cfg(test)]
|
||||
mod test_vmfunction_import {
|
||||
use super::VMFunctionImport;
|
||||
use core::mem::size_of;
|
||||
use memoffset::offset_of;
|
||||
use std::mem::size_of;
|
||||
use wasmtime_environ::{Module, VMOffsets};
|
||||
|
||||
#[test]
|
||||
@@ -53,7 +53,7 @@ pub struct VMFunctionBody(u8);
|
||||
#[cfg(test)]
|
||||
mod test_vmfunction_body {
|
||||
use super::VMFunctionBody;
|
||||
use core::mem::size_of;
|
||||
use std::mem::size_of;
|
||||
|
||||
#[test]
|
||||
fn check_vmfunction_body_offsets() {
|
||||
@@ -76,8 +76,8 @@ pub struct VMTableImport {
|
||||
#[cfg(test)]
|
||||
mod test_vmtable_import {
|
||||
use super::VMTableImport;
|
||||
use core::mem::size_of;
|
||||
use memoffset::offset_of;
|
||||
use std::mem::size_of;
|
||||
use wasmtime_environ::{Module, VMOffsets};
|
||||
|
||||
#[test]
|
||||
@@ -114,8 +114,8 @@ pub struct VMMemoryImport {
|
||||
#[cfg(test)]
|
||||
mod test_vmmemory_import {
|
||||
use super::VMMemoryImport;
|
||||
use core::mem::size_of;
|
||||
use memoffset::offset_of;
|
||||
use std::mem::size_of;
|
||||
use wasmtime_environ::{Module, VMOffsets};
|
||||
|
||||
#[test]
|
||||
@@ -149,8 +149,8 @@ pub struct VMGlobalImport {
|
||||
#[cfg(test)]
|
||||
mod test_vmglobal_import {
|
||||
use super::VMGlobalImport;
|
||||
use core::mem::size_of;
|
||||
use memoffset::offset_of;
|
||||
use std::mem::size_of;
|
||||
use wasmtime_environ::{Module, VMOffsets};
|
||||
|
||||
#[test]
|
||||
@@ -184,8 +184,8 @@ pub struct VMMemoryDefinition {
|
||||
#[cfg(test)]
|
||||
mod test_vmmemory_definition {
|
||||
use super::VMMemoryDefinition;
|
||||
use core::mem::size_of;
|
||||
use memoffset::offset_of;
|
||||
use std::mem::size_of;
|
||||
use wasmtime_environ::{Module, VMOffsets};
|
||||
|
||||
#[test]
|
||||
@@ -228,8 +228,8 @@ pub struct VMTableDefinition {
|
||||
#[cfg(test)]
|
||||
mod test_vmtable_definition {
|
||||
use super::VMTableDefinition;
|
||||
use core::mem::size_of;
|
||||
use memoffset::offset_of;
|
||||
use std::mem::size_of;
|
||||
use wasmtime_environ::{Module, VMOffsets};
|
||||
|
||||
#[test]
|
||||
@@ -265,8 +265,8 @@ pub struct VMGlobalDefinition {
|
||||
#[cfg(test)]
|
||||
mod test_vmglobal_definition {
|
||||
use super::VMGlobalDefinition;
|
||||
use core::mem::{align_of, size_of};
|
||||
use more_asserts::assert_ge;
|
||||
use std::mem::{align_of, size_of};
|
||||
use wasmtime_environ::{Module, VMOffsets};
|
||||
|
||||
#[test]
|
||||
@@ -432,7 +432,7 @@ pub struct VMSharedSignatureIndex(u32);
|
||||
#[cfg(test)]
|
||||
mod test_vmshared_signature_index {
|
||||
use super::VMSharedSignatureIndex;
|
||||
use core::mem::size_of;
|
||||
use std::mem::size_of;
|
||||
use wasmtime_environ::{Module, TargetSharedSignatureIndex, VMOffsets};
|
||||
|
||||
#[test]
|
||||
@@ -485,8 +485,8 @@ pub struct VMCallerCheckedAnyfunc {
|
||||
#[cfg(test)]
|
||||
mod test_vmcaller_checked_anyfunc {
|
||||
use super::VMCallerCheckedAnyfunc;
|
||||
use core::mem::size_of;
|
||||
use memoffset::offset_of;
|
||||
use std::mem::size_of;
|
||||
use wasmtime_environ::{Module, VMOffsets};
|
||||
|
||||
#[test]
|
||||
@@ -559,7 +559,7 @@ pub struct VMInvokeArgument([u8; 16]);
|
||||
#[cfg(test)]
|
||||
mod test_vm_invoke_argument {
|
||||
use super::VMInvokeArgument;
|
||||
use core::mem::{align_of, size_of};
|
||||
use std::mem::{align_of, size_of};
|
||||
use wasmtime_environ::{Module, VMOffsets};
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user