RegExpMatchesArray.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2012 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. #include "config.h"
  26. #include "RegExpMatchesArray.h"
  27. #include "ButterflyInlines.h"
  28. #include "Operations.h"
  29. namespace JSC {
  30. const ClassInfo RegExpMatchesArray::s_info = {"Array", &JSArray::s_info, 0, 0, CREATE_METHOD_TABLE(RegExpMatchesArray)};
  31. RegExpMatchesArray::RegExpMatchesArray(VM& vm, Butterfly* butterfly, JSGlobalObject* globalObject, JSString* input, RegExp* regExp, MatchResult result)
  32. : JSArray(vm, globalObject->regExpMatchesArrayStructure(), butterfly)
  33. , m_result(result)
  34. , m_state(ReifiedNone)
  35. {
  36. m_input.set(vm, this, input);
  37. m_regExp.set(vm, this, regExp);
  38. }
  39. RegExpMatchesArray* RegExpMatchesArray::create(ExecState* exec, JSString* input, RegExp* regExp, MatchResult result)
  40. {
  41. ASSERT(result);
  42. VM& vm = exec->vm();
  43. Butterfly* butterfly = createArrayButterfly(vm, regExp->numSubpatterns() + 1);
  44. RegExpMatchesArray* array = new (NotNull, allocateCell<RegExpMatchesArray>(vm.heap)) RegExpMatchesArray(vm, butterfly, exec->lexicalGlobalObject(), input, regExp, result);
  45. array->finishCreation(vm);
  46. return array;
  47. }
  48. void RegExpMatchesArray::finishCreation(VM& vm)
  49. {
  50. Base::finishCreation(vm);
  51. }
  52. void RegExpMatchesArray::visitChildren(JSCell* cell, SlotVisitor& visitor)
  53. {
  54. RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
  55. ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
  56. COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
  57. ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
  58. Base::visitChildren(thisObject, visitor);
  59. visitor.append(&thisObject->m_input);
  60. visitor.append(&thisObject->m_regExp);
  61. }
  62. void RegExpMatchesArray::reifyAllProperties(ExecState* exec)
  63. {
  64. ASSERT(m_state != ReifiedAll);
  65. ASSERT(m_result);
  66. reifyMatchPropertyIfNecessary(exec);
  67. if (unsigned numSubpatterns = m_regExp->numSubpatterns()) {
  68. Vector<int, 32> subpatternResults;
  69. int position = m_regExp->match(exec->vm(), m_input->value(exec), m_result.start, subpatternResults);
  70. ASSERT_UNUSED(position, position >= 0 && static_cast<size_t>(position) == m_result.start);
  71. ASSERT(m_result.start == static_cast<size_t>(subpatternResults[0]));
  72. ASSERT(m_result.end == static_cast<size_t>(subpatternResults[1]));
  73. for (unsigned i = 1; i <= numSubpatterns; ++i) {
  74. int start = subpatternResults[2 * i];
  75. if (start >= 0)
  76. putDirectIndex(exec, i, jsSubstring(exec, m_input.get(), start, subpatternResults[2 * i + 1] - start));
  77. else
  78. putDirectIndex(exec, i, jsUndefined());
  79. }
  80. }
  81. PutPropertySlot slot;
  82. JSArray::put(this, exec, exec->propertyNames().index, jsNumber(m_result.start), slot);
  83. JSArray::put(this, exec, exec->propertyNames().input, m_input.get(), slot);
  84. m_state = ReifiedAll;
  85. }
  86. void RegExpMatchesArray::reifyMatchProperty(ExecState* exec)
  87. {
  88. ASSERT(m_state == ReifiedNone);
  89. ASSERT(m_result);
  90. putDirectIndex(exec, 0, jsSubstring(exec, m_input.get(), m_result.start, m_result.end - m_result.start));
  91. m_state = ReifiedMatch;
  92. }
  93. JSString* RegExpMatchesArray::leftContext(ExecState* exec)
  94. {
  95. if (!m_result.start)
  96. return jsEmptyString(exec);
  97. return jsSubstring(exec, m_input.get(), 0, m_result.start);
  98. }
  99. JSString* RegExpMatchesArray::rightContext(ExecState* exec)
  100. {
  101. unsigned length = m_input->length();
  102. if (m_result.end == length)
  103. return jsEmptyString(exec);
  104. return jsSubstring(exec, m_input.get(), m_result.end, length - m_result.end);
  105. }
  106. } // namespace JSC