xref: /wasmtime-44.0.1/tests/all/exnrefs.rs (revision eaa4632e)
1 use super::gc_store;
2 use wasmtime::*;
3 
4 #[test]
tag_objects() -> Result<()>5 fn tag_objects() -> Result<()> {
6     let mut store = gc_store()?;
7     let engine = store.engine();
8 
9     let func_ty = FuncType::new(&engine, [ValType::I32, ValType::I64], []);
10     let tag_ty = TagType::new(func_ty);
11 
12     let tag = Tag::new(&mut store, &tag_ty).unwrap();
13 
14     assert!(tag.ty(&store).ty().matches(tag_ty.ty()));
15 
16     let tag2 = Tag::new(&mut store, &tag_ty).unwrap();
17 
18     assert!(!Tag::eq(&tag, &tag2, &store));
19 
20     Ok(())
21 }
22 
23 #[test]
exn_types() -> Result<()>24 fn exn_types() -> Result<()> {
25     let mut store = gc_store()?;
26     let engine = store.engine();
27 
28     let func_ty = FuncType::new(&engine, [ValType::I32, ValType::I64], []);
29     let tag_ty = TagType::new(func_ty);
30 
31     let tag = Tag::new(&mut store, &tag_ty).unwrap();
32 
33     assert!(tag.ty(&store).ty().matches(tag_ty.ty()));
34 
35     let tag2 = Tag::new(&mut store, &tag_ty).unwrap();
36 
37     assert!(!Tag::eq(&tag, &tag2, &store));
38 
39     let exntype = ExnType::from_tag_type(&tag_ty).unwrap();
40     let exntype2 = ExnType::new(store.engine(), [ValType::I32, ValType::I64]).unwrap();
41 
42     assert!(exntype.matches(&exntype2));
43     assert!(exntype.tag_type().ty().matches(&tag_ty.ty()));
44 
45     Ok(())
46 }
47 
48 #[test]
exn_objects() -> Result<()>49 fn exn_objects() -> Result<()> {
50     let mut store = gc_store()?;
51     let exntype = ExnType::new(store.engine(), [ValType::I32, ValType::I64]).unwrap();
52 
53     // Create a tag instance to associate with our exception objects.
54     let tag = Tag::new(&mut store, &exntype.tag_type()).unwrap();
55 
56     // Create an allocator for the exn type.
57     let allocator = ExnRefPre::new(&mut store, exntype);
58 
59     {
60         let mut scope = RootScope::new(&mut store);
61 
62         for i in 0..10 {
63             ExnRef::new(
64                 &mut scope,
65                 &allocator,
66                 &tag,
67                 &[Val::I32(i), Val::I64(i64::MAX)],
68             )?;
69         }
70 
71         let obj = ExnRef::new(
72             &mut scope,
73             &allocator,
74             &tag,
75             &[Val::I32(42), Val::I64(i64::MIN)],
76         )?;
77 
78         assert_eq!(obj.fields(&mut scope)?.len(), 2);
79         assert_eq!(obj.field(&mut scope, 0)?.unwrap_i32(), 42);
80         assert_eq!(obj.field(&mut scope, 1)?.unwrap_i64(), i64::MIN);
81         assert!(Tag::eq(&obj.tag(&mut scope)?, &tag, &scope));
82     }
83 
84     Ok(())
85 }
86