1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
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 "RegExpPrototype.h"
23
24 #include "ArrayPrototype.h"
25 #include "JSArray.h"
26 #include "JSObject.h"
27 #include "JSString.h"
28 #include "JSValue.h"
29 #include "ObjectPrototype.h"
30 #include "PrototypeFunction.h"
31 #include "RegExpObject.h"
32 #include "RegExp.h"
33
34 namespace JSC {
35
36 ASSERT_CLASS_FITS_IN_CELL(RegExpPrototype);
37
38 static JSValuePtr regExpProtoFuncTest(ExecState*, JSObject*, JSValuePtr, const ArgList&);
39 static JSValuePtr regExpProtoFuncExec(ExecState*, JSObject*, JSValuePtr, const ArgList&);
40 static JSValuePtr regExpProtoFuncCompile(ExecState*, JSObject*, JSValuePtr, const ArgList&);
41 static JSValuePtr regExpProtoFuncToString(ExecState*, JSObject*, JSValuePtr, const ArgList&);
42
43 // ECMA 15.10.5
44
45 const ClassInfo RegExpPrototype::info = { "RegExpPrototype", 0, 0, 0 };
46
RegExpPrototype(ExecState * exec,PassRefPtr<Structure> structure,Structure * prototypeFunctionStructure)47 RegExpPrototype::RegExpPrototype(ExecState* exec, PassRefPtr<Structure> structure, Structure* prototypeFunctionStructure)
48 : JSObject(structure)
49 {
50 putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().compile, regExpProtoFuncCompile), DontEnum);
51 putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().exec, regExpProtoFuncExec), DontEnum);
52 putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().test, regExpProtoFuncTest), DontEnum);
53 putDirectFunctionWithoutTransition(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, regExpProtoFuncToString), DontEnum);
54 }
55
56 // ------------------------------ Functions ---------------------------
57
regExpProtoFuncTest(ExecState * exec,JSObject *,JSValuePtr thisValue,const ArgList & args)58 JSValuePtr regExpProtoFuncTest(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList& args)
59 {
60 if (!thisValue.isObject(&RegExpObject::info))
61 return throwError(exec, TypeError);
62 return asRegExpObject(thisValue)->test(exec, args);
63 }
64
regExpProtoFuncExec(ExecState * exec,JSObject *,JSValuePtr thisValue,const ArgList & args)65 JSValuePtr regExpProtoFuncExec(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList& args)
66 {
67 if (!thisValue.isObject(&RegExpObject::info))
68 return throwError(exec, TypeError);
69 return asRegExpObject(thisValue)->exec(exec, args);
70 }
71
regExpProtoFuncCompile(ExecState * exec,JSObject *,JSValuePtr thisValue,const ArgList & args)72 JSValuePtr regExpProtoFuncCompile(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList& args)
73 {
74 if (!thisValue.isObject(&RegExpObject::info))
75 return throwError(exec, TypeError);
76
77 RefPtr<RegExp> regExp;
78 JSValuePtr arg0 = args.at(exec, 0);
79 JSValuePtr arg1 = args.at(exec, 1);
80
81 if (arg0.isObject(&RegExpObject::info)) {
82 if (!arg1.isUndefined())
83 return throwError(exec, TypeError, "Cannot supply flags when constructing one RegExp from another.");
84 regExp = asRegExpObject(arg0)->regExp();
85 } else {
86 UString pattern = args.isEmpty() ? UString("") : arg0.toString(exec);
87 UString flags = arg1.isUndefined() ? UString("") : arg1.toString(exec);
88 regExp = RegExp::create(&exec->globalData(), pattern, flags);
89 }
90
91 if (!regExp->isValid())
92 return throwError(exec, SyntaxError, UString("Invalid regular expression: ").append(regExp->errorMessage()));
93
94 asRegExpObject(thisValue)->setRegExp(regExp.release());
95 asRegExpObject(thisValue)->setLastIndex(0);
96 return jsUndefined();
97 }
98
regExpProtoFuncToString(ExecState * exec,JSObject *,JSValuePtr thisValue,const ArgList &)99 JSValuePtr regExpProtoFuncToString(ExecState* exec, JSObject*, JSValuePtr thisValue, const ArgList&)
100 {
101 if (!thisValue.isObject(&RegExpObject::info)) {
102 if (thisValue.isObject(&RegExpPrototype::info))
103 return jsNontrivialString(exec, "//");
104 return throwError(exec, TypeError);
105 }
106
107 UString result = "/" + asRegExpObject(thisValue)->get(exec, exec->propertyNames().source).toString(exec);
108 result.append('/');
109 if (asRegExpObject(thisValue)->get(exec, exec->propertyNames().global).toBoolean(exec))
110 result.append('g');
111 if (asRegExpObject(thisValue)->get(exec, exec->propertyNames().ignoreCase).toBoolean(exec))
112 result.append('i');
113 if (asRegExpObject(thisValue)->get(exec, exec->propertyNames().multiline).toBoolean(exec))
114 result.append('m');
115 return jsNontrivialString(exec, result);
116 }
117
118 } // namespace JSC
119