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