Skip to content

Commit

Permalink
Removed some useless as_context_mut()
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Jun 29, 2022
1 parent 7433215 commit 3fb46b6
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 143 deletions.
76 changes: 38 additions & 38 deletions tests/compilers/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> {
};
let mut ctx = WasmerContext::new(&store, env);
let f0 = Function::new(
&mut ctx.as_context_mut(),
&mut ctx,
FunctionType::new(vec![], vec![]),
|ctx, _values| {
assert_eq!(ctx.data().fetch_add(1, SeqCst), 0);
Ok(vec![])
},
);
let f1 = Function::new(
&mut ctx.as_context_mut(),
&mut ctx,
FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]),
|ctx, values| {
assert_eq!(values[0], Value::I32(0));
Expand All @@ -123,7 +123,7 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> {
},
);
let f2 = Function::new(
&mut ctx.as_context_mut(),
&mut ctx,
FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]),
|ctx, values| {
assert_eq!(values[0], Value::I32(2));
Expand All @@ -133,7 +133,7 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> {
},
);
let f3 = Function::new(
&mut ctx.as_context_mut(),
&mut ctx,
FunctionType::new(
vec![
ValueType::I32,
Expand All @@ -155,7 +155,7 @@ fn dynamic_function_with_env(config: crate::Config) -> Result<()> {
},
);
Instance::new(
&mut ctx.as_context_mut(),
&mut ctx,
&module,
&imports! {
"host" => {
Expand All @@ -178,27 +178,27 @@ fn static_function(config: crate::Config) -> Result<()> {

static HITS: AtomicUsize = AtomicUsize::new(0);
let mut ctx = WasmerContext::new(&store, ());
let f0 = Function::new_native(&mut ctx.as_context_mut(), |_ctx: ContextMut<_>| {
let f0 = Function::new_native(&mut ctx, |_ctx: ContextMut<_>| {
assert_eq!(HITS.fetch_add(1, SeqCst), 0);
});
let f1 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|_ctx: ContextMut<_>, x: i32| -> i32 {
assert_eq!(x, 0);
assert_eq!(HITS.fetch_add(1, SeqCst), 1);
1
},
);
let f2 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|_ctx: ContextMut<_>, x: i32, y: i64| {
assert_eq!(x, 2);
assert_eq!(y, 3);
assert_eq!(HITS.fetch_add(1, SeqCst), 2);
},
);
let f3 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|_ctx: ContextMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| {
assert_eq!(a, 100);
assert_eq!(b, 200);
Expand All @@ -209,7 +209,7 @@ fn static_function(config: crate::Config) -> Result<()> {
},
);
Instance::new(
&mut ctx.as_context_mut(),
&mut ctx,
&module,
&imports! {
"host" => {
Expand All @@ -232,27 +232,27 @@ fn static_function_with_results(config: crate::Config) -> Result<()> {

static HITS: AtomicUsize = AtomicUsize::new(0);
let mut ctx = WasmerContext::new(&store, ());
let f0 = Function::new_native(&mut ctx.as_context_mut(), |_ctx: ContextMut<_>| {
let f0 = Function::new_native(&mut ctx, |_ctx: ContextMut<_>| {
assert_eq!(HITS.fetch_add(1, SeqCst), 0);
});
let f1 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|_ctx: ContextMut<_>, x: i32| -> Result<i32, Infallible> {
assert_eq!(x, 0);
assert_eq!(HITS.fetch_add(1, SeqCst), 1);
Ok(1)
},
);
let f2 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|_ctx: ContextMut<_>, x: i32, y: i64| {
assert_eq!(x, 2);
assert_eq!(y, 3);
assert_eq!(HITS.fetch_add(1, SeqCst), 2);
},
);
let f3 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|_ctx: ContextMut<_>, a: i32, b: i64, c: i32, d: f32, e: f64| {
assert_eq!(a, 100);
assert_eq!(b, 200);
Expand All @@ -263,7 +263,7 @@ fn static_function_with_results(config: crate::Config) -> Result<()> {
},
);
Instance::new(
&mut ctx.as_context_mut(),
&mut ctx,
&module,
&imports! {
"host" => {
Expand Down Expand Up @@ -295,27 +295,27 @@ fn static_function_with_env(config: crate::Config) -> Result<()> {

let env: Env = Env(Arc::new(AtomicUsize::new(0)));
let mut ctx = WasmerContext::new(&store, env);
let f0 = Function::new_native(&mut ctx.as_context_mut(), |ctx: ContextMut<Env>| {
let f0 = Function::new_native(&mut ctx, |ctx: ContextMut<Env>| {
assert_eq!(ctx.data().fetch_add(1, SeqCst), 0);
});
let f1 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|ctx: ContextMut<Env>, x: i32| -> i32 {
assert_eq!(x, 0);
assert_eq!(ctx.data().fetch_add(1, SeqCst), 1);
1
},
);
let f2 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|ctx: ContextMut<Env>, x: i32, y: i64| {
assert_eq!(x, 2);
assert_eq!(y, 3);
assert_eq!(ctx.data().fetch_add(1, SeqCst), 2);
},
);
let f3 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|ctx: ContextMut<Env>, a: i32, b: i64, c: i32, d: f32, e: f64| {
assert_eq!(a, 100);
assert_eq!(b, 200);
Expand All @@ -326,7 +326,7 @@ fn static_function_with_env(config: crate::Config) -> Result<()> {
},
);
Instance::new(
&mut ctx.as_context_mut(),
&mut ctx,
&module,
&imports! {
"host" => {
Expand Down Expand Up @@ -356,13 +356,13 @@ fn static_function_that_fails(config: crate::Config) -> Result<()> {
let module = Module::new(&store, &wat)?;
let mut ctx = WasmerContext::new(&store, ());
let f0 = Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
|_ctx: ContextMut<_>| -> Result<Infallible, RuntimeError> {
Err(RuntimeError::new("oops"))
},
);
let result = Instance::new(
&mut ctx.as_context_mut(),
&mut ctx,
&module,
&imports! {
"host" => {
Expand Down Expand Up @@ -411,7 +411,7 @@ fn dynamic_function_with_env_wasmer_env_init_works(config: crate::Config) -> Res
let env: Env = Env { memory: None };
let mut ctx = WasmerContext::new(&store, env);
let f0 = Function::new(
&mut ctx.as_context_mut(),
&mut ctx,
FunctionType::new(vec![], vec![]),
|ctx, _values| {
assert!(ctx.data().memory.as_ref().is_some());
Expand All @@ -431,8 +431,8 @@ fn dynamic_function_with_env_wasmer_env_init_works(config: crate::Config) -> Res
ctx.data_mut().memory = Some(memory.clone());
let f: TypedFunction<(), ()> = instance
.exports
.get_typed_function(&ctx.as_context_mut(), "main")?;
f.call(&mut ctx.as_context_mut())?;
.get_typed_function(&ctx, "main")?;
f.call(&mut ctx)?;
Ok(())
}

Expand Down Expand Up @@ -463,27 +463,27 @@ fn multi_use_host_fn_manages_memory_correctly(config: crate::Config) -> Result<(
}
let imports = imports! {
"host" => {
"fn" => Function::new_native(&mut ctx.as_context_mut(), host_fn),
"fn" => Function::new_native(&mut ctx, host_fn),
},
};
let instance1 = Instance::new(&mut ctx.as_context_mut(), &module, &imports)?;
let instance2 = Instance::new(&mut ctx.as_context_mut(), &module, &imports)?;
let instance1 = Instance::new(&mut ctx, &module, &imports)?;
let instance2 = Instance::new(&mut ctx, &module, &imports)?;
{
let f1: TypedFunction<(), ()> = instance1
.exports
.get_typed_function(&mut ctx.as_context_mut(), "main")?;
.get_typed_function(&mut ctx, "main")?;
let memory = instance1.exports.get_memory("memory")?;
ctx.data_mut().memory = Some(memory.clone());
f1.call(&mut ctx.as_context_mut())?;
f1.call(&mut ctx)?;
}
drop(instance1);
{
let f2: TypedFunction<(), ()> = instance2
.exports
.get_typed_function(&mut ctx.as_context_mut(), "main")?;
.get_typed_function(&mut ctx, "main")?;
let memory = instance2.exports.get_memory("memory")?;
ctx.data_mut().memory = Some(memory.clone());
f2.call(&mut ctx.as_context_mut())?;
f2.call(&mut ctx)?;
}
drop(instance2);
Ok(())
Expand All @@ -499,7 +499,7 @@ fn instance_local_memory_lifetime(config: crate::Config) -> Result<()> {
(export "memory" (memory $mem))
)"#;
let module = Module::new(&store, wat)?;
let instance = Instance::new(&mut ctx.as_context_mut(), &module, &imports! {})?;
let instance = Instance::new(&mut ctx, &module, &imports! {})?;
instance.exports.get_memory("memory")?.clone()
};

Expand All @@ -520,15 +520,15 @@ fn instance_local_memory_lifetime(config: crate::Config) -> Result<()> {
"memory" => memory,
},
};
let instance = Instance::new(&mut ctx.as_context_mut(), &module, &imports)?;
let instance = Instance::new(&mut ctx, &module, &imports)?;
let set_at: TypedFunction<(i32, i32), ()> = instance
.exports
.get_typed_function(&mut ctx.as_context_mut(), "set_at")?;
.get_typed_function(&mut ctx, "set_at")?;
let get_at: TypedFunction<i32, i32> = instance
.exports
.get_typed_function(&mut ctx.as_context_mut(), "get_at")?;
set_at.call(&mut ctx.as_context_mut(), 200, 123)?;
assert_eq!(get_at.call(&mut ctx.as_context_mut(), 200)?, 123);
.get_typed_function(&mut ctx, "get_at")?;
set_at.call(&mut ctx, 200, 123)?;
assert_eq!(get_at.call(&mut ctx, 200)?, 123);

Ok(())
}
18 changes: 9 additions & 9 deletions tests/compilers/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ fn issue_2329(mut config: crate::Config) -> Result<()> {
let imports: Imports = imports! {
"env" => {
"__read_memory" => Function::new_native(
&mut ctx.as_context_mut(),
&mut ctx,
read_memory
),
}
};
let instance = Instance::new(&mut ctx.as_context_mut(), &module, &imports)?;
let instance = Instance::new(&mut ctx, &module, &imports)?;
instance
.exports
.get_function("read_memory")?
Expand Down Expand Up @@ -189,7 +189,7 @@ fn call_with_static_data_pointers(mut config: crate::Config) -> Result<()> {
let env = Env { memory: None };
let mut ctx = WasmerContext::new(&store, env);
let memory = Memory::new(
&mut ctx.as_context_mut(),
&mut ctx,
MemoryType::new(Pages(1024), Some(Pages(2048)), false),
)
.unwrap();
Expand All @@ -198,24 +198,24 @@ fn call_with_static_data_pointers(mut config: crate::Config) -> Result<()> {
exports.insert("memory", memory);
exports.insert(
"banana",
Function::new_native(&mut ctx.as_context_mut(), banana),
Function::new_native(&mut ctx, banana),
);
exports.insert(
"peach",
Function::new_native(&mut ctx.as_context_mut(), peach),
Function::new_native(&mut ctx, peach),
);
exports.insert(
"chaenomeles",
Function::new_native(&mut ctx.as_context_mut(), chaenomeles),
Function::new_native(&mut ctx, chaenomeles),
);
exports.insert(
"mango",
Function::new_native(&mut ctx.as_context_mut(), mango),
Function::new_native(&mut ctx, mango),
);
exports.insert("gas", Function::new_native(&mut ctx.as_context_mut(), gas));
exports.insert("gas", Function::new_native(&mut ctx, gas));
let mut imports = Imports::new();
imports.register_namespace("env", exports);
let instance = Instance::new(&mut ctx.as_context_mut(), &module, &imports)?;
let instance = Instance::new(&mut ctx, &module, &imports)?;
instance
.exports
.get_function("repro")?
Expand Down
14 changes: 7 additions & 7 deletions tests/compilers/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ fn run_add_with_limit(mut config: crate::Config, limit: u64) -> Result<()> {
let import_object = imports! {};

let module = Module::new(&store, wat).unwrap();
let instance = Instance::new(&mut ctx.as_context_mut(), &module, &import_object)?;
let instance = Instance::new(&mut ctx, &module, &import_object)?;

let f: TypedFunction<(i32, i32), i32> = instance
.exports
.get_typed_function(&mut ctx.as_context_mut(), "add")?;
.get_typed_function(&mut ctx, "add")?;
f.call(&mut ctx, 4, 6)?;
Ok(())
}
Expand Down Expand Up @@ -59,11 +59,11 @@ fn run_loop(mut config: crate::Config, limit: u64, iter_count: i32) -> Result<()

let import_object = imports! {};

let instance = Instance::new(&mut ctx.as_context_mut(), &module, &import_object)?;
let instance = Instance::new(&mut ctx, &module, &import_object)?;

let f: TypedFunction<i32, ()> = instance
.exports
.get_typed_function(&mut ctx.as_context_mut(), "test")?;
.get_typed_function(&mut ctx, "test")?;
f.call(&mut ctx, iter_count)?;
Ok(())
}
Expand Down Expand Up @@ -165,15 +165,15 @@ fn complex_loop(mut config: crate::Config) -> Result<()> {

let import_object = imports! {};

let instance = Instance::new(&mut ctx.as_context_mut(), &module, &import_object)?;
let instance = Instance::new(&mut ctx, &module, &import_object)?;

let f: TypedFunction<(i32, i32), i32> = instance
.exports
.get_typed_function(&mut ctx.as_context_mut(), "add_to")?;
.get_typed_function(&mut ctx, "add_to")?;

// FIXME: Since now a metering error is signaled with an `unreachable`, it is impossible to verify
// the error type. Fix this later.
f.call(&mut ctx.as_context_mut(), 10_000_000, 4)
f.call(&mut ctx, 10_000_000, 4)
.unwrap_err();
Ok(())
}
Loading

0 comments on commit 3fb46b6

Please sign in to comment.