Changeset 29588 in webkit for trunk/JavaScriptCore/kjs/regexp_object.cpp
- Timestamp:
- Jan 17, 2008, 11:27:33 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/regexp_object.cpp
r28476 r29588 1 1 /* 2 2 * Copyright (C) 1999-2000 Harri Porten ([email protected]) 3 * Copyright (C) 2003, 2007 Apple Inc. All Rights Reserved.3 * Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved. 4 4 * 5 5 * This library is free software; you can redistribute it and/or … … 40 40 // ------------------------------ RegExpPrototype --------------------------- 41 41 42 static JSValue* regExpProtoFuncTest(ExecState*, JSObject*, const List&); 43 static JSValue* regExpProtoFuncExec(ExecState*, JSObject*, const List&); 44 static JSValue* regExpProtoFuncCompile(ExecState*, JSObject*, const List&); 45 static JSValue* regExpProtoFuncToString(ExecState*, JSObject*, const List&); 46 42 47 // ECMA 15.10.5 43 48 44 49 const ClassInfo RegExpPrototype::info = { "RegExpPrototype", 0, 0 }; 45 50 46 RegExpPrototype::RegExpPrototype(ExecState *exec, 47 ObjectPrototype *objProto, 48 FunctionPrototype *funcProto) 49 : JSObject(objProto) 50 { 51 static const Identifier* compilePropertyName = new Identifier("compile"); 52 static const Identifier* execPropertyName = new Identifier("exec"); 53 static const Identifier* testPropertyName = new Identifier("test"); 54 55 putDirectFunction(new RegExpProtoFunc(exec, funcProto, RegExpProtoFunc::Compile, 0, *compilePropertyName), DontEnum); 56 putDirectFunction(new RegExpProtoFunc(exec, funcProto, RegExpProtoFunc::Exec, 0, *execPropertyName), DontEnum); 57 putDirectFunction(new RegExpProtoFunc(exec, funcProto, RegExpProtoFunc::Test, 0, *testPropertyName), DontEnum); 58 putDirectFunction(new RegExpProtoFunc(exec, funcProto, RegExpProtoFunc::ToString, 0, exec->propertyNames().toString), DontEnum); 59 } 60 61 // ------------------------------ RegExpProtoFunc --------------------------- 62 63 RegExpProtoFunc::RegExpProtoFunc(ExecState* exec, FunctionPrototype* funcProto, int i, int len, const Identifier& name) 64 : InternalFunctionImp(funcProto, name), id(i) 65 { 66 putDirect(exec->propertyNames().length, len, DontDelete | ReadOnly | DontEnum); 67 } 68 69 JSValue *RegExpProtoFunc::callAsFunction(ExecState *exec, JSObject *thisObj, const List &args) 70 { 71 if (!thisObj->inherits(&RegExpImp::info)) { 72 if (thisObj->inherits(&RegExpPrototype::info)) { 73 switch (id) { 74 case ToString: return jsString("//"); 75 } 76 } 51 RegExpPrototype::RegExpPrototype(ExecState* exec, ObjectPrototype* objectPrototype, FunctionPrototype* functionPrototype) 52 : JSObject(objectPrototype) 53 { 54 static const Identifier* compilePropertyName = new Identifier("compile"); 55 static const Identifier* execPropertyName = new Identifier("exec"); 56 static const Identifier* testPropertyName = new Identifier("test"); 57 58 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 0, *compilePropertyName, regExpProtoFuncCompile), DontEnum); 59 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 0, *execPropertyName, regExpProtoFuncExec), DontEnum); 60 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 0, *testPropertyName, regExpProtoFuncTest), DontEnum); 61 putDirectFunction(new PrototypeFunction(exec, functionPrototype, 0, exec->propertyNames().toString, regExpProtoFuncToString), DontEnum); 62 } 63 64 // ------------------------------ Functions --------------------------- 77 65 78 return throwError(exec, TypeError); 79 } 80 81 switch (id) { 82 case Test: 83 return static_cast<RegExpImp*>(thisObj)->test(exec, args); 84 case Exec: 85 return static_cast<RegExpImp*>(thisObj)->exec(exec, args); 86 case Compile: 87 { 66 JSValue* regExpProtoFuncTest(ExecState* exec, JSObject* thisObj, const List& args) 67 { 68 if (!thisObj->inherits(&RegExpImp::info)) 69 return throwError(exec, TypeError); 70 71 return static_cast<RegExpImp*>(thisObj)->test(exec, args); 72 } 73 74 JSValue* regExpProtoFuncExec(ExecState* exec, JSObject* thisObj, const List& args) 75 { 76 if (!thisObj->inherits(&RegExpImp::info)) 77 return throwError(exec, TypeError); 78 79 return static_cast<RegExpImp*>(thisObj)->exec(exec, args); 80 } 81 82 JSValue* regExpProtoFuncCompile(ExecState* exec, JSObject* thisObj, const List& args) 83 { 84 if (!thisObj->inherits(&RegExpImp::info)) 85 return throwError(exec, TypeError); 86 88 87 RefPtr<RegExp> regExp; 89 88 JSValue* arg0 = args[0]; … … 91 90 92 91 if (arg0->isObject(&RegExpImp::info)) { 93 if (!arg1->isUndefined())94 return throwError(exec, TypeError, "Cannot supply flags when constructing one RegExp from another.");95 regExp = static_cast<RegExpImp*>(arg0)->regExp();92 if (!arg1->isUndefined()) 93 return throwError(exec, TypeError, "Cannot supply flags when constructing one RegExp from another."); 94 regExp = static_cast<RegExpImp*>(arg0)->regExp(); 96 95 } else { 97 UString pattern = args.isEmpty() ? UString("") : arg0->toString(exec);98 UString flags = arg1->isUndefined() ? UString("") : arg1->toString(exec);99 regExp = new RegExp(pattern, flags);96 UString pattern = args.isEmpty() ? UString("") : arg0->toString(exec); 97 UString flags = arg1->isUndefined() ? UString("") : arg1->toString(exec); 98 regExp = new RegExp(pattern, flags); 100 99 } 101 100 102 101 if (!regExp->isValid()) 103 return throwError(exec, SyntaxError, UString("Invalid regular expression: ").append(regExp->errorMessage()));102 return throwError(exec, SyntaxError, UString("Invalid regular expression: ").append(regExp->errorMessage())); 104 103 105 104 static_cast<RegExpImp*>(thisObj)->setRegExp(regExp.release()); 106 105 static_cast<RegExpImp*>(thisObj)->put(exec, exec->propertyNames().lastIndex, jsNumber(0), DontDelete|DontEnum); 107 106 return jsUndefined(); 108 } 109 case ToString: 107 } 108 109 JSValue* regExpProtoFuncToString(ExecState* exec, JSObject* thisObj, const List&) 110 { 111 if (!thisObj->inherits(&RegExpImp::info)) { 112 if (thisObj->inherits(&RegExpPrototype::info)) 113 return jsString("//"); 114 return throwError(exec, TypeError); 115 } 116 110 117 UString result = "/" + thisObj->get(exec, exec->propertyNames().source)->toString(exec) + "/"; 111 if (thisObj->get(exec, exec->propertyNames().global)->toBoolean(exec)) { 112 result += "g"; 113 } 114 if (thisObj->get(exec, exec->propertyNames().ignoreCase)->toBoolean(exec)) { 115 result += "i"; 116 } 117 if (thisObj->get(exec, exec->propertyNames().multiline)->toBoolean(exec)) { 118 result += "m"; 119 } 118 if (thisObj->get(exec, exec->propertyNames().global)->toBoolean(exec)) 119 result += "g"; 120 if (thisObj->get(exec, exec->propertyNames().ignoreCase)->toBoolean(exec)) 121 result += "i"; 122 if (thisObj->get(exec, exec->propertyNames().multiline)->toBoolean(exec)) 123 result += "m"; 120 124 return jsString(result); 121 } 122 123 return jsUndefined(); 125 126 return jsUndefined(); 124 127 } 125 128
Note:
See TracChangeset
for help on using the changeset viewer.