Changeset 34863 in webkit for trunk/JavaScriptCore/kjs/RegExpConstructor.cpp
- Timestamp:
- Jun 28, 2008, 5:09:26 PM (17 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/RegExpConstructor.cpp
r34860 r34863 20 20 21 21 #include "config.h" 22 #include "RegExp Object.h"23 #include "RegExp Object.lut.h"22 #include "RegExpConstructor.h" 23 #include "RegExpConstructor.lut.h" 24 24 25 25 #include "ArrayPrototype.h" 26 26 #include "JSArray.h" 27 #include "JSObject.h"28 27 #include "JSString.h" 29 #include "JSValue.h"30 28 #include "ObjectPrototype.h" 31 #include " UnusedParam.h"29 #include "RegExpPrototype.h" 32 30 #include "error_object.h" 33 #include "operations.h"34 31 #include "regexp.h" 35 32 36 #include <stdio.h>37 38 33 namespace KJS { 39 34 40 // ------------------------------ RegExpPrototype ---------------------------41 42 static JSValue* regExpProtoFuncTest(ExecState*, JSObject*, JSValue*, const ArgList&);43 static JSValue* regExpProtoFuncExec(ExecState*, JSObject*, JSValue*, const ArgList&);44 static JSValue* regExpProtoFuncCompile(ExecState*, JSObject*, JSValue*, const ArgList&);45 static JSValue* regExpProtoFuncToString(ExecState*, JSObject*, JSValue*, const ArgList&);46 47 // ECMA 15.10.548 49 const ClassInfo RegExpPrototype::info = { "RegExpPrototype", 0, 0, 0 };50 51 RegExpPrototype::RegExpPrototype(ExecState* exec, ObjectPrototype* objectPrototype, FunctionPrototype* functionPrototype)52 : JSObject(objectPrototype)53 {54 putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().compile, regExpProtoFuncCompile), DontEnum);55 putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().exec, regExpProtoFuncExec), DontEnum);56 putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().test, regExpProtoFuncTest), DontEnum);57 putDirectFunction(new (exec) PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().toString, regExpProtoFuncToString), DontEnum);58 }59 60 // ------------------------------ Functions ---------------------------61 62 JSValue* regExpProtoFuncTest(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)63 {64 if (!thisValue->isObject(&RegExpObject::info))65 return throwError(exec, TypeError);66 return static_cast<RegExpObject*>(thisValue)->test(exec, args);67 }68 69 JSValue* regExpProtoFuncExec(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)70 {71 if (!thisValue->isObject(&RegExpObject::info))72 return throwError(exec, TypeError);73 return static_cast<RegExpObject*>(thisValue)->exec(exec, args);74 }75 76 JSValue* regExpProtoFuncCompile(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)77 {78 if (!thisValue->isObject(&RegExpObject::info))79 return throwError(exec, TypeError);80 81 RefPtr<RegExp> regExp;82 JSValue* arg0 = args[0];83 JSValue* arg1 = args[1];84 85 if (arg0->isObject(&RegExpObject::info)) {86 if (!arg1->isUndefined())87 return throwError(exec, TypeError, "Cannot supply flags when constructing one RegExp from another.");88 regExp = static_cast<RegExpObject*>(arg0)->regExp();89 } else {90 UString pattern = args.isEmpty() ? UString("") : arg0->toString(exec);91 UString flags = arg1->isUndefined() ? UString("") : arg1->toString(exec);92 regExp = RegExp::create(pattern, flags);93 }94 95 if (!regExp->isValid())96 return throwError(exec, SyntaxError, UString("Invalid regular expression: ").append(regExp->errorMessage()));97 98 static_cast<RegExpObject*>(thisValue)->setRegExp(regExp.release());99 static_cast<RegExpObject*>(thisValue)->setLastIndex(0);100 return jsUndefined();101 }102 103 JSValue* regExpProtoFuncToString(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList&)104 {105 if (!thisValue->isObject(&RegExpObject::info)) {106 if (thisValue->isObject(&RegExpPrototype::info))107 return jsString(exec, "//");108 return throwError(exec, TypeError);109 }110 111 UString result = "/" + static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().source)->toString(exec) + "/";112 if (static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().global)->toBoolean(exec))113 result += "g";114 if (static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().ignoreCase)->toBoolean(exec))115 result += "i";116 if (static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().multiline)->toBoolean(exec))117 result += "m";118 return jsString(exec, result);119 }120 121 // ------------------------------ RegExpObject ------------------------------------122 123 const ClassInfo RegExpObject::info = { "RegExp", 0, 0, ExecState::regExpTable };124 125 /* Source for RegExpObject.lut.h126 @begin regExpTable127 global RegExpObject::Global DontDelete|ReadOnly|DontEnum128 ignoreCase RegExpObject::IgnoreCase DontDelete|ReadOnly|DontEnum129 multiline RegExpObject::Multiline DontDelete|ReadOnly|DontEnum130 source RegExpObject::Source DontDelete|ReadOnly|DontEnum131 lastIndex RegExpObject::LastIndex DontDelete|DontEnum132 @end133 */134 135 RegExpObject::RegExpObject(RegExpPrototype* regexpProto, PassRefPtr<RegExp> regExp)136 : JSObject(regexpProto)137 , m_regExp(regExp)138 , m_lastIndex(0)139 {140 }141 142 RegExpObject::~RegExpObject()143 {144 }145 146 bool RegExpObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)147 {148 return getStaticValueSlot<RegExpObject, JSObject>(exec, ExecState::regExpTable(exec), this, propertyName, slot);149 }150 151 JSValue* RegExpObject::getValueProperty(ExecState* exec, int token) const152 {153 switch (token) {154 case Global:155 return jsBoolean(m_regExp->global());156 case IgnoreCase:157 return jsBoolean(m_regExp->ignoreCase());158 case Multiline:159 return jsBoolean(m_regExp->multiline());160 case Source:161 return jsString(exec, m_regExp->pattern());162 case LastIndex:163 return jsNumber(exec, m_lastIndex);164 }165 166 ASSERT_NOT_REACHED();167 return 0;168 }169 170 void RegExpObject::put(ExecState* exec, const Identifier& propertyName, JSValue* value)171 {172 lookupPut<RegExpObject, JSObject>(exec, propertyName, value, ExecState::regExpTable(exec), this);173 }174 175 void RegExpObject::putValueProperty(ExecState* exec, int token, JSValue* value)176 {177 UNUSED_PARAM(token);178 ASSERT(token == LastIndex);179 m_lastIndex = value->toInteger(exec);180 }181 182 bool RegExpObject::match(ExecState* exec, const ArgList& args)183 {184 RegExpConstructor* regExpObj = exec->lexicalGlobalObject()->regExpConstructor();185 186 UString input;187 if (!args.isEmpty())188 input = args[0]->toString(exec);189 else {190 input = regExpObj->input();191 if (input.isNull()) {192 throwError(exec, GeneralError, "No input.");193 return false;194 }195 }196 197 bool global = get(exec, exec->propertyNames().global)->toBoolean(exec);198 int lastIndex = 0;199 if (global) {200 if (m_lastIndex < 0 || m_lastIndex > input.size()) {201 m_lastIndex = 0;202 return false;203 }204 lastIndex = static_cast<int>(m_lastIndex);205 }206 207 int foundIndex;208 int foundLength;209 regExpObj->performMatch(m_regExp.get(), input, lastIndex, foundIndex, foundLength);210 211 if (global) {212 lastIndex = foundIndex < 0 ? 0 : foundIndex + foundLength;213 m_lastIndex = lastIndex;214 }215 216 return foundIndex >= 0;217 }218 219 JSValue* RegExpObject::test(ExecState* exec, const ArgList& args)220 {221 return jsBoolean(match(exec, args));222 }223 224 JSValue* RegExpObject::exec(ExecState* exec, const ArgList& args)225 {226 return match(exec, args)227 ? exec->lexicalGlobalObject()->regExpConstructor()->arrayOfMatches(exec)228 : jsNull();229 }230 231 static JSValue* callRegExpObject(ExecState* exec, JSObject* function, JSValue*, const ArgList& args)232 {233 return static_cast<RegExpObject*>(function)->exec(exec, args);234 }235 236 CallType RegExpObject::getCallData(CallData& callData)237 {238 callData.native.function = callRegExpObject;239 return CallTypeNative;240 }241 242 // ------------------------------ RegExpConstructor ------------------------------243 244 35 const ClassInfo RegExpConstructor::info = { "Function", &InternalFunction::info, 0, ExecState::regExpConstructorTable }; 245 36 246 /* Source for RegExp Object.lut.h37 /* Source for RegExpConstructor.lut.h 247 38 @begin regExpConstructorTable 248 39 input RegExpConstructor::Input None … … 522 313 } 523 314 524 } 315 } // namespace KJS
Note:
See TracChangeset
for help on using the changeset viewer.