Ignore:
Timestamp:
Oct 31, 2007, 1:29:19 AM (18 years ago)
Author:
ap
Message:

Reviewed by Darin.

http://bugs.webkit.org/show_bug.cgi?id=11001
WebKit doesn't support RegExp.compile method

Test: fast/js/regexp-compile.html

  • kjs/regexp_object.cpp: (RegExpPrototype::RegExpPrototype): (RegExpProtoFunc::callAsFunction):
  • kjs/regexp_object.h: (KJS::RegExpProtoFunc::): Added RegExp.compile.
  • tests/mozilla/expected.html: js1_2/regexp/compile.js now passes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/regexp_object.cpp

    r27011 r27303  
    4949  : JSObject(objProto)
    5050{
     51  static const Identifier* compilePropertyName = new Identifier("compile");
    5152  static const Identifier* execPropertyName = new Identifier("exec");
    5253  static const Identifier* testPropertyName = new Identifier("test");
    5354
     55  putDirectFunction(new RegExpProtoFunc(exec, funcProto, RegExpProtoFunc::Compile, 0, *compilePropertyName), DontEnum);
    5456  putDirectFunction(new RegExpProtoFunc(exec, funcProto, RegExpProtoFunc::Exec, 0, *execPropertyName), DontEnum);
    5557  putDirectFunction(new RegExpProtoFunc(exec, funcProto, RegExpProtoFunc::Test, 0, *testPropertyName), DontEnum);
     
    120122  }
    121123  break;
     124  case Compile:
     125  {
     126    UString source;
     127    bool global = false;
     128    bool ignoreCase = false;
     129    bool multiline = false;
     130    if (args.size() > 0) {
     131      if (args[0]->isObject(&RegExpImp::info)) {
     132        if (args.size() != 1)
     133          return throwError(exec, TypeError, "cannot supply flags when constructing one RegExp from another.");
     134
     135        // Flags are mirrored on the JS object and in the implementation, while source is only preserved on the JS object.
     136        RegExp* rhsRegExp = static_cast<RegExpImp*>(args[0])->regExp();
     137        global = rhsRegExp->flags() & RegExp::Global;
     138        ignoreCase = rhsRegExp->flags() & RegExp::IgnoreCase;
     139        multiline = rhsRegExp->flags() & RegExp::Multiline;
     140        source = static_cast<RegExpImp*>(args[0])->get(exec, exec->propertyNames().source)->toString(exec);
     141      } else
     142        source = args[0]->toString(exec);
     143
     144      if (!args[1]->isUndefined()) {
     145        UString flags = args[1]->toString(exec);
     146
     147        global = (flags.find("g") >= 0);
     148        ignoreCase = (flags.find("i") >= 0);
     149        multiline = (flags.find("m") >= 0);
     150      }
     151    }
     152
     153    int reflags = RegExp::None;
     154    if (global)
     155        reflags |= RegExp::Global;
     156    if (ignoreCase)
     157        reflags |= RegExp::IgnoreCase;
     158    if (multiline)
     159        reflags |= RegExp::Multiline;
     160
     161    OwnPtr<RegExp> newRegExp(new RegExp(source, reflags));
     162    if (!newRegExp->isValid())
     163        return throwError(exec, SyntaxError, UString("Invalid regular expression: ").append(newRegExp->errorMessage()));
     164
     165    thisObj->putDirect(exec->propertyNames().global, jsBoolean(global), DontDelete | ReadOnly | DontEnum);
     166    thisObj->putDirect(exec->propertyNames().ignoreCase, jsBoolean(ignoreCase), DontDelete | ReadOnly | DontEnum);
     167    thisObj->putDirect(exec->propertyNames().multiline, jsBoolean(multiline), DontDelete | ReadOnly | DontEnum);
     168    thisObj->putDirect(exec->propertyNames().source, jsString(source), DontDelete | ReadOnly | DontEnum);
     169    thisObj->putDirect(exec->propertyNames().lastIndex, jsNumber(0), DontDelete | DontEnum);
     170
     171    static_cast<RegExpImp*>(thisObj)->setRegExp(newRegExp.release());
     172    return jsUndefined();
     173  }
    122174  case ToString:
    123175    UString result = "/" + thisObj->get(exec, exec->propertyNames().source)->toString(exec) + "/";
Note: See TracChangeset for help on using the changeset viewer.