Skip to content

Commit

Permalink
fixup! Move Webassembly objects to Store and remove Context
Browse files Browse the repository at this point in the history
Replace ctx with env where it makes sense

Replace "Context" with "Store" in comments & docs.
  • Loading branch information
epilys committed Jul 18, 2022
1 parent 9fa9fcd commit 2755fa3
Show file tree
Hide file tree
Showing 38 changed files with 338 additions and 337 deletions.
4 changes: 2 additions & 2 deletions examples/early_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn main() -> anyhow::Result<()> {
// 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 ctx = FunctionEnv::new(&mut store, ());
let env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand All @@ -74,7 +74,7 @@ fn main() -> anyhow::Result<()> {
// Create an import object.
let import_object = imports! {
"env" => {
"early_exit" => Function::new_native(&mut store, &ctx, early_exit),
"early_exit" => Function::new_native(&mut store, &env, early_exit),
}
};

Expand Down
2 changes: 1 addition & 1 deletion examples/engine_headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// We create a headless Universal engine.
let engine = Universal::headless().engine();
let mut store = Store::new_with_engine(&engine);
let mut ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Deserializing module...");
// Here we go.
Expand Down
2 changes: 1 addition & 1 deletion examples/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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 ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/exports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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 ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/exports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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 ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/exports_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ 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 ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() -> anyhow::Result<()> {

// Now, let's define the store, and compile the module.
let mut store = Store::new_with_engine(&engine.engine());
let mut ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());
let module = Module::new(&store, wasm_bytes)?;

// Finally, let's instantiate the module, and execute something
Expand Down
4 changes: 2 additions & 2 deletions examples/imports_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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 ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand All @@ -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, &ctx, &host_function_signature, |_ctx, _args| {
let host_function = Function::new(&mut store, &env, &host_function_signature, |_ctx, _args| {
Ok(vec![Value::I32(42)])
});

Expand Down
6 changes: 3 additions & 3 deletions examples/imports_function_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
*counter_ref
}

let mut ctx = FunctionEnv::new(
let mut env = FunctionEnv::new(
&mut store,
Env {
counter: shared_counter.clone(),
Expand All @@ -98,8 +98,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create an import object.
let import_object = imports! {
"env" => {
"get_counter" => Function::new_native(&mut store, &ctx, get_counter),
"add_to_counter" => Function::new_native(&mut store, &ctx, add_to_counter),
"get_counter" => Function::new_native(&mut store, &env, get_counter),
"add_to_counter" => Function::new_native(&mut store, &env, add_to_counter),
}
};

Expand Down
2 changes: 1 addition & 1 deletion examples/imports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ 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 ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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 ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() -> anyhow::Result<()> {
// 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 ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
2 changes: 1 addition & 1 deletion examples/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn main() -> anyhow::Result<()> {
// We use our previously create compiler configuration
// with the Universal engine.
let mut store = Store::new_with_engine(&Universal::new(compiler_config).engine());
let mut ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
8 changes: 4 additions & 4 deletions examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() -> anyhow::Result<()> {

// We set up our store with an engine and a compiler.
let mut store = Store::new_with_engine(&Universal::new(Cranelift::default()).engine());
let mut ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());
// Then compile our Wasm.
let module = Module::new(&store, wasm_bytes)?;
let import_object = imports! {};
Expand Down Expand Up @@ -89,7 +89,7 @@ fn main() -> anyhow::Result<()> {
// == Setting elements in a table ==

// We first construct a `Function` over our host_callback.
let func = Function::new_native(&mut store, &ctx, host_callback);
let func = Function::new_native(&mut store, &env, host_callback);

// And set table index 1 of that table to the host_callback `Function`.
guest_table.set(&mut store, 1, func.into())?;
Expand All @@ -103,7 +103,7 @@ fn main() -> anyhow::Result<()> {
// == Growing a table ==

// We again construct a `Function` over our host_callback.
let func = Function::new_native(&mut store, &ctx, host_callback);
let func = Function::new_native(&mut store, &env, host_callback);

// And grow the table by 3 elements, filling in our host_callback in all the
// new elements of the table.
Expand Down Expand Up @@ -134,7 +134,7 @@ fn main() -> anyhow::Result<()> {
assert_eq!(result, 18);

// Now overwrite index 0 with our host_callback.
let func = Function::new_native(&mut store, &ctx, host_callback);
let func = Function::new_native(&mut store, &env, host_callback);
guest_table.set(&mut store, 0, func.into())?;
// And verify that it does what we expect.
let result = call_via_table.call(&mut store, 0, 2, 7)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/tunables_limit_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Create a store, that holds the engine and our custom tunables
let mut store = Store::new_with_tunables(&engine, tunables);
let mut ctx = FunctionEnv::new(&mut store, ());
let mut env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
let module = Module::new(&store, wasm_bytes)?;
Expand Down
8 changes: 4 additions & 4 deletions lib/api/src/js/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ pub enum InstantiationError {
#[cfg_attr(feature = "std", error(transparent))]
Start(RuntimeError),

/// Import from a different [`Context`].
/// This error occurs when an import from a different context is used.
#[cfg_attr(feature = "std", error("cannot mix imports from different contexts"))]
BadContext,
/// Import from a different [`Store`].
/// This error occurs when an import from a different store is used.
#[cfg_attr(feature = "std", error("cannot mix imports from different stores"))]
DifferentStores,

/// A generic error occured while invoking API functions
#[cfg_attr(feature = "std", error(transparent))]
Expand Down
14 changes: 7 additions & 7 deletions lib/api/src/js/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Function {
/// ```
pub fn new_native<T, F, Args, Rets>(
store: &mut impl AsStoreMut,
ctx: &FunctionEnv<T>,
env: &FunctionEnv<T>,
func: F,
) -> Self
where
Expand All @@ -205,7 +205,7 @@ impl Function {
let binded_func = func.bind2(
&JsValue::UNDEFINED,
&JsValue::from_f64(store.as_raw() as *mut u8 as usize as f64),
&JsValue::from_f64(ctx.handle.internal_handle().index() as f64),
&JsValue::from_f64(env.handle.internal_handle().index() as f64),
);
let ty = function.ty();
let vm_function = VMFunction::new(binded_func, ty);
Expand Down Expand Up @@ -242,13 +242,13 @@ impl Function {
/// ```
/// # use wasmer::{Function, FunctionEnv, FunctionEnvMut, Store, Type};
/// # let mut store = Store::default();
/// # let ctx = FunctionEnv::new(&mut store, ());
/// # let env = FunctionEnv::new(&mut store, ());
/// #
/// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 {
/// a + b
/// }
///
/// let f = Function::new_native(&store, &ctx, sum);
/// let f = Function::new_native(&store, &env, sum);
///
/// assert_eq!(f.param_arity(&store), 2);
/// ```
Expand All @@ -263,13 +263,13 @@ impl Function {
/// ```
/// # use wasmer::{Function, FunctionEnv, FunctionEnvMut, Store, Type};
/// # let mut store = Store::default();
/// # let ctx = FunctionEnv::new(&mut store, ());
/// # let env = FunctionEnv::new(&mut store, ());
/// #
/// fn sum(_ctx: FunctionEnvMut<()>, a: i32, b: i32) -> i32 {
/// a + b
/// }
///
/// let f = Function::new_native(&store, &ctx, sum);
/// let f = Function::new_native(&store, &env, sum);
///
/// assert_eq!(f.result_arity(&store), 1);
/// ```
Expand Down Expand Up @@ -314,7 +314,7 @@ impl Function {
let arr = js_sys::Array::new_with_length(params.len() as u32);

// let raw_ctx = ctx.as_raw() as *mut u8;
// let mut ctx = unsafe { FunctionEnvMut::from_raw(raw_ctx as *mut StoreInner<()>) };
// let mut env = unsafe { FunctionEnvMut::from_raw(raw_ctx as *mut StoreInner<()>) };

for (i, param) in params.iter().enumerate() {
let js_value = param.as_jsvalue(&store.as_store_ref());
Expand Down
7 changes: 4 additions & 3 deletions lib/api/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,14 @@ impl Module {
store: &mut impl AsStoreMut,
imports: &Imports,
) -> Result<(StoreHandle<WebAssembly::Instance>, Vec<Extern>), RuntimeError> {
// Ensure all imports come from the same context.
// Ensure all imports come from the same store.
if imports
.into_iter()
.any(|(_, import)| !import.is_from_store(store))
{
// FIXME is RuntimeError::User appropriate?
return Err(RuntimeError::user(Box::new(InstantiationError::BadContext)));
return Err(RuntimeError::user(Box::new(
InstantiationError::DifferentStores,
)));
}
let imports_object = js_sys::Object::new();
let mut import_externs: Vec<Extern> = vec![];
Expand Down
Loading

0 comments on commit 2755fa3

Please sign in to comment.