GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/base_object.h Lines: 2 2 100.0 %
Date: 2019-09-13 22:28:55 Branches: 0 0 - %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_BASE_OBJECT_H_
23
#define SRC_BASE_OBJECT_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "memory_tracker.h"
28
#include "v8.h"
29
#include <type_traits>  // std::remove_reference
30
31
namespace node {
32
33
class Environment;
34
35
class BaseObject : public MemoryRetainer {
36
 public:
37
  // Associates this object with `object`. It uses the 0th internal field for
38
  // that, and in particular aborts if there is no such field.
39
  inline BaseObject(Environment* env, v8::Local<v8::Object> object);
40
  inline ~BaseObject() override;
41
42
  BaseObject() = delete;
43
44
  // Returns the wrapped object.  Returns an empty handle when
45
  // persistent.IsEmpty() is true.
46
  inline v8::Local<v8::Object> object() const;
47
48
  // Same as the above, except it additionally verifies that this object
49
  // is associated with the passed Isolate in debug mode.
50
  inline v8::Local<v8::Object> object(v8::Isolate* isolate) const;
51
52
  inline v8::Global<v8::Object>& persistent();
53
54
  inline Environment* env() const;
55
56
  // Get a BaseObject* pointer, or subclass pointer, for the JS object that
57
  // was also passed to the `BaseObject()` constructor initially.
58
  // This may return `nullptr` if the C++ object has not been constructed yet,
59
  // e.g. when the JS object used `MakeLazilyInitializedJSTemplate`.
60
  static inline BaseObject* FromJSObject(v8::Local<v8::Object> object);
61
  template <typename T>
62
  static inline T* FromJSObject(v8::Local<v8::Object> object);
63
64
  // Make the `v8::Global` a weak reference and, `delete` this object once
65
  // the JS object has been garbage collected.
66
  inline void MakeWeak();
67
68
  // Undo `MakeWeak()`, i.e. turn this into a strong reference.
69
  inline void ClearWeak();
70
71
  // Utility to create a FunctionTemplate with one internal field (used for
72
  // the `BaseObject*` pointer) and a constructor that initializes that field
73
  // to `nullptr`.
74
  static inline v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
75
      Environment* env);
76
77
  // Setter/Getter pair for internal fields that can be passed to SetAccessor.
78
  template <int Field>
79
  static void InternalFieldGet(v8::Local<v8::String> property,
80
                               const v8::PropertyCallbackInfo<v8::Value>& info);
81
  template <int Field, bool (v8::Value::* typecheck)() const>
82
  static void InternalFieldSet(v8::Local<v8::String> property,
83
                               v8::Local<v8::Value> value,
84
                               const v8::PropertyCallbackInfo<void>& info);
85
86
  // This is a bit of a hack. See the override in async_wrap.cc for details.
87
  virtual bool IsDoneInitializing() const;
88
89
 protected:
90
  // Can be used to avoid the automatic object deletion when the Environment
91
  // exits, for example when this object is owned and deleted by another
92
  // BaseObject at that point.
93
  inline void RemoveCleanupHook();
94
95
 private:
96
  v8::Local<v8::Object> WrappedObject() const override;
97
  static void DeleteMe(void* data);
98
99
  // persistent_handle_ needs to be at a fixed offset from the start of the
100
  // class because it is used by src/node_postmortem_metadata.cc to calculate
101
  // offsets and generate debug symbols for BaseObject, which assumes that the
102
  // position of members in memory are predictable. For more information please
103
  // refer to `doc/guides/node-postmortem-support.md`
104
  friend int GenDebugSymbols();
105
  friend class CleanupHookCallback;
106
107
  v8::Global<v8::Object> persistent_handle_;
108
  Environment* env_;
109
};
110
111
112
// Global alias for FromJSObject() to avoid churn.
113
template <typename T>
114
144377
inline T* Unwrap(v8::Local<v8::Object> obj) {
115
144377
  return BaseObject::FromJSObject<T>(obj);
116
}
117
118
119
#define ASSIGN_OR_RETURN_UNWRAP(ptr, obj, ...)                                \
120
  do {                                                                        \
121
    *ptr = static_cast<typename std::remove_reference<decltype(*ptr)>::type>( \
122
        BaseObject::FromJSObject(obj));                                       \
123
    if (*ptr == nullptr)                                                      \
124
      return __VA_ARGS__;                                                     \
125
  } while (0)
126
127
}  // namespace node
128
129
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
130
131
#endif  // SRC_BASE_OBJECT_H_