initial import
[vuplus_webkit] / Source / JavaScriptCore / 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 "ArrayPrototype.h"
26 #include "Error.h"
27 #include "ExceptionHelpers.h"
28 #include "JSArray.h"
29 #include "JSFunction.h"
30 #include "JSString.h"
31 #include "Lookup.h"
32 #include "ObjectPrototype.h"
33 #include "RegExpMatchesArray.h"
34 #include "RegExpObject.h"
35 #include "RegExpPrototype.h"
36 #include "RegExp.h"
37 #include "RegExpCache.h"
38 #include "UStringConcatenate.h"
39 #include <wtf/PassOwnPtr.h>
40
41 namespace JSC {
42
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&);
58
59 static void setRegExpConstructorInput(ExecState*, JSObject*, JSValue);
60 static void setRegExpConstructorMultiline(ExecState*, JSObject*, JSValue);
61
62 } // namespace JSC
63
64 #include "RegExpConstructor.lut.h"
65
66 namespace JSC {
67
68 ASSERT_CLASS_FITS_IN_CELL(RegExpConstructor);
69
70 const ClassInfo RegExpConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::regExpConstructorTable };
71
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
95 @end
96 */
97
98 RegExpConstructor::RegExpConstructor(JSGlobalObject* globalObject, Structure* structure)
99     : InternalFunction(globalObject, structure)
100     , d(adoptPtr(new RegExpConstructorPrivate))
101 {
102 }
103
104 void RegExpConstructor::finishCreation(ExecState* exec, RegExpPrototype* regExpPrototype)
105 {
106     Base::finishCreation(exec->globalData(), Identifier(exec, "RegExp"));
107     ASSERT(inherits(&s_info));
108
109     // ECMA 15.10.5.1 RegExp.prototype
110     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, regExpPrototype, DontEnum | DontDelete | ReadOnly);
111
112     // no. of arguments for constructor
113     putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(2), ReadOnly | DontDelete | DontEnum);
114 }
115
116 RegExpMatchesArray::RegExpMatchesArray(ExecState* exec)
117     : JSArray(exec->globalData(), exec->lexicalGlobalObject()->regExpMatchesArrayStructure())
118 {
119 }
120
121 void RegExpMatchesArray::finishCreation(JSGlobalData& globalData, RegExpConstructorPrivate* data)
122 {
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
132
133     setSubclassData(d);
134 }
135
136 RegExpMatchesArray::~RegExpMatchesArray()
137 {
138     delete static_cast<RegExpConstructorPrivate*>(subclassData());
139 }
140
141 void RegExpMatchesArray::fillArrayInstance(ExecState* exec)
142 {
143     RegExpConstructorPrivate* d = static_cast<RegExpConstructorPrivate*>(subclassData());
144     ASSERT(d);
145
146     unsigned lastNumSubpatterns = d->lastNumSubPatterns;
147
148     for (unsigned i = 0; i <= lastNumSubpatterns; ++i) {
149         int start = d->lastOvector()[2 * i];
150         if (start >= 0)
151             JSArray::put(exec, i, jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start));
152         else
153             JSArray::put(exec, i, jsUndefined());
154     }
155
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);
159
160     delete d;
161     setSubclassData(0);
162 }
163
164 JSObject* RegExpConstructor::arrayOfMatches(ExecState* exec) const
165 {
166     return RegExpMatchesArray::create(exec, d.get());
167 }
168
169 JSValue RegExpConstructor::getBackref(ExecState* exec, unsigned i) const
170 {
171     if (!d->lastOvector().isEmpty() && i <= d->lastNumSubPatterns) {
172         int start = d->lastOvector()[2 * i];
173         if (start >= 0)
174             return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start);
175     }
176     return jsEmptyString(exec);
177 }
178
179 JSValue RegExpConstructor::getLastParen(ExecState* exec) const
180 {
181     unsigned i = d->lastNumSubPatterns;
182     if (i > 0) {
183         ASSERT(!d->lastOvector().isEmpty());
184         int start = d->lastOvector()[2 * i];
185         if (start >= 0)
186             return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start);
187     }
188     return jsEmptyString(exec);
189 }
190
191 JSValue RegExpConstructor::getLeftContext(ExecState* exec) const
192 {
193     if (!d->lastOvector().isEmpty())
194         return jsSubstring(exec, d->lastInput, 0, d->lastOvector()[0]);
195     return jsEmptyString(exec);
196 }
197
198 JSValue RegExpConstructor::getRightContext(ExecState* exec) const
199 {
200     if (!d->lastOvector().isEmpty())
201         return jsSubstring(exec, d->lastInput, d->lastOvector()[1], d->lastInput.length() - d->lastOvector()[1]);
202     return jsEmptyString(exec);
203 }
204     
205 bool RegExpConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
206 {
207     return getStaticValueSlot<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), this, propertyName, slot);
208 }
209
210 bool RegExpConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
211 {
212     return getStaticValueDescriptor<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), this, propertyName, descriptor);
213 }
214
215 JSValue regExpConstructorDollar1(ExecState* exec, JSValue slotBase, const Identifier&)
216 {
217     return asRegExpConstructor(slotBase)->getBackref(exec, 1);
218 }
219
220 JSValue regExpConstructorDollar2(ExecState* exec, JSValue slotBase, const Identifier&)
221 {
222     return asRegExpConstructor(slotBase)->getBackref(exec, 2);
223 }
224
225 JSValue regExpConstructorDollar3(ExecState* exec, JSValue slotBase, const Identifier&)
226 {
227     return asRegExpConstructor(slotBase)->getBackref(exec, 3);
228 }
229
230 JSValue regExpConstructorDollar4(ExecState* exec, JSValue slotBase, const Identifier&)
231 {
232     return asRegExpConstructor(slotBase)->getBackref(exec, 4);
233 }
234
235 JSValue regExpConstructorDollar5(ExecState* exec, JSValue slotBase, const Identifier&)
236 {
237     return asRegExpConstructor(slotBase)->getBackref(exec, 5);
238 }
239
240 JSValue regExpConstructorDollar6(ExecState* exec, JSValue slotBase, const Identifier&)
241 {
242     return asRegExpConstructor(slotBase)->getBackref(exec, 6);
243 }
244
245 JSValue regExpConstructorDollar7(ExecState* exec, JSValue slotBase, const Identifier&)
246 {
247     return asRegExpConstructor(slotBase)->getBackref(exec, 7);
248 }
249
250 JSValue regExpConstructorDollar8(ExecState* exec, JSValue slotBase, const Identifier&)
251 {
252     return asRegExpConstructor(slotBase)->getBackref(exec, 8);
253 }
254
255 JSValue regExpConstructorDollar9(ExecState* exec, JSValue slotBase, const Identifier&)
256 {
257     return asRegExpConstructor(slotBase)->getBackref(exec, 9);
258 }
259
260 JSValue regExpConstructorInput(ExecState* exec, JSValue slotBase, const Identifier&)
261 {
262     return jsString(exec, asRegExpConstructor(slotBase)->input());
263 }
264
265 JSValue regExpConstructorMultiline(ExecState*, JSValue slotBase, const Identifier&)
266 {
267     return jsBoolean(asRegExpConstructor(slotBase)->multiline());
268 }
269
270 JSValue regExpConstructorLastMatch(ExecState* exec, JSValue slotBase, const Identifier&)
271 {
272     return asRegExpConstructor(slotBase)->getBackref(exec, 0);
273 }
274
275 JSValue regExpConstructorLastParen(ExecState* exec, JSValue slotBase, const Identifier&)
276 {
277     return asRegExpConstructor(slotBase)->getLastParen(exec);
278 }
279
280 JSValue regExpConstructorLeftContext(ExecState* exec, JSValue slotBase, const Identifier&)
281 {
282     return asRegExpConstructor(slotBase)->getLeftContext(exec);
283 }
284
285 JSValue regExpConstructorRightContext(ExecState* exec, JSValue slotBase, const Identifier&)
286 {
287     return asRegExpConstructor(slotBase)->getRightContext(exec);
288 }
289
290 void RegExpConstructor::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
291 {
292     lookupPut<RegExpConstructor, InternalFunction>(exec, propertyName, value, ExecState::regExpConstructorTable(exec), this, slot);
293 }
294
295 void setRegExpConstructorInput(ExecState* exec, JSObject* baseObject, JSValue value)
296 {
297     asRegExpConstructor(baseObject)->setInput(value.toString(exec));
298 }
299
300 void setRegExpConstructorMultiline(ExecState* exec, JSObject* baseObject, JSValue value)
301 {
302     asRegExpConstructor(baseObject)->setMultiline(value.toBoolean(exec));
303 }
304
305 // ECMA 15.10.4
306 JSObject* constructRegExp(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, bool callAsConstructor)
307 {
308     JSValue arg0 = args.at(0);
309     JSValue arg1 = args.at(1);
310
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);
318         }
319         return asObject(arg0);
320     }
321
322     UString pattern = arg0.isUndefined() ? UString("") : arg0.toString(exec);
323     if (exec->hadException())
324         return 0;
325
326     RegExpFlags flags = NoFlags;
327     if (!arg1.isUndefined()) {
328         flags = regExpFlags(arg1.toString(exec));
329         if (exec->hadException())
330             return 0;
331         if (flags == InvalidFlags)
332             return throwError(exec, createSyntaxError(exec, "Invalid flags supplied to RegExp constructor."));
333     }
334
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);
339 }
340
341 static EncodedJSValue JSC_HOST_CALL constructWithRegExpConstructor(ExecState* exec)
342 {
343     ArgList args(exec);
344     return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args, true));
345 }
346
347 ConstructType RegExpConstructor::getConstructData(ConstructData& constructData)
348 {
349     constructData.native.function = constructWithRegExpConstructor;
350     return ConstructTypeHost;
351 }
352
353 // ECMA 15.10.3
354 static EncodedJSValue JSC_HOST_CALL callRegExpConstructor(ExecState* exec)
355 {
356     ArgList args(exec);
357     return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args));
358 }
359
360 CallType RegExpConstructor::getCallData(CallData& callData)
361 {
362     callData.native.function = callRegExpConstructor;
363     return CallTypeHost;
364 }
365
366 void RegExpConstructor::setInput(const UString& input)
367 {
368     d->input = input;
369 }
370
371 const UString& RegExpConstructor::input() const
372 {
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).
375     return d->input;
376 }
377
378 void RegExpConstructor::setMultiline(bool multiline)
379 {
380     d->multiline = multiline;
381 }
382
383 bool RegExpConstructor::multiline() const
384 {
385     return d->multiline;
386 }
387
388 } // namespace JSC