GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_context_data.h Lines: 12 13 92.3 %
Date: 2022-08-21 04:19:51 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
// 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
40
#endif
41
42
enum ContextEmbedderIndex {
43
  kEnvironment = NODE_CONTEXT_EMBEDDER_DATA_INDEX,
44
  kSandboxObject = NODE_CONTEXT_SANDBOX_OBJECT_INDEX,
45
  kAllowWasmCodeGeneration = NODE_CONTEXT_ALLOW_WASM_CODE_GENERATION_INDEX,
46
  kBindingListIndex = NODE_BINDING_LIST_INDEX,
47
  kAllowCodeGenerationFromStrings =
48
      NODE_CONTEXT_ALLOW_CODE_GENERATION_FROM_STRINGS_INDEX,
49
  kContextTag = NODE_CONTEXT_TAG,
50
};
51
52
class ContextEmbedderTag {
53
 public:
54
6751
  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
6751
    context->SetAlignedPointerInEmbedderData(
58
        ContextEmbedderIndex::kContextTag,
59
        ContextEmbedderTag::kNodeContextTagPtr);
60
6751
  }
61
62
7740147
  static inline bool IsNodeContext(v8::Local<v8::Context> context) {
63
7740147
    if (UNLIKELY(context.IsEmpty())) {
64
1
      return false;
65
    }
66
7740146
    if (UNLIKELY(context->GetNumberOfEmbedderDataFields() <=
67
7740146
                 ContextEmbedderIndex::kContextTag)) {
68
11
      return false;
69
    }
70
7740135
    if (UNLIKELY(context->GetAlignedPointerFromEmbedderData(
71
                     ContextEmbedderIndex::kContextTag) !=
72
7740135
                 ContextEmbedderTag::kNodeContextTagPtr)) {
73
      return false;
74
    }
75
7740135
    return true;
76
  }
77
78
 private:
79
  static void* const kNodeContextTagPtr;
80
  static int const kNodeContextTag;
81
82
  ContextEmbedderTag() = delete;
83
};
84
85
}  // namespace node
86
87
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
88
89
#endif  // SRC_NODE_CONTEXT_DATA_H_