Skip to content

Commit bc52c65

Browse files
committed
src: extract common context embedder tag checks
Extract common context embedder tag checks to ContextEmbedderTag so that verifying if a context is a node context doesn't need to access private members of node::Environment.
1 parent ec44403 commit bc52c65

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

src/env-inl.h

+1-10
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,7 @@ inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
177177
}
178178

179179
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
180-
if (UNLIKELY(context.IsEmpty())) {
181-
return nullptr;
182-
}
183-
if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
184-
ContextEmbedderIndex::kContextTag)) {
185-
return nullptr;
186-
}
187-
if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
188-
ContextEmbedderIndex::kContextTag) !=
189-
Environment::kNodeContextTagPtr)) {
180+
if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) {
190181
return nullptr;
191182
}
192183
return static_cast<Environment*>(

src/env.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ using v8::WeakCallbackInfo;
6161
using v8::WeakCallbackType;
6262
using worker::Worker;
6363

64-
int const Environment::kNodeContextTag = 0x6e6f64;
65-
void* const Environment::kNodeContextTagPtr = const_cast<void*>(
66-
static_cast<const void*>(&Environment::kNodeContextTag));
64+
int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64;
65+
void* const ContextEmbedderTag::kNodeContextTagPtr = const_cast<void*>(
66+
static_cast<const void*>(&ContextEmbedderTag::kNodeContextTag));
6767

6868
void AsyncHooks::SetJSPromiseHooks(Local<Function> init,
6969
Local<Function> before,
@@ -512,11 +512,9 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() {
512512

513513
void Environment::AssignToContext(Local<v8::Context> context,
514514
const ContextInfo& info) {
515+
ContextEmbedderTag::TagNodeContext(context);
515516
context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
516517
this);
517-
// Used by Environment::GetCurrent to know that we are on a node context.
518-
context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kContextTag,
519-
Environment::kNodeContextTagPtr);
520518
// Used to retrieve bindings
521519
context->SetAlignedPointerInEmbedderData(
522520
ContextEmbedderIndex::kBindingListIndex, &(this->bindings_));

src/env.h

-3
Original file line numberDiff line numberDiff line change
@@ -1573,9 +1573,6 @@ class Environment : public MemoryRetainer {
15731573
uint64_t thread_id_;
15741574
std::unordered_set<worker::Worker*> sub_worker_contexts_;
15751575

1576-
static void* const kNodeContextTagPtr;
1577-
static int const kNodeContextTag;
1578-
15791576
#if HAVE_INSPECTOR
15801577
std::unique_ptr<inspector::Agent> inspector_agent_;
15811578
bool is_in_inspector_console_call_ = false;

src/node_context_data.h

+44-8
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,62 @@ namespace node {
2121
#define NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX 34
2222
#endif
2323

24-
#ifndef NODE_CONTEXT_TAG
25-
#define NODE_CONTEXT_TAG 35
26-
#endif
27-
2824
#ifndef NODE_BINDING_LIST
29-
#define NODE_BINDING_LIST_INDEX 36
25+
#define NODE_BINDING_LIST_INDEX 35
3026
#endif
3127

3228
#ifndef NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX
33-
#define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 37
29+
#define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 36
30+
#endif
31+
32+
// NODE_CONTEXT_TAG must be greater than any embedder indexes so that a single
33+
// check on the number of embedder data fields can assure the presence of all
34+
// embedder indexes.
35+
#ifndef NODE_CONTEXT_TAG
36+
#define NODE_CONTEXT_TAG 37
3437
#endif
3538

3639
enum ContextEmbedderIndex {
3740
kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
3841
kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
3942
kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
40-
kContextTag = NODE_CONTEXT_TAG,
4143
kBindingListIndex = NODE_BINDING_LIST_INDEX,
4244
kAllowCodeGenerationFromStrings =
43-
NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX
45+
NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
46+
kContextTag = NODE_CONTEXT_TAG,
47+
};
48+
49+
class ContextEmbedderTag {
50+
public:
51+
static inline void TagNodeContext(v8::Local<v8::Context> context) {
52+
// Used by ContextEmbedderTag::IsNodeContext to know that we are on a node
53+
// context.
54+
context->SetAlignedPointerInEmbedderData(
55+
ContextEmbedderIndex::kContextTag,
56+
ContextEmbedderTag::kNodeContextTagPtr);
57+
}
58+
59+
static inline bool IsNodeContext(v8::Local<v8::Context> context) {
60+
if (UNLIKELY(context.IsEmpty())) {
61+
return false;
62+
}
63+
if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
64+
ContextEmbedderIndex::kContextTag)) {
65+
return false;
66+
}
67+
if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
68+
ContextEmbedderIndex::kContextTag) !=
69+
ContextEmbedderTag::kNodeContextTagPtr)) {
70+
return false;
71+
}
72+
return true;
73+
}
74+
75+
private:
76+
static void* const kNodeContextTagPtr;
77+
static int const kNodeContextTag;
78+
79+
ContextEmbedderTag() = delete;
4480
};
4581

4682
} // namespace node

0 commit comments

Comments
 (0)