]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/RegExpMatchesArray.cpp
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / runtime / RegExpMatchesArray.cpp
CommitLineData
6fe7ccc8
A
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
26#include "config.h"
27#include "RegExpMatchesArray.h"
28
29namespace JSC {
30
31ASSERT_CLASS_FITS_IN_CELL(RegExpMatchesArray);
32
33const ClassInfo RegExpMatchesArray::s_info = {"Array", &JSArray::s_info, 0, 0, CREATE_METHOD_TABLE(RegExpMatchesArray)};
34
35void RegExpMatchesArray::finishCreation(JSGlobalData& globalData)
36{
37 Base::finishCreation(globalData, m_regExp->numSubpatterns() + 1);
38}
39
40void RegExpMatchesArray::visitChildren(JSCell* cell, SlotVisitor& visitor)
41{
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());
46
47 Base::visitChildren(thisObject, visitor);
48 visitor.append(&thisObject->m_input);
49 visitor.append(&thisObject->m_regExp);
50}
51
52void RegExpMatchesArray::reifyAllProperties(ExecState* exec)
53{
54 ASSERT(m_state != ReifiedAll);
55 ASSERT(m_result);
56
57 reifyMatchPropertyIfNecessary(exec);
58
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]));
65
66 for (unsigned i = 1; i <= numSubpatterns; ++i) {
67 int start = subpatternResults[2 * i];
68 if (start >= 0)
69 putDirectIndex(exec, i, jsSubstring(exec, m_input.get(), start, subpatternResults[2 * i + 1] - start), false);
70 else
71 putDirectIndex(exec, i, jsUndefined(), false);
72 }
73 }
74
75 PutPropertySlot slot;
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);
78
79 m_state = ReifiedAll;
80}
81
82void RegExpMatchesArray::reifyMatchProperty(ExecState* exec)
83{
84 ASSERT(m_state == ReifiedNone);
85 ASSERT(m_result);
86 putDirectIndex(exec, 0, jsSubstring(exec, m_input.get(), m_result.start, m_result.end - m_result.start), false);
87 m_state = ReifiedMatch;
88}
89
90JSString* RegExpMatchesArray::leftContext(ExecState* exec)
91{
92 if (!m_result.start)
93 return jsEmptyString(exec);
94 return jsSubstring(exec, m_input.get(), 0, m_result.start);
95}
96
97JSString* RegExpMatchesArray::rightContext(ExecState* exec)
98{
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);
103}
104
105} // namespace JSC