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

Function instantiate_with_struct_global

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

Source from the content-addressed store, hash-verified

652
653#[test]
654fn instantiate_with_struct_global() -> Result<()> {
655 let mut store = gc_store()?;
656
657 let module = Module::new(
658 store.engine(),
659 r#"
660 (module
661 (type (struct (field i8)))
662 (import "" "" (global (ref null 0)))
663 (export "g" (global 0))
664 )
665 "#,
666 )?;
667
668 let struct_ty = StructType::new(
669 store.engine(),
670 [FieldType::new(Mutability::Const, StorageType::I8)],
671 )?;
672 let global_ty = GlobalType::new(
673 ValType::Ref(RefType::new(
674 true,
675 HeapType::ConcreteStruct(struct_ty.clone()),
676 )),
677 Mutability::Const,
678 );
679
680 // Instantiate with a null-ref global.
681 let g = Global::new(&mut store, global_ty.clone(), Val::AnyRef(None))?;
682 let instance = Instance::new(&mut store, &module, &[g.into()])?;
683 let g = instance.get_global(&mut store, "g").expect("export exists");
684 let val = g.get(&mut store);
685 assert!(val.unwrap_anyref().is_none());
686
687 // Instantiate with a non-null-ref global.
688 let pre = StructRefPre::new(&mut store, struct_ty);
689 let s0 = StructRef::new(&mut store, &pre, &[Val::I32(42)])?;
690 let g = Global::new(&mut store, global_ty, s0.into())?;
691 let instance = Instance::new(&mut store, &module, &[g.into()])?;
692 let g = instance.get_global(&mut store, "g").expect("export exists");
693 let val = g.get(&mut store);
694 let anyref = val.unwrap_anyref().expect("non-null");
695 let s1 = anyref.unwrap_struct(&store)?;
696 assert_eq!(s1.field(&mut store, 0)?.unwrap_i32(), 42);
697 assert!(Rooted::ref_eq(&store, &s0, &s1)?);
698
699 Ok(())
700}
701
702#[test]
703fn can_put_funcrefs_in_structs() -> Result<()> {

Callers

nothing calls this directly

Calls 12

gc_storeFunction · 0.85
OkFunction · 0.85
unwrap_anyrefMethod · 0.80
newFunction · 0.50
RefEnum · 0.50
AnyRefClass · 0.50
engineMethod · 0.45
cloneMethod · 0.45
expectMethod · 0.45
get_globalMethod · 0.45
getMethod · 0.45
unwrap_structMethod · 0.45

Tested by

no test coverage detected