RegExpMatchesArray.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2008 Apple Inc. All Rights Reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. */
  19. #ifndef RegExpMatchesArray_h
  20. #define RegExpMatchesArray_h
  21. #include "JSArray.h"
  22. #include "JSGlobalObject.h"
  23. #include "RegExpObject.h"
  24. namespace JSC {
  25. class RegExpMatchesArray : public JSArray {
  26. private:
  27. RegExpMatchesArray(VM&, Butterfly*, JSGlobalObject*, JSString*, RegExp*, MatchResult);
  28. enum ReifiedState { ReifiedNone, ReifiedMatch, ReifiedAll };
  29. public:
  30. typedef JSArray Base;
  31. static RegExpMatchesArray* create(ExecState*, JSString*, RegExp*, MatchResult);
  32. JSString* leftContext(ExecState*);
  33. JSString* rightContext(ExecState*);
  34. static const ClassInfo s_info;
  35. static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
  36. {
  37. return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info, ArrayWithArrayStorage);
  38. }
  39. static void visitChildren(JSCell*, SlotVisitor&);
  40. protected:
  41. void finishCreation(VM&);
  42. static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | OverridesGetPropertyNames | Base::StructureFlags;
  43. private:
  44. ALWAYS_INLINE void reifyAllPropertiesIfNecessary(ExecState* exec)
  45. {
  46. if (m_state != ReifiedAll)
  47. reifyAllProperties(exec);
  48. }
  49. ALWAYS_INLINE void reifyMatchPropertyIfNecessary(ExecState* exec)
  50. {
  51. if (m_state == ReifiedNone)
  52. reifyMatchProperty(exec);
  53. }
  54. static bool getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  55. {
  56. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
  57. thisObject->reifyAllPropertiesIfNecessary(exec);
  58. return JSArray::getOwnPropertySlot(thisObject, exec, propertyName, slot);
  59. }
  60. static bool getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)
  61. {
  62. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
  63. if (propertyName)
  64. thisObject->reifyAllPropertiesIfNecessary(exec);
  65. else
  66. thisObject->reifyMatchPropertyIfNecessary(exec);
  67. return JSArray::getOwnPropertySlotByIndex(thisObject, exec, propertyName, slot);
  68. }
  69. static bool getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  70. {
  71. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(object);
  72. thisObject->reifyAllPropertiesIfNecessary(exec);
  73. return JSArray::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
  74. }
  75. static void put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue v, PutPropertySlot& slot)
  76. {
  77. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
  78. thisObject->reifyAllPropertiesIfNecessary(exec);
  79. JSArray::put(thisObject, exec, propertyName, v, slot);
  80. }
  81. static void putByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, JSValue v, bool shouldThrow)
  82. {
  83. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
  84. thisObject->reifyAllPropertiesIfNecessary(exec);
  85. JSArray::putByIndex(thisObject, exec, propertyName, v, shouldThrow);
  86. }
  87. static bool deleteProperty(JSCell* cell, ExecState* exec, PropertyName propertyName)
  88. {
  89. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
  90. thisObject->reifyAllPropertiesIfNecessary(exec);
  91. return JSArray::deleteProperty(thisObject, exec, propertyName);
  92. }
  93. static bool deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned propertyName)
  94. {
  95. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
  96. thisObject->reifyAllPropertiesIfNecessary(exec);
  97. return JSArray::deletePropertyByIndex(thisObject, exec, propertyName);
  98. }
  99. static void getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& arr, EnumerationMode mode = ExcludeDontEnumProperties)
  100. {
  101. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(object);
  102. thisObject->reifyAllPropertiesIfNecessary(exec);
  103. JSArray::getOwnPropertyNames(thisObject, exec, arr, mode);
  104. }
  105. static bool defineOwnProperty(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor, bool shouldThrow)
  106. {
  107. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(object);
  108. thisObject->reifyAllPropertiesIfNecessary(exec);
  109. return JSArray::defineOwnProperty(object, exec, propertyName, descriptor, shouldThrow);
  110. }
  111. void reifyAllProperties(ExecState*);
  112. void reifyMatchProperty(ExecState*);
  113. WriteBarrier<JSString> m_input;
  114. WriteBarrier<RegExp> m_regExp;
  115. MatchResult m_result;
  116. ReifiedState m_state;
  117. };
  118. }
  119. #endif // RegExpMatchesArray_h