Implemented shared memory for Wasmer in preparation for multithreading

Fixed linter

Fixed clippy

Cleaned up some merge leftover
This commit is contained in:
John Sharratt's Shared Account
2022-08-17 17:45:48 +10:00
committed by ptitSeb
parent 0928629051
commit a5f641b4b0
10 changed files with 190 additions and 29 deletions

View File

@@ -73,7 +73,6 @@ 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;
}

View File

@@ -1,11 +1,12 @@
//! 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};
use crate::{AsStoreMut, Exports, Extern, Memory, Module};
use std::collections::HashMap;
use std::fmt;
use wasmer_compiler::LinkError;
use wasmer_types::ImportError;
use wasmer_vm::VMSharedMemory;
/// All of the import data used when instantiating.
///
@@ -111,6 +112,36 @@ impl Imports {
.insert((ns.to_string(), name.to_string()), val.into());
}
/// 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> {
// Determine if shared memory needs to be created and imported
let shared_memory = module
.imports()
.memories()
.next()
.map(|a| *a.ty())
.map(|ty| {
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()),
);
Some(memory)
} else {
None
}
}
/// Returns the contents of a namespace as an `Exports`.
///
/// Returns `None` if the namespace doesn't exist.

View File

@@ -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,
MemoryError, MemoryStyle, TableStyle, VMExtern, VMMemory, VMMemoryDefinition,
VMOwnedMemory, VMSharedMemory, VMTable, VMTableDefinition,
};
}