source: webkit/trunk/JavaScriptCore/kjs/RegExpObject.cpp@ 35906

Last change on this file since 35906 was 35807, checked in by [email protected], 17 years ago

JavaScriptCore:

2008-08-17 Geoffrey Garen <[email protected]>

Reviewed by Cameron Zwarich.

Made room for a free word in JSCell.


SunSpider says no change.


I changed JSCallbackObjectData, Arguments, JSArray, and RegExpObject to
store auxiliary data in a secondary structure.

I changed InternalFunction to store the function's name in the property
map.


I changed JSGlobalObjectData to use a virtual destructor, so WebCore's
JSDOMWindowBaseData could inherit from it safely. (It's a strange design
for JSDOMWindowBase to allocate an object that JSGlobalObject deletes,
but that's really our only option, given the size constraint.)


I also added a bunch of compile-time ASSERTs, and removed lots of comments
in JSObject.h because they were often out of date, and they got in the
way of reading what was actually going on.


Also renamed JSArray::getLength to JSArray::length, to match our style
guidelines.

WebCore:

2008-08-17 Geoffrey Garen <[email protected]>

Reviewed by Cameron Zwarich.

Made room for a free word in JSCell.


Changed JSDOMWindowBase to store its auxiliary data in a subclass of
JSGlobalData, so the two could share a pointer.


Added a bunch of ASSERTs, to help catch over-sized objects.

WebKit/mac:

2008-08-17 Geoffrey Garen <[email protected]>

Reviewed by Cameron Zwarich.

Made room for a free word in JSCell.


(Updated for JavaScriptCore changes.)

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
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#include "RegExpObject.lut.h"
24
25#include "JSArray.h"
26#include "JSGlobalObject.h"
27#include "JSString.h"
28#include "RegExpConstructor.h"
29#include "RegExpPrototype.h"
30
31namespace KJS {
32
33ASSERT_CLASS_FITS_IN_CELL(RegExpObject);
34
35const ClassInfo RegExpObject::info = { "RegExp", 0, 0, ExecState::regExpTable };
36
37/* Source for RegExpObject.lut.h
38@begin regExpTable
39 global RegExpObject::Global DontDelete|ReadOnly|DontEnum
40 ignoreCase RegExpObject::IgnoreCase DontDelete|ReadOnly|DontEnum
41 multiline RegExpObject::Multiline DontDelete|ReadOnly|DontEnum
42 source RegExpObject::Source DontDelete|ReadOnly|DontEnum
43 lastIndex RegExpObject::LastIndex DontDelete|DontEnum
44@end
45*/
46
47RegExpObject::RegExpObject(RegExpPrototype* regExpPrototype, PassRefPtr<RegExp> regExp)
48 : JSObject(regExpPrototype)
49 , d(new RegExpObjectData(regExp, 0))
50{
51}
52
53RegExpObject::~RegExpObject()
54{
55}
56
57bool RegExpObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
58{
59 return getStaticValueSlot<RegExpObject, JSObject>(exec, ExecState::regExpTable(exec), this, propertyName, slot);
60}
61
62JSValue* RegExpObject::getValueProperty(ExecState* exec, int token) const
63{
64 switch (token) {
65 case Global:
66 return jsBoolean(d->regExp->global());
67 case IgnoreCase:
68 return jsBoolean(d->regExp->ignoreCase());
69 case Multiline:
70 return jsBoolean(d->regExp->multiline());
71 case Source:
72 return jsString(exec, d->regExp->pattern());
73 case LastIndex:
74 return jsNumber(exec, d->lastIndex);
75 }
76
77 ASSERT_NOT_REACHED();
78 return 0;
79}
80
81void RegExpObject::put(ExecState* exec, const Identifier& propertyName, JSValue* value)
82{
83 lookupPut<RegExpObject, JSObject>(exec, propertyName, value, ExecState::regExpTable(exec), this);
84}
85
86void RegExpObject::putValueProperty(ExecState* exec, int token, JSValue* value)
87{
88 UNUSED_PARAM(token);
89 ASSERT(token == LastIndex);
90 d->lastIndex = value->toInteger(exec);
91}
92
93bool RegExpObject::match(ExecState* exec, const ArgList& args)
94{
95 RegExpConstructor* regExpObj = exec->lexicalGlobalObject()->regExpConstructor();
96
97 UString input;
98 if (!args.isEmpty())
99 input = args.at(exec, 0)->toString(exec);
100 else {
101 input = regExpObj->input();
102 if (input.isNull()) {
103 throwError(exec, GeneralError, "No input.");
104 return false;
105 }
106 }
107
108 bool global = get(exec, exec->propertyNames().global)->toBoolean(exec);
109 int lastIndex = 0;
110 if (global) {
111 if (d->lastIndex < 0 || d->lastIndex > input.size()) {
112 d->lastIndex = 0;
113 return false;
114 }
115 lastIndex = static_cast<int>(d->lastIndex);
116 }
117
118 int foundIndex;
119 int foundLength;
120 regExpObj->performMatch(d->regExp.get(), input, lastIndex, foundIndex, foundLength);
121
122 if (global) {
123 lastIndex = foundIndex < 0 ? 0 : foundIndex + foundLength;
124 d->lastIndex = lastIndex;
125 }
126
127 return foundIndex >= 0;
128}
129
130JSValue* RegExpObject::test(ExecState* exec, const ArgList& args)
131{
132 return jsBoolean(match(exec, args));
133}
134
135JSValue* RegExpObject::exec(ExecState* exec, const ArgList& args)
136{
137 if (match(exec, args))
138 return exec->lexicalGlobalObject()->regExpConstructor()->arrayOfMatches(exec);
139 return jsNull();
140}
141
142static JSValue* callRegExpObject(ExecState* exec, JSObject* function, JSValue*, const ArgList& args)
143{
144 return static_cast<RegExpObject*>(function)->exec(exec, args);
145}
146
147CallType RegExpObject::getCallData(CallData& callData)
148{
149 callData.native.function = callRegExpObject;
150 return CallTypeHost;
151}
152
153} // namespace KJS
Note: See TracBrowser for help on using the repository browser.