]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
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" | |
f9bf01c6 | 25 | #include "RegExp.h" |
6fe7ccc8 A |
26 | #include "RegExpCachedResult.h" |
27 | #include "RegExpObject.h" | |
6fe7ccc8 | 28 | |
9dae56ea A |
29 | namespace JSC { |
30 | ||
ed1e77d3 | 31 | class RegExpPrototype; |
9dae56ea | 32 | |
ed1e77d3 A |
33 | class RegExpConstructor : public InternalFunction { |
34 | public: | |
35 | typedef InternalFunction Base; | |
36 | static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot; | |
6fe7ccc8 | 37 | |
ed1e77d3 A |
38 | static RegExpConstructor* create(VM& vm, Structure* structure, RegExpPrototype* regExpPrototype) |
39 | { | |
40 | RegExpConstructor* constructor = new (NotNull, allocateCell<RegExpConstructor>(vm.heap)) RegExpConstructor(vm, structure, regExpPrototype); | |
41 | constructor->finishCreation(vm, regExpPrototype); | |
42 | return constructor; | |
43 | } | |
f9bf01c6 | 44 | |
ed1e77d3 A |
45 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
46 | { | |
47 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); | |
48 | } | |
9dae56ea | 49 | |
ed1e77d3 | 50 | static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&); |
6fe7ccc8 | 51 | |
ed1e77d3 | 52 | DECLARE_INFO; |
9dae56ea | 53 | |
ed1e77d3 A |
54 | MatchResult performMatch(VM&, RegExp*, JSString*, const String&, int startOffset, int** ovector); |
55 | MatchResult performMatch(VM&, RegExp*, JSString*, const String&, int startOffset); | |
6fe7ccc8 | 56 | |
ed1e77d3 A |
57 | void setMultiline(bool multiline) { m_multiline = multiline; } |
58 | bool multiline() const { return m_multiline; } | |
9dae56ea | 59 | |
ed1e77d3 A |
60 | JSValue getBackref(ExecState*, unsigned); |
61 | JSValue getLastParen(ExecState*); | |
62 | JSValue getLeftContext(ExecState*); | |
63 | JSValue getRightContext(ExecState*); | |
9dae56ea | 64 | |
ed1e77d3 A |
65 | void setInput(ExecState* exec, JSString* string) { m_cachedResult.setInput(exec, this, string); } |
66 | JSString* input() { return m_cachedResult.input(); } | |
9dae56ea | 67 | |
ed1e77d3 | 68 | static void visitChildren(JSCell*, SlotVisitor&); |
9dae56ea | 69 | |
ed1e77d3 A |
70 | protected: |
71 | void finishCreation(VM&, RegExpPrototype*); | |
f9bf01c6 | 72 | |
ed1e77d3 A |
73 | private: |
74 | RegExpConstructor(VM&, Structure*, RegExpPrototype*); | |
75 | static void destroy(JSCell*); | |
76 | static ConstructType getConstructData(JSCell*, ConstructData&); | |
77 | static CallType getCallData(JSCell*, CallData&); | |
6fe7ccc8 | 78 | |
ed1e77d3 A |
79 | RegExpCachedResult m_cachedResult; |
80 | bool m_multiline; | |
81 | Vector<int, 32> m_ovector; | |
82 | }; | |
9dae56ea | 83 | |
ed1e77d3 | 84 | RegExpConstructor* asRegExpConstructor(JSValue); |
9dae56ea | 85 | |
ed1e77d3 | 86 | JSObject* constructRegExp(ExecState*, JSGlobalObject*, const ArgList&, bool callAsConstructor = false); |
9dae56ea | 87 | |
ed1e77d3 A |
88 | inline RegExpConstructor* asRegExpConstructor(JSValue value) |
89 | { | |
90 | ASSERT(asObject(value)->inherits(RegExpConstructor::info())); | |
91 | return static_cast<RegExpConstructor*>(asObject(value)); | |
92 | } | |
9dae56ea | 93 | |
ed1e77d3 A |
94 | /* |
95 | To facilitate result caching, exec(), test(), match(), search(), and replace() dipatch regular | |
96 | expression matching through the performMatch function. We use cached results to calculate, | |
97 | e.g., RegExp.lastMatch and RegExp.leftParen. | |
98 | */ | |
99 | ALWAYS_INLINE MatchResult RegExpConstructor::performMatch(VM& vm, RegExp* regExp, JSString* string, const String& input, int startOffset, int** ovector) | |
100 | { | |
101 | int position = regExp->match(vm, input, startOffset, m_ovector); | |
f9bf01c6 | 102 | |
ed1e77d3 A |
103 | if (ovector) |
104 | *ovector = m_ovector.data(); | |
f9bf01c6 | 105 | |
ed1e77d3 A |
106 | if (position == -1) |
107 | return MatchResult::failed(); | |
f9bf01c6 | 108 | |
ed1e77d3 A |
109 | ASSERT(!m_ovector.isEmpty()); |
110 | ASSERT(m_ovector[0] == position); | |
111 | ASSERT(m_ovector[1] >= position); | |
112 | size_t end = m_ovector[1]; | |
f9bf01c6 | 113 | |
ed1e77d3 | 114 | m_cachedResult.record(vm, this, regExp, string, MatchResult(position, end)); |
6fe7ccc8 | 115 | |
ed1e77d3 A |
116 | return MatchResult(position, end); |
117 | } | |
118 | ALWAYS_INLINE MatchResult RegExpConstructor::performMatch(VM& vm, RegExp* regExp, JSString* string, const String& input, int startOffset) | |
119 | { | |
120 | MatchResult result = regExp->match(vm, input, startOffset); | |
121 | if (result) | |
122 | m_cachedResult.record(vm, this, regExp, string, result); | |
123 | return result; | |
124 | } | |
f9bf01c6 | 125 | |
9dae56ea A |
126 | } // namespace JSC |
127 | ||
128 | #endif // RegExpConstructor_h |