v8/test/unittests/heap/cppgc-js/unified-heap-utils.cc
Anton Bikineev 4ac7982861 unified-young-gen: Support Oilpan tracing from minor MC
The CL adds standalone Oilpan tracing to minor MC. No cross-heap
references are currently processed. In addition, the CL removes
wrapper iteration from Oilpan Minor MC.

Bug: v8:13475
Change-Id: I3a0670e1f3431a3aa723217d5361e4e74f9b0c0f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4027209
Commit-Queue: Anton Bikineev <bikineev@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Reviewed-by: Omer Katz <omerkatz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#84306}
2022-11-16 17:28:45 +00:00

125 lines
4.6 KiB
C++

// Copyright 2020 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.
#include "test/unittests/heap/cppgc-js/unified-heap-utils.h"
#include "include/cppgc/platform.h"
#include "include/v8-cppgc.h"
#include "include/v8-function.h"
#include "src/api/api-inl.h"
#include "src/heap/cppgc-js/cpp-heap.h"
#include "src/heap/heap.h"
#include "src/objects/objects-inl.h"
namespace v8 {
namespace internal {
UnifiedHeapTest::UnifiedHeapTest()
: UnifiedHeapTest(std::vector<std::unique_ptr<cppgc::CustomSpaceBase>>()) {}
UnifiedHeapTest::UnifiedHeapTest(
std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces)
: cpp_heap_(v8::CppHeap::Create(
V8::GetCurrentPlatform(),
CppHeapCreateParams{std::move(custom_spaces),
WrapperHelper::DefaultWrapperDescriptor()})) {
isolate()->heap()->AttachCppHeap(cpp_heap_.get());
}
void UnifiedHeapTest::CollectGarbageWithEmbedderStack(
cppgc::Heap::SweepingType sweeping_type) {
EmbedderStackStateScope stack_scope(
heap(), EmbedderStackStateScope::kExplicitInvocation,
StackState::kMayContainHeapPointers);
CollectGarbage(OLD_SPACE);
if (sweeping_type == cppgc::Heap::SweepingType::kAtomic) {
cpp_heap().AsBase().sweeper().FinishIfRunning();
}
}
void UnifiedHeapTest::CollectGarbageWithoutEmbedderStack(
cppgc::Heap::SweepingType sweeping_type) {
EmbedderStackStateScope stack_scope(
heap(), EmbedderStackStateScope::kExplicitInvocation,
StackState::kNoHeapPointers);
CollectGarbage(OLD_SPACE);
if (sweeping_type == cppgc::Heap::SweepingType::kAtomic) {
cpp_heap().AsBase().sweeper().FinishIfRunning();
}
}
void UnifiedHeapTest::CollectYoungGarbageWithEmbedderStack(
cppgc::Heap::SweepingType sweeping_type) {
EmbedderStackStateScope stack_scope(
heap(), EmbedderStackStateScope::kExplicitInvocation,
StackState::kMayContainHeapPointers);
CollectGarbage(NEW_SPACE);
if (sweeping_type == cppgc::Heap::SweepingType::kAtomic) {
cpp_heap().AsBase().sweeper().FinishIfRunning();
}
}
void UnifiedHeapTest::CollectYoungGarbageWithoutEmbedderStack(
cppgc::Heap::SweepingType sweeping_type) {
EmbedderStackStateScope stack_scope(
heap(), EmbedderStackStateScope::kExplicitInvocation,
StackState::kNoHeapPointers);
CollectGarbage(NEW_SPACE);
if (sweeping_type == cppgc::Heap::SweepingType::kAtomic) {
cpp_heap().AsBase().sweeper().FinishIfRunning();
}
}
CppHeap& UnifiedHeapTest::cpp_heap() const {
return *CppHeap::From(isolate()->heap()->cpp_heap());
}
cppgc::AllocationHandle& UnifiedHeapTest::allocation_handle() {
return cpp_heap().object_allocator();
}
// static
v8::Local<v8::Object> WrapperHelper::CreateWrapper(
v8::Local<v8::Context> context, void* wrappable_type,
void* wrappable_object, const char* class_name) {
v8::EscapableHandleScope scope(context->GetIsolate());
v8::Local<v8::FunctionTemplate> function_t =
v8::FunctionTemplate::New(context->GetIsolate());
if (strlen(class_name) != 0) {
function_t->SetClassName(
v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), class_name)
.ToLocalChecked());
}
v8::Local<v8::ObjectTemplate> instance_t = function_t->InstanceTemplate();
instance_t->SetInternalFieldCount(2);
v8::Local<v8::Function> function =
function_t->GetFunction(context).ToLocalChecked();
v8::Local<v8::Object> instance =
function->NewInstance(context).ToLocalChecked();
SetWrappableConnection(instance, wrappable_type, wrappable_object);
CHECK(!instance.IsEmpty());
i::Handle<i::JSReceiver> js_obj = v8::Utils::OpenHandle(*instance);
CHECK_EQ(i::JS_API_OBJECT_TYPE, js_obj->map().instance_type());
return scope.Escape(instance);
}
// static
void WrapperHelper::ResetWrappableConnection(v8::Local<v8::Object> api_object) {
api_object->SetAlignedPointerInInternalField(kWrappableTypeEmbedderIndex,
nullptr);
api_object->SetAlignedPointerInInternalField(kWrappableInstanceEmbedderIndex,
nullptr);
}
// static
void WrapperHelper::SetWrappableConnection(v8::Local<v8::Object> api_object,
void* type, void* instance) {
api_object->SetAlignedPointerInInternalField(kWrappableTypeEmbedderIndex,
type);
api_object->SetAlignedPointerInInternalField(kWrappableInstanceEmbedderIndex,
instance);
}
} // namespace internal
} // namespace v8