2 * Copyright (C) 2010 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 WrapperTypeInfo_h
32 #define WrapperTypeInfo_h
34 #include "gin/public/wrapper_info.h"
35 #include "heap/Handle.h"
36 #include "wtf/Assertions.h"
41 class ActiveDOMObject;
45 static const int v8DOMWrapperTypeIndex = static_cast<int>(gin::kWrapperInfoIndex);
46 static const int v8DOMWrapperObjectIndex = static_cast<int>(gin::kEncodedValueIndex);
47 static const int v8DefaultWrapperInternalFieldCount = static_cast<int>(gin::kNumberOfInternalFields);
48 static const int v8PrototypeTypeIndex = 0;
49 static const int v8PrototypeInternalFieldcount = 1;
51 static const uint16_t v8DOMNodeClassId = 1;
52 static const uint16_t v8DOMObjectClassId = 2;
54 enum WrapperWorldType {
60 typedef v8::Handle<v8::FunctionTemplate> (*DomTemplateFunction)(v8::Isolate*, WrapperWorldType);
61 typedef void (*DerefObjectFunction)(void*);
62 typedef ActiveDOMObject* (*ToActiveDOMObjectFunction)(v8::Handle<v8::Object>);
63 typedef EventTarget* (*ToEventTargetFunction)(v8::Handle<v8::Object>);
64 typedef void (*ResolveWrapperReachabilityFunction)(void*, const v8::Persistent<v8::Object>&, v8::Isolate*);
65 typedef void (*InstallPerContextEnabledPrototypePropertiesFunction)(v8::Handle<v8::Object>, v8::Isolate*);
67 enum WrapperTypePrototype {
68 WrapperTypeObjectPrototype,
69 WrapperTypeExceptionPrototype
72 inline void setObjectGroup(void* object, const v8::Persistent<v8::Object>& wrapper, v8::Isolate* isolate)
74 isolate->SetObjectGroupId(wrapper, v8::UniqueId(reinterpret_cast<intptr_t>(object)));
77 // This struct provides a way to store a bunch of information that is helpful when unwrapping
78 // v8 objects. Each v8 bindings class has exactly one static WrapperTypeInfo member, so
79 // comparing pointers is a safe way to determine if types match.
80 struct WrapperTypeInfo {
82 static const WrapperTypeInfo* unwrap(v8::Handle<v8::Value> typeInfoWrapper)
84 return reinterpret_cast<const WrapperTypeInfo*>(v8::External::Cast(*typeInfoWrapper)->Value());
88 bool equals(const WrapperTypeInfo* that) const
93 bool isSubclass(const WrapperTypeInfo* that) const
95 for (const WrapperTypeInfo* current = this; current; current = current->parentClass) {
103 v8::Handle<v8::FunctionTemplate> domTemplate(v8::Isolate* isolate, WrapperWorldType worldType) const { return domTemplateFunction(isolate, worldType); }
105 void installPerContextEnabledMethods(v8::Handle<v8::Object> prototypeTemplate, v8::Isolate* isolate) const
107 if (installPerContextEnabledMethodsFunction)
108 installPerContextEnabledMethodsFunction(prototypeTemplate, isolate);
111 ActiveDOMObject* toActiveDOMObject(v8::Handle<v8::Object> object) const
113 if (!toActiveDOMObjectFunction)
115 return toActiveDOMObjectFunction(object);
118 EventTarget* toEventTarget(v8::Handle<v8::Object> object) const
120 if (!toEventTargetFunction)
122 return toEventTargetFunction(object);
125 void visitDOMWrapper(void* object, const v8::Persistent<v8::Object>& wrapper, v8::Isolate* isolate) const
127 if (!visitDOMWrapperFunction)
128 setObjectGroup(object, wrapper, isolate);
130 visitDOMWrapperFunction(object, wrapper, isolate);
133 // This field must be the first member of the struct WrapperTypeInfo. This is also checked by a COMPILE_ASSERT() below.
134 const gin::GinEmbedder ginEmbedder;
136 const DomTemplateFunction domTemplateFunction;
137 const DerefObjectFunction derefObjectFunction;
138 const ToActiveDOMObjectFunction toActiveDOMObjectFunction;
139 const ToEventTargetFunction toEventTargetFunction;
140 const ResolveWrapperReachabilityFunction visitDOMWrapperFunction;
141 const InstallPerContextEnabledPrototypePropertiesFunction installPerContextEnabledMethodsFunction;
142 const WrapperTypeInfo* parentClass;
143 const WrapperTypePrototype wrapperTypePrototype;
144 const bool isGarbageCollected;
148 COMPILE_ASSERT(offsetof(struct WrapperTypeInfo, ginEmbedder) == offsetof(struct gin::WrapperInfo, embedder), wrapper_type_info_compatible_to_gin);
150 template<typename T, int offset>
151 inline T* getInternalField(const v8::Persistent<v8::Object>& persistent)
153 // This would be unsafe, but InternalFieldCount and GetAlignedPointerFromInternalField are guaranteed not to allocate
154 const v8::Handle<v8::Object>& object = reinterpret_cast<const v8::Handle<v8::Object>&>(persistent);
155 ASSERT(offset < object->InternalFieldCount());
156 return static_cast<T*>(object->GetAlignedPointerFromInternalField(offset));
159 template<typename T, int offset>
160 inline T* getInternalField(v8::Handle<v8::Object> wrapper)
162 ASSERT(offset < wrapper->InternalFieldCount());
163 return static_cast<T*>(wrapper->GetAlignedPointerFromInternalField(offset));
166 inline void* toNative(const v8::Persistent<v8::Object>& wrapper)
168 return getInternalField<void, v8DOMWrapperObjectIndex>(wrapper);
171 inline void* toNative(v8::Handle<v8::Object> wrapper)
173 return getInternalField<void, v8DOMWrapperObjectIndex>(wrapper);
176 inline const WrapperTypeInfo* toWrapperTypeInfo(const v8::Persistent<v8::Object>& wrapper)
178 return getInternalField<WrapperTypeInfo, v8DOMWrapperTypeIndex>(wrapper);
181 inline const WrapperTypeInfo* toWrapperTypeInfo(v8::Handle<v8::Object> wrapper)
183 return getInternalField<WrapperTypeInfo, v8DOMWrapperTypeIndex>(wrapper);
186 inline const PersistentNode* toPersistentHandle(const v8::Handle<v8::Object>& wrapper)
188 // Persistent handle is stored in the last internal field.
189 return static_cast<PersistentNode*>(wrapper->GetAlignedPointerFromInternalField(wrapper->InternalFieldCount() - 1));
192 inline void releaseObject(v8::Handle<v8::Object> wrapper)
194 const WrapperTypeInfo* typeInfo = toWrapperTypeInfo(wrapper);
196 if (typeInfo->isGarbageCollected) {
197 const PersistentNode* handle = toPersistentHandle(wrapper);
201 ASSERT(typeInfo->derefObjectFunction);
202 typeInfo->derefObjectFunction(toNative(wrapper));
205 ASSERT(typeInfo->derefObjectFunction);
206 typeInfo->derefObjectFunction(toNative(wrapper));
210 struct WrapperConfiguration {
213 Dependent, Independent
216 void configureWrapper(v8::Persistent<v8::Object>* wrapper) const
218 wrapper->SetWrapperClassId(classId);
219 if (lifetime == Independent)
220 wrapper->MarkIndependent();
223 const uint16_t classId;
224 const Lifetime lifetime;
227 inline WrapperConfiguration buildWrapperConfiguration(void*, WrapperConfiguration::Lifetime lifetime)
229 WrapperConfiguration configuration = {v8DOMObjectClassId, lifetime};
230 return configuration;
233 inline WrapperConfiguration buildWrapperConfiguration(Node*, WrapperConfiguration::Lifetime lifetime)
235 WrapperConfiguration configuration = {v8DOMNodeClassId, lifetime};
236 return configuration;
239 template<class ElementType>
240 class WrapperTypeTraits {
241 // specialized classes have thier own functions, which are generated by binding generator.
245 #endif // WrapperTypeInfo_h