1 //! Working with GC `struct` objects. 2 3 use crate::runtime::vm::VMGcRef; 4 use crate::store::StoreId; 5 use crate::vm::{VMGcHeader, VMStructRef}; 6 use crate::{ 7 prelude::*, 8 store::{AutoAssertNoGc, StoreContextMut, StoreOpaque}, 9 AsContext, AsContextMut, EqRef, GcHeapOutOfMemory, GcRefImpl, GcRootIndex, HeapType, 10 ManuallyRooted, RefType, Rooted, StructType, Val, ValRaw, ValType, WasmTy, 11 }; 12 use crate::{AnyRef, FieldType}; 13 use core::mem::{self, MaybeUninit}; 14 use wasmtime_environ::{GcLayout, GcStructLayout, VMGcKind, VMSharedTypeIndex}; 15 16 /// An allocator for a particular Wasm GC struct type. 17 /// 18 /// Every `StructRefPre` is associated with a particular 19 /// [`Store`][crate::Store] and a particular [StructType][crate::StructType]. 20 /// 21 /// Reusing an allocator across many allocations amortizes some per-type runtime 22 /// overheads inside Wasmtime. A `StructRefPre` is to `StructRef`s as an 23 /// `InstancePre` is to `Instance`s. 24 /// 25 /// # Example 26 /// 27 /// ``` 28 /// use wasmtime::*; 29 /// 30 /// # fn foo() -> Result<()> { 31 /// let mut config = Config::new(); 32 /// config.wasm_function_references(true); 33 /// config.wasm_gc(true); 34 /// 35 /// let engine = Engine::new(&config)?; 36 /// let mut store = Store::new(&engine, ()); 37 /// 38 /// // Define a struct type. 39 /// let struct_ty = StructType::new( 40 /// store.engine(), 41 /// [FieldType::new(Mutability::Var, StorageType::I8)], 42 /// )?; 43 /// 44 /// // Create an allocator for the struct type. 45 /// let allocator = StructRefPre::new(&mut store, struct_ty); 46 /// 47 /// { 48 /// let mut scope = RootScope::new(&mut store); 49 /// 50 /// // Allocate a bunch of instances of our struct type using the same 51 /// // allocator! This is faster than creating a new allocator for each 52 /// // instance we want to allocate. 53 /// for i in 0..10 { 54 /// StructRef::new(&mut scope, &allocator, &[Val::I32(i)])?; 55 /// } 56 /// } 57 /// # Ok(()) 58 /// # } 59 /// # foo().unwrap(); 60 /// ``` 61 pub struct StructRefPre { 62 store_id: StoreId, 63 ty: StructType, 64 } 65 66 impl StructRefPre { 67 /// Create a new `StructRefPre` that is associated with the given store 68 /// and type. 69 pub fn new(mut store: impl AsContextMut, ty: StructType) -> Self { 70 Self::_new(store.as_context_mut().0, ty) 71 } 72 73 pub(crate) fn _new(store: &mut StoreOpaque, ty: StructType) -> Self { 74 store.insert_gc_host_alloc_type(ty.registered_type().clone()); 75 let store_id = store.id(); 76 77 StructRefPre { store_id, ty } 78 } 79 80 pub(crate) fn layout(&self) -> &GcStructLayout { 81 self.ty 82 .registered_type() 83 .layout() 84 .expect("struct types have a layout") 85 .unwrap_struct() 86 } 87 88 pub(crate) fn type_index(&self) -> VMSharedTypeIndex { 89 self.ty.registered_type().index() 90 } 91 } 92 93 /// A reference to a GC-managed `struct` instance. 94 /// 95 /// WebAssembly `struct`s are static, fixed-length, ordered sequences of 96 /// fields. Fields are named by index, not by identifier; in this way, they are 97 /// similar to Rust's tuples. Each field is mutable or constant and stores 98 /// unpacked [`Val`][crate::Val]s or packed 8-/16-bit integers. 99 /// 100 /// Like all WebAssembly references, these are opaque and unforgeable to Wasm: 101 /// they cannot be faked and Wasm cannot, for example, cast the integer 102 /// `0x12345678` into a reference, pretend it is a valid `structref`, and trick 103 /// the host into dereferencing it and segfaulting or worse. 104 /// 105 /// Note that you can also use `Rooted<StructRef>` and 106 /// `ManuallyRooted<StructRef>` as a type parameter with 107 /// [`Func::typed`][crate::Func::typed]- and 108 /// [`Func::wrap`][crate::Func::wrap]-style APIs. 109 /// 110 /// # Example 111 /// 112 /// ``` 113 /// use wasmtime::*; 114 /// 115 /// # fn foo() -> Result<()> { 116 /// let mut config = Config::new(); 117 /// config.wasm_function_references(true); 118 /// config.wasm_gc(true); 119 /// 120 /// let engine = Engine::new(&config)?; 121 /// let mut store = Store::new(&engine, ()); 122 /// 123 /// // Define a struct type. 124 /// let struct_ty = StructType::new( 125 /// store.engine(), 126 /// [FieldType::new(Mutability::Var, StorageType::I8)], 127 /// )?; 128 /// 129 /// // Create an allocator for the struct type. 130 /// let allocator = StructRefPre::new(&mut store, struct_ty); 131 /// 132 /// { 133 /// let mut scope = RootScope::new(&mut store); 134 /// 135 /// // Allocate an instance of the struct type. 136 /// let my_struct = match StructRef::new(&mut scope, &allocator, &[Val::I32(42)]) { 137 /// Ok(s) => s, 138 /// // If the heap is out of memory, then do a GC and try again. 139 /// Err(e) if e.is::<GcHeapOutOfMemory<()>>() => { 140 /// // Do a GC! Note: in an async context, you'd want to do 141 /// // `scope.as_context_mut().gc_async().await`. 142 /// scope.as_context_mut().gc(); 143 /// 144 /// StructRef::new(&mut scope, &allocator, &[Val::I32(42)])? 145 /// } 146 /// Err(e) => return Err(e), 147 /// }; 148 /// 149 /// // That instance's field should have the expected value. 150 /// let val = my_struct.field(&mut scope, 0)?.unwrap_i32(); 151 /// assert_eq!(val, 42); 152 /// 153 /// // And we can update the field's value because it is a mutable field. 154 /// my_struct.set_field(&mut scope, 0, Val::I32(36))?; 155 /// let new_val = my_struct.field(&mut scope, 0)?.unwrap_i32(); 156 /// assert_eq!(new_val, 36); 157 /// } 158 /// # Ok(()) 159 /// # } 160 /// # foo().unwrap(); 161 /// ``` 162 #[derive(Debug)] 163 #[repr(transparent)] 164 pub struct StructRef { 165 pub(super) inner: GcRootIndex, 166 } 167 168 unsafe impl GcRefImpl for StructRef { 169 #[allow(private_interfaces)] 170 fn transmute_ref(index: &GcRootIndex) -> &Self { 171 // Safety: `StructRef` is a newtype of a `GcRootIndex`. 172 let me: &Self = unsafe { mem::transmute(index) }; 173 174 // Assert we really are just a newtype of a `GcRootIndex`. 175 assert!(matches!( 176 me, 177 Self { 178 inner: GcRootIndex { .. }, 179 } 180 )); 181 182 me 183 } 184 } 185 186 impl Rooted<StructRef> { 187 /// Upcast this `structref` into an `anyref`. 188 #[inline] 189 pub fn to_anyref(self) -> Rooted<AnyRef> { 190 self.unchecked_cast() 191 } 192 193 /// Upcast this `structref` into an `eqref`. 194 #[inline] 195 pub fn to_eqref(self) -> Rooted<EqRef> { 196 self.unchecked_cast() 197 } 198 } 199 200 impl ManuallyRooted<StructRef> { 201 /// Upcast this `structref` into an `anyref`. 202 #[inline] 203 pub fn to_anyref(self) -> ManuallyRooted<AnyRef> { 204 self.unchecked_cast() 205 } 206 207 /// Upcast this `structref` into an `eqref`. 208 #[inline] 209 pub fn to_eqref(self) -> ManuallyRooted<EqRef> { 210 self.unchecked_cast() 211 } 212 } 213 214 impl StructRef { 215 /// Allocate a new `struct` and get a reference to it. 216 /// 217 /// # Errors 218 /// 219 /// If the given `fields` values' types do not match the field types of the 220 /// `allocator`'s struct type, an error is returned. 221 /// 222 /// If the allocation cannot be satisfied because the GC heap is currently 223 /// out of memory, but performing a garbage collection might free up space 224 /// such that retrying the allocation afterwards might succeed, then a 225 /// [`GcHeapOutOfMemory<()>`][crate::GcHeapOutOfMemory] error is returned. 226 /// 227 /// # Panics 228 /// 229 /// Panics if the allocator, or any of the field values, is not associated 230 /// with the given store. 231 pub fn new( 232 mut store: impl AsContextMut, 233 allocator: &StructRefPre, 234 fields: &[Val], 235 ) -> Result<Rooted<StructRef>> { 236 Self::_new(store.as_context_mut().0, allocator, fields) 237 } 238 239 pub(crate) fn _new( 240 store: &mut StoreOpaque, 241 allocator: &StructRefPre, 242 fields: &[Val], 243 ) -> Result<Rooted<StructRef>> { 244 assert_eq!( 245 store.id(), 246 allocator.store_id, 247 "attempted to use a `StructRefPre` with the wrong store" 248 ); 249 250 // Type check the given values against the field types. 251 let expected_len = allocator.ty.fields().len(); 252 let actual_len = fields.len(); 253 ensure!( 254 actual_len == expected_len, 255 "expected {expected_len} fields, got {actual_len}" 256 ); 257 for (ty, val) in allocator.ty.fields().zip(fields) { 258 assert!( 259 val.comes_from_same_store(store), 260 "field value comes from the wrong store", 261 ); 262 let ty = ty.element_type().unpack(); 263 val.ensure_matches_ty(store, ty) 264 .context("field type mismatch")?; 265 } 266 267 // Allocate the struct and write each field value into the appropriate 268 // offset. 269 let structref = store 270 .gc_store_mut()? 271 .alloc_uninit_struct(allocator.type_index(), &allocator.layout()) 272 .err2anyhow() 273 .context("unrecoverable error when allocating new `structref`")? 274 .ok_or_else(|| GcHeapOutOfMemory::new(())) 275 .err2anyhow()?; 276 277 // From this point on, if we get any errors, then the struct is not 278 // fully initialized, so we need to eagerly deallocate it before the 279 // next GC where the collector might try to interpret one of the 280 // uninitialized fields as a GC reference. 281 let mut store = AutoAssertNoGc::new(store); 282 match (|| { 283 for (index, (ty, val)) in allocator.ty.fields().zip(fields).enumerate() { 284 structref.initialize_field( 285 &mut store, 286 allocator.layout(), 287 ty.element_type(), 288 index, 289 *val, 290 )?; 291 } 292 Ok(()) 293 })() { 294 Ok(()) => Ok(Rooted::new(&mut store, structref.into())), 295 Err(e) => { 296 store.gc_store_mut()?.dealloc_uninit_struct(structref); 297 Err(e) 298 } 299 } 300 } 301 302 #[inline] 303 pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool { 304 self.inner.comes_from_same_store(store) 305 } 306 307 /// Get this `structref`'s type. 308 /// 309 /// # Errors 310 /// 311 /// Return an error if this reference has been unrooted. 312 /// 313 /// # Panics 314 /// 315 /// Panics if this reference is associated with a different store. 316 pub fn ty(&self, store: impl AsContext) -> Result<StructType> { 317 self._ty(store.as_context().0) 318 } 319 320 pub(crate) fn _ty(&self, store: &StoreOpaque) -> Result<StructType> { 321 assert!(self.comes_from_same_store(store)); 322 let index = self.type_index(store)?; 323 Ok(StructType::from_shared_type_index(store.engine(), index)) 324 } 325 326 /// Does this `structref` match the given type? 327 /// 328 /// That is, is this struct's type a subtype of the given type? 329 /// 330 /// # Errors 331 /// 332 /// Return an error if this reference has been unrooted. 333 /// 334 /// # Panics 335 /// 336 /// Panics if this reference is associated with a different store or if the 337 /// type is not associated with the store's engine. 338 pub fn matches_ty(&self, store: impl AsContext, ty: &StructType) -> Result<bool> { 339 self._matches_ty(store.as_context().0, ty) 340 } 341 342 pub(crate) fn _matches_ty(&self, store: &StoreOpaque, ty: &StructType) -> Result<bool> { 343 assert!(self.comes_from_same_store(store)); 344 Ok(self._ty(store)?.matches(ty)) 345 } 346 347 pub(crate) fn ensure_matches_ty(&self, store: &StoreOpaque, ty: &StructType) -> Result<()> { 348 if !self.comes_from_same_store(store) { 349 bail!("function used with wrong store"); 350 } 351 if self._matches_ty(store, ty)? { 352 Ok(()) 353 } else { 354 let actual_ty = self._ty(store)?; 355 bail!("type mismatch: expected `(ref {ty})`, found `(ref {actual_ty})`") 356 } 357 } 358 359 /// Get the values of this struct's fields. 360 /// 361 /// Note that `i8` and `i16` field values are zero-extended into 362 /// `Val::I32(_)`s. 363 /// 364 /// # Errors 365 /// 366 /// Return an error if this reference has been unrooted. 367 /// 368 /// # Panics 369 /// 370 /// Panics if this reference is associated with a different store. 371 pub fn fields<'a, T: 'a>( 372 &'a self, 373 store: impl Into<StoreContextMut<'a, T>>, 374 ) -> Result<impl ExactSizeIterator<Item = Val> + 'a> { 375 self._fields(store.into().0) 376 } 377 378 pub(crate) fn _fields<'a>( 379 &'a self, 380 store: &'a mut StoreOpaque, 381 ) -> Result<impl ExactSizeIterator<Item = Val> + 'a> { 382 assert!(self.comes_from_same_store(store)); 383 let store = AutoAssertNoGc::new(store); 384 385 let gc_ref = self.inner.try_gc_ref(&store)?; 386 let header = store.gc_store()?.header(gc_ref); 387 debug_assert!(header.kind().matches(VMGcKind::StructRef)); 388 389 let index = header.ty().expect("structrefs should have concrete types"); 390 let ty = StructType::from_shared_type_index(store.engine(), index); 391 let len = ty.fields().len(); 392 393 return Ok(Fields { 394 structref: self, 395 store, 396 index: 0, 397 len, 398 }); 399 400 struct Fields<'a, 'b> { 401 structref: &'a StructRef, 402 store: AutoAssertNoGc<'b>, 403 index: usize, 404 len: usize, 405 } 406 407 impl Iterator for Fields<'_, '_> { 408 type Item = Val; 409 410 #[inline] 411 fn next(&mut self) -> Option<Self::Item> { 412 let i = self.index; 413 debug_assert!(i <= self.len); 414 if i >= self.len { 415 return None; 416 } 417 self.index += 1; 418 Some(self.structref._field(&mut self.store, i).unwrap()) 419 } 420 421 #[inline] 422 fn size_hint(&self) -> (usize, Option<usize>) { 423 let len = self.len - self.index; 424 (len, Some(len)) 425 } 426 } 427 428 impl ExactSizeIterator for Fields<'_, '_> { 429 #[inline] 430 fn len(&self) -> usize { 431 self.len - self.index 432 } 433 } 434 } 435 436 fn header<'a>(&self, store: &'a AutoAssertNoGc<'_>) -> Result<&'a VMGcHeader> { 437 assert!(self.comes_from_same_store(&store)); 438 let gc_ref = self.inner.try_gc_ref(store)?; 439 Ok(store.gc_store()?.header(gc_ref)) 440 } 441 442 fn structref<'a>(&self, store: &'a AutoAssertNoGc<'_>) -> Result<&'a VMStructRef> { 443 assert!(self.comes_from_same_store(&store)); 444 let gc_ref = self.inner.try_gc_ref(store)?; 445 debug_assert!(self.header(store)?.kind().matches(VMGcKind::StructRef)); 446 Ok(gc_ref.as_structref_unchecked()) 447 } 448 449 fn layout(&self, store: &AutoAssertNoGc<'_>) -> Result<GcStructLayout> { 450 assert!(self.comes_from_same_store(&store)); 451 let type_index = self.type_index(store)?; 452 let layout = store 453 .engine() 454 .signatures() 455 .layout(type_index) 456 .expect("struct types should have GC layouts"); 457 match layout { 458 GcLayout::Struct(s) => Ok(s), 459 GcLayout::Array(_) => unreachable!(), 460 } 461 } 462 463 fn field_ty(&self, store: &StoreOpaque, field: usize) -> Result<FieldType> { 464 let ty = self._ty(store)?; 465 match ty.field(field) { 466 Some(f) => Ok(f), 467 None => { 468 let len = ty.fields().len(); 469 bail!("cannot access field {field}: struct only has {len} fields") 470 } 471 } 472 } 473 474 /// Get this struct's `index`th field. 475 /// 476 /// Note that `i8` and `i16` field values are zero-extended into 477 /// `Val::I32(_)`s. 478 /// 479 /// # Errors 480 /// 481 /// Returns an `Err(_)` if the index is out of bounds or this reference has 482 /// been unrooted. 483 /// 484 /// # Panics 485 /// 486 /// Panics if this reference is associated with a different store. 487 pub fn field(&self, mut store: impl AsContextMut, index: usize) -> Result<Val> { 488 let mut store = AutoAssertNoGc::new(store.as_context_mut().0); 489 self._field(&mut store, index) 490 } 491 492 pub(crate) fn _field(&self, store: &mut AutoAssertNoGc<'_>, index: usize) -> Result<Val> { 493 assert!(self.comes_from_same_store(store)); 494 let structref = self.structref(store)?.unchecked_copy(); 495 let field_ty = self.field_ty(store, index)?; 496 let layout = self.layout(store)?; 497 Ok(structref.read_field(store, &layout, field_ty.element_type(), index)) 498 } 499 500 /// Set this struct's `index`th field. 501 /// 502 /// # Errors 503 /// 504 /// Returns an error in the following scenarios: 505 /// 506 /// * When given a value of the wrong type, such as trying to set an `f32` 507 /// field to an `i64` value. 508 /// 509 /// * When the field is not mutable. 510 /// 511 /// * When this struct does not have an `index`th field, i.e. `index` is out 512 /// of bounds. 513 /// 514 /// * When `value` is a GC reference that has since been unrooted. 515 /// 516 /// # Panics 517 /// 518 /// Panics if this reference is associated with a different store. 519 pub fn set_field(&self, mut store: impl AsContextMut, index: usize, value: Val) -> Result<()> { 520 self._set_field(store.as_context_mut().0, index, value) 521 } 522 523 pub(crate) fn _set_field( 524 &self, 525 store: &mut StoreOpaque, 526 index: usize, 527 value: Val, 528 ) -> Result<()> { 529 assert!(self.comes_from_same_store(store)); 530 let mut store = AutoAssertNoGc::new(store); 531 532 let field_ty = self.field_ty(&store, index)?; 533 ensure!( 534 field_ty.mutability().is_var(), 535 "cannot set field {index}: field is not mutable" 536 ); 537 538 value 539 .ensure_matches_ty(&store, &field_ty.element_type().unpack()) 540 .with_context(|| format!("cannot set field {index}: type mismatch"))?; 541 542 let layout = self.layout(&store)?; 543 let structref = self.structref(&store)?.unchecked_copy(); 544 545 structref.write_field(&mut store, &layout, field_ty.element_type(), index, value) 546 } 547 548 pub(crate) fn type_index(&self, store: &StoreOpaque) -> Result<VMSharedTypeIndex> { 549 let gc_ref = self.inner.try_gc_ref(store)?; 550 let header = store.gc_store()?.header(gc_ref); 551 debug_assert!(header.kind().matches(VMGcKind::StructRef)); 552 Ok(header.ty().expect("structrefs should have concrete types")) 553 } 554 555 /// Create a new `Rooted<StructRef>` from the given GC reference. 556 /// 557 /// `gc_ref` should point to a valid `structref` and should belong to the 558 /// store's GC heap. Failure to uphold these invariants is memory safe but 559 /// will lead to general incorrectness such as panics or wrong results. 560 pub(crate) fn from_cloned_gc_ref( 561 store: &mut AutoAssertNoGc<'_>, 562 gc_ref: VMGcRef, 563 ) -> Rooted<Self> { 564 debug_assert!(!gc_ref.is_i31()); 565 Rooted::new(store, gc_ref) 566 } 567 } 568 569 unsafe impl WasmTy for Rooted<StructRef> { 570 #[inline] 571 fn valtype() -> ValType { 572 ValType::Ref(RefType::new(false, HeapType::Struct)) 573 } 574 575 #[inline] 576 fn compatible_with_store(&self, store: &StoreOpaque) -> bool { 577 self.comes_from_same_store(store) 578 } 579 580 #[inline] 581 fn dynamic_concrete_type_check( 582 &self, 583 store: &StoreOpaque, 584 _nullable: bool, 585 ty: &HeapType, 586 ) -> Result<()> { 587 match ty { 588 HeapType::Any | HeapType::Eq | HeapType::Struct => Ok(()), 589 HeapType::ConcreteStruct(ty) => self.ensure_matches_ty(store, ty), 590 591 HeapType::Extern 592 | HeapType::NoExtern 593 | HeapType::Func 594 | HeapType::ConcreteFunc(_) 595 | HeapType::NoFunc 596 | HeapType::I31 597 | HeapType::Array 598 | HeapType::ConcreteArray(_) 599 | HeapType::None => bail!( 600 "type mismatch: expected `(ref {ty})`, got `(ref {})`", 601 self._ty(store)?, 602 ), 603 } 604 } 605 606 fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> { 607 self.wasm_ty_store(store, ptr, ValRaw::anyref) 608 } 609 610 unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self { 611 Self::wasm_ty_load(store, ptr.get_anyref(), StructRef::from_cloned_gc_ref) 612 } 613 } 614 615 unsafe impl WasmTy for Option<Rooted<StructRef>> { 616 #[inline] 617 fn valtype() -> ValType { 618 ValType::STRUCTREF 619 } 620 621 #[inline] 622 fn compatible_with_store(&self, store: &StoreOpaque) -> bool { 623 self.map_or(true, |x| x.comes_from_same_store(store)) 624 } 625 626 #[inline] 627 fn dynamic_concrete_type_check( 628 &self, 629 store: &StoreOpaque, 630 nullable: bool, 631 ty: &HeapType, 632 ) -> Result<()> { 633 match self { 634 Some(s) => Rooted::<StructRef>::dynamic_concrete_type_check(s, store, nullable, ty), 635 None => { 636 ensure!( 637 nullable, 638 "expected a non-null reference, but found a null reference" 639 ); 640 Ok(()) 641 } 642 } 643 } 644 645 #[inline] 646 fn is_vmgcref_and_points_to_object(&self) -> bool { 647 self.is_some() 648 } 649 650 fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> { 651 <Rooted<StructRef>>::wasm_ty_option_store(self, store, ptr, ValRaw::anyref) 652 } 653 654 unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self { 655 <Rooted<StructRef>>::wasm_ty_option_load( 656 store, 657 ptr.get_anyref(), 658 StructRef::from_cloned_gc_ref, 659 ) 660 } 661 } 662 663 unsafe impl WasmTy for ManuallyRooted<StructRef> { 664 #[inline] 665 fn valtype() -> ValType { 666 ValType::Ref(RefType::new(false, HeapType::Struct)) 667 } 668 669 #[inline] 670 fn compatible_with_store(&self, store: &StoreOpaque) -> bool { 671 self.comes_from_same_store(store) 672 } 673 674 #[inline] 675 fn dynamic_concrete_type_check( 676 &self, 677 store: &StoreOpaque, 678 _: bool, 679 ty: &HeapType, 680 ) -> Result<()> { 681 match ty { 682 HeapType::Any | HeapType::Eq | HeapType::Struct => Ok(()), 683 HeapType::ConcreteStruct(ty) => self.ensure_matches_ty(store, ty), 684 685 HeapType::Extern 686 | HeapType::NoExtern 687 | HeapType::Func 688 | HeapType::ConcreteFunc(_) 689 | HeapType::NoFunc 690 | HeapType::I31 691 | HeapType::Array 692 | HeapType::ConcreteArray(_) 693 | HeapType::None => bail!( 694 "type mismatch: expected `(ref {ty})`, got `(ref {})`", 695 self._ty(store)?, 696 ), 697 } 698 } 699 700 fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> { 701 self.wasm_ty_store(store, ptr, ValRaw::anyref) 702 } 703 704 unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self { 705 Self::wasm_ty_load(store, ptr.get_anyref(), StructRef::from_cloned_gc_ref) 706 } 707 } 708 709 unsafe impl WasmTy for Option<ManuallyRooted<StructRef>> { 710 #[inline] 711 fn valtype() -> ValType { 712 ValType::STRUCTREF 713 } 714 715 #[inline] 716 fn compatible_with_store(&self, store: &StoreOpaque) -> bool { 717 self.as_ref() 718 .map_or(true, |x| x.comes_from_same_store(store)) 719 } 720 721 #[inline] 722 fn dynamic_concrete_type_check( 723 &self, 724 store: &StoreOpaque, 725 nullable: bool, 726 ty: &HeapType, 727 ) -> Result<()> { 728 match self { 729 Some(s) => { 730 ManuallyRooted::<StructRef>::dynamic_concrete_type_check(s, store, nullable, ty) 731 } 732 None => { 733 ensure!( 734 nullable, 735 "expected a non-null reference, but found a null reference" 736 ); 737 Ok(()) 738 } 739 } 740 } 741 742 #[inline] 743 fn is_vmgcref_and_points_to_object(&self) -> bool { 744 self.is_some() 745 } 746 747 fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> { 748 <ManuallyRooted<StructRef>>::wasm_ty_option_store(self, store, ptr, ValRaw::anyref) 749 } 750 751 unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self { 752 <ManuallyRooted<StructRef>>::wasm_ty_option_load( 753 store, 754 ptr.get_anyref(), 755 StructRef::from_cloned_gc_ref, 756 ) 757 } 758 } 759