diff --git a/src/env-inl.h b/src/env-inl.h index 71d3b65729..8e2eacd0dd 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -177,16 +177,7 @@ inline Environment* Environment::GetCurrent(v8::Isolate* isolate) { } inline Environment* Environment::GetCurrent(v8::Local context) { - if (UNLIKELY(context.IsEmpty())) { - return nullptr; - } - if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <= - ContextEmbedderIndex::kContextTag)) { - return nullptr; - } - if (UNLIKELY(context->GetAlignedPointerFromEmbedderData( - ContextEmbedderIndex::kContextTag) != - Environment::kNodeContextTagPtr)) { + if (UNLIKELY(!ContextEmbedderTag::IsNodeContext(context))) { return nullptr; } return static_cast( diff --git a/src/env.cc b/src/env.cc index 22be69ec30..5350c0d7cb 100644 --- a/src/env.cc +++ b/src/env.cc @@ -61,9 +61,9 @@ using v8::WeakCallbackInfo; using v8::WeakCallbackType; using worker::Worker; -int const Environment::kNodeContextTag = 0x6e6f64; -void* const Environment::kNodeContextTagPtr = const_cast( - static_cast(&Environment::kNodeContextTag)); +int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64; +void* const ContextEmbedderTag::kNodeContextTagPtr = const_cast( + static_cast(&ContextEmbedderTag::kNodeContextTag)); void AsyncHooks::SetJSPromiseHooks(Local init, Local before, @@ -431,11 +431,9 @@ void TrackingTraceStateObserver::UpdateTraceCategoryState() { void Environment::AssignToContext(Local context, const ContextInfo& info) { + ContextEmbedderTag::TagNodeContext(context); context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment, this); - // Used by Environment::GetCurrent to know that we are on a node context. - context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kContextTag, - Environment::kNodeContextTagPtr); // Used to retrieve bindings context->SetAlignedPointerInEmbedderData( ContextEmbedderIndex::kBindingListIndex, &(this->bindings_)); diff --git a/src/env.h b/src/env.h index c67d08ef96..0045ca88b4 100644 --- a/src/env.h +++ b/src/env.h @@ -1535,9 +1535,6 @@ class Environment : public MemoryRetainer { uint64_t thread_id_; std::unordered_set sub_worker_contexts_; - static void* const kNodeContextTagPtr; - static int const kNodeContextTag; - #if HAVE_INSPECTOR std::unique_ptr inspector_agent_; bool is_in_inspector_console_call_ = false; diff --git a/src/node_context_data.h b/src/node_context_data.h index 42aae872e6..632b648a0a 100644 --- a/src/node_context_data.h +++ b/src/node_context_data.h @@ -3,6 +3,9 @@ #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS +#include "util.h" +#include "v8.h" + namespace node { // Pick an index that's hopefully out of the way when we're embedded inside @@ -21,26 +24,62 @@ namespace node { #define NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX 34 #endif -#ifndef NODE_CONTEXT_TAG -#define NODE_CONTEXT_TAG 35 -#endif - #ifndef NODE_BINDING_LIST -#define NODE_BINDING_LIST_INDEX 36 +#define NODE_BINDING_LIST_INDEX 35 #endif #ifndef NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX -#define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 37 +#define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 36 +#endif + +// NODE_CONTEXT_TAG must be greater than any embedder indexes so that a single +// check on the number of embedder data fields can assure the presence of all +// embedder indexes. +#ifndef NODE_CONTEXT_TAG +#define NODE_CONTEXT_TAG 37 #endif enum ContextEmbedderIndex { kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX, kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX, kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX, - kContextTag = NODE_CONTEXT_TAG, kBindingListIndex = NODE_BINDING_LIST_INDEX, kAllowCodeGenerationFromStrings = - NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX + NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX, + kContextTag = NODE_CONTEXT_TAG, +}; + +class ContextEmbedderTag { + public: + static inline void TagNodeContext(v8::Local context) { + // Used by ContextEmbedderTag::IsNodeContext to know that we are on a node + // context. + context->SetAlignedPointerInEmbedderData( + ContextEmbedderIndex::kContextTag, + ContextEmbedderTag::kNodeContextTagPtr); + } + + static inline bool IsNodeContext(v8::Local context) { + if (UNLIKELY(context.IsEmpty())) { + return false; + } + if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <= + ContextEmbedderIndex::kContextTag)) { + return false; + } + if (UNLIKELY(context->GetAlignedPointerFromEmbedderData( + ContextEmbedderIndex::kContextTag) != + ContextEmbedderTag::kNodeContextTagPtr)) { + return false; + } + return true; + } + + private: + static void* const kNodeContextTagPtr; + static int const kNodeContextTag; + + ContextEmbedderTag() = delete; }; } // namespace node