Skip to content

Commit b9323ab

Browse files
committed
Make SetContext and GetContext as symmetric as possible.
1 parent 7df6524 commit b9323ab

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

bindings/profilers/wall.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,14 +1037,17 @@ void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
10371037
// No Node AsyncContextFrame in this continuation yet
10381038
if (!cped->IsObject()) return;
10391039

1040+
auto v8Ctx = isolate->GetCurrentContext();
1041+
// This should always be called from a V8 context, but check just in case.
1042+
if (v8Ctx.IsEmpty()) return;
1043+
10401044
auto cpedObj = cped.As<Object>();
10411045
auto localSymbol = cpedSymbol_.Get(isolate);
1042-
auto v8Ctx = isolate->GetCurrentContext();
10431046
auto maybeProfData = cpedObj->Get(v8Ctx, localSymbol);
10441047
if (maybeProfData.IsEmpty()) return;
1045-
auto profData = maybeProfData.ToLocalChecked();
10461048

10471049
PersistentContextPtr* contextPtr = nullptr;
1050+
auto profData = maybeProfData.ToLocalChecked();
10481051
if (profData->IsUndefined()) {
10491052
contextPtr = new PersistentContextPtr();
10501053

@@ -1091,19 +1094,26 @@ ContextPtr WallProfiler::GetContextPtr(Isolate* isolate) {
10911094
return curContext_.Get();
10921095
}
10931096

1097+
#define RETURN_EMPTY_IF(x) if (x) return std::shared_ptr<Global<Value>>()
1098+
10941099
auto cped = isolate->GetContinuationPreservedEmbedderData();
1095-
if (!cped->IsObject()) return std::shared_ptr<Global<Value>>();
1100+
RETURN_EMPTY_IF(!cped->IsObject());
1101+
1102+
auto v8Ctx = isolate->GetCurrentContext();
1103+
RETURN_EMPTY_IF(v8Ctx.IsEmpty());
10961104

10971105
auto cpedObj = cped.As<Object>();
10981106
auto localSymbol = cpedSymbol_.Get(isolate);
1099-
auto maybeProfData = cpedObj->Get(isolate->GetCurrentContext(), localSymbol);
1100-
if (maybeProfData.IsEmpty()) return std::shared_ptr<Global<Value>>();
1101-
auto profData = maybeProfData.ToLocalChecked();
1107+
auto maybeProfData = cpedObj->Get(v8Ctx, localSymbol);
1108+
RETURN_EMPTY_IF(maybeProfData.IsEmpty());
11021109

1103-
if (profData->IsUndefined()) return std::shared_ptr<Global<Value>>();
1110+
auto profData = maybeProfData.ToLocalChecked();
1111+
RETURN_EMPTY_IF(profData->IsUndefined());
11041112

11051113
return static_cast<PersistentContextPtr*>(profData.As<External>()->Value())
11061114
->Get();
1115+
1116+
#undef RETURN_EMPTY_IF
11071117
#else
11081118
return curContext_.Get();
11091119
#endif

0 commit comments

Comments
 (0)