RegExpObject.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2003, 2007, 2008, 2012 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 RegExpObject_h
  21. #define RegExpObject_h
  22. #include "JSObject.h"
  23. #include "RegExp.h"
  24. namespace JSC {
  25. class RegExpObject : public JSNonFinalObject {
  26. public:
  27. typedef JSNonFinalObject Base;
  28. static RegExpObject* create(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, RegExp* regExp)
  29. {
  30. RegExpObject* object = new (NotNull, allocateCell<RegExpObject>(*exec->heap())) RegExpObject(globalObject, structure, regExp);
  31. object->finishCreation(globalObject);
  32. return object;
  33. }
  34. static RegExpObject* create(VM& vm, JSGlobalObject* globalObject, Structure* structure, RegExp* regExp)
  35. {
  36. RegExpObject* object = new (NotNull, allocateCell<RegExpObject>(vm.heap)) RegExpObject(globalObject, structure, regExp);
  37. object->finishCreation(globalObject);
  38. return object;
  39. }
  40. void setRegExp(VM& vm, RegExp* r) { m_regExp.set(vm, this, r); }
  41. RegExp* regExp() const { return m_regExp.get(); }
  42. void setLastIndex(ExecState* exec, size_t lastIndex)
  43. {
  44. m_lastIndex.setWithoutWriteBarrier(jsNumber(lastIndex));
  45. if (LIKELY(m_lastIndexIsWritable))
  46. m_lastIndex.setWithoutWriteBarrier(jsNumber(lastIndex));
  47. else
  48. throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
  49. }
  50. void setLastIndex(ExecState* exec, JSValue lastIndex, bool shouldThrow)
  51. {
  52. if (LIKELY(m_lastIndexIsWritable))
  53. m_lastIndex.set(exec->vm(), this, lastIndex);
  54. else if (shouldThrow)
  55. throwTypeError(exec, StrictModeReadonlyPropertyWriteError);
  56. }
  57. JSValue getLastIndex() const
  58. {
  59. return m_lastIndex.get();
  60. }
  61. bool test(ExecState* exec, JSString* string) { return match(exec, string); }
  62. JSValue exec(ExecState*, JSString*);
  63. static bool getOwnPropertySlot(JSCell*, ExecState*, PropertyName, PropertySlot&);
  64. static bool getOwnPropertyDescriptor(JSObject*, ExecState*, PropertyName, PropertyDescriptor&);
  65. static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
  66. static JS_EXPORTDATA const ClassInfo s_info;
  67. static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
  68. {
  69. return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
  70. }
  71. protected:
  72. JS_EXPORT_PRIVATE RegExpObject(JSGlobalObject*, Structure*, RegExp*);
  73. JS_EXPORT_PRIVATE void finishCreation(JSGlobalObject*);
  74. static const unsigned StructureFlags = OverridesVisitChildren | OverridesGetOwnPropertySlot | Base::StructureFlags;
  75. static void visitChildren(JSCell*, SlotVisitor&);
  76. JS_EXPORT_PRIVATE static bool deleteProperty(JSCell*, ExecState*, PropertyName);
  77. JS_EXPORT_PRIVATE static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  78. JS_EXPORT_PRIVATE static void getPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
  79. JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, PropertyDescriptor&, bool shouldThrow);
  80. private:
  81. MatchResult match(ExecState*, JSString*);
  82. WriteBarrier<RegExp> m_regExp;
  83. WriteBarrier<Unknown> m_lastIndex;
  84. bool m_lastIndexIsWritable;
  85. };
  86. RegExpObject* asRegExpObject(JSValue);
  87. inline RegExpObject* asRegExpObject(JSValue value)
  88. {
  89. ASSERT(asObject(value)->inherits(&RegExpObject::s_info));
  90. return static_cast<RegExpObject*>(asObject(value));
  91. }
  92. } // namespace JSC
  93. #endif // RegExpObject_h