Skip to content

Commit

Permalink
fixup! Address feedback and refine implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Jan 4, 2023
1 parent 24b4169 commit 4f20040
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/workerd/api/global-scope.c++
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(v8::Isolate* isolate)
v8::PromiseRejectEvent event,
jsg::V8Ref<v8::Promise> promise,
jsg::Value value) {
jsg::AsyncContextFrame::Scope scope(js,
KJ_ASSERT_NONNULL(jsg::AsyncContextFrame::tryUnwrap(js, promise)));
// If async context tracking is enabled, then we need to ensure that we enter the frame
// associated with the promise before we invoke the unhandled rejection callback handling.
kj::Maybe<jsg::AsyncContextFrame::Scope> maybeScope;
if (js.isAsyncContextTrackingEnabled()) {
maybeScope.emplace(js,
KJ_ASSERT_NONNULL(jsg::AsyncContextFrame::tryUnwrap(js, promise)));
}
auto ev = jsg::alloc<PromiseRejectionEvent>(event, kj::mv(promise), kj::mv(value));
dispatchEventImpl(js, kj::mv(ev));
}) {}
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/jsg/jsg.c++
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ void Lock::setAsyncContextTrackingEnabled() {
IsolateBase::from(v8Isolate).setAsyncContextTrackingEnabled();
}

bool Lock::isAsyncContextTrackingEnabled() {
return IsolateBase::from(v8Isolate).isAsyncContextTrackingEnabled();
}

void Lock::setCommonJsExportDefault(bool exportDefault) {
IsolateBase::from(v8Isolate).setCommonJsExportDefault({}, exportDefault);
}
Expand Down
1 change: 1 addition & 0 deletions src/workerd/jsg/jsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,7 @@ class Lock {
void setLoggerCallback(kj::Function<Logger>&& logger);

void setAsyncContextTrackingEnabled();
bool isAsyncContextTrackingEnabled();

// ---------------------------------------------------------------------------
// Misc. Stuff
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/jsg/setup.c++
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ v8::ModifyCodeGenerationFromStringsResult IsolateBase::modifyCodeGenCallback(
}

void IsolateBase::setAsyncContextTrackingEnabled() {
if (asyncContextTrackingEnabled) return;
asyncContextTrackingEnabled = true;
ptr->SetPromiseHook(&promiseHook);
}

Expand Down
2 changes: 2 additions & 0 deletions src/workerd/jsg/setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class IsolateBase {
}

void setAsyncContextTrackingEnabled();
bool isAsyncContextTrackingEnabled() { return asyncContextTrackingEnabled; }

private:
template <typename TypeWrapper>
Expand Down Expand Up @@ -141,6 +142,7 @@ class IsolateBase {
// and there are a number of async APIs that currently throw. When the captureThrowsAsRejections
// flag is set, that old behavior is changed to be correct.
bool exportCommonJsDefault = false;
bool asyncContextTrackingEnabled = false;

kj::Maybe<kj::Function<Logger>> maybeLogger;

Expand Down

0 comments on commit 4f20040

Please sign in to comment.