]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/RegExpConstructor.h
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / runtime / RegExpConstructor.h
1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #ifndef RegExpConstructor_h
22 #define RegExpConstructor_h
23
24 #include "InternalFunction.h"
25 #include "RegExp.h"
26 #include "RegExpCachedResult.h"
27 #include "RegExpObject.h"
28 #include <wtf/OwnPtr.h>
29
30
31 namespace JSC {
32
33 class RegExpPrototype;
34
35 class RegExpConstructor : public InternalFunction {
36 public:
37 typedef InternalFunction Base;
38
39 static RegExpConstructor* create(VM& vm, Structure* structure, RegExpPrototype* regExpPrototype)
40 {
41 RegExpConstructor* constructor = new (NotNull, allocateCell<RegExpConstructor>(vm.heap)) RegExpConstructor(vm, structure, regExpPrototype);
42 constructor->finishCreation(vm, regExpPrototype);
43 return constructor;
44 }
45
46 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
47 {
48 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
49 }
50
51 static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
52
53 DECLARE_INFO;
54
55 MatchResult performMatch(VM&, RegExp*, JSString*, const String&, int startOffset, int** ovector);
56 MatchResult performMatch(VM&, RegExp*, JSString*, const String&, int startOffset);
57
58 void setMultiline(bool multiline) { m_multiline = multiline; }
59 bool multiline() const { return m_multiline; }
60
61 JSValue getBackref(ExecState*, unsigned);
62 JSValue getLastParen(ExecState*);
63 JSValue getLeftContext(ExecState*);
64 JSValue getRightContext(ExecState*);
65
66 void setInput(ExecState* exec, JSString* string) { m_cachedResult.setInput(exec, this, string); }
67 JSString* input() { return m_cachedResult.input(); }
68
69 static void visitChildren(JSCell*, SlotVisitor&);
70
71 protected:
72 void finishCreation(VM&, RegExpPrototype*);
73 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | Base::StructureFlags;
74
75 private:
76 RegExpConstructor(VM&, Structure*, RegExpPrototype*);
77 static void destroy(JSCell*);
78 static ConstructType getConstructData(JSCell*, ConstructData&);
79 static CallType getCallData(JSCell*, CallData&);
80
81 RegExpCachedResult m_cachedResult;
82 bool m_multiline;
83 Vector<int, 32> m_ovector;
84 };
85
86 RegExpConstructor* asRegExpConstructor(JSValue);
87
88 JSObject* constructRegExp(ExecState*, JSGlobalObject*, const ArgList&, bool callAsConstructor = false);
89
90 inline RegExpConstructor* asRegExpConstructor(JSValue value)
91 {
92 ASSERT(asObject(value)->inherits(RegExpConstructor::info()));
93 return static_cast<RegExpConstructor*>(asObject(value));
94 }
95
96 /*
97 To facilitate result caching, exec(), test(), match(), search(), and replace() dipatch regular
98 expression matching through the performMatch function. We use cached results to calculate,
99 e.g., RegExp.lastMatch and RegExp.leftParen.
100 */
101 ALWAYS_INLINE MatchResult RegExpConstructor::performMatch(VM& vm, RegExp* regExp, JSString* string, const String& input, int startOffset, int** ovector)
102 {
103 int position = regExp->match(vm, input, startOffset, m_ovector);
104
105 if (ovector)
106 *ovector = m_ovector.data();
107
108 if (position == -1)
109 return MatchResult::failed();
110
111 ASSERT(!m_ovector.isEmpty());
112 ASSERT(m_ovector[0] == position);
113 ASSERT(m_ovector[1] >= position);
114 size_t end = m_ovector[1];
115
116 m_cachedResult.record(vm, this, regExp, string, MatchResult(position, end));
117
118 return MatchResult(position, end);
119 }
120 ALWAYS_INLINE MatchResult RegExpConstructor::performMatch(VM& vm, RegExp* regExp, JSString* string, const String& input, int startOffset)
121 {
122 MatchResult result = regExp->match(vm, input, startOffset);
123 if (result)
124 m_cachedResult.record(vm, this, regExp, string, result);
125 return result;
126 }
127
128 } // namespace JSC
129
130 #endif // RegExpConstructor_h