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

Function manually_destroy

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

Source from the content-addressed store, hash-verified

417
418#[test]
419fn manually_destroy() -> Result<()> {
420 let engine = super::engine();
421 let c = Component::new(
422 &engine,
423 r#"
424 (component
425 (import "t1" (type $t1 (sub resource)))
426
427 (core module $m
428 (global $drops (mut i32) i32.const 0)
429 (global $last-drop (mut i32) i32.const 0)
430
431 (func (export "dtor") (param i32)
432 (global.set $drops (i32.add (global.get $drops) (i32.const 1)))
433 (global.set $last-drop (local.get 0))
434 )
435 (func (export "drops") (result i32) global.get $drops)
436 (func (export "last-drop") (result i32) global.get $last-drop)
437 (func (export "pass") (param i32) (result i32) local.get 0)
438 )
439 (core instance $i (instantiate $m))
440 (type $t2' (resource (rep i32) (dtor (func $i "dtor"))))
441 (export $t2 "t2" (type $t2'))
442 (core func $ctor (canon resource.new $t2))
443 (func (export "[constructor]t2") (param "rep" u32) (result (own $t2))
444 (canon lift (core func $ctor)))
445 (func (export "[static]t2.drops") (result u32)
446 (canon lift (core func $i "drops")))
447 (func (export "[static]t2.last-drop") (result u32)
448 (canon lift (core func $i "last-drop")))
449
450 (func (export "t1-pass") (param "t" (own $t1)) (result (own $t1))
451 (canon lift (core func $i "pass")))
452 )
453 "#,
454 )?;
455
456 struct MyType;
457
458 #[derive(Default)]
459 struct Data {
460 drops: u32,
461 last_drop: Option<u32>,
462 }
463
464 let mut store = Store::new(&engine, Data::default());
465 let mut linker = Linker::new(&engine);
466 linker
467 .root()
468 .resource("t1", ResourceType::host::<MyType>(), |mut cx, rep| {
469 let data: &mut Data = cx.data_mut();
470 data.drops += 1;
471 data.last_drop = Some(rep);
472 Ok(())
473 })?;
474 let i = linker.instantiate(&mut store, &c)?;
475 let t2_ctor = i.get_typed_func::<(u32,), (ResourceAny,)>(&mut store, "[constructor]t2")?;
476 let t2_drops = i.get_typed_func::<(), (u32,)>(&mut store, "[static]t2.drops")?;

Callers

nothing calls this directly

Calls 9

OkFunction · 0.85
engineFunction · 0.50
newFunction · 0.50
resourceMethod · 0.45
rootMethod · 0.45
data_mutMethod · 0.45
instantiateMethod · 0.45
callMethod · 0.45
resource_dropMethod · 0.45

Tested by

no test coverage detected