LCOV - code coverage report
Current view: top level - src - managed.h (source / functions) Hit Total Coverage
Test: app.info Lines: 22 22 100.0 %
Date: 2017-10-20 Functions: 26 26 100.0 %

          Line data    Source code
       1             : // Copyright 2016 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #ifndef V8_MANAGED_H_
       6             : #define V8_MANAGED_H_
       7             : 
       8             : #include "src/factory.h"
       9             : #include "src/global-handles.h"
      10             : #include "src/handles.h"
      11             : #include "src/isolate.h"
      12             : 
      13             : namespace v8 {
      14             : namespace internal {
      15             : // An object that wraps a pointer to a C++ object and manages its lifetime.
      16             : // The C++ object will be deleted when the managed wrapper object is
      17             : // garbage collected, or, last resort, if the isolate is torn down before GC,
      18             : // as part of Isolate::Dispose().
      19             : // Managed<CppType> may be used polymorphically as Foreign, where the held
      20             : // address is typed as CppType**. The double indirection is due to the
      21             : // use, by Managed, of Isolate::ManagedObjectFinalizer, which has a CppType*
      22             : // first field.
      23             : // Calling Foreign::set_foreign_address is not allowed on a Managed object.
      24             : template <class CppType>
      25             : class Managed : public Foreign {
      26             :   class FinalizerWithHandle : public Isolate::ManagedObjectFinalizer {
      27             :    public:
      28             :     FinalizerWithHandle(void* value,
      29             :                         Isolate::ManagedObjectFinalizer::Deleter deleter)
      30             :         : Isolate::ManagedObjectFinalizer(value, deleter) {}
      31             : 
      32             :     Object** global_handle_location;
      33             :   };
      34             : 
      35             :  public:
      36             :   V8_INLINE CppType* get() {
      37     4624744 :     return reinterpret_cast<CppType*>(GetFinalizer()->value());
      38             :   }
      39             : 
      40             :   static Managed<CppType>* cast(Object* obj) {
      41             :     SLOW_DCHECK(obj->IsForeign());
      42             :     return reinterpret_cast<Managed<CppType>*>(obj);
      43             :   }
      44             : 
      45             :   // Allocate a new CppType and wrap it in a Managed.
      46             :   template <typename... Args>
      47      213339 :   static Handle<Managed<CppType>> Allocate(Isolate* isolate, Args&&... args) {
      48      213339 :     CppType* ptr = new CppType(std::forward<Args>(args)...);
      49      213339 :     return From(isolate, ptr);
      50             :   }
      51             : 
      52             :   // Create a Managed from an existing CppType*. Takes ownership of the passed
      53             :   // object.
      54      722778 :   static Handle<Managed<CppType>> From(Isolate* isolate, CppType* ptr) {
      55             :     FinalizerWithHandle* finalizer =
      56      361389 :         new FinalizerWithHandle(ptr, &NativeDelete);
      57      361389 :     isolate->RegisterForReleaseAtTeardown(finalizer);
      58             :     Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast(
      59      361389 :         isolate->factory()->NewForeign(reinterpret_cast<Address>(finalizer)));
      60             :     Handle<Object> global_handle = isolate->global_handles()->Create(*handle);
      61      361389 :     finalizer->global_handle_location = global_handle.location();
      62      361389 :     GlobalHandles::MakeWeak(finalizer->global_handle_location,
      63             :                             handle->GetFinalizer(), &Managed<CppType>::GCDelete,
      64             :                             v8::WeakCallbackType::kParameter);
      65             : 
      66      361389 :     return handle;
      67             :   }
      68             : 
      69             :  private:
      70      110264 :   static void GCDelete(const v8::WeakCallbackInfo<void>& data) {
      71             :     FinalizerWithHandle* finalizer =
      72             :         reinterpret_cast<FinalizerWithHandle*>(data.GetParameter());
      73             : 
      74             :     Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate());
      75      110264 :     isolate->UnregisterFromReleaseAtTeardown(finalizer);
      76             : 
      77      110264 :     GlobalHandles::Destroy(finalizer->global_handle_location);
      78       53708 :     NativeDelete(finalizer);
      79      110264 :   }
      80             : 
      81      361249 :   static void NativeDelete(Isolate::ManagedObjectFinalizer* finalizer) {
      82             :     CppType* typed_value = reinterpret_cast<CppType*>(finalizer->value());
      83      361357 :     delete typed_value;
      84             :     FinalizerWithHandle* finalizer_with_handle =
      85             :         static_cast<FinalizerWithHandle*>(finalizer);
      86      361249 :     delete finalizer_with_handle;
      87      304693 :   }
      88             : 
      89     4624744 :   FinalizerWithHandle* GetFinalizer() {
      90     4624744 :     return reinterpret_cast<FinalizerWithHandle*>(foreign_address());
      91             :   }
      92             : };
      93             : }  // namespace internal
      94             : }  // namespace v8
      95             : 
      96             : #endif  // V8_MANAGED_H_

Generated by: LCOV version 1.10