Skip to content

Commit

Permalink
Merge branch 'wasix' of https://github.com/john-sharratt/wasmer into …
Browse files Browse the repository at this point in the history
…john-sharratt-wasix
  • Loading branch information
ptitSeb committed Aug 24, 2022
2 parents f7797a2 + 5890423 commit d947b95
Show file tree
Hide file tree
Showing 71 changed files with 3,679 additions and 3,640 deletions.
107 changes: 102 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions docs/migration_to_3.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,13 @@ import_object.define("env", "host_function", host_function);
let instance = Instance::new(&mut store, &module, &import_object).expect("Could not instantiate module.");
```

For WASI, don't forget to import memory to `WasiEnv`
For WASI, don't forget to initialize it

```rust
let mut wasi_env = WasiState::new("hello").finalize()?;
let import_object = wasi_env.import_object(&mut store, &module)?;
let instance = Instance::new(&mut store, &module, &import_object).expect("Could not instantiate module.");
let memory = instance.exports.get_memory("memory")?;
wasi_env.data_mut(&mut store).set_memory(memory.clone());
wasi_env.initialize(&mut store, &instance).unwrap();
```

#### `ChainableNamedResolver` is removed
Expand Down
4 changes: 2 additions & 2 deletions examples/imports_function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
fn get_counter(env: FunctionEnvMut<Env>) -> i32 {
*env.data().counter.lock().unwrap()
}
fn add_to_counter(mut env: FunctionEnvMut<Env>, add: i32) -> i32 {
let mut counter_ref = env.data_mut().counter.lock().unwrap();
fn add_to_counter(env: FunctionEnvMut<Env>, add: i32) -> i32 {
let mut counter_ref = env.data().counter.lock().unwrap();

*counter_ref += add;
*counter_ref
Expand Down
9 changes: 4 additions & 5 deletions examples/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("Creating `WasiEnv`...");
// First, we create the `WasiEnv`
let wasi_env = WasiState::new("hello")
let mut wasi_env = WasiState::new("hello")
// .args(&["world"])
// .env("KEY", "Value")
.finalize(&mut store)?;
Expand All @@ -50,10 +50,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let import_object = wasi_env.import_object(&mut store, &module)?;
let instance = Instance::new(&mut store, &module, &import_object)?;

println!("Attach WASI memory...");
// Attach the memory export
let memory = instance.exports.get_memory("memory")?;
wasi_env.data_mut(&mut store).set_memory(memory.clone());
println!("Initializing WASI environment...");
// Initialize the WASI environment (which will attach memory)
wasi_env.initialize(&mut store, &instance).unwrap();

println!("Call WASI `_start` function...");
// And we just call the `_start` function!
Expand Down
7 changes: 3 additions & 4 deletions examples/wasi_pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let import_object = wasi_env.import_object(&mut store, &module)?;
let instance = Instance::new(&mut store, &module, &import_object)?;

println!("Attach WASI memory...");
// Attach the memory export
let memory = instance.exports.get_memory("memory")?;
wasi_env.data_mut(&mut store).set_memory(memory.clone());
println!("Initializing WASI environment...");
// Initialize the WASI environment (which will attach memory)
wasi_env.initialize(&mut store, &instance).unwrap();

let msg = "racecar go zoom";
println!("Writing \"{}\" to the WASI stdin...", msg);
Expand Down
1 change: 1 addition & 0 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ indexmap = { version = "1.6" }
cfg-if = "1.0"
thiserror = "1.0"
more-asserts = "0.2"
bytes = "1"
# - Optional shared dependencies.
wat = { version = "1.0", optional = true }
tracing = { version = "0.1", optional = true }
Expand Down
3 changes: 2 additions & 1 deletion lib/api/src/js/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ unsafe impl Send for VMMemory {}
unsafe impl Sync for VMMemory {}

impl VMMemory {
pub(crate) fn new(memory: Memory, ty: MemoryType) -> Self {
/// Creates a new memory directly from a WebAssembly javascript object
pub fn new(memory: Memory, ty: MemoryType) -> Self {
Self { memory, ty }
}

Expand Down
3 changes: 3 additions & 0 deletions lib/api/src/js/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub enum ExportError {
/// This error arises when an export is missing
#[error("Missing export {0}")]
Missing(String),
/// This error arises when an export is missing
#[error("Serialization failed {0}")]
SerializationFailed(String),
}

/// Exports is a special kind of map that allows easily unwrapping
Expand Down
9 changes: 9 additions & 0 deletions lib/api/src/js/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ impl Memory {
Ok(Self::from_vm_export(store, vm_memory))
}

<<<<<<< HEAD
=======
/// 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))
}

>>>>>>> 5890423eee92efec64e6195ca69144702aa90b8a
/// 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)
Expand Down
20 changes: 12 additions & 8 deletions lib/api/src/js/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use std::fmt;
pub struct Instance {
_handle: StoreHandle<WebAssembly::Instance>,
module: Module,
#[allow(dead_code)]
imports: Imports,
/// The exports for an instance.
pub exports: Exports,
}
Expand Down Expand Up @@ -65,12 +63,11 @@ impl Instance {
module: &Module,
imports: &Imports,
) -> Result<Self, InstantiationError> {
let import_copy = imports.clone();
let (instance, _imports): (StoreHandle<WebAssembly::Instance>, Vec<Extern>) = module
let (instance, externs): (StoreHandle<WebAssembly::Instance>, Vec<Extern>) = module
.instantiate(&mut store, imports)
.map_err(|e| InstantiationError::Start(e))?;

let self_instance = Self::from_module_and_instance(store, module, instance, import_copy)?;
let self_instance = Self::from_module_and_instance(store, module, externs, instance)?;
//self_instance.init_envs(&imports.iter().map(Extern::to_export).collect::<Vec<_>>())?;
Ok(self_instance)
}
Expand All @@ -87,11 +84,11 @@ impl Instance {
pub fn from_module_and_instance(
mut store: &mut impl AsStoreMut,
module: &Module,
externs: Vec<Extern>,
instance: StoreHandle<WebAssembly::Instance>,
imports: Imports,
) -> Result<Self, InstantiationError> {
let instance_exports = instance.get(store.as_store_ref().objects()).exports();
let exports = module
let mut exports = module
.exports()
.map(|export_type| {
let name = export_type.name();
Expand All @@ -110,10 +107,17 @@ impl Instance {
})
.collect::<Result<Exports, InstantiationError>>()?;

// 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() {
exports.insert("memory", memory.clone());
}
}

Ok(Self {
_handle: instance,
module: module.clone(),
imports,
exports,
})
}
Expand Down
Loading

0 comments on commit d947b95

Please sign in to comment.