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

#include "JSSharedWorker.h"

#include "EventListener.h"
#include "JSEventListener.h"
#include "JSMessagePort.h"
#include "MessagePort.h"
#include "SharedWorker.h"
#include <wtf/GetPtr.h>

using namespace JSC;

namespace WebCore {

/* Hash table */

static const HashTableValue JSSharedWorkerTableValues[] =
{
    { "port", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsSharedWorkerPort), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "onerror", DontDelete, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsSharedWorkerOnerror), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(setJSSharedWorkerOnerror) },
    { "constructor", DontEnum | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsSharedWorkerConstructor), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSSharedWorkerTable = { 8, 7, true, JSSharedWorkerTableValues, 0 };
/* Hash table for constructor */

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

static const HashTable JSSharedWorkerConstructorTable = { 1, 0, false, JSSharedWorkerConstructorTableValues, 0 };
const ClassInfo JSSharedWorkerConstructor::s_info = { "SharedWorkerConstructor", &Base::s_info, &JSSharedWorkerConstructorTable, 0, CREATE_METHOD_TABLE(JSSharedWorkerConstructor) };

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

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

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

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

/* Hash table for prototype */

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

static const HashTable JSSharedWorkerPrototypeTable = { 1, 0, false, JSSharedWorkerPrototypeTableValues, 0 };
const ClassInfo JSSharedWorkerPrototype::s_info = { "SharedWorkerPrototype", &Base::s_info, &JSSharedWorkerPrototypeTable, 0, CREATE_METHOD_TABLE(JSSharedWorkerPrototype) };

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

const ClassInfo JSSharedWorker::s_info = { "SharedWorker", &Base::s_info, &JSSharedWorkerTable, 0 , CREATE_METHOD_TABLE(JSSharedWorker) };

JSSharedWorker::JSSharedWorker(Structure* structure, JSDOMGlobalObject* globalObject, PassRefPtr<SharedWorker> impl)
    : JSEventTarget(structure, globalObject, impl)
{
}

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

JSObject* JSSharedWorker::createPrototype(VM& vm, JSGlobalObject* globalObject)
{
    return JSSharedWorkerPrototype::create(vm, globalObject, JSSharedWorkerPrototype::createStructure(vm, globalObject, JSEventTargetPrototype::self(vm, globalObject)));
}

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

EncodedJSValue jsSharedWorkerPort(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSSharedWorker* castedThis = jsDynamicCast<JSSharedWorker*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    SharedWorker& impl = castedThis->impl();
    JSValue result = toJS(exec, castedThis->globalObject(), WTF::getPtr(impl.port()));
    return JSValue::encode(result);
}


EncodedJSValue jsSharedWorkerOnerror(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSSharedWorker* castedThis = jsDynamicCast<JSSharedWorker*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    SharedWorker& impl = castedThis->impl();
    if (EventListener* listener = impl.onerror()) {
        if (const JSEventListener* jsListener = JSEventListener::cast(listener)) {
            if (JSObject* jsFunction = jsListener->jsFunction(impl.scriptExecutionContext()))
                return JSValue::encode(jsFunction);
        }
    }
    return JSValue::encode(jsNull());
}


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

void setJSSharedWorkerOnerror(ExecState* exec, JSObject* /* baseObject */, EncodedJSValue thisValue, EncodedJSValue encodedValue)
{
    JSValue value = JSValue::decode(encodedValue);
    UNUSED_PARAM(exec);
    JSSharedWorker* castedThis = jsDynamicCast<JSSharedWorker*>(JSValue::decode(thisValue));
    if (!castedThis) {
        throwVMTypeError(exec);
        return;
    }
    UNUSED_PARAM(exec);
    SharedWorker& impl = castedThis->impl();
    impl.setOnerror(createJSAttributeEventListener(exec, value, castedThis));
}


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

bool JSSharedWorkerOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)
{
    JSSharedWorker* jsSharedWorker = jsCast<JSSharedWorker*>(handle.get().asCell());
    if (jsSharedWorker->impl().hasPendingActivity())
        return true;
    if (jsSharedWorker->impl().isFiringEventListeners())
        return true;
    UNUSED_PARAM(visitor);
    return false;
}

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

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

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

SharedWorker* toSharedWorker(JSC::JSValue value)
{
    return value.inherits(JSSharedWorker::info()) ? &jsCast<JSSharedWorker*>(value)->impl() : 0;
}

}

#endif // ENABLE(SHARED_WORKERS)
