123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- /*
- * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "config.h"
- #include "JSClassRef.h"
- #include "APICast.h"
- #include "Identifier.h"
- #include "InitializeThreading.h"
- #include "JSCallbackObject.h"
- #include "JSGlobalObject.h"
- #include "JSObjectRef.h"
- #include "ObjectPrototype.h"
- #include "Operations.h"
- #include <wtf/text/StringHash.h>
- #include <wtf/unicode/UTF8.h>
- 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 };
- OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
- : parentClass(definition->parentClass)
- , prototypeClass(0)
- , 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))
- {
- initializeThreading();
- if (const JSStaticValue* staticValue = definition->staticValues) {
- m_staticValues = adoptPtr(new OpaqueJSClassStaticValuesTable);
- while (staticValue->name) {
- 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 = adoptPtr(new OpaqueJSClassStaticFunctionsTable);
- while (staticFunction->name) {
- String functionName = String::fromUTF8(staticFunction->name);
- if (!functionName.isNull())
- m_staticFunctions->set(functionName.impl(), adoptPtr(new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes)));
- ++staticFunction;
- }
- }
-
- if (protoClass)
- prototypeClass = JSClassRetain(protoClass);
- }
- OpaqueJSClass::~OpaqueJSClass()
- {
- // 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->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->key->isIdentifier());
- }
- #endif
-
- if (prototypeClass)
- JSClassRelease(prototypeClass);
- }
- PassRefPtr<OpaqueJSClass> OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition* definition)
- {
- return adoptRef(new OpaqueJSClass(definition, 0));
- }
- PassRefPtr<OpaqueJSClass> OpaqueJSClass::create(const JSClassDefinition* clientDefinition)
- {
- JSClassDefinition definition = *clientDefinition; // Avoid modifying client copy.
- JSClassDefinition protoDefinition = kJSClassDefinitionEmpty;
- 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
- // the only reference to this class object we cheat and use a RefPtr instead.
- RefPtr<OpaqueJSClass> protoClass = adoptRef(new OpaqueJSClass(&protoDefinition, 0));
- return adoptRef(new OpaqueJSClass(&definition, protoClass.get()));
- }
- OpaqueJSClassContextData::OpaqueJSClassContextData(JSC::VM&, OpaqueJSClass* jsClass)
- : m_class(jsClass)
- {
- if (jsClass->m_staticValues) {
- 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->key->isIdentifier());
- staticValues->add(it->key->isolatedCopy(), adoptPtr(new StaticValueEntry(it->value->getProperty, it->value->setProperty, it->value->attributes)));
- }
- }
- if (jsClass->m_staticFunctions) {
- 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->key->isIdentifier());
- staticFunctions->add(it->key->isolatedCopy(), adoptPtr(new StaticFunctionEntry(it->value->callAsFunction, it->value->attributes)));
- }
- }
- }
- OpaqueJSClassContextData& OpaqueJSClass::contextData(ExecState* exec)
- {
- OwnPtr<OpaqueJSClassContextData>& contextData = exec->lexicalGlobalObject()->opaqueJSClassData().add(this, nullptr).iterator->value;
- if (!contextData)
- contextData = adoptPtr(new OpaqueJSClassContextData(exec->vm(), this));
- return *contextData;
- }
- String OpaqueJSClass::className()
- {
- // Make a deep copy, so that the caller has no chance to put the original into IdentifierTable.
- return m_className.isolatedCopy();
- }
- OpaqueJSClassStaticValuesTable* OpaqueJSClass::staticValues(JSC::ExecState* exec)
- {
- return contextData(exec).staticValues.get();
- }
- OpaqueJSClassStaticFunctionsTable* OpaqueJSClass::staticFunctions(JSC::ExecState* exec)
- {
- return contextData(exec).staticFunctions.get();
- }
- /*!
- // Doc here in case we make this public. (Hopefully we won't.)
- @function
- @abstract Returns the prototype that will be used when constructing an object with a given class.
- @param ctx The execution context to use.
- @param jsClass A JSClass whose prototype you want to get.
- @result The JSObject prototype that was automatically generated for jsClass, or NULL if no prototype was automatically generated. This is the prototype that will be used when constructing an object using jsClass.
- */
- JSObject* OpaqueJSClass::prototype(ExecState* exec)
- {
- /* Class (C++) and prototype (JS) inheritance are parallel, so:
- * (C++) | (JS)
- * ParentClass | ParentClassPrototype
- * ^ | ^
- * | | |
- * DerivedClass | DerivedClassPrototype
- */
- if (!prototypeClass)
- return 0;
- OpaqueJSClassContextData& jsClassData = contextData(exec);
- if (JSObject* prototype = jsClassData.cachedPrototype.get())
- return prototype;
- // Recursive, but should be good enough for our purposes
- JSObject* prototype = JSCallbackObject<JSDestructibleObject>::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);
- }
- jsClassData.cachedPrototype = PassWeak<JSObject>(prototype);
- return prototype;
- }
|