#ifndef V8Collection_h
#define V8Collection_h
#include "HTMLFormElement.h"
#include "HTMLSelectElement.h"
#include "V8Binding.h"
#include "V8Proxy.h"
#include <v8.h>
namespace WebCore {
static v8::Handle<v8::Value> getV8Object(void* implementation, v8::Local<v8::Value> implementationType)
{
if (!implementation)
return v8::Handle<v8::Value>();
V8ClassIndex::V8WrapperType type = V8ClassIndex::FromInt(implementationType->Int32Value());
if (type == V8ClassIndex::NODE)
return V8Proxy::NodeToV8Object(static_cast<Node*>(implementation));
return V8Proxy::ToV8Object(type, implementation);
}
template<class T> static v8::Handle<v8::Value> getV8Object(PassRefPtr<T> implementation, v8::Local<v8::Value> implementationType)
{
return getV8Object(implementation.get(), implementationType);
}
template<class Collection, class ItemType> static v8::Handle<v8::Value> getNamedPropertyOfCollection(v8::Local<v8::String> name, v8::Local<v8::Object> object,
v8::Local<v8::Value> implementationType)
{
ASSERT(V8Proxy::MaybeDOMWrapper(object));
V8ClassIndex::V8WrapperType wrapperType = V8Proxy::GetDOMWrapperType(object);
ASSERT(wrapperType != V8ClassIndex::NODE);
Collection* collection = V8Proxy::ToNativeObject<Collection>(wrapperType, object);
String propertyName = toWebCoreString(name);
return getV8Object<ItemType>(collection->namedItem(propertyName), implementationType);
}
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(), info.Data());
}
template<class Collection> static v8::Handle<v8::Value> nodeCollectionNamedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder()));
ASSERT(V8Proxy::GetDOMWrapperType(info.Holder()) == V8ClassIndex::NODE);
v8::Handle<v8::Value> value = info.Holder()->GetRealNamedPropertyInPrototypeChain(name);
if (!value.IsEmpty())
return value;
if (info.Holder()->HasRealNamedCallbackProperty(name))
return notHandledByInterceptor();
Collection* collection = V8Proxy::DOMWrapperToNode<Collection>(info.Holder());
String propertyName = toWebCoreString(name);
void* implementation = collection->namedItem(propertyName);
return getV8Object(implementation, info.Data());
}
template<class Collection, class ItemType> static v8::Handle<v8::Value> getIndexedPropertyOfCollection(uint32_t index, v8::Local<v8::Object> object,
v8::Local<v8::Value> implementationType)
{
ASSERT(V8Proxy::MaybeDOMWrapper(object));
V8ClassIndex::V8WrapperType wrapperType = V8Proxy::GetDOMWrapperType(object);
ASSERT(wrapperType != V8ClassIndex::NODE);
Collection* collection = V8Proxy::ToNativeObject<Collection>(wrapperType, object);
return getV8Object<ItemType>(collection->item(index), implementationType);
}
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(), info.Data());
}
template<class Collection> static v8::Handle<v8::Value> nodeCollectionIndexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
{
ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder()));
ASSERT(V8Proxy::GetDOMWrapperType(info.Holder()) == V8ClassIndex::NODE);
Collection* collection = V8Proxy::DOMWrapperToNode<Collection>(info.Holder());
void* implementation = collection->item(index);
return getV8Object(implementation, info.Data());
}
template<class Collection> static v8::Handle<v8::Array> nodeCollectionIndexedPropertyEnumerator(const v8::AccessorInfo& info)
{
ASSERT(V8Proxy::MaybeDOMWrapper(info.Holder()));
ASSERT(V8Proxy::GetDOMWrapperType(info.Holder()) == V8ClassIndex::NODE);
Collection* collection = V8Proxy::DOMWrapperToNode<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(V8Proxy::MaybeDOMWrapper(info.Holder()));
V8ClassIndex::V8WrapperType wrapperType = V8Proxy::GetDOMWrapperType(info.Holder());
Collection* collection = V8Proxy::ToNativeObject<Collection>(wrapperType, 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(V8Proxy::MaybeDOMWrapper(info.Holder()));
V8ClassIndex::V8WrapperType wrapperType = V8Proxy::GetDOMWrapperType(info.Holder());
Collection* collection = V8Proxy::ToNativeObject<Collection>(wrapperType, info.Holder());
String result = collection->item(index);
return v8StringOrNull(result);
}
template<class Collection, class ItemType> static void setCollectionIndexedGetter(v8::Handle<v8::FunctionTemplate> desc, V8ClassIndex::V8WrapperType type)
{
desc->InstanceTemplate()->SetIndexedPropertyHandler(collectionIndexedPropertyGetter<Collection, ItemType>, 0, 0, 0, collectionIndexedPropertyEnumerator<Collection>,
v8::Integer::New(V8ClassIndex::ToInt(type)));
}
template<class Collection, class ItemType> static void setCollectionNamedGetter(v8::Handle<v8::FunctionTemplate> desc, V8ClassIndex::V8WrapperType type)
{
desc->InstanceTemplate()->SetNamedPropertyHandler(collectionNamedPropertyGetter<Collection, ItemType>, 0, 0, 0, 0, v8::Integer::New(V8ClassIndex::ToInt(type)));
}
template<class Collection, class ItemType> static void setCollectionIndexedAndNamedGetters(v8::Handle<v8::FunctionTemplate> desc, V8ClassIndex::V8WrapperType type)
{
desc->InstanceTemplate()->SetNamedPropertyHandler(collectionNamedPropertyGetter<Collection, ItemType>, 0, 0, 0, 0, v8::Integer::New(V8ClassIndex::ToInt(type)));
desc->InstanceTemplate()->SetIndexedPropertyHandler(collectionIndexedPropertyGetter<Collection, ItemType>, 0, 0, 0, collectionIndexedPropertyEnumerator<Collection>,
v8::Integer::New(V8ClassIndex::ToInt(type)));
}
template<class Collection> static void setCollectionStringOrNullIndexedGetter(v8::Handle<v8::FunctionTemplate> desc)
{
desc->InstanceTemplate()->SetIndexedPropertyHandler(collectionStringOrNullIndexedPropertyGetter<Collection>, 0, 0, 0, collectionIndexedPropertyEnumerator<Collection>);
}
v8::Handle<v8::Value> toOptionsCollectionSetter(uint32_t index, v8::Handle<v8::Value>, HTMLSelectElement*);
}
#endif // V8Collection_h