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 41namespace JSC { 42 43static JSValue regExpConstructorInput(ExecState*, JSValue, const Identifier&); 44static JSValue regExpConstructorMultiline(ExecState*, JSValue, const Identifier&); 45static JSValue regExpConstructorLastMatch(ExecState*, JSValue, const Identifier&); 46static JSValue regExpConstructorLastParen(ExecState*, JSValue, const Identifier&); 47static JSValue regExpConstructorLeftContext(ExecState*, JSValue, const Identifier&); 48static JSValue regExpConstructorRightContext(ExecState*, JSValue, const Identifier&); 49static JSValue regExpConstructorDollar1(ExecState*, JSValue, const Identifier&); 50static JSValue regExpConstructorDollar2(ExecState*, JSValue, const Identifier&); 51static JSValue regExpConstructorDollar3(ExecState*, JSValue, const Identifier&); 52static JSValue regExpConstructorDollar4(ExecState*, JSValue, const Identifier&); 53static JSValue regExpConstructorDollar5(ExecState*, JSValue, const Identifier&); 54static JSValue regExpConstructorDollar6(ExecState*, JSValue, const Identifier&); 55static JSValue regExpConstructorDollar7(ExecState*, JSValue, const Identifier&); 56static JSValue regExpConstructorDollar8(ExecState*, JSValue, const Identifier&); 57static JSValue regExpConstructorDollar9(ExecState*, JSValue, const Identifier&); 58 59static void setRegExpConstructorInput(ExecState*, JSObject*, JSValue); 60static void setRegExpConstructorMultiline(ExecState*, JSObject*, JSValue); 61 62} // namespace JSC 63 64#include "RegExpConstructor.lut.h" 65 66namespace JSC { 67 68ASSERT_CLASS_FITS_IN_CELL(RegExpConstructor); 69 70const 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 98RegExpConstructor::RegExpConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, RegExpPrototype* regExpPrototype) 99 : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, "RegExp")) 100 , d(adoptPtr(new RegExpConstructorPrivate)) 101{ 102 ASSERT(inherits(&s_info)); 103 104 // ECMA 15.10.5.1 RegExp.prototype 105 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, regExpPrototype, DontEnum | DontDelete | ReadOnly); 106 107 // no. of arguments for constructor 108 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(2), ReadOnly | DontDelete | DontEnum); 109} 110 111RegExpMatchesArray::RegExpMatchesArray(ExecState* exec, RegExpConstructorPrivate* data) 112 : JSArray(exec->globalData(), exec->lexicalGlobalObject()->regExpMatchesArrayStructure(), data->lastNumSubPatterns + 1, CreateInitialized) 113{ 114 RegExpConstructorPrivate* d = new RegExpConstructorPrivate; 115 d->input = data->lastInput; 116 d->lastInput = data->lastInput; 117 d->lastNumSubPatterns = data->lastNumSubPatterns; 118 unsigned offsetVectorSize = (data->lastNumSubPatterns + 1) * 2; // only copying the result part of the vector 119 d->lastOvector().resize(offsetVectorSize); 120 memcpy(d->lastOvector().data(), data->lastOvector().data(), offsetVectorSize * sizeof(int)); 121 // d->multiline is not needed, and remains uninitialized 122 123 setSubclassData(d); 124} 125 126RegExpMatchesArray::~RegExpMatchesArray() 127{ 128 delete static_cast<RegExpConstructorPrivate*>(subclassData()); 129} 130 131void RegExpMatchesArray::fillArrayInstance(ExecState* exec) 132{ 133 RegExpConstructorPrivate* d = static_cast<RegExpConstructorPrivate*>(subclassData()); 134 ASSERT(d); 135 136 unsigned lastNumSubpatterns = d->lastNumSubPatterns; 137 138 for (unsigned i = 0; i <= lastNumSubpatterns; ++i) { 139 int start = d->lastOvector()[2 * i]; 140 if (start >= 0) 141 JSArray::put(exec, i, jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start)); 142 else 143 JSArray::put(exec, i, jsUndefined()); 144 } 145 146 PutPropertySlot slot; 147 JSArray::put(exec, exec->propertyNames().index, jsNumber(d->lastOvector()[0]), slot); 148 JSArray::put(exec, exec->propertyNames().input, jsString(exec, d->input), slot); 149 150 delete d; 151 setSubclassData(0); 152} 153 154JSObject* RegExpConstructor::arrayOfMatches(ExecState* exec) const 155{ 156 return new (exec) RegExpMatchesArray(exec, d.get()); 157} 158 159JSValue RegExpConstructor::getBackref(ExecState* exec, unsigned i) const 160{ 161 if (!d->lastOvector().isEmpty() && i <= d->lastNumSubPatterns) { 162 int start = d->lastOvector()[2 * i]; 163 if (start >= 0) 164 return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start); 165 } 166 return jsEmptyString(exec); 167} 168 169JSValue RegExpConstructor::getLastParen(ExecState* exec) const 170{ 171 unsigned i = d->lastNumSubPatterns; 172 if (i > 0) { 173 ASSERT(!d->lastOvector().isEmpty()); 174 int start = d->lastOvector()[2 * i]; 175 if (start >= 0) 176 return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start); 177 } 178 return jsEmptyString(exec); 179} 180 181JSValue RegExpConstructor::getLeftContext(ExecState* exec) const 182{ 183 if (!d->lastOvector().isEmpty()) 184 return jsSubstring(exec, d->lastInput, 0, d->lastOvector()[0]); 185 return jsEmptyString(exec); 186} 187 188JSValue RegExpConstructor::getRightContext(ExecState* exec) const 189{ 190 if (!d->lastOvector().isEmpty()) 191 return jsSubstring(exec, d->lastInput, d->lastOvector()[1], d->lastInput.length() - d->lastOvector()[1]); 192 return jsEmptyString(exec); 193} 194 195bool RegExpConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) 196{ 197 return getStaticValueSlot<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), this, propertyName, slot); 198} 199 200bool RegExpConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) 201{ 202 return getStaticValueDescriptor<RegExpConstructor, InternalFunction>(exec, ExecState::regExpConstructorTable(exec), this, propertyName, descriptor); 203} 204 205JSValue regExpConstructorDollar1(ExecState* exec, JSValue slotBase, const Identifier&) 206{ 207 return asRegExpConstructor(slotBase)->getBackref(exec, 1); 208} 209 210JSValue regExpConstructorDollar2(ExecState* exec, JSValue slotBase, const Identifier&) 211{ 212 return asRegExpConstructor(slotBase)->getBackref(exec, 2); 213} 214 215JSValue regExpConstructorDollar3(ExecState* exec, JSValue slotBase, const Identifier&) 216{ 217 return asRegExpConstructor(slotBase)->getBackref(exec, 3); 218} 219 220JSValue regExpConstructorDollar4(ExecState* exec, JSValue slotBase, const Identifier&) 221{ 222 return asRegExpConstructor(slotBase)->getBackref(exec, 4); 223} 224 225JSValue regExpConstructorDollar5(ExecState* exec, JSValue slotBase, const Identifier&) 226{ 227 return asRegExpConstructor(slotBase)->getBackref(exec, 5); 228} 229 230JSValue regExpConstructorDollar6(ExecState* exec, JSValue slotBase, const Identifier&) 231{ 232 return asRegExpConstructor(slotBase)->getBackref(exec, 6); 233} 234 235JSValue regExpConstructorDollar7(ExecState* exec, JSValue slotBase, const Identifier&) 236{ 237 return asRegExpConstructor(slotBase)->getBackref(exec, 7); 238} 239 240JSValue regExpConstructorDollar8(ExecState* exec, JSValue slotBase, const Identifier&) 241{ 242 return asRegExpConstructor(slotBase)->getBackref(exec, 8); 243} 244 245JSValue regExpConstructorDollar9(ExecState* exec, JSValue slotBase, const Identifier&) 246{ 247 return asRegExpConstructor(slotBase)->getBackref(exec, 9); 248} 249 250JSValue regExpConstructorInput(ExecState* exec, JSValue slotBase, const Identifier&) 251{ 252 return jsString(exec, asRegExpConstructor(slotBase)->input()); 253} 254 255JSValue regExpConstructorMultiline(ExecState*, JSValue slotBase, const Identifier&) 256{ 257 return jsBoolean(asRegExpConstructor(slotBase)->multiline()); 258} 259 260JSValue regExpConstructorLastMatch(ExecState* exec, JSValue slotBase, const Identifier&) 261{ 262 return asRegExpConstructor(slotBase)->getBackref(exec, 0); 263} 264 265JSValue regExpConstructorLastParen(ExecState* exec, JSValue slotBase, const Identifier&) 266{ 267 return asRegExpConstructor(slotBase)->getLastParen(exec); 268} 269 270JSValue regExpConstructorLeftContext(ExecState* exec, JSValue slotBase, const Identifier&) 271{ 272 return asRegExpConstructor(slotBase)->getLeftContext(exec); 273} 274 275JSValue regExpConstructorRightContext(ExecState* exec, JSValue slotBase, const Identifier&) 276{ 277 return asRegExpConstructor(slotBase)->getRightContext(exec); 278} 279 280void RegExpConstructor::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot) 281{ 282 lookupPut<RegExpConstructor, InternalFunction>(exec, propertyName, value, ExecState::regExpConstructorTable(exec), this, slot); 283} 284 285void setRegExpConstructorInput(ExecState* exec, JSObject* baseObject, JSValue value) 286{ 287 asRegExpConstructor(baseObject)->setInput(value.toString(exec)); 288} 289 290void setRegExpConstructorMultiline(ExecState* exec, JSObject* baseObject, JSValue value) 291{ 292 asRegExpConstructor(baseObject)->setMultiline(value.toBoolean(exec)); 293} 294 295// ECMA 15.10.4 296JSObject* constructRegExp(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args) 297{ 298 JSValue arg0 = args.at(0); 299 JSValue arg1 = args.at(1); 300 301 if (arg0.inherits(&RegExpObject::s_info)) { 302 if (!arg1.isUndefined()) 303 return throwError(exec, createTypeError(exec, "Cannot supply flags when constructing one RegExp from another.")); 304 return asObject(arg0); 305 } 306 307 UString pattern = arg0.isUndefined() ? UString("") : arg0.toString(exec); 308 if (exec->hadException()) 309 return 0; 310 311 RegExpFlags flags = NoFlags; 312 if (!arg1.isUndefined()) { 313 flags = regExpFlags(arg1.toString(exec)); 314 if (exec->hadException()) 315 return 0; 316 if (flags == InvalidFlags) 317 return throwError(exec, createSyntaxError(exec, "Invalid flags supplied to RegExp constructor.")); 318 } 319 320 RefPtr<RegExp> regExp = exec->globalData().regExpCache()->lookupOrCreate(pattern, flags); 321 if (!regExp->isValid()) 322 return throwError(exec, createSyntaxError(exec, regExp->errorMessage())); 323 return new (exec) RegExpObject(exec->lexicalGlobalObject(), globalObject->regExpStructure(), regExp.release()); 324} 325 326static EncodedJSValue JSC_HOST_CALL constructWithRegExpConstructor(ExecState* exec) 327{ 328 ArgList args(exec); 329 return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args)); 330} 331 332ConstructType RegExpConstructor::getConstructData(ConstructData& constructData) 333{ 334 constructData.native.function = constructWithRegExpConstructor; 335 return ConstructTypeHost; 336} 337 338// ECMA 15.10.3 339static EncodedJSValue JSC_HOST_CALL callRegExpConstructor(ExecState* exec) 340{ 341 ArgList args(exec); 342 return JSValue::encode(constructRegExp(exec, asInternalFunction(exec->callee())->globalObject(), args)); 343} 344 345CallType RegExpConstructor::getCallData(CallData& callData) 346{ 347 callData.native.function = callRegExpConstructor; 348 return CallTypeHost; 349} 350 351void RegExpConstructor::setInput(const UString& input) 352{ 353 d->input = input; 354} 355 356const UString& RegExpConstructor::input() const 357{ 358 // Can detect a distinct initial state that is invisible to JavaScript, by checking for null 359 // state (since jsString turns null strings to empty strings). 360 return d->input; 361} 362 363void RegExpConstructor::setMultiline(bool multiline) 364{ 365 d->multiline = multiline; 366} 367 368bool RegExpConstructor::multiline() const 369{ 370 return d->multiline; 371} 372 373} // namespace JSC 374