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

Function host_sets_struct_global

tests/all/structs.rs:462–503  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

460#[test]
461#[cfg_attr(miri, ignore)]
462fn host_sets_struct_global() -> Result<()> {
463 let mut store = gc_store()?;
464
465 let module = Module::new(
466 store.engine(),
467 r#"
468 (module
469 (type (struct (field i8)))
470 (global $g (export "g") (mut (ref null 0)) (ref.null 0))
471 (func (export "f") (result (ref null 0))
472 global.get $g
473 )
474 )
475 "#,
476 )?;
477
478 let instance = Instance::new(&mut store, &module, &[])?;
479 let g = instance.get_global(&mut store, "g").unwrap();
480
481 let struct_ty = StructType::new(
482 store.engine(),
483 [FieldType::new(Mutability::Const, StorageType::I8)],
484 )?;
485 let pre = StructRefPre::new(&mut store, struct_ty.clone());
486 let s0 = StructRef::new(&mut store, &pre, &[Val::I32(42)])?;
487 g.set(&mut store, s0.into())?;
488
489 // Get the global from the host.
490 let val = g.get(&mut store);
491 let anyref = val.unwrap_anyref().expect("non-null");
492 let s1 = anyref.unwrap_struct(&store)?;
493 assert_eq!(s1.field(&mut store, 0)?.unwrap_i32(), 42);
494 assert!(Rooted::ref_eq(&store, &s0, &s1)?);
495
496 // Get the global from the guest.
497 let f = instance.get_typed_func::<(), Option<Rooted<StructRef>>>(&mut store, "f")?;
498 let s2 = f.call(&mut store, ())?.expect("non-null");
499 assert_eq!(s2.field(&mut store, 0)?.unwrap_i32(), 42);
500 assert!(Rooted::ref_eq(&store, &s0, &s2)?);
501
502 Ok(())
503}
504
505#[test]
506#[cfg_attr(miri, ignore)]

Callers

nothing calls this directly

Calls 13

gc_storeFunction · 0.85
OkFunction · 0.85
unwrap_anyrefMethod · 0.80
newFunction · 0.50
engineMethod · 0.45
unwrapMethod · 0.45
get_globalMethod · 0.45
cloneMethod · 0.45
setMethod · 0.45
getMethod · 0.45
expectMethod · 0.45
unwrap_structMethod · 0.45

Tested by

no test coverage detected