]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/RegExpObject.cpp
JavaScriptCore-525.tar.gz
[apple/javascriptcore.git] / runtime / RegExpObject.cpp
CommitLineData
9dae56ea
A
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 "RegExpObject.h"
23
24#include "JSArray.h"
25#include "JSGlobalObject.h"
26#include "JSString.h"
27#include "RegExpConstructor.h"
28#include "RegExpPrototype.h"
29
30namespace JSC {
31
32static JSValuePtr regExpObjectGlobal(ExecState*, const Identifier&, const PropertySlot&);
33static JSValuePtr regExpObjectIgnoreCase(ExecState*, const Identifier&, const PropertySlot&);
34static JSValuePtr regExpObjectMultiline(ExecState*, const Identifier&, const PropertySlot&);
35static JSValuePtr regExpObjectSource(ExecState*, const Identifier&, const PropertySlot&);
36static JSValuePtr regExpObjectLastIndex(ExecState*, const Identifier&, const PropertySlot&);
37static void setRegExpObjectLastIndex(ExecState*, JSObject*, JSValuePtr);
38
39} // namespace JSC
40
41#include "RegExpObject.lut.h"
42
43namespace JSC {
44
45ASSERT_CLASS_FITS_IN_CELL(RegExpObject);
46
47const ClassInfo RegExpObject::info = { "RegExp", 0, 0, ExecState::regExpTable };
48
49/* Source for RegExpObject.lut.h
50@begin regExpTable
51 global regExpObjectGlobal DontDelete|ReadOnly|DontEnum
52 ignoreCase regExpObjectIgnoreCase DontDelete|ReadOnly|DontEnum
53 multiline regExpObjectMultiline DontDelete|ReadOnly|DontEnum
54 source regExpObjectSource DontDelete|ReadOnly|DontEnum
55 lastIndex regExpObjectLastIndex DontDelete|DontEnum
56@end
57*/
58
59RegExpObject::RegExpObject(PassRefPtr<Structure> structure, PassRefPtr<RegExp> regExp)
60 : JSObject(structure)
61 , d(new RegExpObjectData(regExp, 0))
62{
63}
64
65RegExpObject::~RegExpObject()
66{
67}
68
69bool RegExpObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
70{
71 return getStaticValueSlot<RegExpObject, JSObject>(exec, ExecState::regExpTable(exec), this, propertyName, slot);
72}
73
74JSValuePtr regExpObjectGlobal(ExecState*, const Identifier&, const PropertySlot& slot)
75{
76 return jsBoolean(asRegExpObject(slot.slotBase())->regExp()->global());
77}
78
79JSValuePtr regExpObjectIgnoreCase(ExecState*, const Identifier&, const PropertySlot& slot)
80{
81 return jsBoolean(asRegExpObject(slot.slotBase())->regExp()->ignoreCase());
82}
83
84JSValuePtr regExpObjectMultiline(ExecState*, const Identifier&, const PropertySlot& slot)
85{
86 return jsBoolean(asRegExpObject(slot.slotBase())->regExp()->multiline());
87}
88
89JSValuePtr regExpObjectSource(ExecState* exec, const Identifier&, const PropertySlot& slot)
90{
91 return jsString(exec, asRegExpObject(slot.slotBase())->regExp()->pattern());
92}
93
94JSValuePtr regExpObjectLastIndex(ExecState* exec, const Identifier&, const PropertySlot& slot)
95{
96 return jsNumber(exec, asRegExpObject(slot.slotBase())->lastIndex());
97}
98
99void RegExpObject::put(ExecState* exec, const Identifier& propertyName, JSValuePtr value, PutPropertySlot& slot)
100{
101 lookupPut<RegExpObject, JSObject>(exec, propertyName, value, ExecState::regExpTable(exec), this, slot);
102}
103
104void setRegExpObjectLastIndex(ExecState* exec, JSObject* baseObject, JSValuePtr value)
105{
106 asRegExpObject(baseObject)->setLastIndex(value.toInteger(exec));
107}
108
109JSValuePtr RegExpObject::test(ExecState* exec, const ArgList& args)
110{
111 return jsBoolean(match(exec, args));
112}
113
114JSValuePtr RegExpObject::exec(ExecState* exec, const ArgList& args)
115{
116 if (match(exec, args))
117 return exec->lexicalGlobalObject()->regExpConstructor()->arrayOfMatches(exec);
118 return jsNull();
119}
120
121static JSValuePtr callRegExpObject(ExecState* exec, JSObject* function, JSValuePtr, const ArgList& args)
122{
123 return asRegExpObject(function)->exec(exec, args);
124}
125
126CallType RegExpObject::getCallData(CallData& callData)
127{
128 callData.native.function = callRegExpObject;
129 return CallTypeHost;
130}
131
132// Shared implementation used by test and exec.
133bool RegExpObject::match(ExecState* exec, const ArgList& args)
134{
135 RegExpConstructor* regExpConstructor = exec->lexicalGlobalObject()->regExpConstructor();
136
137 UString input = args.isEmpty() ? regExpConstructor->input() : args.at(exec, 0).toString(exec);
138 if (input.isNull()) {
139 throwError(exec, GeneralError, "No input to " + toString(exec) + ".");
140 return false;
141 }
142
143 if (!regExp()->global()) {
144 int position;
145 int length;
146 regExpConstructor->performMatch(d->regExp.get(), input, 0, position, length);
147 return position >= 0;
148 }
149
150 if (d->lastIndex < 0 || d->lastIndex > input.size()) {
151 d->lastIndex = 0;
152 return false;
153 }
154
155 int position;
156 int length;
157 regExpConstructor->performMatch(d->regExp.get(), input, static_cast<int>(d->lastIndex), position, length);
158 if (position < 0) {
159 d->lastIndex = 0;
160 return false;
161 }
162
163 d->lastIndex = position + length;
164 return true;
165}
166
167} // namespace JSC