2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef V8DOMWrapper_h
32 #define V8DOMWrapper_h
34 #include "bindings/v8/DOMDataStore.h"
36 #include "wtf/PassRefPtr.h"
37 #include "wtf/RawPtr.h"
38 #include "wtf/text/AtomicString.h"
42 struct WrapperTypeInfo;
46 static v8::Local<v8::Object> createWrapper(v8::Handle<v8::Object> creationContext, const WrapperTypeInfo*, void*, v8::Isolate*);
48 template<typename V8T, typename T>
49 static inline v8::Handle<v8::Object> associateObjectWithWrapper(PassRefPtr<T>, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
50 template<typename V8T, typename T>
51 static inline v8::Handle<v8::Object> associateObjectWithWrapper(RawPtr<T>, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
52 template<typename V8T, typename T>
53 static inline v8::Handle<v8::Object> associateObjectWithWrapper(T*, const WrapperTypeInfo*, v8::Handle<v8::Object>, v8::Isolate*, WrapperConfiguration::Lifetime);
54 static inline void setNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*);
55 static inline void setNativeInfoWithPersistentHandle(v8::Handle<v8::Object>, const WrapperTypeInfo*, void*, PersistentNode*);
56 static inline void clearNativeInfo(v8::Handle<v8::Object>, const WrapperTypeInfo*);
58 static bool isDOMWrapper(v8::Handle<v8::Value>);
61 inline void V8DOMWrapper::setNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type, void* object)
63 ASSERT(wrapper->InternalFieldCount() >= 2);
67 ASSERT(!type->isGarbageCollected);
69 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
70 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
73 inline void V8DOMWrapper::setNativeInfoWithPersistentHandle(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type, void* object, PersistentNode* handle)
75 ASSERT(wrapper->InternalFieldCount() >= 3);
78 ASSERT(type->isGarbageCollected);
79 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, object);
80 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
81 // Persistent handle is stored in the last internal field.
82 wrapper->SetAlignedPointerInInternalField(wrapper->InternalFieldCount() - 1, handle);
85 inline void V8DOMWrapper::clearNativeInfo(v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* type)
87 ASSERT(wrapper->InternalFieldCount() >= 2);
89 // clearNativeInfo() is used only by NP objects, which are not garbage collected.
90 ASSERT(!type->isGarbageCollected);
91 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperTypeIndex, const_cast<WrapperTypeInfo*>(type));
92 wrapper->SetAlignedPointerInInternalField(v8DOMWrapperObjectIndex, 0);
95 template<typename V8T, typename T>
96 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(PassRefPtr<T> object, const WrapperTypeInfo* type, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
98 setNativeInfo(wrapper, type, V8T::toInternalPointer(object.get()));
99 ASSERT(isDOMWrapper(wrapper));
100 WrapperConfiguration configuration = buildWrapperConfiguration(object.get(), lifetime);
101 DOMDataStore::setWrapper<V8T>(object.leakRef(), wrapper, isolate, configuration);
105 template<typename V8T, typename T>
106 inline v8::Handle<v8::Object> V8DOMWrapper::associateObjectWithWrapper(RawPtr<T> object, const WrapperTypeInfo* type, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, WrapperConfiguration::Lifetime lifetime)
108 setNativeInfoWithPersistentHandle(wrapper, type, V8T::toInternalPointer(object.get()), new Persistent<T>(object));
109 ASSERT(isDOMWrapper(wrapper));
110 WrapperConfiguration configuration = buildWrapperConfiguration(object.get(), lifetime);
111 DOMDataStore::setWrapper<V8T>(object.get(), wrapper, isolate, configuration);
115 class V8WrapperInstantiationScope {
117 V8WrapperInstantiationScope(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
118 : m_didEnterContext(false)
119 , m_context(isolate->GetCurrentContext())
121 // FIXME: Remove all empty creationContexts from caller sites.
122 // If a creationContext is empty, we will end up creating a new object
123 // in the context currently entered. This is wrong.
124 if (creationContext.IsEmpty())
126 v8::Handle<v8::Context> contextForWrapper = creationContext->CreationContext();
127 // For performance, we enter the context only if the currently running context
128 // is different from the context that we are about to enter.
129 if (contextForWrapper == m_context)
131 m_context = v8::Local<v8::Context>::New(isolate, contextForWrapper);
132 m_didEnterContext = true;
136 ~V8WrapperInstantiationScope()
138 if (!m_didEnterContext)
143 v8::Handle<v8::Context> context() const { return m_context; }
146 bool m_didEnterContext;
147 v8::Handle<v8::Context> m_context;
152 #endif // V8DOMWrapper_h