#ifndef V8Collection_h
#define V8Collection_h
#include "HTMLFormElement.h"
#include "HTMLSelectElement.h"
#include "V8Binding.h"
#include "V8Node.h"
#include "V8Proxy.h"
#include <v8.h>
namespace WebCore {
template<class T> static v8::Handle<v8::Value> getV8Object(T* implementation)
{
if (!implementation)
return v8::Handle<v8::Value>();
return toV8(implementation);
}
template<class Collection> static Collection* toNativeCollection(v8::Local<v8::Object> object)
{
return reinterpret_cast<Collection*>(object->GetPointerFromInternalField(v8DOMWrapperObjectIndex));
}
template<class T> static v8::Handle<v8::Value> getV8Object(PassRefPtr<T> implementation)
{
return getV8Object(implementation.get());
}
template<class Collection, class ItemType> static v8::Handle<v8::Value> getNamedPropertyOfCollection(v8::Local<v8::String> name, v8::Local<v8::Object> object)
{
ASSERT(V8DOMWrapper::maybeDOMWrapper(object));
ASSERT(V8DOMWrapper::domWrapperType(object) != &V8Node::info);
Collection* collection = toNativeCollection<Collection>(object);
AtomicString propertyName = toAtomicWebCoreStringWithNullCheck(name);
return getV8Object<ItemType>(collection->namedItem(propertyName));
}
template<class Collection, class ItemType> static v8::Handle<v8::Value> collectionNamedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
if (!value.IsEmpty())
return value;
if (info.Holder()->HasRealNamedCallbackProperty(name))
return notHandledByInterceptor();
return getNamedPropertyOfCollection<Collection, ItemType>(name, info.Holder());
}
template<class Collection, class ItemType> static v8::Handle<v8::Value> getIndexedPropertyOfCollection(uint32_t index, v8::Local<v8::Object> object)
{
ASSERT(V8DOMWrapper::maybeDOMWrapper(object));
ASSERT(V8DOMWrapper::domWrapperType(object) != &V8Node::info);
Collection* collection = toNativeCollection<Collection>(object);
return getV8Object<ItemType>(collection->item(index));
}
template<class Collection, class ItemType> static v8::Handle<v8::Value> collectionIndexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
{
return getIndexedPropertyOfCollection<Collection, ItemType>(index, info.Holder());
}
template<class Collection> static v8::Handle<v8::Array> nodeCollectionIndexedPropertyEnumerator(const v8::AccessorInfo& info)
{
ASSERT(V8DOMWrapper::maybeDOMWrapper(info.Holder()));
Collection* collection = toNativeCollection<Collection>(info.Holder());
int length = collection->length();
v8::Handle<v8::Array> properties = v8::Array::New(length);
for (int i = 0; i < length; ++i) {
v8::Handle<v8::Integer> integer = v8::Integer::New(i);
properties->Set(integer, integer);
}
return properties;
}
template<class Collection> static v8::Handle<v8::Array> collectionIndexedPropertyEnumerator(const v8::AccessorInfo& info)
{
ASSERT(V8DOMWrapper::maybeDOMWrapper(info.Holder()));
Collection* collection = toNativeCollection<Collection>(info.Holder());
int length = collection->length();
v8::Handle<v8::Array> properties = v8::Array::New(length);
for (int i = 0; i < length; ++i) {
v8::Handle<v8::Integer> integer = v8::Integer::New(i);
properties->Set(integer, integer);
}
return properties;
}
template<class Collection> static v8::Handle<v8::Value> collectionStringOrNullIndexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
{
ASSERT(V8DOMWrapper::maybeDOMWrapper(info.Holder()));
Collection* collection = toNativeCollection<Collection>(info.Holder());
String result = collection->item(index);
return v8StringOrNull(result);
}
template<class Collection> static v8::Handle<v8::Value> collectionStringIndexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
{
ASSERT(V8DOMWrapper::maybeDOMWrapper(info.Holder()));
Collection* collection = toNativeCollection<Collection>(info.Holder());
String result = collection->item(index);
return v8String(result);
}
template<class Collection, class ItemType> static void setCollectionIndexedGetter(v8::Handle<v8::FunctionTemplate> desc)
{
desc->InstanceTemplate()->SetIndexedPropertyHandler(collectionIndexedPropertyGetter<Collection, ItemType>, 0, 0, 0, collectionIndexedPropertyEnumerator<Collection>);
}
template<class Collection, class ItemType> static void setCollectionNamedGetter(v8::Handle<v8::FunctionTemplate> desc)
{
desc->InstanceTemplate()->SetNamedPropertyHandler(collectionNamedPropertyGetter<Collection, ItemType>, 0, 0, 0, 0);
}
template<class Collection> static void setCollectionStringOrNullIndexedGetter(v8::Handle<v8::FunctionTemplate> desc)
{
desc->InstanceTemplate()->SetIndexedPropertyHandler(collectionStringOrNullIndexedPropertyGetter<Collection>, 0, 0, 0, collectionIndexedPropertyEnumerator<Collection>);
}
template<class Collection> static void setCollectionStringIndexedGetter(v8::Handle<v8::FunctionTemplate> desc)
{
desc->InstanceTemplate()->SetIndexedPropertyHandler(collectionStringIndexedPropertyGetter<Collection>, 0, 0, 0, collectionIndexedPropertyEnumerator<Collection>);
}
v8::Handle<v8::Value> toOptionsCollectionSetter(uint32_t index, v8::Handle<v8::Value>, HTMLSelectElement*);
}
#endif // V8Collection_h