/*
    This file is part of the WebKit open source project.
    This file has been generated by generate-bindings.pl. DO NOT MODIFY!

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "config.h"

#if ENABLE(GAMEPAD)

#include "JSGamepadList.h"

#include "ExceptionCode.h"
#include "Gamepad.h"
#include "GamepadList.h"
#include "JSDOMBinding.h"
#include "JSGamepad.h"
#include <runtime/Error.h>
#include <runtime/PropertyNameArray.h>
#include <wtf/GetPtr.h>

using namespace JSC;

namespace WebCore {

/* Hash table */

static const HashTableValue JSGamepadListTableValues[] =
{
    { "length", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsGamepadListLength), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSGamepadListTable = { 2, 1, true, JSGamepadListTableValues, 0 };
/* Hash table for prototype */

static const HashTableValue JSGamepadListPrototypeTableValues[] =
{
    { "item", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsGamepadListPrototypeFunctionItem), (intptr_t) (0) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSGamepadListPrototypeTable = { 2, 1, false, JSGamepadListPrototypeTableValues, 0 };
const ClassInfo JSGamepadListPrototype::s_info = { "GamepadListPrototype", &Base::s_info, &JSGamepadListPrototypeTable, 0, CREATE_METHOD_TABLE(JSGamepadListPrototype) };

JSObject* JSGamepadListPrototype::self(VM& vm, JSGlobalObject* globalObject)
{
    return getDOMPrototype<JSGamepadList>(vm, globalObject);
}

bool JSGamepadListPrototype::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
    JSGamepadListPrototype* thisObject = jsCast<JSGamepadListPrototype*>(object);
    return getStaticFunctionSlot<JSObject>(exec, JSGamepadListPrototypeTable, thisObject, propertyName, slot);
}

const ClassInfo JSGamepadList::s_info = { "GamepadList", &Base::s_info, &JSGamepadListTable, 0 , CREATE_METHOD_TABLE(JSGamepadList) };

JSGamepadList::JSGamepadList(Structure* structure, JSDOMGlobalObject* globalObject, PassRefPtr<GamepadList> impl)
    : JSDOMWrapper(structure, globalObject)
    , m_impl(impl.leakRef())
{
}

void JSGamepadList::finishCreation(VM& vm)
{
    Base::finishCreation(vm);
    ASSERT(inherits(info()));
}

JSObject* JSGamepadList::createPrototype(VM& vm, JSGlobalObject* globalObject)
{
    return JSGamepadListPrototype::create(vm, globalObject, JSGamepadListPrototype::createStructure(vm, globalObject, globalObject->objectPrototype()));
}

void JSGamepadList::destroy(JSC::JSCell* cell)
{
    JSGamepadList* thisObject = static_cast<JSGamepadList*>(cell);
    thisObject->JSGamepadList::~JSGamepadList();
}

JSGamepadList::~JSGamepadList()
{
    releaseImplIfNotNull();
}

bool JSGamepadList::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
    JSGamepadList* thisObject = jsCast<JSGamepadList*>(object);
    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
    const HashEntry* entry = getStaticValueSlotEntryWithoutCaching<JSGamepadList>(exec, propertyName);
    if (entry) {
        slot.setCustom(thisObject, entry->attributes(), entry->propertyGetter());
        return true;
    }
    unsigned index = propertyName.asIndex();
    if (index != PropertyName::NotAnIndex && index < thisObject->impl().length()) {
        unsigned attributes = DontDelete | ReadOnly;
        slot.setCustomIndex(thisObject, attributes, index, indexGetter);
        return true;
    }
    return getStaticValueSlot<JSGamepadList, Base>(exec, JSGamepadListTable, thisObject, propertyName, slot);
}

bool JSGamepadList::getOwnPropertySlotByIndex(JSObject* object, ExecState* exec, unsigned index, PropertySlot& slot)
{
    JSGamepadList* thisObject = jsCast<JSGamepadList*>(object);
    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
    if (index < thisObject->impl().length()) {
        unsigned attributes = DontDelete | ReadOnly;
        slot.setCustomIndex(thisObject, attributes, index, thisObject->indexGetter);
        return true;
    }
    return Base::getOwnPropertySlotByIndex(thisObject, exec, index, slot);
}

EncodedJSValue jsGamepadListLength(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSGamepadList* castedThis = jsDynamicCast<JSGamepadList*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    GamepadList& impl = castedThis->impl();
    JSValue result = jsNumber(impl.length());
    return JSValue::encode(result);
}


void JSGamepadList::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
{
    JSGamepadList* thisObject = jsCast<JSGamepadList*>(object);
    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
    for (unsigned i = 0, count = thisObject->impl().length(); i < count; ++i)
        propertyNames.add(Identifier::from(exec, i));
     Base::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
}

EncodedJSValue JSC_HOST_CALL jsGamepadListPrototypeFunctionItem(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSGamepadList* castedThis = jsDynamicCast<JSGamepadList*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSGamepadList::info());
    GamepadList& impl = castedThis->impl();
    unsigned index(toUInt32(exec, exec->argument(0), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.item(index)));
    return JSValue::encode(result);
}


EncodedJSValue JSGamepadList::indexGetter(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue, unsigned index)
{
    JSGamepadList* thisObj = jsCast<JSGamepadList*>(JSValue::decode(slotBase));
    ASSERT_GC_OBJECT_INHERITS(thisObj, info());
    return JSValue::encode(toJS(exec, thisObj->globalObject(), thisObj->impl().item(index)));
}

bool JSGamepadListOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)
{
    UNUSED_PARAM(handle);
    UNUSED_PARAM(visitor);
    return false;
}

void JSGamepadListOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
{
    JSGamepadList* jsGamepadList = jsCast<JSGamepadList*>(handle.get().asCell());
    DOMWrapperWorld& world = *static_cast<DOMWrapperWorld*>(context);
    uncacheWrapper(world, &jsGamepadList->impl(), jsGamepadList);
    jsGamepadList->releaseImpl();
}

JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, GamepadList* impl)
{
    if (!impl)
        return jsNull();
    if (JSValue result = getExistingWrapper<JSGamepadList>(exec, impl))
        return result;
#if COMPILER(CLANG)
    // If you hit this failure the interface definition has the ImplementationLacksVTable
    // attribute. You should remove that attribute. If the class has subclasses
    // that may be passed through this toJS() function you should use the SkipVTableValidation
    // attribute to GamepadList.
    COMPILE_ASSERT(!__is_polymorphic(GamepadList), GamepadList_is_polymorphic_but_idl_claims_not_to_be);
#endif
    ReportMemoryCost<GamepadList>::reportMemoryCost(exec, impl);
    return createNewWrapper<JSGamepadList>(exec, globalObject, impl);
}

GamepadList* toGamepadList(JSC::JSValue value)
{
    return value.inherits(JSGamepadList::info()) ? &jsCast<JSGamepadList*>(value)->impl() : 0;
}

}

#endif // ENABLE(GAMEPAD)
