GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_context_data.h Lines: 12 13 92.3 %
Date: 2022-08-31 04:20:31 Branches: 5 6 83.3 %

Line Branch Exec Source
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_LIST
28
#define NODE_BINDING_LIST_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
// NODE_CONTEXT_TAG must be greater than any embedder indexes so that a single
40
// check on the number of embedder data fields can assure the presence of all
41
// embedder indexes.
42
#ifndef NODE_CONTEXT_TAG
43
#define NODE_CONTEXT_TAG 38
44
#endif
45
46
enum ContextEmbedderIndex {
47
  kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
48
  kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
49
  kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
50
  kBindingListIndex = NODE_BINDING_LIST_INDEX,
51
  kAllowCodeGenerationFromStrings =
52
      NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
53
  kContextifyContext = NODE_CONTEXT_CONTEXTIFY_CONTEXT_INDEX,
54
  kContextTag = NODE_CONTEXT_TAG,
55
};
56
57
class ContextEmbedderTag {
58
 public:
59
6823
  static inline void TagNodeContext(v8::Local<v8::Context> context) {
60
    // Used by ContextEmbedderTag::IsNodeContext to know that we are on a node
61
    // context.
62
6823
    context->SetAlignedPointerInEmbedderData(
63
        ContextEmbedderIndex::kContextTag,
64
        ContextEmbedderTag::kNodeContextTagPtr);
65
6823
  }
66
67
8786264
  static inline bool IsNodeContext(v8::Local<v8::Context> context) {
68
8786264
    if (UNLIKELY(context.IsEmpty())) {
69
1
      return false;
70
    }
71
8786263
    if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
72
8786263
                 ContextEmbedderIndex::kContextTag)) {
73
55
      return false;
74
    }
75
8786208
    if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
76
                     ContextEmbedderIndex::kContextTag) !=
77
8786208
                 ContextEmbedderTag::kNodeContextTagPtr)) {
78
      return false;
79
    }
80
8786208
    return true;
81
  }
82
83
 private:
84
  static void* const kNodeContextTagPtr;
85
  static int const kNodeContextTag;
86
87
  ContextEmbedderTag() = delete;
88
};
89
90
}  // namespace node
91
92
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
93
94
#endif  // SRC_NODE_CONTEXT_DATA_H_