mirror of
https://github.com/fluencelabs/wasmer
synced 2025-03-31 06:51:04 +00:00
Add Pages and Bytes newtypes
This commit is contained in:
parent
bc78738bb7
commit
69e9c5154d
@ -13,8 +13,6 @@ use wasmer_runtime_core::{
|
|||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
|
|
||||||
const WASM_PAGE_SIZE: usize = 65_536;
|
|
||||||
|
|
||||||
pub struct FuncEnv<'env, 'module, 'isa> {
|
pub struct FuncEnv<'env, 'module, 'isa> {
|
||||||
env: &'env ModuleEnv<'module, 'isa>,
|
env: &'env ModuleEnv<'module, 'isa>,
|
||||||
}
|
}
|
||||||
@ -219,7 +217,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
|
|||||||
|
|
||||||
func.create_heap(ir::HeapData {
|
func.create_heap(ir::HeapData {
|
||||||
base: local_memory_base,
|
base: local_memory_base,
|
||||||
min_size: ((description.minimum as u64) * (WASM_PAGE_SIZE as u64)).into(),
|
min_size: (description.minimum.bytes().0 as u64).into(),
|
||||||
offset_guard_size: mem_type.guard_size().into(),
|
offset_guard_size: mem_type.guard_size().into(),
|
||||||
style: ir::HeapStyle::Dynamic {
|
style: ir::HeapStyle::Dynamic {
|
||||||
bound_gv: local_memory_bound,
|
bound_gv: local_memory_bound,
|
||||||
@ -230,7 +228,7 @@ impl<'env, 'module, 'isa> FuncEnvironment for FuncEnv<'env, 'module, 'isa> {
|
|||||||
mem_type @ MemoryType::Static | mem_type @ MemoryType::SharedStatic => func
|
mem_type @ MemoryType::Static | mem_type @ MemoryType::SharedStatic => func
|
||||||
.create_heap(ir::HeapData {
|
.create_heap(ir::HeapData {
|
||||||
base: local_memory_base,
|
base: local_memory_base,
|
||||||
min_size: ((description.minimum as u64) * (WASM_PAGE_SIZE as u64)).into(),
|
min_size: (description.minimum.bytes().0 as u64).into(),
|
||||||
offset_guard_size: mem_type.guard_size().into(),
|
offset_guard_size: mem_type.guard_size().into(),
|
||||||
style: ir::HeapStyle::Static {
|
style: ir::HeapStyle::Static {
|
||||||
bound: mem_type.bounds().unwrap().into(),
|
bound: mem_type.bounds().unwrap().into(),
|
||||||
|
@ -14,6 +14,7 @@ use wasmer_runtime_core::{
|
|||||||
ElementType, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer,
|
ElementType, FuncSig, GlobalDescriptor, GlobalIndex, GlobalInit, Initializer,
|
||||||
LocalFuncIndex, LocalOrImport, MemoryDescriptor, SigIndex, TableDescriptor, Value,
|
LocalFuncIndex, LocalOrImport, MemoryDescriptor, SigIndex, TableDescriptor, Value,
|
||||||
},
|
},
|
||||||
|
units::Pages,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct ModuleEnv<'module, 'isa> {
|
pub struct ModuleEnv<'module, 'isa> {
|
||||||
@ -251,8 +252,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
|
|||||||
/// Declares a memory to the environment
|
/// Declares a memory to the environment
|
||||||
fn declare_memory(&mut self, memory: cranelift_wasm::Memory) {
|
fn declare_memory(&mut self, memory: cranelift_wasm::Memory) {
|
||||||
self.module.memories.push(MemoryDescriptor {
|
self.module.memories.push(MemoryDescriptor {
|
||||||
minimum: memory.minimum,
|
minimum: Pages(memory.minimum),
|
||||||
maximum: memory.maximum,
|
maximum: memory.maximum.map(|max| Pages(max)),
|
||||||
shared: memory.shared,
|
shared: memory.shared,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -270,8 +271,8 @@ impl<'module, 'isa, 'data> ModuleEnvironment<'data> for ModuleEnv<'module, 'isa>
|
|||||||
};
|
};
|
||||||
|
|
||||||
let memory = MemoryDescriptor {
|
let memory = MemoryDescriptor {
|
||||||
minimum: memory.minimum,
|
minimum: Pages(memory.minimum),
|
||||||
maximum: memory.maximum,
|
maximum: memory.maximum.map(|max| Pages(max)),
|
||||||
shared: memory.shared,
|
shared: memory.shared,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ use wasmer_runtime_core::{
|
|||||||
prelude::*,
|
prelude::*,
|
||||||
table::Table,
|
table::Table,
|
||||||
types::{ElementType, MemoryDescriptor, TableDescriptor, Value},
|
types::{ElementType, MemoryDescriptor, TableDescriptor, Value},
|
||||||
|
units::Pages,
|
||||||
};
|
};
|
||||||
|
|
||||||
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
|
static EXAMPLE_WASM: &'static [u8] = include_bytes!("simple.wasm");
|
||||||
@ -16,8 +17,8 @@ fn main() -> Result<()> {
|
|||||||
let inner_module = wasmer_runtime_core::compile_with(&wasm_binary, &CraneliftCompiler::new())?;
|
let inner_module = wasmer_runtime_core::compile_with(&wasm_binary, &CraneliftCompiler::new())?;
|
||||||
|
|
||||||
let memory = Memory::new(MemoryDescriptor {
|
let memory = Memory::new(MemoryDescriptor {
|
||||||
min: 1,
|
minimum: Pages(1),
|
||||||
max: Some(1),
|
maximum: Some(Pages(1)),
|
||||||
shared: false,
|
shared: false,
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -26,8 +27,8 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
let table = Table::new(TableDescriptor {
|
let table = Table::new(TableDescriptor {
|
||||||
element: ElementType::Anyfunc,
|
element: ElementType::Anyfunc,
|
||||||
min: 10,
|
minimum: 10,
|
||||||
max: None,
|
maximum: None,
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ use crate::{
|
|||||||
export::{Context, Export},
|
export::{Context, Export},
|
||||||
global::Global,
|
global::Global,
|
||||||
import::ImportObject,
|
import::ImportObject,
|
||||||
memory::{Memory, WASM_PAGE_SIZE},
|
memory::Memory,
|
||||||
module::{ImportName, ModuleInner},
|
module::{ImportName, ModuleInner},
|
||||||
structures::{BoxedMap, Map, SliceMap, TypedIndex},
|
structures::{BoxedMap, Map, SliceMap, TypedIndex},
|
||||||
table::Table,
|
table::Table,
|
||||||
@ -104,7 +104,7 @@ impl LocalBacking {
|
|||||||
LocalOrImport::Local(local_memory_index) => {
|
LocalOrImport::Local(local_memory_index) => {
|
||||||
let memory_desc = module.memories[local_memory_index];
|
let memory_desc = module.memories[local_memory_index];
|
||||||
let data_top = init_base + init.data.len();
|
let data_top = init_base + init.data.len();
|
||||||
assert!(memory_desc.minimum as usize * WASM_PAGE_SIZE >= data_top);
|
assert!(memory_desc.minimum.bytes().0 >= data_top);
|
||||||
|
|
||||||
let mem = &memories[local_memory_index];
|
let mem = &memories[local_memory_index];
|
||||||
mem.write_many(init_base as u32, &init.data).unwrap();
|
mem.write_many(init_base as u32, &init.data).unwrap();
|
||||||
@ -167,9 +167,8 @@ impl LocalBacking {
|
|||||||
LocalOrImport::Local(local_table_index) => {
|
LocalOrImport::Local(local_table_index) => {
|
||||||
let table = &tables[local_table_index];
|
let table = &tables[local_table_index];
|
||||||
|
|
||||||
if (table.current_size() as usize) < init_base + init.elements.len() {
|
if (table.size() as usize) < init_base + init.elements.len() {
|
||||||
let delta =
|
let delta = (init_base + init.elements.len()) - table.size() as usize;
|
||||||
(init_base + init.elements.len()) - table.current_size() as usize;
|
|
||||||
// Grow the table if it's too small.
|
// Grow the table if it's too small.
|
||||||
table.grow(delta as u32).expect("couldn't grow table");
|
table.grow(delta as u32).expect("couldn't grow table");
|
||||||
}
|
}
|
||||||
@ -203,9 +202,8 @@ impl LocalBacking {
|
|||||||
LocalOrImport::Import(import_table_index) => {
|
LocalOrImport::Import(import_table_index) => {
|
||||||
let table = &imports.tables[import_table_index];
|
let table = &imports.tables[import_table_index];
|
||||||
|
|
||||||
if (table.current_size() as usize) < init_base + init.elements.len() {
|
if (table.size() as usize) < init_base + init.elements.len() {
|
||||||
let delta =
|
let delta = (init_base + init.elements.len()) - table.size() as usize;
|
||||||
(init_base + init.elements.len()) - table.current_size() as usize;
|
|
||||||
// Grow the table if it's too small.
|
// Grow the table if it's too small.
|
||||||
table.grow(delta as u32).expect("couldn't grow table");
|
table.grow(delta as u32).expect("couldn't grow table");
|
||||||
}
|
}
|
||||||
|
@ -140,6 +140,20 @@ impl PartialEq for CallError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This error type is produced when creating something,
|
||||||
|
/// like a `Memory` or a `Table`.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum CreationError {
|
||||||
|
UnableToCreateMemory,
|
||||||
|
UnableToCreateTable,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for CreationError {
|
||||||
|
fn eq(&self, _other: &CreationError) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The amalgamation of all errors that can occur
|
/// The amalgamation of all errors that can occur
|
||||||
/// during the compilation, instantiation, or execution
|
/// during the compilation, instantiation, or execution
|
||||||
/// of a webassembly module.
|
/// of a webassembly module.
|
||||||
@ -152,6 +166,7 @@ pub enum Error {
|
|||||||
RuntimeError(RuntimeError),
|
RuntimeError(RuntimeError),
|
||||||
ResolveError(ResolveError),
|
ResolveError(ResolveError),
|
||||||
CallError(CallError),
|
CallError(CallError),
|
||||||
|
CreationError(CreationError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Error {
|
impl PartialEq for Error {
|
||||||
@ -220,6 +235,12 @@ impl From<CallError> for Box<Error> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<CreationError> for Box<Error> {
|
||||||
|
fn from(creation_err: CreationError) -> Self {
|
||||||
|
Box::new(Error::CreationError(creation_err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<RuntimeError> for Box<CallError> {
|
impl From<RuntimeError> for Box<CallError> {
|
||||||
fn from(runtime_err: RuntimeError) -> Self {
|
fn from(runtime_err: RuntimeError) -> Self {
|
||||||
Box::new(CallError::Runtime(runtime_err))
|
Box::new(CallError::Runtime(runtime_err))
|
||||||
|
@ -12,10 +12,28 @@ pub struct Global {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Global {
|
impl Global {
|
||||||
|
/// Create a new `Global` value.
|
||||||
|
///
|
||||||
|
/// Usage:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use wasmer_runtime_core::global::Global;
|
||||||
|
/// # use wasmer_runtime_core::types::Value;
|
||||||
|
/// let global = Global::new(Value::I32(42));
|
||||||
|
/// ```
|
||||||
pub fn new(value: Value) -> Self {
|
pub fn new(value: Value) -> Self {
|
||||||
Self::new_internal(value, false)
|
Self::new_internal(value, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new, mutable `Global` value.
|
||||||
|
///
|
||||||
|
/// Usage:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use wasmer_runtime_core::global::Global;
|
||||||
|
/// # use wasmer_runtime_core::types::Value;
|
||||||
|
/// let global = Global::new_mutable(Value::I32(42));
|
||||||
|
/// ```
|
||||||
pub fn new_mutable(value: Value) -> Self {
|
pub fn new_mutable(value: Value) -> Self {
|
||||||
Self::new_internal(value, true)
|
Self::new_internal(value, true)
|
||||||
}
|
}
|
||||||
@ -41,10 +59,17 @@ impl Global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the [`GlobalDescriptor`] generated for this global.
|
||||||
|
///
|
||||||
|
/// [`GlobalDescriptor`]: struct.GlobalDescriptor.html
|
||||||
pub fn descriptor(&self) -> GlobalDescriptor {
|
pub fn descriptor(&self) -> GlobalDescriptor {
|
||||||
self.desc
|
self.desc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the value help by this global.
|
||||||
|
///
|
||||||
|
/// This method will panic if the value is
|
||||||
|
/// the wrong type.
|
||||||
pub fn set(&self, value: Value) {
|
pub fn set(&self, value: Value) {
|
||||||
if self.desc.mutable {
|
if self.desc.mutable {
|
||||||
if self.desc.ty == value.ty() {
|
if self.desc.ty == value.ty() {
|
||||||
@ -65,6 +90,7 @@ impl Global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the value held by this global.
|
||||||
pub fn get(&self) -> Value {
|
pub fn get(&self) -> Value {
|
||||||
let data = self.storage.borrow().data;
|
let data = self.storage.borrow().data;
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ pub mod structures;
|
|||||||
mod sys;
|
mod sys;
|
||||||
pub mod table;
|
pub mod table;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
pub mod units;
|
||||||
pub mod vm;
|
pub mod vm;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub mod vmcalls;
|
pub mod vmcalls;
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
memory::{WASM_MAX_PAGES, WASM_PAGE_SIZE},
|
error::CreationError,
|
||||||
sys,
|
sys,
|
||||||
types::MemoryDescriptor,
|
types::MemoryDescriptor,
|
||||||
|
units::{Bytes, Pages},
|
||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -22,24 +23,24 @@ pub const DYNAMIC_GUARD_SIZE: usize = 4096;
|
|||||||
/// us to add a guard-page at the end to help elide some bounds-checks.
|
/// us to add a guard-page at the end to help elide some bounds-checks.
|
||||||
pub struct DynamicMemory {
|
pub struct DynamicMemory {
|
||||||
memory: sys::Memory,
|
memory: sys::Memory,
|
||||||
current: u32,
|
current: Pages,
|
||||||
max: Option<u32>,
|
max: Option<Pages>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DynamicMemory {
|
impl DynamicMemory {
|
||||||
pub(super) fn new(desc: MemoryDescriptor, local: &mut vm::LocalMemory) -> Option<Box<Self>> {
|
pub(super) fn new(
|
||||||
|
desc: MemoryDescriptor,
|
||||||
|
local: &mut vm::LocalMemory,
|
||||||
|
) -> Result<Box<Self>, CreationError> {
|
||||||
|
let min_bytes: Bytes = desc.minimum.into();
|
||||||
let memory = {
|
let memory = {
|
||||||
let mut memory =
|
let mut memory = sys::Memory::with_size(min_bytes.0 + DYNAMIC_GUARD_SIZE)
|
||||||
sys::Memory::with_size((desc.minimum as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE)
|
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
||||||
.ok()?;
|
if desc.minimum != Pages(0) {
|
||||||
if desc.minimum != 0 {
|
|
||||||
unsafe {
|
unsafe {
|
||||||
memory
|
memory
|
||||||
.protect(
|
.protect(0..min_bytes.0, sys::Protect::ReadWrite)
|
||||||
0..(desc.minimum as usize * WASM_PAGE_SIZE),
|
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
||||||
sys::Protect::ReadWrite,
|
|
||||||
)
|
|
||||||
.ok()?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,18 +55,18 @@ impl DynamicMemory {
|
|||||||
let storage_ptr: *mut DynamicMemory = &mut *storage;
|
let storage_ptr: *mut DynamicMemory = &mut *storage;
|
||||||
|
|
||||||
local.base = storage.memory.as_ptr();
|
local.base = storage.memory.as_ptr();
|
||||||
local.bound = desc.minimum as usize * WASM_PAGE_SIZE;
|
local.bound = min_bytes.0;
|
||||||
local.memory = storage_ptr as *mut ();
|
local.memory = storage_ptr as *mut ();
|
||||||
|
|
||||||
Some(storage)
|
Ok(storage)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current(&self) -> u32 {
|
pub fn size(&self) -> Pages {
|
||||||
self.current
|
self.current
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn grow(&mut self, delta: u32, local: &mut vm::LocalMemory) -> Option<u32> {
|
pub fn grow(&mut self, delta: Pages, local: &mut vm::LocalMemory) -> Option<Pages> {
|
||||||
if delta == 0 {
|
if delta == Pages(0) {
|
||||||
return Some(self.current);
|
return Some(self.current);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,30 +78,22 @@ impl DynamicMemory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if new_pages as usize > WASM_MAX_PAGES {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut new_memory =
|
let mut new_memory =
|
||||||
sys::Memory::with_size((new_pages as usize * WASM_PAGE_SIZE) + DYNAMIC_GUARD_SIZE)
|
sys::Memory::with_size(new_pages.bytes().0 + DYNAMIC_GUARD_SIZE).ok()?;
|
||||||
.ok()?;
|
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
new_memory
|
new_memory
|
||||||
.protect(
|
.protect(0..new_pages.bytes().0, sys::Protect::ReadWrite)
|
||||||
0..(new_pages as usize * WASM_PAGE_SIZE),
|
|
||||||
sys::Protect::ReadWrite,
|
|
||||||
)
|
|
||||||
.ok()?;
|
.ok()?;
|
||||||
|
|
||||||
new_memory.as_slice_mut()[..self.current as usize * WASM_PAGE_SIZE]
|
new_memory.as_slice_mut()[..self.current.bytes().0]
|
||||||
.copy_from_slice(&self.memory.as_slice()[..self.current as usize * WASM_PAGE_SIZE]);
|
.copy_from_slice(&self.memory.as_slice()[..self.current.bytes().0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.memory = new_memory; //The old memory gets dropped.
|
self.memory = new_memory; //The old memory gets dropped.
|
||||||
|
|
||||||
local.base = self.memory.as_ptr();
|
local.base = self.memory.as_ptr();
|
||||||
local.bound = new_pages as usize * WASM_PAGE_SIZE;
|
local.bound = new_pages.bytes().0;
|
||||||
|
|
||||||
let old_pages = self.current;
|
let old_pages = self.current;
|
||||||
self.current = new_pages;
|
self.current = new_pages;
|
||||||
@ -108,10 +101,10 @@ impl DynamicMemory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_slice(&self) -> &[u8] {
|
pub fn as_slice(&self) -> &[u8] {
|
||||||
unsafe { &self.memory.as_slice()[0..self.current as usize * WASM_PAGE_SIZE] }
|
unsafe { &self.memory.as_slice()[0..self.current.bytes().0] }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
||||||
unsafe { &mut self.memory.as_slice_mut()[0..self.current as usize * WASM_PAGE_SIZE] }
|
unsafe { &mut self.memory.as_slice_mut()[0..self.current.bytes().0] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
error::CreationError,
|
||||||
export::Export,
|
export::Export,
|
||||||
import::IsExport,
|
import::IsExport,
|
||||||
memory::dynamic::DYNAMIC_GUARD_SIZE,
|
memory::dynamic::DYNAMIC_GUARD_SIZE,
|
||||||
memory::static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
|
memory::static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
|
||||||
types::{MemoryDescriptor, ValueType},
|
types::{MemoryDescriptor, ValueType},
|
||||||
|
units::Pages,
|
||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
use std::{cell::RefCell, fmt, mem, ptr, rc::Rc, slice};
|
use std::{cell::RefCell, fmt, mem, ptr, rc::Rc, slice};
|
||||||
@ -14,9 +16,6 @@ pub use self::static_::{SharedStaticMemory, StaticMemory};
|
|||||||
mod dynamic;
|
mod dynamic;
|
||||||
mod static_;
|
mod static_;
|
||||||
|
|
||||||
pub const WASM_PAGE_SIZE: usize = 65_536;
|
|
||||||
pub const WASM_MAX_PAGES: usize = 65_536;
|
|
||||||
|
|
||||||
pub struct Memory {
|
pub struct Memory {
|
||||||
desc: MemoryDescriptor,
|
desc: MemoryDescriptor,
|
||||||
storage: Rc<RefCell<(MemoryStorage, Box<vm::LocalMemory>)>>,
|
storage: Rc<RefCell<(MemoryStorage, Box<vm::LocalMemory>)>>,
|
||||||
@ -24,29 +23,28 @@ pub struct Memory {
|
|||||||
|
|
||||||
impl Memory {
|
impl Memory {
|
||||||
/// Create a new `Memory` from a [`MemoryDescriptor`]
|
/// Create a new `Memory` from a [`MemoryDescriptor`]
|
||||||
///
|
///
|
||||||
/// [`MemoryDescriptor`]: struct.MemoryDescriptor.html
|
/// [`MemoryDescriptor`]: struct.MemoryDescriptor.html
|
||||||
///
|
///
|
||||||
/// Usage:
|
/// Usage:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use wasmer_runtime_core::types::MemoryDescriptor;
|
/// # use wasmer_runtime_core::types::MemoryDescriptor;
|
||||||
/// # use wasmer_runtime_core::memory::Memory;
|
/// # use wasmer_runtime_core::memory::Memory;
|
||||||
/// # use wasmer_runtime_core::error::Result;
|
/// # use wasmer_runtime_core::error::Result;
|
||||||
///
|
/// # use wasmer_runtime_core::units::Pages;
|
||||||
/// # fn create_memory() -> Result<()> {
|
/// # fn create_memory() -> Result<()> {
|
||||||
/// let descriptor = MemoryDescriptor {
|
/// let descriptor = MemoryDescriptor {
|
||||||
/// minimum: 10,
|
/// minimum: Pages(10),
|
||||||
/// maximum: None,
|
/// maximum: None,
|
||||||
/// shared: false,
|
/// shared: false,
|
||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
/// let memory = Memory::new(descriptor)?;
|
/// let memory = Memory::new(descriptor)?;
|
||||||
///
|
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new(desc: MemoryDescriptor) -> Option<Self> {
|
pub fn new(desc: MemoryDescriptor) -> Result<Self, CreationError> {
|
||||||
let mut vm_local_memory = Box::new(vm::LocalMemory {
|
let mut vm_local_memory = Box::new(vm::LocalMemory {
|
||||||
base: ptr::null_mut(),
|
base: ptr::null_mut(),
|
||||||
bound: 0,
|
bound: 0,
|
||||||
@ -63,7 +61,7 @@ impl Memory {
|
|||||||
MemoryType::SharedStatic => unimplemented!("shared memories are not yet implemented"),
|
MemoryType::SharedStatic => unimplemented!("shared memories are not yet implemented"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Some(Memory {
|
Ok(Memory {
|
||||||
desc,
|
desc,
|
||||||
storage: Rc::new(RefCell::new((memory_storage, vm_local_memory))),
|
storage: Rc::new(RefCell::new((memory_storage, vm_local_memory))),
|
||||||
})
|
})
|
||||||
@ -71,14 +69,14 @@ impl Memory {
|
|||||||
|
|
||||||
/// Return the [`MemoryDescriptor`] that this memory
|
/// Return the [`MemoryDescriptor`] that this memory
|
||||||
/// was created with.
|
/// was created with.
|
||||||
///
|
///
|
||||||
/// [`MemoryDescriptor`]: struct.MemoryDescriptor.html
|
/// [`MemoryDescriptor`]: struct.MemoryDescriptor.html
|
||||||
pub fn descriptor(&self) -> MemoryDescriptor {
|
pub fn descriptor(&self) -> MemoryDescriptor {
|
||||||
self.desc
|
self.desc
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Grow this memory by the specfied number of pages.
|
/// Grow this memory by the specfied number of pages.
|
||||||
pub fn grow(&mut self, delta: u32) -> Option<u32> {
|
pub fn grow(&mut self, delta: Pages) -> Option<Pages> {
|
||||||
match &mut *self.storage.borrow_mut() {
|
match &mut *self.storage.borrow_mut() {
|
||||||
(MemoryStorage::Dynamic(ref mut dynamic_memory), ref mut local) => {
|
(MemoryStorage::Dynamic(ref mut dynamic_memory), ref mut local) => {
|
||||||
dynamic_memory.grow(delta, local)
|
dynamic_memory.grow(delta, local)
|
||||||
@ -90,16 +88,11 @@ impl Memory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The size, in bytes, of this memory.
|
|
||||||
pub fn bytes(&self) -> usize {
|
|
||||||
(self.size() as usize) * WASM_PAGE_SIZE
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The size, in wasm pages, of this memory.
|
/// The size, in wasm pages, of this memory.
|
||||||
pub fn size(&self) -> u32 {
|
pub fn size(&self) -> Pages {
|
||||||
match &*self.storage.borrow() {
|
match &*self.storage.borrow() {
|
||||||
(MemoryStorage::Dynamic(ref dynamic_memory), _) => dynamic_memory.current(),
|
(MemoryStorage::Dynamic(ref dynamic_memory), _) => dynamic_memory.size(),
|
||||||
(MemoryStorage::Static(ref static_memory), _) => static_memory.current(),
|
(MemoryStorage::Static(ref static_memory), _) => static_memory.size(),
|
||||||
(MemoryStorage::SharedStatic(_), _) => unimplemented!(),
|
(MemoryStorage::SharedStatic(_), _) => unimplemented!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -304,7 +297,7 @@ impl fmt::Debug for Memory {
|
|||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
f.debug_struct("Memory")
|
f.debug_struct("Memory")
|
||||||
.field("desc", &self.desc)
|
.field("desc", &self.desc)
|
||||||
.field("size", &self.bytes())
|
.field("size", &self.size())
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
memory::{
|
error::CreationError,
|
||||||
static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
|
memory::static_::{SAFE_STATIC_GUARD_SIZE, SAFE_STATIC_HEAP_SIZE},
|
||||||
WASM_MAX_PAGES, WASM_PAGE_SIZE,
|
|
||||||
},
|
|
||||||
sys,
|
sys,
|
||||||
types::MemoryDescriptor,
|
types::MemoryDescriptor,
|
||||||
|
units::Pages,
|
||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -21,26 +20,23 @@ use crate::{
|
|||||||
/// allows them to select the type of memory used however.
|
/// allows them to select the type of memory used however.
|
||||||
pub struct StaticMemory {
|
pub struct StaticMemory {
|
||||||
memory: sys::Memory,
|
memory: sys::Memory,
|
||||||
current: u32,
|
current: Pages,
|
||||||
max: Option<u32>,
|
max: Option<Pages>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StaticMemory {
|
impl StaticMemory {
|
||||||
pub(in crate::memory) fn new(
|
pub(in crate::memory) fn new(
|
||||||
desc: MemoryDescriptor,
|
desc: MemoryDescriptor,
|
||||||
local: &mut vm::LocalMemory,
|
local: &mut vm::LocalMemory,
|
||||||
) -> Option<Box<Self>> {
|
) -> Result<Box<Self>, CreationError> {
|
||||||
let memory = {
|
let memory = {
|
||||||
let mut memory =
|
let mut memory = sys::Memory::with_size(SAFE_STATIC_HEAP_SIZE + SAFE_STATIC_GUARD_SIZE)
|
||||||
sys::Memory::with_size(SAFE_STATIC_HEAP_SIZE + SAFE_STATIC_GUARD_SIZE).ok()?;
|
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
||||||
if desc.minimum != 0 {
|
if desc.minimum != Pages(0) {
|
||||||
unsafe {
|
unsafe {
|
||||||
memory
|
memory
|
||||||
.protect(
|
.protect(0..desc.minimum.bytes().0, sys::Protect::ReadWrite)
|
||||||
0..(desc.minimum as usize * WASM_PAGE_SIZE),
|
.map_err(|_| CreationError::UnableToCreateMemory)?;
|
||||||
sys::Protect::ReadWrite,
|
|
||||||
)
|
|
||||||
.ok()?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,18 +51,18 @@ impl StaticMemory {
|
|||||||
let storage_ptr: *mut StaticMemory = &mut *storage;
|
let storage_ptr: *mut StaticMemory = &mut *storage;
|
||||||
|
|
||||||
local.base = storage.memory.as_ptr();
|
local.base = storage.memory.as_ptr();
|
||||||
local.bound = desc.minimum as usize * WASM_PAGE_SIZE;
|
local.bound = desc.minimum.bytes().0;
|
||||||
local.memory = storage_ptr as *mut ();
|
local.memory = storage_ptr as *mut ();
|
||||||
|
|
||||||
Some(storage)
|
Ok(storage)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current(&self) -> u32 {
|
pub fn size(&self) -> Pages {
|
||||||
self.current
|
self.current
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn grow(&mut self, delta: u32, local: &mut vm::LocalMemory) -> Option<u32> {
|
pub fn grow(&mut self, delta: Pages, local: &mut vm::LocalMemory) -> Option<Pages> {
|
||||||
if delta == 0 {
|
if delta == Pages(0) {
|
||||||
return Some(self.current);
|
return Some(self.current);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,20 +74,16 @@ impl StaticMemory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if new_pages as usize > WASM_MAX_PAGES {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
self.memory
|
self.memory
|
||||||
.protect(
|
.protect(
|
||||||
self.current as usize * WASM_PAGE_SIZE..new_pages as usize * WASM_PAGE_SIZE,
|
self.current.bytes().0..new_pages.bytes().0,
|
||||||
sys::Protect::ReadWrite,
|
sys::Protect::ReadWrite,
|
||||||
)
|
)
|
||||||
.ok()?;
|
.ok()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
local.bound = new_pages as usize * WASM_PAGE_SIZE;
|
local.bound = new_pages.bytes().0;
|
||||||
|
|
||||||
let old_pages = self.current;
|
let old_pages = self.current;
|
||||||
|
|
||||||
@ -101,10 +93,10 @@ impl StaticMemory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_slice(&self) -> &[u8] {
|
pub fn as_slice(&self) -> &[u8] {
|
||||||
unsafe { &self.memory.as_slice()[0..self.current as usize * WASM_PAGE_SIZE] }
|
unsafe { &self.memory.as_slice()[0..self.current.bytes().0] }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
pub fn as_slice_mut(&mut self) -> &mut [u8] {
|
||||||
unsafe { &mut self.memory.as_slice_mut()[0..self.current as usize * WASM_PAGE_SIZE] }
|
unsafe { &mut self.memory.as_slice_mut()[0..self.current.bytes().0] }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
error::CreationError,
|
||||||
instance::Function,
|
instance::Function,
|
||||||
sig_registry::SigRegistry,
|
sig_registry::SigRegistry,
|
||||||
structures::TypedIndex,
|
structures::TypedIndex,
|
||||||
@ -48,7 +49,10 @@ pub struct AnyfuncTable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AnyfuncTable {
|
impl AnyfuncTable {
|
||||||
pub fn new(desc: TableDescriptor, local: &mut vm::LocalTable) -> Result<Box<Self>, ()> {
|
pub fn new(
|
||||||
|
desc: TableDescriptor,
|
||||||
|
local: &mut vm::LocalTable,
|
||||||
|
) -> Result<Box<Self>, CreationError> {
|
||||||
let initial_table_backing_len = match desc.maximum {
|
let initial_table_backing_len = match desc.maximum {
|
||||||
Some(max) => max,
|
Some(max) => max,
|
||||||
None => desc.minimum,
|
None => desc.minimum,
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
error::CreationError,
|
||||||
export::Export,
|
export::Export,
|
||||||
import::IsExport,
|
import::IsExport,
|
||||||
types::{ElementType, TableDescriptor},
|
types::{ElementType, TableDescriptor},
|
||||||
@ -28,29 +29,27 @@ pub struct Table {
|
|||||||
|
|
||||||
impl Table {
|
impl Table {
|
||||||
/// Create a new `Table` from a [`TableDescriptor`]
|
/// Create a new `Table` from a [`TableDescriptor`]
|
||||||
///
|
///
|
||||||
/// [`TableDescriptor`]: struct.TableDescriptor.html
|
/// [`TableDescriptor`]: struct.TableDescriptor.html
|
||||||
///
|
///
|
||||||
/// Usage:
|
/// Usage:
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use wasmer_runtime_core::types::{TableDescriptor, ElementType};
|
/// # use wasmer_runtime_core::types::{TableDescriptor, ElementType};
|
||||||
/// # use wasmer_runtime_core::table::Table;
|
/// # use wasmer_runtime_core::table::Table;
|
||||||
/// # use wasmer_runtime_core::error::Result;
|
/// # use wasmer_runtime_core::error::Result;
|
||||||
///
|
|
||||||
/// # fn create_table() -> Result<()> {
|
/// # fn create_table() -> Result<()> {
|
||||||
/// let descriptor = TableDescriptor {
|
/// let descriptor = TableDescriptor {
|
||||||
/// element: ElementType::Anyfunc,
|
/// element: ElementType::Anyfunc,
|
||||||
/// minimum: 10,
|
/// minimum: 10,
|
||||||
/// maximum: None,
|
/// maximum: None,
|
||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
/// let table = Table::new(descriptor)?;
|
/// let table = Table::new(descriptor)?;
|
||||||
///
|
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new(desc: TableDescriptor) -> Result<Self, ()> {
|
pub fn new(desc: TableDescriptor) -> Result<Self, CreationError> {
|
||||||
let mut local = vm::LocalTable {
|
let mut local = vm::LocalTable {
|
||||||
base: ptr::null_mut(),
|
base: ptr::null_mut(),
|
||||||
count: 0,
|
count: 0,
|
||||||
@ -67,10 +66,12 @@ impl Table {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the `TableDescriptor` used to create this `Table`.
|
||||||
pub fn descriptor(&self) -> TableDescriptor {
|
pub fn descriptor(&self) -> TableDescriptor {
|
||||||
self.desc
|
self.desc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the element at index.
|
||||||
pub fn set(&self, index: u32, element: Element) -> Result<(), ()> {
|
pub fn set(&self, index: u32, element: Element) -> Result<(), ()> {
|
||||||
match &mut *self.storage.borrow_mut() {
|
match &mut *self.storage.borrow_mut() {
|
||||||
(TableStorage::Anyfunc(ref mut anyfunc_table), _) => {
|
(TableStorage::Anyfunc(ref mut anyfunc_table), _) => {
|
||||||
@ -91,15 +92,17 @@ impl Table {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_size(&self) -> u32 {
|
/// The current size of this table.
|
||||||
|
pub fn size(&self) -> u32 {
|
||||||
match &*self.storage.borrow() {
|
match &*self.storage.borrow() {
|
||||||
(TableStorage::Anyfunc(ref anyfunc_table), _) => anyfunc_table.current_size(),
|
(TableStorage::Anyfunc(ref anyfunc_table), _) => anyfunc_table.current_size(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Grow this table by `delta`.
|
||||||
pub fn grow(&self, delta: u32) -> Option<u32> {
|
pub fn grow(&self, delta: u32) -> Option<u32> {
|
||||||
if delta == 0 {
|
if delta == 0 {
|
||||||
return Some(self.current_size());
|
return Some(self.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
match &mut *self.storage.borrow_mut() {
|
match &mut *self.storage.borrow_mut() {
|
||||||
@ -131,6 +134,9 @@ impl Clone for Table {
|
|||||||
|
|
||||||
impl fmt::Debug for Table {
|
impl fmt::Debug for Table {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
f.debug_struct("Table").field("desc", &self.desc).finish()
|
f.debug_struct("Table")
|
||||||
|
.field("desc", &self.desc)
|
||||||
|
.field("size", &self.size())
|
||||||
|
.finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::{memory::MemoryType, module::ModuleInner, structures::TypedIndex};
|
use crate::{memory::MemoryType, module::ModuleInner, structures::TypedIndex, units::Pages};
|
||||||
use std::{borrow::Cow, mem};
|
use std::{borrow::Cow, mem};
|
||||||
|
|
||||||
/// Represents a WebAssembly type.
|
/// Represents a WebAssembly type.
|
||||||
@ -142,7 +142,9 @@ impl TableDescriptor {
|
|||||||
// TODO: We should define implementation limits.
|
// TODO: We should define implementation limits.
|
||||||
let imported_max = imported.maximum.unwrap_or(u32::max_value());
|
let imported_max = imported.maximum.unwrap_or(u32::max_value());
|
||||||
let self_max = self.maximum.unwrap_or(u32::max_value());
|
let self_max = self.maximum.unwrap_or(u32::max_value());
|
||||||
self.element == imported.element && imported_max <= self_max && self.minimum <= imported.minimum
|
self.element == imported.element
|
||||||
|
&& imported_max <= self_max
|
||||||
|
&& self.minimum <= imported.minimum
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,9 +176,9 @@ pub struct GlobalInit {
|
|||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct MemoryDescriptor {
|
pub struct MemoryDescriptor {
|
||||||
/// The minimum number of allowed pages.
|
/// The minimum number of allowed pages.
|
||||||
pub minimum: u32,
|
pub minimum: Pages,
|
||||||
/// The maximum number of allowed pages.
|
/// The maximum number of allowed pages.
|
||||||
pub maximum: Option<u32>,
|
pub maximum: Option<Pages>,
|
||||||
/// This memory can be shared between wasm threads.
|
/// This memory can be shared between wasm threads.
|
||||||
pub shared: bool,
|
pub shared: bool,
|
||||||
}
|
}
|
||||||
@ -192,10 +194,12 @@ impl MemoryDescriptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fits_in_imported(&self, imported: MemoryDescriptor) -> bool {
|
pub(crate) fn fits_in_imported(&self, imported: MemoryDescriptor) -> bool {
|
||||||
let imported_max = imported.maximum.unwrap_or(65_536);
|
let imported_max = imported.maximum.unwrap_or(Pages(65_536));
|
||||||
let self_max = self.maximum.unwrap_or(65_536);
|
let self_max = self.maximum.unwrap_or(Pages(65_536));
|
||||||
|
|
||||||
self.shared == imported.shared && imported_max <= self_max && self.minimum <= imported.minimum
|
self.shared == imported.shared
|
||||||
|
&& imported_max <= self_max
|
||||||
|
&& self.minimum <= imported.minimum
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
94
lib/runtime-core/src/units.rs
Normal file
94
lib/runtime-core/src/units.rs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
use std::{
|
||||||
|
fmt,
|
||||||
|
ops::{Add, Sub},
|
||||||
|
};
|
||||||
|
|
||||||
|
const WASM_PAGE_SIZE: usize = 65_536;
|
||||||
|
const WASM_MAX_PAGES: usize = 65_536;
|
||||||
|
|
||||||
|
/// Units of WebAssembly pages (as specified to be 65,536 bytes).
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct Pages(pub u32);
|
||||||
|
|
||||||
|
impl Pages {
|
||||||
|
pub fn checked_add(self, rhs: Pages) -> Option<Pages> {
|
||||||
|
let added = (self.0 as usize) + (rhs.0 as usize);
|
||||||
|
if added <= WASM_MAX_PAGES {
|
||||||
|
Some(Pages(added as u32))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bytes(self) -> Bytes {
|
||||||
|
self.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for Pages {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{} pages", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Units of WebAssembly memory in terms of 8-bit bytes.
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct Bytes(pub usize);
|
||||||
|
|
||||||
|
impl fmt::Debug for Bytes {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{} bytes", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Pages> for Bytes {
|
||||||
|
fn from(pages: Pages) -> Bytes {
|
||||||
|
Bytes((pages.0 as usize) * WASM_PAGE_SIZE)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Sub<T> for Pages
|
||||||
|
where
|
||||||
|
T: Into<Pages>,
|
||||||
|
{
|
||||||
|
type Output = Pages;
|
||||||
|
fn sub(self, rhs: T) -> Pages {
|
||||||
|
Pages(self.0 - rhs.into().0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Add<T> for Pages
|
||||||
|
where
|
||||||
|
T: Into<Pages>,
|
||||||
|
{
|
||||||
|
type Output = Pages;
|
||||||
|
fn add(self, rhs: T) -> Pages {
|
||||||
|
Pages(self.0 + rhs.into().0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Bytes> for Pages {
|
||||||
|
fn from(bytes: Bytes) -> Pages {
|
||||||
|
Pages((bytes.0 / WASM_PAGE_SIZE) as u32)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Sub<T> for Bytes
|
||||||
|
where
|
||||||
|
T: Into<Bytes>,
|
||||||
|
{
|
||||||
|
type Output = Bytes;
|
||||||
|
fn sub(self, rhs: T) -> Bytes {
|
||||||
|
Bytes(self.0 - rhs.into().0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Add<T> for Bytes
|
||||||
|
where
|
||||||
|
T: Into<Bytes>,
|
||||||
|
{
|
||||||
|
type Output = Bytes;
|
||||||
|
fn add(self, rhs: T) -> Bytes {
|
||||||
|
Bytes(self.0 + rhs.into().0)
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ use crate::{
|
|||||||
memory::{DynamicMemory, StaticMemory},
|
memory::{DynamicMemory, StaticMemory},
|
||||||
structures::TypedIndex,
|
structures::TypedIndex,
|
||||||
types::{ImportedMemoryIndex, LocalMemoryIndex, LocalTableIndex},
|
types::{ImportedMemoryIndex, LocalMemoryIndex, LocalTableIndex},
|
||||||
|
units::Pages,
|
||||||
vm,
|
vm,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -13,14 +14,14 @@ use crate::{
|
|||||||
|
|
||||||
pub unsafe extern "C" fn local_static_memory_grow(
|
pub unsafe extern "C" fn local_static_memory_grow(
|
||||||
memory_index: LocalMemoryIndex,
|
memory_index: LocalMemoryIndex,
|
||||||
delta: u32,
|
delta: Pages,
|
||||||
ctx: &mut vm::Ctx,
|
ctx: &mut vm::Ctx,
|
||||||
) -> i32 {
|
) -> i32 {
|
||||||
let local_memory = *ctx.memories.add(memory_index.index());
|
let local_memory = *ctx.memories.add(memory_index.index());
|
||||||
let memory = (*local_memory).memory as *mut StaticMemory;
|
let memory = (*local_memory).memory as *mut StaticMemory;
|
||||||
|
|
||||||
if let Some(old) = (*memory).grow(delta, &mut *local_memory) {
|
if let Some(old) = (*memory).grow(delta, &mut *local_memory) {
|
||||||
old as i32
|
old.0 as i32
|
||||||
} else {
|
} else {
|
||||||
-1
|
-1
|
||||||
}
|
}
|
||||||
@ -29,23 +30,23 @@ pub unsafe extern "C" fn local_static_memory_grow(
|
|||||||
pub unsafe extern "C" fn local_static_memory_size(
|
pub unsafe extern "C" fn local_static_memory_size(
|
||||||
memory_index: LocalMemoryIndex,
|
memory_index: LocalMemoryIndex,
|
||||||
ctx: &vm::Ctx,
|
ctx: &vm::Ctx,
|
||||||
) -> u32 {
|
) -> Pages {
|
||||||
let local_memory = *ctx.memories.add(memory_index.index());
|
let local_memory = *ctx.memories.add(memory_index.index());
|
||||||
let memory = (*local_memory).memory as *mut StaticMemory;
|
let memory = (*local_memory).memory as *mut StaticMemory;
|
||||||
|
|
||||||
(*memory).current()
|
(*memory).size()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe extern "C" fn local_dynamic_memory_grow(
|
pub unsafe extern "C" fn local_dynamic_memory_grow(
|
||||||
memory_index: LocalMemoryIndex,
|
memory_index: LocalMemoryIndex,
|
||||||
delta: u32,
|
delta: Pages,
|
||||||
ctx: &mut vm::Ctx,
|
ctx: &mut vm::Ctx,
|
||||||
) -> i32 {
|
) -> i32 {
|
||||||
let local_memory = *ctx.memories.add(memory_index.index());
|
let local_memory = *ctx.memories.add(memory_index.index());
|
||||||
let memory = (*local_memory).memory as *mut DynamicMemory;
|
let memory = (*local_memory).memory as *mut DynamicMemory;
|
||||||
|
|
||||||
if let Some(old) = (*memory).grow(delta, &mut *local_memory) {
|
if let Some(old) = (*memory).grow(delta, &mut *local_memory) {
|
||||||
old as i32
|
old.0 as i32
|
||||||
} else {
|
} else {
|
||||||
-1
|
-1
|
||||||
}
|
}
|
||||||
@ -54,11 +55,11 @@ pub unsafe extern "C" fn local_dynamic_memory_grow(
|
|||||||
pub unsafe extern "C" fn local_dynamic_memory_size(
|
pub unsafe extern "C" fn local_dynamic_memory_size(
|
||||||
memory_index: LocalMemoryIndex,
|
memory_index: LocalMemoryIndex,
|
||||||
ctx: &vm::Ctx,
|
ctx: &vm::Ctx,
|
||||||
) -> u32 {
|
) -> Pages {
|
||||||
let local_memory = *ctx.memories.add(memory_index.index());
|
let local_memory = *ctx.memories.add(memory_index.index());
|
||||||
let memory = (*local_memory).memory as *mut DynamicMemory;
|
let memory = (*local_memory).memory as *mut DynamicMemory;
|
||||||
|
|
||||||
(*memory).current()
|
(*memory).size()
|
||||||
}
|
}
|
||||||
|
|
||||||
// +*****************************+
|
// +*****************************+
|
||||||
@ -67,14 +68,14 @@ pub unsafe extern "C" fn local_dynamic_memory_size(
|
|||||||
|
|
||||||
pub unsafe extern "C" fn imported_static_memory_grow(
|
pub unsafe extern "C" fn imported_static_memory_grow(
|
||||||
import_memory_index: ImportedMemoryIndex,
|
import_memory_index: ImportedMemoryIndex,
|
||||||
delta: u32,
|
delta: Pages,
|
||||||
ctx: &mut vm::Ctx,
|
ctx: &mut vm::Ctx,
|
||||||
) -> i32 {
|
) -> i32 {
|
||||||
let local_memory = *ctx.imported_memories.add(import_memory_index.index());
|
let local_memory = *ctx.imported_memories.add(import_memory_index.index());
|
||||||
let memory = (*local_memory).memory as *mut StaticMemory;
|
let memory = (*local_memory).memory as *mut StaticMemory;
|
||||||
|
|
||||||
if let Some(old) = (*memory).grow(delta, &mut *local_memory) {
|
if let Some(old) = (*memory).grow(delta, &mut *local_memory) {
|
||||||
old as i32
|
old.0 as i32
|
||||||
} else {
|
} else {
|
||||||
-1
|
-1
|
||||||
}
|
}
|
||||||
@ -83,23 +84,23 @@ pub unsafe extern "C" fn imported_static_memory_grow(
|
|||||||
pub unsafe extern "C" fn imported_static_memory_size(
|
pub unsafe extern "C" fn imported_static_memory_size(
|
||||||
import_memory_index: ImportedMemoryIndex,
|
import_memory_index: ImportedMemoryIndex,
|
||||||
ctx: &vm::Ctx,
|
ctx: &vm::Ctx,
|
||||||
) -> u32 {
|
) -> Pages {
|
||||||
let local_memory = *ctx.imported_memories.add(import_memory_index.index());
|
let local_memory = *ctx.imported_memories.add(import_memory_index.index());
|
||||||
let memory = (*local_memory).memory as *mut StaticMemory;
|
let memory = (*local_memory).memory as *mut StaticMemory;
|
||||||
|
|
||||||
(*memory).current()
|
(*memory).size()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe extern "C" fn imported_dynamic_memory_grow(
|
pub unsafe extern "C" fn imported_dynamic_memory_grow(
|
||||||
memory_index: ImportedMemoryIndex,
|
memory_index: ImportedMemoryIndex,
|
||||||
delta: u32,
|
delta: Pages,
|
||||||
ctx: &mut vm::Ctx,
|
ctx: &mut vm::Ctx,
|
||||||
) -> i32 {
|
) -> i32 {
|
||||||
let local_memory = *ctx.imported_memories.add(memory_index.index());
|
let local_memory = *ctx.imported_memories.add(memory_index.index());
|
||||||
let memory = (*local_memory).memory as *mut DynamicMemory;
|
let memory = (*local_memory).memory as *mut DynamicMemory;
|
||||||
|
|
||||||
if let Some(old) = (*memory).grow(delta, &mut *local_memory) {
|
if let Some(old) = (*memory).grow(delta, &mut *local_memory) {
|
||||||
old as i32
|
old.0 as i32
|
||||||
} else {
|
} else {
|
||||||
-1
|
-1
|
||||||
}
|
}
|
||||||
@ -108,11 +109,11 @@ pub unsafe extern "C" fn imported_dynamic_memory_grow(
|
|||||||
pub unsafe extern "C" fn imported_dynamic_memory_size(
|
pub unsafe extern "C" fn imported_dynamic_memory_size(
|
||||||
memory_index: ImportedMemoryIndex,
|
memory_index: ImportedMemoryIndex,
|
||||||
ctx: &vm::Ctx,
|
ctx: &vm::Ctx,
|
||||||
) -> u32 {
|
) -> Pages {
|
||||||
let local_memory = *ctx.imported_memories.add(memory_index.index());
|
let local_memory = *ctx.imported_memories.add(memory_index.index());
|
||||||
let memory = (*local_memory).memory as *mut DynamicMemory;
|
let memory = (*local_memory).memory as *mut DynamicMemory;
|
||||||
|
|
||||||
(*memory).current()
|
(*memory).size()
|
||||||
}
|
}
|
||||||
|
|
||||||
// +*****************************+
|
// +*****************************+
|
||||||
|
@ -88,6 +88,7 @@ pub use wasmer_runtime_core::error;
|
|||||||
pub use wasmer_runtime_core::{func, imports};
|
pub use wasmer_runtime_core::{func, imports};
|
||||||
|
|
||||||
pub mod wasm {
|
pub mod wasm {
|
||||||
|
//! Various types exposed by the Wasmer Runtime.
|
||||||
pub use wasmer_runtime_core::global::Global;
|
pub use wasmer_runtime_core::global::Global;
|
||||||
pub use wasmer_runtime_core::instance::Function;
|
pub use wasmer_runtime_core::instance::Function;
|
||||||
pub use wasmer_runtime_core::memory::Memory;
|
pub use wasmer_runtime_core::memory::Memory;
|
||||||
@ -95,6 +96,12 @@ pub mod wasm {
|
|||||||
pub use wasmer_runtime_core::types::{FuncSig, MemoryDescriptor, TableDescriptor, Type, Value};
|
pub use wasmer_runtime_core::types::{FuncSig, MemoryDescriptor, TableDescriptor, Type, Value};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod units {
|
||||||
|
//! Various unit types.
|
||||||
|
|
||||||
|
pub use wasmer_runtime_core::units::{Bytes, Pages};
|
||||||
|
}
|
||||||
|
|
||||||
/// Compile WebAssembly binary code into a [`Module`].
|
/// Compile WebAssembly binary code into a [`Module`].
|
||||||
/// This function is useful if it is necessary to
|
/// This function is useful if it is necessary to
|
||||||
/// compile a module before it can be instantiated
|
/// compile a module before it can be instantiated
|
||||||
|
Loading…
x
Reference in New Issue
Block a user