Implement MouseEvent constructor
[WebKit-https.git] / Source / WebCore / bindings / v8 / WrapperTypeInfo.h
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #ifndef WrapperTypeInfo_h
32 #define WrapperTypeInfo_h
33
34 #include <v8.h>
35
36 namespace WebCore {
37     
38     class ActiveDOMObject;
39     class DOMDataStore;
40     class EventTarget;
41
42     static const int v8DOMWrapperTypeIndex = 0;
43     static const int v8DOMWrapperObjectIndex = 1;
44     static const int v8DefaultWrapperInternalFieldCount = 2;
45
46     static const uint16_t v8DOMNodeClassId = 1;
47     static const uint16_t v8DOMObjectClassId = 2;
48
49     typedef v8::Persistent<v8::FunctionTemplate> (*GetTemplateFunction)(v8::Isolate*);
50     typedef void (*DerefObjectFunction)(void*);
51     typedef ActiveDOMObject* (*ToActiveDOMObjectFunction)(v8::Handle<v8::Object>);
52     typedef EventTarget* (*ToEventTargetFunction)(v8::Handle<v8::Object>);
53     typedef void* (*OpaqueRootForGC)(void*, v8::Persistent<v8::Object>);
54     typedef void (*InstallPerContextPrototypePropertiesFunction)(v8::Handle<v8::Object>);
55
56     enum WrapperTypePrototype {
57         WrapperTypeObjectPrototype,
58         WrapperTypeErrorPrototype
59     };
60
61     // This struct provides a way to store a bunch of information that is helpful when unwrapping
62     // v8 objects. Each v8 bindings class has exactly one static WrapperTypeInfo member, so
63     // comparing pointers is a safe way to determine if types match.
64     struct WrapperTypeInfo {
65
66         static WrapperTypeInfo* unwrap(v8::Handle<v8::Value> typeInfoWrapper)
67         {
68             return reinterpret_cast<WrapperTypeInfo*>(v8::External::Cast(*typeInfoWrapper)->Value());
69         }
70         
71         
72         bool equals(const WrapperTypeInfo* that) const
73         {
74             return this == that;
75         }
76
77         bool isSubclass(const WrapperTypeInfo* that) const
78         {
79             for (const WrapperTypeInfo* current = this; current; current = current->parentClass) {
80                 if (current == that)
81                     return true;
82             }
83
84             return false;
85         }
86         
87         v8::Persistent<v8::FunctionTemplate> getTemplate(v8::Isolate* isolate = 0) { return getTemplateFunction(isolate); }
88         
89         void derefObject(void* object)
90         {
91             if (derefObjectFunction) 
92                 derefObjectFunction(object);
93         }
94         
95         void installPerContextPrototypeProperties(v8::Handle<v8::Object> proto)
96         {
97             if (installPerContextPrototypePropertiesFunction)
98                 installPerContextPrototypePropertiesFunction(proto);
99         }
100
101         ActiveDOMObject* toActiveDOMObject(v8::Handle<v8::Object> object)
102         {
103             if (!toActiveDOMObjectFunction)
104                 return 0;
105             return toActiveDOMObjectFunction(object);
106         }
107
108         EventTarget* toEventTarget(v8::Handle<v8::Object> object)
109         {
110             if (!toEventTargetFunction)
111                 return 0;
112             return toEventTargetFunction(object);
113         }
114
115         void* opaqueRootForGC(void* object, v8::Persistent<v8::Object> wrapper)
116         {
117             if (!opaqueRootForGCFunction)
118                 return object;
119             return opaqueRootForGCFunction(object, wrapper);
120         }
121
122         const GetTemplateFunction getTemplateFunction;
123         const DerefObjectFunction derefObjectFunction;
124         const ToActiveDOMObjectFunction toActiveDOMObjectFunction;
125         const ToEventTargetFunction toEventTargetFunction;
126         const OpaqueRootForGC opaqueRootForGCFunction;
127         const InstallPerContextPrototypePropertiesFunction installPerContextPrototypePropertiesFunction;
128         const WrapperTypeInfo* parentClass;
129         const WrapperTypePrototype wrapperTypePrototype;
130     };
131
132     inline void* toNative(v8::Handle<v8::Object> object)
133     {
134         ASSERT(object->InternalFieldCount() >= v8DOMWrapperObjectIndex);
135         return object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectIndex);
136     }
137
138     inline WrapperTypeInfo* toWrapperTypeInfo(v8::Handle<v8::Object> object)
139     {
140         ASSERT(object->InternalFieldCount() >= v8DOMWrapperTypeIndex);
141         return static_cast<WrapperTypeInfo*>(object->GetAlignedPointerFromInternalField(v8DOMWrapperTypeIndex));
142     }
143
144 }
145
146 #endif // WrapperTypeInfo_h