1 /*
2 * Copyright (C) 2006, 2007, 2008, 2009 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 V8Collection_h
32 #define V8Collection_h
33
34 #include "HTMLFormElement.h"
35 #include "HTMLSelectElement.h"
36 #include "V8Binding.h"
37 #include "V8Proxy.h"
38 #include <v8.h>
39
40 namespace WebCore {
41 // FIXME: These functions should be named using to* since they return the item (get* is used for method that take a ref param).
42 // See https://bugs.webkit.org/show_bug.cgi?id=24664.
43
getV8Object(T * implementation)44 template<class T> static v8::Handle<v8::Value> getV8Object(T* implementation)
45 {
46 if (!implementation)
47 return v8::Handle<v8::Value>();
48 return toV8(implementation);
49 }
50
toNativeCollection(v8::Local<v8::Object> object)51 template<class Collection> static Collection* toNativeCollection(v8::Local<v8::Object> object)
52 {
53 return reinterpret_cast<Collection*>(object->GetPointerFromInternalField(v8DOMWrapperObjectIndex));
54 }
55
getV8Object(PassRefPtr<T> implementation)56 template<class T> static v8::Handle<v8::Value> getV8Object(PassRefPtr<T> implementation)
57 {
58 return getV8Object(implementation.get());
59 }
60
61 // Returns named property of a collection.
getNamedPropertyOfCollection(v8::Local<v8::String> name,v8::Local<v8::Object> object)62 template<class Collection, class ItemType> static v8::Handle<v8::Value> getNamedPropertyOfCollection(v8::Local<v8::String> name, v8::Local<v8::Object> object)
63 {
64 // FIXME: assert object is a collection type
65 ASSERT(V8DOMWrapper::maybeDOMWrapper(object));
66 ASSERT(V8DOMWrapper::domWrapperType(object) != V8ClassIndex::NODE);
67 Collection* collection = toNativeCollection<Collection>(object);
68 AtomicString propertyName = toAtomicWebCoreStringWithNullCheck(name);
69 return getV8Object<ItemType>(collection->namedItem(propertyName));
70 }
71
72 // A template of named property accessor of collections.
collectionNamedPropertyGetter(v8::Local<v8::String> name,const v8::AccessorInfo & info)73 template<class Collection, class ItemType> static v8::Handle<v8::Value> collectionNamedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
74 {
75 v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
76
77 if (!value.IsEmpty())
78 return value;
79
80 // Search local callback properties next to find IDL defined
81 // properties.
82 if (info.Holder()->HasRealNamedCallbackProperty(name))
83 return notHandledByInterceptor();
84 return getNamedPropertyOfCollection<Collection, ItemType>(name, info.Holder());
85 }
86
87 // Returns the property at the index of a collection.
getIndexedPropertyOfCollection(uint32_t index,v8::Local<v8::Object> object)88 template<class Collection, class ItemType> static v8::Handle<v8::Value> getIndexedPropertyOfCollection(uint32_t index, v8::Local<v8::Object> object)
89 {
90 // FIXME: Assert that object must be a collection type.
91 ASSERT(V8DOMWrapper::maybeDOMWrapper(object));
92 ASSERT(V8DOMWrapper::domWrapperType(object) != V8ClassIndex::NODE);
93 Collection* collection = toNativeCollection<Collection>(object);
94 return getV8Object<ItemType>(collection->item(index));
95 }
96
97 // A template of index interceptor of collections.
collectionIndexedPropertyGetter(uint32_t index,const v8::AccessorInfo & info)98 template<class Collection, class ItemType> static v8::Handle<v8::Value> collectionIndexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
99 {
100 return getIndexedPropertyOfCollection<Collection, ItemType>(index, info.Holder());
101 }
102
103 // Get an array containing the names of indexed properties of HTMLSelectElement and HTMLFormElement.
nodeCollectionIndexedPropertyEnumerator(const v8::AccessorInfo & info)104 template<class Collection> static v8::Handle<v8::Array> nodeCollectionIndexedPropertyEnumerator(const v8::AccessorInfo& info)
105 {
106 ASSERT(V8DOMWrapper::maybeDOMWrapper(info.Holder()));
107 Collection* collection = toNativeCollection<Collection>(info.Holder());
108 int length = collection->length();
109 v8::Handle<v8::Array> properties = v8::Array::New(length);
110 for (int i = 0; i < length; ++i) {
111 // FIXME: Do we need to check that the item function returns a non-null value for this index?
112 v8::Handle<v8::Integer> integer = v8::Integer::New(i);
113 properties->Set(integer, integer);
114 }
115 return properties;
116 }
117
118 // Get an array containing the names of indexed properties in a collection.
collectionIndexedPropertyEnumerator(const v8::AccessorInfo & info)119 template<class Collection> static v8::Handle<v8::Array> collectionIndexedPropertyEnumerator(const v8::AccessorInfo& info)
120 {
121 ASSERT(V8DOMWrapper::maybeDOMWrapper(info.Holder()));
122 Collection* collection = toNativeCollection<Collection>(info.Holder());
123 int length = collection->length();
124 v8::Handle<v8::Array> properties = v8::Array::New(length);
125 for (int i = 0; i < length; ++i) {
126 // FIXME: Do we need to check that the item function returns a non-null value for this index?
127 v8::Handle<v8::Integer> integer = v8::Integer::New(i);
128 properties->Set(integer, integer);
129 }
130 return properties;
131 }
132
133
134 // A template for indexed getters on collections of strings that should return null if the resulting string is a null string.
collectionStringOrNullIndexedPropertyGetter(uint32_t index,const v8::AccessorInfo & info)135 template<class Collection> static v8::Handle<v8::Value> collectionStringOrNullIndexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
136 {
137 // FIXME: assert that object must be a collection type
138 ASSERT(V8DOMWrapper::maybeDOMWrapper(info.Holder()));
139 Collection* collection = toNativeCollection<Collection>(info.Holder());
140 String result = collection->item(index);
141 return v8StringOrNull(result);
142 }
143
144
145 // A template for indexed getters on collections of strings.
collectionStringIndexedPropertyGetter(uint32_t index,const v8::AccessorInfo & info)146 template<class Collection> static v8::Handle<v8::Value> collectionStringIndexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
147 {
148 // FIXME: assert that object must be a collection type
149 ASSERT(V8DOMWrapper::maybeDOMWrapper(info.Holder()));
150 Collection* collection = toNativeCollection<Collection>(info.Holder());
151 String result = collection->item(index);
152 return v8String(result);
153 }
154
155
156 // Add indexed getter to the function template for a collection.
setCollectionIndexedGetter(v8::Handle<v8::FunctionTemplate> desc,V8ClassIndex::V8WrapperType type)157 template<class Collection, class ItemType> static void setCollectionIndexedGetter(v8::Handle<v8::FunctionTemplate> desc, V8ClassIndex::V8WrapperType type)
158 {
159 desc->InstanceTemplate()->SetIndexedPropertyHandler(collectionIndexedPropertyGetter<Collection, ItemType>, 0, 0, 0, collectionIndexedPropertyEnumerator<Collection>,
160 v8::Integer::New(V8ClassIndex::ToInt(type)));
161 }
162
163
164 // Add named getter to the function template for a collection.
setCollectionNamedGetter(v8::Handle<v8::FunctionTemplate> desc,V8ClassIndex::V8WrapperType type)165 template<class Collection, class ItemType> static void setCollectionNamedGetter(v8::Handle<v8::FunctionTemplate> desc, V8ClassIndex::V8WrapperType type)
166 {
167 desc->InstanceTemplate()->SetNamedPropertyHandler(collectionNamedPropertyGetter<Collection, ItemType>, 0, 0, 0, 0, v8::Integer::New(V8ClassIndex::ToInt(type)));
168 }
169
170 // Add indexed getter returning a string or null to a function template for a collection.
setCollectionStringOrNullIndexedGetter(v8::Handle<v8::FunctionTemplate> desc)171 template<class Collection> static void setCollectionStringOrNullIndexedGetter(v8::Handle<v8::FunctionTemplate> desc)
172 {
173 desc->InstanceTemplate()->SetIndexedPropertyHandler(collectionStringOrNullIndexedPropertyGetter<Collection>, 0, 0, 0, collectionIndexedPropertyEnumerator<Collection>);
174 }
175
176
177 // Add indexed getter returning a string to a function template for a collection.
setCollectionStringIndexedGetter(v8::Handle<v8::FunctionTemplate> desc)178 template<class Collection> static void setCollectionStringIndexedGetter(v8::Handle<v8::FunctionTemplate> desc)
179 {
180 desc->InstanceTemplate()->SetIndexedPropertyHandler(collectionStringIndexedPropertyGetter<Collection>, 0, 0, 0, collectionIndexedPropertyEnumerator<Collection>);
181 }
182
183 v8::Handle<v8::Value> toOptionsCollectionSetter(uint32_t index, v8::Handle<v8::Value>, HTMLSelectElement*);
184
185 } // namespace WebCore
186
187 #endif // V8Collection_h
188