2  * Copyright (C) 2012 Apple Inc. All rights reserved. 
   4  * Redistribution and use in source and binary forms, with or without 
   5  * modification, are permitted provided that the following conditions 
   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. 
  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.  
  27 #include "RegExpMatchesArray.h" 
  29 #include "ButterflyInlines.h" 
  30 #include "Operations.h" 
  34 const ClassInfo 
RegExpMatchesArray::s_info 
= {"Array", &JSArray::s_info
, 0, 0, CREATE_METHOD_TABLE(RegExpMatchesArray
)}; 
  36 RegExpMatchesArray::RegExpMatchesArray(VM
& vm
, Butterfly
* butterfly
, JSGlobalObject
* globalObject
, JSString
* input
, RegExp
* regExp
, MatchResult result
) 
  37     : JSArray(vm
, globalObject
->regExpMatchesArrayStructure(), butterfly
) 
  39     , m_state(ReifiedNone
) 
  41     m_input
.set(vm
, this, input
); 
  42     m_regExp
.set(vm
, this, regExp
); 
  45 RegExpMatchesArray
* RegExpMatchesArray::create(ExecState
* exec
, JSString
* input
, RegExp
* regExp
, MatchResult result
) 
  49     Butterfly
* butterfly 
= createArrayButterfly(vm
, regExp
->numSubpatterns() + 1); 
  50     RegExpMatchesArray
* array 
= new (NotNull
, allocateCell
<RegExpMatchesArray
>(vm
.heap
)) RegExpMatchesArray(vm
, butterfly
, exec
->lexicalGlobalObject(), input
, regExp
, result
); 
  51     array
->finishCreation(vm
); 
  55 void RegExpMatchesArray::finishCreation(VM
& vm
) 
  57     Base::finishCreation(vm
); 
  60 void RegExpMatchesArray::visitChildren(JSCell
* cell
, SlotVisitor
& visitor
) 
  62     RegExpMatchesArray
* thisObject 
= jsCast
<RegExpMatchesArray
*>(cell
); 
  63     ASSERT_GC_OBJECT_INHERITS(thisObject
, &s_info
); 
  64     COMPILE_ASSERT(StructureFlags 
& OverridesVisitChildren
, OverridesVisitChildrenWithoutSettingFlag
); 
  65     ASSERT(thisObject
->structure()->typeInfo().overridesVisitChildren()); 
  67     Base::visitChildren(thisObject
, visitor
); 
  68     visitor
.append(&thisObject
->m_input
); 
  69     visitor
.append(&thisObject
->m_regExp
); 
  72 void RegExpMatchesArray::reifyAllProperties(ExecState
* exec
) 
  74     ASSERT(m_state 
!= ReifiedAll
); 
  77     reifyMatchPropertyIfNecessary(exec
); 
  79     if (unsigned numSubpatterns 
= m_regExp
->numSubpatterns()) { 
  80         Vector
<int, 32> subpatternResults
; 
  81         int position 
= m_regExp
->match(exec
->vm(), m_input
->value(exec
), m_result
.start
, subpatternResults
); 
  82         ASSERT_UNUSED(position
, position 
>= 0 && static_cast<size_t>(position
) == m_result
.start
); 
  83         ASSERT(m_result
.start 
== static_cast<size_t>(subpatternResults
[0])); 
  84         ASSERT(m_result
.end 
== static_cast<size_t>(subpatternResults
[1])); 
  86         for (unsigned i 
= 1; i 
<= numSubpatterns
; ++i
) { 
  87             int start 
= subpatternResults
[2 * i
]; 
  89                 putDirectIndex(exec
, i
, jsSubstring(exec
, m_input
.get(), start
, subpatternResults
[2 * i 
+ 1] - start
)); 
  91                 putDirectIndex(exec
, i
, jsUndefined()); 
  96     JSArray::put(this, exec
, exec
->propertyNames().index
, jsNumber(m_result
.start
), slot
); 
  97     JSArray::put(this, exec
, exec
->propertyNames().input
, m_input
.get(), slot
); 
 102 void RegExpMatchesArray::reifyMatchProperty(ExecState
* exec
) 
 104     ASSERT(m_state 
== ReifiedNone
); 
 106     putDirectIndex(exec
, 0, jsSubstring(exec
, m_input
.get(), m_result
.start
, m_result
.end 
- m_result
.start
)); 
 107     m_state 
= ReifiedMatch
; 
 110 JSString
* RegExpMatchesArray::leftContext(ExecState
* exec
) 
 113         return jsEmptyString(exec
); 
 114     return jsSubstring(exec
, m_input
.get(), 0, m_result
.start
); 
 117 JSString
* RegExpMatchesArray::rightContext(ExecState
* exec
) 
 119     unsigned length 
= m_input
->length(); 
 120     if (m_result
.end 
== length
) 
 121         return jsEmptyString(exec
); 
 122     return jsSubstring(exec
, m_input
.get(), m_result
.end
, length 
- m_result
.end
);