managed.h 3.83 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_MANAGED_H_
#define V8_MANAGED_H_
7 8 9

#include "src/global-handles.h"
#include "src/handles.h"
10
#include "src/heap/factory.h"
11 12 13 14 15 16 17 18 19 20 21 22
#include "src/isolate.h"

namespace v8 {
namespace internal {
// An object that wraps a pointer to a C++ object and manages its lifetime.
// The C++ object will be deleted when the managed wrapper object is
// garbage collected, or, last resort, if the isolate is torn down before GC,
// as part of Isolate::Dispose().
// Managed<CppType> may be used polymorphically as Foreign, where the held
// address is typed as CppType**. The double indirection is due to the
// use, by Managed, of Isolate::ManagedObjectFinalizer, which has a CppType*
// first field.
23
// Calling Foreign::set_foreign_address is not allowed on a Managed object.
24 25
template <class CppType>
class Managed : public Foreign {
26 27 28 29 30 31 32 33 34
  class FinalizerWithHandle : public Isolate::ManagedObjectFinalizer {
   public:
    FinalizerWithHandle(void* value,
                        Isolate::ManagedObjectFinalizer::Deleter deleter)
        : Isolate::ManagedObjectFinalizer(value, deleter) {}

    Object** global_handle_location;
  };

35 36
 public:
  V8_INLINE CppType* get() {
37
    return reinterpret_cast<CppType*>(GetFinalizer()->value());
38 39 40 41 42 43 44
  }

  static Managed<CppType>* cast(Object* obj) {
    SLOW_DCHECK(obj->IsForeign());
    return reinterpret_cast<Managed<CppType>*>(obj);
  }

45 46 47 48 49 50 51 52 53 54
  // Allocate a new CppType and wrap it in a Managed.
  template <typename... Args>
  static Handle<Managed<CppType>> Allocate(Isolate* isolate, Args&&... args) {
    CppType* ptr = new CppType(std::forward<Args>(args)...);
    return From(isolate, ptr);
  }

  // Create a Managed from an existing CppType*. Takes ownership of the passed
  // object.
  static Handle<Managed<CppType>> From(Isolate* isolate, CppType* ptr) {
55 56 57
    FinalizerWithHandle* finalizer =
        new FinalizerWithHandle(ptr, &NativeDelete);
    isolate->RegisterForReleaseAtTeardown(finalizer);
58
    Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast(
59 60 61
        isolate->factory()->NewForeign(reinterpret_cast<Address>(finalizer)));
    Handle<Object> global_handle = isolate->global_handles()->Create(*handle);
    finalizer->global_handle_location = global_handle.location();
62 63 64
    GlobalHandles::MakeWeak(
        finalizer->global_handle_location, handle->GetFinalizer(),
        &ResetWeakAndScheduleGCDelete, v8::WeakCallbackType::kParameter);
65

66 67 68 69
    return handle;
  }

 private:
70 71
  static void ResetWeakAndScheduleGCDelete(
      const v8::WeakCallbackInfo<void>& data) {
72 73
    FinalizerWithHandle* finalizer =
        reinterpret_cast<FinalizerWithHandle*>(data.GetParameter());
74
    GlobalHandles::Destroy(finalizer->global_handle_location);
75
    Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate());
76
    isolate->UnregisterFromReleaseAtTeardown(finalizer);
77 78 79 80 81
    // We need to call GCDelete as a second pass callback because
    // it can trigger garbage collection. The first pass callbacks
    // are not allowed to invoke V8 API.
    data.SetSecondPassCallback(&GCDelete);
  }
82

83 84 85
  static void GCDelete(const v8::WeakCallbackInfo<void>& data) {
    FinalizerWithHandle* finalizer =
        reinterpret_cast<FinalizerWithHandle*>(data.GetParameter());
86
    NativeDelete(finalizer);
87 88
  }

89 90
  static void NativeDelete(Isolate::ManagedObjectFinalizer* finalizer) {
    CppType* typed_value = reinterpret_cast<CppType*>(finalizer->value());
91
    delete typed_value;
92 93 94
    FinalizerWithHandle* finalizer_with_handle =
        static_cast<FinalizerWithHandle*>(finalizer);
    delete finalizer_with_handle;
95 96
  }

97 98
  FinalizerWithHandle* GetFinalizer() {
    return reinterpret_cast<FinalizerWithHandle*>(foreign_address());
99 100 101 102 103
  }
};
}  // namespace internal
}  // namespace v8

104
#endif  // V8_MANAGED_H_