[ES6] Implement Reflect.set without receiver support
[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*, EncodedJSValue, PropertyName);
35 static EncodedJSValue regExpConstructorMultiline(ExecState*, EncodedJSValue, PropertyName);
36 static EncodedJSValue regExpConstructorLastMatch(ExecState*, EncodedJSValue, PropertyName);
37 static EncodedJSValue regExpConstructorLastParen(ExecState*, EncodedJSValue, PropertyName);
38 static EncodedJSValue regExpConstructorLeftContext(ExecState*, EncodedJSValue, PropertyName);
39 static EncodedJSValue regExpConstructorRightContext(ExecState*, EncodedJSValue, PropertyName);
40 static EncodedJSValue regExpConstructorDollar1(ExecState*, EncodedJSValue, PropertyName);
41 static EncodedJSValue regExpConstructorDollar2(ExecState*, EncodedJSValue, PropertyName);
42 static EncodedJSValue regExpConstructorDollar3(ExecState*, EncodedJSValue, PropertyName);
43 static EncodedJSValue regExpConstructorDollar4(ExecState*, EncodedJSValue, PropertyName);
44 static EncodedJSValue regExpConstructorDollar5(ExecState*, EncodedJSValue, PropertyName);
45 static EncodedJSValue regExpConstructorDollar6(ExecState*, EncodedJSValue, PropertyName);
46 static EncodedJSValue regExpConstructorDollar7(ExecState*, EncodedJSValue, PropertyName);
47 static EncodedJSValue regExpConstructorDollar8(ExecState*, EncodedJSValue, PropertyName);
48 static EncodedJSValue regExpConstructorDollar9(ExecState*, EncodedJSValue, PropertyName);
49
50 static bool setRegExpConstructorInput(ExecState*, EncodedJSValue, EncodedJSValue);
51 static bool setRegExpConstructorMultiline(ExecState*, 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);
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, EncodedJSValue thisValue, PropertyName)
163 {
164     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 1));
165 }
166
167 EncodedJSValue regExpConstructorDollar2(ExecState* exec, EncodedJSValue thisValue, PropertyName)
168 {
169     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 2));
170 }
171
172 EncodedJSValue regExpConstructorDollar3(ExecState* exec, EncodedJSValue thisValue, PropertyName)
173 {
174     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 3));
175 }
176
177 EncodedJSValue regExpConstructorDollar4(ExecState* exec, EncodedJSValue thisValue, PropertyName)
178 {
179     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 4));
180 }
181
182 EncodedJSValue regExpConstructorDollar5(ExecState* exec, EncodedJSValue thisValue, PropertyName)
183 {
184     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 5));
185 }
186
187 EncodedJSValue regExpConstructorDollar6(ExecState* exec, EncodedJSValue thisValue, PropertyName)
188 {
189     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 6));
190 }
191
192 EncodedJSValue regExpConstructorDollar7(ExecState* exec, EncodedJSValue thisValue, PropertyName)
193 {
194     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 7));
195 }
196
197 EncodedJSValue regExpConstructorDollar8(ExecState* exec, EncodedJSValue thisValue, PropertyName)
198 {
199     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 8));
200 }
201
202 EncodedJSValue regExpConstructorDollar9(ExecState* exec, EncodedJSValue thisValue, PropertyName)
203 {
204     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 9));
205 }
206
207 EncodedJSValue regExpConstructorInput(ExecState*, EncodedJSValue thisValue, PropertyName)
208 {
209     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->input());
210 }
211
212 EncodedJSValue regExpConstructorMultiline(ExecState*, EncodedJSValue thisValue, PropertyName)
213 {
214     return JSValue::encode(jsBoolean(asRegExpConstructor(JSValue::decode(thisValue))->multiline()));
215 }
216
217 EncodedJSValue regExpConstructorLastMatch(ExecState* exec, EncodedJSValue thisValue, PropertyName)
218 {
219     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getBackref(exec, 0));
220 }
221
222 EncodedJSValue regExpConstructorLastParen(ExecState* exec, EncodedJSValue thisValue, PropertyName)
223 {
224     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getLastParen(exec));
225 }
226
227 EncodedJSValue regExpConstructorLeftContext(ExecState* exec, EncodedJSValue thisValue, PropertyName)
228 {
229     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getLeftContext(exec));
230 }
231
232 EncodedJSValue regExpConstructorRightContext(ExecState* exec, EncodedJSValue thisValue, PropertyName)
233 {
234     return JSValue::encode(asRegExpConstructor(JSValue::decode(thisValue))->getRightContext(exec));
235 }
236
237 bool setRegExpConstructorInput(ExecState* exec, EncodedJSValue thisValue, EncodedJSValue value)
238 {
239     if (auto constructor = jsDynamicCast<RegExpConstructor*>(JSValue::decode(thisValue))) {
240         constructor->setInput(exec, JSValue::decode(value).toString(exec));
241         return true;
242     }
243     return false;
244 }
245
246 bool setRegExpConstructorMultiline(ExecState* exec, EncodedJSValue thisValue, EncodedJSValue value)
247 {
248     if (auto constructor = jsDynamicCast<RegExpConstructor*>(JSValue::decode(thisValue))) {
249         constructor->setMultiline(JSValue::decode(value).toBoolean(exec));
250         return true;
251     }
252     return false;
253 }
254
255 inline Structure* getRegExpStructure(ExecState* exec, JSGlobalObject* globalObject, JSValue newTarget)
256 {
257     Structure* structure = globalObject->regExpStructure();
258     if (newTarget != jsUndefined())
259         structure = InternalFunction::createSubclassStructure(exec, newTarget, structure);
260     return structure;
261 }
262
263 // ECMA 15.10.4
264 JSObject* constructRegExp(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args, JSValue newTarget)
265 {
266     JSValue arg0 = args.at(0);
267     JSValue arg1 = args.at(1);
268
269     VM& vm = exec->vm();
270     RegExpFlags flags = NoFlags;
271     bool haveFlags = false;
272     if (!arg1.isUndefined()) {
273         flags = regExpFlags(arg1.toString(exec)->value(exec));
274         if (vm.exception())
275             return 0;
276         if (flags == InvalidFlags)
277             return vm.throwException(exec, createSyntaxError(exec, ASCIILiteral("Invalid flags supplied to RegExp constructor.")));
278         haveFlags = true;
279     }
280
281     if (arg0.inherits(RegExpObject::info())) {
282         // If called as a function, this just returns the first argument (see 15.10.3.1).
283         if (newTarget != jsUndefined()) {
284             RegExp* regExp = static_cast<RegExpObject*>(asObject(arg0))->regExp();
285             Structure* structure = getRegExpStructure(exec, globalObject, newTarget);
286             if (vm.exception())
287                 return nullptr;
288
289             if (haveFlags) {
290                 regExp = RegExp::create(vm, regExp->pattern(), flags);
291                 if (vm.exception())
292                     return nullptr;
293             }
294
295             return RegExpObject::create(vm, structure, regExp);
296         }
297         return asObject(arg0);
298     }
299
300     String pattern = arg0.isUndefined() ? emptyString() : arg0.toString(exec)->value(exec);
301     if (vm.exception())
302         return 0;
303
304     RegExp* regExp = RegExp::create(vm, pattern, flags);
305     if (!regExp->isValid())
306         return vm.throwException(exec, createSyntaxError(exec, regExp->errorMessage()));
307
308     Structure* structure = getRegExpStructure(exec, globalObject, newTarget);
309     if (vm.exception())
310         return nullptr;
311     return RegExpObject::create(vm, structure, regExp);
312 }
313
314 static EncodedJSValue JSC_HOST_CALL constructWithRegExpConstructor(ExecState* exec)
315 {
316     ArgList args(exec);
317     return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args, exec->newTarget()));
318 }
319
320 ConstructType RegExpConstructor::getConstructData(JSCell*, ConstructData& constructData)
321 {
322     constructData.native.function = constructWithRegExpConstructor;
323     return ConstructType::Host;
324 }
325
326 // ECMA 15.10.3
327 static EncodedJSValue JSC_HOST_CALL callRegExpConstructor(ExecState* exec)
328 {
329     ArgList args(exec);
330     return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args));
331 }
332
333 CallType RegExpConstructor::getCallData(JSCell*, CallData& callData)
334 {
335     callData.native.function = callRegExpConstructor;
336     return CallType::Host;
337 }
338
339 } // namespace JSC