2020-10-22 07:02:17 +00:00
|
|
|
// 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/unified-heap-utils.h"
|
|
|
|
|
2020-10-28 08:13:33 +00:00
|
|
|
#include "include/cppgc/platform.h"
|
2020-11-15 10:36:09 +00:00
|
|
|
#include "include/v8-cppgc.h"
|
2020-10-22 07:02:17 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2020-10-28 08:13:33 +00:00
|
|
|
#include "src/heap/cppgc-js/cpp-heap.h"
|
2020-11-15 10:36:09 +00:00
|
|
|
#include "src/heap/heap.h"
|
2020-10-22 07:02:17 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2020-10-28 08:13:33 +00:00
|
|
|
UnifiedHeapTest::UnifiedHeapTest()
|
|
|
|
: saved_incremental_marking_wrappers_(FLAG_incremental_marking_wrappers) {
|
|
|
|
FLAG_incremental_marking_wrappers = false;
|
2020-11-15 10:36:09 +00:00
|
|
|
isolate()->heap()->ConfigureCppHeap(std::make_unique<CppHeapCreateParams>());
|
2020-10-28 08:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnifiedHeapTest::~UnifiedHeapTest() {
|
|
|
|
FLAG_incremental_marking_wrappers = saved_incremental_marking_wrappers_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnifiedHeapTest::CollectGarbageWithEmbedderStack() {
|
|
|
|
heap()->SetEmbedderStackStateForNextFinalization(
|
|
|
|
EmbedderHeapTracer::EmbedderStackState::kMayContainHeapPointers);
|
|
|
|
CollectGarbage(OLD_SPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnifiedHeapTest::CollectGarbageWithoutEmbedderStack() {
|
|
|
|
heap()->SetEmbedderStackStateForNextFinalization(
|
|
|
|
EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers);
|
|
|
|
CollectGarbage(OLD_SPACE);
|
|
|
|
}
|
|
|
|
|
2020-11-15 10:36:09 +00:00
|
|
|
CppHeap& UnifiedHeapTest::cpp_heap() const {
|
|
|
|
return *CppHeap::From(isolate()->heap()->cpp_heap());
|
|
|
|
}
|
2020-10-28 08:13:33 +00:00
|
|
|
|
|
|
|
cppgc::AllocationHandle& UnifiedHeapTest::allocation_handle() {
|
|
|
|
return cpp_heap().object_allocator();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Local<v8::Object> WrapperHelper::CreateWrapper(
|
|
|
|
v8::Local<v8::Context> context, void* wrappable_object,
|
|
|
|
const char* class_name) {
|
2020-10-22 07:02:17 +00:00
|
|
|
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();
|
2020-10-28 08:13:33 +00:00
|
|
|
instance->SetAlignedPointerInInternalField(0, wrappable_object);
|
|
|
|
instance->SetAlignedPointerInInternalField(1, wrappable_object);
|
2020-10-22 07:02:17 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-28 08:13:33 +00:00
|
|
|
// static
|
|
|
|
void WrapperHelper::ResetWrappableConnection(v8::Local<v8::Object> api_object) {
|
|
|
|
api_object->SetAlignedPointerInInternalField(0, nullptr);
|
|
|
|
api_object->SetAlignedPointerInInternalField(1, nullptr);
|
|
|
|
}
|
|
|
|
|
2020-10-22 07:02:17 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|