]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/RegExpMatchesArray.cpp
JavaScriptCore-1218.tar.gz
[apple/javascriptcore.git] / runtime / RegExpMatchesArray.cpp
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
29 #include "ButterflyInlines.h"
30 #include "Operations.h"
31
32 namespace JSC {
33
34 const ClassInfo RegExpMatchesArray::s_info = {"Array", &JSArray::s_info, 0, 0, CREATE_METHOD_TABLE(RegExpMatchesArray)};
35
36 RegExpMatchesArray::RegExpMatchesArray(VM& vm, Butterfly* butterfly, JSGlobalObject* globalObject, JSString* input, RegExp* regExp, MatchResult result)
37 : JSArray(vm, globalObject->regExpMatchesArrayStructure(), butterfly)
38 , m_result(result)
39 , m_state(ReifiedNone)
40 {
41 m_input.set(vm, this, input);
42 m_regExp.set(vm, this, regExp);
43 }
44
45 RegExpMatchesArray* RegExpMatchesArray::create(ExecState* exec, JSString* input, RegExp* regExp, MatchResult result)
46 {
47 ASSERT(result);
48 VM& vm = exec->vm();
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);
52 return array;
53 }
54
55 void RegExpMatchesArray::finishCreation(VM& vm)
56 {
57 Base::finishCreation(vm);
58 }
59
60 void RegExpMatchesArray::visitChildren(JSCell* cell, SlotVisitor& visitor)
61 {
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());
66
67 Base::visitChildren(thisObject, visitor);
68 visitor.append(&thisObject->m_input);
69 visitor.append(&thisObject->m_regExp);
70 }
71
72 void RegExpMatchesArray::reifyAllProperties(ExecState* exec)
73 {
74 ASSERT(m_state != ReifiedAll);
75 ASSERT(m_result);
76
77 reifyMatchPropertyIfNecessary(exec);
78
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]));
85
86 for (unsigned i = 1; i <= numSubpatterns; ++i) {
87 int start = subpatternResults[2 * i];
88 if (start >= 0)
89 putDirectIndex(exec, i, jsSubstring(exec, m_input.get(), start, subpatternResults[2 * i + 1] - start));
90 else
91 putDirectIndex(exec, i, jsUndefined());
92 }
93 }
94
95 PutPropertySlot slot;
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);
98
99 m_state = ReifiedAll;
100 }
101
102 void RegExpMatchesArray::reifyMatchProperty(ExecState* exec)
103 {
104 ASSERT(m_state == ReifiedNone);
105 ASSERT(m_result);
106 putDirectIndex(exec, 0, jsSubstring(exec, m_input.get(), m_result.start, m_result.end - m_result.start));
107 m_state = ReifiedMatch;
108 }
109
110 JSString* RegExpMatchesArray::leftContext(ExecState* exec)
111 {
112 if (!m_result.start)
113 return jsEmptyString(exec);
114 return jsSubstring(exec, m_input.get(), 0, m_result.start);
115 }
116
117 JSString* RegExpMatchesArray::rightContext(ExecState* exec)
118 {
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);
123 }
124
125 } // namespace JSC