use crate::runtime::vm::VMGcRef;
use crate::store::{Asyncness, StoreId, StoreResourceLimiter};
#[cfg(feature = "async")]
use crate::vm::VMStore;
use crate::vm::{self, VMExnRef, VMGcHeader};
use crate::{
AsContext, AsContextMut, GcRefImpl, GcRootIndex, HeapType, OwnedRooted, RefType, Result,
Rooted, Val, ValRaw, ValType, WasmTy,
store::{AutoAssertNoGc, StoreOpaque},
};
use crate::{ExnType, FieldType, GcHeapOutOfMemory, StoreContextMut, Tag, prelude::*};
use core::mem;
use core::mem::MaybeUninit;
use wasmtime_environ::{GcLayout, GcStructLayout, VMGcKind, VMSharedTypeIndex};
pub struct ExnRefPre {
store_id: StoreId,
ty: ExnType,
}
impl ExnRefPre {
pub fn new(mut store: impl AsContextMut, ty: ExnType) -> Self {
Self::_new(store.as_context_mut().0, ty)
}
pub(crate) fn _new(store: &mut StoreOpaque, ty: ExnType) -> Self {
store.insert_gc_host_alloc_type(ty.registered_type().clone());
let store_id = store.id();
ExnRefPre { store_id, ty }
}
pub(crate) fn layout(&self) -> &GcStructLayout {
self.ty
.registered_type()
.layout()
.expect("exn types have a layout")
.unwrap_struct()
}
pub(crate) fn type_index(&self) -> VMSharedTypeIndex {
self.ty.registered_type().index()
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct ExnRef {
pub(super) inner: GcRootIndex,
}
unsafe impl GcRefImpl for ExnRef {
fn transmute_ref(index: &GcRootIndex) -> &Self {
let me: &Self = unsafe { mem::transmute(index) };
assert!(matches!(
me,
Self {
inner: GcRootIndex { .. },
}
));
me
}
}
impl ExnRef {
pub fn from_raw(mut store: impl AsContextMut, raw: u32) -> Option<Rooted<Self>> {
let mut store = AutoAssertNoGc::new(store.as_context_mut().0);
Self::_from_raw(&mut store, raw)
}
pub(crate) fn _from_raw(store: &mut AutoAssertNoGc, raw: u32) -> Option<Rooted<Self>> {
let gc_ref = VMGcRef::from_raw_u32(raw)?;
let gc_ref = store.clone_gc_ref(&gc_ref);
Some(Self::from_cloned_gc_ref(store, gc_ref))
}
pub fn new(
mut store: impl AsContextMut,
allocator: &ExnRefPre,
tag: &Tag,
fields: &[Val],
) -> Result<Rooted<ExnRef>> {
let (mut limiter, store) = store
.as_context_mut()
.0
.validate_sync_resource_limiter_and_store_opaque()?;
vm::assert_ready(Self::_new_async(
store,
limiter.as_mut(),
allocator,
tag,
fields,
Asyncness::No,
))
}
#[cfg(feature = "async")]
pub async fn new_async(
mut store: impl AsContextMut,
allocator: &ExnRefPre,
tag: &Tag,
fields: &[Val],
) -> Result<Rooted<ExnRef>> {
let (mut limiter, store) = store.as_context_mut().0.resource_limiter_and_store_opaque();
Self::_new_async(
store,
limiter.as_mut(),
allocator,
tag,
fields,
Asyncness::Yes,
)
.await
}
pub(crate) async fn _new_async(
store: &mut StoreOpaque,
limiter: Option<&mut StoreResourceLimiter<'_>>,
allocator: &ExnRefPre,
tag: &Tag,
fields: &[Val],
asyncness: Asyncness,
) -> Result<Rooted<ExnRef>> {
Self::type_check_tag_and_fields(store, allocator, tag, fields)?;
store
.retry_after_gc_async(limiter, (), asyncness, |store, ()| {
Self::new_unchecked(store, allocator, tag, fields)
})
.await
}
fn type_check_tag_and_fields(
store: &mut StoreOpaque,
allocator: &ExnRefPre,
tag: &Tag,
fields: &[Val],
) -> Result<(), Error> {
assert!(
tag.comes_from_same_store(store),
"tag comes from the wrong store"
);
ensure!(
tag.wasmtime_ty(store).signature.unwrap_engine_type_index()
== allocator.ty.tag_type().ty().type_index(),
"incorrect signature for tag when creating exception object"
);
let expected_len = allocator.ty.fields().len();
let actual_len = fields.len();
ensure!(
actual_len == expected_len,
"expected {expected_len} fields, got {actual_len}"
);
for (ty, val) in allocator.ty.fields().zip(fields) {
assert!(
val.comes_from_same_store(store),
"field value comes from the wrong store",
);
let ty = ty.element_type().unpack();
val.ensure_matches_ty(store, ty)
.context("field type mismatch")?;
}
Ok(())
}
fn new_unchecked(
store: &mut StoreOpaque,
allocator: &ExnRefPre,
tag: &Tag,
fields: &[Val],
) -> Result<Rooted<ExnRef>> {
assert_eq!(
store.id(),
allocator.store_id,
"attempted to use a `ExnRefPre` with the wrong store"
);
let exnref = store
.require_gc_store_mut()?
.alloc_uninit_exn(allocator.type_index(), &allocator.layout())
.context("unrecoverable error when allocating new `exnref`")?
.map_err(|n| GcHeapOutOfMemory::new((), n))?;
let mut store = AutoAssertNoGc::new(store);
match (|| {
let (instance, index) = tag.to_raw_indices();
exnref.initialize_tag(&mut store, instance, index)?;
for (index, (ty, val)) in allocator.ty.fields().zip(fields).enumerate() {
exnref.initialize_field(
&mut store,
allocator.layout(),
ty.element_type(),
index,
*val,
)?;
}
Ok(())
})() {
Ok(()) => Ok(Rooted::new(&mut store, exnref.into())),
Err(e) => {
store.require_gc_store_mut()?.dealloc_uninit_exn(exnref);
Err(e)
}
}
}
pub(crate) fn type_index(&self, store: &StoreOpaque) -> Result<VMSharedTypeIndex> {
let gc_ref = self.inner.try_gc_ref(store)?;
let header = store.require_gc_store()?.header(gc_ref);
debug_assert!(header.kind().matches(VMGcKind::ExnRef));
Ok(header.ty().expect("exnrefs should have concrete types"))
}
pub(crate) fn from_cloned_gc_ref(
store: &mut AutoAssertNoGc<'_>,
gc_ref: VMGcRef,
) -> Rooted<Self> {
debug_assert!(
store
.unwrap_gc_store()
.header(&gc_ref)
.kind()
.matches(VMGcKind::ExnRef)
);
Rooted::new(store, gc_ref)
}
#[inline]
pub(crate) fn comes_from_same_store(&self, store: &StoreOpaque) -> bool {
self.inner.comes_from_same_store(store)
}
pub fn to_raw(&self, mut store: impl AsContextMut) -> Result<u32> {
let mut store = AutoAssertNoGc::new(store.as_context_mut().0);
self._to_raw(&mut store)
}
pub(crate) fn _to_raw(&self, store: &mut AutoAssertNoGc<'_>) -> Result<u32> {
let gc_ref = self.inner.try_clone_gc_ref(store)?;
let raw = if gc_ref.is_i31() {
gc_ref.as_raw_non_zero_u32()
} else {
store.require_gc_store_mut()?.expose_gc_ref_to_wasm(gc_ref)
};
Ok(raw.get())
}
pub fn ty(&self, store: impl AsContext) -> Result<ExnType> {
self._ty(store.as_context().0)
}
pub(crate) fn _ty(&self, store: &StoreOpaque) -> Result<ExnType> {
assert!(self.comes_from_same_store(store));
let index = self.type_index(store)?;
Ok(ExnType::from_shared_type_index(store.engine(), index))
}
pub fn matches_ty(&self, store: impl AsContext, ty: &HeapType) -> Result<bool> {
self._matches_ty(store.as_context().0, ty)
}
pub(crate) fn _matches_ty(&self, store: &StoreOpaque, ty: &HeapType) -> Result<bool> {
assert!(self.comes_from_same_store(store));
Ok(HeapType::from(self._ty(store)?).matches(ty))
}
pub(crate) fn ensure_matches_ty(&self, store: &StoreOpaque, ty: &HeapType) -> Result<()> {
if !self.comes_from_same_store(store) {
bail!("function used with wrong store");
}
if self._matches_ty(store, ty)? {
Ok(())
} else {
let actual_ty = self._ty(store)?;
bail!("type mismatch: expected `(ref {ty})`, found `(ref {actual_ty})`")
}
}
pub fn fields<'a, T: 'static>(
&'a self,
store: impl Into<StoreContextMut<'a, T>>,
) -> Result<impl ExactSizeIterator<Item = Val> + 'a> {
self._fields(store.into().0)
}
pub(crate) fn _fields<'a>(
&'a self,
store: &'a mut StoreOpaque,
) -> Result<impl ExactSizeIterator<Item = Val> + 'a> {
assert!(self.comes_from_same_store(store));
let store = AutoAssertNoGc::new(store);
let gc_ref = self.inner.try_gc_ref(&store)?;
let header = store.require_gc_store()?.header(gc_ref);
debug_assert!(header.kind().matches(VMGcKind::ExnRef));
let index = header.ty().expect("exnrefs should have concrete types");
let ty = ExnType::from_shared_type_index(store.engine(), index);
let len = ty.fields().len();
return Ok(Fields {
exnref: self,
store,
index: 0,
len,
});
struct Fields<'a, 'b> {
exnref: &'a ExnRef,
store: AutoAssertNoGc<'b>,
index: usize,
len: usize,
}
impl Iterator for Fields<'_, '_> {
type Item = Val;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let i = self.index;
debug_assert!(i <= self.len);
if i >= self.len {
return None;
}
self.index += 1;
Some(self.exnref._field(&mut self.store, i).unwrap())
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len - self.index;
(len, Some(len))
}
}
impl ExactSizeIterator for Fields<'_, '_> {
#[inline]
fn len(&self) -> usize {
self.len - self.index
}
}
}
fn header<'a>(&self, store: &'a AutoAssertNoGc<'_>) -> Result<&'a VMGcHeader> {
assert!(self.comes_from_same_store(&store));
let gc_ref = self.inner.try_gc_ref(store)?;
Ok(store.require_gc_store()?.header(gc_ref))
}
fn exnref<'a>(&self, store: &'a AutoAssertNoGc<'_>) -> Result<&'a VMExnRef> {
assert!(self.comes_from_same_store(&store));
let gc_ref = self.inner.try_gc_ref(store)?;
debug_assert!(self.header(store)?.kind().matches(VMGcKind::ExnRef));
Ok(gc_ref.as_exnref_unchecked())
}
fn layout(&self, store: &AutoAssertNoGc<'_>) -> Result<GcStructLayout> {
assert!(self.comes_from_same_store(&store));
let type_index = self.type_index(store)?;
let layout = store
.engine()
.signatures()
.layout(type_index)
.expect("exn types should have GC layouts");
match layout {
GcLayout::Struct(s) => Ok(s),
GcLayout::Array(_) => unreachable!(),
}
}
fn field_ty(&self, store: &StoreOpaque, field: usize) -> Result<FieldType> {
let ty = self._ty(store)?;
match ty.field(field) {
Some(f) => Ok(f),
None => {
let len = ty.fields().len();
bail!("cannot access field {field}: exn only has {len} fields")
}
}
}
pub fn field(&self, mut store: impl AsContextMut, index: usize) -> Result<Val> {
let mut store = AutoAssertNoGc::new(store.as_context_mut().0);
self._field(&mut store, index)
}
pub(crate) fn _field(&self, store: &mut AutoAssertNoGc<'_>, index: usize) -> Result<Val> {
assert!(self.comes_from_same_store(store));
let exnref = self.exnref(store)?.unchecked_copy();
let field_ty = self.field_ty(store, index)?;
let layout = self.layout(store)?;
Ok(exnref.read_field(store, &layout, field_ty.element_type(), index))
}
pub fn tag(&self, mut store: impl AsContextMut) -> Result<Tag> {
let mut store = AutoAssertNoGc::new(store.as_context_mut().0);
assert!(self.comes_from_same_store(&store));
let exnref = self.exnref(&store)?.unchecked_copy();
let (instance, index) = exnref.tag(&mut store)?;
Ok(Tag::from_raw_indices(&*store, instance, index))
}
}
unsafe impl WasmTy for Rooted<ExnRef> {
#[inline]
fn valtype() -> ValType {
ValType::Ref(RefType::new(false, HeapType::Exn))
}
#[inline]
fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
self.comes_from_same_store(store)
}
#[inline]
fn dynamic_concrete_type_check(
&self,
_store: &StoreOpaque,
_nullable: bool,
_ty: &HeapType,
) -> Result<()> {
Ok(())
}
fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
self.wasm_ty_store(store, ptr, ValRaw::anyref)
}
unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self {
Self::wasm_ty_load(store, ptr.get_anyref(), ExnRef::from_cloned_gc_ref)
}
}
unsafe impl WasmTy for Option<Rooted<ExnRef>> {
#[inline]
fn valtype() -> ValType {
ValType::EXNREF
}
#[inline]
fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
self.map_or(true, |x| x.comes_from_same_store(store))
}
#[inline]
fn dynamic_concrete_type_check(
&self,
store: &StoreOpaque,
nullable: bool,
ty: &HeapType,
) -> Result<()> {
match self {
Some(a) => a.ensure_matches_ty(store, ty),
None => {
ensure!(
nullable,
"expected a non-null reference, but found a null reference"
);
Ok(())
}
}
}
#[inline]
fn is_vmgcref_and_points_to_object(&self) -> bool {
self.is_some()
}
fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
<Rooted<ExnRef>>::wasm_ty_option_store(self, store, ptr, ValRaw::anyref)
}
unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self {
<Rooted<ExnRef>>::wasm_ty_option_load(store, ptr.get_anyref(), ExnRef::from_cloned_gc_ref)
}
}
unsafe impl WasmTy for OwnedRooted<ExnRef> {
#[inline]
fn valtype() -> ValType {
ValType::Ref(RefType::new(false, HeapType::Exn))
}
#[inline]
fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
self.comes_from_same_store(store)
}
#[inline]
fn dynamic_concrete_type_check(
&self,
store: &StoreOpaque,
_nullable: bool,
ty: &HeapType,
) -> Result<()> {
self.ensure_matches_ty(store, ty)
}
fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
self.wasm_ty_store(store, ptr, ValRaw::anyref)
}
unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self {
Self::wasm_ty_load(store, ptr.get_anyref(), ExnRef::from_cloned_gc_ref)
}
}
unsafe impl WasmTy for Option<OwnedRooted<ExnRef>> {
#[inline]
fn valtype() -> ValType {
ValType::EXNREF
}
#[inline]
fn compatible_with_store(&self, store: &StoreOpaque) -> bool {
self.as_ref()
.map_or(true, |x| x.comes_from_same_store(store))
}
#[inline]
fn dynamic_concrete_type_check(
&self,
store: &StoreOpaque,
nullable: bool,
ty: &HeapType,
) -> Result<()> {
match self {
Some(a) => a.ensure_matches_ty(store, ty),
None => {
ensure!(
nullable,
"expected a non-null reference, but found a null reference"
);
Ok(())
}
}
}
#[inline]
fn is_vmgcref_and_points_to_object(&self) -> bool {
self.is_some()
}
fn store(self, store: &mut AutoAssertNoGc<'_>, ptr: &mut MaybeUninit<ValRaw>) -> Result<()> {
<OwnedRooted<ExnRef>>::wasm_ty_option_store(self, store, ptr, ValRaw::anyref)
}
unsafe fn load(store: &mut AutoAssertNoGc<'_>, ptr: &ValRaw) -> Self {
<OwnedRooted<ExnRef>>::wasm_ty_option_load(
store,
ptr.get_anyref(),
ExnRef::from_cloned_gc_ref,
)
}
}