| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gin/wrappable.h" | 5 #include "gin/wrappable.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | |
| 8 #include "gin/object_template_builder.h" | 7 #include "gin/object_template_builder.h" |
| 9 #include "gin/per_isolate_data.h" | 8 #include "gin/per_isolate_data.h" |
| 10 | 9 |
| 11 namespace gin { | 10 namespace gin { |
| 12 | 11 |
| 13 WrappableBase::WrappableBase() { | 12 WrappableBase::WrappableBase() { |
| 14 } | 13 } |
| 15 | 14 |
| 16 WrappableBase::~WrappableBase() { | 15 WrappableBase::~WrappableBase() { |
| 17 wrapper_.Reset(); | 16 wrapper_.Reset(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 29 data.SetSecondPassCallback(SecondWeakCallback); | 28 data.SetSecondPassCallback(SecondWeakCallback); |
| 30 } | 29 } |
| 31 | 30 |
| 32 void WrappableBase::SecondWeakCallback( | 31 void WrappableBase::SecondWeakCallback( |
| 33 const v8::WeakCallbackInfo<WrappableBase>& data) { | 32 const v8::WeakCallbackInfo<WrappableBase>& data) { |
| 34 WrappableBase* wrappable = data.GetParameter(); | 33 WrappableBase* wrappable = data.GetParameter(); |
| 35 delete wrappable; | 34 delete wrappable; |
| 36 } | 35 } |
| 37 | 36 |
| 38 v8::Local<v8::Object> WrappableBase::GetWrapperImpl(v8::Isolate* isolate, | 37 v8::Local<v8::Object> WrappableBase::GetWrapperImpl(v8::Isolate* isolate, |
| 39 WrapperInfo* info) { | 38 WrapperInfo* info) { |
| 40 if (!wrapper_.IsEmpty()) { | 39 if (!wrapper_.IsEmpty()) { |
| 41 return v8::Local<v8::Object>::New(isolate, wrapper_); | 40 return v8::Local<v8::Object>::New(isolate, wrapper_); |
| 42 } | 41 } |
| 43 | 42 |
| 44 PerIsolateData* data = PerIsolateData::From(isolate); | 43 PerIsolateData* data = PerIsolateData::From(isolate); |
| 45 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(info); | 44 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(info); |
| 46 if (templ.IsEmpty()) { | 45 if (templ.IsEmpty()) { |
| 47 templ = GetObjectTemplateBuilder(isolate).Build(); | 46 templ = GetObjectTemplateBuilder(isolate).Build(); |
| 48 CHECK(!templ.IsEmpty()); | 47 CHECK(!templ.IsEmpty()); |
| 49 data->SetObjectTemplate(info, templ); | 48 data->SetObjectTemplate(info, templ); |
| 50 } | 49 } |
| 51 CHECK_EQ(kNumberOfInternalFields, templ->InternalFieldCount()); | 50 CHECK_EQ(kNumberOfInternalFields, templ->InternalFieldCount()); |
| 52 v8::Local<v8::Object> wrapper; | 51 v8::Local<v8::Object> wrapper; |
| 53 // |wrapper| may be empty in some extreme cases, e.g., when | 52 // |wrapper| may be empty in some extreme cases, e.g., when |
| 54 // Object.prototype.constructor is overwritten. | 53 // Object.prototype.constructor is overwritten. |
| 55 if (!templ->NewInstance(isolate->GetCurrentContext()).ToLocal(&wrapper)) { | 54 if (!templ->NewInstance(isolate->GetCurrentContext()).ToLocal(&wrapper)) { |
| 56 // The current wrappable object will be no longer managed by V8. Delete this | 55 // The current wrappable object will be no longer managed by V8. Delete this |
| 57 // now. | 56 // now. |
| 58 delete this; | 57 delete this; |
| 59 return wrapper; | 58 return wrapper; |
| 60 } | 59 } |
| 61 wrapper->SetAlignedPointerInInternalField(kWrapperInfoIndex, info); | 60 wrapper->SetAlignedPointerInInternalField(kWrapperInfoIndex, info); |
| 61 // This needs to be adjusted with downcasting on retrieval, as this points |
| 62 // to the WrapperBase object, which may not be the start of the whole object. |
| 62 wrapper->SetAlignedPointerInInternalField(kEncodedValueIndex, this); | 63 wrapper->SetAlignedPointerInInternalField(kEncodedValueIndex, this); |
| 63 wrapper_.Reset(isolate, wrapper); | 64 wrapper_.Reset(isolate, wrapper); |
| 64 wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter); | 65 wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter); |
| 65 return wrapper; | 66 return wrapper; |
| 66 } | 67 } |
| 67 | 68 |
| 68 namespace internal { | |
| 69 | |
| 70 void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val, | |
| 71 WrapperInfo* wrapper_info) { | |
| 72 if (!val->IsObject()) | |
| 73 return NULL; | |
| 74 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(val); | |
| 75 WrapperInfo* info = WrapperInfo::From(obj); | |
| 76 | |
| 77 // If this fails, the object is not managed by Gin. It is either a normal JS | |
| 78 // object that's not wrapping any external C++ object, or it is wrapping some | |
| 79 // C++ object, but that object isn't managed by Gin (maybe Blink). | |
| 80 if (!info) | |
| 81 return NULL; | |
| 82 | |
| 83 // If this fails, the object is managed by Gin, but it's not wrapping an | |
| 84 // instance of the C++ class associated with wrapper_info. | |
| 85 if (info != wrapper_info) | |
| 86 return NULL; | |
| 87 | |
| 88 return obj->GetAlignedPointerFromInternalField(kEncodedValueIndex); | |
| 89 } | |
| 90 | |
| 91 } // namespace internal | |
| 92 | |
| 93 } // namespace gin | 69 } // namespace gin |
| OLD | NEW |