Skip to content

Commit

Permalink
Merge pull request #3028 from wasmerio/ctx-store-rename
Browse files Browse the repository at this point in the history
Ctx store rename
  • Loading branch information
syrusakbary authored Jul 20, 2022
2 parents a4b9d9e + 43138b5 commit 8f1a401
Show file tree
Hide file tree
Showing 44 changed files with 570 additions and 557 deletions.
2 changes: 1 addition & 1 deletion examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn main() -> anyhow::Result<()> {
let module = Module::new(&store, wasm_bytes)?;

// We declare the host function that we'll use to terminate execution.
fn early_exit(_ctx: FunctionEnvMut<()>) -> Result<(), ExitCode> {
fn early_exit(_env: FunctionEnvMut<()>) -> Result<(), ExitCode> {
// This is where it happens.
Err(ExitCode(1))
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn main() -> anyhow::Result<()> {

// We define a function to act as our "env" "say_hello" function imported in the
// Wasm program above.
fn say_hello_world(_ctx: FunctionEnvMut<'_, ()>) {
fn say_hello_world(_env: FunctionEnvMut<'_, ()>) {
println!("Hello, world!")
}

Expand Down
2 changes: 1 addition & 1 deletion examples/imports_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// covered in more detail in other examples.
println!("Creating the imported function...");
let host_function_signature = FunctionType::new(vec![], vec![Type::I32]);
let host_function = Function::new(&mut store, &env, &host_function_signature, |_ctx, _args| {
let host_function = Function::new(&mut store, &env, &host_function_signature, |_env, _args| {
Ok(vec![Value::I32(42)])
});

Expand Down
12 changes: 6 additions & 6 deletions examples/imports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine());
let mut ctx1 = FunctionEnv::new(&mut store, ());
let mut env1 = FunctionEnv::new(&mut store, ());
struct MyEnv;
let mut ctx2 = FunctionEnv::new(&mut store, MyEnv {});
let mut env2 = FunctionEnv::new(&mut store, MyEnv {});

println!("Compiling module...");
// Let's compile the Wasm module.
Expand All @@ -58,9 +58,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let multiply_dynamic_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]);
let multiply_dynamic = Function::new(
&mut store,
&ctx1,
&env1,
&multiply_dynamic_signature,
|_ctx, args| {
|_env, args| {
println!("Calling `multiply_dynamic`...");

let result = args[0].unwrap_i32() * 2;
Expand All @@ -71,15 +71,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
},
);

fn multiply(_ctx: FunctionEnvMut<MyEnv>, a: i32) -> i32 {
fn multiply(_env: FunctionEnvMut<MyEnv>, a: i32) -> i32 {
println!("Calling `multiply_native`...");
let result = a * 3;

println!("Result of `multiply_native`: {:?}", result);

result
}
let multiply_native = Function::new_native(&mut store, &ctx2, multiply);
let multiply_native = Function::new_native(&mut store, &env2, multiply);

// Create an import object.
let import_object = imports! {
Expand Down
8 changes: 4 additions & 4 deletions examples/imports_function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}

// Create the functions
fn get_counter(ctx: FunctionEnvMut<Env>) -> i32 {
*ctx.data().counter.lock().unwrap()
fn get_counter(env: FunctionEnvMut<Env>) -> i32 {
*env.data().counter.lock().unwrap()
}
fn add_to_counter(mut ctx: FunctionEnvMut<Env>, add: i32) -> i32 {
let mut counter_ref = ctx.data_mut().counter.lock().unwrap();
fn add_to_counter(mut env: FunctionEnvMut<Env>, add: i32) -> i32 {
let mut counter_ref = env.data_mut().counter.lock().unwrap();

*counter_ref += add;
*counter_ref
Expand Down
2 changes: 1 addition & 1 deletion examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wasmer_compiler::Universal;
use wasmer_compiler_cranelift::Cranelift;

/// A function we'll call through a table.
fn host_callback(_ctx: FunctionEnvMut<()>, arg1: i32, arg2: i32) -> i32 {
fn host_callback(_env: FunctionEnvMut<()>, arg1: i32, arg2: i32) -> i32 {
arg1 + arg2
}

Expand Down
23 changes: 13 additions & 10 deletions lib/api/src/js/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,22 @@ pub enum Export {

impl Export {
/// Return the export as a `JSValue`.
pub fn as_jsvalue<'context>(&self, ctx: &'context impl AsStoreRef) -> &'context JsValue {
pub fn as_jsvalue<'context>(&self, store: &'context impl AsStoreRef) -> &'context JsValue {
match self {
Self::Memory(js_wasm_memory) => js_wasm_memory
.get(ctx.as_store_ref().objects())
.get(store.as_store_ref().objects())
.memory
.as_ref(),
Self::Function(js_func) => js_func.get(ctx.as_store_ref().objects()).function.as_ref(),
Self::Function(js_func) => js_func
.get(store.as_store_ref().objects())
.function
.as_ref(),
Self::Table(js_wasm_table) => js_wasm_table
.get(ctx.as_store_ref().objects())
.get(store.as_store_ref().objects())
.table
.as_ref(),
Self::Global(js_wasm_global) => js_wasm_global
.get(ctx.as_store_ref().objects())
.get(store.as_store_ref().objects())
.global
.as_ref(),
}
Expand All @@ -120,14 +123,14 @@ impl Export {
/// Convert a `JsValue` into an `Export` within a given `Context`.
pub fn from_js_value(
val: JsValue,
ctx: &mut impl AsStoreMut,
store: &mut impl AsStoreMut,
extern_type: ExternType,
) -> Result<Self, WasmError> {
match extern_type {
ExternType::Memory(memory_type) => {
if val.is_instance_of::<Memory>() {
Ok(Self::Memory(InternalStoreHandle::new(
&mut ctx.objects_mut(),
&mut store.objects_mut(),
VMMemory::new(val.unchecked_into::<Memory>(), memory_type),
)))
} else {
Expand All @@ -143,7 +146,7 @@ impl Export {
ExternType::Global(global_type) => {
if val.is_instance_of::<Global>() {
Ok(Self::Global(InternalStoreHandle::new(
&mut ctx.objects_mut(),
&mut store.objects_mut(),
VMGlobal::new(val.unchecked_into::<Global>(), global_type),
)))
} else {
Expand All @@ -153,7 +156,7 @@ impl Export {
ExternType::Function(function_type) => {
if val.is_instance_of::<Function>() {
Ok(Self::Function(InternalStoreHandle::new(
&mut ctx.objects_mut(),
&mut store.objects_mut(),
VMFunction::new(val.unchecked_into::<Function>(), function_type),
)))
} else {
Expand All @@ -163,7 +166,7 @@ impl Export {
ExternType::Table(table_type) => {
if val.is_instance_of::<Table>() {
Ok(Self::Table(InternalStoreHandle::new(
&mut ctx.objects_mut(),
&mut store.objects_mut(),
VMTable::new(val.unchecked_into::<Table>(), table_type),
)))
} else {
Expand Down
16 changes: 8 additions & 8 deletions lib/api/src/js/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ impl Exports {
/// Get an export as a `TypedFunction`.
pub fn get_native_function<Args, Rets>(
&self,
ctx: &impl AsStoreRef,
store: &impl AsStoreRef,
name: &str,
) -> Result<TypedFunction<Args, Rets>, ExportError>
where
Args: WasmTypeList,
Rets: WasmTypeList,
{
self.get_typed_function(ctx, name)
self.get_typed_function(store, name)
}

/// Get an export as a `TypedFunction`.
Expand All @@ -169,7 +169,7 @@ impl Exports {
/// Hack to get this working with nativefunc too
pub fn get_with_generics<'a, T, Args, Rets>(
&'a self,
ctx: &impl AsStoreRef,
store: &impl AsStoreRef,
name: &str,
) -> Result<T, ExportError>
where
Expand All @@ -179,23 +179,23 @@ impl Exports {
{
match self.map.get(name) {
None => Err(ExportError::Missing(name.to_string())),
Some(extern_) => T::get_self_from_extern_with_generics(ctx, extern_),
Some(extern_) => T::get_self_from_extern_with_generics(store, extern_),
}
}

/// Like `get_with_generics` but with a WeakReference to the `InstanceRef` internally.
/// This is useful for passing data into Context data, for example.
pub fn get_with_generics_weak<'a, T, Args, Rets>(
&'a self,
ctx: &impl AsStoreRef,
store: &impl AsStoreRef,
name: &str,
) -> Result<T, ExportError>
where
Args: WasmTypeList,
Rets: WasmTypeList,
T: ExportableWithGenerics<'a, Args, Rets>,
{
let out: T = self.get_with_generics(ctx, name)?;
let out: T = self.get_with_generics(store, name)?;
Ok(out)
}

Expand Down Expand Up @@ -334,7 +334,7 @@ pub trait Exportable<'a>: Sized {
pub trait ExportableWithGenerics<'a, Args: WasmTypeList, Rets: WasmTypeList>: Sized {
/// Get an export with the given generics.
fn get_self_from_extern_with_generics(
ctx: &impl AsStoreRef,
store: &impl AsStoreRef,
_extern: &'a Extern,
) -> Result<Self, ExportError>;
}
Expand All @@ -343,7 +343,7 @@ pub trait ExportableWithGenerics<'a, Args: WasmTypeList, Rets: WasmTypeList>: Si
/// with empty `Args` and `Rets`.
impl<'a, T: Exportable<'a> + Clone + 'static> ExportableWithGenerics<'a, (), ()> for T {
fn get_self_from_extern_with_generics(
_ctx: &impl AsStoreRef,
_store: &impl AsStoreRef,
_extern: &'a Extern,
) -> Result<Self, ExportError> {
T::get_self_from_extern(_extern).map(|i| i.clone())
Expand Down
Loading

0 comments on commit 8f1a401

Please sign in to comment.