RegExpConstructor.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. #ifndef RegExpConstructor_h
  21. #define RegExpConstructor_h
  22. #include "InternalFunction.h"
  23. #include "RegExp.h"
  24. #include "RegExpCachedResult.h"
  25. #include "RegExpObject.h"
  26. #include <wtf/OwnPtr.h>
  27. namespace JSC {
  28. class RegExpPrototype;
  29. class RegExpConstructor : public InternalFunction {
  30. public:
  31. typedef InternalFunction Base;
  32. static RegExpConstructor* create(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, RegExpPrototype* regExpPrototype)
  33. {
  34. RegExpConstructor* constructor = new (NotNull, allocateCell<RegExpConstructor>(*exec->heap())) RegExpConstructor(globalObject, structure, regExpPrototype);
  35. constructor->finishCreation(exec, regExpPrototype);
  36. return constructor;
  37. }
  38. static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
  39. {
  40. return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
  41. }
  42. static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
  43. static bool getOwnPropertySlot(JSCell*, ExecState*, PropertyName, PropertySlot&);
  44. static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
  45. static const ClassInfo s_info;
  46. MatchResult performMatch(VM&, RegExp*, JSString*, const String&, int startOffset, int** ovector);
  47. MatchResult performMatch(VM&, RegExp*, JSString*, const String&, int startOffset);
  48. void setMultiline(bool multiline) { m_multiline = multiline; }
  49. bool multiline() const { return m_multiline; }
  50. JSValue getBackref(ExecState*, unsigned);
  51. JSValue getLastParen(ExecState*);
  52. JSValue getLeftContext(ExecState*);
  53. JSValue getRightContext(ExecState*);
  54. void setInput(ExecState* exec, JSString* string) { m_cachedResult.setInput(exec, this, string); }
  55. JSString* input() { return m_cachedResult.input(); }
  56. static void visitChildren(JSCell*, SlotVisitor&);
  57. protected:
  58. void finishCreation(ExecState*, RegExpPrototype*);
  59. static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | Base::StructureFlags;
  60. private:
  61. RegExpConstructor(JSGlobalObject*, Structure*, RegExpPrototype*);
  62. static void destroy(JSCell*);
  63. static ConstructType getConstructData(JSCell*, ConstructData&);
  64. static CallType getCallData(JSCell*, CallData&);
  65. RegExpCachedResult m_cachedResult;
  66. bool m_multiline;
  67. Vector<int, 32> m_ovector;
  68. };
  69. RegExpConstructor* asRegExpConstructor(JSValue);
  70. JSObject* constructRegExp(ExecState*, JSGlobalObject*, const ArgList&, bool callAsConstructor = false);
  71. inline RegExpConstructor* asRegExpConstructor(JSValue value)
  72. {
  73. ASSERT(asObject(value)->inherits(&RegExpConstructor::s_info));
  74. return static_cast<RegExpConstructor*>(asObject(value));
  75. }
  76. /*
  77. To facilitate result caching, exec(), test(), match(), search(), and replace() dipatch regular
  78. expression matching through the performMatch function. We use cached results to calculate,
  79. e.g., RegExp.lastMatch and RegExp.leftParen.
  80. */
  81. ALWAYS_INLINE MatchResult RegExpConstructor::performMatch(VM& vm, RegExp* regExp, JSString* string, const String& input, int startOffset, int** ovector)
  82. {
  83. int position = regExp->match(vm, input, startOffset, m_ovector);
  84. if (ovector)
  85. *ovector = m_ovector.data();
  86. if (position == -1)
  87. return MatchResult::failed();
  88. ASSERT(!m_ovector.isEmpty());
  89. ASSERT(m_ovector[0] == position);
  90. ASSERT(m_ovector[1] >= position);
  91. size_t end = m_ovector[1];
  92. m_cachedResult.record(vm, this, regExp, string, MatchResult(position, end));
  93. return MatchResult(position, end);
  94. }
  95. ALWAYS_INLINE MatchResult RegExpConstructor::performMatch(VM& vm, RegExp* regExp, JSString* string, const String& input, int startOffset)
  96. {
  97. MatchResult result = regExp->match(vm, input, startOffset);
  98. if (result)
  99. m_cachedResult.record(vm, this, regExp, string, result);
  100. return result;
  101. }
  102. } // namespace JSC
  103. #endif // RegExpConstructor_h