2021-02-11 19:03:35 +01: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.
|
|
|
|
|
2022-01-29 08:33:07 +01:00
|
|
|
#include "test/unittests/heap/cppgc-js/unified-heap-utils.h"
|
2021-02-11 19:03:35 +01:00
|
|
|
|
|
|
|
#include "include/cppgc/platform.h"
|
2021-02-24 14:47:06 +01:00
|
|
|
#include "include/v8-cppgc.h"
|
2021-10-10 11:10:43 +02:00
|
|
|
#include "include/v8-function.h"
|
2021-02-11 19:03:35 +01:00
|
|
|
#include "src/api/api-inl.h"
|
|
|
|
#include "src/heap/cppgc-js/cpp-heap.h"
|
2021-02-24 14:47:06 +01:00
|
|
|
#include "src/heap/heap.h"
|
2024-08-14 20:41:00 +02:00
|
|
|
#include "src/objects/js-objects.h"
|
2021-02-11 19:03:35 +01:00
|
|
|
#include "src/objects/objects-inl.h"
|
2023-03-30 12:11:08 +02:00
|
|
|
#include "test/unittests/heap/heap-utils.h"
|
2021-02-11 19:03:35 +01:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Local<v8::Object> WrapperHelper::CreateWrapper(
|
2024-08-14 20:41:00 +02:00
|
|
|
v8::Local<v8::Context> context, void* wrappable_object,
|
|
|
|
const char* class_name) {
|
2021-02-11 19:03:35 +01:00
|
|
|
v8::EscapableHandleScope scope(context->GetIsolate());
|
|
|
|
v8::Local<v8::FunctionTemplate> function_t =
|
|
|
|
v8::FunctionTemplate::New(context->GetIsolate());
|
2024-08-14 20:41:00 +02:00
|
|
|
if (class_name && strlen(class_name) != 0) {
|
2021-02-11 19:03:35 +01:00
|
|
|
function_t->SetClassName(
|
|
|
|
v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), class_name)
|
|
|
|
.ToLocalChecked());
|
|
|
|
}
|
|
|
|
v8::Local<v8::Function> function =
|
|
|
|
function_t->GetFunction(context).ToLocalChecked();
|
|
|
|
v8::Local<v8::Object> instance =
|
|
|
|
function->NewInstance(context).ToLocalChecked();
|
2024-08-14 20:41:00 +02:00
|
|
|
SetWrappableConnection(context->GetIsolate(), instance, wrappable_object);
|
2021-02-11 19:03:35 +01:00
|
|
|
CHECK(!instance.IsEmpty());
|
2024-08-14 20:41:00 +02:00
|
|
|
CHECK_EQ(wrappable_object,
|
|
|
|
ReadWrappablePointer(context->GetIsolate(), instance));
|
|
|
|
i::DirectHandle<i::JSReceiver> js_obj =
|
|
|
|
v8::Utils::OpenDirectHandle(*instance);
|
2023-10-05 08:46:07 +02:00
|
|
|
CHECK_EQ(i::JS_API_OBJECT_TYPE, js_obj->map()->instance_type());
|
2021-02-11 19:03:35 +01:00
|
|
|
return scope.Escape(instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2024-08-14 20:41:00 +02:00
|
|
|
void WrapperHelper::ResetWrappableConnection(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> api_object) {
|
|
|
|
i::DirectHandle<i::JSReceiver> js_obj =
|
|
|
|
v8::Utils::OpenDirectHandle(*api_object);
|
|
|
|
JSApiWrapper(Cast<JSObject>(*js_obj))
|
|
|
|
.SetCppHeapWrappable<CppHeapPointerTag::kDefaultTag>(
|
|
|
|
reinterpret_cast<i::Isolate*>(isolate), nullptr);
|
2021-03-12 08:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2024-08-14 20:41:00 +02:00
|
|
|
void WrapperHelper::SetWrappableConnection(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> api_object,
|
|
|
|
void* instance) {
|
|
|
|
i::DirectHandle<i::JSReceiver> js_obj =
|
|
|
|
v8::Utils::OpenDirectHandle(*api_object);
|
|
|
|
JSApiWrapper(Cast<JSObject>(*js_obj))
|
|
|
|
.SetCppHeapWrappable<CppHeapPointerTag::kDefaultTag>(
|
|
|
|
reinterpret_cast<i::Isolate*>(isolate), instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void* WrapperHelper::ReadWrappablePointer(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> api_object) {
|
|
|
|
i::DirectHandle<i::JSReceiver> js_obj =
|
|
|
|
v8::Utils::OpenDirectHandle(*api_object);
|
|
|
|
return JSApiWrapper(Cast<JSObject>(*js_obj))
|
|
|
|
.GetCppHeapWrappable(reinterpret_cast<i::Isolate*>(isolate),
|
|
|
|
kAnyCppHeapPointer);
|
2021-02-11 19:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|