2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved.
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.
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.
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
22 #include "RegExpPrototype.h"
24 #include "ArrayPrototype.h"
27 #include "JSFunction.h"
30 #include "JSStringBuilder.h"
32 #include "ObjectPrototype.h"
33 #include "PrototypeFunction.h"
34 #include "RegExpObject.h"
36 #include "RegExpCache.h"
37 #include "StringRecursionChecker.h"
38 #include "UStringConcatenate.h"
42 ASSERT_CLASS_FITS_IN_CELL(RegExpPrototype);
44 static EncodedJSValue JSC_HOST_CALL regExpProtoFuncTest(ExecState*);
45 static EncodedJSValue JSC_HOST_CALL regExpProtoFuncExec(ExecState*);
46 static EncodedJSValue JSC_HOST_CALL regExpProtoFuncCompile(ExecState*);
47 static EncodedJSValue JSC_HOST_CALL regExpProtoFuncToString(ExecState*);
51 RegExpPrototype::RegExpPrototype(ExecState* exec, JSGlobalObject* globalObject, NonNullPassRefPtr<Structure> structure, Structure* prototypeFunctionStructure)
52 : RegExpObject(globalObject, structure, RegExp::create(&exec->globalData(), "", ""))
54 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().compile, regExpProtoFuncCompile), DontEnum);
55 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().exec, regExpProtoFuncExec), DontEnum);
56 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().test, regExpProtoFuncTest), DontEnum);
57 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().toString, regExpProtoFuncToString), DontEnum);
60 // ------------------------------ Functions ---------------------------
62 EncodedJSValue JSC_HOST_CALL regExpProtoFuncTest(ExecState* exec)
64 JSValue thisValue = exec->hostThisValue();
65 if (!thisValue.inherits(&RegExpObject::info))
66 return throwVMTypeError(exec);
67 return JSValue::encode(asRegExpObject(thisValue)->test(exec));
70 EncodedJSValue JSC_HOST_CALL regExpProtoFuncExec(ExecState* exec)
72 JSValue thisValue = exec->hostThisValue();
73 if (!thisValue.inherits(&RegExpObject::info))
74 return throwVMTypeError(exec);
75 return JSValue::encode(asRegExpObject(thisValue)->exec(exec));
78 EncodedJSValue JSC_HOST_CALL regExpProtoFuncCompile(ExecState* exec)
80 JSValue thisValue = exec->hostThisValue();
81 if (!thisValue.inherits(&RegExpObject::info))
82 return throwVMTypeError(exec);
84 RefPtr<RegExp> regExp;
85 JSValue arg0 = exec->argument(0);
86 JSValue arg1 = exec->argument(1);
88 if (arg0.inherits(&RegExpObject::info)) {
89 if (!arg1.isUndefined())
90 return throwVMError(exec, createTypeError(exec, "Cannot supply flags when constructing one RegExp from another."));
91 regExp = asRegExpObject(arg0)->regExp();
93 UString pattern = !exec->argumentCount() ? UString("") : arg0.toString(exec);
94 UString flags = arg1.isUndefined() ? UString("") : arg1.toString(exec);
95 regExp = exec->globalData().regExpCache()->lookupOrCreate(pattern, flags);
98 if (!regExp->isValid())
99 return throwVMError(exec, createSyntaxError(exec, regExp->errorMessage()));
101 asRegExpObject(thisValue)->setRegExp(regExp.release());
102 asRegExpObject(thisValue)->setLastIndex(0);
103 return JSValue::encode(jsUndefined());
106 EncodedJSValue JSC_HOST_CALL regExpProtoFuncToString(ExecState* exec)
108 JSValue thisValue = exec->hostThisValue();
109 if (!thisValue.inherits(&RegExpObject::info)) {
110 if (thisValue.inherits(&RegExpPrototype::info))
111 return JSValue::encode(jsNontrivialString(exec, "//"));
112 return throwVMTypeError(exec);
115 RegExpObject* thisObject = asRegExpObject(thisValue);
117 StringRecursionChecker checker(exec, thisObject);
118 if (EncodedJSValue earlyReturnValue = checker.earlyReturnValue())
119 return earlyReturnValue;
121 char postfix[5] = { '/', 0, 0, 0, 0 };
123 if (thisObject->get(exec, exec->propertyNames().global).toBoolean(exec))
124 postfix[index++] = 'g';
125 if (thisObject->get(exec, exec->propertyNames().ignoreCase).toBoolean(exec))
126 postfix[index++] = 'i';
127 if (thisObject->get(exec, exec->propertyNames().multiline).toBoolean(exec))
128 postfix[index] = 'm';
129 UString source = thisObject->get(exec, exec->propertyNames().source).toString(exec);
130 // If source is empty, use "/(?:)/" to avoid colliding with comment syntax
131 return JSValue::encode(jsMakeNontrivialString(exec, "/", source.length() ? source : UString("(?:)"), postfix));