• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef SRC_NODE_CONTEXT_DATA_H_
2 #define SRC_NODE_CONTEXT_DATA_H_
3 
4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5 
6 #include "util.h"
7 #include "v8.h"
8 
9 namespace node {
10 
11 // Pick an index that's hopefully out of the way when we're embedded inside
12 // another application. Performance-wise or memory-wise it doesn't matter:
13 // Context::SetAlignedPointerInEmbedderData() is backed by a FixedArray,
14 // worst case we pay a one-time penalty for resizing the array.
15 #ifndef NODE_CONTEXT_EMBEDDER_DATA_INDEX
16 #define NODE_CONTEXT_EMBEDDER_DATA_INDEX 32
17 #endif
18 
19 #ifndef NODE_CONTEXT_SANDBOX_OBJECT_INDEX
20 #define NODE_CONTEXT_SANDBOX_OBJECT_INDEX 33
21 #endif
22 
23 #ifndef NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX
24 #define NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX 34
25 #endif
26 
27 #ifndef NODE_BINDING_DATA_STORE_INDEX
28 #define NODE_BINDING_DATA_STORE_INDEX 35
29 #endif
30 
31 #ifndef NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX
32 #define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 36
33 #endif
34 
35 #ifndef NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX
36 #define NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX 37
37 #endif
38 
39 #ifndef NODE_CONTEXT_REALM_INDEX
40 #define NODE_CONTEXT_REALM_INDEX 38
41 #endif
42 
43 // NODE_CONTEXT_TAG must be greater than any embedder indexes so that a single
44 // check on the number of embedder data fields can assure the presence of all
45 // embedder indexes.
46 #ifndef NODE_CONTEXT_TAG
47 #define NODE_CONTEXT_TAG 39
48 #endif
49 
50 enum ContextEmbedderIndex {
51   kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
52   kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
53   kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
54   kBindingDataStoreIndex = NODE_BINDING_DATA_STORE_INDEX,
55   kAllowCodeGenerationFromStrings =
56       NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
57   kContextifyContext = NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX,
58   kRealm = NODE_CONTEXT_REALM_INDEX,
59   kContextTag = NODE_CONTEXT_TAG,
60 };
61 
62 class ContextEmbedderTag {
63  public:
TagNodeContext(v8::Local<v8::Context> context)64   static inline void TagNodeContext(v8::Local<v8::Context> context) {
65     // Used by ContextEmbedderTag::IsNodeContext to know that we are on a node
66     // context.
67     context->SetAlignedPointerInEmbedderData(
68         ContextEmbedderIndex::kContextTag,
69         ContextEmbedderTag::kNodeContextTagPtr);
70   }
71 
IsNodeContext(v8::Local<v8::Context> context)72   static inline bool IsNodeContext(v8::Local<v8::Context> context) {
73     if (UNLIKELY(context.IsEmpty())) {
74       return false;
75     }
76     if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
77                  ContextEmbedderIndex::kContextTag)) {
78       return false;
79     }
80     if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
81                      ContextEmbedderIndex::kContextTag) !=
82                  ContextEmbedderTag::kNodeContextTagPtr)) {
83       return false;
84     }
85     return true;
86   }
87 
88  private:
89   static void* const kNodeContextTagPtr;
90   static int const kNodeContextTag;
91 
92   ContextEmbedderTag() = delete;
93 };
94 
95 }  // namespace node
96 
97 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
98 
99 #endif  // SRC_NODE_CONTEXT_DATA_H_
100