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