@@ -21,26 +21,62 @@ namespace node {
21
21
#define NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX 34
22
22
#endif
23
23
24
- #ifndef NODE_CONTEXT_TAG
25
- #define NODE_CONTEXT_TAG 35
26
- #endif
27
-
28
24
#ifndef NODE_BINDING_LIST
29
- #define NODE_BINDING_LIST_INDEX 36
25
+ #define NODE_BINDING_LIST_INDEX 35
30
26
#endif
31
27
32
28
#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
34
37
#endif
35
38
36
39
enum ContextEmbedderIndex {
37
40
kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
38
41
kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
39
42
kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
40
- kContextTag = NODE_CONTEXT_TAG,
41
43
kBindingListIndex = NODE_BINDING_LIST_INDEX,
42
44
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 ;
44
80
};
45
81
46
82
} // namespace node
0 commit comments