From c9b4de55c061ecb8d64cda9e36821c21f8150925 Mon Sep 17 00:00:00 2001 From: Gus Caplan Date: Sun, 4 Mar 2018 14:59:51 -0600 Subject: [PATCH] src: standardise context embedder indices PR-URL: https://github.com/nodejs/node/pull/19135 Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Ben Noordhuis Reviewed-By: Jeremiah Senkpiel Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Joyee Cheung Reviewed-By: Matheus Marchini --- src/env-inl.h | 11 +++++---- src/env.h | 10 -------- src/node_context_data.h | 25 ++++++++++++++++++++ src/node_contextify.cc | 3 ++- src/node_contextify.h | 7 ++---- src/node_postmortem_metadata.cc | 7 +++--- test/cctest/test_node_postmortem_metadata.cc | 10 ++++---- 7 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 src/node_context_data.h diff --git a/src/env-inl.h b/src/env-inl.h index 365ace55784837..89a321a92003a7 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -31,6 +31,7 @@ #include "uv.h" #include "v8.h" #include "node_perf_common.h" +#include "node_context_data.h" #include #include @@ -283,7 +284,8 @@ inline void Environment::TickInfo::set_has_thrown(bool state) { inline void Environment::AssignToContext(v8::Local context, const ContextInfo& info) { - context->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, this); + context->SetAlignedPointerInEmbedderData( + ContextEmbedderIndex::kEnvironment, this); #if HAVE_INSPECTOR inspector_agent()->ContextCreated(context, info); #endif // HAVE_INSPECTOR @@ -295,7 +297,8 @@ inline Environment* Environment::GetCurrent(v8::Isolate* isolate) { inline Environment* Environment::GetCurrent(v8::Local context) { return static_cast( - context->GetAlignedPointerFromEmbedderData(kContextEmbedderDataIndex)); + context->GetAlignedPointerFromEmbedderData( + ContextEmbedderIndex::kEnvironment)); } inline Environment* Environment::GetCurrent( @@ -368,8 +371,8 @@ inline Environment::~Environment() { inspector_agent_.reset(); #endif - context()->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, - nullptr); + context()->SetAlignedPointerInEmbedderData( + ContextEmbedderIndex::kEnvironment, nullptr); delete[] heap_statistics_buffer_; delete[] heap_space_statistics_buffer_; diff --git a/src/env.h b/src/env.h index 4df4cb13b8209e..27e72f35d12bce 100644 --- a/src/env.h +++ b/src/env.h @@ -75,14 +75,6 @@ struct PackageConfig { }; } // namespace loader -// Pick an index that's hopefully out of the way when we're embedded inside -// another application. Performance-wise or memory-wise it doesn't matter: -// Context::SetAlignedPointerInEmbedderData() is backed by a FixedArray, -// worst case we pay a one-time penalty for resizing the array. -#ifndef NODE_CONTEXT_EMBEDDER_DATA_INDEX -#define NODE_CONTEXT_EMBEDDER_DATA_INDEX 32 -#endif - // The number of items passed to push_values_to_array_function has diminishing // returns around 8. This should be used at all call sites using said function. #ifndef NODE_PUSH_VAL_TO_ARRAY_MAX @@ -730,8 +722,6 @@ class Environment { inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; } inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; } - static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX; - void AddPromiseHook(promise_hook_func fn, void* arg); bool RemovePromiseHook(promise_hook_func fn, void* arg); bool EmitNapiWarning(); diff --git a/src/node_context_data.h b/src/node_context_data.h new file mode 100644 index 00000000000000..9d3145bb800b59 --- /dev/null +++ b/src/node_context_data.h @@ -0,0 +1,25 @@ +#ifndef SRC_NODE_CONTEXT_DATA_H_ +#define SRC_NODE_CONTEXT_DATA_H_ + +namespace node { + +// Pick an index that's hopefully out of the way when we're embedded inside +// another application. Performance-wise or memory-wise it doesn't matter: +// Context::SetAlignedPointerInEmbedderData() is backed by a FixedArray, +// worst case we pay a one-time penalty for resizing the array. +#ifndef NODE_CONTEXT_EMBEDDER_DATA_INDEX +#define NODE_CONTEXT_EMBEDDER_DATA_INDEX 32 +#endif + +#ifndef NODE_CONTEXT_SANDBOX_OBJECT_INDEX +#define NODE_CONTEXT_SANDBOX_OBJECT_INDEX 33 +#endif + +enum ContextEmbedderIndex { + kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX, + kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX, +}; + +} // namespace node + +#endif // SRC_NODE_CONTEXT_DATA_H_ diff --git a/src/node_contextify.cc b/src/node_contextify.cc index ef785936b77a69..d267a5180ff655 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -23,6 +23,7 @@ #include "node_watchdog.h" #include "base_object-inl.h" #include "node_contextify.h" +#include "node_context_data.h" namespace node { namespace contextify { @@ -173,7 +174,7 @@ Local ContextifyContext::CreateV8Context( // embedder data field. However, we cannot hold a reference to a v8::Context // directly in an Object, we instead hold onto the new context's global // object instead (which then has a reference to the context). - ctx->SetEmbedderData(kSandboxObjectIndex, sandbox_obj); + ctx->SetEmbedderData(ContextEmbedderIndex::kSandboxObject, sandbox_obj); sandbox_obj->SetPrivate(env->context(), env->contextify_global_private_symbol(), ctx->Global()); diff --git a/src/node_contextify.h b/src/node_contextify.h index 9f83740e43c73d..b25e1a75d4f26c 100644 --- a/src/node_contextify.h +++ b/src/node_contextify.h @@ -4,16 +4,13 @@ #include "node_internals.h" #include "node_watchdog.h" #include "base_object-inl.h" +#include "node_context_data.h" namespace node { namespace contextify { class ContextifyContext { protected: - // V8 reserves the first field in context objects for the debugger. We use the - // second field to hold a reference to the sandbox object. - enum { kSandboxObjectIndex = 1 }; - Environment* const env_; Persistent context_; @@ -45,7 +42,7 @@ class ContextifyContext { inline v8::Local sandbox() const { return v8::Local::Cast( - context()->GetEmbedderData(kSandboxObjectIndex)); + context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject)); } private: diff --git a/src/node_postmortem_metadata.cc b/src/node_postmortem_metadata.cc index b335e7fbf818c6..93bf5a4dd7d8f4 100644 --- a/src/node_postmortem_metadata.cc +++ b/src/node_postmortem_metadata.cc @@ -4,6 +4,7 @@ #include "util-inl.h" #include "req_wrap.h" #include "v8abbr.h" +#include "node_context_data.h" #define NODEDBG_SYMBOL(Name) nodedbg_ ## Name @@ -34,7 +35,7 @@ V(ListNode_ReqWrap, next_, uintptr_t, ListNode>::next_) extern "C" { -int nodedbg_const_Environment__kContextEmbedderDataIndex__int; +int nodedbg_const_ContextEmbedderIndex__kEnvironment__int; uintptr_t nodedbg_offset_ExternalString__data__uintptr_t; #define V(Class, Member, Type, Accessor) \ @@ -46,8 +47,8 @@ uintptr_t nodedbg_offset_ExternalString__data__uintptr_t; namespace node { int GenDebugSymbols() { - nodedbg_const_Environment__kContextEmbedderDataIndex__int = - Environment::kContextEmbedderDataIndex; + nodedbg_const_ContextEmbedderIndex__kEnvironment__int = + ContextEmbedderIndex::kEnvironment; nodedbg_offset_ExternalString__data__uintptr_t = NODE_OFF_EXTSTR_DATA; diff --git a/test/cctest/test_node_postmortem_metadata.cc b/test/cctest/test_node_postmortem_metadata.cc index e901d97668f5ff..335f3e85812547 100644 --- a/test/cctest/test_node_postmortem_metadata.cc +++ b/test/cctest/test_node_postmortem_metadata.cc @@ -13,7 +13,7 @@ extern uintptr_t extern uintptr_t nodedbg_offset_Environment__handle_wrap_queue___Environment_HandleWrapQueue; extern int debug_symbols_generated; -extern int nodedbg_const_Environment__kContextEmbedderDataIndex__int; +extern int nodedbg_const_ContextEmbedderIndex__kEnvironment__int; extern uintptr_t nodedbg_offset_Environment_HandleWrapQueue__head___ListNode_HandleWrap; extern uintptr_t @@ -56,10 +56,10 @@ class TestReqWrap : public node::ReqWrap { node::AsyncWrap::PROVIDER_TIMERWRAP) {} }; -TEST_F(DebugSymbolsTest, ContextEmbedderDataIndex) { - int kContextEmbedderDataIndex = node::Environment::kContextEmbedderDataIndex; - EXPECT_EQ(nodedbg_const_Environment__kContextEmbedderDataIndex__int, - kContextEmbedderDataIndex); +TEST_F(DebugSymbolsTest, ContextEmbedderEnvironmentIndex) { + int kEnvironmentIndex = node::ContextEmbedderIndex::kEnvironment; + EXPECT_EQ(nodedbg_const_ContextEmbedderIndex__kEnvironment__int, + kEnvironmentIndex); } TEST_F(DebugSymbolsTest, ExternalStringDataOffset) {