wasmtime/runtime/gc/enabled/
eqref.rs1use crate::{
4 AnyRef, ArrayRef, ArrayType, AsContext, GcRefImpl, GcRootIndex, HeapType, I31, ManuallyRooted,
5 RefType, Rooted, StructRef, StructType, ValRaw, ValType, WasmTy,
6 prelude::*,
7 runtime::vm::VMGcRef,
8 store::{AutoAssertNoGc, StoreOpaque},
9};
10use core::mem::{self, MaybeUninit};
11use wasmtime_environ::VMGcKind;
12
13#[derive(Debug)]
90#[repr(transparent)]
91pub struct EqRef {
92 pub(super) inner: GcRootIndex,
93}
94
95impl From<Rooted<StructRef>> for Rooted<EqRef> {
96 #[inline]
97 fn from(s: Rooted<StructRef>) -> Self {
98 s.to_eqref()
99 }
100}
101
102impl From<ManuallyRooted<StructRef>> for ManuallyRooted<EqRef> {
103 #[inline]
104 fn from(s: ManuallyRooted<StructRef>) -> Self {
105 s.to_eqref()
106 }
107}
108
109impl From<Rooted<ArrayRef>> for Rooted<EqRef> {
110 #[inline]
111 fn from(s: Rooted<ArrayRef>) -> Self {
112 s.to_eqref()
113 }
114}
115
116impl From<ManuallyRooted<ArrayRef>> for ManuallyRooted<EqRef> {
117 #[inline]
118 fn from(s: ManuallyRooted<ArrayRef>) -> Self {
119 s.to_eqref()
120 }
121}
122
123unsafe impl GcRefImpl for EqRef {
124 fn transmute_ref(index: &GcRootIndex) -> &Self {
125 let me: &Self = unsafe { mem::transmute(index) };
127
128 assert!(matches!(
130 me,
131 Self {
132 inner: GcRootIndex { .. },
133 }
134 ));
135
136 me
137 }
138}
139
140impl Rooted<EqRef> {
141 #[inline]
143 pub fn to_anyref(self) -> Rooted<AnyRef> {
144 self.unchecked_cast()
145 }
146}
147
148impl ManuallyRooted<EqRef> {
149 #[inline]
151 pub fn to_anyref(self) -> ManuallyRooted<AnyRef> {
152 self.unchecked_cast()
153 }
154}
155
156impl EqRef {
157 pub(crate) fn from_cloned_gc_ref(
163 store: &mut AutoAssertNoGc<'_>,
164 gc_ref: VMGcRef,
165 ) -> Rooted<Self> {
166 debug_assert!(
167 gc_ref.is_i31()
168 || store
169 .unwrap_gc_store()
170 .header(&gc_ref)
171 .kind()
172 .matches(VMGcKind::EqRef)
173 );
174 Rooted::new(store, gc_ref)
175 }
176
177 #[inline]
178 pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
179 self.inner.comes_from_same_store(store)
180 }
181
182 pub fn ty(&self, store: impl AsContext) -> Result<HeapType> {
192 self._ty(store.as_context().0)
193 }
194
195 pub(crate) fn _ty(&self, store: &StoreOpaque) -> Result<HeapType> {
196 let gc_ref = self.inner.try_gc_ref(store)?;
197 if gc_ref.is_i31() {
198 return Ok(HeapType::I31);
199 }
200
201 let header = store.gc_store()?.header(gc_ref);
202
203 if header.kind().matches(VMGcKind::StructRef) {
204 return Ok(HeapType::ConcreteStruct(
205 StructType::from_shared_type_index(store.engine(), header.ty().unwrap()),
206 ));
207 }
208
209 if header.kind().matches(VMGcKind::ArrayRef) {
210 return Ok(HeapType::ConcreteArray(ArrayType::from_shared_type_index(
211 store.engine(),
212 header.ty().unwrap(),
213 )));
214 }
215
216 unreachable!("no other kinds of `eqref`s")
217 }
218
219 pub fn matches_ty(&self, store: impl AsContext, ty: &HeapType) -> Result<bool> {
231 self._matches_ty(store.as_context().0, ty)
232 }
233
234 pub(crate) fn _matches_ty(&self, store: &StoreOpaque, ty: &HeapType) -> Result<bool> {
235 assert!(self.comes_from_same_store(store));
236 Ok(self._ty(store)?.matches(ty))
237 }
238
239 pub(crate) fn ensure_matches_ty(&self, store: &StoreOpaque, ty: &HeapType) -> Result<()> {
240 if !self.comes_from_same_store(store) {
241 bail!("function used with wrong store");
242 }
243 if self._matches_ty(store, ty)? {
244 Ok(())
245 } else {
246 let actual_ty = self._ty(store)?;
247 bail!("type mismatch: expected `(ref {ty})`, found `(ref {actual_ty})`")
248 }
249 }
250
251 pub fn is_i31(&self, store: impl AsContext) -> Result<bool> {
261 self._is_i31(store.as_context().0)
262 }
263
264 pub(crate) fn _is_i31(&self, store: &StoreOpaque) -> Result<bool> {
265 assert!(self.comes_from_same_store(store));
266 let gc_ref = self.inner.try_gc_ref(store)?;
267 Ok(gc_ref.is_i31())
268 }
269
270 pub fn as_i31(&self, store: impl AsContext) -> Result<Option<I31>> {
284 self._as_i31(store.as_context().0)
285 }
286
287 pub(crate) fn _as_i31(&self, store: &StoreOpaque) -> Result<Option<I31>> {
288 assert!(self.comes_from_same_store(store));
289 let gc_ref = self.inner.try_gc_ref(store)?;
290 Ok(gc_ref.as_i31().map(Into::into))
291 }
292
293 pub fn unwrap_i31(&self, store: impl AsContext) -> Result<I31> {
305 Ok(self.as_i31(store)?.expect("EqRef::unwrap_i31 on non-i31"))
306 }
307
308 pub fn is_struct(&self, store: impl AsContext) -> Result<bool> {
318 self._is_struct(store.as_context().0)
319 }
320
321 pub(crate) fn _is_struct(&self, store: &StoreOpaque) -> Result<bool> {
322 let gc_ref = self.inner.try_gc_ref(store)?;
323 Ok(!gc_ref.is_i31() && store.gc_store()?.kind(gc_ref).matches(VMGcKind::StructRef))
324 }
325
326 pub fn as_struct(&self, store: impl AsContext) -> Result<Option<Rooted<StructRef>>> {
340 self._as_struct(store.as_context().0)
341 }
342
343 pub(crate) fn _as_struct(&self, store: &StoreOpaque) -> Result<Option<Rooted<StructRef>>> {
344 if self._is_struct(store)? {
345 Ok(Some(Rooted::from_gc_root_index(self.inner)))
346 } else {
347 Ok(None)
348 }
349 }
350
351 pub fn unwrap_struct(&self, store: impl AsContext) -> Result<Rooted<StructRef>> {
363 self._unwrap_struct(store.as_context().0)
364 }
365
366 pub(crate) fn _unwrap_struct(&self, store: &StoreOpaque) -> Result<Rooted<StructRef>> {
367 Ok(self
368 ._as_struct(store)?
369 .expect("EqRef::unwrap_struct on non-structref"))
370 }
371
372 pub fn is_array(&self, store: impl AsContext) -> Result<bool> {
382 self._is_array(store.as_context().0)
383 }
384
385 pub(crate) fn _is_array(&self, store: &StoreOpaque) -> Result<bool> {
386 let gc_ref = self.inner.try_gc_ref(store)?;
387 Ok(!gc_ref.is_i31() && store.gc_store()?.kind(gc_ref).matches(VMGcKind::ArrayRef))
388 }
389
390 pub fn as_array(&self, store: impl AsContext) -> Result<Option<Rooted<ArrayRef>>> {
404 self._as_array(store.as_context().0)
405 }
406
407 pub(crate) fn _as_array(&self, store: &StoreOpaque) -> Result<Option<Rooted<ArrayRef>>> {
408 if self._is_array(store)? {
409 Ok(Some(Rooted::from_gc_root_index(self.inner)))
410 } else {
411 Ok(None)
412 }
413 }
414
415 pub fn unwrap_array(&self, store: impl AsContext) -> Result<Rooted<ArrayRef>> {
427 self._unwrap_array(store.as_context().0)
428 }
429
430 pub(crate) fn _unwrap_array(&self, store: &StoreOpaque) -> Result<Rooted<ArrayRef>> {
431 Ok(self
432 ._as_array(store)?
433 .expect("EqRef::unwrap_array on non-arrayref"))
434 }
435}
436
437unsafe impl WasmTy for Rooted<EqRef> {
438 #[inline]
439 fn valtype() -> ValType {
440 ValType::Ref(RefType::new(false, HeapType::Eq))
441 }
442
443 #[inline]
444 fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
445 self.comes_from_same_store(store)
446 }
447
448 #[inline]
449 fn dynamic_concrete_type_check(
450 &self,
451 store: &StoreOpaque,
452 _nullable: bool,
453 ty: &HeapType,
454 ) -> Result<()> {
455 self.ensure_matches_ty(store, ty)
456 }
457
458 fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
459 self.wasm_ty_store(store, ptr, ValRaw::anyref)
460 }
461
462 unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self {
463 Self::wasm_ty_load(store, ptr.get_anyref(), EqRef::from_cloned_gc_ref)
464 }
465}
466
467unsafe impl WasmTy for Option<Rooted<EqRef>> {
468 #[inline]
469 fn valtype() -> ValType {
470 ValType::EQREF
471 }
472
473 #[inline]
474 fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
475 self.map_or(true, |x| x.comes_from_same_store(store))
476 }
477
478 #[inline]
479 fn dynamic_concrete_type_check(
480 &self,
481 store: &StoreOpaque,
482 nullable: bool,
483 ty: &HeapType,
484 ) -> Result<()> {
485 match self {
486 Some(s) => Rooted::<EqRef>::dynamic_concrete_type_check(s, store, nullable, ty),
487 None => {
488 ensure!(
489 nullable,
490 "expected a non-null reference, but found a null reference"
491 );
492 Ok(())
493 }
494 }
495 }
496
497 #[inline]
498 fn is_vmgcref_and_points_to_object(&self) -> bool {
499 self.is_some()
500 }
501
502 fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
503 <Rooted<EqRef>>::wasm_ty_option_store(self, store, ptr, ValRaw::anyref)
504 }
505
506 unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self {
507 <Rooted<EqRef>>::wasm_ty_option_load(store, ptr.get_anyref(), EqRef::from_cloned_gc_ref)
508 }
509}
510
511unsafe impl WasmTy for ManuallyRooted<EqRef> {
512 #[inline]
513 fn valtype() -> ValType {
514 ValType::Ref(RefType::new(false, HeapType::Eq))
515 }
516
517 #[inline]
518 fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
519 self.comes_from_same_store(store)
520 }
521
522 #[inline]
523 fn dynamic_concrete_type_check(
524 &self,
525 store: &StoreOpaque,
526 _: bool,
527 ty: &HeapType,
528 ) -> Result<()> {
529 self.ensure_matches_ty(store, ty)
530 }
531
532 fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
533 self.wasm_ty_store(store, ptr, ValRaw::anyref)
534 }
535
536 unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self {
537 Self::wasm_ty_load(store, ptr.get_anyref(), EqRef::from_cloned_gc_ref)
538 }
539}
540
541unsafe impl WasmTy for Option<ManuallyRooted<EqRef>> {
542 #[inline]
543 fn valtype() -> ValType {
544 ValType::EQREF
545 }
546
547 #[inline]
548 fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
549 self.as_ref()
550 .map_or(true, |x| x.comes_from_same_store(store))
551 }
552
553 #[inline]
554 fn dynamic_concrete_type_check(
555 &self,
556 store: &StoreOpaque,
557 nullable: bool,
558 ty: &HeapType,
559 ) -> Result<()> {
560 match self {
561 Some(s) => ManuallyRooted::<EqRef>::dynamic_concrete_type_check(s, store, nullable, ty),
562 None => {
563 ensure!(
564 nullable,
565 "expected a non-null reference, but found a null reference"
566 );
567 Ok(())
568 }
569 }
570 }
571
572 #[inline]
573 fn is_vmgcref_and_points_to_object(&self) -> bool {
574 self.is_some()
575 }
576
577 fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
578 <ManuallyRooted<EqRef>>::wasm_ty_option_store(self, store, ptr, ValRaw::anyref)
579 }
580
581 unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self {
582 <ManuallyRooted<EqRef>>::wasm_ty_option_load(
583 store,
584 ptr.get_anyref(),
585 EqRef::from_cloned_gc_ref,
586 )
587 }
588}