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-02-13 22:28:58 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 "node_persistent.h"
28
#include "memory_tracker-inl.h"
29
#include "v8.h"
30
#include <type_traits>  // std::remove_reference
31
32
namespace node {
33
34
class Environment;
35
36
class BaseObject : public MemoryRetainer {
37
 public:
38
  // Associates this object with `object`. It uses the 0th internal field for
39
  // that, and in particular aborts if there is no such field.
40
  inline BaseObject(Environment* env, v8::Local<v8::Object> object);
41
  virtual inline ~BaseObject();
42
43
  // Returns the wrapped object.  Returns an empty handle when
44
  // persistent.IsEmpty() is true.
45
  inline v8::Local<v8::Object> object() const;
46
47
  // Same as the above, except it additionally verifies that this object
48
  // is associated with the passed Isolate in debug mode.
49
  inline v8::Local<v8::Object> object(v8::Isolate* isolate) const;
50
51
  inline Persistent<v8::Object>& persistent();
52
53
  inline Environment* env() const;
54
55
  // Get a BaseObject* pointer, or subclass pointer, for the JS object that
56
  // was also passed to the `BaseObject()` constructor initially.
57
  // This may return `nullptr` if the C++ object has not been constructed yet,
58
  // e.g. when the JS object used `MakeLazilyInitializedJSTemplate`.
59
  static inline BaseObject* FromJSObject(v8::Local<v8::Object> object);
60
  template <typename T>
61
  static inline T* FromJSObject(v8::Local<v8::Object> object);
62
63
  // Make the `Persistent` a weak reference and, `delete` this object once
64
  // the JS object has been garbage collected.
65
  inline void MakeWeak();
66
67
  // Undo `MakeWeak()`, i.e. turn this into a strong reference.
68
  inline void ClearWeak();
69
70
  // Utility to create a FunctionTemplate with one internal field (used for
71
  // the `BaseObject*` pointer) and a constructor that initializes that field
72
  // to `nullptr`.
73
  static inline v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
74
      Environment* env);
75
76
 private:
77
  BaseObject();
78
79
  v8::Local<v8::Object> WrappedObject() const override;
80
  bool IsRootNode() const override;
81
  static void DeleteMe(void* data);
82
83
  // persistent_handle_ needs to be at a fixed offset from the start of the
84
  // class because it is used by src/node_postmortem_metadata.cc to calculate
85
  // offsets and generate debug symbols for BaseObject, which assumes that the
86
  // position of members in memory are predictable. For more information please
87
  // refer to `doc/guides/node-postmortem-support.md`
88
  friend int GenDebugSymbols();
89
  friend class Environment;
90
91
  Persistent<v8::Object> persistent_handle_;
92
  Environment* env_;
93
};
94
95
96
// Global alias for FromJSObject() to avoid churn.
97
template <typename T>
98
201545
inline T* Unwrap(v8::Local<v8::Object> obj) {
99
201545
  return BaseObject::FromJSObject<T>(obj);
100
}
101
102
103
#define ASSIGN_OR_RETURN_UNWRAP(ptr, obj, ...)                                \
104
  do {                                                                        \
105
    *ptr = static_cast<typename std::remove_reference<decltype(*ptr)>::type>( \
106
        BaseObject::FromJSObject(obj));                                       \
107
    if (*ptr == nullptr)                                                      \
108
      return __VA_ARGS__;                                                     \
109
  } while (0)
110
111
}  // namespace node
112
113
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
114
115
#endif  // SRC_BASE_OBJECT_H_