#ifndef GIN_WRAPPABLE_H_
#define GIN_WRAPPABLE_H_
#include <type_traits>
#include "gin/converter.h"
#include "gin/gin_export.h"
#include "gin/public/wrapper_info.h"
#include "v8/include/cppgc/garbage-collected.h"
#include "v8/include/v8-sandbox.h"
namespace gin {
class NamedPropertyInterceptor;
namespace internal {
static_assert(sizeof(v8::CppHeapPointerTag) == sizeof(WrappablePointerTag),
"WrappablePointerTag defines a subrange of v8::CppHeapPointerTag "
"which is used for subclasses of gin::Wrappable. They should "
"therefore have the same underlying type.");
GIN_EXPORT void* FromV8Impl(v8::Isolate* isolate,
v8::Local<v8::Value> val,
DeprecatedWrapperInfo* info);
}
class ObjectTemplateBuilder;
class GIN_EXPORT WrappableBase : public v8::Object::Wrappable {
public:
WrappableBase(const WrappableBase&) = delete;
WrappableBase& operator=(const WrappableBase&) = delete;
void Trace(cppgc::Visitor*) const override;
virtual const WrapperInfo* wrapper_info() const = 0;
const v8::Object::WrapperTypeInfo* GetWrapperTypeInfo() const override;
virtual NamedPropertyInterceptor* GetNamedPropertyInterceptor();
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate);
void SetWrapper(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
protected:
WrappableBase() = default;
virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate);
private:
void AssociateWithWrapper(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper);
v8::TracedReference<v8::Object> wrapper_;
};
template <typename T>
class Wrappable : public WrappableBase {
public:
Wrappable(const Wrappable&) = delete;
Wrappable& operator=(const Wrappable&) = delete;
protected:
Wrappable() = default;
};
template <typename T>
requires(std::is_convertible_v<T*, WrappableBase*>)
struct Converter<T*> {
static v8::MaybeLocal<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
if (val == nullptr) {
return v8::Null(isolate);
}
v8::Local<v8::Object> wrapper;
if (!val->GetWrapper(isolate).ToLocal(&wrapper)) {
return v8::MaybeLocal<v8::Value>();
}
return v8::MaybeLocal<v8::Value>(wrapper);
}
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) {
if (!val->IsObject()) {
*out = nullptr;
return false;
}
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(val);
if (!obj->IsApiWrapper()) {
*out = nullptr;
return false;
}
auto tag = static_cast<v8::CppHeapPointerTag>(T::kWrapperInfo.pointer_tag);
v8::Object::Wrappable* wrappable =
v8::Object::Unwrap<v8::Object::Wrappable>(isolate, obj, {tag, tag});
if (!wrappable) {
*out = nullptr;
return false;
}
if (wrappable->GetWrapperTypeInfo() != &T::kWrapperInfo) {
*out = nullptr;
return false;
}
*out = static_cast<T*>(wrappable);
return *out != nullptr;
}
};
class GIN_EXPORT DeprecatedWrappableBase {
public:
DeprecatedWrappableBase(const DeprecatedWrappableBase&) = delete;
DeprecatedWrappableBase& operator=(const DeprecatedWrappableBase&) = delete;
protected:
DeprecatedWrappableBase();
virtual ~DeprecatedWrappableBase();
virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate);
virtual const char* GetTypeName();
v8::MaybeLocal<v8::Object> GetWrapperImpl(
v8::Isolate* isolate,
DeprecatedWrapperInfo* wrapper_info);
private:
static void FirstWeakCallback(
const v8::WeakCallbackInfo<DeprecatedWrappableBase>& data);
static void SecondWeakCallback(
const v8::WeakCallbackInfo<DeprecatedWrappableBase>& data);
bool dead_ = false;
v8::Global<v8::Object> wrapper_;
};
template <typename T>
class DeprecatedWrappable : public DeprecatedWrappableBase {
public:
DeprecatedWrappable(const DeprecatedWrappable&) = delete;
DeprecatedWrappable& operator=(const DeprecatedWrappable&) = delete;
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate) {
return GetWrapperImpl(isolate, &T::kWrapperInfo);
}
protected:
DeprecatedWrappable() = default;
~DeprecatedWrappable() override = default;
};
template <typename T>
requires(std::is_convertible_v<T*, DeprecatedWrappableBase*>)
struct Converter<T*> {
static v8::MaybeLocal<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
if (val == nullptr) {
return v8::Null(isolate);
}
v8::Local<v8::Object> wrapper;
if (!val->GetWrapper(isolate).ToLocal(&wrapper)) {
return v8::MaybeLocal<v8::Value>();
}
return v8::MaybeLocal<v8::Value>(wrapper);
}
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) {
*out = static_cast<T*>(static_cast<DeprecatedWrappableBase*>(
internal::FromV8Impl(isolate, val, &T::kWrapperInfo)));
return *out != NULL;
}
};
}
#endif