RegExpPrototype.cpp 6.06 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
/*
 *  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"
25
#include "Error.h"
26
#include "JSArray.h"
oliver@apple.com's avatar
oliver@apple.com committed
27
#include "JSFunction.h"
28 29
#include "JSObject.h"
#include "JSString.h"
30
#include "JSStringBuilder.h"
31 32 33
#include "JSValue.h"
#include "ObjectPrototype.h"
#include "RegExpObject.h"
34
#include "RegExp.h"
35
#include "RegExpCache.h"
36
#include "StringRecursionChecker.h"
37
#include "UStringConcatenate.h"
38

39
namespace JSC {
40

41 42 43 44
static EncodedJSValue JSC_HOST_CALL regExpProtoFuncTest(ExecState*);
static EncodedJSValue JSC_HOST_CALL regExpProtoFuncExec(ExecState*);
static EncodedJSValue JSC_HOST_CALL regExpProtoFuncCompile(ExecState*);
static EncodedJSValue JSC_HOST_CALL regExpProtoFuncToString(ExecState*);
45

46 47 48 49 50 51
}

#include "RegExpPrototype.lut.h"

namespace JSC {

52
const ClassInfo RegExpPrototype::s_info = { "RegExp", &RegExpObject::s_info, 0, ExecState::regExpPrototypeTable, CREATE_METHOD_TABLE(RegExpPrototype) };
53 54 55 56 57 58 59 60 61

/* Source for RegExpPrototype.lut.h
@begin regExpPrototypeTable
  compile   regExpProtoFuncCompile      DontEnum|Function 2
  exec      regExpProtoFuncExec         DontEnum|Function 1
  test      regExpProtoFuncTest         DontEnum|Function 1
  toString  regExpProtoFuncToString     DontEnum|Function 0
@end
*/
62

63 64
ASSERT_CLASS_FITS_IN_CELL(RegExpPrototype);

65
RegExpPrototype::RegExpPrototype(JSGlobalObject* globalObject, Structure* structure, RegExp* regExp)
66
    : RegExpObject(globalObject, structure, regExp)
67
{
68 69
}

70 71
bool RegExpPrototype::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
{
72
    return getStaticFunctionSlot<RegExpObject>(exec, ExecState::regExpPrototypeTable(exec), jsCast<RegExpPrototype*>(cell), propertyName, slot);
73 74
}

75
bool RegExpPrototype::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
76
{
77
    return getStaticFunctionDescriptor<RegExpObject>(exec, ExecState::regExpPrototypeTable(exec), jsCast<RegExpPrototype*>(object), propertyName, descriptor);
78 79 80
}

// ------------------------------ Functions ---------------------------
81 82

EncodedJSValue JSC_HOST_CALL regExpProtoFuncTest(ExecState* exec)
83
{
84
    JSValue thisValue = exec->hostThisValue();
85
    if (!thisValue.inherits(&RegExpObject::s_info))
86
        return throwVMTypeError(exec);
87
    return JSValue::encode(asRegExpObject(thisValue)->test(exec));
88 89
}

90
EncodedJSValue JSC_HOST_CALL regExpProtoFuncExec(ExecState* exec)
91
{
92
    JSValue thisValue = exec->hostThisValue();
93
    if (!thisValue.inherits(&RegExpObject::s_info))
94
        return throwVMTypeError(exec);
95
    return JSValue::encode(asRegExpObject(thisValue)->exec(exec));
96 97
}

98
EncodedJSValue JSC_HOST_CALL regExpProtoFuncCompile(ExecState* exec)
99
{
100
    JSValue thisValue = exec->hostThisValue();
101
    if (!thisValue.inherits(&RegExpObject::s_info))
102
        return throwVMTypeError(exec);
103

104
    RegExp* regExp;
105 106
    JSValue arg0 = exec->argument(0);
    JSValue arg1 = exec->argument(1);
107
    
108
    if (arg0.inherits(&RegExpObject::s_info)) {
weinig@apple.com's avatar
weinig@apple.com committed
109
        if (!arg1.isUndefined())
110
            return throwVMError(exec, createTypeError(exec, "Cannot supply flags when constructing one RegExp from another."));
darin@apple.com's avatar
darin@apple.com committed
111
        regExp = asRegExpObject(arg0)->regExp();
112
    } else {
113
        UString pattern = !exec->argumentCount() ? UString("") : arg0.toString(exec)->value(exec);
114 115 116 117 118
        if (exec->hadException())
            return JSValue::encode(jsUndefined());

        RegExpFlags flags = NoFlags;
        if (!arg1.isUndefined()) {
119
            flags = regExpFlags(arg1.toString(exec)->value(exec));
120 121 122 123 124
            if (exec->hadException())
                return JSValue::encode(jsUndefined());
            if (flags == InvalidFlags)
                return throwVMError(exec, createSyntaxError(exec, "Invalid flags supplied to RegExp constructor."));
        }
125
        regExp = RegExp::create(exec->globalData(), pattern, flags);
126 127 128
    }

    if (!regExp->isValid())
129
        return throwVMError(exec, createSyntaxError(exec, regExp->errorMessage()));
130

131
    asRegExpObject(thisValue)->setRegExp(exec->globalData(), regExp);
132
    asRegExpObject(thisValue)->setLastIndex(exec, 0);
133
    return JSValue::encode(jsUndefined());
134 135
}

136
EncodedJSValue JSC_HOST_CALL regExpProtoFuncToString(ExecState* exec)
137
{
138
    JSValue thisValue = exec->hostThisValue();
139
    if (!thisValue.inherits(&RegExpObject::s_info))
140
        return throwVMTypeError(exec);
141

142 143 144
    RegExpObject* thisObject = asRegExpObject(thisValue);

    StringRecursionChecker checker(exec, thisObject);
145 146
    if (JSValue earlyReturnValue = checker.earlyReturnValue())
        return JSValue::encode(earlyReturnValue);
147

148 149
    char postfix[5] = { '/', 0, 0, 0, 0 };
    int index = 1;
150
    if (thisObject->get(exec, exec->propertyNames().global).toBoolean(exec))
151
        postfix[index++] = 'g';
152
    if (thisObject->get(exec, exec->propertyNames().ignoreCase).toBoolean(exec))
153
        postfix[index++] = 'i';
154
    if (thisObject->get(exec, exec->propertyNames().multiline).toBoolean(exec))
155
        postfix[index] = 'm';
156
    UString source = thisObject->get(exec, exec->propertyNames().source).toString(exec)->value(exec);
157
    // If source is empty, use "/(?:)/" to avoid colliding with comment syntax
158
    return JSValue::encode(jsMakeNontrivialString(exec, "/", source.length() ? source : UString("(?:)"), postfix));
159 160
}

161
} // namespace JSC