| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // Copied from base/basictypes.h with some modifications | 4 // Copied from base/basictypes.h with some modifications |
| 5 | 5 |
| 6 #ifndef V8_PROPERTY_H__ | 6 #ifndef V8_PROPERTY_H__ |
| 7 #define V8_PROPERTY_H__ | 7 #define V8_PROPERTY_H__ |
| 8 | 8 |
| 9 #include <v8.h> | 9 #include <v8.h> |
| 10 #include "v8_proxy.h" | 10 #include "v8_proxy.h" |
| 11 | 11 |
| 12 namespace WebCore { | 12 namespace WebCore { |
| 13 | 13 |
| 14 | |
| 15 // Returns named property of a collection. | 14 // Returns named property of a collection. |
| 16 template <class C> | 15 template <class C> |
| 17 static v8::Handle<v8::Value> GetNamedPropertyOfCollection( | 16 static v8::Handle<v8::Value> GetNamedPropertyOfCollection( |
| 18 v8::Local<v8::String> name, | 17 v8::Local<v8::String> name, |
| 19 v8::Local<v8::Object> object, | 18 v8::Local<v8::Object> object, |
| 20 v8::Local<v8::Value> data) { | 19 v8::Local<v8::Value> data) { |
| 21 // TODO: assert object is a collection type | 20 // TODO: assert object is a collection type |
| 22 ASSERT(V8Proxy::MaybeDOMWrapper(object)); | 21 ASSERT(V8Proxy::MaybeDOMWrapper(object)); |
| 23 | |
| 24 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(object); | 22 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(object); |
| 25 C* collection = V8Proxy::FastToNativeObject<C>(t, object); | 23 ASSERT(t != V8ClassIndex::NODE); |
| 24 C* collection = V8Proxy::ToNativeObject<C>(t, object); |
| 26 String prop_name = ToWebCoreString(name); | 25 String prop_name = ToWebCoreString(name); |
| 27 void* result = collection->namedItem(prop_name); | 26 void* result = collection->namedItem(prop_name); |
| 28 if (!result) return v8::Handle<v8::Value>(); | 27 if (!result) return v8::Handle<v8::Value>(); |
| 29 V8ClassIndex::V8WrapperType type = V8ClassIndex::ToWrapperType(data); | 28 V8ClassIndex::V8WrapperType type = V8ClassIndex::ToWrapperType(data); |
| 30 return V8Proxy::ToV8Object(type, result); | 29 if (type == V8ClassIndex::NODE) |
| 30 return V8Proxy::NodeToV8Object(static_cast<Node*>(result)); |
| 31 else |
| 32 return V8Proxy::ToV8Object(type, result); |
| 31 } | 33 } |
| 32 | 34 |
| 33 // A template of named property accessor of collections. | 35 // A template of named property accessor of collections. |
| 34 template <class C> | 36 template <class C> |
| 35 static v8::Handle<v8::Value> CollectionNamedPropertyGetter( | 37 static v8::Handle<v8::Value> CollectionNamedPropertyGetter( |
| 36 v8::Local<v8::String> name, const v8::AccessorInfo& info) { | 38 v8::Local<v8::String> name, const v8::AccessorInfo& info) { |
| 37 return GetNamedPropertyOfCollection<C>(name, info.Holder(), info.Data()); | 39 return GetNamedPropertyOfCollection<C>(name, info.Holder(), info.Data()); |
| 38 } | 40 } |
| 39 | 41 |
| 42 |
| 43 // A template of named property accessor of HTMLSelectElement and |
| 44 // HTMLFormElement. |
| 45 template <class C> |
| 46 static v8::Handle<v8::Value> NodeCollectionNamedPropertyGetter( |
| 47 v8::Local<v8::String> name, const v8::AccessorInfo& info) { |
| 48 ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder())); |
| 49 ASSERT(V8Proxy::GetDOMWrapperType(info.Holder()) == V8ClassIndex::NODE); |
| 50 C* collection = V8Proxy::DOMWrapperToNode<C>(info.Holder()); |
| 51 String prop_name = ToWebCoreString(name); |
| 52 void* result = collection->namedItem(prop_name); |
| 53 if (!result) return v8::Handle<v8::Value>(); |
| 54 V8ClassIndex::V8WrapperType type = V8ClassIndex::ToWrapperType(info.Data()); |
| 55 if (type == V8ClassIndex::NODE) |
| 56 return V8Proxy::NodeToV8Object(static_cast<Node*>(result)); |
| 57 else |
| 58 return V8Proxy::ToV8Object(type, result); |
| 59 } |
| 60 |
| 61 |
| 40 // A template returns whether a collection has a named property. | 62 // A template returns whether a collection has a named property. |
| 41 // This function does not cause JS heap allocation. | 63 // This function does not cause JS heap allocation. |
| 42 template <class C> | 64 template <class C> |
| 43 static bool HasNamedPropertyOfCollection(v8::Local<v8::String> name, | 65 static bool HasNamedPropertyOfCollection(v8::Local<v8::String> name, |
| 44 v8::Local<v8::Object> object, | 66 v8::Local<v8::Object> object, |
| 45 v8::Local<v8::Value> data) { | 67 v8::Local<v8::Value> data) { |
| 46 // TODO: assert object is a collection type | 68 // TODO: assert object is a collection type |
| 47 ASSERT(V8Proxy::MaybeDOMWrapper(object)); | 69 ASSERT(V8Proxy::MaybeDOMWrapper(object)); |
| 48 | 70 |
| 49 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(object); | 71 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(object); |
| 50 C* collection = V8Proxy::FastToNativeObject<C>(t, object); | 72 C* collection = V8Proxy::ToNativeObject<C>(t, object); |
| 51 String prop_name = ToWebCoreString(name); | 73 String prop_name = ToWebCoreString(name); |
| 52 void* result = collection->namedItem(prop_name); | 74 void* result = collection->namedItem(prop_name); |
| 53 return result != NULL; | 75 return result != NULL; |
| 54 } | 76 } |
| 55 | 77 |
| 56 | 78 |
| 57 // Returns the property at the index of a collection. | 79 // Returns the property at the index of a collection. |
| 58 template <class C> | 80 template <class C> |
| 59 static v8::Handle<v8::Value> GetIndexedPropertyOfCollection( | 81 static v8::Handle<v8::Value> GetIndexedPropertyOfCollection( |
| 60 uint32_t index, v8::Local<v8::Object> object, v8::Local<v8::Value> data) { | 82 uint32_t index, v8::Local<v8::Object> object, v8::Local<v8::Value> data) { |
| 61 // TODO, assert that object must be a collection type | 83 // TODO, assert that object must be a collection type |
| 62 ASSERT(V8Proxy::MaybeDOMWrapper(object)); | 84 ASSERT(V8Proxy::MaybeDOMWrapper(object)); |
| 63 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(object); | 85 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(object); |
| 64 C* collection = V8Proxy::FastToNativeObject<C>(t, object); | 86 ASSERT(t != V8ClassIndex::NODE); |
| 87 C* collection = V8Proxy::ToNativeObject<C>(t, object); |
| 65 void* result = collection->item(index); | 88 void* result = collection->item(index); |
| 66 if (!result) return v8::Handle<v8::Value>(); | 89 if (!result) return v8::Handle<v8::Value>(); |
| 67 V8ClassIndex::V8WrapperType type = V8ClassIndex::ToWrapperType(data); | 90 V8ClassIndex::V8WrapperType type = V8ClassIndex::ToWrapperType(data); |
| 68 return V8Proxy::ToV8Object(type, result); | 91 if (type == V8ClassIndex::NODE) |
| 92 return V8Proxy::NodeToV8Object(static_cast<Node*>(result)); |
| 93 else |
| 94 return V8Proxy::ToV8Object(type, result); |
| 69 } | 95 } |
| 70 | 96 |
| 71 | 97 |
| 72 // A template of index interceptor of collections. | 98 // A template of index interceptor of collections. |
| 73 template <class C> | 99 template <class C> |
| 74 static v8::Handle<v8::Value> CollectionIndexedPropertyGetter( | 100 static v8::Handle<v8::Value> CollectionIndexedPropertyGetter( |
| 75 uint32_t index, const v8::AccessorInfo& info) { | 101 uint32_t index, const v8::AccessorInfo& info) { |
| 76 return GetIndexedPropertyOfCollection<C>(index, info.Holder(), info.Data()); | 102 return GetIndexedPropertyOfCollection<C>(index, info.Holder(), info.Data()); |
| 77 } | 103 } |
| 78 | 104 |
| 79 | 105 |
| 106 // A template of index interceptor of HTMLSelectElement and HTMLFormElement. |
| 107 template <class C> |
| 108 static v8::Handle<v8::Value> NodeCollectionIndexedPropertyGetter( |
| 109 uint32_t index, const v8::AccessorInfo& info) { |
| 110 ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder())); |
| 111 ASSERT(V8Proxy::GetDOMWrapperType(info.Holder()) == V8ClassIndex::NODE); |
| 112 C* collection = V8Proxy::DOMWrapperToNode<C>(info.Holder()); |
| 113 void* result = collection->item(index); |
| 114 if (!result) return v8::Handle<v8::Value>(); |
| 115 V8ClassIndex::V8WrapperType type = V8ClassIndex::ToWrapperType(info.Data()); |
| 116 if (type == V8ClassIndex::NODE) |
| 117 return V8Proxy::NodeToV8Object(static_cast<Node*>(result)); |
| 118 else |
| 119 return V8Proxy::ToV8Object(type, result); |
| 120 } |
| 121 |
| 122 |
| 123 // Get an array containing the names of indexed properties of |
| 124 // HTMLSelectElement and HTMLFormElement. |
| 125 template <class C> |
| 126 static v8::Handle<v8::Array> NodeCollectionIndexedPropertyEnumerator( |
| 127 const v8::AccessorInfo& info) { |
| 128 ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder())); |
| 129 ASSERT(V8Proxy::GetDOMWrapperType(info.Holder()) == V8ClassIndex::NODE); |
| 130 C* collection = V8Proxy::DOMWrapperToNode<C>(info.Holder()); |
| 131 int length = collection->length(); |
| 132 v8::Handle<v8::Array> properties = v8::Array::New(length); |
| 133 for (int i = 0; i < length; i++) { |
| 134 // TODO(ager): Do we need to check that the item function returns |
| 135 // a non-null value for this index? |
| 136 v8::Handle<v8::Integer> integer = v8::Integer::New(i); |
| 137 properties->Set(integer, integer); |
| 138 } |
| 139 return properties; |
| 140 } |
| 141 |
| 80 // Get an array containing the names of indexed properties in a collection. | 142 // Get an array containing the names of indexed properties in a collection. |
| 81 template <class C> | 143 template <class C> |
| 82 static v8::Handle<v8::Array> CollectionIndexedPropertyEnumerator( | 144 static v8::Handle<v8::Array> CollectionIndexedPropertyEnumerator( |
| 83 const v8::AccessorInfo& info) { | 145 const v8::AccessorInfo& info) { |
| 84 ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder())); | 146 ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder())); |
| 85 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(info.Holder()); | 147 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(info.Holder()); |
| 86 C* collection = V8Proxy::FastToNativeObject<C>(t, info.Holder()); | 148 C* collection = V8Proxy::ToNativeObject<C>(t, info.Holder()); |
| 87 int length = collection->length(); | 149 int length = collection->length(); |
| 88 v8::Handle<v8::Array> properties = v8::Array::New(length); | 150 v8::Handle<v8::Array> properties = v8::Array::New(length); |
| 89 for (int i = 0; i < length; i++) { | 151 for (int i = 0; i < length; i++) { |
| 90 // TODO(ager): Do we need to check that the item function returns | 152 // TODO(ager): Do we need to check that the item function returns |
| 91 // a non-null value for this index? | 153 // a non-null value for this index? |
| 92 v8::Handle<v8::Integer> integer = v8::Integer::New(i); | 154 v8::Handle<v8::Integer> integer = v8::Integer::New(i); |
| 93 properties->Set(integer, integer); | 155 properties->Set(integer, integer); |
| 94 } | 156 } |
| 95 return properties; | 157 return properties; |
| 96 } | 158 } |
| 97 | 159 |
| 98 | 160 |
| 99 // Returns whether a collection has a property at a given index. | 161 // Returns whether a collection has a property at a given index. |
| 100 // This function does not cause JS heap allocation. | 162 // This function does not cause JS heap allocation. |
| 101 template <class C> | 163 template <class C> |
| 102 static bool HasIndexedPropertyOfCollection(uint32_t index, | 164 static bool HasIndexedPropertyOfCollection(uint32_t index, |
| 103 v8::Local<v8::Object> object, | 165 v8::Local<v8::Object> object, |
| 104 v8::Local<v8::Value> data) { | 166 v8::Local<v8::Value> data) { |
| 105 // TODO, assert that object must be a collection type | 167 // TODO, assert that object must be a collection type |
| 106 ASSERT(V8Proxy::MaybeDOMWrapper(object)); | 168 ASSERT(V8Proxy::MaybeDOMWrapper(object)); |
| 107 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(object); | 169 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(object); |
| 108 C* collection = V8Proxy::FastToNativeObject<C>(t, object); | 170 C* collection = V8Proxy::ToNativeObject<C>(t, object); |
| 109 void* result = collection->item(index); | 171 void* result = collection->item(index); |
| 110 return result != NULL; | 172 return result != NULL; |
| 111 } | 173 } |
| 112 | 174 |
| 113 | 175 |
| 114 // A template for indexed getters on collections of strings that should return | 176 // A template for indexed getters on collections of strings that should return |
| 115 // null if the resulting string is a null string. | 177 // null if the resulting string is a null string. |
| 116 template <class C> | 178 template <class C> |
| 117 static v8::Handle<v8::Value> CollectionStringOrNullIndexedPropertyGetter( | 179 static v8::Handle<v8::Value> CollectionStringOrNullIndexedPropertyGetter( |
| 118 uint32_t index, const v8::AccessorInfo& info) { | 180 uint32_t index, const v8::AccessorInfo& info) { |
| 119 // TODO, assert that object must be a collection type | 181 // TODO, assert that object must be a collection type |
| 120 ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder())); | 182 ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder())); |
| 121 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(info.Holder()); | 183 V8ClassIndex::V8WrapperType t = V8Proxy::GetDOMWrapperType(info.Holder()); |
| 122 C* collection = V8Proxy::FastToNativeObject<C>(t, info.Holder()); | 184 C* collection = V8Proxy::ToNativeObject<C>(t, info.Holder()); |
| 123 String result = collection->item(index); | 185 String result = collection->item(index); |
| 124 return v8StringOrNull(result); | 186 return v8StringOrNull(result); |
| 125 } | 187 } |
| 126 | 188 |
| 127 | 189 |
| 128 // Add indexed getter to the function template for a collection. | 190 // Add indexed getter to the function template for a collection. |
| 129 template <class T> | 191 template <class T> |
| 130 static void SetCollectionIndexedGetter(v8::Handle<v8::FunctionTemplate> desc, | 192 static void SetCollectionIndexedGetter(v8::Handle<v8::FunctionTemplate> desc, |
| 131 V8ClassIndex::V8WrapperType type) { | 193 V8ClassIndex::V8WrapperType type) { |
| 132 desc->InstanceTemplate()->SetIndexedPropertyHandler( | 194 desc->InstanceTemplate()->SetIndexedPropertyHandler( |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 0, | 250 0, |
| 189 0, | 251 0, |
| 190 CollectionIndexedPropertyEnumerator<T>); | 252 CollectionIndexedPropertyEnumerator<T>); |
| 191 } | 253 } |
| 192 | 254 |
| 193 | 255 |
| 194 } // namespace WebCore | 256 } // namespace WebCore |
| 195 | 257 |
| 196 #endif // V8_PROPERTY_H__ | 258 #endif // V8_PROPERTY_H__ |
| 197 | 259 |
| OLD | NEW |