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.
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.
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.
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
23 #include "RegExpConstructor.h"
25 #include "ArrayPrototype.h"
27 #include "ExceptionHelpers.h"
29 #include "JSFunction.h"
32 #include "ObjectPrototype.h"
33 #include "RegExpMatchesArray.h"
34 #include "RegExpObject.h"
35 #include "RegExpPrototype.h"
37 #include "RegExpCache.h"
38 #include "UStringConcatenate.h"
39 #include <wtf/PassOwnPtr.h>
43 static JSValue regExpConstructorInput(ExecState*, JSValue, const Identifier&);
44 static JSValue regExpConstructorMultiline(ExecState*, JSValue, const Identifier&);
45 static JSValue regExpConstructorLastMatch(ExecState*, JSValue, const Identifier&);
46 static JSValue regExpConstructorLastParen(ExecState*, JSValue, const Identifier&);
47 static JSValue regExpConstructorLeftContext(ExecState*, JSValue, const Identifier&);
48 static JSValue regExpConstructorRightContext(ExecState*, JSValue, const Identifier&);
49 static JSValue regExpConstructorDollar1(ExecState*, JSValue, const Identifier&);
50 static JSValue regExpConstructorDollar2(ExecState*, JSValue, const Identifier&);
51 static JSValue regExpConstructorDollar3(ExecState*, JSValue, const Identifier&);
52 static JSValue regExpConstructorDollar4(ExecState*, JSValue, const Identifier&);
53 static JSValue regExpConstructorDollar5(ExecState*, JSValue, const Identifier&);
54 static JSValue regExpConstructorDollar6(ExecState*, JSValue, const Identifier&);
55 static JSValue regExpConstructorDollar7(ExecState*, JSValue, const Identifier&);
56 static JSValue regExpConstructorDollar8(ExecState*, JSValue, const Identifier&);
57 static JSValue regExpConstructorDollar9(ExecState*, JSValue, const Identifier&);
59 static void setRegExpConstructorInput(ExecState*, JSObject*, JSValue);
60 static void setRegExpConstructorMultiline(ExecState*, JSObject*, JSValue);
64 #include "RegExpConstructor.lut.h"
68 ASSERT_CLASS_FITS_IN_CELL(RegExpConstructor);
70 const ClassInfo RegExpConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::regExpConstructorTable, CREATE_METHOD_TABLE(RegExpConstructor) };
72 const ClassInfo RegExpMatchesArray::s_info = {"Array", &JSArray::s_info, 0, 0, CREATE_METHOD_TABLE(RegExpMatchesArray)};
74 /* Source for RegExpConstructor.lut.h
75 @begin regExpConstructorTable
76 input regExpConstructorInput None
77 $_ regExpConstructorInput DontEnum
78 multiline regExpConstructorMultiline None
79 $* regExpConstructorMultiline DontEnum
80 lastMatch regExpConstructorLastMatch DontDelete|ReadOnly
81 $& regExpConstructorLastMatch DontDelete|ReadOnly|DontEnum
82 lastParen regExpConstructorLastParen DontDelete|ReadOnly
83 $+ regExpConstructorLastParen DontDelete|ReadOnly|DontEnum
84 leftContext regExpConstructorLeftContext DontDelete|ReadOnly
85 $` regExpConstructorLeftContext DontDelete|ReadOnly|DontEnum
86 rightContext regExpConstructorRightContext DontDelete|ReadOnly
87 $' regExpConstructorRightContext DontDelete|ReadOnly|DontEnum
88 $1 regExpConstructorDollar1 DontDelete|ReadOnly
89 $2 regExpConstructorDollar2 DontDelete|ReadOnly
90 $3 regExpConstructorDollar3 DontDelete|ReadOnly
91 $4 regExpConstructorDollar4 DontDelete|ReadOnly
92 $5 regExpConstructorDollar5 DontDelete|ReadOnly
93 $6 regExpConstructorDollar6 DontDelete|ReadOnly
94 $7 regExpConstructorDollar7 DontDelete|ReadOnly
95 $8 regExpConstructorDollar8 DontDelete|ReadOnly
96 $9 regExpConstructorDollar9 DontDelete|ReadOnly
100 RegExpConstructor::RegExpConstructor(JSGlobalObject* globalObject, Structure* structure)
101 : InternalFunction(globalObject, structure)
102 , d(adoptPtr(new RegExpConstructorPrivate))
106 void RegExpConstructor::finishCreation(ExecState* exec, RegExpPrototype* regExpPrototype)
108 Base::finishCreation(exec->globalData(), Identifier(exec, "RegExp"));
109 ASSERT(inherits(&s_info));
111 // ECMA 15.10.5.1 RegExp.prototype
112 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, regExpPrototype, DontEnum | DontDelete | ReadOnly);
114 // no. of arguments for constructor
115 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(2), ReadOnly | DontDelete | DontEnum);
118 RegExpMatchesArray::RegExpMatchesArray(ExecState* exec)
119 : JSArray(exec->globalData(), exec->lexicalGlobalObject()->regExpMatchesArrayStructure())
123 void RegExpMatchesArray::finishCreation(JSGlobalData& globalData, RegExpConstructorPrivate* data)
125 Base::finishCreation(globalData, data->lastNumSubPatterns + 1, CreateInitialized);
126 RegExpConstructorPrivate* d = new RegExpConstructorPrivate;
127 d->input = data->lastInput;
128 d->lastInput = data->lastInput;
129 d->lastNumSubPatterns = data->lastNumSubPatterns;
130 unsigned offsetVectorSize = (data->lastNumSubPatterns + 1) * 2; // only copying the result part of the vector
131 d->lastOvector().resize(offsetVectorSize);
132 memcpy(d->lastOvector().data(), data->lastOvector().data(), offsetVectorSize * sizeof(int));
133 // d->multiline is not needed, and remains uninitialized
138 RegExpMatchesArray::~RegExpMatchesArray()
140 delete static_cast<RegExpConstructorPrivate*>(subclassData());
143 void RegExpMatchesArray::fillArrayInstance(ExecState* exec)
145 RegExpConstructorPrivate* d = static_cast<RegExpConstructorPrivate*>(subclassData());
148 unsigned lastNumSubpatterns = d->lastNumSubPatterns;
150 for (unsigned i = 0; i <= lastNumSubpatterns; ++i) {
151 int start = d->lastOvector()[2 * i];
153 JSArray::putByIndex(this, exec, i, jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start));
155 JSArray::putByIndex(this, exec, i, jsUndefined());
158 PutPropertySlot slot;
159 JSArray::put(this, exec, exec->propertyNames().index, jsNumber(d->lastOvector()[0]), slot);
160 JSArray::put(this, exec, exec->propertyNames().input, jsString(exec, d->input), slot);
166 JSObject* RegExpConstructor::arrayOfMatches(ExecState* exec) const
168 return RegExpMatchesArray::create(exec, d.get());
171 JSValue RegExpConstructor::getBackref(ExecState* exec, unsigned i) const
173 if (!d->lastOvector().isEmpty() && i <= d->lastNumSubPatterns) {
174 int start = d->lastOvector()[2 * i];
176 return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start);
178 return jsEmptyString(exec);
181 JSValue RegExpConstructor::getLastParen(ExecState* exec) const
183 unsigned i = d->lastNumSubPatterns;
185 ASSERT(!d->lastOvector().isEmpty());
186 int start = d->lastOvector()[2 * i];
188 return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start);
190 return jsEmptyString(exec);
193 JSValue RegExpConstructor::getLeftContext(ExecState* exec) const
195 if (!d->lastOvector().isEmpty())
196 return jsSubstring(exec, d->lastInput, 0, d->lastOvector()[0]);
197 return jsEmptyString(exec);
200 JSValue RegExpConstructor::getRightContext(ExecState* exec) const
202 if (!d->lastOvector().isEmpty())
203 return jsSubstring(exec, d->lastInput, d->lastOvector()[1], d->lastInput.length() - d->lastOvector()[1]);
204 return jsEmptyString(exec);
207 bool RegExpConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
209 return getStaticValueSlot<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), jsCast<RegExpConstructor*>(cell), propertyName, slot);
212 bool RegExpConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
214 return getStaticValueDescriptor<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), jsCast<RegExpConstructor*>(object), propertyName, descriptor);
217 JSValue regExpConstructorDollar1(ExecState* exec, JSValue slotBase, const Identifier&)
219 return asRegExpConstructor(slotBase)->getBackref(exec, 1);
222 JSValue regExpConstructorDollar2(ExecState* exec, JSValue slotBase, const Identifier&)
224 return asRegExpConstructor(slotBase)->getBackref(exec, 2);
227 JSValue regExpConstructorDollar3(ExecState* exec, JSValue slotBase, const Identifier&)
229 return asRegExpConstructor(slotBase)->getBackref(exec, 3);
232 JSValue regExpConstructorDollar4(ExecState* exec, JSValue slotBase, const Identifier&)
234 return asRegExpConstructor(slotBase)->getBackref(exec, 4);
237 JSValue regExpConstructorDollar5(ExecState* exec, JSValue slotBase, const Identifier&)
239 return asRegExpConstructor(slotBase)->getBackref(exec, 5);
242 JSValue regExpConstructorDollar6(ExecState* exec, JSValue slotBase, const Identifier&)
244 return asRegExpConstructor(slotBase)->getBackref(exec, 6);
247 JSValue regExpConstructorDollar7(ExecState* exec, JSValue slotBase, const Identifier&)
249 return asRegExpConstructor(slotBase)->getBackref(exec, 7);
252 JSValue regExpConstructorDollar8(ExecState* exec, JSValue slotBase, const Identifier&)
254 return asRegExpConstructor(slotBase)->getBackref(exec, 8);
257 JSValue regExpConstructorDollar9(ExecState* exec, JSValue slotBase, const Identifier&)
259 return asRegExpConstructor(slotBase)->getBackref(exec, 9);
262 JSValue regExpConstructorInput(ExecState* exec, JSValue slotBase, const Identifier&)
264 return jsString(exec, asRegExpConstructor(slotBase)->input());
267 JSValue regExpConstructorMultiline(ExecState*, JSValue slotBase, const Identifier&)
269 return jsBoolean(asRegExpConstructor(slotBase)->multiline());
272 JSValue regExpConstructorLastMatch(ExecState* exec, JSValue slotBase, const Identifier&)
274 return asRegExpConstructor(slotBase)->getBackref(exec, 0);
277 JSValue regExpConstructorLastParen(ExecState* exec, JSValue slotBase, const Identifier&)
279 return asRegExpConstructor(slotBase)->getLastParen(exec);
282 JSValue regExpConstructorLeftContext(ExecState* exec, JSValue slotBase, const Identifier&)
284 return asRegExpConstructor(slotBase)->getLeftContext(exec);
287 JSValue regExpConstructorRightContext(ExecState* exec, JSValue slotBase, const Identifier&)
289 return asRegExpConstructor(slotBase)->getRightContext(exec);
292 void RegExpConstructor::put(JSCell* cell, ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
294 lookupPut<RegExpConstructor, InternalFunction>(exec, propertyName, value, ExecState::regExpConstructorTable(exec), jsCast<RegExpConstructor*>(cell), slot);
297 void setRegExpConstructorInput(ExecState* exec, JSObject* baseObject, JSValue value)
299 asRegExpConstructor(baseObject)->setInput(value.toString(exec));
302 void setRegExpConstructorMultiline(ExecState* exec, JSObject* baseObject, JSValue value)
304 asRegExpConstructor(baseObject)->setMultiline(value.toBoolean(exec));
308 JSObject* constructRegExp(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, bool callAsConstructor)
310 JSValue arg0 = args.at(0);
311 JSValue arg1 = args.at(1);
313 if (arg0.inherits(&RegExpObject::s_info)) {
314 if (!arg1.isUndefined())
315 return throwError(exec, createTypeError(exec, "Cannot supply flags when constructing one RegExp from another."));
316 // If called as a function, this just returns the first argument (see 15.10.3.1).
317 if (callAsConstructor) {
318 RegExp* regExp = static_cast<RegExpObject*>(asObject(arg0))->regExp();
319 return RegExpObject::create(exec, globalObject, globalObject->regExpStructure(), regExp);
321 return asObject(arg0);
324 UString pattern = arg0.isUndefined() ? UString("") : arg0.toString(exec);
325 if (exec->hadException())
328 RegExpFlags flags = NoFlags;
329 if (!arg1.isUndefined()) {
330 flags = regExpFlags(arg1.toString(exec));
331 if (exec->hadException())
333 if (flags == InvalidFlags)
334 return throwError(exec, createSyntaxError(exec, "Invalid flags supplied to RegExp constructor."));
337 RegExp* regExp = RegExp::create(exec->globalData(), pattern, flags);
338 if (!regExp->isValid())
339 return throwError(exec, createSyntaxError(exec, regExp->errorMessage()));
340 return RegExpObject::create(exec, exec->lexicalGlobalObject(), globalObject->regExpStructure(), regExp);
343 static EncodedJSValue JSC_HOST_CALL constructWithRegExpConstructor(ExecState* exec)
346 return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args, true));
349 ConstructType RegExpConstructor::getConstructData(JSCell*, ConstructData& constructData)
351 constructData.native.function = constructWithRegExpConstructor;
352 return ConstructTypeHost;
356 static EncodedJSValue JSC_HOST_CALL callRegExpConstructor(ExecState* exec)
359 return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args));
362 CallType RegExpConstructor::getCallData(JSCell*, CallData& callData)
364 callData.native.function = callRegExpConstructor;
368 void RegExpConstructor::setInput(const UString& input)
373 const UString& RegExpConstructor::input() const
375 // Can detect a distinct initial state that is invisible to JavaScript, by checking for null
376 // state (since jsString turns null strings to empty strings).
380 void RegExpConstructor::setMultiline(bool multiline)
382 d->multiline = multiline;
385 bool RegExpConstructor::multiline() const