/*
    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(INDEXED_DATABASE)

#include "JSIDBFactory.h"

#include "ExceptionCode.h"
#include "IDBFactory.h"
#include "IDBOpenDBRequest.h"
#include "JSDOMBinding.h"
#include "JSIDBOpenDBRequest.h"
#include <runtime/Error.h>
#include <wtf/GetPtr.h>

using namespace JSC;

namespace WebCore {

/* Hash table */

static const HashTableValue JSIDBFactoryTableValues[] =
{
    { "constructor", DontEnum | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsIDBFactoryConstructor), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSIDBFactoryTable = { 2, 1, true, JSIDBFactoryTableValues, 0 };
/* Hash table for constructor */

static const HashTableValue JSIDBFactoryConstructorTableValues[] =
{
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSIDBFactoryConstructorTable = { 1, 0, false, JSIDBFactoryConstructorTableValues, 0 };
static const HashTable& getJSIDBFactoryConstructorTable(VM& vm)
{
    return getHashTableForGlobalData(vm, JSIDBFactoryConstructorTable);
}

const ClassInfo JSIDBFactoryConstructor::s_info = { "IDBFactoryConstructor", &Base::s_info, 0, getJSIDBFactoryConstructorTable, CREATE_METHOD_TABLE(JSIDBFactoryConstructor) };

JSIDBFactoryConstructor::JSIDBFactoryConstructor(Structure* structure, JSDOMGlobalObject* globalObject)
    : DOMConstructorObject(structure, globalObject)
{
}

void JSIDBFactoryConstructor::finishCreation(VM& vm, JSDOMGlobalObject* globalObject)
{
    Base::finishCreation(vm);
    ASSERT(inherits(info()));
    putDirect(vm, vm.propertyNames->prototype, JSIDBFactoryPrototype::self(vm, globalObject), DontDelete | ReadOnly);
    putDirect(vm, vm.propertyNames->length, jsNumber(0), ReadOnly | DontDelete | DontEnum);
}

bool JSIDBFactoryConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
    return getStaticValueSlot<JSIDBFactoryConstructor, JSDOMWrapper>(exec, getJSIDBFactoryConstructorTable(exec->vm()), jsCast<JSIDBFactoryConstructor*>(object), propertyName, slot);
}

/* Hash table for prototype */

static const HashTableValue JSIDBFactoryPrototypeTableValues[] =
{
    { "open", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsIDBFactoryPrototypeFunctionOpen), (intptr_t) (1) },
    { "deleteDatabase", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsIDBFactoryPrototypeFunctionDeleteDatabase), (intptr_t) (1) },
    { "cmp", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsIDBFactoryPrototypeFunctionCmp), (intptr_t) (2) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSIDBFactoryPrototypeTable = { 8, 7, false, JSIDBFactoryPrototypeTableValues, 0 };
static const HashTable& getJSIDBFactoryPrototypeTable(VM& vm)
{
    return getHashTableForGlobalData(vm, JSIDBFactoryPrototypeTable);
}

const ClassInfo JSIDBFactoryPrototype::s_info = { "IDBFactoryPrototype", &Base::s_info, 0, getJSIDBFactoryPrototypeTable, CREATE_METHOD_TABLE(JSIDBFactoryPrototype) };

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

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

static const HashTable& getJSIDBFactoryTable(VM& vm)
{
    return getHashTableForGlobalData(vm, JSIDBFactoryTable);
}

const ClassInfo JSIDBFactory::s_info = { "IDBFactory", &Base::s_info, 0, getJSIDBFactoryTable , CREATE_METHOD_TABLE(JSIDBFactory) };

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

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

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

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

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

bool JSIDBFactory::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
{
    JSIDBFactory* thisObject = jsCast<JSIDBFactory*>(object);
    ASSERT_GC_OBJECT_INHERITS(thisObject, info());
    return getStaticValueSlot<JSIDBFactory, Base>(exec, getJSIDBFactoryTable(exec->vm()), thisObject, propertyName, slot);
}

EncodedJSValue jsIDBFactoryConstructor(ExecState* exec, EncodedJSValue thisValue, EncodedJSValue, PropertyName)
{
    JSIDBFactory* domObject = jsDynamicCast<JSIDBFactory*>(JSValue::decode(thisValue));
    if (!domObject)
        return throwVMTypeError(exec);
    if (!domObject)
        return throwVMTypeError(exec);
    return JSValue::encode(JSIDBFactory::getConstructor(exec->vm(), domObject->globalObject()));
}

JSValue JSIDBFactory::getConstructor(VM& vm, JSGlobalObject* globalObject)
{
    return getDOMConstructor<JSIDBFactoryConstructor>(vm, jsCast<JSDOMGlobalObject*>(globalObject));
}

EncodedJSValue JSC_HOST_CALL jsIDBFactoryPrototypeFunctionOpen(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSIDBFactory* castedThis = jsDynamicCast<JSIDBFactory*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSIDBFactory::info());
    IDBFactory& impl = castedThis->impl();
    if (exec->argumentCount() < 1)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    ExceptionCode ec = 0;
    ScriptExecutionContext* scriptContext = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject())->scriptExecutionContext();
    if (!scriptContext)
        return JSValue::encode(jsUndefined());
    const String& name(exec->argument(0).isEmpty() ? String() : exec->argument(0).toString(exec)->value(exec));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    size_t argsCount = exec->argumentCount();
    if (argsCount <= 1) {

        JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.open(scriptContext, name, ec)));
        setDOMException(exec, ec);
        return JSValue::encode(result);
    }

    unsigned long long version(toUInt64(exec, exec->argument(1), EnforceRange));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.open(scriptContext, name, version, ec)));
    setDOMException(exec, ec);
    return JSValue::encode(result);
}

EncodedJSValue JSC_HOST_CALL jsIDBFactoryPrototypeFunctionDeleteDatabase(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSIDBFactory* castedThis = jsDynamicCast<JSIDBFactory*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSIDBFactory::info());
    IDBFactory& impl = castedThis->impl();
    if (exec->argumentCount() < 1)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    ExceptionCode ec = 0;
    ScriptExecutionContext* scriptContext = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject())->scriptExecutionContext();
    if (!scriptContext)
        return JSValue::encode(jsUndefined());
    const String& name(exec->argument(0).isEmpty() ? String() : exec->argument(0).toString(exec)->value(exec));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    JSC::JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.deleteDatabase(scriptContext, name, ec)));
    setDOMException(exec, ec);
    return JSValue::encode(result);
}

EncodedJSValue JSC_HOST_CALL jsIDBFactoryPrototypeFunctionCmp(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSIDBFactory* castedThis = jsDynamicCast<JSIDBFactory*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSIDBFactory::info());
    IDBFactory& impl = castedThis->impl();
    if (exec->argumentCount() < 2)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    ExceptionCode ec = 0;
    ScriptExecutionContext* scriptContext = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject())->scriptExecutionContext();
    if (!scriptContext)
        return JSValue::encode(jsUndefined());
    Deprecated::ScriptValue first(exec->vm(), exec->argument(0));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    Deprecated::ScriptValue second(exec->vm(), exec->argument(1));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());

    JSC::JSValue result = jsNumber(impl.cmp(scriptContext, first, second, ec));
    setDOMException(exec, ec);
    return JSValue::encode(result);
}

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

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

JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, IDBFactory* impl)
{
    if (!impl)
        return jsNull();
    if (JSValue result = getExistingWrapper<JSIDBFactory>(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 IDBFactory.
    COMPILE_ASSERT(!__is_polymorphic(IDBFactory), IDBFactory_is_polymorphic_but_idl_claims_not_to_be);
#endif
    ReportMemoryCost<IDBFactory>::reportMemoryCost(exec, impl);
    return createNewWrapper<JSIDBFactory>(exec, globalObject, impl);
}

IDBFactory* toIDBFactory(JSC::JSValue value)
{
    return value.inherits(JSIDBFactory::info()) ? &jsCast<JSIDBFactory*>(value)->impl() : 0;
}

}

#endif // ENABLE(INDEXED_DATABASE)
