// Copyright 2021 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. #ifndef V8_OBJECTS_MANAGED_INL_H_ #define V8_OBJECTS_MANAGED_INL_H_ #include "src/handles/global-handles-inl.h" #include "src/objects/managed.h" namespace v8 { namespace internal { // static template template Handle> Managed::Allocate(Isolate* isolate, size_t estimated_size, Args&&... args) { return FromSharedPtr(isolate, estimated_size, std::make_shared(std::forward(args)...)); } // static template Handle> Managed::FromRawPtr(Isolate* isolate, size_t estimated_size, CppType* ptr) { return FromSharedPtr(isolate, estimated_size, std::shared_ptr{ptr}); } // static template Handle> Managed::FromUniquePtr( Isolate* isolate, size_t estimated_size, std::unique_ptr unique_ptr) { return FromSharedPtr(isolate, estimated_size, std::move(unique_ptr)); } // static template Handle> Managed::FromSharedPtr( Isolate* isolate, size_t estimated_size, std::shared_ptr shared_ptr) { reinterpret_cast(isolate) ->AdjustAmountOfExternalAllocatedMemory(estimated_size); auto destructor = new ManagedPtrDestructor( estimated_size, new std::shared_ptr{std::move(shared_ptr)}, Destructor); Handle> handle = Handle>::cast( isolate->factory()->NewForeign(reinterpret_cast
(destructor))); Handle global_handle = isolate->global_handles()->Create(*handle); destructor->global_handle_location_ = global_handle.location(); GlobalHandles::MakeWeak(destructor->global_handle_location_, destructor, &ManagedObjectFinalizer, v8::WeakCallbackType::kParameter); isolate->RegisterManagedPtrDestructor(destructor); return handle; } } // namespace internal } // namespace v8 #endif // V8_OBJECTS_MANAGED_INL_H_