1 | /*
|
---|
2 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved.
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "config.h"
|
---|
22 | #include "RegExpObject.h"
|
---|
23 | #include "RegExpObject.lut.h"
|
---|
24 |
|
---|
25 | #include "JSArray.h"
|
---|
26 | #include "ArrayPrototype.h"
|
---|
27 | #include "error_object.h"
|
---|
28 | #include "JSString.h"
|
---|
29 | #include "JSObject.h"
|
---|
30 | #include "operations.h"
|
---|
31 | #include "regexp.h"
|
---|
32 | #include "JSValue.h"
|
---|
33 | #include "UnusedParam.h"
|
---|
34 |
|
---|
35 | #include <stdio.h>
|
---|
36 |
|
---|
37 | namespace KJS {
|
---|
38 |
|
---|
39 | // ------------------------------ RegExpPrototype ---------------------------
|
---|
40 |
|
---|
41 | static JSValue* regExpProtoFuncTest(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
42 | static JSValue* regExpProtoFuncExec(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
43 | static JSValue* regExpProtoFuncCompile(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
44 | static JSValue* regExpProtoFuncToString(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
45 |
|
---|
46 | // ECMA 15.10.5
|
---|
47 |
|
---|
48 | const ClassInfo RegExpPrototype::info = { "RegExpPrototype", 0, 0, 0 };
|
---|
49 |
|
---|
50 | RegExpPrototype::RegExpPrototype(ExecState* exec, ObjectPrototype* objectPrototype, FunctionPrototype* functionPrototype)
|
---|
51 | : JSObject(objectPrototype)
|
---|
52 | {
|
---|
53 | putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().compile, regExpProtoFuncCompile), DontEnum);
|
---|
54 | putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().exec, regExpProtoFuncExec), DontEnum);
|
---|
55 | putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().test, regExpProtoFuncTest), DontEnum);
|
---|
56 | putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().toString, regExpProtoFuncToString), DontEnum);
|
---|
57 | }
|
---|
58 |
|
---|
59 | // ------------------------------ Functions ---------------------------
|
---|
60 |
|
---|
61 | JSValue* regExpProtoFuncTest(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
|
---|
62 | {
|
---|
63 | if (!thisValue->isObject(&RegExpObject::info))
|
---|
64 | return throwError(exec, TypeError);
|
---|
65 | return static_cast<RegExpObject*>(thisValue)->test(exec, args);
|
---|
66 | }
|
---|
67 |
|
---|
68 | JSValue* regExpProtoFuncExec(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
|
---|
69 | {
|
---|
70 | if (!thisValue->isObject(&RegExpObject::info))
|
---|
71 | return throwError(exec, TypeError);
|
---|
72 | return static_cast<RegExpObject*>(thisValue)->exec(exec, args);
|
---|
73 | }
|
---|
74 |
|
---|
75 | JSValue* regExpProtoFuncCompile(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
|
---|
76 | {
|
---|
77 | if (!thisValue->isObject(&RegExpObject::info))
|
---|
78 | return throwError(exec, TypeError);
|
---|
79 |
|
---|
80 | RefPtr<RegExp> regExp;
|
---|
81 | JSValue* arg0 = args[0];
|
---|
82 | JSValue* arg1 = args[1];
|
---|
83 |
|
---|
84 | if (arg0->isObject(&RegExpObject::info)) {
|
---|
85 | if (!arg1->isUndefined())
|
---|
86 | return throwError(exec, TypeError, "Cannot supply flags when constructing one RegExp from another.");
|
---|
87 | regExp = static_cast<RegExpObject*>(arg0)->regExp();
|
---|
88 | } else {
|
---|
89 | UString pattern = args.isEmpty() ? UString("") : arg0->toString(exec);
|
---|
90 | UString flags = arg1->isUndefined() ? UString("") : arg1->toString(exec);
|
---|
91 | regExp = RegExp::create(pattern, flags);
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (!regExp->isValid())
|
---|
95 | return throwError(exec, SyntaxError, UString("Invalid regular expression: ").append(regExp->errorMessage()));
|
---|
96 |
|
---|
97 | static_cast<RegExpObject*>(thisValue)->setRegExp(regExp.release());
|
---|
98 | static_cast<RegExpObject*>(thisValue)->setLastIndex(0);
|
---|
99 | return jsUndefined();
|
---|
100 | }
|
---|
101 |
|
---|
102 | JSValue* regExpProtoFuncToString(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList&)
|
---|
103 | {
|
---|
104 | if (!thisValue->isObject(&RegExpObject::info)) {
|
---|
105 | if (thisValue->isObject(&RegExpPrototype::info))
|
---|
106 | return jsString(exec, "//");
|
---|
107 | return throwError(exec, TypeError);
|
---|
108 | }
|
---|
109 |
|
---|
110 | UString result = "/" + static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().source)->toString(exec) + "/";
|
---|
111 | if (static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().global)->toBoolean(exec))
|
---|
112 | result += "g";
|
---|
113 | if (static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().ignoreCase)->toBoolean(exec))
|
---|
114 | result += "i";
|
---|
115 | if (static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().multiline)->toBoolean(exec))
|
---|
116 | result += "m";
|
---|
117 | return jsString(exec, result);
|
---|
118 | }
|
---|
119 |
|
---|
120 | // ------------------------------ RegExpObject ------------------------------------
|
---|
121 |
|
---|
122 | const ClassInfo RegExpObject::info = { "RegExp", 0, 0, ExecState::regExpTable };
|
---|
123 |
|
---|
124 | /* Source for RegExpObject.lut.h
|
---|
125 | @begin regExpTable
|
---|
126 | global RegExpObject::Global DontDelete|ReadOnly|DontEnum
|
---|
127 | ignoreCase RegExpObject::IgnoreCase DontDelete|ReadOnly|DontEnum
|
---|
128 | multiline RegExpObject::Multiline DontDelete|ReadOnly|DontEnum
|
---|
129 | source RegExpObject::Source DontDelete|ReadOnly|DontEnum
|
---|
130 | lastIndex RegExpObject::LastIndex DontDelete|DontEnum
|
---|
131 | @end
|
---|
132 | */
|
---|
133 |
|
---|
134 | RegExpObject::RegExpObject(RegExpPrototype* regexpProto, PassRefPtr<RegExp> regExp)
|
---|
135 | : JSObject(regexpProto)
|
---|
136 | , m_regExp(regExp)
|
---|
137 | , m_lastIndex(0)
|
---|
138 | {
|
---|
139 | }
|
---|
140 |
|
---|
141 | RegExpObject::~RegExpObject()
|
---|
142 | {
|
---|
143 | }
|
---|
144 |
|
---|
145 | bool RegExpObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
146 | {
|
---|
147 | return getStaticValueSlot<RegExpObject, JSObject>(exec, ExecState::regExpTable(exec), this, propertyName, slot);
|
---|
148 | }
|
---|
149 |
|
---|
150 | JSValue* RegExpObject::getValueProperty(ExecState* exec, int token) const
|
---|
151 | {
|
---|
152 | switch (token) {
|
---|
153 | case Global:
|
---|
154 | return jsBoolean(m_regExp->global());
|
---|
155 | case IgnoreCase:
|
---|
156 | return jsBoolean(m_regExp->ignoreCase());
|
---|
157 | case Multiline:
|
---|
158 | return jsBoolean(m_regExp->multiline());
|
---|
159 | case Source:
|
---|
160 | return jsString(exec, m_regExp->pattern());
|
---|
161 | case LastIndex:
|
---|
162 | return jsNumber(exec, m_lastIndex);
|
---|
163 | }
|
---|
164 |
|
---|
165 | ASSERT_NOT_REACHED();
|
---|
166 | return 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 | void RegExpObject::put(ExecState* exec, const Identifier& propertyName, JSValue* value)
|
---|
170 | {
|
---|
171 | lookupPut<RegExpObject, JSObject>(exec, propertyName, value, ExecState::regExpTable(exec), this);
|
---|
172 | }
|
---|
173 |
|
---|
174 | void RegExpObject::putValueProperty(ExecState* exec, int token, JSValue* value)
|
---|
175 | {
|
---|
176 | UNUSED_PARAM(token);
|
---|
177 | ASSERT(token == LastIndex);
|
---|
178 | m_lastIndex = value->toInteger(exec);
|
---|
179 | }
|
---|
180 |
|
---|
181 | bool RegExpObject::match(ExecState* exec, const ArgList& args)
|
---|
182 | {
|
---|
183 | RegExpConstructor* regExpObj = exec->lexicalGlobalObject()->regExpConstructor();
|
---|
184 |
|
---|
185 | UString input;
|
---|
186 | if (!args.isEmpty())
|
---|
187 | input = args[0]->toString(exec);
|
---|
188 | else {
|
---|
189 | input = regExpObj->input();
|
---|
190 | if (input.isNull()) {
|
---|
191 | throwError(exec, GeneralError, "No input.");
|
---|
192 | return false;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | bool global = get(exec, exec->propertyNames().global)->toBoolean(exec);
|
---|
197 | int lastIndex = 0;
|
---|
198 | if (global) {
|
---|
199 | if (m_lastIndex < 0 || m_lastIndex > input.size()) {
|
---|
200 | m_lastIndex = 0;
|
---|
201 | return false;
|
---|
202 | }
|
---|
203 | lastIndex = static_cast<int>(m_lastIndex);
|
---|
204 | }
|
---|
205 |
|
---|
206 | int foundIndex;
|
---|
207 | int foundLength;
|
---|
208 | regExpObj->performMatch(m_regExp.get(), input, lastIndex, foundIndex, foundLength);
|
---|
209 |
|
---|
210 | if (global) {
|
---|
211 | lastIndex = foundIndex < 0 ? 0 : foundIndex + foundLength;
|
---|
212 | m_lastIndex = lastIndex;
|
---|
213 | }
|
---|
214 |
|
---|
215 | return foundIndex >= 0;
|
---|
216 | }
|
---|
217 |
|
---|
218 | JSValue* RegExpObject::test(ExecState* exec, const ArgList& args)
|
---|
219 | {
|
---|
220 | return jsBoolean(match(exec, args));
|
---|
221 | }
|
---|
222 |
|
---|
223 | JSValue* RegExpObject::exec(ExecState* exec, const ArgList& args)
|
---|
224 | {
|
---|
225 | return match(exec, args)
|
---|
226 | ? exec->lexicalGlobalObject()->regExpConstructor()->arrayOfMatches(exec)
|
---|
227 | : jsNull();
|
---|
228 | }
|
---|
229 |
|
---|
230 | static JSValue* callRegExpObject(ExecState* exec, JSObject* function, JSValue*, const ArgList& args)
|
---|
231 | {
|
---|
232 | return static_cast<RegExpObject*>(function)->exec(exec, args);
|
---|
233 | }
|
---|
234 |
|
---|
235 | CallType RegExpObject::getCallData(CallData& callData)
|
---|
236 | {
|
---|
237 | callData.native.function = callRegExpObject;
|
---|
238 | return CallTypeNative;
|
---|
239 | }
|
---|
240 |
|
---|
241 | // ------------------------------ RegExpConstructor ------------------------------
|
---|
242 |
|
---|
243 | const ClassInfo RegExpConstructor::info = { "Function", &InternalFunction::info, 0, ExecState::regExpConstructorTable };
|
---|
244 |
|
---|
245 | /* Source for RegExpObject.lut.h
|
---|
246 | @begin regExpConstructorTable
|
---|
247 | input RegExpConstructor::Input None
|
---|
248 | $_ RegExpConstructor::Input DontEnum
|
---|
249 | multiline RegExpConstructor::Multiline None
|
---|
250 | $* RegExpConstructor::Multiline DontEnum
|
---|
251 | lastMatch RegExpConstructor::LastMatch DontDelete|ReadOnly
|
---|
252 | $& RegExpConstructor::LastMatch DontDelete|ReadOnly|DontEnum
|
---|
253 | lastParen RegExpConstructor::LastParen DontDelete|ReadOnly
|
---|
254 | $+ RegExpConstructor::LastParen DontDelete|ReadOnly|DontEnum
|
---|
255 | leftContext RegExpConstructor::LeftContext DontDelete|ReadOnly
|
---|
256 | $` RegExpConstructor::LeftContext DontDelete|ReadOnly|DontEnum
|
---|
257 | rightContext RegExpConstructor::RightContext DontDelete|ReadOnly
|
---|
258 | $' RegExpConstructor::RightContext DontDelete|ReadOnly|DontEnum
|
---|
259 | $1 RegExpConstructor::Dollar1 DontDelete|ReadOnly
|
---|
260 | $2 RegExpConstructor::Dollar2 DontDelete|ReadOnly
|
---|
261 | $3 RegExpConstructor::Dollar3 DontDelete|ReadOnly
|
---|
262 | $4 RegExpConstructor::Dollar4 DontDelete|ReadOnly
|
---|
263 | $5 RegExpConstructor::Dollar5 DontDelete|ReadOnly
|
---|
264 | $6 RegExpConstructor::Dollar6 DontDelete|ReadOnly
|
---|
265 | $7 RegExpConstructor::Dollar7 DontDelete|ReadOnly
|
---|
266 | $8 RegExpConstructor::Dollar8 DontDelete|ReadOnly
|
---|
267 | $9 RegExpConstructor::Dollar9 DontDelete|ReadOnly
|
---|
268 | @end
|
---|
269 | */
|
---|
270 |
|
---|
271 | struct RegExpConstructorPrivate {
|
---|
272 | // Global search cache / settings
|
---|
273 | RegExpConstructorPrivate() : lastNumSubPatterns(0), multiline(false) { }
|
---|
274 | UString lastInput;
|
---|
275 | OwnArrayPtr<int> lastOvector;
|
---|
276 | unsigned lastNumSubPatterns : 31;
|
---|
277 | bool multiline : 1;
|
---|
278 | };
|
---|
279 |
|
---|
280 | RegExpConstructor::RegExpConstructor(ExecState* exec, FunctionPrototype* funcProto, RegExpPrototype* regProto)
|
---|
281 | : InternalFunction(funcProto, Identifier(exec, "RegExp"))
|
---|
282 | , d(new RegExpConstructorPrivate)
|
---|
283 | {
|
---|
284 | // ECMA 15.10.5.1 RegExp.prototype
|
---|
285 | putDirect(exec->propertyNames().prototype, regProto, DontEnum | DontDelete | ReadOnly);
|
---|
286 |
|
---|
287 | // no. of arguments for constructor
|
---|
288 | putDirect(exec->propertyNames().length, jsNumber(exec, 2), ReadOnly | DontDelete | DontEnum);
|
---|
289 | }
|
---|
290 |
|
---|
291 | /*
|
---|
292 | To facilitate result caching, exec(), test(), match(), search(), and replace() dipatch regular
|
---|
293 | expression matching through the performMatch function. We use cached results to calculate,
|
---|
294 | e.g., RegExp.lastMatch and RegExp.leftParen.
|
---|
295 | */
|
---|
296 | void RegExpConstructor::performMatch(RegExp* r, const UString& s, int startOffset, int& position, int& length, int** ovector)
|
---|
297 | {
|
---|
298 | OwnArrayPtr<int> tmpOvector;
|
---|
299 | position = r->match(s, startOffset, &tmpOvector);
|
---|
300 |
|
---|
301 | if (ovector)
|
---|
302 | *ovector = tmpOvector.get();
|
---|
303 |
|
---|
304 | if (position != -1) {
|
---|
305 | ASSERT(tmpOvector);
|
---|
306 |
|
---|
307 | length = tmpOvector[1] - tmpOvector[0];
|
---|
308 |
|
---|
309 | d->lastInput = s;
|
---|
310 | d->lastOvector.set(tmpOvector.release());
|
---|
311 | d->lastNumSubPatterns = r->numSubpatterns();
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | class RegExpMatchesArray : public JSArray {
|
---|
316 | public:
|
---|
317 | RegExpMatchesArray(ExecState*, RegExpConstructorPrivate*);
|
---|
318 | virtual ~RegExpMatchesArray();
|
---|
319 |
|
---|
320 | private:
|
---|
321 | virtual bool getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) { if (lazyCreationData()) fillArrayInstance(exec); return JSArray::getOwnPropertySlot(exec, propertyName, slot); }
|
---|
322 | virtual bool getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot) { if (lazyCreationData()) fillArrayInstance(exec); return JSArray::getOwnPropertySlot(exec, propertyName, slot); }
|
---|
323 | virtual void put(ExecState* exec, const Identifier& propertyName, JSValue* v) { if (lazyCreationData()) fillArrayInstance(exec); JSArray::put(exec, propertyName, v); }
|
---|
324 | virtual void put(ExecState* exec, unsigned propertyName, JSValue* v) { if (lazyCreationData()) fillArrayInstance(exec); JSArray::put(exec, propertyName, v); }
|
---|
325 | virtual bool deleteProperty(ExecState* exec, const Identifier& propertyName) { if (lazyCreationData()) fillArrayInstance(exec); return JSArray::deleteProperty(exec, propertyName); }
|
---|
326 | virtual bool deleteProperty(ExecState* exec, unsigned propertyName) { if (lazyCreationData()) fillArrayInstance(exec); return JSArray::deleteProperty(exec, propertyName); }
|
---|
327 | virtual void getPropertyNames(ExecState* exec, PropertyNameArray& arr) { if (lazyCreationData()) fillArrayInstance(exec); JSArray::getPropertyNames(exec, arr); }
|
---|
328 |
|
---|
329 | void fillArrayInstance(ExecState*);
|
---|
330 | };
|
---|
331 |
|
---|
332 | RegExpMatchesArray::RegExpMatchesArray(ExecState* exec, RegExpConstructorPrivate* data)
|
---|
333 | : JSArray(exec->lexicalGlobalObject()->arrayPrototype(), data->lastNumSubPatterns + 1)
|
---|
334 | {
|
---|
335 | RegExpConstructorPrivate* d = new RegExpConstructorPrivate;
|
---|
336 | d->lastInput = data->lastInput;
|
---|
337 | d->lastNumSubPatterns = data->lastNumSubPatterns;
|
---|
338 | unsigned offsetVectorSize = (data->lastNumSubPatterns + 1) * 2; // only copying the result part of the vector
|
---|
339 | d->lastOvector.set(new int[offsetVectorSize]);
|
---|
340 | memcpy(d->lastOvector.get(), data->lastOvector.get(), offsetVectorSize * sizeof(int));
|
---|
341 | // d->multiline is not needed, and remains uninitialized
|
---|
342 |
|
---|
343 | setLazyCreationData(d);
|
---|
344 | }
|
---|
345 |
|
---|
346 | RegExpMatchesArray::~RegExpMatchesArray()
|
---|
347 | {
|
---|
348 | delete static_cast<RegExpConstructorPrivate*>(lazyCreationData());
|
---|
349 | }
|
---|
350 |
|
---|
351 | void RegExpMatchesArray::fillArrayInstance(ExecState* exec)
|
---|
352 | {
|
---|
353 | RegExpConstructorPrivate* d = static_cast<RegExpConstructorPrivate*>(lazyCreationData());
|
---|
354 | ASSERT(d);
|
---|
355 |
|
---|
356 | unsigned lastNumSubpatterns = d->lastNumSubPatterns;
|
---|
357 |
|
---|
358 | for (unsigned i = 0; i <= lastNumSubpatterns; ++i) {
|
---|
359 | int start = d->lastOvector[2 * i];
|
---|
360 | if (start >= 0)
|
---|
361 | JSArray::put(exec, i, jsString(exec, d->lastInput.substr(start, d->lastOvector[2 * i + 1] - start)));
|
---|
362 | }
|
---|
363 | JSArray::put(exec, exec->propertyNames().index, jsNumber(exec, d->lastOvector[0]));
|
---|
364 | JSArray::put(exec, exec->propertyNames().input, jsString(exec, d->lastInput));
|
---|
365 |
|
---|
366 | delete d;
|
---|
367 | setLazyCreationData(0);
|
---|
368 | }
|
---|
369 |
|
---|
370 | JSObject* RegExpConstructor::arrayOfMatches(ExecState* exec) const
|
---|
371 | {
|
---|
372 | return new (exec) RegExpMatchesArray(exec, d.get());
|
---|
373 | }
|
---|
374 |
|
---|
375 | JSValue* RegExpConstructor::getBackref(ExecState* exec, unsigned i) const
|
---|
376 | {
|
---|
377 | if (d->lastOvector && i <= d->lastNumSubPatterns)
|
---|
378 | return jsString(exec, d->lastInput.substr(d->lastOvector[2 * i], d->lastOvector[2 * i + 1] - d->lastOvector[2 * i]));
|
---|
379 | return jsString(exec, "");
|
---|
380 | }
|
---|
381 |
|
---|
382 | JSValue* RegExpConstructor::getLastParen(ExecState* exec) const
|
---|
383 | {
|
---|
384 | unsigned i = d->lastNumSubPatterns;
|
---|
385 | if (i > 0) {
|
---|
386 | ASSERT(d->lastOvector);
|
---|
387 | return jsString(exec, d->lastInput.substr(d->lastOvector[2 * i], d->lastOvector[2 * i + 1] - d->lastOvector[2 * i]));
|
---|
388 | }
|
---|
389 | return jsString(exec, "");
|
---|
390 | }
|
---|
391 |
|
---|
392 | JSValue* RegExpConstructor::getLeftContext(ExecState* exec) const
|
---|
393 | {
|
---|
394 | if (d->lastOvector)
|
---|
395 | return jsString(exec, d->lastInput.substr(0, d->lastOvector[0]));
|
---|
396 | return jsString(exec, "");
|
---|
397 | }
|
---|
398 |
|
---|
399 | JSValue* RegExpConstructor::getRightContext(ExecState* exec) const
|
---|
400 | {
|
---|
401 | if (d->lastOvector) {
|
---|
402 | UString s = d->lastInput;
|
---|
403 | return jsString(exec, s.substr(d->lastOvector[1], s.size() - d->lastOvector[1]));
|
---|
404 | }
|
---|
405 | return jsString(exec, "");
|
---|
406 | }
|
---|
407 |
|
---|
408 | bool RegExpConstructor::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
409 | {
|
---|
410 | return getStaticValueSlot<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), this, propertyName, slot);
|
---|
411 | }
|
---|
412 |
|
---|
413 | JSValue *RegExpConstructor::getValueProperty(ExecState* exec, int token) const
|
---|
414 | {
|
---|
415 | switch (token) {
|
---|
416 | case Dollar1:
|
---|
417 | return getBackref(exec, 1);
|
---|
418 | case Dollar2:
|
---|
419 | return getBackref(exec, 2);
|
---|
420 | case Dollar3:
|
---|
421 | return getBackref(exec, 3);
|
---|
422 | case Dollar4:
|
---|
423 | return getBackref(exec, 4);
|
---|
424 | case Dollar5:
|
---|
425 | return getBackref(exec, 5);
|
---|
426 | case Dollar6:
|
---|
427 | return getBackref(exec, 6);
|
---|
428 | case Dollar7:
|
---|
429 | return getBackref(exec, 7);
|
---|
430 | case Dollar8:
|
---|
431 | return getBackref(exec, 8);
|
---|
432 | case Dollar9:
|
---|
433 | return getBackref(exec, 9);
|
---|
434 | case Input:
|
---|
435 | return jsString(exec, d->lastInput);
|
---|
436 | case Multiline:
|
---|
437 | return jsBoolean(d->multiline);
|
---|
438 | case LastMatch:
|
---|
439 | return getBackref(exec, 0);
|
---|
440 | case LastParen:
|
---|
441 | return getLastParen(exec);
|
---|
442 | case LeftContext:
|
---|
443 | return getLeftContext(exec);
|
---|
444 | case RightContext:
|
---|
445 | return getRightContext(exec);
|
---|
446 | default:
|
---|
447 | ASSERT_NOT_REACHED();
|
---|
448 | }
|
---|
449 |
|
---|
450 | return jsString(exec, "");
|
---|
451 | }
|
---|
452 |
|
---|
453 | void RegExpConstructor::put(ExecState *exec, const Identifier &propertyName, JSValue *value)
|
---|
454 | {
|
---|
455 | lookupPut<RegExpConstructor, InternalFunction>(exec, propertyName, value, ExecState::regExpConstructorTable(exec), this);
|
---|
456 | }
|
---|
457 |
|
---|
458 | void RegExpConstructor::putValueProperty(ExecState *exec, int token, JSValue *value)
|
---|
459 | {
|
---|
460 | switch (token) {
|
---|
461 | case Input:
|
---|
462 | d->lastInput = value->toString(exec);
|
---|
463 | break;
|
---|
464 | case Multiline:
|
---|
465 | d->multiline = value->toBoolean(exec);
|
---|
466 | break;
|
---|
467 | default:
|
---|
468 | ASSERT(0);
|
---|
469 | }
|
---|
470 | }
|
---|
471 |
|
---|
472 | // ECMA 15.10.4
|
---|
473 | static JSObject* constructRegExp(ExecState* exec, const ArgList& args)
|
---|
474 | {
|
---|
475 | JSValue* arg0 = args[0];
|
---|
476 | JSValue* arg1 = args[1];
|
---|
477 |
|
---|
478 | if (arg0->isObject(&RegExpObject::info)) {
|
---|
479 | if (!arg1->isUndefined())
|
---|
480 | return throwError(exec, TypeError, "Cannot supply flags when constructing one RegExp from another.");
|
---|
481 | return static_cast<JSObject*>(arg0);
|
---|
482 | }
|
---|
483 |
|
---|
484 | UString pattern = arg0->isUndefined() ? UString("") : arg0->toString(exec);
|
---|
485 | UString flags = arg1->isUndefined() ? UString("") : arg1->toString(exec);
|
---|
486 |
|
---|
487 | RefPtr<RegExp> regExp = RegExp::create(pattern, flags);
|
---|
488 | return regExp->isValid()
|
---|
489 | ? new (exec) RegExpObject(exec->lexicalGlobalObject()->regExpPrototype(), regExp.release())
|
---|
490 | : throwError(exec, SyntaxError, UString("Invalid regular expression: ").append(regExp->errorMessage()));
|
---|
491 | }
|
---|
492 |
|
---|
493 | static JSObject* constructWithRegExpConstructor(ExecState* exec, JSObject*, const ArgList& args)
|
---|
494 | {
|
---|
495 | return constructRegExp(exec, args);
|
---|
496 | }
|
---|
497 |
|
---|
498 | ConstructType RegExpConstructor::getConstructData(ConstructData& constructData)
|
---|
499 | {
|
---|
500 | constructData.native.function = constructWithRegExpConstructor;
|
---|
501 | return ConstructTypeNative;
|
---|
502 | }
|
---|
503 |
|
---|
504 | // ECMA 15.10.3
|
---|
505 | static JSValue* callRegExpConstructor(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
506 | {
|
---|
507 | return constructRegExp(exec, args);
|
---|
508 | }
|
---|
509 |
|
---|
510 | CallType RegExpConstructor::getCallData(CallData& callData)
|
---|
511 | {
|
---|
512 | callData.native.function = callRegExpConstructor;
|
---|
513 | return CallTypeNative;
|
---|
514 | }
|
---|
515 |
|
---|
516 | const UString& RegExpConstructor::input() const
|
---|
517 | {
|
---|
518 | // Can detect a distinct initial state that is invisible to JavaScript, by checking for null
|
---|
519 | // state (since jsString turns null strings to empty strings).
|
---|
520 | return d->lastInput;
|
---|
521 | }
|
---|
522 |
|
---|
523 | }
|
---|