/*
    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"
#include "JSMutationObserver.h"

#include "Dictionary.h"
#include "ExceptionCode.h"
#include "JSDOMBinding.h"
#include "JSMutationRecord.h"
#include "JSNode.h"
#include "MutationObserver.h"
#include "MutationRecord.h"
#include <runtime/Error.h>
#include <runtime/JSArray.h>
#include <wtf/GetPtr.h>

using namespace JSC;

namespace WebCore {

/* Hash table */

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

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

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

static const HashTable JSMutationObserverConstructorTable = { 1, 0, false, JSMutationObserverConstructorTableValues, 0 };
const ClassInfo JSMutationObserverConstructor::s_info = { "MutationObserverConstructor", &Base::s_info, &JSMutationObserverConstructorTable, 0, CREATE_METHOD_TABLE(JSMutationObserverConstructor) };

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

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

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

ConstructType JSMutationObserverConstructor::getConstructData(JSCell*, ConstructData& constructData)
{
    constructData.native.function = constructJSMutationObserver;
    return ConstructTypeHost;
}

/* Hash table for prototype */

static const HashTableValue JSMutationObserverPrototypeTableValues[] =
{
    { "observe", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsMutationObserverPrototypeFunctionObserve), (intptr_t) (2) },
    { "takeRecords", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsMutationObserverPrototypeFunctionTakeRecords), (intptr_t) (0) },
    { "disconnect", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsMutationObserverPrototypeFunctionDisconnect), (intptr_t) (0) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSMutationObserverPrototypeTable = { 8, 7, false, JSMutationObserverPrototypeTableValues, 0 };
const ClassInfo JSMutationObserverPrototype::s_info = { "MutationObserverPrototype", &Base::s_info, &JSMutationObserverPrototypeTable, 0, CREATE_METHOD_TABLE(JSMutationObserverPrototype) };

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

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

const ClassInfo JSMutationObserver::s_info = { "MutationObserver", &Base::s_info, &JSMutationObserverTable, 0 , CREATE_METHOD_TABLE(JSMutationObserver) };

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

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

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

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

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

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

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

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

EncodedJSValue JSC_HOST_CALL jsMutationObserverPrototypeFunctionObserve(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSMutationObserver* castedThis = jsDynamicCast<JSMutationObserver*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSMutationObserver::info());
    MutationObserver& impl = castedThis->impl();
    if (exec->argumentCount() < 2)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    ExceptionCode ec = 0;
    Node* target(toNode(exec->argument(0)));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    Dictionary options(exec, exec->argument(1));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    impl.observe(target, options, ec);
    setDOMException(exec, ec);
    return JSValue::encode(jsUndefined());
}

EncodedJSValue JSC_HOST_CALL jsMutationObserverPrototypeFunctionTakeRecords(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSMutationObserver* castedThis = jsDynamicCast<JSMutationObserver*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSMutationObserver::info());
    MutationObserver& impl = castedThis->impl();

    JSC::JSValue result = jsArray(exec, castedThis->globalObject(), impl.takeRecords());
    return JSValue::encode(result);
}

EncodedJSValue JSC_HOST_CALL jsMutationObserverPrototypeFunctionDisconnect(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSMutationObserver* castedThis = jsDynamicCast<JSMutationObserver*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSMutationObserver::info());
    MutationObserver& impl = castedThis->impl();
    impl.disconnect();
    return JSValue::encode(jsUndefined());
}

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

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

MutationObserver* toMutationObserver(JSC::JSValue value)
{
    return value.inherits(JSMutationObserver::info()) ? &jsCast<JSMutationObserver*>(value)->impl() : 0;
}

}
