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

#include "JSANGLEInstancedArrays.h"

#include "ANGLEInstancedArrays.h"
#include "ExceptionCode.h"
#include "JSDOMBinding.h"
#include <runtime/Error.h>
#include <wtf/GetPtr.h>

using namespace JSC;

namespace WebCore {

/* Hash table for prototype */

static const HashTableValue JSANGLEInstancedArraysPrototypeTableValues[] =
{
    { "VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsANGLEInstancedArraysVERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "drawArraysInstancedANGLE", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsANGLEInstancedArraysPrototypeFunctionDrawArraysInstancedANGLE), (intptr_t) (4) },
    { "drawElementsInstancedANGLE", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsANGLEInstancedArraysPrototypeFunctionDrawElementsInstancedANGLE), (intptr_t) (5) },
    { "vertexAttribDivisorANGLE", JSC::Function, NoIntrinsic, (intptr_t)static_cast<NativeFunction>(jsANGLEInstancedArraysPrototypeFunctionVertexAttribDivisorANGLE), (intptr_t) (2) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSANGLEInstancedArraysPrototypeTable = { 9, 7, true, JSANGLEInstancedArraysPrototypeTableValues, 0 };
const ClassInfo JSANGLEInstancedArraysPrototype::s_info = { "ANGLEInstancedArraysPrototype", &Base::s_info, &JSANGLEInstancedArraysPrototypeTable, 0, CREATE_METHOD_TABLE(JSANGLEInstancedArraysPrototype) };

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

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

const ClassInfo JSANGLEInstancedArrays::s_info = { "ANGLEInstancedArrays", &Base::s_info, 0, 0 , CREATE_METHOD_TABLE(JSANGLEInstancedArrays) };

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

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

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

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

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

EncodedJSValue JSC_HOST_CALL jsANGLEInstancedArraysPrototypeFunctionDrawArraysInstancedANGLE(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSANGLEInstancedArrays* castedThis = jsDynamicCast<JSANGLEInstancedArrays*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSANGLEInstancedArrays::info());
    ANGLEInstancedArrays& impl = castedThis->impl();
    if (exec->argumentCount() < 4)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    unsigned mode(toUInt32(exec, exec->argument(0), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    int first(toInt32(exec, exec->argument(1), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    int count(toInt32(exec, exec->argument(2), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    int primcount(toInt32(exec, exec->argument(3), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    impl.drawArraysInstancedANGLE(mode, first, count, primcount);
    return JSValue::encode(jsUndefined());
}

EncodedJSValue JSC_HOST_CALL jsANGLEInstancedArraysPrototypeFunctionDrawElementsInstancedANGLE(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSANGLEInstancedArrays* castedThis = jsDynamicCast<JSANGLEInstancedArrays*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSANGLEInstancedArrays::info());
    ANGLEInstancedArrays& impl = castedThis->impl();
    if (exec->argumentCount() < 5)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    unsigned mode(toUInt32(exec, exec->argument(0), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    int count(toInt32(exec, exec->argument(1), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    unsigned type(toUInt32(exec, exec->argument(2), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    long long offset(toInt64(exec, exec->argument(3), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    int primcount(toInt32(exec, exec->argument(4), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    impl.drawElementsInstancedANGLE(mode, count, type, offset, primcount);
    return JSValue::encode(jsUndefined());
}

EncodedJSValue JSC_HOST_CALL jsANGLEInstancedArraysPrototypeFunctionVertexAttribDivisorANGLE(ExecState* exec)
{
    JSValue thisValue = exec->hostThisValue();
    JSANGLEInstancedArrays* castedThis = jsDynamicCast<JSANGLEInstancedArrays*>(thisValue);
    if (!castedThis)
        return throwVMTypeError(exec);
    ASSERT_GC_OBJECT_INHERITS(castedThis, JSANGLEInstancedArrays::info());
    ANGLEInstancedArrays& impl = castedThis->impl();
    if (exec->argumentCount() < 2)
        return throwVMError(exec, createNotEnoughArgumentsError(exec));
    unsigned index(toUInt32(exec, exec->argument(0), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    unsigned divisor(toUInt32(exec, exec->argument(1), NormalConversion));
    if (exec->hadException())
        return JSValue::encode(jsUndefined());
    impl.vertexAttribDivisorANGLE(index, divisor);
    return JSValue::encode(jsUndefined());
}

// Constant getters

EncodedJSValue jsANGLEInstancedArraysVERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE(ExecState* exec, EncodedJSValue, EncodedJSValue, PropertyName)
{
    UNUSED_PARAM(exec);
    return JSValue::encode(jsNumber(static_cast<int>(0x88FE)));
}

static inline bool isObservable(JSANGLEInstancedArrays* jsANGLEInstancedArrays)
{
    if (jsANGLEInstancedArrays->hasCustomProperties())
        return true;
    return false;
}

bool JSANGLEInstancedArraysOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)
{
    JSANGLEInstancedArrays* jsANGLEInstancedArrays = jsCast<JSANGLEInstancedArrays*>(handle.get().asCell());
    if (!isObservable(jsANGLEInstancedArrays))
        return false;
    WebGLRenderingContext* root = jsANGLEInstancedArrays->impl().context();
    return visitor.containsOpaqueRoot(root);
}

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

#if ENABLE(BINDING_INTEGRITY)
#if PLATFORM(WIN)
#pragma warning(disable: 4483)
extern "C" { extern void (*const __identifier("??_7ANGLEInstancedArrays@WebCore@@6B@")[])(); }
#else
extern "C" { extern void* _ZTVN7WebCore20ANGLEInstancedArraysE[]; }
#endif
#endif
JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, ANGLEInstancedArrays* impl)
{
    if (!impl)
        return jsNull();
    if (JSValue result = getExistingWrapper<JSANGLEInstancedArrays>(exec, impl))
        return result;

#if ENABLE(BINDING_INTEGRITY)
    void* actualVTablePointer = *(reinterpret_cast<void**>(impl));
#if PLATFORM(WIN)
    void* expectedVTablePointer = reinterpret_cast<void*>(__identifier("??_7ANGLEInstancedArrays@WebCore@@6B@"));
#else
    void* expectedVTablePointer = &_ZTVN7WebCore20ANGLEInstancedArraysE[2];
#if COMPILER(CLANG)
    // If this fails ANGLEInstancedArrays does not have a vtable, so you need to add the
    // ImplementationLacksVTable attribute to the interface definition
    COMPILE_ASSERT(__is_polymorphic(ANGLEInstancedArrays), ANGLEInstancedArrays_is_not_polymorphic);
#endif
#endif
    // If you hit this assertion you either have a use after free bug, or
    // ANGLEInstancedArrays has subclasses. If ANGLEInstancedArrays has subclasses that get passed
    // to toJS() we currently require ANGLEInstancedArrays you to opt out of binding hardening
    // by adding the SkipVTableValidation attribute to the interface IDL definition
    RELEASE_ASSERT(actualVTablePointer == expectedVTablePointer);
#endif
    ReportMemoryCost<ANGLEInstancedArrays>::reportMemoryCost(exec, impl);
    return createNewWrapper<JSANGLEInstancedArrays>(exec, globalObject, impl);
}

ANGLEInstancedArrays* toANGLEInstancedArrays(JSC::JSValue value)
{
    return value.inherits(JSANGLEInstancedArrays::info()) ? &jsCast<JSANGLEInstancedArrays*>(value)->impl() : 0;
}

}

#endif // ENABLE(WEBGL)
