JSClassRef.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "JSClassRef.h"
  27. #include "APICast.h"
  28. #include "Identifier.h"
  29. #include "InitializeThreading.h"
  30. #include "JSCallbackObject.h"
  31. #include "JSGlobalObject.h"
  32. #include "JSObjectRef.h"
  33. #include "ObjectPrototype.h"
  34. #include "Operations.h"
  35. #include <wtf/text/StringHash.h>
  36. #include <wtf/unicode/UTF8.h>
  37. using namespace std;
  38. using namespace JSC;
  39. using namespace WTF::Unicode;
  40. const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  41. OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
  42. : parentClass(definition->parentClass)
  43. , prototypeClass(0)
  44. , initialize(definition->initialize)
  45. , finalize(definition->finalize)
  46. , hasProperty(definition->hasProperty)
  47. , getProperty(definition->getProperty)
  48. , setProperty(definition->setProperty)
  49. , deleteProperty(definition->deleteProperty)
  50. , getPropertyNames(definition->getPropertyNames)
  51. , callAsFunction(definition->callAsFunction)
  52. , callAsConstructor(definition->callAsConstructor)
  53. , hasInstance(definition->hasInstance)
  54. , convertToType(definition->convertToType)
  55. , m_className(String::fromUTF8(definition->className))
  56. {
  57. initializeThreading();
  58. if (const JSStaticValue* staticValue = definition->staticValues) {
  59. m_staticValues = adoptPtr(new OpaqueJSClassStaticValuesTable);
  60. while (staticValue->name) {
  61. String valueName = String::fromUTF8(staticValue->name);
  62. if (!valueName.isNull())
  63. m_staticValues->set(valueName.impl(), adoptPtr(new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes)));
  64. ++staticValue;
  65. }
  66. }
  67. if (const JSStaticFunction* staticFunction = definition->staticFunctions) {
  68. m_staticFunctions = adoptPtr(new OpaqueJSClassStaticFunctionsTable);
  69. while (staticFunction->name) {
  70. String functionName = String::fromUTF8(staticFunction->name);
  71. if (!functionName.isNull())
  72. m_staticFunctions->set(functionName.impl(), adoptPtr(new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes)));
  73. ++staticFunction;
  74. }
  75. }
  76. if (protoClass)
  77. prototypeClass = JSClassRetain(protoClass);
  78. }
  79. OpaqueJSClass::~OpaqueJSClass()
  80. {
  81. // The empty string is shared across threads & is an identifier, in all other cases we should have done a deep copy in className(), below.
  82. ASSERT(!m_className.length() || !m_className.impl()->isIdentifier());
  83. #ifndef NDEBUG
  84. if (m_staticValues) {
  85. OpaqueJSClassStaticValuesTable::const_iterator end = m_staticValues->end();
  86. for (OpaqueJSClassStaticValuesTable::const_iterator it = m_staticValues->begin(); it != end; ++it)
  87. ASSERT(!it->key->isIdentifier());
  88. }
  89. if (m_staticFunctions) {
  90. OpaqueJSClassStaticFunctionsTable::const_iterator end = m_staticFunctions->end();
  91. for (OpaqueJSClassStaticFunctionsTable::const_iterator it = m_staticFunctions->begin(); it != end; ++it)
  92. ASSERT(!it->key->isIdentifier());
  93. }
  94. #endif
  95. if (prototypeClass)
  96. JSClassRelease(prototypeClass);
  97. }
  98. PassRefPtr<OpaqueJSClass> OpaqueJSClass::createNoAutomaticPrototype(const JSClassDefinition* definition)
  99. {
  100. return adoptRef(new OpaqueJSClass(definition, 0));
  101. }
  102. PassRefPtr<OpaqueJSClass> OpaqueJSClass::create(const JSClassDefinition* clientDefinition)
  103. {
  104. JSClassDefinition definition = *clientDefinition; // Avoid modifying client copy.
  105. JSClassDefinition protoDefinition = kJSClassDefinitionEmpty;
  106. protoDefinition.finalize = 0;
  107. swap(definition.staticFunctions, protoDefinition.staticFunctions); // Move static functions to the prototype.
  108. // We are supposed to use JSClassRetain/Release but since we know that we currently have
  109. // the only reference to this class object we cheat and use a RefPtr instead.
  110. RefPtr<OpaqueJSClass> protoClass = adoptRef(new OpaqueJSClass(&protoDefinition, 0));
  111. return adoptRef(new OpaqueJSClass(&definition, protoClass.get()));
  112. }
  113. OpaqueJSClassContextData::OpaqueJSClassContextData(JSC::VM&, OpaqueJSClass* jsClass)
  114. : m_class(jsClass)
  115. {
  116. if (jsClass->m_staticValues) {
  117. staticValues = adoptPtr(new OpaqueJSClassStaticValuesTable);
  118. OpaqueJSClassStaticValuesTable::const_iterator end = jsClass->m_staticValues->end();
  119. for (OpaqueJSClassStaticValuesTable::const_iterator it = jsClass->m_staticValues->begin(); it != end; ++it) {
  120. ASSERT(!it->key->isIdentifier());
  121. staticValues->add(it->key->isolatedCopy(), adoptPtr(new StaticValueEntry(it->value->getProperty, it->value->setProperty, it->value->attributes)));
  122. }
  123. }
  124. if (jsClass->m_staticFunctions) {
  125. staticFunctions = adoptPtr(new OpaqueJSClassStaticFunctionsTable);
  126. OpaqueJSClassStaticFunctionsTable::const_iterator end = jsClass->m_staticFunctions->end();
  127. for (OpaqueJSClassStaticFunctionsTable::const_iterator it = jsClass->m_staticFunctions->begin(); it != end; ++it) {
  128. ASSERT(!it->key->isIdentifier());
  129. staticFunctions->add(it->key->isolatedCopy(), adoptPtr(new StaticFunctionEntry(it->value->callAsFunction, it->value->attributes)));
  130. }
  131. }
  132. }
  133. OpaqueJSClassContextData& OpaqueJSClass::contextData(ExecState* exec)
  134. {
  135. OwnPtr<OpaqueJSClassContextData>& contextData = exec->lexicalGlobalObject()->opaqueJSClassData().add(this, nullptr).iterator->value;
  136. if (!contextData)
  137. contextData = adoptPtr(new OpaqueJSClassContextData(exec->vm(), this));
  138. return *contextData;
  139. }
  140. String OpaqueJSClass::className()
  141. {
  142. // Make a deep copy, so that the caller has no chance to put the original into IdentifierTable.
  143. return m_className.isolatedCopy();
  144. }
  145. OpaqueJSClassStaticValuesTable* OpaqueJSClass::staticValues(JSC::ExecState* exec)
  146. {
  147. return contextData(exec).staticValues.get();
  148. }
  149. OpaqueJSClassStaticFunctionsTable* OpaqueJSClass::staticFunctions(JSC::ExecState* exec)
  150. {
  151. return contextData(exec).staticFunctions.get();
  152. }
  153. /*!
  154. // Doc here in case we make this public. (Hopefully we won't.)
  155. @function
  156. @abstract Returns the prototype that will be used when constructing an object with a given class.
  157. @param ctx The execution context to use.
  158. @param jsClass A JSClass whose prototype you want to get.
  159. @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.
  160. */
  161. JSObject* OpaqueJSClass::prototype(ExecState* exec)
  162. {
  163. /* Class (C++) and prototype (JS) inheritance are parallel, so:
  164. * (C++) | (JS)
  165. * ParentClass | ParentClassPrototype
  166. * ^ | ^
  167. * | | |
  168. * DerivedClass | DerivedClassPrototype
  169. */
  170. if (!prototypeClass)
  171. return 0;
  172. OpaqueJSClassContextData& jsClassData = contextData(exec);
  173. if (JSObject* prototype = jsClassData.cachedPrototype.get())
  174. return prototype;
  175. // Recursive, but should be good enough for our purposes
  176. 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
  177. if (parentClass) {
  178. if (JSObject* parentPrototype = parentClass->prototype(exec))
  179. prototype->setPrototype(exec->vm(), parentPrototype);
  180. }
  181. jsClassData.cachedPrototype = PassWeak<JSObject>(prototype);
  182. return prototype;
  183. }