Use a profile to store allocation structures for subclasses of InternalFunctions
[WebKit-https.git] / Source / JavaScriptCore / runtime / RegExpConstructor.cpp
1 /*
2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2003, 2007, 2008, 2016 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 #include "StructureInlines.h"
30
31 namespace JSC {
32
33 static EncodedJSValue regExpConstructorInput(ExecState*, JSObject*, EncodedJSValue, PropertyName);
34 static EncodedJSValue regExpConstructorMultiline(ExecState*, JSObject*, EncodedJSValue, PropertyName);
35 static EncodedJSValue regExpConstructorLastMatch(ExecState*, JSObject*, EncodedJSValue, PropertyName);
36 static EncodedJSValue regExpConstructorLastParen(ExecState*, JSObject*, EncodedJSValue, PropertyName);
37 static EncodedJSValue regExpConstructorLeftContext(ExecState*, JSObject*, EncodedJSValue, PropertyName);
38 static EncodedJSValue regExpConstructorRightContext(ExecState*, JSObject*, EncodedJSValue, PropertyName);
39 static EncodedJSValue regExpConstructorDollar1(ExecState*, JSObject*, EncodedJSValue, PropertyName);
40 static EncodedJSValue regExpConstructorDollar2(ExecState*, JSObject*, EncodedJSValue, PropertyName);
41 static EncodedJSValue regExpConstructorDollar3(ExecState*, JSObject*, EncodedJSValue, PropertyName);
42 static EncodedJSValue regExpConstructorDollar4(ExecState*, JSObject*, EncodedJSValue, PropertyName);
43 static EncodedJSValue regExpConstructorDollar5(ExecState*, JSObject*, EncodedJSValue, PropertyName);
44 static EncodedJSValue regExpConstructorDollar6(ExecState*, JSObject*, EncodedJSValue, PropertyName);
45 static EncodedJSValue regExpConstructorDollar7(ExecState*, JSObject*, EncodedJSValue, PropertyName);
46 static EncodedJSValue regExpConstructorDollar8(ExecState*, JSObject*, EncodedJSValue, PropertyName);
47 static EncodedJSValue regExpConstructorDollar9(ExecState*, JSObject*, EncodedJSValue, PropertyName);
48
49 static void setRegExpConstructorInput(ExecState*, JSObject*, EncodedJSValue, EncodedJSValue);
50 static void setRegExpConstructorMultiline(ExecState*, JSObject*, EncodedJSValue, EncodedJSValue);
51
52 } // namespace JSC
53
54 #include "RegExpConstructor.lut.h"
55
56 namespace JSC {
57
58 const ClassInfo RegExpConstructor::s_info = { "Function", &InternalFunction::s_info, &regExpConstructorTable, CREATE_METHOD_TABLE(RegExpConstructor) };
59
60 /* Source for RegExpConstructor.lut.h
61 @begin regExpConstructorTable
62     input           regExpConstructorInput          None
63     $_              regExpConstructorInput          DontEnum
64     multiline       regExpConstructorMultiline      None
65     $*              regExpConstructorMultiline      DontEnum
66     lastMatch       regExpConstructorLastMatch      DontDelete|ReadOnly
67     $&              regExpConstructorLastMatch      DontDelete|ReadOnly|DontEnum
68     lastParen       regExpConstructorLastParen      DontDelete|ReadOnly
69     $+              regExpConstructorLastParen      DontDelete|ReadOnly|DontEnum
70     leftContext     regExpConstructorLeftContext    DontDelete|ReadOnly
71     $`              regExpConstructorLeftContext    DontDelete|ReadOnly|DontEnum
72     rightContext    regExpConstructorRightContext   DontDelete|ReadOnly
73     $'              regExpConstructorRightContext   DontDelete|ReadOnly|DontEnum
74     $1              regExpConstructorDollar1        DontDelete|ReadOnly
75     $2              regExpConstructorDollar2        DontDelete|ReadOnly
76     $3              regExpConstructorDollar3        DontDelete|ReadOnly
77     $4              regExpConstructorDollar4        DontDelete|ReadOnly
78     $5              regExpConstructorDollar5        DontDelete|ReadOnly
79     $6              regExpConstructorDollar6        DontDelete|ReadOnly
80     $7              regExpConstructorDollar7        DontDelete|ReadOnly
81     $8              regExpConstructorDollar8        DontDelete|ReadOnly
82     $9              regExpConstructorDollar9        DontDelete|ReadOnly
83 @end
84 */
85
86 RegExpConstructor::RegExpConstructor(VM& vm, Structure* structure, RegExpPrototype* regExpPrototype)
87     : InternalFunction(vm, structure)
88     , m_cachedResult(vm, this, regExpPrototype->regExp())
89     , m_multiline(false)
90 {
91 }
92
93 void RegExpConstructor::finishCreation(VM& vm, RegExpPrototype* regExpPrototype)
94 {
95     Base::finishCreation(vm, regExpPrototype->classInfo()->className);
96     ASSERT(inherits(info()));
97
98     // ECMA 15.10.5.1 RegExp.prototype
99     putDirectWithoutTransition(vm, vm.propertyNames->prototype, regExpPrototype, DontEnum | DontDelete | ReadOnly);
100
101     // no. of arguments for constructor
102     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(2), ReadOnly | DontDelete | DontEnum);
103 }
104
105 void RegExpConstructor::destroy(JSCell* cell)
106 {
107     static_cast<RegExpConstructor*>(cell)->RegExpConstructor::~RegExpConstructor();
108 }
109
110 void RegExpConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
111 {
112     RegExpConstructor* thisObject = jsCast<RegExpConstructor*>(cell);
113     ASSERT_GC_OBJECT_INHERITS(thisObject, info());
114     Base::visitChildren(thisObject, visitor);
115     thisObject->m_cachedResult.visitChildren(visitor);
116 }
117
118 JSValue RegExpConstructor::getBackref(ExecState* exec, unsigned i)
119 {
120     JSArray* array = m_cachedResult.lastResult(exec, this);
121
122     if (i < array->length()) {
123         JSValue result = JSValue(array).get(exec, i);
124         ASSERT(result.isString() || result.isUndefined());
125         if (!result.isUndefined())
126             return result;
127     }
128     return jsEmptyString(exec);
129 }
130
131 JSValue RegExpConstructor::getLastParen(ExecState* exec)
132 {
133     JSArray* array = m_cachedResult.lastResult(exec, this);
134     unsigned length = array->length();
135     if (length > 1) {
136         JSValue result = JSValue(array).get(exec, length - 1);
137         ASSERT(result.isString() || result.isUndefined());
138         if (!result.isUndefined())
139             return result;
140     }
141     return jsEmptyString(exec);
142 }
143
144 JSValue RegExpConstructor::getLeftContext(ExecState* exec)
145 {
146     return m_cachedResult.leftContext(exec, this);
147 }
148
149 JSValue RegExpConstructor::getRightContext(ExecState* exec)
150 {
151     return m_cachedResult.rightContext(exec, this);
152 }
153     
154 bool RegExpConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
155 {
156     return getStaticValueSlot<RegExpConstructor, InternalFunction>(exec, regExpConstructorTable, jsCast<RegExpConstructor*>(object), propertyName, slot);
157 }
158     
159 EncodedJSValue regExpConstructorDollar1(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
160 {
161     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 1));
162 }
163
164 EncodedJSValue regExpConstructorDollar2(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
165 {
166     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 2));
167 }
168
169 EncodedJSValue regExpConstructorDollar3(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
170 {
171     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 3));
172 }
173
174 EncodedJSValue regExpConstructorDollar4(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
175 {
176     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 4));
177 }
178
179 EncodedJSValue regExpConstructorDollar5(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
180 {
181     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 5));
182 }
183
184 EncodedJSValue regExpConstructorDollar6(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
185 {
186     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 6));
187 }
188
189 EncodedJSValue regExpConstructorDollar7(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
190 {
191     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 7));
192 }
193
194 EncodedJSValue regExpConstructorDollar8(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
195 {
196     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 8));
197 }
198
199 EncodedJSValue regExpConstructorDollar9(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
200 {
201     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 9));
202 }
203
204 EncodedJSValue regExpConstructorInput(ExecState*, JSObject* slotBase, EncodedJSValue, PropertyName)
205 {
206     return JSValue::encode(asRegExpConstructor(slotBase)->input());
207 }
208
209 EncodedJSValue regExpConstructorMultiline(ExecState*, JSObject* slotBase, EncodedJSValue, PropertyName)
210 {
211     return JSValue::encode(jsBoolean(asRegExpConstructor(slotBase)->multiline()));
212 }
213
214 EncodedJSValue regExpConstructorLastMatch(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
215 {
216     return JSValue::encode(asRegExpConstructor(slotBase)->getBackref(exec, 0));
217 }
218
219 EncodedJSValue regExpConstructorLastParen(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
220 {
221     return JSValue::encode(asRegExpConstructor(slotBase)->getLastParen(exec));
222 }
223
224 EncodedJSValue regExpConstructorLeftContext(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
225 {
226     return JSValue::encode(asRegExpConstructor(slotBase)->getLeftContext(exec));
227 }
228
229 EncodedJSValue regExpConstructorRightContext(ExecState* exec, JSObject* slotBase, EncodedJSValue, PropertyName)
230 {
231     return JSValue::encode(asRegExpConstructor(slotBase)->getRightContext(exec));
232 }
233
234 void setRegExpConstructorInput(ExecState* exec, JSObject* baseObject, EncodedJSValue, EncodedJSValue value)
235 {
236     if (auto constructor = jsDynamicCast<RegExpConstructor*>(baseObject))
237         constructor->setInput(exec, JSValue::decode(value).toString(exec));
238 }
239
240 void setRegExpConstructorMultiline(ExecState* exec, JSObject* baseObject, EncodedJSValue, EncodedJSValue value)
241 {
242     if (auto constructor = jsDynamicCast<RegExpConstructor*>(baseObject))
243         constructor->setMultiline(JSValue::decode(value).toBoolean(exec));
244 }
245
246 inline Structure* getRegExpStructure(ExecState* exec, JSGlobalObject* globalObject, JSValue newTarget)
247 {
248     Structure* structure = globalObject->regExpStructure();
249     if (newTarget != jsUndefined()) {
250         structure = InternalFunction::createSubclassStructure(exec, newTarget, structure);
251     }
252     return structure;
253 }
254
255 // ECMA 15.10.4
256 JSObject* constructRegExp(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, JSValue newTarget)
257 {
258     JSValue arg0 = args.at(0);
259     JSValue arg1 = args.at(1);
260
261     if (arg0.inherits(RegExpObject::info())) {
262         if (!arg1.isUndefined())
263             return exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Cannot supply flags when constructing one RegExp from another.")));
264         // If called as a function, this just returns the first argument (see 15.10.3.1).
265         if (newTarget != jsUndefined()) {
266             RegExp* regExp = static_cast<RegExpObject*>(asObject(arg0))->regExp();
267
268             return RegExpObject::create(exec->vm(), getRegExpStructure(exec, globalObject, newTarget), regExp);
269         }
270         return asObject(arg0);
271     }
272
273     String pattern = arg0.isUndefined() ? emptyString() : arg0.toString(exec)->value(exec);
274     if (exec->hadException())
275         return 0;
276
277     RegExpFlags flags = NoFlags;
278     if (!arg1.isUndefined()) {
279         flags = regExpFlags(arg1.toString(exec)->value(exec));
280         if (exec->hadException())
281             return 0;
282         if (flags == InvalidFlags)
283             return exec->vm().throwException(exec, createSyntaxError(exec, ASCIILiteral("Invalid flags supplied to RegExp constructor.")));
284     }
285
286     VM& vm = exec->vm();
287     RegExp* regExp = RegExp::create(vm, pattern, flags);
288     if (!regExp->isValid())
289         return vm.throwException(exec, createSyntaxError(exec, regExp->errorMessage()));
290
291     return RegExpObject::create(vm, getRegExpStructure(exec, globalObject, newTarget), regExp);
292 }
293
294 static EncodedJSValue JSC_HOST_CALL constructWithRegExpConstructor(ExecState* exec)
295 {
296     ArgList args(exec);
297     return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args, exec->newTarget()));
298 }
299
300 ConstructType RegExpConstructor::getConstructData(JSCell*, ConstructData& constructData)
301 {
302     constructData.native.function = constructWithRegExpConstructor;
303     return ConstructTypeHost;
304 }
305
306 // ECMA 15.10.3
307 static EncodedJSValue JSC_HOST_CALL callRegExpConstructor(ExecState* exec)
308 {
309     ArgList args(exec);
310     return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args));
311 }
312
313 CallType RegExpConstructor::getCallData(JSCell*, CallData& callData)
314 {
315     callData.native.function = callRegExpConstructor;
316     return CallTypeHost;
317 }
318
319 } // namespace JSC