mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 1 | // -*- c-basic-offset: 2 -*- |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 2 | /* |
| 3 | * This file is part of the KDE libraries |
| 4 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
darin | 253b57c | 2003-01-22 00:11:44 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Apple Computer, Inc. |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 20 | * |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 25 | #include "value.h" |
| 26 | #include "object.h" |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 27 | #include "types.h" |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 28 | #include "interpreter.h" |
| 29 | #include "operations.h" |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 30 | #include "internal.h" |
| 31 | #include "regexp.h" |
| 32 | #include "regexp_object.h" |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 33 | #include "error_object.h" |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace KJS; |
| 36 | |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 37 | // ------------------------------ RegExpPrototypeImp --------------------------- |
| 38 | |
| 39 | // ECMA 15.9.4 |
| 40 | |
| 41 | RegExpPrototypeImp::RegExpPrototypeImp(ExecState *exec, |
| 42 | ObjectPrototypeImp *objProto, |
| 43 | FunctionPrototypeImp *funcProto) |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 44 | : ObjectImp(objProto) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 45 | { |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 46 | Value protect(this); |
| 47 | setInternalValue(String("")); |
| 48 | |
| 49 | // The constructor will be added later in RegExpObject's constructor (?) |
| 50 | |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 51 | static const Identifier execPropertyName("exec"); |
| 52 | putDirect(execPropertyName, new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::Exec, 0), DontEnum); |
| 53 | static const Identifier testPropertyName("test"); |
| 54 | putDirect(testPropertyName, new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::Test, 0), DontEnum); |
| 55 | putDirect(toStringPropertyName, new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::ToString, 0), DontEnum); |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // ------------------------------ RegExpProtoFuncImp --------------------------- |
| 59 | |
| 60 | RegExpProtoFuncImp::RegExpProtoFuncImp(ExecState *exec, |
| 61 | FunctionPrototypeImp *funcProto, int i, int len) |
| 62 | : InternalFunctionImp(funcProto), id(i) |
| 63 | { |
| 64 | Value protect(this); |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 65 | putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum); |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | bool RegExpProtoFuncImp::implementsCall() const |
| 69 | { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | Value RegExpProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &args) |
| 74 | { |
| 75 | if (!thisObj.inherits(&RegExpImp::info)) { |
| 76 | Object err = Error::create(exec,TypeError); |
| 77 | exec->setException(err); |
| 78 | return err; |
| 79 | } |
| 80 | |
| 81 | RegExpImp *reimp = static_cast<RegExpImp*>(thisObj.imp()); |
| 82 | RegExp *re = reimp->regExp(); |
| 83 | String s; |
| 84 | UString str; |
| 85 | switch (id) { |
| 86 | case Exec: // 15.10.6.2 |
| 87 | case Test: |
| 88 | { |
| 89 | s = args[0].toString(exec); |
| 90 | int length = s.value().size(); |
| 91 | Value lastIndex = thisObj.get(exec,"lastIndex"); |
| 92 | int i = lastIndex.isNull() ? 0 : lastIndex.toInt32(exec); |
| 93 | bool globalFlag = thisObj.get(exec,"global").toBoolean(exec); |
| 94 | if (!globalFlag) |
| 95 | i = 0; |
| 96 | if (i < 0 || i > length) { |
| 97 | thisObj.put(exec,"lastIndex", Number(0), DontDelete | DontEnum); |
darin | f028f81 | 2002-06-10 20:08:04 +0000 | [diff] [blame] | 98 | if (id == Test) |
| 99 | return Boolean(false); |
| 100 | else |
| 101 | Null(); |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 102 | } |
| 103 | RegExpObjectImp* regExpObj = static_cast<RegExpObjectImp*>(exec->interpreter()->builtinRegExp().imp()); |
| 104 | int **ovector = regExpObj->registerRegexp( re, s.value() ); |
| 105 | |
| 106 | str = re->match(s.value(), i, 0L, ovector); |
darin | f028f81 | 2002-06-10 20:08:04 +0000 | [diff] [blame] | 107 | regExpObj->setSubPatterns(re->subPatterns()); |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 108 | |
| 109 | if (id == Test) |
| 110 | return Boolean(!str.isNull()); |
| 111 | |
| 112 | if (str.isNull()) // no match |
| 113 | { |
| 114 | if (globalFlag) |
| 115 | thisObj.put(exec,"lastIndex",Number(0), DontDelete | DontEnum); |
| 116 | return Null(); |
| 117 | } |
| 118 | else // success |
| 119 | { |
| 120 | if (globalFlag) |
| 121 | thisObj.put(exec,"lastIndex",Number( (*ovector)[1] ), DontDelete | DontEnum); |
| 122 | return regExpObj->arrayOfMatches(exec,str); |
| 123 | } |
| 124 | } |
| 125 | break; |
| 126 | case ToString: |
| 127 | s = thisObj.get(exec,"source").toString(exec); |
| 128 | str = "/"; |
| 129 | str += s.value(); |
| 130 | str += "/"; |
| 131 | // TODO append the flags |
| 132 | return String(str); |
| 133 | } |
| 134 | |
| 135 | return Undefined(); |
| 136 | } |
| 137 | |
| 138 | // ------------------------------ RegExpImp ------------------------------------ |
| 139 | |
| 140 | const ClassInfo RegExpImp::info = {"RegExp", 0, 0, 0}; |
| 141 | |
| 142 | RegExpImp::RegExpImp(RegExpPrototypeImp *regexpProto) |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 143 | : ObjectImp(regexpProto), reg(0L) |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 144 | { |
| 145 | } |
| 146 | |
| 147 | RegExpImp::~RegExpImp() |
| 148 | { |
| 149 | delete reg; |
| 150 | } |
| 151 | |
| 152 | // ------------------------------ RegExpObjectImp ------------------------------ |
| 153 | |
| 154 | RegExpObjectImp::RegExpObjectImp(ExecState *exec, |
darin | 74f6ed6 | 2002-07-22 05:38:39 +0000 | [diff] [blame] | 155 | FunctionPrototypeImp *funcProto, |
| 156 | RegExpPrototypeImp *regProto) |
| 157 | |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 158 | : InternalFunctionImp(funcProto), lastOvector(0L), lastNrSubPatterns(0) |
| 159 | { |
| 160 | Value protect(this); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 161 | // ECMA 15.10.5.1 RegExp.prototype |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 162 | putDirect(prototypePropertyName, regProto, DontEnum|DontDelete|ReadOnly); |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 163 | |
| 164 | // no. of arguments for constructor |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 165 | putDirect(lengthPropertyName, NumberImp::two(), ReadOnly|DontDelete|DontEnum); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 166 | } |
| 167 | |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 168 | RegExpObjectImp::~RegExpObjectImp() |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 169 | { |
darin | f028f81 | 2002-06-10 20:08:04 +0000 | [diff] [blame] | 170 | delete [] lastOvector; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 171 | } |
| 172 | |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 173 | int **RegExpObjectImp::registerRegexp( const RegExp* re, const UString& s ) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 174 | { |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 175 | lastString = s; |
darin | f028f81 | 2002-06-10 20:08:04 +0000 | [diff] [blame] | 176 | delete [] lastOvector; |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 177 | lastOvector = 0; |
| 178 | lastNrSubPatterns = re->subPatterns(); |
| 179 | return &lastOvector; |
| 180 | } |
| 181 | |
darin | 74f6ed6 | 2002-07-22 05:38:39 +0000 | [diff] [blame] | 182 | Object RegExpObjectImp::arrayOfMatches(ExecState *exec, const UString &result) const |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 183 | { |
| 184 | List list; |
| 185 | // The returned array contains 'result' as first item, followed by the list of matches |
| 186 | list.append(String(result)); |
| 187 | if ( lastOvector ) |
| 188 | for ( uint i = 1 ; i < lastNrSubPatterns + 1 ; ++i ) |
| 189 | { |
| 190 | UString substring = lastString.substr( lastOvector[2*i], lastOvector[2*i+1] - lastOvector[2*i] ); |
| 191 | list.append(String(substring)); |
| 192 | } |
darin | 74f6ed6 | 2002-07-22 05:38:39 +0000 | [diff] [blame] | 193 | Object arr = exec->interpreter()->builtinArray().construct(exec, list); |
| 194 | arr.put(exec, "index", Number(lastOvector[0])); |
| 195 | arr.put(exec, "input", String(lastString)); |
| 196 | return arr; |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 197 | } |
| 198 | |
darin | 880105d | 2002-11-19 22:02:26 +0000 | [diff] [blame] | 199 | Value RegExpObjectImp::get(ExecState *exec, const Identifier &p) const |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 200 | { |
darin | d0ba328 | 2002-11-19 23:45:44 +0000 | [diff] [blame] | 201 | UString s = p.ustring(); |
| 202 | if (s[0] == '$' && lastOvector) |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 203 | { |
| 204 | bool ok; |
darin | d0ba328 | 2002-11-19 23:45:44 +0000 | [diff] [blame] | 205 | unsigned long i = s.substr(1).toULong(&ok); |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 206 | if (ok) |
| 207 | { |
| 208 | if (i < lastNrSubPatterns + 1) |
| 209 | { |
| 210 | UString substring = lastString.substr( lastOvector[2*i], lastOvector[2*i+1] - lastOvector[2*i] ); |
| 211 | return String(substring); |
| 212 | } |
| 213 | return String(""); |
| 214 | } |
| 215 | } |
darin | 74f6ed6 | 2002-07-22 05:38:39 +0000 | [diff] [blame] | 216 | return InternalFunctionImp::get(exec, p); |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | bool RegExpObjectImp::implementsConstruct() const |
| 220 | { |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | // ECMA 15.10.4 |
| 225 | Object RegExpObjectImp::construct(ExecState *exec, const List &args) |
| 226 | { |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 227 | UString p = args.isEmpty() ? UString("") : args[0].toString(exec); |
darin | 74f6ed6 | 2002-07-22 05:38:39 +0000 | [diff] [blame] | 228 | UString flags = args[1].toString(exec); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 229 | |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 230 | RegExpPrototypeImp *proto = static_cast<RegExpPrototypeImp*>(exec->interpreter()->builtinRegExpPrototype().imp()); |
| 231 | RegExpImp *dat = new RegExpImp(proto); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 232 | Object obj(dat); // protect from GC |
| 233 | |
| 234 | bool global = (flags.find("g") >= 0); |
| 235 | bool ignoreCase = (flags.find("i") >= 0); |
| 236 | bool multiline = (flags.find("m") >= 0); |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 237 | // TODO: throw a syntax error on invalid flags |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 238 | |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 239 | dat->putDirect("global", global ? BooleanImp::staticTrue : BooleanImp::staticFalse); |
| 240 | dat->putDirect("ignoreCase", ignoreCase ? BooleanImp::staticTrue : BooleanImp::staticFalse); |
| 241 | dat->putDirect("multiline", multiline ? BooleanImp::staticTrue : BooleanImp::staticFalse); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 242 | |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 243 | dat->putDirect("source", new StringImp(p)); |
| 244 | dat->putDirect("lastIndex", NumberImp::zero(), DontDelete | DontEnum); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 245 | |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 246 | int reflags = RegExp::None; |
| 247 | if (global) |
| 248 | reflags |= RegExp::Global; |
| 249 | if (ignoreCase) |
| 250 | reflags |= RegExp::IgnoreCase; |
| 251 | if (multiline) |
| 252 | reflags |= RegExp::Multiline; |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 253 | dat->setRegExp(new RegExp(p, reflags)); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 254 | |
| 255 | return obj; |
| 256 | } |
| 257 | |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 258 | bool RegExpObjectImp::implementsCall() const |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 259 | { |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 260 | return true; |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 261 | } |
| 262 | |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 263 | // ECMA 15.10.3 |
darin | 74f6ed6 | 2002-07-22 05:38:39 +0000 | [diff] [blame] | 264 | Value RegExpObjectImp::call(ExecState *exec, Object &/*thisObj*/, |
| 265 | const List &args) |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 266 | { |
darin | 74f6ed6 | 2002-07-22 05:38:39 +0000 | [diff] [blame] | 267 | // TODO: handle RegExp argument case (15.10.3.1) |
| 268 | |
| 269 | return construct(exec, args); |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 270 | } |