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.
6 #include "bindings/core/v8/ScriptWrappable.h"
8 #include "bindings/core/v8/DOMDataStore.h"
9 #include "bindings/core/v8/V8DOMWrapper.h"
16 struct SameSizeAsScriptWrappableBase { };
18 COMPILE_ASSERT(sizeof(ScriptWrappableBase) <= sizeof(SameSizeAsScriptWrappableBase), ScriptWrappableBase_should_stay_small);
20 struct SameSizeAsScriptWrappable : public ScriptWrappableBase {
21 virtual ~SameSizeAsScriptWrappable() { }
22 v8::Object* m_wrapper;
25 COMPILE_ASSERT(sizeof(ScriptWrappable) <= sizeof(SameSizeAsScriptWrappable), ScriptWrappable_should_stay_small);
29 class ScriptWrappableBaseProtector final {
30 WTF_MAKE_NONCOPYABLE(ScriptWrappableBaseProtector);
32 ScriptWrappableBaseProtector(ScriptWrappableBase* scriptWrappableBase, const WrapperTypeInfo* wrapperTypeInfo)
33 : m_scriptWrappableBase(scriptWrappableBase), m_wrapperTypeInfo(wrapperTypeInfo)
35 m_wrapperTypeInfo->refObject(m_scriptWrappableBase);
37 ~ScriptWrappableBaseProtector()
39 m_wrapperTypeInfo->derefObject(m_scriptWrappableBase);
43 ScriptWrappableBase* m_scriptWrappableBase;
44 const WrapperTypeInfo* m_wrapperTypeInfo;
49 // ScriptWrappableBase
51 v8::Handle<v8::Object> ScriptWrappableBase::wrap(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
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);
58 ASSERT(!DOMDataStore::containsWrapper(this, isolate));
60 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, wrapperTypeInfo, this, isolate);
61 if (UNLIKELY(wrapper.IsEmpty()))
64 wrapperTypeInfo->installConditionallyEnabledProperties(wrapper, isolate);
65 return V8DOMWrapper::associateObjectWithWrapper(isolate, this, wrapperTypeInfo, wrapper);
70 v8::Handle<v8::Object> ScriptWrappable::wrap(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
72 const WrapperTypeInfo* wrapperTypeInfo = this->wrapperTypeInfo();
74 // It's possible that no one except for the new wrapper owns this object at
75 // this moment, so we have to prevent GC to collect this object until the
76 // object gets associated with the wrapper.
77 ScriptWrappableBaseProtector protect(this, wrapperTypeInfo);
79 ASSERT(!DOMDataStore::containsWrapper(this, isolate));
81 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, wrapperTypeInfo, toScriptWrappableBase(), isolate);
82 if (UNLIKELY(wrapper.IsEmpty()))
85 wrapperTypeInfo->installConditionallyEnabledProperties(wrapper, isolate);
86 return associateWithWrapper(wrapperTypeInfo, wrapper, isolate);
89 v8::Handle<v8::Object> ScriptWrappable::associateWithWrapper(const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate)
91 return V8DOMWrapper::associateObjectWithWrapper(isolate, this, wrapperTypeInfo, wrapper);