]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/RegExpConstructor.h
JavaScriptCore-621.1.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"
9dae56ea
A
26#include <wtf/OwnPtr.h>
27
28namespace JSC {
29
30 class RegExp;
31 class RegExpPrototype;
32 struct RegExpConstructorPrivate;
33
f9bf01c6
A
34 struct RegExpConstructorPrivate : FastAllocBase {
35 // Global search cache / settings
36 RegExpConstructorPrivate()
37 : lastNumSubPatterns(0)
38 , multiline(false)
39 , lastOvectorIndex(0)
40 {
41 }
42
43 const Vector<int, 32>& lastOvector() const { return ovector[lastOvectorIndex]; }
44 Vector<int, 32>& lastOvector() { return ovector[lastOvectorIndex]; }
45 Vector<int, 32>& tempOvector() { return ovector[lastOvectorIndex ? 0 : 1]; }
46 void changeLastOvector() { lastOvectorIndex = lastOvectorIndex ? 0 : 1; }
47
48 UString input;
49 UString lastInput;
50 Vector<int, 32> ovector[2];
51 unsigned lastNumSubPatterns : 30;
52 bool multiline : 1;
53 unsigned lastOvectorIndex : 1;
54 };
55
9dae56ea
A
56 class RegExpConstructor : public InternalFunction {
57 public:
f9bf01c6 58 RegExpConstructor(ExecState*, NonNullPassRefPtr<Structure>, RegExpPrototype*);
9dae56ea 59
ba379fdc 60 static PassRefPtr<Structure> createStructure(JSValue prototype)
9dae56ea 61 {
f9bf01c6 62 return Structure::create(prototype, TypeInfo(ObjectType, StructureFlags), AnonymousSlotCount);
9dae56ea
A
63 }
64
ba379fdc 65 virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
9dae56ea 66 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
f9bf01c6 67 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
9dae56ea
A
68
69 static const ClassInfo info;
70
71 void performMatch(RegExp*, const UString&, int startOffset, int& position, int& length, int** ovector = 0);
72 JSObject* arrayOfMatches(ExecState*) const;
73
74 void setInput(const UString&);
75 const UString& input() const;
76
77 void setMultiline(bool);
78 bool multiline() const;
79
ba379fdc
A
80 JSValue getBackref(ExecState*, unsigned) const;
81 JSValue getLastParen(ExecState*) const;
82 JSValue getLeftContext(ExecState*) const;
83 JSValue getRightContext(ExecState*) const;
9dae56ea 84
f9bf01c6
A
85 protected:
86 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | ImplementsHasInstance | InternalFunction::StructureFlags;
87
9dae56ea
A
88 private:
89 virtual ConstructType getConstructData(ConstructData&);
90 virtual CallType getCallData(CallData&);
91
92 virtual const ClassInfo* classInfo() const { return &info; }
93
94 OwnPtr<RegExpConstructorPrivate> d;
95 };
96
ba379fdc 97 RegExpConstructor* asRegExpConstructor(JSValue);
9dae56ea
A
98
99 JSObject* constructRegExp(ExecState*, const ArgList&);
100
ba379fdc 101 inline RegExpConstructor* asRegExpConstructor(JSValue value)
9dae56ea
A
102 {
103 ASSERT(asObject(value)->inherits(&RegExpConstructor::info));
104 return static_cast<RegExpConstructor*>(asObject(value));
105 }
106
f9bf01c6
A
107 /*
108 To facilitate result caching, exec(), test(), match(), search(), and replace() dipatch regular
109 expression matching through the performMatch function. We use cached results to calculate,
110 e.g., RegExp.lastMatch and RegExp.leftParen.
111 */
112 inline void RegExpConstructor::performMatch(RegExp* r, const UString& s, int startOffset, int& position, int& length, int** ovector)
113 {
114 position = r->match(s, startOffset, &d->tempOvector());
115
116 if (ovector)
117 *ovector = d->tempOvector().data();
118
119 if (position != -1) {
120 ASSERT(!d->tempOvector().isEmpty());
121
122 length = d->tempOvector()[1] - d->tempOvector()[0];
123
124 d->input = s;
125 d->lastInput = s;
126 d->changeLastOvector();
127 d->lastNumSubPatterns = r->numSubpatterns();
128 }
129 }
130
9dae56ea
A
131} // namespace JSC
132
133#endif // RegExpConstructor_h