1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "config.h" 6 #include "bindings/core/v8/ScriptWrappable.h" 7 8 #include "bindings/core/v8/DOMDataStore.h" 9 #include "bindings/core/v8/V8DOMWrapper.h" 10 11 namespace blink { 12 13 #if COMPILER(MSVC) 14 __declspec(align(4)) 15 #endif 16 struct SameSizeAsScriptWrappableBase { }; 17 18 COMPILE_ASSERT(sizeof(ScriptWrappableBase) <= sizeof(SameSizeAsScriptWrappableBase), ScriptWrappableBase_should_stay_small); 19 20 struct SameSizeAsScriptWrappable : public ScriptWrappableBase { 21 virtual ~SameSizeAsScriptWrappable() { } 22 v8::Object* m_wrapper; 23 }; 24 25 COMPILE_ASSERT(sizeof(ScriptWrappable) <= sizeof(SameSizeAsScriptWrappable), ScriptWrappable_should_stay_small); 26 27 namespace { 28 29 class ScriptWrappableBaseProtector FINAL { 30 WTF_MAKE_NONCOPYABLE(ScriptWrappableBaseProtector); 31 public: 32 ScriptWrappableBaseProtector(ScriptWrappableBase* scriptWrappableBase, const WrapperTypeInfo* wrapperTypeInfo) 33 : m_scriptWrappableBase(scriptWrappableBase), m_wrapperTypeInfo(wrapperTypeInfo) 34 { 35 m_wrapperTypeInfo->refObject(m_scriptWrappableBase); 36 } 37 ~ScriptWrappableBaseProtector() 38 { 39 m_wrapperTypeInfo->derefObject(m_scriptWrappableBase); 40 } 41 42 private: 43 ScriptWrappableBase* m_scriptWrappableBase; 44 const WrapperTypeInfo* m_wrapperTypeInfo; 45 }; 46 47 } // namespace 48 49 v8::Handle<v8::Object> ScriptWrappable::wrap(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 50 { 51 const WrapperTypeInfo* wrapperTypeInfo = this->wrapperTypeInfo(); 52 53 // It's possible that no one except for the new wrapper owns this object at 54 // this moment, so we have to prevent GC to collect this object until the 55 // object gets associated with the wrapper. 56 ScriptWrappableBaseProtector protect(this, wrapperTypeInfo); 57 58 ASSERT(!DOMDataStore::containsWrapperNonTemplate(this, isolate)); 59 60 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, wrapperTypeInfo, toScriptWrappableBase(), isolate); 61 if (UNLIKELY(wrapper.IsEmpty())) 62 return wrapper; 63 64 wrapperTypeInfo->installConditionallyEnabledProperties(wrapper, isolate); 65 return associateWithWrapper(wrapperTypeInfo, wrapper, isolate); 66 } 67 68 v8::Handle<v8::Object> ScriptWrappable::associateWithWrapper(const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate) 69 { 70 return V8DOMWrapper::associateObjectWithWrapperNonTemplate(this, wrapperTypeInfo, wrapper, isolate); 71 } 72 73 } // namespace blink 74