mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-10 14:48:27 +00:00
Formatting...
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
use crate::js::error::WasmError;
|
||||
use crate::js::store::{AsStoreMut, AsStoreRef, InternalStoreHandle};
|
||||
use crate::js::wasm_bindgen_polyfill::Global;
|
||||
use crate::MemoryView;
|
||||
use js_sys::Function;
|
||||
use js_sys::WebAssembly::{Memory, Table};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
use wasm_bindgen::{JsCast, JsValue};
|
||||
use wasmer_types::{ExternType, FunctionType, GlobalType, MemoryType, TableType, Pages, WASM_PAGE_SIZE, StoreSnapshot};
|
||||
use crate::MemoryView;
|
||||
#[cfg(feature="tracing")]
|
||||
#[cfg(feature = "tracing")]
|
||||
use tracing::trace;
|
||||
use wasm_bindgen::{JsCast, JsValue};
|
||||
use wasmer_types::{
|
||||
ExternType, FunctionType, GlobalType, MemoryType, Pages, StoreSnapshot, TableType,
|
||||
WASM_PAGE_SIZE,
|
||||
};
|
||||
|
||||
pub use wasmer_types::MemoryError;
|
||||
|
||||
@@ -44,7 +47,7 @@ impl VMMemory {
|
||||
pub fn fork(&self) -> Result<VMMemory, wasmer_types::MemoryError> {
|
||||
let new_memory = crate::Memory::new_internal(self.ty.clone())?;
|
||||
|
||||
#[cfg(feature="tracing")]
|
||||
#[cfg(feature = "tracing")]
|
||||
trace!("memory copy started");
|
||||
|
||||
let src = MemoryView::new_raw(&self.memory);
|
||||
@@ -56,7 +59,8 @@ impl VMMemory {
|
||||
let delta = amount - dst_size;
|
||||
let pages = ((delta - 1) / WASM_PAGE_SIZE) + 1;
|
||||
|
||||
let our_js_memory: &crate::js::externals::memory::JSMemory = JsCast::unchecked_from_js_ref(&new_memory);
|
||||
let our_js_memory: &crate::js::externals::memory::JSMemory =
|
||||
JsCast::unchecked_from_js_ref(&new_memory);
|
||||
our_js_memory.grow(pages as u32).map_err(|err| {
|
||||
if err.is_instance_of::<js_sys::RangeError>() {
|
||||
let cur_pages = dst_size;
|
||||
@@ -72,20 +76,17 @@ impl VMMemory {
|
||||
dst = MemoryView::new_raw(&new_memory);
|
||||
}
|
||||
|
||||
src.copy_to_memory(amount as u64, &dst)
|
||||
.map_err(|err| {
|
||||
wasmer_types::MemoryError::Generic(format!("failed to copy the memory - {}", err))
|
||||
})?;
|
||||
src.copy_to_memory(amount as u64, &dst).map_err(|err| {
|
||||
wasmer_types::MemoryError::Generic(format!("failed to copy the memory - {}", err))
|
||||
})?;
|
||||
|
||||
#[cfg(feature="tracing")]
|
||||
#[cfg(feature = "tracing")]
|
||||
trace!("memory copy finished (size={})", dst.size().bytes().0);
|
||||
|
||||
Ok(
|
||||
Self {
|
||||
memory: new_memory,
|
||||
ty: self.ty.clone(),
|
||||
}
|
||||
)
|
||||
Ok(Self {
|
||||
memory: new_memory,
|
||||
ty: self.ty.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,9 +104,7 @@ impl VMGlobal {
|
||||
/// Saves the global value into the snapshot
|
||||
pub fn save_snapshot(&self, index: usize, snapshot: &mut StoreSnapshot) {
|
||||
if let Some(val) = self.global.as_f64() {
|
||||
let entry = snapshot.globals
|
||||
.entry(index as u32)
|
||||
.or_default();
|
||||
let entry = snapshot.globals.entry(index as u32).or_default();
|
||||
*entry = val as u128;
|
||||
}
|
||||
}
|
||||
|
||||
29
lib/api/src/js/externals/function.rs
vendored
29
lib/api/src/js/externals/function.rs
vendored
@@ -61,13 +61,9 @@ pub struct Function {
|
||||
pub(crate) handle: StoreHandle<VMFunction>,
|
||||
}
|
||||
|
||||
impl Into<Function>
|
||||
for StoreHandle<VMFunction>
|
||||
{
|
||||
impl Into<Function> for StoreHandle<VMFunction> {
|
||||
fn into(self) -> Function {
|
||||
Function {
|
||||
handle: self
|
||||
}
|
||||
Function { handle: self }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -405,7 +401,10 @@ impl Function {
|
||||
) -> Result<Box<[Value]>, RuntimeError> {
|
||||
#[allow(unused_unsafe)]
|
||||
let params: Vec<_> = unsafe {
|
||||
params.iter().map(|a| a.as_raw_value(&store.as_store_ref())).collect()
|
||||
params
|
||||
.iter()
|
||||
.map(|a| a.as_raw_value(&store.as_store_ref()))
|
||||
.collect()
|
||||
};
|
||||
let arr = js_sys::Array::new_with_length(params.len() as u32);
|
||||
|
||||
@@ -428,10 +427,16 @@ impl Function {
|
||||
let store_mut = store.as_store_mut();
|
||||
if let Some(callback) = store_mut.inner.on_called.take() {
|
||||
match callback(store_mut) {
|
||||
Ok(wasmer_types::OnCalledAction::InvokeAgain) => { continue; }
|
||||
Ok(wasmer_types::OnCalledAction::Finish) => { break; }
|
||||
Ok(wasmer_types::OnCalledAction::Trap(trap)) => { return Err(RuntimeError::user(trap)) },
|
||||
Err(trap) => { return Err(RuntimeError::user(trap)) },
|
||||
Ok(wasmer_types::OnCalledAction::InvokeAgain) => {
|
||||
continue;
|
||||
}
|
||||
Ok(wasmer_types::OnCalledAction::Finish) => {
|
||||
break;
|
||||
}
|
||||
Ok(wasmer_types::OnCalledAction::Trap(trap)) => {
|
||||
return Err(RuntimeError::user(trap))
|
||||
}
|
||||
Err(trap) => return Err(RuntimeError::user(trap)),
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -1458,4 +1463,4 @@ mod inner {
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
17
lib/api/src/js/externals/memory.rs
vendored
17
lib/api/src/js/externals/memory.rs
vendored
@@ -101,13 +101,15 @@ impl Memory {
|
||||
let js_memory = js_sys::WebAssembly::Memory::new(&descriptor)
|
||||
.map_err(|_e| MemoryError::Generic("Error while creating the memory".to_owned()))?;
|
||||
|
||||
Ok(
|
||||
js_memory
|
||||
)
|
||||
Ok(js_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> {
|
||||
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))
|
||||
}
|
||||
@@ -199,8 +201,7 @@ impl Memory {
|
||||
&self,
|
||||
store: &impl AsStoreRef,
|
||||
new_store: &mut impl AsStoreMut,
|
||||
) -> Result<Self, MemoryError>
|
||||
{
|
||||
) -> Result<Self, MemoryError> {
|
||||
// Create the new memory using the parameters of the existing memory
|
||||
let view = self.view(store);
|
||||
let ty = self.ty(store);
|
||||
@@ -218,9 +219,7 @@ impl Memory {
|
||||
|
||||
// Copy the bytes
|
||||
view.copy_to_memory(amount as u64, &new_view)
|
||||
.map_err(|err| {
|
||||
MemoryError::Generic(err.to_string())
|
||||
})?;
|
||||
.map_err(|err| MemoryError::Generic(err.to_string()))?;
|
||||
|
||||
// Return the new memory
|
||||
Ok(new_memory)
|
||||
|
||||
6
lib/api/src/js/externals/memory_view.rs
vendored
6
lib/api/src/js/externals/memory_view.rs
vendored
@@ -27,9 +27,7 @@ pub struct MemoryView<'a> {
|
||||
|
||||
impl<'a> MemoryView<'a> {
|
||||
pub(crate) fn new(memory: &Memory, store: &impl AsStoreRef) -> Self {
|
||||
let memory = memory
|
||||
.handle
|
||||
.get(store.as_store_ref().objects());
|
||||
let memory = memory.handle.get(store.as_store_ref().objects());
|
||||
Self::new_raw(&memory.memory)
|
||||
}
|
||||
|
||||
@@ -279,7 +277,7 @@ impl<'a> MemoryView<'a> {
|
||||
self.read(offset, &mut chunk[..sublen])?;
|
||||
|
||||
new_memory.write(offset, &chunk[..sublen])?;
|
||||
|
||||
|
||||
offset += sublen as u64;
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -184,27 +184,23 @@ impl Imports {
|
||||
}
|
||||
|
||||
pub struct ImportsIterator<'a> {
|
||||
iter: std::collections::hash_map::Iter<'a, (String, String), Extern>
|
||||
iter: std::collections::hash_map::Iter<'a, (String, String), Extern>,
|
||||
}
|
||||
|
||||
impl<'a> ImportsIterator<'a>
|
||||
{
|
||||
impl<'a> ImportsIterator<'a> {
|
||||
fn new(imports: &'a Imports) -> Self {
|
||||
let iter = imports.map.iter();
|
||||
Self { iter }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator
|
||||
for ImportsIterator<'a> {
|
||||
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)
|
||||
})
|
||||
.map(|(k, v)| (k.0.as_str(), k.1.as_str(), v))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -127,7 +127,11 @@ impl Instance {
|
||||
// If the memory is imported then also export it for backwards compatibility reasons
|
||||
// (many will assume the memory is always exported) - later we can remove this
|
||||
if exports.get_memory("memory").is_err() {
|
||||
if let Some(memory) = externs.iter().filter(|a| a.ty(store).memory().is_some()).next() {
|
||||
if let Some(memory) = externs
|
||||
.iter()
|
||||
.filter(|a| a.ty(store).memory().is_some())
|
||||
.next()
|
||||
{
|
||||
exports.insert("memory", memory.clone());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,14 +73,14 @@ 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,
|
||||
WASM_MIN_PAGES, WASM_PAGE_SIZE, OnCalledAction, StoreSnapshot
|
||||
Bytes, ExportIndex, GlobalInit, LocalFunctionIndex, OnCalledAction, Pages, StoreSnapshot,
|
||||
ValueType, WASM_MAX_PAGES, WASM_MIN_PAGES, WASM_PAGE_SIZE,
|
||||
};
|
||||
|
||||
#[cfg(feature = "wat")]
|
||||
|
||||
@@ -14,16 +14,15 @@ use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use bytes::Bytes;
|
||||
#[cfg(feature = "std")]
|
||||
use thiserror::Error;
|
||||
#[cfg(feature = "tracing")]
|
||||
use tracing::{debug, warn};
|
||||
use wasm_bindgen::JsValue;
|
||||
use wasmer_types::{
|
||||
ExportsIterator, ExternType, FunctionType, GlobalType, ImportsIterator, MemoryType, Mutability,
|
||||
Pages, TableType, Type,
|
||||
};
|
||||
#[cfg(feature = "tracing")]
|
||||
use tracing::{debug, warn};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "std", derive(Error))]
|
||||
@@ -111,27 +110,23 @@ pub struct Module {
|
||||
raw_bytes: Option<Bytes>,
|
||||
}
|
||||
|
||||
pub trait IntoBytes
|
||||
{
|
||||
pub trait IntoBytes {
|
||||
fn into_bytes(self) -> Bytes;
|
||||
}
|
||||
|
||||
impl IntoBytes
|
||||
for Bytes {
|
||||
impl IntoBytes for Bytes {
|
||||
fn into_bytes(self) -> Bytes {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoBytes
|
||||
for Vec<u8> {
|
||||
impl IntoBytes for Vec<u8> {
|
||||
fn into_bytes(self) -> Bytes {
|
||||
Bytes::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoBytes
|
||||
for &[u8] {
|
||||
impl IntoBytes for &[u8] {
|
||||
fn into_bytes(self) -> Bytes {
|
||||
Bytes::from(self.to_vec())
|
||||
}
|
||||
@@ -227,7 +222,10 @@ impl Module {
|
||||
/// Opposed to [`Module::new`], this function is not compatible with
|
||||
/// the WebAssembly text format (if the "wat" feature is enabled for
|
||||
/// this crate).
|
||||
pub fn from_binary(_store: &impl AsStoreRef, binary: impl IntoBytes) -> Result<Self, CompileError> {
|
||||
pub fn from_binary(
|
||||
_store: &impl AsStoreRef,
|
||||
binary: impl IntoBytes,
|
||||
) -> Result<Self, CompileError> {
|
||||
let binary = binary.into_bytes();
|
||||
//
|
||||
// Self::validate(store, binary)?;
|
||||
@@ -366,7 +364,11 @@ impl Module {
|
||||
import_externs.push(import);
|
||||
} else {
|
||||
#[cfg(feature = "tracing")]
|
||||
warn!("import not found {}:{}", import_type.module(), import_type.name());
|
||||
warn!(
|
||||
"import not found {}:{}",
|
||||
import_type.module(),
|
||||
import_type.name()
|
||||
);
|
||||
}
|
||||
// in case the import is not found, the JS Wasm VM will handle
|
||||
// the error for us, so we don't need to handle it
|
||||
@@ -676,8 +678,7 @@ impl Module {
|
||||
/// between threads except via a post_message())
|
||||
pub fn is_ok(&self) -> bool {
|
||||
let val = JsValue::from(&self.module);
|
||||
!val.is_undefined() &&
|
||||
!val.is_null()
|
||||
!val.is_undefined() && !val.is_null()
|
||||
}
|
||||
|
||||
// /// Get the custom sections of the module given a `name`.
|
||||
|
||||
@@ -6,7 +6,14 @@ use wasmer_types::OnCalledAction;
|
||||
/// wrap the actual context in a box.
|
||||
pub(crate) struct StoreInner {
|
||||
pub(crate) objects: StoreObjects,
|
||||
pub(crate) on_called: Option<Box<dyn FnOnce(StoreMut) -> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>>>>,
|
||||
pub(crate) on_called: Option<
|
||||
Box<
|
||||
dyn FnOnce(
|
||||
StoreMut,
|
||||
)
|
||||
-> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>>,
|
||||
>,
|
||||
>,
|
||||
}
|
||||
|
||||
/// The store represents all global state that can be manipulated by
|
||||
@@ -40,7 +47,7 @@ impl Store {
|
||||
pub fn same(_a: &Self, _b: &Self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
/// Returns the ID of this store
|
||||
pub fn id(&self) -> StoreId {
|
||||
self.inner.objects.id()
|
||||
@@ -149,11 +156,12 @@ impl<'a> StoreMut<'a> {
|
||||
}
|
||||
|
||||
/// Sets the unwind callback which will be invoked when the call finishes
|
||||
pub fn on_called<F>(
|
||||
&mut self,
|
||||
callback: F,
|
||||
)
|
||||
where F: FnOnce(StoreMut<'_>) -> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>> + Send + Sync + 'static,
|
||||
pub fn on_called<F>(&mut self, callback: F)
|
||||
where
|
||||
F: FnOnce(StoreMut<'_>) -> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>>
|
||||
+ Send
|
||||
+ Sync
|
||||
+ 'static,
|
||||
{
|
||||
self.inner.on_called.replace(Box::new(callback));
|
||||
}
|
||||
@@ -328,7 +336,7 @@ mod objects {
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
|
||||
/// Serializes the mutable things into a snapshot
|
||||
pub fn restore_snapshot(&mut self, snapshot: &wasmer_types::StoreSnapshot) {
|
||||
for (index, global) in self.globals.iter_mut().enumerate() {
|
||||
|
||||
@@ -57,8 +57,6 @@ impl AsJs for Value {
|
||||
|
||||
impl AsJs for wasmer_types::RawValue {
|
||||
fn as_jsvalue(&self, _store: &impl AsStoreRef) -> JsValue {
|
||||
unsafe {
|
||||
JsValue::from_f64(self.f64)
|
||||
}
|
||||
unsafe { JsValue::from_f64(self.f64) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
use std::string::{String, ToString};
|
||||
|
||||
use wasmer_types::Type;
|
||||
use wasmer_types::RawValue;
|
||||
use wasmer_types::Type;
|
||||
|
||||
//use crate::ExternRef;
|
||||
use crate::js::externals::function::Function;
|
||||
@@ -110,7 +110,7 @@ impl Value {
|
||||
/// Converts the `Value` into a `RawValue`.
|
||||
pub unsafe fn as_raw_value(&self, store: &impl AsStoreRef) -> RawValue {
|
||||
RawValue {
|
||||
f64: self.as_raw(store)
|
||||
f64: self.as_raw(store),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
30
lib/api/src/sys/externals/function.rs
vendored
30
lib/api/src/sys/externals/function.rs
vendored
@@ -41,13 +41,9 @@ pub struct Function {
|
||||
pub(crate) handle: StoreHandle<VMFunction>,
|
||||
}
|
||||
|
||||
impl Into<Function>
|
||||
for StoreHandle<VMFunction>
|
||||
{
|
||||
impl Into<Function> for StoreHandle<VMFunction> {
|
||||
fn into(self) -> Function {
|
||||
Function {
|
||||
handle: self
|
||||
}
|
||||
Function { handle: self }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -424,7 +420,6 @@ impl Function {
|
||||
mut params: Vec<RawValue>,
|
||||
results: &mut [Value],
|
||||
) -> Result<(), RuntimeError> {
|
||||
|
||||
// Call the trampoline.
|
||||
let result = {
|
||||
let mut r;
|
||||
@@ -442,20 +437,23 @@ impl Function {
|
||||
let store_mut = store.as_store_mut();
|
||||
if let Some(callback) = store_mut.inner.on_called.take() {
|
||||
match callback(store_mut) {
|
||||
Ok(wasmer_types::OnCalledAction::InvokeAgain) => { continue; }
|
||||
Ok(wasmer_types::OnCalledAction::Finish) => { break; }
|
||||
Ok(wasmer_types::OnCalledAction::Trap(trap)) => { return Err(RuntimeError::user(trap)) },
|
||||
Err(trap) => {
|
||||
Ok(wasmer_types::OnCalledAction::InvokeAgain) => {
|
||||
continue;
|
||||
}
|
||||
Ok(wasmer_types::OnCalledAction::Finish) => {
|
||||
break;
|
||||
}
|
||||
Ok(wasmer_types::OnCalledAction::Trap(trap)) => {
|
||||
return Err(RuntimeError::user(trap))
|
||||
},
|
||||
}
|
||||
Err(trap) => return Err(RuntimeError::user(trap)),
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
r
|
||||
};
|
||||
if let Err(error) = result
|
||||
{
|
||||
if let Err(error) = result {
|
||||
return Err(RuntimeError::from_trap(error));
|
||||
}
|
||||
|
||||
@@ -1349,7 +1347,7 @@ mod inner {
|
||||
let mut store = StoreMut::from_raw(env.raw_store as *mut _);
|
||||
let result = on_host_stack(|| {
|
||||
// println!("func wrapper1");
|
||||
panic::catch_unwind(AssertUnwindSafe(|| {
|
||||
panic::catch_unwind(AssertUnwindSafe(|| {
|
||||
$(
|
||||
let $x = FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x));
|
||||
)*
|
||||
@@ -1715,4 +1713,4 @@ mod inner {
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
14
lib/api/src/sys/externals/memory.rs
vendored
14
lib/api/src/sys/externals/memory.rs
vendored
@@ -10,7 +10,7 @@ use std::mem::MaybeUninit;
|
||||
use std::slice;
|
||||
#[cfg(feature = "tracing")]
|
||||
use tracing::warn;
|
||||
use wasmer_types::{Pages, LinearMemory, WASM_PAGE_SIZE};
|
||||
use wasmer_types::{LinearMemory, Pages, WASM_PAGE_SIZE};
|
||||
use wasmer_vm::{InternalStoreHandle, MemoryError, StoreHandle, VMExtern, VMMemory};
|
||||
|
||||
use super::MemoryView;
|
||||
@@ -63,7 +63,7 @@ impl 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 {
|
||||
handle: StoreHandle::new(new_store.objects_mut(), memory)
|
||||
handle: StoreHandle::new(new_store.objects_mut(), memory),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,8 +138,7 @@ impl Memory {
|
||||
&self,
|
||||
store: &impl AsStoreRef,
|
||||
new_store: &mut impl AsStoreMut,
|
||||
) -> Result<Self, MemoryError>
|
||||
{
|
||||
) -> Result<Self, MemoryError> {
|
||||
// Create the new memory using the parameters of the existing memory
|
||||
let view = self.view(store);
|
||||
let ty = self.ty(store);
|
||||
@@ -157,9 +156,7 @@ impl Memory {
|
||||
|
||||
// Copy the bytes
|
||||
view.copy_to_memory(amount as u64, &new_view)
|
||||
.map_err(|err| {
|
||||
MemoryError::Generic(err.to_string())
|
||||
})?;
|
||||
.map_err(|err| MemoryError::Generic(err.to_string()))?;
|
||||
|
||||
// Return the new memory
|
||||
Ok(new_memory)
|
||||
@@ -184,8 +181,7 @@ 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()
|
||||
.map(|mem| mem.into())
|
||||
mem.try_clone().map(|mem| mem.into())
|
||||
}
|
||||
|
||||
pub(crate) fn to_vm_extern(&self) -> VMExtern {
|
||||
|
||||
4
lib/api/src/sys/externals/memory_view.rs
vendored
4
lib/api/src/sys/externals/memory_view.rs
vendored
@@ -4,7 +4,7 @@ use std::convert::TryInto;
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::slice;
|
||||
use wasmer_types::{Pages, LinearMemory};
|
||||
use wasmer_types::{LinearMemory, Pages};
|
||||
|
||||
use super::memory::MemoryBuffer;
|
||||
use super::Memory;
|
||||
@@ -184,7 +184,7 @@ impl<'a> MemoryView<'a> {
|
||||
self.read(offset, &mut chunk[..sublen])?;
|
||||
|
||||
new_memory.write(offset, &chunk[..sublen])?;
|
||||
|
||||
|
||||
offset += sublen as u64;
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! The import module contains the implementation data structures and helper functions used to
|
||||
//! manipulate and access a wasm module's imports including memories, tables, globals, and
|
||||
//! functions.
|
||||
use crate::{Exports, Extern, Module, AsStoreMut, Memory};
|
||||
use crate::{AsStoreMut, Exports, Extern, Memory, Module};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use wasmer_compiler::LinkError;
|
||||
@@ -114,7 +114,11 @@ impl Imports {
|
||||
|
||||
/// Imports (any) shared memory into the imports.
|
||||
/// (if the module does not import memory then this function is ignored)
|
||||
pub fn import_shared_memory(&mut self, module: &Module, store: &mut impl AsStoreMut) -> Option<VMSharedMemory> {
|
||||
pub fn import_shared_memory(
|
||||
&mut self,
|
||||
module: &Module,
|
||||
store: &mut impl AsStoreMut,
|
||||
) -> Option<VMSharedMemory> {
|
||||
// Determine if shared memory needs to be created and imported
|
||||
let shared_memory = module
|
||||
.imports()
|
||||
@@ -122,16 +126,16 @@ impl Imports {
|
||||
.next()
|
||||
.map(|a| *a.ty())
|
||||
.map(|ty| {
|
||||
let style = store
|
||||
.as_store_ref()
|
||||
.tunables()
|
||||
.memory_style(&ty);
|
||||
VMSharedMemory::new(&ty, &style)
|
||||
.unwrap()
|
||||
let style = store.as_store_ref().tunables().memory_style(&ty);
|
||||
VMSharedMemory::new(&ty, &style).unwrap()
|
||||
});
|
||||
|
||||
if let Some(memory) = shared_memory {
|
||||
self.define("env", "memory", Memory::new_from_existing(store, memory.clone().into()));
|
||||
self.define(
|
||||
"env",
|
||||
"memory",
|
||||
Memory::new_from_existing(store, memory.clone().into()),
|
||||
);
|
||||
Some(memory)
|
||||
} else {
|
||||
None
|
||||
@@ -184,27 +188,23 @@ impl Imports {
|
||||
}
|
||||
|
||||
pub struct ImportsIterator<'a> {
|
||||
iter: std::collections::hash_map::Iter<'a, (String, String), Extern>
|
||||
iter: std::collections::hash_map::Iter<'a, (String, String), Extern>,
|
||||
}
|
||||
|
||||
impl<'a> ImportsIterator<'a>
|
||||
{
|
||||
impl<'a> ImportsIterator<'a> {
|
||||
fn new(imports: &'a Imports) -> Self {
|
||||
let iter = imports.map.iter();
|
||||
Self { iter }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator
|
||||
for ImportsIterator<'a> {
|
||||
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)
|
||||
})
|
||||
.map(|(k, v)| (k.0.as_str(), k.1.as_str(), v))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ impl Instance {
|
||||
module: &Module,
|
||||
imports: &Imports,
|
||||
) -> Result<Self, InstantiationError> {
|
||||
let externs = imports
|
||||
let externs = imports
|
||||
.imports_for_module(module)
|
||||
.map_err(InstantiationError::Link)?;
|
||||
let mut handle = module.instantiate(store, &externs)?;
|
||||
@@ -128,11 +128,15 @@ impl Instance {
|
||||
(name, extern_)
|
||||
})
|
||||
.collect::<Exports>();
|
||||
|
||||
|
||||
// If the memory is imported then also export it for backwards compatibility reasons
|
||||
// (many will assume the memory is always exported) - later we can remove this
|
||||
if exports.get_memory("memory").is_err() {
|
||||
if let Some(memory) = externs.iter().filter(|a| a.ty(store).memory().is_some()).next() {
|
||||
if let Some(memory) = externs
|
||||
.iter()
|
||||
.filter(|a| a.ty(store).memory().is_some())
|
||||
.next()
|
||||
{
|
||||
exports.insert("memory", memory.clone());
|
||||
}
|
||||
}
|
||||
@@ -177,7 +181,11 @@ impl Instance {
|
||||
// If the memory is imported then also export it for backwards compatibility reasons
|
||||
// (many will assume the memory is always exported) - later we can remove this
|
||||
if exports.get_memory("memory").is_err() {
|
||||
if let Some(memory) = externs.iter().filter(|a| a.ty(store).memory().is_some()).next() {
|
||||
if let Some(memory) = externs
|
||||
.iter()
|
||||
.filter(|a| a.ty(store).memory().is_some())
|
||||
.next()
|
||||
{
|
||||
exports.insert("memory", memory.clone());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,7 +347,7 @@ impl<'a, T: ValueType> WasmSlice<'a, T> {
|
||||
ret.set_len(len);
|
||||
}
|
||||
Ok(ret)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: ValueType> fmt::Debug for WasmSlice<'a, T> {
|
||||
|
||||
@@ -42,7 +42,7 @@ pub use wasmer_derive::ValueType;
|
||||
pub use wasmer_types::is_wasm;
|
||||
pub use wasmer_types::{
|
||||
CpuFeature, ExportType, ExternType, FunctionType, GlobalType, ImportType, MemoryType,
|
||||
Mutability, TableType, Target, Type, OnCalledAction, StoreSnapshot
|
||||
Mutability, OnCalledAction, StoreSnapshot, TableType, Target, Type,
|
||||
};
|
||||
|
||||
pub use wasmer_types::{
|
||||
@@ -57,8 +57,8 @@ pub mod vm {
|
||||
//! The `vm` module re-exports wasmer-vm types.
|
||||
|
||||
pub use wasmer_vm::{
|
||||
MemoryError, MemoryStyle, TableStyle, VMExtern, VMMemory, VMMemoryDefinition, VMTable,
|
||||
VMTableDefinition, VMOwnedMemory, VMSharedMemory
|
||||
MemoryError, MemoryStyle, TableStyle, VMExtern, VMMemory, VMMemoryDefinition,
|
||||
VMOwnedMemory, VMSharedMemory, VMTable, VMTableDefinition,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,10 @@ use crate::sys::InstantiationError;
|
||||
use crate::AsStoreMut;
|
||||
use crate::AsStoreRef;
|
||||
use bytes::Bytes;
|
||||
use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use bytes::Bytes;
|
||||
use thiserror::Error;
|
||||
use wasmer_compiler::Artifact;
|
||||
use wasmer_compiler::ArtifactCreate;
|
||||
@@ -57,34 +55,29 @@ pub struct Module {
|
||||
module_info: Arc<ModuleInfo>,
|
||||
}
|
||||
|
||||
pub trait IntoBytes
|
||||
{
|
||||
pub trait IntoBytes {
|
||||
fn into_bytes(self) -> Bytes;
|
||||
}
|
||||
|
||||
impl IntoBytes
|
||||
for Bytes {
|
||||
impl IntoBytes for Bytes {
|
||||
fn into_bytes(self) -> Bytes {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoBytes
|
||||
for Vec<u8> {
|
||||
impl IntoBytes for Vec<u8> {
|
||||
fn into_bytes(self) -> Bytes {
|
||||
Bytes::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoBytes
|
||||
for &Vec<u8> {
|
||||
impl IntoBytes for &Vec<u8> {
|
||||
fn into_bytes(self) -> Bytes {
|
||||
Bytes::from(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoBytes
|
||||
for &[u8] {
|
||||
impl IntoBytes for &[u8] {
|
||||
fn into_bytes(self) -> Bytes {
|
||||
Bytes::from(self.to_vec())
|
||||
}
|
||||
@@ -96,8 +89,7 @@ impl<const N: usize> IntoBytes for &[u8; N] {
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoBytes
|
||||
for &str {
|
||||
impl IntoBytes for &str {
|
||||
fn into_bytes(self) -> Bytes {
|
||||
Bytes::from(self.as_bytes().to_vec())
|
||||
}
|
||||
@@ -203,7 +195,10 @@ impl Module {
|
||||
/// Opposed to [`Module::new`], this function is not compatible with
|
||||
/// the WebAssembly text format (if the "wat" feature is enabled for
|
||||
/// this crate).
|
||||
pub fn from_binary(store: &impl AsStoreRef, binary: impl IntoBytes) -> Result<Self, CompileError> {
|
||||
pub fn from_binary(
|
||||
store: &impl AsStoreRef,
|
||||
binary: impl IntoBytes,
|
||||
) -> Result<Self, CompileError> {
|
||||
let binary = binary.into_bytes();
|
||||
Self::validate(store, binary.clone())?;
|
||||
unsafe { Self::from_binary_unchecked(store, binary) }
|
||||
@@ -265,8 +260,7 @@ impl Module {
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn serialize(&self) -> Result<Bytes, SerializeError> {
|
||||
self.artifact.serialize()
|
||||
.map(|bytes| bytes.into())
|
||||
self.artifact.serialize().map(|bytes| bytes.into())
|
||||
}
|
||||
|
||||
/// Serializes a module into a file that the `Engine`
|
||||
@@ -508,7 +502,6 @@ impl Module {
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
/// Get the custom sections of the module given a `name`.
|
||||
///
|
||||
/// # Important
|
||||
|
||||
@@ -7,16 +7,14 @@
|
||||
//! let add_one = instance.exports.get_function("function_name")?;
|
||||
//! let add_one_native: TypedFunction<i32, i32> = add_one.native().unwrap();
|
||||
//! ```
|
||||
use std::marker::PhantomData;
|
||||
use std::cell::Cell;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::StoreMut;
|
||||
use crate::sys::{
|
||||
AsStoreMut, FromToNativeWasmType, Function, NativeWasmTypeInto, RuntimeError, WasmTypeList,
|
||||
};
|
||||
use wasmer_types::{
|
||||
RawValue, OnCalledAction
|
||||
};
|
||||
use crate::StoreMut;
|
||||
use wasmer_types::{OnCalledAction, RawValue};
|
||||
|
||||
/// A WebAssembly function that can be called natively
|
||||
/// (using the Native ABI).
|
||||
|
||||
@@ -215,7 +215,10 @@ impl<M: MemorySize> WasmPtr<u8, M> {
|
||||
/// This method is safe to call even if the memory is being concurrently
|
||||
/// modified.
|
||||
#[inline]
|
||||
pub fn read_utf8_string_with_nul(&self, view: &MemoryView) -> Result<String, MemoryAccessError> {
|
||||
pub fn read_utf8_string_with_nul(
|
||||
&self,
|
||||
view: &MemoryView,
|
||||
) -> Result<String, MemoryAccessError> {
|
||||
let vec = self.read_until(view, |&byte| byte == 0)?;
|
||||
Ok(String::from_utf8(vec)?)
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::sys::tunables::BaseTunables;
|
||||
use derivative::Derivative;
|
||||
use std::fmt;
|
||||
#[cfg(feature = "compiler")]
|
||||
use wasmer_compiler::{Engine, EngineBuilder, Tunables};
|
||||
use wasmer_types::{OnCalledAction, StoreSnapshot};
|
||||
use wasmer_vm::{init_traps, TrapHandler, TrapHandlerFn, StoreId};
|
||||
use derivative::Derivative;
|
||||
use wasmer_vm::{init_traps, StoreId, TrapHandler, TrapHandlerFn};
|
||||
|
||||
use wasmer_vm::StoreObjects;
|
||||
|
||||
@@ -24,11 +24,17 @@ pub(crate) struct StoreInner {
|
||||
#[derivative(Debug = "ignore")]
|
||||
pub(crate) trap_handler: Option<Box<TrapHandlerFn<'static>>>,
|
||||
#[derivative(Debug = "ignore")]
|
||||
pub(crate) on_called: Option<Box<dyn FnOnce(StoreMut<'_>) -> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>>>>,
|
||||
pub(crate) on_called: Option<
|
||||
Box<
|
||||
dyn FnOnce(
|
||||
StoreMut<'_>,
|
||||
)
|
||||
-> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>>,
|
||||
>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl StoreInner
|
||||
{
|
||||
impl StoreInner {
|
||||
// Serializes the mutable things into a snapshot
|
||||
pub fn save_snapshot(&self) -> StoreSnapshot {
|
||||
self.objects.save_snapshot()
|
||||
@@ -324,17 +330,16 @@ impl<'a> StoreMut<'a> {
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn from_raw(raw: *mut StoreInner) -> Self {
|
||||
Self {
|
||||
inner: &mut *raw,
|
||||
}
|
||||
Self { inner: &mut *raw }
|
||||
}
|
||||
|
||||
/// Sets the unwind callback which will be invoked when the call finishes
|
||||
pub fn on_called<F>(
|
||||
&mut self,
|
||||
callback: F,
|
||||
)
|
||||
where F: FnOnce(StoreMut<'_>) -> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>> + Send + Sync + 'static,
|
||||
pub fn on_called<F>(&mut self, callback: F)
|
||||
where
|
||||
F: FnOnce(StoreMut<'_>) -> Result<OnCalledAction, Box<dyn std::error::Error + Send + Sync>>
|
||||
+ Send
|
||||
+ Sync
|
||||
+ 'static,
|
||||
{
|
||||
self.inner.on_called.replace(Box::new(callback));
|
||||
}
|
||||
@@ -368,9 +373,7 @@ impl AsStoreRef for StoreMut<'_> {
|
||||
}
|
||||
impl AsStoreMut for StoreMut<'_> {
|
||||
fn as_store_mut(&mut self) -> StoreMut<'_> {
|
||||
StoreMut {
|
||||
inner: self.inner,
|
||||
}
|
||||
StoreMut { inner: self.inner }
|
||||
}
|
||||
fn objects_mut(&mut self) -> &mut StoreObjects {
|
||||
&mut self.inner.objects
|
||||
|
||||
Reference in New Issue
Block a user