#include "config.h"
#include "JSClassRef.h"
#include "APICast.h"
#include "InitializeThreading.h"
#include "JSCInlines.h"
#include "JSCallbackObject.h"
using namespace JSC;
const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
: parentClass(definition->parentClass)
, prototypeClass(nullptr)
, initialize(definition->initialize)
, finalize(definition->finalize)
, hasProperty(definition->hasProperty)
, getProperty(definition->getProperty)
, setProperty(definition->setProperty)
, deleteProperty(definition->deleteProperty)
, getPropertyNames(definition->getPropertyNames)
, callAsFunction(definition->callAsFunction)
, callAsConstructor(definition->callAsConstructor)
, hasInstance(definition->hasInstance)
, convertToType(definition->convertToType)
, m_className(String::fromUTF8(definition->className))
{
JSC::initialize();
if (const JSStaticValue* staticValue = definition->staticValues) {
m_staticValues = makeUnique<OpaqueJSClassStaticValuesTable>();
while (staticValue->name) {
String valueName = String::fromUTF8(staticValue->name);
if (!valueName.isNull())
m_staticValues->set(valueName.impl(), makeUnique<StaticValueEntry>(staticValue->getProperty, staticValue->setProperty, staticValue->attributes, valueName));
++staticValue;
}
}
if (const JSStaticFunction* staticFunction = definition->staticFunctions) {
m_staticFunctions = makeUnique<OpaqueJSClassStaticFunctionsTable>();
while (staticFunction->name) {
String functionName = String::fromUTF8(staticFunction->name);
if (!functionName.isNull())
m_staticFunctions->set(functionName.impl(), makeUnique<StaticFunctionEntry>(staticFunction->callAsFunction, staticFunction->attributes));
++staticFunction;
}
}
if (protoClass)
prototypeClass = JSClassRetain(protoClass);
}
OpaqueJSClass::~OpaqueJSClass()
{
ASSERT(!m_className.length() || !m_className.impl()->isAtom());
#ifndef NDEBUG
if (m_staticValues) {
OpaqueJSClassStaticValuesTable::const_iterator end = m_staticValues->end();
for (OpaqueJSClassStaticValuesTable::const_iterator it = m_staticValues->begin(); it != end; ++it)
ASSERT(!it->key->isAtom());
}
if (m_staticFunctions) {
OpaqueJSClassStaticFunctionsTable::const_iterator end = m_staticFunctions->end();
for (OpaqueJSClassStaticFunctionsTable::const_iterator it = m_staticFunctions->begin(); it != end; ++it)
ASSERT(!it->key->isAtom());
}
#endif
if (prototypeClass)
JSClassRelease(prototypeClass);
}
Ref<OpaqueJSClass> OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition* definition)
{
return adoptRef(*new OpaqueJSClass(definition, nullptr));
}
Ref<OpaqueJSClass> OpaqueJSClass::create(const JSClassDefinition* clientDefinition)
{
JSClassDefinition definition = *clientDefinition;
JSClassDefinition protoDefinition = kJSClassDefinitionEmpty;
protoDefinition.finalize = nullptr;
std::swap(definition.staticFunctions, protoDefinition.staticFunctions);
RefPtr<OpaqueJSClass> protoClass = adoptRef(new OpaqueJSClass(&protoDefinition, nullptr));
return adoptRef(*new OpaqueJSClass(&definition, protoClass.get()));
}
OpaqueJSClassContextData::OpaqueJSClassContextData(JSC::VM&, OpaqueJSClass* jsClass)
: m_class(jsClass)
{
if (jsClass->m_staticValues) {
staticValues = makeUnique<OpaqueJSClassStaticValuesTable>();
OpaqueJSClassStaticValuesTable::const_iterator end = jsClass->m_staticValues->end();
for (OpaqueJSClassStaticValuesTable::const_iterator it = jsClass->m_staticValues->begin(); it != end; ++it) {
ASSERT(!it->key->isAtom());
String valueName = it->key->isolatedCopy();
staticValues->add(valueName.impl(), makeUnique<StaticValueEntry>(it->value->getProperty, it->value->setProperty, it->value->attributes, valueName));
}
}
if (jsClass->m_staticFunctions) {
staticFunctions = makeUnique<OpaqueJSClassStaticFunctionsTable>();
OpaqueJSClassStaticFunctionsTable::const_iterator end = jsClass->m_staticFunctions->end();
for (OpaqueJSClassStaticFunctionsTable::const_iterator it = jsClass->m_staticFunctions->begin(); it != end; ++it) {
ASSERT(!it->key->isAtom());
staticFunctions->add(it->key->isolatedCopy(), makeUnique<StaticFunctionEntry>(it->value->callAsFunction, it->value->attributes));
}
}
}
OpaqueJSClassContextData& OpaqueJSClass::contextData(JSGlobalObject* globalObject)
{
std::unique_ptr<OpaqueJSClassContextData>& contextData = globalObject->opaqueJSClassData().add(this, nullptr).iterator->value;
if (!contextData)
contextData = makeUnique<OpaqueJSClassContextData>(globalObject->vm(), this);
return *contextData;
}
String OpaqueJSClass::className()
{
return m_className.isolatedCopy();
}
OpaqueJSClassStaticValuesTable* OpaqueJSClass::staticValues(JSC::JSGlobalObject* globalObject)
{
return contextData(globalObject).staticValues.get();
}
OpaqueJSClassStaticFunctionsTable* OpaqueJSClass::staticFunctions(JSC::JSGlobalObject* globalObject)
{
return contextData(globalObject).staticFunctions.get();
}
JSObject* OpaqueJSClass::prototype(JSGlobalObject* globalObject)
{
if (!prototypeClass)
return nullptr;
OpaqueJSClassContextData& jsClassData = contextData(globalObject);
if (JSObject* prototype = jsClassData.cachedPrototype.get())
return prototype;
JSObject* prototype = JSCallbackObject<JSNonFinalObject>::create(globalObject, globalObject->callbackObjectStructure(), prototypeClass, &jsClassData); if (parentClass) {
if (JSObject* parentPrototype = parentClass->prototype(globalObject))
prototype->setPrototypeDirect(globalObject->vm(), parentPrototype);
}
jsClassData.cachedPrototype = Weak<JSObject>(prototype);
return prototype;
}