Skip to content

Commit

Permalink
fix(vm): Forward all functions in the VmType impl of RuntimeResult
Browse files Browse the repository at this point in the history
This fixes a panic when trying to return a RuntimeResult<IO<T>, E>
  • Loading branch information
Laegluin committed Dec 3, 2018
1 parent 4506cac commit 9d6c7f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
33 changes: 32 additions & 1 deletion tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use futures::{Future, IntoFuture};
use gluon::base::types::{Alias, ArcType, Type};
use gluon::import::{add_extern_module, Import};
use gluon::vm::api::de::De;
use gluon::vm::api::{FunctionRef, FutureResult, OpaqueValue, Userdata, VmType, IO};
use gluon::vm::api::{FunctionRef, FutureResult, OpaqueValue, RuntimeResult, Userdata, VmType, IO};
use gluon::vm::thread::{RootedThread, Thread, Traverseable};
use gluon::vm::types::VmInt;
use gluon::vm::{Error, ExternModule};
Expand Down Expand Up @@ -374,3 +374,34 @@ fn get_value_boxed_or_unboxed() {
.unwrap_or_else(|err| panic!("{}", err));
assert_eq!(boxed, Box::new(27));
}

#[test]
fn runtime_result_vm_type_forwarding() {
let _ = ::env_logger::try_init();
let vm = make_vm();

add_extern_module(&vm, "test", |vm| {
ExternModule::new(
vm,
record! {
primes => primitive!(1, |_: ()| -> RuntimeResult<IO<()>, String> {
RuntimeResult::Return(IO::Value(()))
})
},
)
});

let text = r#"
let { primes } = import! test
let { ? } = import! std.io
let { wrap } = import! std.applicative
do primes = primes ()
wrap ()
"#;

let _ = Compiler::new()
.run_io(true)
.run_expr::<IO<()>>(&vm, "test", text)
.unwrap_or_else(|err| panic!("{}", err));
}
15 changes: 11 additions & 4 deletions vm/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,7 @@ fn insert_forall_walker(
Type::ExtendRow { .. } => types::walk_move_type_opt(
typ,
&mut types::ControlVisitation(|typ: &ArcType| insert_forall(variables, typ)),
)
.map(|typ| ArcType::from(Type::Record(typ))),
).map(|typ| ArcType::from(Type::Record(typ))),
_ => None,
},
_ => types::walk_move_type_opt(
Expand Down Expand Up @@ -768,8 +767,7 @@ impl VmType for bool {
(*vm.global_env()
.get_env()
.find_type_info("std.types.Bool")
.unwrap())
.clone()
.unwrap()).clone()
.into_type()
}
}
Expand Down Expand Up @@ -1314,9 +1312,18 @@ impl<T, E> From<StdResult<T, E>> for RuntimeResult<T, E> {

impl<T: VmType, E> VmType for RuntimeResult<T, E> {
type Type = T::Type;

fn make_forall_type(vm: &Thread) -> ArcType {
T::make_forall_type(vm)
}

fn make_type(vm: &Thread) -> ArcType {
T::make_type(vm)
}

fn extra_args() -> VmIndex {
T::extra_args()
}
}
impl<'vm, T: Pushable<'vm>, E: fmt::Display> Pushable<'vm> for RuntimeResult<T, E> {
fn push(self, context: &mut ActiveThread<'vm>) -> Result<()> {
Expand Down

0 comments on commit 9d6c7f4

Please sign in to comment.