• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kjs
 

kjs

  • kjs
regexp_object.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4  * Copyright (C) 2003 Apple Computer, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21 
22 #include <stdio.h>
23 
24 #include "value.h"
25 #include "object.h"
26 #include "types.h"
27 #include "interpreter.h"
28 #include "operations.h"
29 #include "internal.h"
30 #include "regexp.h"
31 #include "regexp_object.h"
32 #include "error_object.h"
33 #include "lookup.h"
34 
35 using namespace KJS;
36 
37 // ------------------------------ RegExpPrototypeImp ---------------------------
38 
39 // ECMA 15.9.4
40 
41 const ClassInfo RegExpPrototypeImp::info = {"RegExp", 0, 0, 0};
42 
43 RegExpPrototypeImp::RegExpPrototypeImp(ExecState *exec,
44  ObjectPrototypeImp *objProto,
45  FunctionPrototypeImp *funcProto)
46  : ObjectImp(objProto)
47 {
48  Value protect(this);
49  setInternalValue(String(""));
50 
51  // The constructor will be added later in RegExpObject's constructor (?)
52 
53  static const Identifier execPropertyName("exec");
54  putDirect(execPropertyName,
55  new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::Exec, 0, execPropertyName), DontEnum);
56  static const Identifier testPropertyName("test");
57  putDirect(testPropertyName,
58  new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::Test, 0, testPropertyName), DontEnum);
59  putDirect(toStringPropertyName,
60  new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::ToString, 0, toStringPropertyName), DontEnum);
61  static const Identifier compilePropertyName("compile");
62  putDirect(compilePropertyName,
63  new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::Compile, 1, compilePropertyName), DontEnum);
64 }
65 
66 // ------------------------------ RegExpProtoFuncImp ---------------------------
67 
68 RegExpProtoFuncImp::RegExpProtoFuncImp(ExecState * /*exec*/, FunctionPrototypeImp *funcProto,
69  int i, int len, const Identifier &_ident)
70  : InternalFunctionImp(funcProto), id(i)
71 {
72  Value protect(this);
73  putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);
74  ident = _ident;
75 }
76 
77 bool RegExpProtoFuncImp::implementsCall() const
78 {
79  return true;
80 }
81 
82 Value RegExpProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &args)
83 {
84  if (!thisObj.inherits(&RegExpImp::info)) {
85  if (thisObj.inherits(&RegExpPrototypeImp::info)) {
86  switch (id) {
87  case ToString: return String("//"); // FireFox returns /(?:)/
88  }
89  }
90  Object err = Error::create(exec,TypeError);
91  exec->setException(err);
92  return err;
93  }
94 
95  RegExpImp *reimp = static_cast<RegExpImp*>(thisObj.imp());
96  RegExp *re = reimp->regExp();
97  String s;
98  UString str;
99  switch (id) {
100  case Exec: // 15.10.6.2
101  case Test:
102  {
103  s = args[0].toString(exec);
104  int length = s.value().size();
105 
106  // Get values from the last time (in case of /g)
107  Value lastIndex = thisObj.get(exec,"lastIndex");
108  int i = lastIndex.isValid() ? lastIndex.toInt32(exec) : 0;
109  bool globalFlag = thisObj.get(exec,"global").toBoolean(exec);
110  if (!globalFlag)
111  i = 0;
112  if (i < 0 || i > length) {
113  thisObj.put(exec,"lastIndex", Number(0), DontDelete | DontEnum);
114  if (id == Test)
115  return Boolean(false);
116  else
117  return Null();
118  }
119  RegExpObjectImp* regExpObj = static_cast<RegExpObjectImp*>(exec->lexicalInterpreter()->builtinRegExp().imp());
120  int **ovector = regExpObj->registerRegexp( re, s.value() );
121 
122  re->prepareMatch(s.value());
123  str = re->match(s.value(), i, 0L, ovector);
124  re->doneMatch();
125  regExpObj->setSubPatterns(re->subPatterns());
126 
127  if (id == Test)
128  return Boolean(!str.isNull());
129 
130  if (str.isNull()) // no match
131  {
132  if (globalFlag)
133  thisObj.put(exec,"lastIndex",Number(0), DontDelete | DontEnum);
134  return Null();
135  }
136  else // success
137  {
138  if (globalFlag)
139  thisObj.put(exec,"lastIndex",Number( (*ovector)[1] ), DontDelete | DontEnum);
140  return regExpObj->arrayOfMatches(exec,str);
141  }
142  }
143  break;
144  case ToString:
145  s = thisObj.get(exec,"source").toString(exec);
146  str = "/";
147  str += s.value();
148  str += "/";
149  if (thisObj.get(exec,"global").toBoolean(exec)) {
150  str += "g";
151  }
152  if (thisObj.get(exec,"ignoreCase").toBoolean(exec)) {
153  str += "i";
154  }
155  if (thisObj.get(exec,"multiline").toBoolean(exec)) {
156  str += "m";
157  }
158  return String(str);
159  case Compile: {
160  RegExp* newEngine = RegExpObjectImp::makeEngine(exec, args[0].toString(exec), args[1]);
161  if (!newEngine)
162  return exec->exception();
163  reimp->setRegExp(newEngine);
164  return Value(reimp);
165  }
166  }
167 
168 
169  return Undefined();
170 }
171 
172 // ------------------------------ RegExpImp ------------------------------------
173 
174 const ClassInfo RegExpImp::info = {"RegExp", 0, 0, 0};
175 
176 RegExpImp::RegExpImp(RegExpPrototypeImp *regexpProto)
177  : ObjectImp(regexpProto), reg(0L)
178 {
179 }
180 
181 RegExpImp::~RegExpImp()
182 {
183  delete reg;
184 }
185 
186 void RegExpImp::setRegExp(RegExp *r)
187 {
188  delete reg;
189  reg = r;
190 
191  Object protect(this);//Protect self from GC (we are allocating a StringImp, and may be new)
192  putDirect("global", (r->flags() & RegExp::Global) ? BooleanImp::staticTrue : BooleanImp::staticFalse,
193  DontDelete | ReadOnly | DontEnum);
194  putDirect("ignoreCase", (r->flags() & RegExp::IgnoreCase) ? BooleanImp::staticTrue : BooleanImp::staticFalse,
195  DontDelete | ReadOnly | DontEnum);
196  putDirect("multiline", (r->flags() & RegExp::Multiline) ? BooleanImp::staticTrue : BooleanImp::staticFalse,
197  DontDelete | ReadOnly | DontEnum);
198 
199  putDirect("source", new StringImp(r->pattern()), DontDelete | ReadOnly | DontEnum);
200  putDirect("lastIndex", NumberImp::zero(), DontDelete | DontEnum);
201 }
202 
203 // ------------------------------ RegExpObjectImp ------------------------------
204 
205 RegExpObjectImp::RegExpObjectImp(ExecState * /*exec*/,
206  FunctionPrototypeImp *funcProto,
207  RegExpPrototypeImp *regProto)
208 
209  : InternalFunctionImp(funcProto), lastOvector(0L), lastNrSubPatterns(0)
210 {
211  Value protect(this);
212  // ECMA 15.10.5.1 RegExp.prototype
213  putDirect(prototypePropertyName, regProto, DontEnum|DontDelete|ReadOnly);
214 
215  // no. of arguments for constructor
216  putDirect(lengthPropertyName, NumberImp::two(), ReadOnly|DontDelete|DontEnum);
217 }
218 
219 RegExpObjectImp::~RegExpObjectImp()
220 {
221  delete [] lastOvector;
222 }
223 
224 int **RegExpObjectImp::registerRegexp( const RegExp* re, const UString& s )
225 {
226  lastString = s;
227  delete [] lastOvector;
228  lastOvector = 0;
229  lastNrSubPatterns = re->subPatterns();
230  return &lastOvector;
231 }
232 
233 Object RegExpObjectImp::arrayOfMatches(ExecState *exec, const UString &result) const
234 {
235  List list;
236  // The returned array contains 'result' as first item, followed by the list of matches
237  list.append(String(result));
238  if ( lastOvector )
239  for ( unsigned int i = 1 ; i < lastNrSubPatterns + 1 ; ++i )
240  {
241  UString substring = lastString.substr( lastOvector[2*i], lastOvector[2*i+1] - lastOvector[2*i] );
242  list.append(String(substring));
243  }
244  Object arr = exec->lexicalInterpreter()->builtinArray().construct(exec, list);
245  arr.put(exec, "index", Number(lastOvector[0]));
246  arr.put(exec, "input", String(lastString));
247  return arr;
248 }
249 
250 Value RegExpObjectImp::get(ExecState *exec, const Identifier &p) const
251 {
252  UString s = p.ustring();
253  if (s[0] == '$' && lastOvector)
254  {
255  bool ok;
256  unsigned long i = s.substr(1).toULong(&ok);
257  if (ok)
258  {
259  if (i < lastNrSubPatterns + 1)
260  {
261  UString substring = lastString.substr( lastOvector[2*i], lastOvector[2*i+1] - lastOvector[2*i] );
262  return String(substring);
263  }
264  return String("");
265  }
266  }
267  return InternalFunctionImp::get(exec, p);
268 }
269 
270 bool RegExpObjectImp::hasProperty(ExecState *exec, const Identifier &p) const
271 {
272  UString s = p.ustring();
273  if (s[0] == '$' && lastOvector) {
274  bool ok;
275  (void)s.substr(1).toULong(&ok);
276  if (ok)
277  return true;
278  }
279 
280  return InternalFunctionImp::hasProperty(exec, p);
281 }
282 
283 bool RegExpObjectImp::implementsConstruct() const
284 {
285  return true;
286 }
287 
288 RegExp* RegExpObjectImp::makeEngine(ExecState *exec, const UString &p, const Value &flagsInput)
289 {
290  UString flags = flagsInput.type() == UndefinedType ? UString("") : flagsInput.toString(exec);
291 
292  // Check for validity of flags
293  for (int pos = 0; pos < flags.size(); ++pos) {
294  switch (flags[pos].unicode()) {
295  case 'g':
296  case 'i':
297  case 'm':
298  break;
299  default: {
300  Object err = Error::create(exec, SyntaxError,
301  "Invalid regular expression flags");
302  exec->setException(err);
303  return 0;
304  }
305  }
306  }
307 
308  bool global = (flags.find("g") >= 0);
309  bool ignoreCase = (flags.find("i") >= 0);
310  bool multiline = (flags.find("m") >= 0);
311 
312  int reflags = RegExp::None;
313  if (global)
314  reflags |= RegExp::Global;
315  if (ignoreCase)
316  reflags |= RegExp::IgnoreCase;
317  if (multiline)
318  reflags |= RegExp::Multiline;
319 
320  RegExp *re = new RegExp(p, reflags);
321  if (!re->isValid()) {
322  Object err = Error::create(exec, SyntaxError,
323  "Invalid regular expression");
324  exec->setException(err);
325  delete re;
326  return 0;
327  }
328  return re;
329 }
330 
331 // ECMA 15.10.4
332 Object RegExpObjectImp::construct(ExecState *exec, const List &args)
333 {
334  UString p;
335  if (args.isEmpty()) {
336  p = "";
337  } else {
338  Value a0 = args[0];
339  if (a0.isA(ObjectType) && a0.toObject(exec).inherits(&RegExpImp::info)) {
340  // It's a regexp. Check that no flags were passed.
341  if (args.size() > 1 && args[1].type() != UndefinedType) {
342  Object err = Error::create(exec,TypeError);
343  exec->setException(err);
344  return err;
345  }
346  RegExpImp *rimp = static_cast<RegExpImp*>(Object::dynamicCast(a0).imp());
347  p = rimp->regExp()->pattern();
348  } else {
349  p = a0.toString(exec);
350  }
351  }
352 
353  RegExp* re = makeEngine(exec, p, args[1]);
354  if (!re)
355  return exec->exception().toObject(exec);
356 
357  RegExpPrototypeImp *proto = static_cast<RegExpPrototypeImp*>(exec->lexicalInterpreter()->builtinRegExpPrototype().imp());
358  RegExpImp *dat = new RegExpImp(proto);
359  Object obj(dat); // protect from GC
360  dat->setRegExp(re);
361 
362  return obj;
363 }
364 
365 bool RegExpObjectImp::implementsCall() const
366 {
367  return true;
368 }
369 
370 // ECMA 15.10.3
371 Value RegExpObjectImp::call(ExecState *exec, Object &/*thisObj*/,
372  const List &args)
373 {
374  // TODO: handle RegExp argument case (15.10.3.1)
375 
376  return construct(exec, args);
377 }
KJS::Value
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents...
Definition: value.h:167
KJS::UString::substr
UString substr(int pos=0, int len=-1) const
Definition: ustring.cpp:868
KJS::Value::type
Type type() const
Returns the type of value.
Definition: value.h:195
KJS::InternalFunctionImp
Base class for all function objects.
Definition: function.h:40
KJS::Object::get
Value get(ExecState *exec, const Identifier &propertyName) const
Retrieves the specified property from the object.
Definition: object.h:663
KJS::UString::size
int size() const
Definition: ustring.h:359
KJS::Object::construct
Object construct(ExecState *exec, const List &args)
Creates a new object based on this object.
Definition: object.h:696
KJS::Number
Represents an primitive Number value.
Definition: value.h:367
KJS::List::append
void append(const Value &val)
Append an object to the end of the list.
Definition: list.h:66
KJS::Value::toBoolean
bool toBoolean(ExecState *exec) const
Performs the ToBoolean type conversion operation on this value (ECMA 9.2)
Definition: value.h:216
KJS::UString::find
int find(const UString &f, int pos=0) const
Definition: ustring.cpp:798
KJS::Null
Represents an primitive Null value.
Definition: value.h:294
KJS::ExecState::lexicalInterpreter
Interpreter * lexicalInterpreter() const
Returns the interpreter associated with the current scope's global object.
Definition: interpreter.cpp:394
KJS::Interpreter::builtinRegExpPrototype
Object builtinRegExpPrototype() const
Returns the builtin "RegExp.prototype" object.
Definition: interpreter.cpp:253
KJS::Error::create
static Object create(ExecState *exec, ErrorType errtype=GeneralError, const char *message=0, int lineno=-1, int sourceId=-1)
Factory method for error objects.
Definition: object.cpp:503
KJS::FunctionPrototypeImp
The initial value of Function.prototype (and thus all objects created with the Function constructor) ...
Definition: function_object.h:34
KJS::Value::toInt32
int toInt32(ExecState *exec) const
Performs the ToInt32 type conversion operation on this value (ECMA 9.5)
Definition: value.h:231
KJS::Object
Represents an Object.
Definition: object.h:81
KJS::Undefined
Represents an primitive Undefined value.
Definition: value.h:269
KJS::Value::toObject
Object toObject(ExecState *exec) const
Performs the ToObject type conversion operation on this value (ECMA 9.9)
Definition: object.h:358
KJS::Value::isA
bool isA(Type t) const
Checks whether or not the value is of a particular tpye.
Definition: value.h:203
KJS::UString
Unicode string class.
Definition: ustring.h:189
KJS::String
Represents an primitive String value.
Definition: value.h:340
KJS::Object::dynamicCast
static Object dynamicCast(const Value &v)
Converts a Value into an Object.
Definition: object.cpp:45
KJS::UString::isNull
bool isNull() const
Definition: ustring.h:343
KJS
Definition: array_instance.h:27
KJS::Identifier::ustring
const UString & ustring() const
returns a UString of the identifier
Definition: identifier.h:52
KJS::List::size
int size() const
Definition: list.h:90
KJS::Object::put
void put(ExecState *exec, const Identifier &propertyName, const Value &value, int attr=None)
Sets the specified property.
Definition: object.h:669
KJS::List
Native list type.
Definition: list.h:48
KJS::Boolean
Represents an primitive Boolean value.
Definition: value.h:316
KJS::Value::toString
UString toString(ExecState *exec) const
Performs the ToString type conversion operation on this value (ECMA 9.8)
Definition: value.h:246
KJS::ClassInfo
Class Information.
Definition: object.h:58
KJS::Interpreter::builtinArray
Object builtinArray() const
Returns the builtin "Array" object.
Definition: interpreter.cpp:183
KJS::List::isEmpty
bool isEmpty() const
Definition: list.h:86
KJS::Value::isValid
bool isValid() const
Returns whether or not this is a valid value.
Definition: value.h:181
KJS::Interpreter::builtinRegExp
Object builtinRegExp() const
Returns the builtin "RegExp" object.
Definition: interpreter.cpp:208
KJS::UString::toULong
unsigned long toULong(bool *ok, bool tolerateEmptyString) const
Attempts an conversion to an unsigned long integer.
Definition: ustring.cpp:685
KJS::ExecState
Represents the current state of script execution.
Definition: interpreter.h:438
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32

kjs

Skip menu "kjs"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kjs

Skip menu "kjs"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kjs by doxygen 1.8.8
This website is maintained by Timothy Pearson.