Skip to content

Commit c9b4de5

Browse files
committed
src: standardise context embedder indices
PR-URL: #19135 Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Matheus Marchini <matheus@sthima.com>
1 parent a6c14b2 commit c9b4de5

File tree

7 files changed

+45
-28
lines changed

7 files changed

+45
-28
lines changed

src/env-inl.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "uv.h"
3232
#include "v8.h"
3333
#include "node_perf_common.h"
34+
#include "node_context_data.h"
3435

3536
#include <stddef.h>
3637
#include <stdint.h>
@@ -283,7 +284,8 @@ inline void Environment::TickInfo::set_has_thrown(bool state) {
283284

284285
inline void Environment::AssignToContext(v8::Local<v8::Context> context,
285286
const ContextInfo& info) {
286-
context->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, this);
287+
context->SetAlignedPointerInEmbedderData(
288+
ContextEmbedderIndex::kEnvironment, this);
287289
#if HAVE_INSPECTOR
288290
inspector_agent()->ContextCreated(context, info);
289291
#endif // HAVE_INSPECTOR
@@ -295,7 +297,8 @@ inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
295297

296298
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
297299
return static_cast<Environment*>(
298-
context->GetAlignedPointerFromEmbedderData(kContextEmbedderDataIndex));
300+
context->GetAlignedPointerFromEmbedderData(
301+
ContextEmbedderIndex::kEnvironment));
299302
}
300303

301304
inline Environment* Environment::GetCurrent(
@@ -368,8 +371,8 @@ inline Environment::~Environment() {
368371
inspector_agent_.reset();
369372
#endif
370373

371-
context()->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex,
372-
nullptr);
374+
context()->SetAlignedPointerInEmbedderData(
375+
ContextEmbedderIndex::kEnvironment, nullptr);
373376

374377
delete[] heap_statistics_buffer_;
375378
delete[] heap_space_statistics_buffer_;

src/env.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ struct PackageConfig {
7575
};
7676
} // namespace loader
7777

78-
// Pick an index that's hopefully out of the way when we're embedded inside
79-
// another application. Performance-wise or memory-wise it doesn't matter:
80-
// Context::SetAlignedPointerInEmbedderData() is backed by a FixedArray,
81-
// worst case we pay a one-time penalty for resizing the array.
82-
#ifndef NODE_CONTEXT_EMBEDDER_DATA_INDEX
83-
#define NODE_CONTEXT_EMBEDDER_DATA_INDEX 32
84-
#endif
85-
8678
// The number of items passed to push_values_to_array_function has diminishing
8779
// returns around 8. This should be used at all call sites using said function.
8880
#ifndef NODE_PUSH_VAL_TO_ARRAY_MAX
@@ -730,8 +722,6 @@ class Environment {
730722
inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; }
731723
inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; }
732724

733-
static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;
734-
735725
void AddPromiseHook(promise_hook_func fn, void* arg);
736726
bool RemovePromiseHook(promise_hook_func fn, void* arg);
737727
bool EmitNapiWarning();

src/node_context_data.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef SRC_NODE_CONTEXT_DATA_H_
2+
#define SRC_NODE_CONTEXT_DATA_H_
3+
4+
namespace node {
5+
6+
// Pick an index that's hopefully out of the way when we're embedded inside
7+
// another application. Performance-wise or memory-wise it doesn't matter:
8+
// Context::SetAlignedPointerInEmbedderData() is backed by a FixedArray,
9+
// worst case we pay a one-time penalty for resizing the array.
10+
#ifndef NODE_CONTEXT_EMBEDDER_DATA_INDEX
11+
#define NODE_CONTEXT_EMBEDDER_DATA_INDEX 32
12+
#endif
13+
14+
#ifndef NODE_CONTEXT_SANDBOX_OBJECT_INDEX
15+
#define NODE_CONTEXT_SANDBOX_OBJECT_INDEX 33
16+
#endif
17+
18+
enum ContextEmbedderIndex {
19+
kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
20+
kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
21+
};
22+
23+
} // namespace node
24+
25+
#endif // SRC_NODE_CONTEXT_DATA_H_

src/node_contextify.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "node_watchdog.h"
2424
#include "base_object-inl.h"
2525
#include "node_contextify.h"
26+
#include "node_context_data.h"
2627

2728
namespace node {
2829
namespace contextify {
@@ -173,7 +174,7 @@ Local<Context> ContextifyContext::CreateV8Context(
173174
// embedder data field. However, we cannot hold a reference to a v8::Context
174175
// directly in an Object, we instead hold onto the new context's global
175176
// object instead (which then has a reference to the context).
176-
ctx->SetEmbedderData(kSandboxObjectIndex, sandbox_obj);
177+
ctx->SetEmbedderData(ContextEmbedderIndex::kSandboxObject, sandbox_obj);
177178
sandbox_obj->SetPrivate(env->context(),
178179
env->contextify_global_private_symbol(),
179180
ctx->Global());

src/node_contextify.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
#include "node_internals.h"
55
#include "node_watchdog.h"
66
#include "base_object-inl.h"
7+
#include "node_context_data.h"
78

89
namespace node {
910
namespace contextify {
1011

1112
class ContextifyContext {
1213
protected:
13-
// V8 reserves the first field in context objects for the debugger. We use the
14-
// second field to hold a reference to the sandbox object.
15-
enum { kSandboxObjectIndex = 1 };
16-
1714
Environment* const env_;
1815
Persistent<v8::Context> context_;
1916

@@ -45,7 +42,7 @@ class ContextifyContext {
4542

4643
inline v8::Local<v8::Object> sandbox() const {
4744
return v8::Local<v8::Object>::Cast(
48-
context()->GetEmbedderData(kSandboxObjectIndex));
45+
context()->GetEmbedderData(ContextEmbedderIndex::kSandboxObject));
4946
}
5047

5148
private:

src/node_postmortem_metadata.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "util-inl.h"
55
#include "req_wrap.h"
66
#include "v8abbr.h"
7+
#include "node_context_data.h"
78

89
#define NODEDBG_SYMBOL(Name) nodedbg_ ## Name
910

@@ -34,7 +35,7 @@
3435
V(ListNode_ReqWrap, next_, uintptr_t, ListNode<ReqWrap<uv_req_t>>::next_)
3536

3637
extern "C" {
37-
int nodedbg_const_Environment__kContextEmbedderDataIndex__int;
38+
int nodedbg_const_ContextEmbedderIndex__kEnvironment__int;
3839
uintptr_t nodedbg_offset_ExternalString__data__uintptr_t;
3940

4041
#define V(Class, Member, Type, Accessor) \
@@ -46,8 +47,8 @@ uintptr_t nodedbg_offset_ExternalString__data__uintptr_t;
4647
namespace node {
4748

4849
int GenDebugSymbols() {
49-
nodedbg_const_Environment__kContextEmbedderDataIndex__int =
50-
Environment::kContextEmbedderDataIndex;
50+
nodedbg_const_ContextEmbedderIndex__kEnvironment__int =
51+
ContextEmbedderIndex::kEnvironment;
5152

5253
nodedbg_offset_ExternalString__data__uintptr_t = NODE_OFF_EXTSTR_DATA;
5354

test/cctest/test_node_postmortem_metadata.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern uintptr_t
1313
extern uintptr_t
1414
nodedbg_offset_Environment__handle_wrap_queue___Environment_HandleWrapQueue;
1515
extern int debug_symbols_generated;
16-
extern int nodedbg_const_Environment__kContextEmbedderDataIndex__int;
16+
extern int nodedbg_const_ContextEmbedderIndex__kEnvironment__int;
1717
extern uintptr_t
1818
nodedbg_offset_Environment_HandleWrapQueue__head___ListNode_HandleWrap;
1919
extern uintptr_t
@@ -56,10 +56,10 @@ class TestReqWrap : public node::ReqWrap<uv_req_t> {
5656
node::AsyncWrap::PROVIDER_TIMERWRAP) {}
5757
};
5858

59-
TEST_F(DebugSymbolsTest, ContextEmbedderDataIndex) {
60-
int kContextEmbedderDataIndex = node::Environment::kContextEmbedderDataIndex;
61-
EXPECT_EQ(nodedbg_const_Environment__kContextEmbedderDataIndex__int,
62-
kContextEmbedderDataIndex);
59+
TEST_F(DebugSymbolsTest, ContextEmbedderEnvironmentIndex) {
60+
int kEnvironmentIndex = node::ContextEmbedderIndex::kEnvironment;
61+
EXPECT_EQ(nodedbg_const_ContextEmbedderIndex__kEnvironment__int,
62+
kEnvironmentIndex);
6363
}
6464

6565
TEST_F(DebugSymbolsTest, ExternalStringDataOffset) {

0 commit comments

Comments
 (0)