Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add tests and fix the regression with clone_userdata #780

Merged
merged 1 commit into from
Aug 30, 2019
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
6 changes: 3 additions & 3 deletions codegen/src/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ fn gen_impl(container: &Container, ident: Ident, generics: Generics) -> TokenStr

let deep_clone = if container.clone {
quote! {
fn deep_clone(
fn deep_clone<'gc>(
&self,
deep_cloner: &mut _gluon_api::Cloner
) -> _gluon_Result<_gluon_gc::GcPtr<Box<dyn _gluon_api::Userdata>>> {
deep_cloner: &'gc mut _gluon_api::Cloner
) -> _gluon_Result<_gluon_gc::GcRef<'gc, Box<dyn _gluon_api::Userdata>>> {
let data: Box<dyn _gluon_api::Userdata> = Box::new(self.clone());
deep_cloner.gc().alloc(_gluon_gc::Move(data))
}
Expand Down
5 changes: 4 additions & 1 deletion examples/marshalling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@ fn marshal_wrapper() -> Result<()> {
Ok(())
}

#[derive(Userdata, Trace, Debug, Clone, VmType)]
#[derive(Userdata, Trace, Clone, Debug, VmType)]
#[gluon_userdata(clone)]
// Lets gluon know that the value can be cloned which can be needed when transferring the value between threads
#[gluon(vm_type = "WindowHandle")]
struct WindowHandle {
id: Arc<u64>,
Expand All @@ -348,6 +350,7 @@ fn load_mod(vm: &gluon::Thread) -> vm::Result<ExternModule> {
create_hwnd => primitive!(2, create_hwnd),
id => primitive!(1, id),
metadata => primitive!(1, metadata),
default_hwnd => create_hwnd(0, "default".into()),
};

ExternModule::new(vm, module)
Expand Down
4 changes: 2 additions & 2 deletions parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -806,15 +806,15 @@ Expr: Expr<Id> = {
},

<metadata: Metadata?> "rec" <bindings: RecursiveValueBinding+> <body: InExpr> => {
let mut bindings = bindings.into_iter().map(|x| *x).collect::<Vec<_>>();;
let mut bindings = bindings.into_iter().map(|x| *x).collect::<Vec<_>>();
if let Some(metadata) = metadata {
bindings[0].metadata = metadata;
}
Expr::LetBindings(ValueBindings::Recursive(bindings), Box::new(body))
},

<metadata: Metadata?> "rec" <bindings: TypeBinding+> <body: InExpr> => {
let mut bindings = bindings.into_iter().map(|x| *x).collect::<Vec<_>>();;
let mut bindings = bindings.into_iter().map(|x| *x).collect::<Vec<_>>();
if let Some(metadata) = metadata {
bindings[0].metadata = metadata;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,28 @@ fn child_vm_do_not_cause_undroppable_cycle_reverse_drop_order() {
"The virtual machine and its values were not dropped"
);
}

#[test]
fn clone_userdata() {
let _ = ::env_logger::try_init();

let expr = r#"
import! test
"#;

#[derive(Debug, Userdata, Trace, VmType, Clone, PartialEq)]
#[gluon(vm_type = "Test")]
#[gluon_userdata(clone)]
struct Test(VmInt);

let vm = make_vm();
vm.register_type::<Test>("Test", &[])
.unwrap_or_else(|_| panic!("Could not add type"));
add_extern_module(&vm, "test", |thread| ExternModule::new(thread, Test(123)));

let (result, _) = Compiler::new()
.run_expr::<OpaqueValue<RootedThread, Test>>(&vm, "<top>", expr)
.unwrap_or_else(|err| panic!("{}", err));

assert_eq!(*result, Test(123));
}