mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-07 21:28:21 +00:00
Implemented LinearMemory trait
This commit is contained in:
committed by
ptitSeb
parent
6080fb010e
commit
c7c1507b8b
@@ -7,6 +7,7 @@ use std::fmt;
|
||||
use wasm_bindgen::{JsCast, JsValue};
|
||||
use wasmer_types::{ExternType, FunctionType, GlobalType, MemoryType, TableType};
|
||||
|
||||
/// Represents linear memory that is managed by the javascript runtime
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct VMMemory {
|
||||
pub(crate) memory: Memory,
|
||||
@@ -20,6 +21,11 @@ impl VMMemory {
|
||||
pub(crate) fn new(memory: Memory, ty: MemoryType) -> Self {
|
||||
Self { memory, ty }
|
||||
}
|
||||
|
||||
/// Attempts to clone this memory (if its clonable)
|
||||
pub(crate) fn try_clone(&self) -> Option<VMMemory> {
|
||||
Some(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
||||
35
lib/api/src/js/externals/memory.rs
vendored
35
lib/api/src/js/externals/memory.rs
vendored
@@ -6,7 +6,6 @@ use crate::js::{MemoryAccessError, MemoryType};
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::slice;
|
||||
use thiserror::Error;
|
||||
#[cfg(feature = "tracing")]
|
||||
use tracing::warn;
|
||||
|
||||
@@ -16,22 +15,7 @@ use wasmer_types::Pages;
|
||||
|
||||
use super::MemoryView;
|
||||
|
||||
/// Error type describing things that can go wrong when operating on Wasm Memories.
|
||||
#[derive(Error, Debug, Clone, PartialEq, Hash)]
|
||||
pub enum MemoryError {
|
||||
/// The operation would cause the size of the memory to exceed the maximum or would cause
|
||||
/// an overflow leading to unindexable memory.
|
||||
#[error("The memory could not grow: current size {} pages, requested increase: {} pages", current.0, attempted_delta.0)]
|
||||
CouldNotGrow {
|
||||
/// The current size in pages.
|
||||
current: Pages,
|
||||
/// The attempted amount to grow by in pages.
|
||||
attempted_delta: Pages,
|
||||
},
|
||||
/// A user defined error value, used for error cases not listed above.
|
||||
#[error("A user-defined error occurred: {0}")]
|
||||
Generic(String),
|
||||
}
|
||||
pub use wasmer_types::MemoryError;
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
@@ -116,6 +100,17 @@ impl Memory {
|
||||
Ok(Self::from_vm_export(store, vm_memory))
|
||||
}
|
||||
|
||||
/// Creates a new host `Memory` from provided JavaScript memory.
|
||||
pub fn new_raw(store: &mut impl AsStoreMut, js_memory: js_sys::WebAssembly::Memory, ty: MemoryType) -> Result<Self, MemoryError> {
|
||||
let vm_memory = VMMemory::new(js_memory, ty);
|
||||
Ok(Self::from_vm_export(store, vm_memory))
|
||||
}
|
||||
|
||||
/// Create a memory object from an existing memory and attaches it to the store
|
||||
pub fn new_from_existing(new_store: &mut impl AsStoreMut, memory: VMMemory) -> Self {
|
||||
Self::from_vm_export(new_store, memory)
|
||||
}
|
||||
|
||||
/// Returns the [`MemoryType`] of the `Memory`.
|
||||
///
|
||||
/// # Example
|
||||
@@ -210,6 +205,12 @@ impl Memory {
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to clone this memory (if its clonable)
|
||||
pub fn try_clone(&self, store: &impl AsStoreRef) -> Option<VMMemory> {
|
||||
let mem = self.handle.get(store.as_store_ref().objects());
|
||||
mem.try_clone()
|
||||
}
|
||||
|
||||
/// Checks whether this `Global` can be used with the given context.
|
||||
pub fn is_from_store(&self, store: &impl AsStoreRef) -> bool {
|
||||
self.handle.store_id() == store.as_store_ref().objects().id()
|
||||
|
||||
@@ -36,7 +36,7 @@ impl<T> FunctionEnv<T> {
|
||||
}
|
||||
|
||||
/// Get the data as reference
|
||||
pub fn as_ref<'a>(&self, store: &'a impl AsStoreMut) -> &'a T
|
||||
pub fn as_ref<'a>(&self, store: &'a impl AsStoreRef) -> &'a T
|
||||
where
|
||||
T: Any + Send + 'static + Sized,
|
||||
{
|
||||
@@ -112,6 +112,11 @@ impl<T: Send + 'static> FunctionEnvMut<'_, T> {
|
||||
self.func_env.as_mut(&mut self.store_mut)
|
||||
}
|
||||
|
||||
/// Borrows a new immmutable reference
|
||||
pub fn as_ref(&self) -> FunctionEnv<T> {
|
||||
self.func_env.clone()
|
||||
}
|
||||
|
||||
/// Borrows a new mutable reference
|
||||
pub fn as_mut<'a>(&'a mut self) -> FunctionEnvMut<'a, T> {
|
||||
FunctionEnvMut {
|
||||
|
||||
@@ -174,6 +174,36 @@ impl Imports {
|
||||
}
|
||||
imports
|
||||
}
|
||||
|
||||
/// Iterates through all the imports in this structure
|
||||
pub fn iter<'a>(&'a self) -> ImportsIterator<'a> {
|
||||
ImportsIterator::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ImportsIterator<'a> {
|
||||
iter: std::collections::hash_map::Iter<'a, (String, String), Extern>
|
||||
}
|
||||
|
||||
impl<'a> ImportsIterator<'a>
|
||||
{
|
||||
fn new(imports: &'a Imports) -> Self {
|
||||
let iter = imports.map.iter();
|
||||
Self { iter }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator
|
||||
for ImportsIterator<'a> {
|
||||
type Item = (&'a str, &'a str, &'a Extern);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.iter
|
||||
.next()
|
||||
.map(|(k, v)| {
|
||||
(k.0.as_str(), k.1.as_str(), v)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoIterator for &Imports {
|
||||
|
||||
@@ -73,6 +73,12 @@ pub use crate::js::types::{
|
||||
pub use crate::js::value::Value;
|
||||
pub use crate::js::value::Value as Val;
|
||||
|
||||
pub mod vm {
|
||||
//! The `vm` module re-exports wasmer-vm types.
|
||||
|
||||
pub use crate::js::export::VMMemory;
|
||||
}
|
||||
|
||||
pub use wasmer_types::is_wasm;
|
||||
pub use wasmer_types::{
|
||||
Bytes, ExportIndex, GlobalInit, LocalFunctionIndex, Pages, ValueType, WASM_MAX_PAGES,
|
||||
|
||||
@@ -11,7 +11,6 @@ use std::marker::PhantomData;
|
||||
|
||||
use crate::js::externals::Function;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, StoreHandle};
|
||||
use crate::js::FunctionEnv;
|
||||
use crate::js::{FromToNativeWasmType, RuntimeError, WasmTypeList};
|
||||
// use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use crate::js::export::VMFunction;
|
||||
|
||||
@@ -263,6 +263,11 @@ mod objects {
|
||||
self.id
|
||||
}
|
||||
|
||||
/// Sets the ID of this store
|
||||
pub fn set_id(&mut self, id: StoreId) {
|
||||
self.id = id;
|
||||
}
|
||||
|
||||
/// Returns a pair of mutable references from two handles.
|
||||
///
|
||||
/// Panics if both handles point to the same object.
|
||||
|
||||
Reference in New Issue
Block a user