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

#include "JSCryptoKey.h"

#include "CryptoKey.h"
#include <runtime/JSArray.h>
#include <runtime/JSString.h>
#include <wtf/GetPtr.h>

using namespace JSC;

namespace WebCore {

/* Hash table */

static const HashTableValue JSCryptoKeyTableValues[] =
{
    { "type", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsCryptoKeyType), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "extractable", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsCryptoKeyExtractable), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "algorithm", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsCryptoKeyAlgorithm), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { "usages", DontDelete | ReadOnly, NoIntrinsic, (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsCryptoKeyUsages), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) },
    { 0, 0, NoIntrinsic, 0, 0 }
};

static const HashTable JSCryptoKeyTable = { 9, 7, true, JSCryptoKeyTableValues, 0 };
/* Hash table for prototype */

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

static const HashTable JSCryptoKeyPrototypeTable = { 1, 0, false, JSCryptoKeyPrototypeTableValues, 0 };
static const HashTable& getJSCryptoKeyPrototypeTable(VM& vm)
{
    return getHashTableForGlobalData(vm, JSCryptoKeyPrototypeTable);
}

const ClassInfo JSCryptoKeyPrototype::s_info = { "KeyPrototype", &Base::s_info, 0, getJSCryptoKeyPrototypeTable, CREATE_METHOD_TABLE(JSCryptoKeyPrototype) };

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

static const HashTable& getJSCryptoKeyTable(VM& vm)
{
    return getHashTableForGlobalData(vm, JSCryptoKeyTable);
}

const ClassInfo JSCryptoKey::s_info = { "Key", &Base::s_info, 0, getJSCryptoKeyTable , CREATE_METHOD_TABLE(JSCryptoKey) };

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

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

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

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

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

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

EncodedJSValue jsCryptoKeyType(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSCryptoKey* castedThis = jsDynamicCast<JSCryptoKey*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    CryptoKey& impl = castedThis->impl();
    JSValue result = jsStringWithCache(exec, impl.type());
    return JSValue::encode(result);
}


EncodedJSValue jsCryptoKeyExtractable(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSCryptoKey* castedThis = jsDynamicCast<JSCryptoKey*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    CryptoKey& impl = castedThis->impl();
    JSValue result = jsBoolean(impl.extractable());
    return JSValue::encode(result);
}


EncodedJSValue jsCryptoKeyAlgorithm(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSCryptoKey* castedThis = jsDynamicCast<JSCryptoKey*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    return JSValue::encode(castedThis->algorithm(exec));
}


EncodedJSValue jsCryptoKeyUsages(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue thisValue, PropertyName)
{
    JSCryptoKey* castedThis = jsDynamicCast<JSCryptoKey*>(JSValue::decode(thisValue));
    UNUSED_PARAM(slotBase);
    if (!castedThis)
        return throwVMTypeError(exec);
    UNUSED_PARAM(exec);
    CryptoKey& impl = castedThis->impl();
    JSValue result = jsArray(exec, castedThis->globalObject(), impl.usages());
    return JSValue::encode(result);
}


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

bool JSCryptoKeyOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)
{
    JSCryptoKey* jsCryptoKey = jsCast<JSCryptoKey*>(handle.get().asCell());
    if (!isObservable(jsCryptoKey))
        return false;
    CryptoKey* root = &jsCryptoKey->impl();
    return visitor.containsOpaqueRoot(root);
}

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

JSC::JSValue toJS(JSC::ExecState* exec, JSDOMGlobalObject* globalObject, CryptoKey* impl)
{
    if (!impl)
        return jsNull();
    if (JSValue result = getExistingWrapper<JSCryptoKey>(exec, impl))
        return result;
    ReportMemoryCost<CryptoKey>::reportMemoryCost(exec, impl);
    return createNewWrapper<JSCryptoKey>(exec, globalObject, impl);
}

CryptoKey* toCryptoKey(JSC::JSValue value)
{
    return value.inherits(JSCryptoKey::info()) ? &jsCast<JSCryptoKey*>(value)->impl() : 0;
}

}

#endif // ENABLE(SUBTLE_CRYPTO)
