1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/wrappable.h"
7 #include "base/check_op.h"
8 #include "gin/object_template_builder.h"
9 #include "gin/per_isolate_data.h"
13 WrappableBase::WrappableBase() = default;
15 WrappableBase::~WrappableBase() {
19 ObjectTemplateBuilder WrappableBase::GetObjectTemplateBuilder(
20 v8::Isolate* isolate) {
21 return ObjectTemplateBuilder(isolate, GetTypeName());
24 const char* WrappableBase::GetTypeName() {
28 void WrappableBase::FirstWeakCallback(
29 const v8::WeakCallbackInfo<WrappableBase>& data) {
30 WrappableBase* wrappable = data.GetParameter();
31 wrappable->dead_ = true;
32 wrappable->wrapper_.Reset();
33 data.SetSecondPassCallback(SecondWeakCallback);
36 void WrappableBase::SecondWeakCallback(
37 const v8::WeakCallbackInfo<WrappableBase>& data) {
38 WrappableBase* wrappable = data.GetParameter();
42 v8::MaybeLocal<v8::Object> WrappableBase::GetWrapperImpl(v8::Isolate* isolate,
44 if (!wrapper_.IsEmpty()) {
45 return v8::MaybeLocal<v8::Object>(
46 v8::Local<v8::Object>::New(isolate, wrapper_));
50 return v8::MaybeLocal<v8::Object>();
52 PerIsolateData* data = PerIsolateData::From(isolate);
53 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(info);
54 if (templ.IsEmpty()) {
55 templ = GetObjectTemplateBuilder(isolate).Build();
56 CHECK(!templ.IsEmpty());
57 data->SetObjectTemplate(info, templ);
59 CHECK_EQ(kNumberOfInternalFields, templ->InternalFieldCount());
60 v8::Local<v8::Object> wrapper;
61 // |wrapper| may be empty in some extreme cases, e.g., when
62 // Object.prototype.constructor is overwritten.
63 if (!templ->NewInstance(isolate->GetCurrentContext()).ToLocal(&wrapper)) {
64 // The current wrappable object will be no longer managed by V8. Delete this
67 return v8::MaybeLocal<v8::Object>(wrapper);
70 int indices[] = {kWrapperInfoIndex, kEncodedValueIndex};
71 void* values[] = {info, this};
72 wrapper->SetAlignedPointerInInternalFields(2, indices, values);
73 wrapper_.Reset(isolate, wrapper);
74 wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter);
75 return v8::MaybeLocal<v8::Object>(wrapper);
80 void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val,
81 WrapperInfo* wrapper_info) {
84 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(val);
85 WrapperInfo* info = WrapperInfo::From(obj);
87 // If this fails, the object is not managed by Gin. It is either a normal JS
88 // object that's not wrapping any external C++ object, or it is wrapping some
89 // C++ object, but that object isn't managed by Gin (maybe Blink).
93 // If this fails, the object is managed by Gin, but it's not wrapping an
94 // instance of the C++ class associated with wrapper_info.
95 if (info != wrapper_info)
98 return obj->GetAlignedPointerFromInternalField(kEncodedValueIndex);
101 } // namespace internal