JSClassRef.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2006 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 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 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. #ifndef JSClassRef_h
  26. #define JSClassRef_h
  27. #include "OpaqueJSString.h"
  28. #include "Protect.h"
  29. #include "Weak.h"
  30. #include <JavaScriptCore/JSObjectRef.h>
  31. #include <wtf/HashMap.h>
  32. #include <wtf/text/WTFString.h>
  33. struct StaticValueEntry {
  34. WTF_MAKE_FAST_ALLOCATED;
  35. public:
  36. StaticValueEntry(JSObjectGetPropertyCallback _getProperty, JSObjectSetPropertyCallback _setProperty, JSPropertyAttributes _attributes, String& propertyName)
  37. : getProperty(_getProperty)
  38. , setProperty(_setProperty)
  39. , attributes(_attributes)
  40. , propertyNameRef(OpaqueJSString::tryCreate(propertyName))
  41. {
  42. }
  43. JSObjectGetPropertyCallback getProperty;
  44. JSObjectSetPropertyCallback setProperty;
  45. JSPropertyAttributes attributes;
  46. RefPtr<OpaqueJSString> propertyNameRef;
  47. };
  48. struct StaticFunctionEntry {
  49. WTF_MAKE_FAST_ALLOCATED;
  50. public:
  51. StaticFunctionEntry(JSObjectCallAsFunctionCallback _callAsFunction, JSPropertyAttributes _attributes)
  52. : callAsFunction(_callAsFunction), attributes(_attributes)
  53. {
  54. }
  55. JSObjectCallAsFunctionCallback callAsFunction;
  56. JSPropertyAttributes attributes;
  57. };
  58. typedef HashMap<RefPtr<StringImpl>, std::unique_ptr<StaticValueEntry>> OpaqueJSClassStaticValuesTable;
  59. typedef HashMap<RefPtr<StringImpl>, std::unique_ptr<StaticFunctionEntry>> OpaqueJSClassStaticFunctionsTable;
  60. struct OpaqueJSClass;
  61. // An OpaqueJSClass (JSClass) is created without a context, so it can be used with any context, even across context groups.
  62. // This structure holds data members that vary across context groups.
  63. struct OpaqueJSClassContextData {
  64. WTF_MAKE_NONCOPYABLE(OpaqueJSClassContextData); WTF_MAKE_FAST_ALLOCATED;
  65. public:
  66. OpaqueJSClassContextData(JSC::VM&, OpaqueJSClass*);
  67. // It is necessary to keep OpaqueJSClass alive because of the following rare scenario:
  68. // 1. A class is created and used, so its context data is stored in VM hash map.
  69. // 2. The class is released, and when all JS objects that use it are collected, OpaqueJSClass
  70. // is deleted (that's the part prevented by this RefPtr).
  71. // 3. Another class is created at the same address.
  72. // 4. When it is used, the old context data is found in VM and used.
  73. RefPtr<OpaqueJSClass> m_class;
  74. std::unique_ptr<OpaqueJSClassStaticValuesTable> staticValues;
  75. std::unique_ptr<OpaqueJSClassStaticFunctionsTable> staticFunctions;
  76. JSC::Weak<JSC::JSObject> cachedPrototype;
  77. };
  78. struct OpaqueJSClass : public ThreadSafeRefCounted<OpaqueJSClass> {
  79. static Ref<OpaqueJSClass> create(const JSClassDefinition*);
  80. static Ref<OpaqueJSClass> createNoAutomaticPrototype(const JSClassDefinition*);
  81. JS_EXPORT_PRIVATE ~OpaqueJSClass();
  82. String className();
  83. OpaqueJSClassStaticValuesTable* staticValues(JSC::ExecState*);
  84. OpaqueJSClassStaticFunctionsTable* staticFunctions(JSC::ExecState*);
  85. JSC::JSObject* prototype(JSC::ExecState*);
  86. OpaqueJSClass* parentClass;
  87. OpaqueJSClass* prototypeClass;
  88. JSObjectInitializeCallback initialize;
  89. JSObjectFinalizeCallback finalize;
  90. JSObjectHasPropertyCallback hasProperty;
  91. JSObjectGetPropertyCallback getProperty;
  92. JSObjectSetPropertyCallback setProperty;
  93. JSObjectDeletePropertyCallback deleteProperty;
  94. JSObjectGetPropertyNamesCallback getPropertyNames;
  95. JSObjectCallAsFunctionCallback callAsFunction;
  96. JSObjectCallAsConstructorCallback callAsConstructor;
  97. JSObjectHasInstanceCallback hasInstance;
  98. JSObjectConvertToTypeCallback convertToType;
  99. private:
  100. friend struct OpaqueJSClassContextData;
  101. OpaqueJSClass();
  102. OpaqueJSClass(const OpaqueJSClass&);
  103. OpaqueJSClass(const JSClassDefinition*, OpaqueJSClass* protoClass);
  104. OpaqueJSClassContextData& contextData(JSC::ExecState*);
  105. // Strings in these data members should not be put into any AtomicStringTable.
  106. String m_className;
  107. std::unique_ptr<OpaqueJSClassStaticValuesTable> m_staticValues;
  108. std::unique_ptr<OpaqueJSClassStaticFunctionsTable> m_staticFunctions;
  109. };
  110. #endif // JSClassRef_h