Skip to content

Commit

Permalink
Cleanup maybeAddGcPassForTest more
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Sep 20, 2023
1 parent 2c2648a commit 8b2c4fb
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/workerd/io/worker-entrypoint.c++
Original file line number Diff line number Diff line change
Expand Up @@ -507,36 +507,34 @@ kj::Promise<WorkerInterface::CustomEvent::Result>
return maybeAddGcPassForTest(context, kj::mv(promise));
}

template <typename T>
kj::Promise<T> WorkerEntrypoint::maybeAddGcPassForTest(
IoContext& context, kj::Promise<T> promise) {
#ifdef KJ_DEBUG
kj::Maybe<kj::Own<const Worker>> worker;
if (isPredictableModeForTest()) {
worker = kj::atomicAddRef(context.getWorker());
}

static auto constexpr maybeRequestGc = [](auto& worker) {
if (isPredictableModeForTest()) {
jsg::V8StackScope stackScope;
auto lock = KJ_ASSERT_NONNULL(worker)->getIsolate().getApiIsolate().lock(stackScope);
lock->requestGcForTesting();
}
};
#endif // KJ_DEBUG
namespace {
void requestGc(const Worker& worker) {
jsg::V8StackScope stackScope;
auto lock = worker.getIsolate().getApiIsolate().lock(stackScope);
lock->requestGcForTesting();
}

template <typename T>
kj::Promise<T> addGcPassForTest(IoContext& context, kj::Promise<T> promise) {
auto worker = kj::atomicAddRef(context.getWorker());
if constexpr (kj::isSameType<T, void>()) {
co_await promise;
#ifdef KJ_DEBUG
maybeRequestGc(worker);
#endif // KJ_DEBUG
requestGc(*worker);
} else {
auto ret = co_await promise;
#ifdef KJ_DEBUG
maybeRequestGc(worker);
#endif // KJ_DEBUG
requestGc(*worker);
co_return kj::mv(ret);
}
}
} // namespace

template <typename T>
kj::Promise<T> WorkerEntrypoint::maybeAddGcPassForTest(
IoContext& context, kj::Promise<T> promise) {
if (isPredictableModeForTest()) {
return addGcPassForTest(context, kj::mv(promise));
}
return kj::mv(promise);
}

} // namespace workerd

0 comments on commit 8b2c4fb

Please sign in to comment.