RegExpPrototype.cpp 4.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 *  Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"
#include "RegExpPrototype.h"

#include "ArrayPrototype.h"
#include "JSArray.h"
#include "JSObject.h"
#include "JSString.h"
#include "JSValue.h"
#include "ObjectPrototype.h"
30
#include "PrototypeFunction.h"
31 32 33
#include "RegExpObject.h"
#include "regexp.h"

34
namespace JSC {
35

ggaren@apple.com's avatar
ggaren@apple.com committed
36 37
ASSERT_CLASS_FITS_IN_CELL(RegExpPrototype);

38 39 40 41 42 43 44 45 46
static JSValue* regExpProtoFuncTest(ExecState*, JSObject*, JSValue*, const ArgList&);
static JSValue* regExpProtoFuncExec(ExecState*, JSObject*, JSValue*, const ArgList&);
static JSValue* regExpProtoFuncCompile(ExecState*, JSObject*, JSValue*, const ArgList&);
static JSValue* regExpProtoFuncToString(ExecState*, JSObject*, JSValue*, const ArgList&);

// ECMA 15.10.5

const ClassInfo RegExpPrototype::info = { "RegExpPrototype", 0, 0, 0 };

darin@apple.com's avatar
darin@apple.com committed
47 48
RegExpPrototype::RegExpPrototype(ExecState* exec, PassRefPtr<StructureID> structure, StructureID* prototypeFunctionStructure)
    : JSObject(structure)
49
{
darin@apple.com's avatar
darin@apple.com committed
50 51 52 53
    putDirectFunction(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().compile, regExpProtoFuncCompile), DontEnum);
    putDirectFunction(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().exec, regExpProtoFuncExec), DontEnum);
    putDirectFunction(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().test, regExpProtoFuncTest), DontEnum);
    putDirectFunction(exec, new (exec) PrototypeFunction(exec, prototypeFunctionStructure, 0, exec->propertyNames().toString, regExpProtoFuncToString), DontEnum);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
}

// ------------------------------ Functions ---------------------------
    
JSValue* regExpProtoFuncTest(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
{
    if (!thisValue->isObject(&RegExpObject::info))
        return throwError(exec, TypeError);
    return static_cast<RegExpObject*>(thisValue)->test(exec, args);
}

JSValue* regExpProtoFuncExec(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
{
    if (!thisValue->isObject(&RegExpObject::info))
        return throwError(exec, TypeError);
    return static_cast<RegExpObject*>(thisValue)->exec(exec, args);
}

JSValue* regExpProtoFuncCompile(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList& args)
{
    if (!thisValue->isObject(&RegExpObject::info))
        return throwError(exec, TypeError);

    RefPtr<RegExp> regExp;
ggaren@apple.com's avatar
ggaren@apple.com committed
78 79
    JSValue* arg0 = args.at(exec, 0);
    JSValue* arg1 = args.at(exec, 1);
80 81 82 83 84 85 86 87
    
    if (arg0->isObject(&RegExpObject::info)) {
        if (!arg1->isUndefined())
            return throwError(exec, TypeError, "Cannot supply flags when constructing one RegExp from another.");
        regExp = static_cast<RegExpObject*>(arg0)->regExp();
    } else {
        UString pattern = args.isEmpty() ? UString("") : arg0->toString(exec);
        UString flags = arg1->isUndefined() ? UString("") : arg1->toString(exec);
88
        regExp = RegExp::create(exec, pattern, flags);
89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }

    if (!regExp->isValid())
        return throwError(exec, SyntaxError, UString("Invalid regular expression: ").append(regExp->errorMessage()));

    static_cast<RegExpObject*>(thisValue)->setRegExp(regExp.release());
    static_cast<RegExpObject*>(thisValue)->setLastIndex(0);
    return jsUndefined();
}

JSValue* regExpProtoFuncToString(ExecState* exec, JSObject*, JSValue* thisValue, const ArgList&)
{
    if (!thisValue->isObject(&RegExpObject::info)) {
        if (thisValue->isObject(&RegExpPrototype::info))
darin@apple.com's avatar
darin@apple.com committed
103
            return jsNontrivialString(exec, "//");
104 105 106
        return throwError(exec, TypeError);
    }

darin@apple.com's avatar
darin@apple.com committed
107 108
    UString result = "/" + static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().source)->toString(exec);
    result.append('/');
mjs@apple.com's avatar
mjs@apple.com committed
109
    if (static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().global)->toBoolean(exec))
darin@apple.com's avatar
darin@apple.com committed
110
        result.append('g');
mjs@apple.com's avatar
mjs@apple.com committed
111
    if (static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().ignoreCase)->toBoolean(exec))
darin@apple.com's avatar
darin@apple.com committed
112
        result.append('i');
mjs@apple.com's avatar
mjs@apple.com committed
113
    if (static_cast<RegExpObject*>(thisValue)->get(exec, exec->propertyNames().multiline)->toBoolean(exec))
darin@apple.com's avatar
darin@apple.com committed
114 115
        result.append('m');
    return jsNontrivialString(exec, result);
116 117
}

118
} // namespace JSC