00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024
00025 #include "value.h"
00026 #include "object.h"
00027 #include "types.h"
00028 #include "interpreter.h"
00029 #include "operations.h"
00030 #include "internal.h"
00031 #include "regexp.h"
00032 #include "regexp_object.h"
00033 #include "error_object.h"
00034 #include "lookup.h"
00035
00036 using namespace KJS;
00037
00038
00039
00040
00041
00042 const ClassInfo RegExpPrototypeImp::info = {"RegExp", 0, 0, 0};
00043
00044 RegExpPrototypeImp::RegExpPrototypeImp(ExecState *exec,
00045 ObjectPrototypeImp *objProto,
00046 FunctionPrototypeImp *funcProto)
00047 : ObjectImp(objProto)
00048 {
00049 Value protect(this);
00050 setInternalValue(String(""));
00051
00052
00053
00054 static const Identifier execPropertyName("exec");
00055 putDirect(execPropertyName,
00056 new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::Exec, 0, execPropertyName), DontEnum);
00057 static const Identifier testPropertyName("test");
00058 putDirect(testPropertyName,
00059 new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::Test, 0, testPropertyName), DontEnum);
00060 putDirect(toStringPropertyName,
00061 new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::ToString, 0, toStringPropertyName), DontEnum);
00062 static const Identifier compilePropertyName("compile");
00063 putDirect(compilePropertyName,
00064 new RegExpProtoFuncImp(exec,funcProto,RegExpProtoFuncImp::Compile, 1, compilePropertyName), DontEnum);
00065 }
00066
00067
00068
00069 RegExpProtoFuncImp::RegExpProtoFuncImp(ExecState * , FunctionPrototypeImp *funcProto,
00070 int i, int len, const Identifier &_ident)
00071 : InternalFunctionImp(funcProto), id(i)
00072 {
00073 Value protect(this);
00074 putDirect(lengthPropertyName, len, DontDelete|ReadOnly|DontEnum);
00075 ident = _ident;
00076 }
00077
00078 bool RegExpProtoFuncImp::implementsCall() const
00079 {
00080 return true;
00081 }
00082
00083 Value RegExpProtoFuncImp::call(ExecState *exec, Object &thisObj, const List &args)
00084 {
00085 if (!thisObj.inherits(&RegExpImp::info)) {
00086 if (thisObj.inherits(&RegExpPrototypeImp::info)) {
00087 switch (id) {
00088 case ToString: return String("//");
00089 }
00090 }
00091 Object err = Error::create(exec,TypeError);
00092 exec->setException(err);
00093 return err;
00094 }
00095
00096 RegExpImp *reimp = static_cast<RegExpImp*>(thisObj.imp());
00097 RegExp *re = reimp->regExp();
00098 String s;
00099 UString str;
00100 switch (id) {
00101 case Exec:
00102 case Test:
00103 {
00104 s = args[0].toString(exec);
00105 int length = s.value().size();
00106
00107
00108 Value lastIndex = thisObj.get(exec,"lastIndex");
00109 int i = lastIndex.isValid() ? lastIndex.toInt32(exec) : 0;
00110 bool globalFlag = thisObj.get(exec,"global").toBoolean(exec);
00111 if (!globalFlag)
00112 i = 0;
00113 if (i < 0 || i > length) {
00114 thisObj.put(exec,"lastIndex", Number(0), DontDelete | DontEnum);
00115 if (id == Test)
00116 return Boolean(false);
00117 else
00118 return Null();
00119 }
00120 RegExpObjectImp* regExpObj = static_cast<RegExpObjectImp*>(exec->lexicalInterpreter()->builtinRegExp().imp());
00121 int **ovector = regExpObj->registerRegexp( re, s.value() );
00122
00123 re->prepareMatch(s.value());
00124 str = re->match(s.value(), i, 0L, ovector);
00125 re->doneMatch();
00126 regExpObj->setSubPatterns(re->subPatterns());
00127
00128 if (id == Test)
00129 return Boolean(!str.isNull());
00130
00131 if (str.isNull())
00132 {
00133 if (globalFlag)
00134 thisObj.put(exec,"lastIndex",Number(0), DontDelete | DontEnum);
00135 return Null();
00136 }
00137 else
00138 {
00139 if (globalFlag)
00140 thisObj.put(exec,"lastIndex",Number( (*ovector)[1] ), DontDelete | DontEnum);
00141 return regExpObj->arrayOfMatches(exec,str);
00142 }
00143 }
00144 break;
00145 case ToString:
00146 s = thisObj.get(exec,"source").toString(exec);
00147 str = "/";
00148 str += s.value();
00149 str += "/";
00150 if (thisObj.get(exec,"global").toBoolean(exec)) {
00151 str += "g";
00152 }
00153 if (thisObj.get(exec,"ignoreCase").toBoolean(exec)) {
00154 str += "i";
00155 }
00156 if (thisObj.get(exec,"multiline").toBoolean(exec)) {
00157 str += "m";
00158 }
00159 return String(str);
00160 case Compile: {
00161 RegExp* newEngine = RegExpObjectImp::makeEngine(exec, args[0].toString(exec), args[1]);
00162 if (!newEngine)
00163 return exec->exception();
00164 reimp->setRegExp(newEngine);
00165 return Value(reimp);
00166 }
00167 }
00168
00169
00170 return Undefined();
00171 }
00172
00173
00174
00175 const ClassInfo RegExpImp::info = {"RegExp", 0, 0, 0};
00176
00177 RegExpImp::RegExpImp(RegExpPrototypeImp *regexpProto)
00178 : ObjectImp(regexpProto), reg(0L)
00179 {
00180 }
00181
00182 RegExpImp::~RegExpImp()
00183 {
00184 delete reg;
00185 }
00186
00187 void RegExpImp::setRegExp(RegExp *r)
00188 {
00189 delete reg;
00190 reg = r;
00191
00192 Object protect(this);
00193 putDirect("global", (r->flags() & RegExp::Global) ? BooleanImp::staticTrue : BooleanImp::staticFalse,
00194 DontDelete | ReadOnly | DontEnum);
00195 putDirect("ignoreCase", (r->flags() & RegExp::IgnoreCase) ? BooleanImp::staticTrue : BooleanImp::staticFalse,
00196 DontDelete | ReadOnly | DontEnum);
00197 putDirect("multiline", (r->flags() & RegExp::Multiline) ? BooleanImp::staticTrue : BooleanImp::staticFalse,
00198 DontDelete | ReadOnly | DontEnum);
00199
00200 putDirect("source", new StringImp(r->pattern()), DontDelete | ReadOnly | DontEnum);
00201 putDirect("lastIndex", NumberImp::zero(), DontDelete | DontEnum);
00202 }
00203
00204
00205
00206 RegExpObjectImp::RegExpObjectImp(ExecState * ,
00207 FunctionPrototypeImp *funcProto,
00208 RegExpPrototypeImp *regProto)
00209
00210 : InternalFunctionImp(funcProto), lastOvector(0L), lastNrSubPatterns(0)
00211 {
00212 Value protect(this);
00213
00214 putDirect(prototypePropertyName, regProto, DontEnum|DontDelete|ReadOnly);
00215
00216
00217 putDirect(lengthPropertyName, NumberImp::two(), ReadOnly|DontDelete|DontEnum);
00218 }
00219
00220 RegExpObjectImp::~RegExpObjectImp()
00221 {
00222 delete [] lastOvector;
00223 }
00224
00225 int **RegExpObjectImp::registerRegexp( const RegExp* re, const UString& s )
00226 {
00227 lastString = s;
00228 delete [] lastOvector;
00229 lastOvector = 0;
00230 lastNrSubPatterns = re->subPatterns();
00231 return &lastOvector;
00232 }
00233
00234 Object RegExpObjectImp::arrayOfMatches(ExecState *exec, const UString &result) const
00235 {
00236 List list;
00237
00238 list.append(String(result));
00239 if ( lastOvector )
00240 for ( unsigned int i = 1 ; i < lastNrSubPatterns + 1 ; ++i )
00241 {
00242 UString substring = lastString.substr( lastOvector[2*i], lastOvector[2*i+1] - lastOvector[2*i] );
00243 list.append(String(substring));
00244 }
00245 Object arr = exec->lexicalInterpreter()->builtinArray().construct(exec, list);
00246 arr.put(exec, "index", Number(lastOvector[0]));
00247 arr.put(exec, "input", String(lastString));
00248 return arr;
00249 }
00250
00251 Value RegExpObjectImp::get(ExecState *exec, const Identifier &p) const
00252 {
00253 UString s = p.ustring();
00254 if (s[0] == '$' && lastOvector)
00255 {
00256 bool ok;
00257 unsigned long i = s.substr(1).toULong(&ok);
00258 if (ok)
00259 {
00260 if (i < lastNrSubPatterns + 1)
00261 {
00262 UString substring = lastString.substr( lastOvector[2*i], lastOvector[2*i+1] - lastOvector[2*i] );
00263 return String(substring);
00264 }
00265 return String("");
00266 }
00267 }
00268 return InternalFunctionImp::get(exec, p);
00269 }
00270
00271 bool RegExpObjectImp::implementsConstruct() const
00272 {
00273 return true;
00274 }
00275
00276 RegExp* RegExpObjectImp::makeEngine(ExecState *exec, const UString &p, const Value &flagsInput)
00277 {
00278 UString flags = flagsInput.type() == UndefinedType ? UString("") : flagsInput.toString(exec);
00279
00280
00281 for (int pos = 0; pos < flags.size(); ++pos) {
00282 switch (flags[pos].unicode()) {
00283 case 'g':
00284 case 'i':
00285 case 'm':
00286 break;
00287 default: {
00288 Object err = Error::create(exec, SyntaxError,
00289 "Invalid regular expression flags");
00290 exec->setException(err);
00291 return 0;
00292 }
00293 }
00294 }
00295
00296 bool global = (flags.find("g") >= 0);
00297 bool ignoreCase = (flags.find("i") >= 0);
00298 bool multiline = (flags.find("m") >= 0);
00299
00300 int reflags = RegExp::None;
00301 if (global)
00302 reflags |= RegExp::Global;
00303 if (ignoreCase)
00304 reflags |= RegExp::IgnoreCase;
00305 if (multiline)
00306 reflags |= RegExp::Multiline;
00307
00308 RegExp *re = new RegExp(p, reflags);
00309 if (!re->isValid()) {
00310 Object err = Error::create(exec, SyntaxError,
00311 "Invalid regular expression");
00312 exec->setException(err);
00313 delete re;
00314 return 0;
00315 }
00316 return re;
00317 }
00318
00319
00320 Object RegExpObjectImp::construct(ExecState *exec, const List &args)
00321 {
00322 UString p;
00323 if (args.isEmpty()) {
00324 p = "";
00325 } else {
00326 Value a0 = args[0];
00327 if (a0.isA(ObjectType) && a0.toObject(exec).inherits(&RegExpImp::info)) {
00328
00329 if (args.size() > 1 && args[1].type() != UndefinedType) {
00330 Object err = Error::create(exec,TypeError);
00331 exec->setException(err);
00332 return err;
00333 }
00334 RegExpImp *rimp = static_cast<RegExpImp*>(Object::dynamicCast(a0).imp());
00335 p = rimp->regExp()->pattern();
00336 } else {
00337 p = a0.toString(exec);
00338 }
00339 }
00340
00341 RegExp* re = makeEngine(exec, p, args[1]);
00342 if (!re)
00343 return exec->exception().toObject(exec);
00344
00345 RegExpPrototypeImp *proto = static_cast<RegExpPrototypeImp*>(exec->lexicalInterpreter()->builtinRegExpPrototype().imp());
00346 RegExpImp *dat = new RegExpImp(proto);
00347 Object obj(dat);
00348 dat->setRegExp(re);
00349
00350 return obj;
00351 }
00352
00353 bool RegExpObjectImp::implementsCall() const
00354 {
00355 return true;
00356 }
00357
00358
00359 Value RegExpObjectImp::call(ExecState *exec, Object &,
00360 const List &args)
00361 {
00362
00363
00364 return construct(exec, args);
00365 }