Migrated internal samples to new Context API

This commit is contained in:
ptitSeb
2022-06-30 15:37:39 +02:00
committed by Manos Pitsidianakis
parent 267a09ce8e
commit d12759f150
12 changed files with 156 additions and 96 deletions

View File

@@ -41,6 +41,7 @@
//!
//! ```rust
//! use wasmer::{Store, Module, Instance, Value, imports};
//! use wasmer::Context as WasmerContext;
//!
//! fn main() -> anyhow::Result<()> {
//! let module_wat = r#"
@@ -53,13 +54,14 @@
//! "#;
//!
//! let store = Store::default();
//! let mut ctx = WasmerContext::new(&store, ());
//! let module = Module::new(&store, &module_wat)?;
//! // The module doesn't import anything, so we create an empty import object.
//! let import_object = imports! {};
//! let instance = Instance::new(&module, &import_object)?;
//! let instance = Instance::new(&mut ctx, &module, &import_object)?;
//!
//! let add_one = instance.exports.get_function("add_one")?;
//! let result = add_one.call(&[Value::I32(42)])?;
//! let result = add_one.call(&mut ctx, &[Value::I32(42)])?;
//! assert_eq!(result[0], Value::I32(43));
//!
//! Ok(())
@@ -150,11 +152,12 @@
//!
//! ```
//! # use wasmer::{imports, Function, Memory, MemoryType, Store, Imports};
//! # fn imports_example(store: &Store) -> Imports {
//! let memory = Memory::new(&store, MemoryType::new(1, None, false)).unwrap();
//! # use wasmer::ContextMut;
//! # fn imports_example(mut ctx: ContextMut<()>, store: &Store) -> Imports {
//! let memory = Memory::new(&mut ctx, MemoryType::new(1, None, false)).unwrap();
//! imports! {
//! "env" => {
//! "my_function" => Function::new_native(store, || println!("Hello")),
//! "my_function" => Function::new_native(&mut ctx, |_ctx: ContextMut<()>| println!("Hello")),
//! "memory" => memory,
//! }
//! }
@@ -165,12 +168,12 @@
//! from any instance via `instance.exports`:
//!
//! ```
//! # use wasmer::{imports, Instance, Function, Memory, TypedFunction};
//! # fn exports_example(instance: &Instance) -> anyhow::Result<()> {
//! # use wasmer::{imports, Instance, Function, Memory, TypedFunction, ContextMut};
//! # fn exports_example(mut ctx: ContextMut<()>, instance: &Instance) -> anyhow::Result<()> {
//! let memory = instance.exports.get_memory("memory")?;
//! let memory: &Memory = instance.exports.get("some_other_memory")?;
//! let add: TypedFunction<(i32, i32), i32> = instance.exports.get_typed_function("add")?;
//! let result = add.call(5, 37)?;
//! let add: TypedFunction<(i32, i32), i32> = instance.exports.get_typed_function(&mut ctx, "add")?;
//! let result = add.call(&mut ctx, 5, 37)?;
//! assert_eq!(result, 42);
//! # Ok(())
//! # }