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 "UStringConcatenate.h"
41 ASSERT_CLASS_FITS_IN_CELL(RegExpPrototype);
43 static EncodedJSValue JSC_HOST_CALL regExpProtoFuncTest(ExecState*);
44 static EncodedJSValue JSC_HOST_CALL regExpProtoFuncExec(ExecState*);
45 static EncodedJSValue JSC_HOST_CALL regExpProtoFuncCompile(ExecState*);
46 static EncodedJSValue JSC_HOST_CALL regExpProtoFuncToString(ExecState*);
50 RegExpPrototype::RegExpPrototype(ExecState* exec, JSGlobalObject* globalObject, NonNullPassRefPtr<Structure> structure, Structure* prototypeFunctionStructure)
51 : RegExpObject(globalObject, structure, RegExp::create(&exec->globalData(), "", ""))
53 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().compile, regExpProtoFuncCompile), DontEnum);
54 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().exec, regExpProtoFuncExec), DontEnum);
55 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().test, regExpProtoFuncTest), DontEnum);
56 putDirectFunctionWithoutTransition(exec, new (exec) NativeFunctionWrapper(exec, globalObject, prototypeFunctionStructure, 0, exec->propertyNames().toString, regExpProtoFuncToString), DontEnum);
59 // ------------------------------ Functions ---------------------------
61 EncodedJSValue JSC_HOST_CALL regExpProtoFuncTest(ExecState* exec)
63 JSValue thisValue = exec->hostThisValue();
64 if (!thisValue.inherits(&RegExpObject::info))
65 return throwVMTypeError(exec);
66 return JSValue::encode(asRegExpObject(thisValue)->test(exec));
69 EncodedJSValue JSC_HOST_CALL regExpProtoFuncExec(ExecState* exec)
71 JSValue thisValue = exec->hostThisValue();
72 if (!thisValue.inherits(&RegExpObject::info))
73 return throwVMTypeError(exec);
74 return JSValue::encode(asRegExpObject(thisValue)->exec(exec));
77 EncodedJSValue JSC_HOST_CALL regExpProtoFuncCompile(ExecState* exec)
79 JSValue thisValue = exec->hostThisValue();
80 if (!thisValue.inherits(&RegExpObject::info))
81 return throwVMTypeError(exec);
83 RefPtr<RegExp> regExp;
84 JSValue arg0 = exec->argument(0);
85 JSValue arg1 = exec->argument(1);
87 if (arg0.inherits(&RegExpObject::info)) {
88 if (!arg1.isUndefined())
89 return throwVMError(exec, createTypeError(exec, "Cannot supply flags when constructing one RegExp from another."));
90 regExp = asRegExpObject(arg0)->regExp();
92 UString pattern = !exec->argumentCount() ? UString("") : arg0.toString(exec);
93 UString flags = arg1.isUndefined() ? UString("") : arg1.toString(exec);
94 regExp = exec->globalData().regExpCache()->lookupOrCreate(pattern, flags);
97 if (!regExp->isValid())
98 return throwVMError(exec, createSyntaxError(exec, regExp->errorMessage()));
100 asRegExpObject(thisValue)->setRegExp(regExp.release());
101 asRegExpObject(thisValue)->setLastIndex(0);
102 return JSValue::encode(jsUndefined());
105 EncodedJSValue JSC_HOST_CALL regExpProtoFuncToString(ExecState* exec)
107 JSValue thisValue = exec->hostThisValue();
108 if (!thisValue.inherits(&RegExpObject::info)) {
109 if (thisValue.inherits(&RegExpPrototype::info))
110 return JSValue::encode(jsNontrivialString(exec, "//"));
111 return throwVMTypeError(exec);
114 char postfix[5] = { '/', 0, 0, 0, 0 };
116 if (asRegExpObject(thisValue)->get(exec, exec->propertyNames().global).toBoolean(exec))
117 postfix[index++] = 'g';
118 if (asRegExpObject(thisValue)->get(exec, exec->propertyNames().ignoreCase).toBoolean(exec))
119 postfix[index++] = 'i';
120 if (asRegExpObject(thisValue)->get(exec, exec->propertyNames().multiline).toBoolean(exec))
121 postfix[index] = 'm';
122 UString source = asRegExpObject(thisValue)->get(exec, exec->propertyNames().source).toString(exec);
123 // If source is empty, use "/(?:)/" to avoid colliding with comment syntax
124 return JSValue::encode(jsMakeNontrivialString(exec, "/", source.length() ? source : UString("(?:)"), postfix));