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