]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/RegExpConstructor.h
JavaScriptCore-7600.1.4.15.12.tar.gz
[apple/javascriptcore.git] / runtime / RegExpConstructor.h
CommitLineData
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
31namespace JSC {
32
9dae56ea 33 class RegExpPrototype;
9dae56ea 34
6fe7ccc8 35 class RegExpConstructor : public InternalFunction {
14957cd0 36 public:
6fe7ccc8
A
37 typedef InternalFunction Base;
38
81345200 39 static RegExpConstructor* create(VM& vm, Structure* structure, RegExpPrototype* regExpPrototype)
f9bf01c6 40 {
81345200
A
41 RegExpConstructor* constructor = new (NotNull, allocateCell<RegExpConstructor>(vm.heap)) RegExpConstructor(vm, structure, regExpPrototype);
42 constructor->finishCreation(vm, regExpPrototype);
6fe7ccc8 43 return constructor;
f9bf01c6
A
44 }
45
93a37866 46 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
9dae56ea 47 {
81345200 48 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
9dae56ea
A
49 }
50
81345200 51 static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
6fe7ccc8 52
81345200 53 DECLARE_INFO;
9dae56ea 54
93a37866
A
55 MatchResult performMatch(VM&, RegExp*, JSString*, const String&, int startOffset, int** ovector);
56 MatchResult performMatch(VM&, RegExp*, JSString*, const String&, int startOffset);
6fe7ccc8
A
57
58 void setMultiline(bool multiline) { m_multiline = multiline; }
59 bool multiline() const { return m_multiline; }
9dae56ea 60
6fe7ccc8
A
61 JSValue getBackref(ExecState*, unsigned);
62 JSValue getLastParen(ExecState*);
63 JSValue getLeftContext(ExecState*);
64 JSValue getRightContext(ExecState*);
9dae56ea 65
6fe7ccc8
A
66 void setInput(ExecState* exec, JSString* string) { m_cachedResult.setInput(exec, this, string); }
67 JSString* input() { return m_cachedResult.input(); }
9dae56ea 68
6fe7ccc8 69 static void visitChildren(JSCell*, SlotVisitor&);
9dae56ea 70
f9bf01c6 71 protected:
81345200 72 void finishCreation(VM&, RegExpPrototype*);
6fe7ccc8 73 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | Base::StructureFlags;
f9bf01c6 74
9dae56ea 75 private:
81345200 76 RegExpConstructor(VM&, Structure*, RegExpPrototype*);
6fe7ccc8
A
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;
9dae56ea
A
84 };
85
ba379fdc 86 RegExpConstructor* asRegExpConstructor(JSValue);
9dae56ea 87
6fe7ccc8 88 JSObject* constructRegExp(ExecState*, JSGlobalObject*, const ArgList&, bool callAsConstructor = false);
9dae56ea 89
ba379fdc 90 inline RegExpConstructor* asRegExpConstructor(JSValue value)
9dae56ea 91 {
81345200 92 ASSERT(asObject(value)->inherits(RegExpConstructor::info()));
9dae56ea
A
93 return static_cast<RegExpConstructor*>(asObject(value));
94 }
95
f9bf01c6
A
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 */
93a37866 101 ALWAYS_INLINE MatchResult RegExpConstructor::performMatch(VM& vm, RegExp* regExp, JSString* string, const String& input, int startOffset, int** ovector)
f9bf01c6 102 {
93a37866 103 int position = regExp->match(vm, input, startOffset, m_ovector);
f9bf01c6
A
104
105 if (ovector)
6fe7ccc8 106 *ovector = m_ovector.data();
f9bf01c6 107
6fe7ccc8
A
108 if (position == -1)
109 return MatchResult::failed();
f9bf01c6 110
6fe7ccc8
A
111 ASSERT(!m_ovector.isEmpty());
112 ASSERT(m_ovector[0] == position);
113 ASSERT(m_ovector[1] >= position);
114 size_t end = m_ovector[1];
f9bf01c6 115
93a37866 116 m_cachedResult.record(vm, this, regExp, string, MatchResult(position, end));
6fe7ccc8
A
117
118 return MatchResult(position, end);
119 }
93a37866 120 ALWAYS_INLINE MatchResult RegExpConstructor::performMatch(VM& vm, RegExp* regExp, JSString* string, const String& input, int startOffset)
6fe7ccc8 121 {
93a37866 122 MatchResult result = regExp->match(vm, input, startOffset);
6fe7ccc8 123 if (result)
93a37866 124 m_cachedResult.record(vm, this, regExp, string, result);
6fe7ccc8 125 return result;
f9bf01c6
A
126 }
127
9dae56ea
A
128} // namespace JSC
129
130#endif // RegExpConstructor_h