X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/f9bf01c6616d5ddcf65b13b33cedf9e387ff7a63..12899fa232562c774004a3a9d7d3149944dec712:/API/JSClassRef.cpp diff --git a/API/JSClassRef.cpp b/API/JSClassRef.cpp index d75d5cd..c77f63c 100644 --- a/API/JSClassRef.cpp +++ b/API/JSClassRef.cpp @@ -27,15 +27,19 @@ #include "JSClassRef.h" #include "APICast.h" +#include "Identifier.h" +#include "InitializeThreading.h" #include "JSCallbackObject.h" +#include "JSGlobalObject.h" #include "JSObjectRef.h" -#include -#include -#include -#include +#include "ObjectPrototype.h" +#include "Operations.h" +#include +#include using namespace std; using namespace JSC; +using namespace WTF::Unicode; const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; @@ -53,28 +57,26 @@ OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* , callAsConstructor(definition->callAsConstructor) , hasInstance(definition->hasInstance) , convertToType(definition->convertToType) - , m_className(UString::createFromUTF8(definition->className).rep()->ref()) - , m_staticValues(0) - , m_staticFunctions(0) + , m_className(String::fromUTF8(definition->className)) { initializeThreading(); if (const JSStaticValue* staticValue = definition->staticValues) { - m_staticValues = new OpaqueJSClassStaticValuesTable(); + m_staticValues = adoptPtr(new OpaqueJSClassStaticValuesTable); while (staticValue->name) { - // Use a local variable here to sidestep an RVCT compiler bug. - StaticValueEntry* entry = new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes); - m_staticValues->add(UString::createFromUTF8(staticValue->name).rep()->ref(), entry); + String valueName = String::fromUTF8(staticValue->name); + if (!valueName.isNull()) + m_staticValues->set(valueName.impl(), adoptPtr(new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes))); ++staticValue; } } if (const JSStaticFunction* staticFunction = definition->staticFunctions) { - m_staticFunctions = new OpaqueJSClassStaticFunctionsTable(); + m_staticFunctions = adoptPtr(new OpaqueJSClassStaticFunctionsTable); while (staticFunction->name) { - // Use a local variable here to sidestep an RVCT compiler bug. - StaticFunctionEntry* entry = new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes); - m_staticFunctions->add(UString::createFromUTF8(staticFunction->name).rep()->ref(), entry); + String functionName = String::fromUTF8(staticFunction->name); + if (!functionName.isNull()) + m_staticFunctions->set(functionName.impl(), adoptPtr(new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes))); ++staticFunction; } } @@ -85,25 +87,22 @@ OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* OpaqueJSClass::~OpaqueJSClass() { - ASSERT(!m_className.rep()->isIdentifier()); + // The empty string is shared across threads & is an identifier, in all other cases we should have done a deep copy in className(), below. + ASSERT(!m_className.length() || !m_className.impl()->isIdentifier()); +#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->first->isIdentifier()); - delete it->second; - } - delete m_staticValues; + for (OpaqueJSClassStaticValuesTable::const_iterator it = m_staticValues->begin(); it != end; ++it) + ASSERT(!it->key->isIdentifier()); } if (m_staticFunctions) { OpaqueJSClassStaticFunctionsTable::const_iterator end = m_staticFunctions->end(); - for (OpaqueJSClassStaticFunctionsTable::const_iterator it = m_staticFunctions->begin(); it != end; ++it) { - ASSERT(!it->first->isIdentifier()); - delete it->second; - } - delete m_staticFunctions; + for (OpaqueJSClassStaticFunctionsTable::const_iterator it = m_staticFunctions->begin(); it != end; ++it) + ASSERT(!it->key->isIdentifier()); } +#endif if (prototypeClass) JSClassRelease(prototypeClass); @@ -114,19 +113,12 @@ PassRefPtr OpaqueJSClass::createNoAutomaticPrototype(const JSClas return adoptRef(new OpaqueJSClass(definition, 0)); } -static void clearReferenceToPrototype(JSObjectRef prototype) -{ - OpaqueJSClassContextData* jsClassData = static_cast(JSObjectGetPrivate(prototype)); - ASSERT(jsClassData); - jsClassData->cachedPrototype.clear(toJS(prototype)); -} - PassRefPtr OpaqueJSClass::create(const JSClassDefinition* clientDefinition) { JSClassDefinition definition = *clientDefinition; // Avoid modifying client copy. JSClassDefinition protoDefinition = kJSClassDefinitionEmpty; - protoDefinition.finalize = clearReferenceToPrototype; + protoDefinition.finalize = 0; swap(definition.staticFunctions, protoDefinition.staticFunctions); // Move static functions to the prototype. // We are supposed to use JSClassRetain/Release but since we know that we currently have @@ -135,75 +127,50 @@ PassRefPtr OpaqueJSClass::create(const JSClassDefinition* clientD return adoptRef(new OpaqueJSClass(&definition, protoClass.get())); } -OpaqueJSClassContextData::OpaqueJSClassContextData(OpaqueJSClass* jsClass) +OpaqueJSClassContextData::OpaqueJSClassContextData(JSC::VM&, OpaqueJSClass* jsClass) : m_class(jsClass) { if (jsClass->m_staticValues) { - staticValues = new OpaqueJSClassStaticValuesTable; + staticValues = adoptPtr(new OpaqueJSClassStaticValuesTable); OpaqueJSClassStaticValuesTable::const_iterator end = jsClass->m_staticValues->end(); for (OpaqueJSClassStaticValuesTable::const_iterator it = jsClass->m_staticValues->begin(); it != end; ++it) { - ASSERT(!it->first->isIdentifier()); - // Use a local variable here to sidestep an RVCT compiler bug. - StaticValueEntry* entry = new StaticValueEntry(it->second->getProperty, it->second->setProperty, it->second->attributes); - staticValues->add(UString::Rep::create(it->first->data(), it->first->size()), entry); - + ASSERT(!it->key->isIdentifier()); + staticValues->add(it->key->isolatedCopy(), adoptPtr(new StaticValueEntry(it->value->getProperty, it->value->setProperty, it->value->attributes))); } - - } else - staticValues = 0; - + } if (jsClass->m_staticFunctions) { - staticFunctions = new OpaqueJSClassStaticFunctionsTable; + staticFunctions = adoptPtr(new OpaqueJSClassStaticFunctionsTable); OpaqueJSClassStaticFunctionsTable::const_iterator end = jsClass->m_staticFunctions->end(); for (OpaqueJSClassStaticFunctionsTable::const_iterator it = jsClass->m_staticFunctions->begin(); it != end; ++it) { - ASSERT(!it->first->isIdentifier()); - // Use a local variable here to sidestep an RVCT compiler bug. - StaticFunctionEntry* entry = new StaticFunctionEntry(it->second->callAsFunction, it->second->attributes); - staticFunctions->add(UString::Rep::create(it->first->data(), it->first->size()), entry); + ASSERT(!it->key->isIdentifier()); + staticFunctions->add(it->key->isolatedCopy(), adoptPtr(new StaticFunctionEntry(it->value->callAsFunction, it->value->attributes))); } - - } else - staticFunctions = 0; -} - -OpaqueJSClassContextData::~OpaqueJSClassContextData() -{ - if (staticValues) { - deleteAllValues(*staticValues); - delete staticValues; - } - - if (staticFunctions) { - deleteAllValues(*staticFunctions); - delete staticFunctions; } } OpaqueJSClassContextData& OpaqueJSClass::contextData(ExecState* exec) { - OpaqueJSClassContextData*& contextData = exec->globalData().opaqueJSClassData.add(this, 0).first->second; + OwnPtr& contextData = exec->lexicalGlobalObject()->opaqueJSClassData().add(this, nullptr).iterator->value; if (!contextData) - contextData = new OpaqueJSClassContextData(this); + contextData = adoptPtr(new OpaqueJSClassContextData(exec->vm(), this)); return *contextData; } -UString OpaqueJSClass::className() +String OpaqueJSClass::className() { // Make a deep copy, so that the caller has no chance to put the original into IdentifierTable. - return UString(m_className.data(), m_className.size()); + return m_className.isolatedCopy(); } OpaqueJSClassStaticValuesTable* OpaqueJSClass::staticValues(JSC::ExecState* exec) { - OpaqueJSClassContextData& jsClassData = contextData(exec); - return jsClassData.staticValues; + return contextData(exec).staticValues.get(); } OpaqueJSClassStaticFunctionsTable* OpaqueJSClass::staticFunctions(JSC::ExecState* exec) { - OpaqueJSClassContextData& jsClassData = contextData(exec); - return jsClassData.staticFunctions; + return contextData(exec).staticFunctions.get(); } /*! @@ -223,19 +190,22 @@ JSObject* OpaqueJSClass::prototype(ExecState* exec) * | | | * DerivedClass | DerivedClassPrototype */ - + if (!prototypeClass) return 0; OpaqueJSClassContextData& jsClassData = contextData(exec); - if (!jsClassData.cachedPrototype) { - // Recursive, but should be good enough for our purposes - jsClassData.cachedPrototype = new (exec) JSCallbackObject(exec, exec->lexicalGlobalObject()->callbackObjectStructure(), prototypeClass, &jsClassData); // set jsClassData as the object's private data, so it can clear our reference on destruction - if (parentClass) { - if (JSObject* prototype = parentClass->prototype(exec)) - jsClassData.cachedPrototype->setPrototype(prototype); - } + if (JSObject* prototype = jsClassData.cachedPrototype.get()) + return prototype; + + // Recursive, but should be good enough for our purposes + JSObject* prototype = JSCallbackObject::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->callbackObjectStructure(), prototypeClass, &jsClassData); // set jsClassData as the object's private data, so it can clear our reference on destruction + if (parentClass) { + if (JSObject* parentPrototype = parentClass->prototype(exec)) + prototype->setPrototype(exec->vm(), parentPrototype); } - return jsClassData.cachedPrototype.get(); + + jsClassData.cachedPrototype = PassWeak(prototype); + return prototype; }