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 };
72 /* Source for RegExpConstructor.lut.h
73 @begin regExpConstructorTable
74 input regExpConstructorInput None
75 $_ regExpConstructorInput DontEnum
76 multiline regExpConstructorMultiline None
77 $* regExpConstructorMultiline DontEnum
78 lastMatch regExpConstructorLastMatch DontDelete|ReadOnly
79 $& regExpConstructorLastMatch DontDelete|ReadOnly|DontEnum
80 lastParen regExpConstructorLastParen DontDelete|ReadOnly
81 $+ regExpConstructorLastParen DontDelete|ReadOnly|DontEnum
82 leftContext regExpConstructorLeftContext DontDelete|ReadOnly
83 $` regExpConstructorLeftContext DontDelete|ReadOnly|DontEnum
84 rightContext regExpConstructorRightContext DontDelete|ReadOnly
85 $' regExpConstructorRightContext DontDelete|ReadOnly|DontEnum
86 $1 regExpConstructorDollar1 DontDelete|ReadOnly
87 $2 regExpConstructorDollar2 DontDelete|ReadOnly
88 $3 regExpConstructorDollar3 DontDelete|ReadOnly
89 $4 regExpConstructorDollar4 DontDelete|ReadOnly
90 $5 regExpConstructorDollar5 DontDelete|ReadOnly
91 $6 regExpConstructorDollar6 DontDelete|ReadOnly
92 $7 regExpConstructorDollar7 DontDelete|ReadOnly
93 $8 regExpConstructorDollar8 DontDelete|ReadOnly
94 $9 regExpConstructorDollar9 DontDelete|ReadOnly
98 RegExpConstructor::RegExpConstructor(JSGlobalObject* globalObject, Structure* structure)
99 : InternalFunction(globalObject, structure)
100 , d(adoptPtr(new RegExpConstructorPrivate))
104 void RegExpConstructor::finishCreation(ExecState* exec, RegExpPrototype* regExpPrototype)
106 Base::finishCreation(exec->globalData(), Identifier(exec, "RegExp"));
107 ASSERT(inherits(&s_info));
109 // ECMA 15.10.5.1 RegExp.prototype
110 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, regExpPrototype, DontEnum | DontDelete | ReadOnly);
112 // no. of arguments for constructor
113 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(2), ReadOnly | DontDelete | DontEnum);
116 RegExpMatchesArray::RegExpMatchesArray(ExecState* exec)
117 : JSArray(exec->globalData(), exec->lexicalGlobalObject()->regExpMatchesArrayStructure())
121 void RegExpMatchesArray::finishCreation(JSGlobalData& globalData, RegExpConstructorPrivate* data)
123 Base::finishCreation(globalData, data->lastNumSubPatterns + 1, CreateInitialized);
124 RegExpConstructorPrivate* d = new RegExpConstructorPrivate;
125 d->input = data->lastInput;
126 d->lastInput = data->lastInput;
127 d->lastNumSubPatterns = data->lastNumSubPatterns;
128 unsigned offsetVectorSize = (data->lastNumSubPatterns + 1) * 2; // only copying the result part of the vector
129 d->lastOvector().resize(offsetVectorSize);
130 memcpy(d->lastOvector().data(), data->lastOvector().data(), offsetVectorSize * sizeof(int));
131 // d->multiline is not needed, and remains uninitialized
136 RegExpMatchesArray::~RegExpMatchesArray()
138 delete static_cast<RegExpConstructorPrivate*>(subclassData());
141 void RegExpMatchesArray::fillArrayInstance(ExecState* exec)
143 RegExpConstructorPrivate* d = static_cast<RegExpConstructorPrivate*>(subclassData());
146 unsigned lastNumSubpatterns = d->lastNumSubPatterns;
148 for (unsigned i = 0; i <= lastNumSubpatterns; ++i) {
149 int start = d->lastOvector()[2 * i];
151 JSArray::put(exec, i, jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start));
153 JSArray::put(exec, i, jsUndefined());
156 PutPropertySlot slot;
157 JSArray::put(exec, exec->propertyNames().index, jsNumber(d->lastOvector()[0]), slot);
158 JSArray::put(exec, exec->propertyNames().input, jsString(exec, d->input), slot);
164 JSObject* RegExpConstructor::arrayOfMatches(ExecState* exec) const
166 return RegExpMatchesArray::create(exec, d.get());
169 JSValue RegExpConstructor::getBackref(ExecState* exec, unsigned i) const
171 if (!d->lastOvector().isEmpty() && i <= d->lastNumSubPatterns) {
172 int start = d->lastOvector()[2 * i];
174 return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start);
176 return jsEmptyString(exec);
179 JSValue RegExpConstructor::getLastParen(ExecState* exec) const
181 unsigned i = d->lastNumSubPatterns;
183 ASSERT(!d->lastOvector().isEmpty());
184 int start = d->lastOvector()[2 * i];
186 return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start);
188 return jsEmptyString(exec);
191 JSValue RegExpConstructor::getLeftContext(ExecState* exec) const
193 if (!d->lastOvector().isEmpty())
194 return jsSubstring(exec, d->lastInput, 0, d->lastOvector()[0]);
195 return jsEmptyString(exec);
198 JSValue RegExpConstructor::getRightContext(ExecState* exec) const
200 if (!d->lastOvector().isEmpty())
201 return jsSubstring(exec, d->lastInput, d->lastOvector()[1], d->lastInput.length() - d->lastOvector()[1]);
202 return jsEmptyString(exec);
205 bool RegExpConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
207 return getStaticValueSlot<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), this, propertyName, slot);
210 bool RegExpConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
212 return getStaticValueDescriptor<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), this, propertyName, descriptor);
215 JSValue regExpConstructorDollar1(ExecState* exec, JSValue slotBase, const Identifier&)
217 return asRegExpConstructor(slotBase)->getBackref(exec, 1);
220 JSValue regExpConstructorDollar2(ExecState* exec, JSValue slotBase, const Identifier&)
222 return asRegExpConstructor(slotBase)->getBackref(exec, 2);
225 JSValue regExpConstructorDollar3(ExecState* exec, JSValue slotBase, const Identifier&)
227 return asRegExpConstructor(slotBase)->getBackref(exec, 3);
230 JSValue regExpConstructorDollar4(ExecState* exec, JSValue slotBase, const Identifier&)
232 return asRegExpConstructor(slotBase)->getBackref(exec, 4);
235 JSValue regExpConstructorDollar5(ExecState* exec, JSValue slotBase, const Identifier&)
237 return asRegExpConstructor(slotBase)->getBackref(exec, 5);
240 JSValue regExpConstructorDollar6(ExecState* exec, JSValue slotBase, const Identifier&)
242 return asRegExpConstructor(slotBase)->getBackref(exec, 6);
245 JSValue regExpConstructorDollar7(ExecState* exec, JSValue slotBase, const Identifier&)
247 return asRegExpConstructor(slotBase)->getBackref(exec, 7);
250 JSValue regExpConstructorDollar8(ExecState* exec, JSValue slotBase, const Identifier&)
252 return asRegExpConstructor(slotBase)->getBackref(exec, 8);
255 JSValue regExpConstructorDollar9(ExecState* exec, JSValue slotBase, const Identifier&)
257 return asRegExpConstructor(slotBase)->getBackref(exec, 9);
260 JSValue regExpConstructorInput(ExecState* exec, JSValue slotBase, const Identifier&)
262 return jsString(exec, asRegExpConstructor(slotBase)->input());
265 JSValue regExpConstructorMultiline(ExecState*, JSValue slotBase, const Identifier&)
267 return jsBoolean(asRegExpConstructor(slotBase)->multiline());
270 JSValue regExpConstructorLastMatch(ExecState* exec, JSValue slotBase, const Identifier&)
272 return asRegExpConstructor(slotBase)->getBackref(exec, 0);
275 JSValue regExpConstructorLastParen(ExecState* exec, JSValue slotBase, const Identifier&)
277 return asRegExpConstructor(slotBase)->getLastParen(exec);
280 JSValue regExpConstructorLeftContext(ExecState* exec, JSValue slotBase, const Identifier&)
282 return asRegExpConstructor(slotBase)->getLeftContext(exec);
285 JSValue regExpConstructorRightContext(ExecState* exec, JSValue slotBase, const Identifier&)
287 return asRegExpConstructor(slotBase)->getRightContext(exec);
290 void RegExpConstructor::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
292 lookupPut<RegExpConstructor, InternalFunction>(exec, propertyName, value, ExecState::regExpConstructorTable(exec), this, slot);
295 void setRegExpConstructorInput(ExecState* exec, JSObject* baseObject, JSValue value)
297 asRegExpConstructor(baseObject)->setInput(value.toString(exec));
300 void setRegExpConstructorMultiline(ExecState* exec, JSObject* baseObject, JSValue value)
302 asRegExpConstructor(baseObject)->setMultiline(value.toBoolean(exec));
306 JSObject* constructRegExp(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, bool callAsConstructor)
308 JSValue arg0 = args.at(0);
309 JSValue arg1 = args.at(1);
311 if (arg0.inherits(&RegExpObject::s_info)) {
312 if (!arg1.isUndefined())
313 return throwError(exec, createTypeError(exec, "Cannot supply flags when constructing one RegExp from another."));
314 // If called as a function, this just returns the first argument (see 15.10.3.1).
315 if (callAsConstructor) {
316 RegExp* regExp = static_cast<RegExpObject*>(asObject(arg0))->regExp();
317 return RegExpObject::create(exec, globalObject, globalObject->regExpStructure(), regExp);
319 return asObject(arg0);
322 UString pattern = arg0.isUndefined() ? UString("") : arg0.toString(exec);
323 if (exec->hadException())
326 RegExpFlags flags = NoFlags;
327 if (!arg1.isUndefined()) {
328 flags = regExpFlags(arg1.toString(exec));
329 if (exec->hadException())
331 if (flags == InvalidFlags)
332 return throwError(exec, createSyntaxError(exec, "Invalid flags supplied to RegExp constructor."));
335 RegExp* regExp = RegExp::create(exec->globalData(), pattern, flags);
336 if (!regExp->isValid())
337 return throwError(exec, createSyntaxError(exec, regExp->errorMessage()));
338 return RegExpObject::create(exec, exec->lexicalGlobalObject(), globalObject->regExpStructure(), regExp);
341 static EncodedJSValue JSC_HOST_CALL constructWithRegExpConstructor(ExecState* exec)
344 return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args, true));
347 ConstructType RegExpConstructor::getConstructData(ConstructData& constructData)
349 constructData.native.function = constructWithRegExpConstructor;
350 return ConstructTypeHost;
354 static EncodedJSValue JSC_HOST_CALL callRegExpConstructor(ExecState* exec)
357 return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args));
360 CallType RegExpConstructor::getCallData(CallData& callData)
362 callData.native.function = callRegExpConstructor;
366 void RegExpConstructor::setInput(const UString& input)
371 const UString& RegExpConstructor::input() const
373 // Can detect a distinct initial state that is invisible to JavaScript, by checking for null
374 // state (since jsString turns null strings to empty strings).
378 void RegExpConstructor::setMultiline(bool multiline)
380 d->multiline = multiline;
383 bool RegExpConstructor::multiline() const