33
44#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55
6+ #include " util.h"
7+ #include " v8.h"
8+
69namespace node {
710
811// Pick an index that's hopefully out of the way when we're embedded inside
@@ -21,26 +24,62 @@ namespace node {
2124#define NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX 34
2225#endif
2326
24- #ifndef NODE_CONTEXT_TAG
25- #define NODE_CONTEXT_TAG 35
26- #endif
27-
2827#ifndef NODE_BINDING_LIST
29- #define NODE_BINDING_LIST_INDEX 36
28+ #define NODE_BINDING_LIST_INDEX 35
3029#endif
3130
3231#ifndef NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX
33- #define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 37
32+ #define NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX 36
33+ #endif
34+
35+ // NODE_CONTEXT_TAG must be greater than any embedder indexes so that a single
36+ // check on the number of embedder data fields can assure the presence of all
37+ // embedder indexes.
38+ #ifndef NODE_CONTEXT_TAG
39+ #define NODE_CONTEXT_TAG 37
3440#endif
3541
3642enum ContextEmbedderIndex {
3743 kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
3844 kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
3945 kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
40- kContextTag = NODE_CONTEXT_TAG,
4146 kBindingListIndex = NODE_BINDING_LIST_INDEX,
4247 kAllowCodeGenerationFromStrings =
43- NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX
48+ NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
49+ kContextTag = NODE_CONTEXT_TAG,
50+ };
51+
52+ class ContextEmbedderTag {
53+ public:
54+ static inline void TagNodeContext (v8::Local<v8::Context> context) {
55+ // Used by ContextEmbedderTag::IsNodeContext to know that we are on a node
56+ // context.
57+ context->SetAlignedPointerInEmbedderData (
58+ ContextEmbedderIndex::kContextTag ,
59+ ContextEmbedderTag::kNodeContextTagPtr );
60+ }
61+
62+ static inline bool IsNodeContext (v8::Local<v8::Context> context) {
63+ if (UNLIKELY (context.IsEmpty ())) {
64+ return false ;
65+ }
66+ if (UNLIKELY (context->GetNumberOfEmbedderDataFields () <=
67+ ContextEmbedderIndex::kContextTag )) {
68+ return false ;
69+ }
70+ if (UNLIKELY (context->GetAlignedPointerFromEmbedderData (
71+ ContextEmbedderIndex::kContextTag ) !=
72+ ContextEmbedderTag::kNodeContextTagPtr )) {
73+ return false ;
74+ }
75+ return true ;
76+ }
77+
78+ private:
79+ static void * const kNodeContextTagPtr ;
80+ static int const kNodeContextTag ;
81+
82+ ContextEmbedderTag () = delete ;
4483};
4584
4685} // namespace node
0 commit comments