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

Function wasm_sets_struct_global

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

Source from the content-addressed store, hash-verified

505#[test]
506#[cfg_attr(miri, ignore)]
507fn wasm_sets_struct_global() -> Result<()> {
508 let mut store = gc_store()?;
509
510 let module = Module::new(
511 store.engine(),
512 r#"
513 (module
514 (type (struct (field i8)))
515 (global $g (export "g") (mut (ref null 0)) (ref.null 0))
516 (func (export "get") (result (ref null 0))
517 global.get $g
518 )
519 (func (export "set") (param (ref null 0))
520 local.get 0
521 global.set $g
522 )
523 )
524 "#,
525 )?;
526
527 let struct_ty = StructType::new(
528 store.engine(),
529 [FieldType::new(Mutability::Const, StorageType::I8)],
530 )?;
531 let pre = StructRefPre::new(&mut store, struct_ty.clone());
532 let s0 = StructRef::new(&mut store, &pre, &[Val::I32(42)])?;
533
534 let instance = Instance::new(&mut store, &module, &[])?;
535 let set = instance.get_func(&mut store, "set").unwrap();
536 set.call(&mut store, &[s0.into()], &mut [])?;
537
538 // Get the global from the host.
539 let g = instance.get_global(&mut store, "g").unwrap();
540 let val = g.get(&mut store);
541 let anyref = val.unwrap_anyref().expect("non-null");
542 let s1 = anyref.unwrap_struct(&store)?;
543 assert_eq!(s1.field(&mut store, 0)?.unwrap_i32(), 42);
544 assert!(Rooted::ref_eq(&store, &s0, &s1)?);
545
546 // Get the global from the guest.
547 let f = instance.get_typed_func::<(), Option<Rooted<StructRef>>>(&mut store, "get")?;
548 let s2 = f.call(&mut store, ())?.expect("non-null");
549 assert_eq!(s2.field(&mut store, 0)?.unwrap_i32(), 42);
550 assert!(Rooted::ref_eq(&store, &s0, &s2)?);
551
552 Ok(())
553}
554
555#[test]
556#[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
cloneMethod · 0.45
unwrapMethod · 0.45
get_funcMethod · 0.45
callMethod · 0.45
get_globalMethod · 0.45
getMethod · 0.45
expectMethod · 0.45

Tested by

no test coverage detected