1 use anyhow::Result; 2 use wasmtime::component::*; 3 use wasmtime::{Module, Store}; 4 5 #[test] 6 fn instance_exports() -> Result<()> { 7 let engine = super::engine(); 8 let component = r#" 9 (component 10 (import "a" (instance $i)) 11 (import "b" (instance $i2 (export "m" (core module)))) 12 13 (alias export $i2 "m" (core module $m)) 14 15 (component $c 16 (component $c 17 (export "m" (core module $m)) 18 ) 19 (instance $c (instantiate $c)) 20 (export "i" (instance $c)) 21 ) 22 (instance $c (instantiate $c)) 23 (export "i" (instance $c)) 24 (export "r" (instance $i)) 25 (export "r2" (instance $i2)) 26 ) 27 "#; 28 let component = Component::new(&engine, component)?; 29 let mut store = Store::new(&engine, ()); 30 let mut linker = Linker::new(&engine); 31 linker.instance("a")?; 32 linker 33 .instance("b")? 34 .module("m", &Module::new(&engine, "(module)")?)?; 35 let instance = linker.instantiate(&mut store, &component)?; 36 37 assert!(instance 38 .get_export(&mut store, None, "not an instance") 39 .is_none()); 40 let i = instance.get_export(&mut store, None, "r").unwrap(); 41 assert!(instance.get_export(&mut store, Some(&i), "x").is_none()); 42 instance.get_export(&mut store, None, "i").unwrap(); 43 let i2 = instance.get_export(&mut store, None, "r2").unwrap(); 44 let m = instance.get_export(&mut store, Some(&i2), "m").unwrap(); 45 assert!(instance.get_func(&mut store, &m).is_none()); 46 assert!(instance.get_module(&mut store, &m).is_some()); 47 48 let i = instance.get_export(&mut store, None, "i").unwrap(); 49 let i = instance.get_export(&mut store, Some(&i), "i").unwrap(); 50 let m = instance.get_export(&mut store, Some(&i), "m").unwrap(); 51 instance.get_module(&mut store, &m).unwrap(); 52 53 Ok(()) 54 } 55 56 #[test] 57 fn export_old_get_new() -> Result<()> { 58 let engine = super::engine(); 59 let component = r#" 60 (component 61 (core module $m) 62 (export "a:b/[email protected]" (core module $m)) 63 64 (instance $i (export "m" (core module $m))) 65 (export "a:b/[email protected]" (instance $i)) 66 ) 67 "#; 68 69 let component = Component::new(&engine, component)?; 70 component.export_index(None, "a:b/[email protected]").unwrap(); 71 let (_, i) = component.export_index(None, "a:b/[email protected]").unwrap(); 72 component.export_index(Some(&i), "m").unwrap(); 73 74 let mut store = Store::new(&engine, ()); 75 let linker = Linker::new(&engine); 76 let instance = linker.instantiate(&mut store, &component)?; 77 78 instance.get_module(&mut store, "a:b/[email protected]").unwrap(); 79 instance 80 .get_export(&mut store, None, "a:b/[email protected]") 81 .unwrap(); 82 83 let i = instance 84 .get_export(&mut store, None, "a:b/[email protected]") 85 .unwrap(); 86 instance.get_export(&mut store, Some(&i), "m").unwrap(); 87 88 Ok(()) 89 } 90 91 #[test] 92 fn export_new_get_old() -> Result<()> { 93 let engine = super::engine(); 94 let component = r#" 95 (component 96 (core module $m) 97 (export "a:b/[email protected]" (core module $m)) 98 99 (instance $i (export "m" (core module $m))) 100 (export "a:b/[email protected]" (instance $i)) 101 ) 102 "#; 103 104 let component = Component::new(&engine, component)?; 105 component.export_index(None, "a:b/[email protected]").unwrap(); 106 let (_, i) = component.export_index(None, "a:b/[email protected]").unwrap(); 107 component.export_index(Some(&i), "m").unwrap(); 108 109 let mut store = Store::new(&engine, ()); 110 let linker = Linker::new(&engine); 111 let instance = linker.instantiate(&mut store, &component)?; 112 113 instance.get_module(&mut store, "a:b/[email protected]").unwrap(); 114 instance 115 .get_export(&mut store, None, "a:b/[email protected]") 116 .unwrap(); 117 118 let i = instance 119 .get_export(&mut store, None, "a:b/[email protected]") 120 .unwrap(); 121 instance.get_export(&mut store, Some(&i), "m").unwrap(); 122 123 Ok(()) 124 } 125 126 #[test] 127 fn export_missing_get_max() -> Result<()> { 128 let engine = super::engine(); 129 let component = r#" 130 (component 131 (core module $m1) 132 (core module $m2 (import "" "" (func))) 133 (export "a:b/[email protected]" (core module $m1)) 134 (export "a:b/[email protected]" (core module $m2)) 135 ) 136 "#; 137 138 fn assert_m2(module: &Module) { 139 assert_eq!(module.imports().len(), 1); 140 } 141 fn assert_m1(module: &Module) { 142 assert_eq!(module.imports().len(), 0); 143 } 144 145 let component = Component::new(&engine, component)?; 146 let mut store = Store::new(&engine, ()); 147 let instance = Linker::new(&engine).instantiate(&mut store, &component)?; 148 149 let tests = [ 150 ("a:b/[email protected]", assert_m2 as fn(&_)), // no exact, should pick max available 151 ("a:b/[email protected]", assert_m1), // exact hit 152 ("a:b/[email protected]", assert_m2), // no exact, should pick max available 153 ("a:b/[email protected]", assert_m2), // exact hit 154 ("a:b/[email protected]", assert_m2), // no exact, should pick max available 155 ]; 156 157 for (name, test_fn) in tests { 158 println!("test {name}"); 159 let (_, m) = component.export_index(None, name).unwrap(); 160 let m = instance.get_module(&mut store, &m).unwrap(); 161 test_fn(&m); 162 163 let m = instance.get_module(&mut store, name).unwrap(); 164 test_fn(&m); 165 166 let m = instance.get_export(&mut store, None, name).unwrap(); 167 let m = instance.get_module(&mut store, &m).unwrap(); 168 test_fn(&m); 169 } 170 171 Ok(()) 172 } 173