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 <wtf/OwnPtr.h>
27
28namespace JSC {
29
30 class RegExp;
31 class RegExpPrototype;
32 struct RegExpConstructorPrivate;
33
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
56 class RegExpConstructor : public InternalFunction {
57 public:
58 RegExpConstructor(ExecState*, NonNullPassRefPtr<Structure>, RegExpPrototype*);
59
60 static PassRefPtr<Structure> createStructure(JSValue prototype)
61 {
62 return Structure::create(prototype, typeInfo: TypeInfo(ObjectType, StructureFlags));
63 }
64
65 virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
66 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
67 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
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
80 JSValue getBackref(ExecState*, unsigned) const;
81 JSValue getLastParen(ExecState*) const;
82 JSValue getLeftContext(ExecState*) const;
83 JSValue getRightContext(ExecState*) const;
84
85 protected:
86 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | ImplementsHasInstance | InternalFunction::StructureFlags;
87
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
97 RegExpConstructor* asRegExpConstructor(JSValue);
98
99 JSObject* constructRegExp(ExecState*, const ArgList&);
100
101 inline RegExpConstructor* asRegExpConstructor(JSValue value)
102 {
103 ASSERT(asObject(value)->inherits(&RegExpConstructor::info));
104 return static_cast<RegExpConstructor*>(asObject(value));
105 }
106
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, ovector: &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
131} // namespace JSC
132
133#endif // RegExpConstructor_h
134

source code of qtscript/src/3rdparty/javascriptcore/JavaScriptCore/runtime/RegExpConstructor.h