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