]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/RegExpMatchesArray.h
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / runtime / RegExpMatchesArray.h
1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 */
19
20 #ifndef RegExpMatchesArray_h
21 #define RegExpMatchesArray_h
22
23 #include "JSArray.h"
24 #include "JSGlobalObject.h"
25 #include "RegExpObject.h"
26
27 namespace JSC {
28
29 class RegExpMatchesArray : public JSArray {
30 private:
31 RegExpMatchesArray(JSGlobalData& globalData, JSGlobalObject* globalObject, JSString* input, RegExp* regExp, MatchResult result)
32 : JSArray(globalData, globalObject->regExpMatchesArrayStructure())
33 , m_result(result)
34 , m_state(ReifiedNone)
35 {
36 m_input.set(globalData, this, input);
37 m_regExp.set(globalData, this, regExp);
38 }
39
40 enum ReifiedState { ReifiedNone, ReifiedMatch, ReifiedAll };
41
42 public:
43 typedef JSArray Base;
44
45 static RegExpMatchesArray* create(ExecState* exec, JSString* input, RegExp* regExp, MatchResult result)
46 {
47 ASSERT(result);
48 JSGlobalData& globalData = exec->globalData();
49 RegExpMatchesArray* array = new (NotNull, allocateCell<RegExpMatchesArray>(globalData.heap)) RegExpMatchesArray(globalData, exec->lexicalGlobalObject(), input, regExp, result);
50 array->finishCreation(globalData);
51 return array;
52 }
53
54 JSString* leftContext(ExecState*);
55 JSString* rightContext(ExecState*);
56
57 static const ClassInfo s_info;
58
59 static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue prototype)
60 {
61 return Structure::create(globalData, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
62 }
63
64 static void visitChildren(JSCell*, SlotVisitor&);
65
66 protected:
67 void finishCreation(JSGlobalData&);
68
69 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesVisitChildren | OverridesGetPropertyNames | Base::StructureFlags;
70
71 private:
72 ALWAYS_INLINE void reifyAllPropertiesIfNecessary(ExecState* exec)
73 {
74 if (m_state != ReifiedAll)
75 reifyAllProperties(exec);
76 }
77
78 ALWAYS_INLINE void reifyMatchPropertyIfNecessary(ExecState* exec)
79 {
80 if (m_state == ReifiedNone)
81 reifyMatchProperty(exec);
82 }
83
84 static bool getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
85 {
86 RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
87 thisObject->reifyAllPropertiesIfNecessary(exec);
88 return JSArray::getOwnPropertySlot(thisObject, exec, propertyName, slot);
89 }
90
91 static bool getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)
92 {
93 RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
94 if (propertyName)
95 thisObject->reifyAllPropertiesIfNecessary(exec);
96 else
97 thisObject->reifyMatchPropertyIfNecessary(exec);
98 return JSArray::getOwnPropertySlotByIndex(thisObject, exec, propertyName, slot);
99 }
100
101 static bool getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
102 {
103 RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(object);
104 thisObject->reifyAllPropertiesIfNecessary(exec);
105 return JSArray::getOwnPropertyDescriptor(thisObject, exec, propertyName, descriptor);
106 }
107
108 static void put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue v, PutPropertySlot& slot)
109 {
110 RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
111 thisObject->reifyAllPropertiesIfNecessary(exec);
112 JSArray::put(thisObject, exec, propertyName, v, slot);
113 }
114
115 static void putByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, JSValue v, bool shouldThrow)
116 {
117 RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
118 thisObject->reifyAllPropertiesIfNecessary(exec);
119 JSArray::putByIndex(thisObject, exec, propertyName, v, shouldThrow);
120 }
121
122 static bool deleteProperty(JSCell* cell, ExecState* exec, const Identifier& propertyName)
123 {
124 RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
125 thisObject->reifyAllPropertiesIfNecessary(exec);
126 return JSArray::deleteProperty(thisObject, exec, propertyName);
127 }
128
129 static bool deletePropertyByIndex(JSCell* cell, ExecState* exec, unsigned propertyName)
130 {
131 RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(cell);
132 thisObject->reifyAllPropertiesIfNecessary(exec);
133 return JSArray::deletePropertyByIndex(thisObject, exec, propertyName);
134 }
135
136 static void getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& arr, EnumerationMode mode = ExcludeDontEnumProperties)
137 {
138 RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(object);
139 thisObject->reifyAllPropertiesIfNecessary(exec);
140 JSArray::getOwnPropertyNames(thisObject, exec, arr, mode);
141 }
142
143 static bool defineOwnProperty(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor, bool shouldThrow)
144 {
145 RegExpMatchesArray* thisObject = jsCast<RegExpMatchesArray*>(object);
146 thisObject->reifyAllPropertiesIfNecessary(exec);
147 return JSArray::defineOwnProperty(object, exec, propertyName, descriptor, shouldThrow);
148 }
149
150 void reifyAllProperties(ExecState*);
151 void reifyMatchProperty(ExecState*);
152
153 WriteBarrier<JSString> m_input;
154 WriteBarrier<RegExp> m_regExp;
155 MatchResult m_result;
156 ReifiedState m_state;
157 };
158
159 }
160
161 #endif // RegExpMatchesArray_h