JSCallbackObject.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2006-2019 Apple Inc. All rights reserved.
  3. * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef JSCallbackObject_h
  27. #define JSCallbackObject_h
  28. #include "JSObjectRef.h"
  29. #include "JSValueRef.h"
  30. #include "JSObject.h"
  31. namespace JSC {
  32. struct JSCallbackObjectData {
  33. WTF_MAKE_FAST_ALLOCATED;
  34. public:
  35. JSCallbackObjectData(void* privateData, JSClassRef jsClass)
  36. : privateData(privateData)
  37. , jsClass(jsClass)
  38. {
  39. JSClassRetain(jsClass);
  40. }
  41. ~JSCallbackObjectData()
  42. {
  43. JSClassRelease(jsClass);
  44. }
  45. JSValue getPrivateProperty(const Identifier& propertyName) const
  46. {
  47. if (!m_privateProperties)
  48. return JSValue();
  49. return m_privateProperties->getPrivateProperty(propertyName);
  50. }
  51. void setPrivateProperty(VM& vm, JSCell* owner, const Identifier& propertyName, JSValue value)
  52. {
  53. if (!m_privateProperties)
  54. m_privateProperties = std::make_unique<JSPrivatePropertyMap>();
  55. m_privateProperties->setPrivateProperty(vm, owner, propertyName, value);
  56. }
  57. void deletePrivateProperty(const Identifier& propertyName)
  58. {
  59. if (!m_privateProperties)
  60. return;
  61. m_privateProperties->deletePrivateProperty(propertyName);
  62. }
  63. void visitChildren(SlotVisitor& visitor)
  64. {
  65. JSPrivatePropertyMap* properties = m_privateProperties.get();
  66. if (!properties)
  67. return;
  68. properties->visitChildren(visitor);
  69. }
  70. void* privateData;
  71. JSClassRef jsClass;
  72. struct JSPrivatePropertyMap {
  73. WTF_MAKE_FAST_ALLOCATED;
  74. public:
  75. JSValue getPrivateProperty(const Identifier& propertyName) const
  76. {
  77. PrivatePropertyMap::const_iterator location = m_propertyMap.find(propertyName.impl());
  78. if (location == m_propertyMap.end())
  79. return JSValue();
  80. return location->value.get();
  81. }
  82. void setPrivateProperty(VM& vm, JSCell* owner, const Identifier& propertyName, JSValue value)
  83. {
  84. LockHolder locker(m_lock);
  85. WriteBarrier<Unknown> empty;
  86. m_propertyMap.add(propertyName.impl(), empty).iterator->value.set(vm, owner, value);
  87. }
  88. void deletePrivateProperty(const Identifier& propertyName)
  89. {
  90. LockHolder locker(m_lock);
  91. m_propertyMap.remove(propertyName.impl());
  92. }
  93. void visitChildren(SlotVisitor& visitor)
  94. {
  95. LockHolder locker(m_lock);
  96. for (auto& pair : m_propertyMap) {
  97. if (pair.value)
  98. visitor.append(pair.value);
  99. }
  100. }
  101. private:
  102. typedef HashMap<RefPtr<UniquedStringImpl>, WriteBarrier<Unknown>, IdentifierRepHash> PrivatePropertyMap;
  103. PrivatePropertyMap m_propertyMap;
  104. Lock m_lock;
  105. };
  106. std::unique_ptr<JSPrivatePropertyMap> m_privateProperties;
  107. };
  108. template <class Parent>
  109. class JSCallbackObject final : public Parent {
  110. protected:
  111. JSCallbackObject(ExecState*, Structure*, JSClassRef, void* data);
  112. JSCallbackObject(VM&, JSClassRef, Structure*);
  113. void finishCreation(ExecState*);
  114. void finishCreation(VM&);
  115. public:
  116. typedef Parent Base;
  117. static const unsigned StructureFlags = Base::StructureFlags | ProhibitsPropertyCaching | OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | ImplementsHasInstance | OverridesGetPropertyNames | OverridesGetCallData;
  118. static_assert(!(StructureFlags & ImplementsDefaultHasInstance), "using customHasInstance");
  119. ~JSCallbackObject();
  120. static JSCallbackObject* create(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, JSClassRef classRef, void* data)
  121. {
  122. VM& vm = exec->vm();
  123. ASSERT_UNUSED(globalObject, !structure->globalObject() || structure->globalObject() == globalObject);
  124. JSCallbackObject* callbackObject = new (NotNull, allocateCell<JSCallbackObject>(vm.heap)) JSCallbackObject(exec, structure, classRef, data);
  125. callbackObject->finishCreation(exec);
  126. return callbackObject;
  127. }
  128. static JSCallbackObject<Parent>* create(VM&, JSClassRef, Structure*);
  129. static const bool needsDestruction;
  130. static void destroy(JSCell* cell)
  131. {
  132. static_cast<JSCallbackObject*>(cell)->JSCallbackObject::~JSCallbackObject();
  133. }
  134. void setPrivate(void* data);
  135. void* getPrivate();
  136. // FIXME: We should fix the warnings for extern-template in JSObject template classes: https://bugs.webkit.org/show_bug.cgi?id=161979
  137. IGNORE_CLANG_WARNINGS_BEGIN("undefined-var-template")
  138. DECLARE_INFO;
  139. IGNORE_CLANG_WARNINGS_END
  140. JSClassRef classRef() const { return m_callbackObjectData->jsClass; }
  141. bool inherits(JSClassRef) const;
  142. static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
  143. JSValue getPrivateProperty(const Identifier& propertyName) const
  144. {
  145. return m_callbackObjectData->getPrivateProperty(propertyName);
  146. }
  147. void setPrivateProperty(VM& vm, const Identifier& propertyName, JSValue value)
  148. {
  149. m_callbackObjectData->setPrivateProperty(vm, this, propertyName, value);
  150. }
  151. void deletePrivateProperty(const Identifier& propertyName)
  152. {
  153. m_callbackObjectData->deletePrivateProperty(propertyName);
  154. }
  155. using Parent::methodTable;
  156. private:
  157. static String className(const JSObject*, VM&);
  158. static String toStringName(const JSObject*, ExecState*);
  159. static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType);
  160. static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
  161. static bool getOwnPropertySlotByIndex(JSObject*, ExecState*, unsigned propertyName, PropertySlot&);
  162. static bool put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
  163. static bool putByIndex(JSCell*, ExecState*, unsigned, JSValue, bool shouldThrow);
  164. static bool deleteProperty(JSCell*, ExecState*, PropertyName);
  165. static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned);
  166. static bool customHasInstance(JSObject*, ExecState*, JSValue);
  167. static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  168. static ConstructType getConstructData(JSCell*, ConstructData&);
  169. static CallType getCallData(JSCell*, CallData&);
  170. static void visitChildren(JSCell* cell, SlotVisitor& visitor)
  171. {
  172. JSCallbackObject* thisObject = jsCast<JSCallbackObject*>(cell);
  173. ASSERT_GC_OBJECT_INHERITS((static_cast<Parent*>(thisObject)), JSCallbackObject<Parent>::info());
  174. Parent::visitChildren(thisObject, visitor);
  175. thisObject->m_callbackObjectData->visitChildren(visitor);
  176. }
  177. void init(ExecState*);
  178. static JSCallbackObject* asCallbackObject(JSValue);
  179. static JSCallbackObject* asCallbackObject(EncodedJSValue);
  180. static EncodedJSValue JSC_HOST_CALL call(ExecState*);
  181. static EncodedJSValue JSC_HOST_CALL construct(ExecState*);
  182. JSValue getStaticValue(ExecState*, PropertyName);
  183. static EncodedJSValue staticFunctionGetter(ExecState*, EncodedJSValue, PropertyName);
  184. static EncodedJSValue callbackGetter(ExecState*, EncodedJSValue, PropertyName);
  185. std::unique_ptr<JSCallbackObjectData> m_callbackObjectData;
  186. const ClassInfo* m_classInfo { nullptr };
  187. };
  188. } // namespace JSC
  189. // include the actual template class implementation
  190. #include "JSCallbackObjectFunctions.h"
  191. #endif // JSCallbackObject_h