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