MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / resource_any

Function resource_any

tests/all/component_model/resources.rs:109–215  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

107
108#[test]
109fn resource_any() -> Result<()> {
110 let engine = super::engine();
111 let c = Component::new(
112 &engine,
113 r#"
114 (component
115 (type $t' (resource (rep i32)))
116 (type $u' (resource (rep i32)))
117
118 (export $t "t" (type $t'))
119 (export $u "u" (type $u'))
120
121 (core func $t_ctor (canon resource.new $t))
122 (core func $u_ctor (canon resource.new $u))
123
124 (func (export "[constructor]t") (param "x" u32) (result (own $t))
125 (canon lift (core func $t_ctor)))
126 (func (export "[constructor]u") (param "x" u32) (result (own $u))
127 (canon lift (core func $u_ctor)))
128
129 (core func $t_drop (canon resource.drop $t))
130 (core func $u_drop (canon resource.drop $u))
131
132 (func (export "drop-t") (param "x" (own $t))
133 (canon lift (core func $t_drop)))
134 (func (export "drop-u") (param "x" (own $u))
135 (canon lift (core func $u_drop)))
136 )
137 "#,
138 )?;
139
140 let linker = Linker::new(&engine);
141 {
142 let mut store = Store::new(&engine, ());
143 let i = linker.instantiate(&mut store, &c)?;
144 let t = i.get_resource(&mut store, "t").unwrap();
145 let u = i.get_resource(&mut store, "u").unwrap();
146
147 assert_ne!(t, u);
148
149 let t_ctor = i.get_typed_func::<(u32,), (ResourceAny,)>(&mut store, "[constructor]t")?;
150 let u_ctor = i.get_typed_func::<(u32,), (ResourceAny,)>(&mut store, "[constructor]u")?;
151 let t_dtor = i.get_typed_func::<(ResourceAny,), ()>(&mut store, "drop-t")?;
152 let u_dtor = i.get_typed_func::<(ResourceAny,), ()>(&mut store, "drop-u")?;
153
154 let (t1,) = t_ctor.call(&mut store, (100,))?;
155 let (t2,) = t_ctor.call(&mut store, (200,))?;
156 let (u1,) = u_ctor.call(&mut store, (300,))?;
157 let (u2,) = u_ctor.call(&mut store, (400,))?;
158
159 assert_eq!(t1.ty(), t);
160 assert_eq!(t2.ty(), t);
161 assert_eq!(u1.ty(), u);
162 assert_eq!(u2.ty(), u);
163
164 u_dtor.call(&mut store, (u2,))?;
165
166 u_dtor.call(&mut store, (u1,))?;

Callers

nothing calls this directly

Calls 7

OkFunction · 0.85
get_resourceMethod · 0.80
engineFunction · 0.50
newFunction · 0.50
instantiateMethod · 0.45
unwrapMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected