Skip to content

Commit 4abb213

Browse files
authored
Deduplicate some more GC functions (#11480)
Use the `async` version from sync API entrypoints to deduplicate the implementation.
1 parent e1f50aa commit 4abb213

File tree

5 files changed

+18
-129
lines changed

5 files changed

+18
-129
lines changed

crates/wasmtime/src/runtime/gc/enabled/arrayref.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::runtime::vm::VMGcRef;
44
use crate::store::StoreId;
5-
use crate::vm::{VMArrayRef, VMGcHeader};
5+
use crate::vm::{self, VMArrayRef, VMGcHeader};
66
use crate::{AnyRef, FieldType};
77
use crate::{
88
ArrayType, AsContext, AsContextMut, EqRef, GcHeapOutOfMemory, GcRefImpl, GcRootIndex, HeapType,
@@ -297,18 +297,9 @@ impl ArrayRef {
297297
elem: &Val,
298298
len: u32,
299299
) -> Result<Rooted<ArrayRef>> {
300-
Self::_new(store.as_context_mut().0, allocator, elem, len)
301-
}
302-
303-
pub(crate) fn _new(
304-
store: &mut StoreOpaque,
305-
allocator: &ArrayRefPre,
306-
elem: &Val,
307-
len: u32,
308-
) -> Result<Rooted<ArrayRef>> {
309-
store.retry_after_gc((), |store, ()| {
310-
Self::new_from_iter(store, allocator, RepeatN(elem, len))
311-
})
300+
let store = store.as_context_mut().0;
301+
assert!(!store.async_support());
302+
vm::assert_ready(Self::_new_async(store, allocator, elem, len))
312303
}
313304

314305
/// Asynchronously allocate a new `array` of the given length, with every
@@ -454,17 +445,9 @@ impl ArrayRef {
454445
allocator: &ArrayRefPre,
455446
elems: &[Val],
456447
) -> Result<Rooted<ArrayRef>> {
457-
Self::_new_fixed(store.as_context_mut().0, allocator, elems)
458-
}
459-
460-
pub(crate) fn _new_fixed(
461-
store: &mut StoreOpaque,
462-
allocator: &ArrayRefPre,
463-
elems: &[Val],
464-
) -> Result<Rooted<ArrayRef>> {
465-
store.retry_after_gc((), |store, ()| {
466-
Self::new_from_iter(store, allocator, elems.iter())
467-
})
448+
let store = store.as_context_mut().0;
449+
assert!(!store.async_support());
450+
vm::assert_ready(Self::_new_fixed_async(store, allocator, elems))
468451
}
469452

470453
/// Asynchronously allocate a new `array` containing the given elements.

crates/wasmtime/src/runtime/gc/enabled/exnref.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::runtime::vm::VMGcRef;
44
use crate::store::StoreId;
5-
use crate::vm::{VMExnRef, VMGcHeader};
5+
use crate::vm::{self, VMExnRef, VMGcHeader};
66
use crate::{
77
AsContext, AsContextMut, GcRefImpl, GcRootIndex, HeapType, ManuallyRooted, RefType, Result,
88
Rooted, Val, ValRaw, ValType, WasmTy,
@@ -205,23 +205,9 @@ impl ExnRef {
205205
tag: &Tag,
206206
fields: &[Val],
207207
) -> Result<Rooted<ExnRef>> {
208-
Self::_new(store.as_context_mut().0, allocator, tag, fields)
209-
}
210-
211-
pub(crate) fn _new(
212-
store: &mut StoreOpaque,
213-
allocator: &ExnRefPre,
214-
tag: &Tag,
215-
fields: &[Val],
216-
) -> Result<Rooted<ExnRef>> {
217-
assert!(
218-
!store.async_support(),
219-
"use `ExnRef::new_async` with asynchronous stores"
220-
);
221-
Self::type_check_tag_and_fields(store, allocator, tag, fields)?;
222-
store.retry_after_gc((), |store, ()| {
223-
Self::new_unchecked(store, allocator, tag, fields)
224-
})
208+
let store = store.as_context_mut().0;
209+
assert!(!store.async_support());
210+
vm::assert_ready(Self::_new_async(store, allocator, tag, fields))
225211
}
226212

227213
/// Asynchronously allocate a new exception object and get a
@@ -262,17 +248,12 @@ impl ExnRef {
262248
Self::_new_async(store.as_context_mut().0, allocator, tag, fields).await
263249
}
264250

265-
#[cfg(feature = "async")]
266251
pub(crate) async fn _new_async(
267252
store: &mut StoreOpaque,
268253
allocator: &ExnRefPre,
269254
tag: &Tag,
270255
fields: &[Val],
271256
) -> Result<Rooted<ExnRef>> {
272-
assert!(
273-
store.async_support(),
274-
"use `ExnRef::new` with synchronous stores"
275-
);
276257
Self::type_check_tag_and_fields(store, allocator, tag, fields)?;
277258
store
278259
.retry_after_gc_async((), |store, ()| {

crates/wasmtime/src/runtime/gc/enabled/externref.rs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::{AnyRef, RootedGcRefImpl};
44
use crate::prelude::*;
5-
use crate::runtime::vm::VMGcRef;
5+
use crate::runtime::vm::{self, VMGcRef};
66
use crate::{
77
AsContextMut, GcHeapOutOfMemory, GcRefImpl, GcRootIndex, HeapType, ManuallyRooted, RefType,
88
Result, Rooted, StoreContext, StoreContextMut, ValRaw, ValType, WasmTy,
@@ -215,36 +215,8 @@ impl ExternRef {
215215
T: 'static + Any + Send + Sync,
216216
{
217217
let ctx = context.as_context_mut().0;
218-
Self::_new(ctx, value)
219-
}
220-
221-
pub(crate) fn _new<T>(store: &mut StoreOpaque, value: T) -> Result<Rooted<ExternRef>>
222-
where
223-
T: 'static + Any + Send + Sync,
224-
{
225-
// Allocate the box once, regardless how many gc-and-retry attempts we
226-
// make.
227-
let value: Box<dyn Any + Send + Sync> = Box::new(value);
228-
229-
let gc_ref = store
230-
.retry_after_gc(value, |store, value| {
231-
store
232-
.require_gc_store_mut()?
233-
.alloc_externref(value)
234-
.context("unrecoverable error when allocating new `externref`")?
235-
.map_err(|(x, n)| GcHeapOutOfMemory::new(x, n).into())
236-
})
237-
// Translate the `GcHeapOutOfMemory`'s inner value from the boxed
238-
// trait object into `T`.
239-
.map_err(
240-
|e| match e.downcast::<GcHeapOutOfMemory<Box<dyn Any + Send + Sync>>>() {
241-
Ok(oom) => oom.map_inner(|x| *x.downcast::<T>().unwrap()).into(),
242-
Err(e) => e,
243-
},
244-
)?;
245-
246-
let mut ctx = AutoAssertNoGc::new(store);
247-
Ok(Self::from_cloned_gc_ref(&mut ctx, gc_ref.into()))
218+
assert!(!ctx.async_support());
219+
vm::assert_ready(Self::_new_async(ctx, value))
248220
}
249221

250222
/// Asynchronously allocates a new `ExternRef` wrapping the given value.
@@ -331,7 +303,6 @@ impl ExternRef {
331303
Self::_new_async(ctx, value).await
332304
}
333305

334-
#[cfg(feature = "async")]
335306
pub(crate) async fn _new_async<T>(
336307
store: &mut StoreOpaque,
337308
value: T,

crates/wasmtime/src/runtime/gc/enabled/structref.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::runtime::vm::VMGcRef;
44
use crate::store::StoreId;
5-
use crate::vm::{VMGcHeader, VMStructRef};
5+
use crate::vm::{self, VMGcHeader, VMStructRef};
66
use crate::{AnyRef, FieldType};
77
use crate::{
88
AsContext, AsContextMut, EqRef, GcHeapOutOfMemory, GcRefImpl, GcRootIndex, HeapType,
@@ -231,22 +231,9 @@ impl StructRef {
231231
allocator: &StructRefPre,
232232
fields: &[Val],
233233
) -> Result<Rooted<StructRef>> {
234-
Self::_new(store.as_context_mut().0, allocator, fields)
235-
}
236-
237-
pub(crate) fn _new(
238-
store: &mut StoreOpaque,
239-
allocator: &StructRefPre,
240-
fields: &[Val],
241-
) -> Result<Rooted<StructRef>> {
242-
assert!(
243-
!store.async_support(),
244-
"use `StructRef::new_async` with asynchronous stores"
245-
);
246-
Self::type_check_fields(store, allocator, fields)?;
247-
store.retry_after_gc((), |store, ()| {
248-
Self::new_unchecked(store, allocator, fields)
249-
})
234+
let store = store.as_context_mut().0;
235+
assert!(!store.async_support());
236+
vm::assert_ready(Self::_new_async(store, allocator, fields))
250237
}
251238

252239
/// Asynchronously allocate a new `struct` and get a reference to it.

crates/wasmtime/src/runtime/store/gc.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -165,39 +165,6 @@ impl StoreOpaque {
165165
}
166166
}
167167

168-
/// Attempt an allocation, if it fails due to GC OOM, then do a GC and
169-
/// retry.
170-
pub(crate) fn retry_after_gc<T, U>(
171-
&mut self,
172-
value: T,
173-
alloc_func: impl Fn(&mut Self, T) -> Result<U>,
174-
) -> Result<U>
175-
where
176-
T: Send + Sync + 'static,
177-
{
178-
assert!(
179-
!self.async_support(),
180-
"use the `*_async` versions of methods when async is configured"
181-
);
182-
vm::assert_ready(self.ensure_gc_store())?;
183-
match alloc_func(self, value) {
184-
Ok(x) => Ok(x),
185-
Err(e) => match e.downcast::<crate::GcHeapOutOfMemory<T>>() {
186-
Ok(oom) => {
187-
let (value, oom) = oom.take_inner();
188-
// SAFETY: FIXME(#11409)
189-
unsafe {
190-
vm::assert_ready(
191-
self.gc_unsafe_get_limiter(None, Some(oom.bytes_needed())),
192-
);
193-
}
194-
alloc_func(self, value)
195-
}
196-
Err(e) => Err(e),
197-
},
198-
}
199-
}
200-
201168
/// Attempt an allocation, if it fails due to GC OOM, then do a GC and
202169
/// retry.
203170
pub(crate) async fn retry_after_gc_async<T, U>(

0 commit comments

Comments
 (0)