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"
31 ASSERT_CLASS_FITS_IN_CELL(RegExpMatchesArray
);
33 const ClassInfo
RegExpMatchesArray::s_info
= {"Array", &JSArray::s_info
, 0, 0, CREATE_METHOD_TABLE(RegExpMatchesArray
)};
35 void RegExpMatchesArray::finishCreation(JSGlobalData
& globalData
)
37 Base::finishCreation(globalData
, m_regExp
->numSubpatterns() + 1);
40 void RegExpMatchesArray::visitChildren(JSCell
* cell
, SlotVisitor
& visitor
)
42 RegExpMatchesArray
* thisObject
= jsCast
<RegExpMatchesArray
*>(cell
);
43 ASSERT_GC_OBJECT_INHERITS(thisObject
, &s_info
);
44 COMPILE_ASSERT(StructureFlags
& OverridesVisitChildren
, OverridesVisitChildrenWithoutSettingFlag
);
45 ASSERT(thisObject
->structure()->typeInfo().overridesVisitChildren());
47 Base::visitChildren(thisObject
, visitor
);
48 visitor
.append(&thisObject
->m_input
);
49 visitor
.append(&thisObject
->m_regExp
);
52 void RegExpMatchesArray::reifyAllProperties(ExecState
* exec
)
54 ASSERT(m_state
!= ReifiedAll
);
57 reifyMatchPropertyIfNecessary(exec
);
59 if (unsigned numSubpatterns
= m_regExp
->numSubpatterns()) {
60 Vector
<int, 32> subpatternResults
;
61 int position
= m_regExp
->match(exec
->globalData(), m_input
->value(exec
), m_result
.start
, subpatternResults
);
62 ASSERT_UNUSED(position
, position
>= 0 && static_cast<size_t>(position
) == m_result
.start
);
63 ASSERT(m_result
.start
== static_cast<size_t>(subpatternResults
[0]));
64 ASSERT(m_result
.end
== static_cast<size_t>(subpatternResults
[1]));
66 for (unsigned i
= 1; i
<= numSubpatterns
; ++i
) {
67 int start
= subpatternResults
[2 * i
];
69 putDirectIndex(exec
, i
, jsSubstring(exec
, m_input
.get(), start
, subpatternResults
[2 * i
+ 1] - start
), false);
71 putDirectIndex(exec
, i
, jsUndefined(), false);
76 JSArray::put(this, exec
, exec
->propertyNames().index
, jsNumber(m_result
.start
), slot
);
77 JSArray::put(this, exec
, exec
->propertyNames().input
, m_input
.get(), slot
);
82 void RegExpMatchesArray::reifyMatchProperty(ExecState
* exec
)
84 ASSERT(m_state
== ReifiedNone
);
86 putDirectIndex(exec
, 0, jsSubstring(exec
, m_input
.get(), m_result
.start
, m_result
.end
- m_result
.start
), false);
87 m_state
= ReifiedMatch
;
90 JSString
* RegExpMatchesArray::leftContext(ExecState
* exec
)
93 return jsEmptyString(exec
);
94 return jsSubstring(exec
, m_input
.get(), 0, m_result
.start
);
97 JSString
* RegExpMatchesArray::rightContext(ExecState
* exec
)
99 unsigned length
= m_input
->length();
100 if (m_result
.end
== length
)
101 return jsEmptyString(exec
);
102 return jsSubstring(exec
, m_input
.get(), m_result
.end
, length
- m_result
.end
);