]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/RegExpConstructor.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / RegExpConstructor.cpp
1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved.
4 * Copyright (C) 2009 Torch Mobile, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "config.h"
23 #include "RegExpConstructor.h"
24
25 #include "Error.h"
26 #include "JSCInlines.h"
27 #include "RegExpMatchesArray.h"
28 #include "RegExpPrototype.h"
29
30 namespace JSC {
31
32 static EncodedJSValue regExpConstructorInput(ExecState*, JSObject*, EncodedJSValue, PropertyName);
33 static EncodedJSValue regExpConstructorMultiline(ExecState*, JSObject*, EncodedJSValue, PropertyName);
34 static EncodedJSValue regExpConstructorLastMatch(ExecState*, JSObject*, EncodedJSValue, PropertyName);
35 static EncodedJSValue regExpConstructorLastParen(ExecState*, JSObject*, EncodedJSValue, PropertyName);
36 static EncodedJSValue regExpConstructorLeftContext(ExecState*, JSObject*, EncodedJSValue, PropertyName);
37 static EncodedJSValue regExpConstructorRightContext(ExecState*, JSObject*, EncodedJSValue, PropertyName);
38 static EncodedJSValue regExpConstructorDollar1(ExecState*, JSObject*, EncodedJSValue, PropertyName);
39 static EncodedJSValue regExpConstructorDollar2(ExecState*, JSObject*, EncodedJSValue, PropertyName);
40 static EncodedJSValue regExpConstructorDollar3(ExecState*, JSObject*, EncodedJSValue, PropertyName);
41 static EncodedJSValue regExpConstructorDollar4(ExecState*, JSObject*, EncodedJSValue, PropertyName);
42 static EncodedJSValue regExpConstructorDollar5(ExecState*, JSObject*, EncodedJSValue, PropertyName);
43 static EncodedJSValue regExpConstructorDollar6(ExecState*, JSObject*, EncodedJSValue, PropertyName);
44 static EncodedJSValue regExpConstructorDollar7(ExecState*, JSObject*, EncodedJSValue, PropertyName);
45 static EncodedJSValue regExpConstructorDollar8(ExecState*, JSObject*, EncodedJSValue, PropertyName);
46 static EncodedJSValue regExpConstructorDollar9(ExecState*, JSObject*, EncodedJSValue, PropertyName);
47
48 static void setRegExpConstructorInput(ExecState*, JSObject*, EncodedJSValue, EncodedJSValue);
49 static void setRegExpConstructorMultiline(ExecState*, JSObject*, EncodedJSValue, EncodedJSValue);
50
51 } // namespace JSC
52
53 #include "RegExpConstructor.lut.h"
54
55 namespace JSC {
56
57 const ClassInfo RegExpConstructor::s_info = { "Function", &InternalFunction::s_info, &regExpConstructorTable, CREATE_METHOD_TABLE(RegExpConstructor) };
58
59 /* Source for RegExpConstructor.lut.h
60 @begin regExpConstructorTable
61 input regExpConstructorInput None
62 $_ regExpConstructorInput DontEnum
63 multiline regExpConstructorMultiline None
64 $* regExpConstructorMultiline DontEnum
65 lastMatch regExpConstructorLastMatch DontDelete|ReadOnly
66 $& regExpConstructorLastMatch DontDelete|ReadOnly|DontEnum
67 lastParen regExpConstructorLastParen DontDelete|ReadOnly
68 $+ regExpConstructorLastParen DontDelete|ReadOnly|DontEnum
69 leftContext regExpConstructorLeftContext DontDelete|ReadOnly
70 $` regExpConstructorLeftContext DontDelete|ReadOnly|DontEnum
71 rightContext regExpConstructorRightContext DontDelete|ReadOnly
72 $' regExpConstructorRightContext DontDelete|ReadOnly|DontEnum
73 $1 regExpConstructorDollar1 DontDelete|ReadOnly
74 $2 regExpConstructorDollar2 DontDelete|ReadOnly
75 $3 regExpConstructorDollar3 DontDelete|ReadOnly
76 $4 regExpConstructorDollar4 DontDelete|ReadOnly
77 $5 regExpConstructorDollar5 DontDelete|ReadOnly
78 $6 regExpConstructorDollar6 DontDelete|ReadOnly
79 $7 regExpConstructorDollar7 DontDelete|ReadOnly
80 $8 regExpConstructorDollar8 DontDelete|ReadOnly
81 $9 regExpConstructorDollar9 DontDelete|ReadOnly
82 @end
83 */
84
85 RegExpConstructor::RegExpConstructor(VM& vm, Structure* structure, RegExpPrototype* regExpPrototype)
86 : InternalFunction(vm, structure)
87 , m_cachedResult(vm, this, regExpPrototype->regExp())
88 , m_multiline(false)
89 {
90 }
91
92 void RegExpConstructor::finishCreation(VM& vm, RegExpPrototype* regExpPrototype)
93 {
94 Base::finishCreation(vm, regExpPrototype->classInfo()->className);
95 ASSERT(inherits(info()));
96
97 // ECMA 15.10.5.1 RegExp.prototype
98 putDirectWithoutTransition(vm, vm.propertyNames->prototype, regExpPrototype, DontEnum | DontDelete | ReadOnly);
99
100 // no. of arguments for constructor
101 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(2), ReadOnly | DontDelete | DontEnum);
102 }
103
104 void RegExpConstructor::destroy(JSCell* cell)
105 {
106 static_cast<RegExpConstructor*>(cell)->RegExpConstructor::~RegExpConstructor();
107 }
108
109 void RegExpConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
110 {
111 RegExpConstructor* thisObject = jsCast<RegExpConstructor*>(cell);
112 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
113 Base::visitChildren(thisObject, visitor);
114 thisObject->m_cachedResult.visitChildren(visitor);
115 }
116
117 JSValue RegExpConstructor::getBackref(ExecState* exec, unsigned i)
118 {
119 JSArray* array = m_cachedResult.lastResult(exec, this);
120
121 if (i < array->length()) {
122 JSValue result = JSValue(array).get(exec, i);
123 ASSERT(result.isString() || result.isUndefined());
124 if (!result.isUndefined())
125 return result;
126 }
127 return jsEmptyString(exec);
128 }
129
130 JSValue RegExpConstructor::getLastParen(ExecState* exec)
131 {
132 JSArray* array = m_cachedResult.lastResult(exec, this);
133 unsigned length = array->length();
134 if (length > 1) {
135 JSValue result = JSValue(array).get(exec, length - 1);
136 ASSERT(result.isString() || result.isUndefined());
137 if (!result.isUndefined())
138 return result;
139 }
140 return jsEmptyString(exec);
141 }
142
143 JSValue RegExpConstructor::getLeftContext(ExecState* exec)
144 {
145 return m_cachedResult.leftContext(exec, this);
146 }
147
148 JSValue RegExpConstructor::getRightContext(ExecState* exec)
149 {
150 return m_cachedResult.rightContext(exec, this);
151 }
152
153 bool RegExpConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
154 {
155 return getStaticValueSlot<RegExpConstructor, InternalFunction>(exec, regExpConstructorTable, jsCast<RegExpConstructor*>(object), propertyName, slot);
156 }
157
158 EncodedJSValue regExpConstructorDollar1(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
159 {
160 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 1));
161 }
162
163 EncodedJSValue regExpConstructorDollar2(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
164 {
165 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 2));
166 }
167
168 EncodedJSValue regExpConstructorDollar3(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
169 {
170 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 3));
171 }
172
173 EncodedJSValue regExpConstructorDollar4(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
174 {
175 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 4));
176 }
177
178 EncodedJSValue regExpConstructorDollar5(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
179 {
180 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 5));
181 }
182
183 EncodedJSValue regExpConstructorDollar6(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
184 {
185 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 6));
186 }
187
188 EncodedJSValue regExpConstructorDollar7(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
189 {
190 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 7));
191 }
192
193 EncodedJSValue regExpConstructorDollar8(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
194 {
195 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 8));
196 }
197
198 EncodedJSValue regExpConstructorDollar9(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
199 {
200 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 9));
201 }
202
203 EncodedJSValue regExpConstructorInput(ExecState*, JSObject* slotBase, EncodedJSValue, PropertyName)
204 {
205 return JSValue::encode(asRegExpConstructor(slotBase)->input());
206 }
207
208 EncodedJSValue regExpConstructorMultiline(ExecState*, JSObject* slotBase, EncodedJSValue, PropertyName)
209 {
210 return JSValue::encode(jsBoolean(asRegExpConstructor(slotBase)->multiline()));
211 }
212
213 EncodedJSValue regExpConstructorLastMatch(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
214 {
215 return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 0));
216 }
217
218 EncodedJSValue regExpConstructorLastParen(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
219 {
220 return JSValue::encode(asRegExpConstructor(slotBase)->getLastParen(exec));
221 }
222
223 EncodedJSValue regExpConstructorLeftContext(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
224 {
225 return JSValue::encode(asRegExpConstructor(slotBase)->getLeftContext(exec));
226 }
227
228 EncodedJSValue regExpConstructorRightContext(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
229 {
230 return JSValue::encode(asRegExpConstructor(slotBase)->getRightContext(exec));
231 }
232
233 void setRegExpConstructorInput(ExecState* exec, JSObject* baseObject, EncodedJSValue, EncodedJSValue value)
234 {
235 if (auto constructor = jsDynamicCast<RegExpConstructor*>(baseObject))
236 constructor->setInput(exec, JSValue::decode(value).toString(exec));
237 }
238
239 void setRegExpConstructorMultiline(ExecState* exec, JSObject* baseObject, EncodedJSValue, EncodedJSValue value)
240 {
241 if (auto constructor = jsDynamicCast<RegExpConstructor*>(baseObject))
242 constructor->setMultiline(JSValue::decode(value).toBoolean(exec));
243 }
244
245 // ECMA 15.10.4
246 JSObject* constructRegExp(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, bool callAsConstructor)
247 {
248 JSValue arg0 = args.at(0);
249 JSValue arg1 = args.at(1);
250
251 if (arg0.inherits(RegExpObject::info())) {
252 if (!arg1.isUndefined())
253 return exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Cannot supply flags when constructing one RegExp from another.")));
254 // If called as a function, this just returns the first argument (see 15.10.3.1).
255 if (callAsConstructor) {
256 RegExp* regExp = static_cast<RegExpObject*>(asObject(arg0))->regExp();
257 return RegExpObject::create(exec->vm(), globalObject->regExpStructure(), regExp);
258 }
259 return asObject(arg0);
260 }
261
262 String pattern = arg0.isUndefined() ? emptyString() : arg0.toString(exec)->value(exec);
263 if (exec->hadException())
264 return 0;
265
266 RegExpFlags flags = NoFlags;
267 if (!arg1.isUndefined()) {
268 flags = regExpFlags(arg1.toString(exec)->value(exec));
269 if (exec->hadException())
270 return 0;
271 if (flags == InvalidFlags)
272 return exec->vm().throwException(exec, createSyntaxError(exec, ASCIILiteral("Invalid flags supplied to RegExp constructor.")));
273 }
274
275 VM& vm = exec->vm();
276 RegExp* regExp = RegExp::create(vm, pattern, flags);
277 if (!regExp->isValid())
278 return vm.throwException(exec, createSyntaxError(exec, regExp->errorMessage()));
279 return RegExpObject::create(vm, globalObject->regExpStructure(), regExp);
280 }
281
282 static EncodedJSValue JSC_HOST_CALL constructWithRegExpConstructor(ExecState* exec)
283 {
284 ArgList args(exec);
285 return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args, true));
286 }
287
288 ConstructType RegExpConstructor::getConstructData(JSCell*, ConstructData& constructData)
289 {
290 constructData.native.function = constructWithRegExpConstructor;
291 return ConstructTypeHost;
292 }
293
294 // ECMA 15.10.3
295 static EncodedJSValue JSC_HOST_CALL callRegExpConstructor(ExecState* exec)
296 {
297 ArgList args(exec);
298 return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args));
299 }
300
301 CallType RegExpConstructor::getCallData(JSCell*, CallData& callData)
302 {
303 callData.native.function = callRegExpConstructor;
304 return CallTypeHost;
305 }
306
307 } // namespace JSC