|
29 | 29 | #include <unistd.h> |
30 | 30 | #endif |
31 | 31 |
|
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`. |
34 | 34 | // |
35 | 35 | // This exists for AsyncContextFrame feature detection. With ACF active, Node |
36 | 36 | // implements AsyncLocalStorage#run by installing an AsyncContextFrame — a JS |
|
39 | 39 | // store therefore observes the property this addon actually depends on, |
40 | 40 | // instead of inferring it from the Node version, process.execArgv, or whether |
41 | 41 | // 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. |
58 | 42 | static NAN_METHOD(CpedMapContains) { |
59 | 43 | #if NODE_MAJOR_VERSION >= 22 |
60 | | - auto isolate = info.GetIsolate(); |
61 | | - |
62 | 44 | // A malformed call must not accidentally answer true by comparing an absent |
63 | 45 | // 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 | + } |
67 | 59 | } |
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); |
92 | 60 | #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); |
93 | 64 | } |
94 | 65 |
|
95 | 66 | static NAN_METHOD(GetNativeThreadId) { |
|
0 commit comments