1use crate::store::{AutoAssertNoGc, StoreOpaque};
2use crate::{
3 AnyRef, ArrayRef, AsContext, AsContextMut, ExnRef, ExternRef, Func, HeapTopType, HeapType,
4 RefType, Rooted, StructRef, V128, ValType, prelude::*,
5};
6use core::ptr;
7
8pub use crate::runtime::vm::ValRaw;
9
10#[derive(Debug, Clone, Copy)]
15pub struct ContRef;
16
17#[derive(Debug, Clone, Copy)]
23pub enum Val {
24 I32(i32),
29
30 I64(i64),
32
33 F32(u32),
38
39 F64(u64),
44
45 V128(V128),
47
48 FuncRef(Option<Func>),
50
51 ExternRef(Option<Rooted<ExternRef>>),
53
54 AnyRef(Option<Rooted<AnyRef>>),
56
57 ExnRef(Option<Rooted<ExnRef>>),
59
60 ContRef(Option<ContRef>),
65}
66
67macro_rules! accessors {
68 ($bind:ident $(($variant:ident($ty:ty) $get:ident $unwrap:ident $cvt:expr))*) => ($(
69 #[inline]
72 pub fn $get(&self) -> Option<$ty> {
73 if let Val::$variant($bind) = self {
74 Some($cvt)
75 } else {
76 None
77 }
78 }
79
80 #[inline]
87 pub fn $unwrap(&self) -> $ty {
88 self.$get().expect(concat!("expected ", stringify!($ty)))
89 }
90 )*)
91}
92
93impl Val {
94 #[inline]
96 pub fn null_ref(heap_type: &HeapType) -> Val {
97 Ref::null(&heap_type).into()
98 }
99
100 #[inline]
105 pub const fn null_func_ref() -> Val {
106 Val::FuncRef(None)
107 }
108
109 #[inline]
114 pub const fn null_extern_ref() -> Val {
115 Val::ExternRef(None)
116 }
117
118 #[inline]
123 pub const fn null_any_ref() -> Val {
124 Val::AnyRef(None)
125 }
126
127 pub fn default_for_ty(ty: &ValType) -> Option<Val> {
132 match ty {
133 ValType::I32 => Some(Val::I32(0)),
134 ValType::I64 => Some(Val::I64(0)),
135 ValType::F32 => Some(Val::F32(0)),
136 ValType::F64 => Some(Val::F64(0)),
137 ValType::V128 => Some(Val::V128(V128::from(0))),
138 ValType::Ref(ref_ty) => {
139 if ref_ty.is_nullable() {
140 Some(Val::null_ref(ref_ty.heap_type()))
141 } else {
142 None
143 }
144 }
145 }
146 }
147
148 #[inline]
159 pub fn ty(&self, store: impl AsContext) -> Result<ValType> {
160 self.load_ty(&store.as_context().0)
161 }
162
163 #[inline]
164 pub(crate) fn load_ty(&self, store: &StoreOpaque) -> Result<ValType> {
165 Ok(match self {
166 Val::I32(_) => ValType::I32,
167 Val::I64(_) => ValType::I64,
168 Val::F32(_) => ValType::F32,
169 Val::F64(_) => ValType::F64,
170 Val::V128(_) => ValType::V128,
171 Val::ExternRef(Some(e)) => {
172 e.try_gc_ref(store)?;
173 ValType::Ref(RefType::new(false, HeapType::Extern))
174 }
175 Val::ExternRef(None) => ValType::NULLEXTERNREF,
176 Val::FuncRef(None) => ValType::NULLFUNCREF,
177 Val::FuncRef(Some(f)) => ValType::Ref(RefType::new(
178 false,
179 HeapType::ConcreteFunc(f.load_ty(store)),
180 )),
181 Val::AnyRef(None) => ValType::NULLREF,
182 Val::AnyRef(Some(a)) => ValType::Ref(RefType::new(false, a._ty(store)?)),
183 Val::ExnRef(None) => ValType::NULLEXNREF,
184 Val::ExnRef(Some(e)) => {
185 e.try_gc_ref(store)?;
186 ValType::Ref(RefType::new(false, HeapType::Exn))
187 }
188 Val::ContRef(_) => {
189 return Err(crate::format_err!(
191 "continuation references not yet supported in embedder API"
192 ));
193 }
194 })
195 }
196
197 pub fn matches_ty(&self, store: impl AsContext, ty: &ValType) -> Result<bool> {
205 self._matches_ty(&store.as_context().0, ty)
206 }
207
208 pub(crate) fn _matches_ty(&self, store: &StoreOpaque, ty: &ValType) -> Result<bool> {
209 assert!(self.comes_from_same_store(store));
210 assert!(ty.comes_from_same_engine(store.engine()));
211 Ok(match (self, ty) {
212 (Val::I32(_), ValType::I32)
213 | (Val::I64(_), ValType::I64)
214 | (Val::F32(_), ValType::F32)
215 | (Val::F64(_), ValType::F64)
216 | (Val::V128(_), ValType::V128) => true,
217
218 (Val::FuncRef(f), ValType::Ref(ref_ty)) => Ref::from(*f)._matches_ty(store, ref_ty)?,
219 (Val::ExternRef(e), ValType::Ref(ref_ty)) => {
220 Ref::from(*e)._matches_ty(store, ref_ty)?
221 }
222 (Val::AnyRef(a), ValType::Ref(ref_ty)) => Ref::from(*a)._matches_ty(store, ref_ty)?,
223 (Val::ExnRef(e), ValType::Ref(ref_ty)) => Ref::from(*e)._matches_ty(store, ref_ty)?,
224
225 (Val::I32(_), _)
226 | (Val::I64(_), _)
227 | (Val::F32(_), _)
228 | (Val::F64(_), _)
229 | (Val::V128(_), _)
230 | (Val::FuncRef(_), _)
231 | (Val::ExternRef(_), _)
232 | (Val::AnyRef(_), _)
233 | (Val::ExnRef(_), _)
234 | (Val::ContRef(_), _) => false,
235 })
236 }
237
238 pub(crate) fn ensure_matches_ty(&self, store: &StoreOpaque, ty: &ValType) -> Result<()> {
239 if !self.comes_from_same_store(store) {
240 bail!("value used with wrong store")
241 }
242 if !ty.comes_from_same_engine(store.engine()) {
243 bail!("type used with wrong engine")
244 }
245 if self._matches_ty(store, ty)? {
246 Ok(())
247 } else {
248 let actual_ty = self.load_ty(store)?;
249 bail!("type mismatch: expected {ty}, found {actual_ty}")
250 }
251 }
252
253 pub fn to_raw(&self, mut store: impl AsContextMut) -> Result<ValRaw> {
264 let mut store = AutoAssertNoGc::new(store.as_context_mut().0);
265 self.to_raw_(&mut store)
266 }
267
268 pub(crate) fn to_raw_(&self, store: &mut AutoAssertNoGc) -> Result<ValRaw> {
269 match self {
270 Val::I32(i) => Ok(ValRaw::i32(*i)),
271 Val::I64(i) => Ok(ValRaw::i64(*i)),
272 Val::F32(u) => Ok(ValRaw::f32(*u)),
273 Val::F64(u) => Ok(ValRaw::f64(*u)),
274 Val::V128(b) => Ok(ValRaw::v128(b.as_u128())),
275 Val::ExternRef(e) => Ok(ValRaw::externref(match e {
276 None => 0,
277 Some(e) => e._to_raw(store)?,
278 })),
279 Val::AnyRef(e) => Ok(ValRaw::anyref(match e {
280 None => 0,
281 Some(e) => e._to_raw(store)?,
282 })),
283 Val::ExnRef(e) => Ok(ValRaw::exnref(match e {
284 None => 0,
285 Some(e) => e._to_raw(store)?,
286 })),
287 Val::FuncRef(f) => Ok(ValRaw::funcref(match f {
288 Some(f) => f.to_raw_(store),
289 None => ptr::null_mut(),
290 })),
291 Val::ContRef(_) => {
292 Err(crate::format_err!(
294 "continuation references not yet supported in to_raw conversion"
295 ))
296 }
297 }
298 }
299
300 pub unsafe fn from_raw(mut store: impl AsContextMut, raw: ValRaw, ty: ValType) -> Val {
308 let mut store = AutoAssertNoGc::new(store.as_context_mut().0);
309 unsafe { Self::_from_raw(&mut store, raw, &ty) }
311 }
312
313 pub(crate) unsafe fn _from_raw(
315 store: &mut AutoAssertNoGc<'_>,
316 raw: ValRaw,
317 ty: &ValType,
318 ) -> Val {
319 match ty {
320 ValType::I32 => Val::I32(raw.get_i32()),
321 ValType::I64 => Val::I64(raw.get_i64()),
322 ValType::F32 => Val::F32(raw.get_f32()),
323 ValType::F64 => Val::F64(raw.get_f64()),
324 ValType::V128 => Val::V128(raw.get_v128().into()),
325 ValType::Ref(ref_ty) => {
326 let ref_ = match ref_ty.heap_type() {
327 HeapType::Func | HeapType::ConcreteFunc(_) => unsafe {
330 Func::_from_raw(store, raw.get_funcref()).into()
331 },
332
333 HeapType::NoFunc => Ref::Func(None),
334
335 HeapType::NoCont | HeapType::ConcreteCont(_) | HeapType::Cont => {
336 unimplemented!()
338 }
339
340 HeapType::Extern => ExternRef::_from_raw(store, raw.get_externref()).into(),
341
342 HeapType::NoExtern => Ref::Extern(None),
343
344 HeapType::Any
345 | HeapType::Eq
346 | HeapType::I31
347 | HeapType::Array
348 | HeapType::ConcreteArray(_)
349 | HeapType::Struct
350 | HeapType::ConcreteStruct(_) => {
351 AnyRef::_from_raw(store, raw.get_anyref()).into()
352 }
353
354 HeapType::Exn => ExnRef::_from_raw(store, raw.get_exnref()).into(),
355 HeapType::NoExn => Ref::Exn(None),
356
357 HeapType::None => Ref::Any(None),
358 };
359 assert!(
360 ref_ty.is_nullable() || !ref_.is_null(),
361 "if the type is not nullable, we shouldn't get null; got \
362 type = {ref_ty}, ref = {ref_:?}"
363 );
364 ref_.into()
365 }
366 }
367 }
368
369 accessors! {
370 e
371 (I32(i32) i32 unwrap_i32 *e)
372 (I64(i64) i64 unwrap_i64 *e)
373 (F32(f32) f32 unwrap_f32 f32::from_bits(*e))
374 (F64(f64) f64 unwrap_f64 f64::from_bits(*e))
375 (FuncRef(Option<&Func>) func_ref unwrap_func_ref e.as_ref())
376 (ExternRef(Option<&Rooted<ExternRef>>) extern_ref unwrap_extern_ref e.as_ref())
377 (AnyRef(Option<&Rooted<AnyRef>>) any_ref unwrap_any_ref e.as_ref())
378 (V128(V128) v128 unwrap_v128 *e)
379 }
380
381 #[inline]
383 pub fn ref_(self) -> Option<Ref> {
384 match self {
385 Val::FuncRef(f) => Some(Ref::Func(f)),
386 Val::ExternRef(e) => Some(Ref::Extern(e)),
387 Val::AnyRef(a) => Some(Ref::Any(a)),
388 Val::ExnRef(e) => Some(Ref::Exn(e)),
389 Val::I32(_) | Val::I64(_) | Val::F32(_) | Val::F64(_) | Val::V128(_) => None,
390 Val::ContRef(_) => None, }
392 }
393
394 #[inline]
402 pub fn externref(&self) -> Option<Option<&Rooted<ExternRef>>> {
403 match self {
404 Val::ExternRef(None) => Some(None),
405 Val::ExternRef(Some(e)) => Some(Some(e)),
406 _ => None,
407 }
408 }
409
410 #[inline]
421 pub fn unwrap_externref(&self) -> Option<&Rooted<ExternRef>> {
422 self.externref().expect("expected externref")
423 }
424
425 #[inline]
433 pub fn anyref(&self) -> Option<Option<&Rooted<AnyRef>>> {
434 match self {
435 Val::AnyRef(None) => Some(None),
436 Val::AnyRef(Some(e)) => Some(Some(e)),
437 _ => None,
438 }
439 }
440
441 #[inline]
452 pub fn unwrap_anyref(&self) -> Option<&Rooted<AnyRef>> {
453 self.anyref().expect("expected anyref")
454 }
455
456 #[inline]
464 pub fn exnref(&self) -> Option<Option<&Rooted<ExnRef>>> {
465 match self {
466 Val::ExnRef(None) => Some(None),
467 Val::ExnRef(Some(e)) => Some(Some(e)),
468 _ => None,
469 }
470 }
471
472 #[inline]
483 pub fn unwrap_exnref(&self) -> Option<&Rooted<ExnRef>> {
484 self.exnref().expect("expected exnref")
485 }
486
487 #[inline]
495 pub fn funcref(&self) -> Option<Option<&Func>> {
496 match self {
497 Val::FuncRef(None) => Some(None),
498 Val::FuncRef(Some(f)) => Some(Some(f)),
499 _ => None,
500 }
501 }
502
503 #[inline]
514 pub fn unwrap_funcref(&self) -> Option<&Func> {
515 self.funcref().expect("expected funcref")
516 }
517
518 #[inline]
519 pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
520 match self {
521 Val::FuncRef(Some(f)) => f.comes_from_same_store(store),
522 Val::FuncRef(None) => true,
523
524 Val::ExternRef(Some(x)) => x.comes_from_same_store(store),
525 Val::ExternRef(None) => true,
526
527 Val::AnyRef(Some(a)) => a.comes_from_same_store(store),
528 Val::AnyRef(None) => true,
529
530 Val::ExnRef(Some(e)) => e.comes_from_same_store(store),
531 Val::ExnRef(None) => true,
532
533 Val::I32(_) | Val::I64(_) | Val::F32(_) | Val::F64(_) | Val::V128(_) => true,
537
538 Val::ContRef(_) => true, }
541 }
542}
543
544impl From<i32> for Val {
545 #[inline]
546 fn from(val: i32) -> Val {
547 Val::I32(val)
548 }
549}
550
551impl From<i64> for Val {
552 #[inline]
553 fn from(val: i64) -> Val {
554 Val::I64(val)
555 }
556}
557
558impl From<f32> for Val {
559 #[inline]
560 fn from(val: f32) -> Val {
561 Val::F32(val.to_bits())
562 }
563}
564
565impl From<f64> for Val {
566 #[inline]
567 fn from(val: f64) -> Val {
568 Val::F64(val.to_bits())
569 }
570}
571
572impl From<Ref> for Val {
573 #[inline]
574 fn from(val: Ref) -> Val {
575 match val {
576 Ref::Extern(e) => Val::ExternRef(e),
577 Ref::Func(f) => Val::FuncRef(f),
578 Ref::Any(a) => Val::AnyRef(a),
579 Ref::Exn(e) => Val::ExnRef(e),
580 }
581 }
582}
583
584impl From<Rooted<ExternRef>> for Val {
585 #[inline]
586 fn from(val: Rooted<ExternRef>) -> Val {
587 Val::ExternRef(Some(val))
588 }
589}
590
591impl From<Option<Rooted<ExternRef>>> for Val {
592 #[inline]
593 fn from(val: Option<Rooted<ExternRef>>) -> Val {
594 Val::ExternRef(val)
595 }
596}
597
598impl From<Rooted<AnyRef>> for Val {
599 #[inline]
600 fn from(val: Rooted<AnyRef>) -> Val {
601 Val::AnyRef(Some(val))
602 }
603}
604
605impl From<Option<Rooted<AnyRef>>> for Val {
606 #[inline]
607 fn from(val: Option<Rooted<AnyRef>>) -> Val {
608 Val::AnyRef(val)
609 }
610}
611
612impl From<Rooted<StructRef>> for Val {
613 #[inline]
614 fn from(val: Rooted<StructRef>) -> Val {
615 Val::AnyRef(Some(val.into()))
616 }
617}
618
619impl From<Option<Rooted<StructRef>>> for Val {
620 #[inline]
621 fn from(val: Option<Rooted<StructRef>>) -> Val {
622 Val::AnyRef(val.map(Into::into))
623 }
624}
625
626impl From<Rooted<ArrayRef>> for Val {
627 #[inline]
628 fn from(val: Rooted<ArrayRef>) -> Val {
629 Val::AnyRef(Some(val.into()))
630 }
631}
632
633impl From<Option<Rooted<ArrayRef>>> for Val {
634 #[inline]
635 fn from(val: Option<Rooted<ArrayRef>>) -> Val {
636 Val::AnyRef(val.map(Into::into))
637 }
638}
639
640impl From<Rooted<ExnRef>> for Val {
641 #[inline]
642 fn from(val: Rooted<ExnRef>) -> Val {
643 Val::ExnRef(Some(val))
644 }
645}
646
647impl From<Option<Rooted<ExnRef>>> for Val {
648 #[inline]
649 fn from(val: Option<Rooted<ExnRef>>) -> Val {
650 Val::ExnRef(val)
651 }
652}
653
654impl From<Func> for Val {
655 #[inline]
656 fn from(val: Func) -> Val {
657 Val::FuncRef(Some(val))
658 }
659}
660
661impl From<Option<Func>> for Val {
662 #[inline]
663 fn from(val: Option<Func>) -> Val {
664 Val::FuncRef(val)
665 }
666}
667
668impl From<u128> for Val {
669 #[inline]
670 fn from(val: u128) -> Val {
671 Val::V128(val.into())
672 }
673}
674
675impl From<V128> for Val {
676 #[inline]
677 fn from(val: V128) -> Val {
678 Val::V128(val)
679 }
680}
681
682#[derive(Debug, Clone)]
707pub enum Ref {
708 Func(Option<Func>),
745
746 Extern(Option<Rooted<ExternRef>>),
756
757 Any(Option<Rooted<AnyRef>>),
766
767 Exn(Option<Rooted<ExnRef>>),
774}
775
776impl From<Func> for Ref {
777 #[inline]
778 fn from(f: Func) -> Ref {
779 Ref::Func(Some(f))
780 }
781}
782
783impl From<Option<Func>> for Ref {
784 #[inline]
785 fn from(f: Option<Func>) -> Ref {
786 Ref::Func(f)
787 }
788}
789
790impl From<Rooted<ExternRef>> for Ref {
791 #[inline]
792 fn from(e: Rooted<ExternRef>) -> Ref {
793 Ref::Extern(Some(e))
794 }
795}
796
797impl From<Option<Rooted<ExternRef>>> for Ref {
798 #[inline]
799 fn from(e: Option<Rooted<ExternRef>>) -> Ref {
800 Ref::Extern(e)
801 }
802}
803
804impl From<Rooted<AnyRef>> for Ref {
805 #[inline]
806 fn from(e: Rooted<AnyRef>) -> Ref {
807 Ref::Any(Some(e))
808 }
809}
810
811impl From<Option<Rooted<AnyRef>>> for Ref {
812 #[inline]
813 fn from(e: Option<Rooted<AnyRef>>) -> Ref {
814 Ref::Any(e)
815 }
816}
817
818impl From<Rooted<StructRef>> for Ref {
819 #[inline]
820 fn from(e: Rooted<StructRef>) -> Ref {
821 Ref::Any(Some(e.into()))
822 }
823}
824
825impl From<Option<Rooted<StructRef>>> for Ref {
826 #[inline]
827 fn from(e: Option<Rooted<StructRef>>) -> Ref {
828 Ref::Any(e.map(Into::into))
829 }
830}
831
832impl From<Rooted<ArrayRef>> for Ref {
833 #[inline]
834 fn from(e: Rooted<ArrayRef>) -> Ref {
835 Ref::Any(Some(e.into()))
836 }
837}
838
839impl From<Option<Rooted<ArrayRef>>> for Ref {
840 #[inline]
841 fn from(e: Option<Rooted<ArrayRef>>) -> Ref {
842 Ref::Any(e.map(Into::into))
843 }
844}
845
846impl From<Rooted<ExnRef>> for Ref {
847 #[inline]
848 fn from(e: Rooted<ExnRef>) -> Ref {
849 Ref::Exn(Some(e))
850 }
851}
852
853impl From<Option<Rooted<ExnRef>>> for Ref {
854 #[inline]
855 fn from(e: Option<Rooted<ExnRef>>) -> Ref {
856 Ref::Exn(e)
857 }
858}
859
860impl Ref {
861 #[inline]
863 pub fn null(heap_type: &HeapType) -> Self {
864 match heap_type.top() {
865 HeapTopType::Any => Ref::Any(None),
866 HeapTopType::Extern => Ref::Extern(None),
867 HeapTopType::Func => Ref::Func(None),
868 HeapTopType::Exn => Ref::Exn(None),
869 HeapTopType::Cont => unimplemented!("embedding API for `(ref cont)`"),
870 }
871 }
872
873 #[inline]
875 pub fn is_null(&self) -> bool {
876 match self {
877 Ref::Any(None) | Ref::Extern(None) | Ref::Func(None) | Ref::Exn(None) => true,
878 Ref::Any(Some(_)) | Ref::Extern(Some(_)) | Ref::Func(Some(_)) | Ref::Exn(Some(_)) => {
879 false
880 }
881 }
882 }
883
884 #[inline]
886 pub fn is_non_null(&self) -> bool {
887 !self.is_null()
888 }
889
890 #[inline]
892 pub fn is_extern(&self) -> bool {
893 matches!(self, Ref::Extern(_))
894 }
895
896 #[inline]
905 pub fn as_extern(&self) -> Option<Option<&Rooted<ExternRef>>> {
906 match self {
907 Ref::Extern(e) => Some(e.as_ref()),
908 _ => None,
909 }
910 }
911
912 #[inline]
919 pub fn unwrap_extern(&self) -> Option<&Rooted<ExternRef>> {
920 self.as_extern()
921 .expect("Ref::unwrap_extern on non-extern reference")
922 }
923
924 #[inline]
926 pub fn is_any(&self) -> bool {
927 matches!(self, Ref::Any(_))
928 }
929
930 #[inline]
939 pub fn as_any(&self) -> Option<Option<&Rooted<AnyRef>>> {
940 match self {
941 Ref::Any(e) => Some(e.as_ref()),
942 _ => None,
943 }
944 }
945
946 #[inline]
953 pub fn unwrap_any(&self) -> Option<&Rooted<AnyRef>> {
954 self.as_any().expect("Ref::unwrap_any on non-any reference")
955 }
956
957 #[inline]
959 pub fn is_exn(&self) -> bool {
960 matches!(self, Ref::Exn(_))
961 }
962
963 #[inline]
972 pub fn as_exn(&self) -> Option<Option<&Rooted<ExnRef>>> {
973 match self {
974 Ref::Exn(e) => Some(e.as_ref()),
975 _ => None,
976 }
977 }
978
979 #[inline]
986 pub fn unwrap_exn(&self) -> Option<&Rooted<ExnRef>> {
987 self.as_exn().expect("Ref::unwrap_exn on non-exn reference")
988 }
989
990 #[inline]
992 pub fn is_func(&self) -> bool {
993 matches!(self, Ref::Func(_))
994 }
995
996 #[inline]
1005 pub fn as_func(&self) -> Option<Option<&Func>> {
1006 match self {
1007 Ref::Func(f) => Some(f.as_ref()),
1008 _ => None,
1009 }
1010 }
1011
1012 #[inline]
1019 pub fn unwrap_func(&self) -> Option<&Func> {
1020 self.as_func()
1021 .expect("Ref::unwrap_func on non-func reference")
1022 }
1023
1024 pub fn ty(&self, store: impl AsContext) -> Result<RefType> {
1034 self.load_ty(&store.as_context().0)
1035 }
1036
1037 pub(crate) fn load_ty(&self, store: &StoreOpaque) -> Result<RefType> {
1038 assert!(self.comes_from_same_store(store));
1039 Ok(RefType::new(
1040 self.is_null(),
1041 match self {
1045 Ref::Extern(None) => HeapType::NoExtern,
1046 Ref::Extern(Some(e)) => {
1047 e.try_gc_ref(store)?;
1048 HeapType::Extern
1049 }
1050
1051 Ref::Func(None) => HeapType::NoFunc,
1052 Ref::Func(Some(f)) => HeapType::ConcreteFunc(f.load_ty(store)),
1053
1054 Ref::Any(None) => HeapType::None,
1055 Ref::Any(Some(a)) => a._ty(store)?,
1056
1057 Ref::Exn(None) => HeapType::NoExn,
1058 Ref::Exn(Some(e)) => {
1059 e.try_gc_ref(store)?;
1060 HeapType::Exn
1061 }
1062 },
1063 ))
1064 }
1065
1066 pub fn matches_ty(&self, store: impl AsContext, ty: &RefType) -> Result<bool> {
1074 self._matches_ty(&store.as_context().0, ty)
1075 }
1076
1077 pub(crate) fn _matches_ty(&self, store: &StoreOpaque, ty: &RefType) -> Result<bool> {
1078 assert!(self.comes_from_same_store(store));
1079 assert!(ty.comes_from_same_engine(store.engine()));
1080 if self.is_null() && !ty.is_nullable() {
1081 return Ok(false);
1082 }
1083 Ok(match (self, ty.heap_type()) {
1084 (Ref::Extern(Some(e)), HeapType::Extern) => {
1085 e.try_gc_ref(store)?;
1086 true
1087 }
1088 (Ref::Extern(None), HeapType::Extern) => true,
1089 (Ref::Extern(None), HeapType::NoExtern) => true,
1090 (Ref::Extern(_), _) => false,
1091
1092 (Ref::Func(_), HeapType::Func) => true,
1093 (Ref::Func(None), HeapType::NoFunc | HeapType::ConcreteFunc(_)) => true,
1094 (Ref::Func(Some(f)), HeapType::ConcreteFunc(func_ty)) => f._matches_ty(store, func_ty),
1095 (Ref::Func(_), _) => false,
1096
1097 (Ref::Any(Some(a)), HeapType::Any) => {
1098 a.try_gc_ref(store)?;
1099 true
1100 }
1101 (Ref::Any(None), HeapType::Any) => true,
1102 (Ref::Any(Some(a)), HeapType::I31) => a._is_i31(store)?,
1103 (Ref::Any(Some(a)), HeapType::Struct) => a._is_struct(store)?,
1104 (Ref::Any(Some(a)), HeapType::ConcreteStruct(_ty)) => match a._as_struct(store)? {
1105 None => false,
1106 Some(s) => s._matches_ty(store, _ty)?,
1107 },
1108 (Ref::Any(Some(a)), HeapType::Eq) => a._is_eqref(store)?,
1109 (Ref::Any(Some(a)), HeapType::Array) => a._is_array(store)?,
1110 (Ref::Any(Some(a)), HeapType::ConcreteArray(_ty)) => match a._as_array(store)? {
1111 None => false,
1112 Some(a) => a._matches_ty(store, _ty)?,
1113 },
1114 (
1115 Ref::Any(None),
1116 HeapType::None
1117 | HeapType::I31
1118 | HeapType::ConcreteStruct(_)
1119 | HeapType::Struct
1120 | HeapType::ConcreteArray(_)
1121 | HeapType::Array
1122 | HeapType::Eq,
1123 ) => true,
1124 (Ref::Any(_), _) => false,
1125
1126 (Ref::Exn(Some(e)), HeapType::Exn) => {
1127 e.try_gc_ref(store)?;
1128 true
1129 }
1130 (Ref::Exn(None), HeapType::Exn) => true,
1131 (Ref::Exn(None), HeapType::NoExn) => true,
1132 (Ref::Exn(_), _) => false,
1133 })
1134 }
1135
1136 pub(crate) fn ensure_matches_ty(&self, store: &StoreOpaque, ty: &RefType) -> Result<()> {
1137 if !self.comes_from_same_store(store) {
1138 bail!("reference used with wrong store")
1139 }
1140 if !ty.comes_from_same_engine(store.engine()) {
1141 bail!("type used with wrong engine")
1142 }
1143 if self._matches_ty(store, ty)? {
1144 Ok(())
1145 } else {
1146 let actual_ty = self.load_ty(store)?;
1147 bail!("type mismatch: expected {ty}, found {actual_ty}")
1148 }
1149 }
1150
1151 pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
1152 match self {
1153 Ref::Func(Some(f)) => f.comes_from_same_store(store),
1154 Ref::Func(None) => true,
1155 Ref::Extern(Some(x)) => x.comes_from_same_store(store),
1156 Ref::Extern(None) => true,
1157 Ref::Any(Some(a)) => a.comes_from_same_store(store),
1158 Ref::Any(None) => true,
1159 Ref::Exn(Some(e)) => e.comes_from_same_store(store),
1160 Ref::Exn(None) => true,
1161 }
1162 }
1163}
1164
1165#[cfg(test)]
1166mod tests {
1167 use crate::*;
1168
1169 #[test]
1170 fn size_of_val() {
1171 let expected = if cfg!(target_arch = "x86_64")
1174 || cfg!(target_arch = "aarch64")
1175 || cfg!(target_arch = "s390x")
1176 || cfg!(target_arch = "riscv64")
1177 || cfg!(target_arch = "arm")
1178 {
1179 24
1180 } else if cfg!(target_arch = "x86") {
1181 20
1182 } else {
1183 panic!("unsupported architecture")
1184 };
1185 assert_eq!(std::mem::size_of::<Val>(), expected);
1186 }
1187
1188 #[test]
1189 fn size_of_ref() {
1190 let expected = if cfg!(target_arch = "x86_64")
1193 || cfg!(target_arch = "aarch64")
1194 || cfg!(target_arch = "s390x")
1195 || cfg!(target_arch = "riscv64")
1196 || cfg!(target_arch = "arm")
1197 {
1198 24
1199 } else if cfg!(target_arch = "x86") {
1200 20
1201 } else {
1202 panic!("unsupported architecture")
1203 };
1204 assert_eq!(std::mem::size_of::<Ref>(), expected);
1205 }
1206
1207 #[test]
1208 #[should_panic]
1209 fn val_matches_ty_wrong_engine() {
1210 let e1 = Engine::default();
1211 let e2 = Engine::default();
1212
1213 let t1 = FuncType::new(&e1, None, None);
1214 let t2 = FuncType::new(&e2, None, None);
1215
1216 let mut s1 = Store::new(&e1, ());
1217 let f = Func::new(&mut s1, t1.clone(), |_caller, _args, _results| Ok(()));
1218
1219 let _ = Val::FuncRef(Some(f)).matches_ty(
1221 &s1,
1222 &ValType::Ref(RefType::new(true, HeapType::ConcreteFunc(t2))),
1223 );
1224 }
1225
1226 #[test]
1227 #[should_panic]
1228 fn ref_matches_ty_wrong_engine() {
1229 let e1 = Engine::default();
1230 let e2 = Engine::default();
1231
1232 let t1 = FuncType::new(&e1, None, None);
1233 let t2 = FuncType::new(&e2, None, None);
1234
1235 let mut s1 = Store::new(&e1, ());
1236 let f = Func::new(&mut s1, t1.clone(), |_caller, _args, _results| Ok(()));
1237
1238 let _ = Ref::Func(Some(f)).matches_ty(&s1, &RefType::new(true, HeapType::ConcreteFunc(t2)));
1240 }
1241}