Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,23 @@ impl<'args> FuncInvocation<'args> {
return_val: Option<RuntimeValue>,
externals: &'externals mut E,
) -> Result<Option<RuntimeValue>, ResumableError> {
match self.kind {
FuncInvocationKind::Internal(ref mut interpreter) => {
if !interpreter.state().is_resumable() {
return Err(ResumableError::AlreadyStarted);
use crate::TrapKind;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is a new keyword and apparently it doesn't require the 2018 edition. What is the version of a compiler is required in order to use this keyword?

Copy link
Contributor Author

@eira-fransham eira-fransham Dec 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if return_val.map(|v| v.value_type()) != self.resumable_value_type() {
return Err(ResumableError::Trap(Trap::new(
TrapKind::UnexpectedSignature,
)));
}

match &mut self.kind {
FuncInvocationKind::Internal(interpreter) => {
if interpreter.state().is_resumable() {
Ok(interpreter.resume_execution(return_val, externals)?)
} else {
Err(ResumableError::AlreadyStarted)
}
Ok(interpreter.resume_execution(return_val, externals)?)
}
FuncInvocationKind::Host { .. } => {
return Err(ResumableError::NotResumable);
}
FuncInvocationKind::Host { .. } => Err(ResumableError::NotResumable),
}
}
}
Expand Down
60 changes: 60 additions & 0 deletions src/tests/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,66 @@ fn resume_call_host_func() {
);
}

#[test]
fn resume_call_host_func_type_mismatch() {
fn resume_with_val(val: Option<RuntimeValue>) {
let module = parse_wat(
r#"
(module
(import "env" "trap_sub" (func $trap_sub (param i32 i32) (result i32)))

(func (export "test") (result i32)
(call $trap_sub
(i32.const 5)
(i32.const 7)
)
)
)
"#,
);

let mut env = TestHost::new();

let instance =
ModuleInstance::new(&module, &ImportsBuilder::new().with_resolver("env", &env))
.expect("Failed to instantiate module")
.assert_no_start();

let export = instance.export_by_name("test").unwrap();
let func_instance = export.as_func().unwrap();

let mut invocation = FuncInstance::invoke_resumable(&func_instance, &[]).unwrap();
let result = invocation.start_execution(&mut env);
match result {
Err(ResumableError::Trap(_)) => {}
_ => panic!(),
}

assert!(invocation.is_resumable());
let err = invocation.resume_execution(val, &mut env).unwrap_err();

match &err {
ResumableError::Trap(trap) => {
if let TrapKind::UnexpectedSignature = trap.kind() {
return;
}
}
_ => {}
}

// If didn't return in the previous `match`...

panic!(
"Expected `ResumableError::Trap(Trap {{ kind: \
TrapKind::UnexpectedSignature, }})`, got `{:?}`",
err
)
}

resume_with_val(None);
resume_with_val(Some((-1i64).into()));
}

#[test]
fn host_err() {
let module = parse_wat(
Expand Down