1 // Copyright 2018 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 #include "src/objects/managed.h"
6 
7 namespace v8 {
8 namespace internal {
9 
10 namespace {
11 // Called by the GC in its second pass when a Managed<CppType> is
12 // garbage collected.
ManagedObjectFinalizerSecondPass(const v8::WeakCallbackInfo<void> & data)13 void ManagedObjectFinalizerSecondPass(const v8::WeakCallbackInfo<void>& data) {
14   auto destructor =
15       reinterpret_cast<ManagedPtrDestructor*>(data.GetParameter());
16   int64_t adjustment = 0 - static_cast<int64_t>(destructor->estimated_size_);
17   destructor->destructor_(destructor->shared_ptr_ptr_);
18   delete destructor;
19   data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory(adjustment);
20 }
21 }  // namespace
22 
23 // Called by the GC in its first pass when a Managed<CppType> is
24 // garbage collected.
ManagedObjectFinalizer(const v8::WeakCallbackInfo<void> & data)25 void ManagedObjectFinalizer(const v8::WeakCallbackInfo<void>& data) {
26   auto destructor =
27       reinterpret_cast<ManagedPtrDestructor*>(data.GetParameter());
28   GlobalHandles::Destroy(destructor->global_handle_location_);
29   Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate());
30   isolate->UnregisterManagedPtrDestructor(destructor);
31   // We need to do the main work as a second pass callback because
32   // it can trigger garbage collection. The first pass callbacks
33   // are not allowed to invoke V8 API.
34   data.SetSecondPassCallback(&ManagedObjectFinalizerSecondPass);
35 }
36 
37 }  // namespace internal
38 }  // namespace v8
39