Library restructure (#104)

* Move wasmer-runtime to wasmer-runtime-core

* Add the runtime library

* Fix issue with macros using wasmer_runtime, fmt

* Make default compiler dependency optional

* Add instantiate and validate functions
This commit is contained in:
Brandon Fish
2019-01-22 13:02:06 -06:00
committed by Lachlan Sneff
parent 62b8e7cc2d
commit 74875ed554
129 changed files with 218 additions and 130 deletions

View File

@ -0,0 +1,46 @@
use super::{SliceMap, TypedIndex};
use std::{
marker::PhantomData,
mem,
ops::{Deref, DerefMut},
};
#[derive(Debug, Clone)]
pub struct BoxedMap<K, V>
where
K: TypedIndex,
{
elems: Box<[V]>,
_marker: PhantomData<K>,
}
impl<K, V> BoxedMap<K, V>
where
K: TypedIndex,
{
pub(in crate::structures) fn new(elems: Box<[V]>) -> Self {
Self {
elems,
_marker: PhantomData,
}
}
}
impl<K, V> Deref for BoxedMap<K, V>
where
K: TypedIndex,
{
type Target = SliceMap<K, V>;
fn deref(&self) -> &SliceMap<K, V> {
unsafe { mem::transmute::<&[V], _>(&*self.elems) }
}
}
impl<K, V> DerefMut for BoxedMap<K, V>
where
K: TypedIndex,
{
fn deref_mut(&mut self) -> &mut SliceMap<K, V> {
unsafe { mem::transmute::<&mut [V], _>(&mut *self.elems) }
}
}

View File

@ -0,0 +1,221 @@
use super::{BoxedMap, SliceMap, TypedIndex};
use std::{
iter::{self, Extend, FromIterator},
marker::PhantomData,
mem,
ops::{Deref, DerefMut},
slice, vec,
};
/// Dense item map
#[derive(Debug, Clone)]
pub struct Map<K, V>
where
K: TypedIndex,
{
elems: Vec<V>,
_marker: PhantomData<K>,
}
impl<K, V> Map<K, V>
where
K: TypedIndex,
{
pub fn new() -> Self {
Self {
elems: Vec::new(),
_marker: PhantomData,
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
elems: Vec::with_capacity(capacity),
_marker: PhantomData,
}
}
pub fn len(&self) -> usize {
self.elems.len()
}
pub fn push(&mut self, value: V) -> K {
let len = self.len();
self.elems.push(value);
K::new(len)
}
pub fn next_index(&self) -> K {
K::new(self.len())
}
pub fn reserve_exact(&mut self, size: usize) {
self.elems.reserve_exact(size);
}
pub fn into_boxed_map(self) -> BoxedMap<K, V> {
BoxedMap::new(self.elems.into_boxed_slice())
}
}
impl<K, V> Map<K, V>
where
K: TypedIndex,
V: Clone,
{
pub fn resize(&mut self, new_len: usize, value: V) {
self.elems.resize(new_len, value);
}
}
impl<K, V> Extend<V> for Map<K, V>
where
K: TypedIndex,
{
fn extend<I: IntoIterator<Item = V>>(&mut self, iter: I) {
self.elems.extend(iter);
}
}
impl<K, V> FromIterator<V> for Map<K, V>
where
K: TypedIndex,
{
fn from_iter<I: IntoIterator<Item = V>>(iter: I) -> Self {
let elems: Vec<V> = iter.into_iter().collect();
Self {
elems,
_marker: PhantomData,
}
}
}
impl<K, V> Deref for Map<K, V>
where
K: TypedIndex,
{
type Target = SliceMap<K, V>;
fn deref(&self) -> &SliceMap<K, V> {
unsafe { mem::transmute::<&[V], _>(self.elems.as_slice()) }
}
}
impl<K, V> DerefMut for Map<K, V>
where
K: TypedIndex,
{
fn deref_mut(&mut self) -> &mut SliceMap<K, V> {
unsafe { mem::transmute::<&mut [V], _>(self.elems.as_mut_slice()) }
}
}
pub struct IntoIter<K, V>
where
K: TypedIndex,
{
enumerated: iter::Enumerate<vec::IntoIter<V>>,
_marker: PhantomData<K>,
}
impl<K, V> IntoIter<K, V>
where
K: TypedIndex,
{
pub(in crate::structures) fn new(into_iter: vec::IntoIter<V>) -> Self {
Self {
enumerated: into_iter.enumerate(),
_marker: PhantomData,
}
}
}
impl<K, V> Iterator for IntoIter<K, V>
where
K: TypedIndex,
{
type Item = (K, V);
fn next(&mut self) -> Option<(K, V)> {
self.enumerated.next().map(|(i, v)| (K::new(i), v))
}
}
impl<K, V> IntoIterator for Map<K, V>
where
K: TypedIndex,
{
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self.elems.into_iter())
}
}
impl<'a, K, V> IntoIterator for &'a Map<K, V>
where
K: TypedIndex,
{
type Item = (K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
Iter::new(self.elems.iter())
}
}
impl<'a, K, V> IntoIterator for &'a mut Map<K, V>
where
K: TypedIndex,
{
type Item = (K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
IterMut::new(self.elems.iter_mut())
}
}
pub struct Iter<'a, K: TypedIndex, V: 'a> {
enumerated: iter::Enumerate<slice::Iter<'a, V>>,
_marker: PhantomData<K>,
}
impl<'a, K: TypedIndex, V: 'a> Iter<'a, K, V> {
pub(in crate::structures) fn new(iter: slice::Iter<'a, V>) -> Self {
Self {
enumerated: iter.enumerate(),
_marker: PhantomData,
}
}
}
impl<'a, K: TypedIndex, V: 'a> Iterator for Iter<'a, K, V> {
type Item = (K, &'a V);
fn next(&mut self) -> Option<Self::Item> {
self.enumerated.next().map(|(i, v)| (K::new(i), v))
}
}
pub struct IterMut<'a, K: TypedIndex, V: 'a> {
enumerated: iter::Enumerate<slice::IterMut<'a, V>>,
_marker: PhantomData<K>,
}
impl<'a, K: TypedIndex, V: 'a> IterMut<'a, K, V> {
pub(in crate::structures) fn new(iter: slice::IterMut<'a, V>) -> Self {
Self {
enumerated: iter.enumerate(),
_marker: PhantomData,
}
}
}
impl<'a, K: TypedIndex, V: 'a> Iterator for IterMut<'a, K, V> {
type Item = (K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> {
self.enumerated.next().map(|(i, v)| (K::new(i), v))
}
}

View File

@ -0,0 +1,14 @@
mod boxed;
mod map;
mod slice;
pub use self::boxed::BoxedMap;
pub use self::map::{Iter, IterMut, Map};
pub use self::slice::SliceMap;
pub trait TypedIndex {
#[doc(hidden)]
fn new(index: usize) -> Self;
#[doc(hidden)]
fn index(&self) -> usize;
}

View File

@ -0,0 +1,86 @@
#[derive(Debug, Clone)]
enum MonoVecInner<T> {
None,
Inline(T),
Heap(Vec<T>),
}
/// A type that can hold zero items,
/// one item, or many items.
#[derive(Debug, Clone)]
pub struct MonoVec<T> {
inner: MonoVecInner<T>,
}
impl<T> MonoVec<T> {
pub fn new() -> Self {
Self {
inner: MonoVecInner::None,
}
}
pub fn new_inline(item: T) -> Self {
Self {
inner: MonoVecInner::Inline(item),
}
}
pub fn with_capacity(capacity: usize) -> Self {
match capacity {
0 | 1 => Self::new(),
_ => Self {
inner: MonoVecInner::Heap(Vec::with_capacity(capacity)),
},
}
}
pub fn push(&mut self, item: T) {
let uninit = unsafe { mem::uninitialized() };
let prev = mem::replace(&mut self.inner, uninit);
let next = match prev {
MonoVecInner::None => MonoVecInner::Inline(item),
MonoVecInner::Inline(previous_item) => MonoVecInner::Heap(vec![previous_item, item]),
MonoVecInner::Heap(mut v) => {
v.push(item);
MonoVecInner::Heap(v)
}
};
let uninit = mem::replace(&mut self.inner, next);
mem::forget(uninit);
}
pub fn pop(&mut self) -> Option<T> {
match self.inner {
MonoVecInner::None => None,
MonoVecInner::Inline(ref mut item) => {
let uninit = unsafe { mem::uninitialized() };
let item = mem::replace(item, uninit);
let uninit = mem::replace(&mut self.inner, MonoVecInner::None);
mem::forget(uninit);
Some(item)
}
MonoVecInner::Heap(ref mut v) => v.pop(),
}
}
pub fn as_slice(&self) -> &[T] {
match self.inner {
MonoVecInner::None => unsafe {
slice::from_raw_parts(mem::align_of::<T>() as *const T, 0)
},
MonoVecInner::Inline(ref item) => slice::from_ref(item),
MonoVecInner::Heap(ref v) => &v[..],
}
}
pub fn as_slice_mut(&mut self) -> &mut [T] {
match self.inner {
MonoVecInner::None => unsafe {
slice::from_raw_parts_mut(mem::align_of::<T>() as *mut T, 0)
},
MonoVecInner::Inline(ref mut item) => slice::from_mut(item),
MonoVecInner::Heap(ref mut v) => &mut v[..],
}
}
}

View File

@ -0,0 +1,69 @@
use super::{Iter, IterMut, TypedIndex};
use std::{
marker::PhantomData,
ops::{Index, IndexMut},
};
/// This is a dynamically-sized slice
/// that can only be indexed by the
/// correct index type.
#[derive(Debug)]
pub struct SliceMap<K, V>
where
K: TypedIndex,
{
_marker: PhantomData<K>,
slice: [V],
}
impl<K, V> SliceMap<K, V>
where
K: TypedIndex,
{
pub fn get(&self, index: K) -> Option<&V> {
self.slice.get(index.index())
}
pub fn get_mut(&mut self, index: K) -> Option<&mut V> {
self.slice.get_mut(index.index())
}
pub fn len(&self) -> usize {
self.slice.len()
}
pub fn iter(&self) -> Iter<K, V> {
Iter::new(self.slice.iter())
}
pub fn iter_mut(&mut self) -> IterMut<K, V> {
IterMut::new(self.slice.iter_mut())
}
pub fn as_ptr(&self) -> *const V {
self as *const SliceMap<K, V> as *const V
}
pub fn as_mut_ptr(&mut self) -> *mut V {
self as *mut SliceMap<K, V> as *mut V
}
}
impl<K, V> Index<K> for SliceMap<K, V>
where
K: TypedIndex,
{
type Output = V;
fn index(&self, index: K) -> &V {
&self.slice[index.index()]
}
}
impl<K, V> IndexMut<K> for SliceMap<K, V>
where
K: TypedIndex,
{
fn index_mut(&mut self, index: K) -> &mut V {
&mut self.slice[index.index()]
}
}