Skip to content

Commit bbbe8a4

Browse files
committed
Rewrote it to be more compact
1 parent 10cd8c8 commit bbbe8a4

1 file changed

Lines changed: 18 additions & 47 deletions

File tree

bindings/binding.cc

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
#include <unistd.h>
3030
#endif
3131

32-
// Whether the isolate's ContinuationPreservedEmbedderData currently binds
33-
// `key` to `value`.
32+
// Whether the isolate's ContinuationPreservedEmbedderData is a JS Map that
33+
// currently binds `key` to `value`.
3434
//
3535
// This exists for AsyncContextFrame feature detection. With ACF active, Node
3636
// implements AsyncLocalStorage#run by installing an AsyncContextFrame — a JS
@@ -39,57 +39,28 @@
3939
// store therefore observes the property this addon actually depends on,
4040
// instead of inferring it from the Node version, process.execArgv, or whether
4141
// run() happens to dispatch through the instance's enterWith.
42-
//
43-
// It is the same slot, and the same "is it a Map" question, that
44-
// WallProfiler::SetContext asks before storing a context, and the identity
45-
// hash published as otel_thread_ctx_nodejs_v1.als_identity_hash is the hash of
46-
// this very key — so a false answer here means both consumers are broken.
47-
//
48-
// Deliberately total: no context entered, CPED unset or not a Map, or the key
49-
// absent all yield false rather than throwing. The otel thread-ctx writer calls
50-
// this from ensureHook(), which runs inside dd-trace-js diagnostic-channel
51-
// subscribers, where an exception would surface in application code.
52-
//
53-
// Uses the public v8::Map::Get rather than the raw OrderedHashMap walk in
54-
// map-get.hh on purpose: this answers "is ACF on", and conflating it with "is
55-
// our map layout knowledge still correct" would report a V8 layout change as
56-
// ACF being unavailable. Layout is covered separately, by the GetValueFromMap
57-
// tests.
5842
static NAN_METHOD(CpedMapContains) {
5943
#if NODE_MAJOR_VERSION >= 22
60-
auto isolate = info.GetIsolate();
61-
6244
// A malformed call must not accidentally answer true by comparing an absent
6345
// key's undefined against an undefined expected value.
64-
if (info.Length() < 2) {
65-
info.GetReturnValue().Set(false);
66-
return;
46+
if (info.Length() >= 2) {
47+
auto isolate = info.GetIsolate();
48+
auto cped = isolate->GetContinuationPreservedEmbedderData();
49+
if (!cped.IsEmpty() && cped->IsMap()) {
50+
auto context = isolate->GetCurrentContext();
51+
if (!context.IsEmpty()) {
52+
v8::Local<v8::Value> found;
53+
if (cped.As<v8::Map>()->Get(context, info[0]).ToLocal(&found)) {
54+
info.GetReturnValue().Set(found->StrictEquals(info[1]));
55+
return;
56+
}
57+
}
58+
}
6759
}
68-
69-
auto context = isolate->GetCurrentContext();
70-
if (context.IsEmpty()) {
71-
info.GetReturnValue().Set(false);
72-
return;
73-
}
74-
75-
auto cped = isolate->GetContinuationPreservedEmbedderData();
76-
if (cped.IsEmpty() || !cped->IsMap()) {
77-
info.GetReturnValue().Set(false);
78-
return;
79-
}
80-
81-
v8::Local<v8::Value> found;
82-
if (!cped.As<v8::Map>()->Get(context, info[0]).ToLocal(&found)) {
83-
info.GetReturnValue().Set(false);
84-
return;
85-
}
86-
87-
info.GetReturnValue().Set(found->StrictEquals(info[1]));
88-
#else
89-
// No ContinuationPreservedEmbedderData, and no AsyncContextFrame to put in
90-
// it, so false is the right answer rather than a missing export.
91-
info.GetReturnValue().Set(false);
9260
#endif
61+
// Either code above didn't reach the innermost if statement, or
62+
// we're compiling for Node.js < 22.
63+
info.GetReturnValue().Set(false);
9364
}
9465

9566
static NAN_METHOD(GetNativeThreadId) {

0 commit comments

Comments
 (0)