1
/****************************************************************************
3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: Nokia Corporation (qt-info@nokia.com)
6
** This file is part of the QtScript module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
10
** Licensees holding valid Qt Commercial licenses may use this file in
11
** accordance with the Qt Commercial License Agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Nokia.
15
** GNU Lesser General Public License Usage
16
** Alternatively, this file may be used under the terms of the GNU Lesser
17
** General Public License version 2.1 as published by the Free Software
18
** Foundation and appearing in the file LICENSE.LGPL included in the
19
** packaging of this file. Please review the following information to
20
** ensure the GNU Lesser General Public License version 2.1 requirements
21
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23
** In addition, as a special exception, Nokia gives you certain
24
** additional rights. These rights are described in the Nokia Qt LGPL
25
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
28
** GNU General Public License Usage
29
** Alternatively, this file may be used under the terms of the GNU
30
** General Public License version 3.0 as published by the Free Software
31
** Foundation and appearing in the file LICENSE.GPL included in the
32
** packaging of this file. Please review the following information to
33
** ensure the GNU General Public License version 3.0 requirements will be
34
** met: http://www.gnu.org/copyleft/gpl.html.
36
** If you are unsure which license is appropriate for your use, please
37
** contact the sales department at http://www.qtsoftware.com/contact.
40
****************************************************************************/
42
#include "qscriptecmaregexp_p.h"
46
#include "qscriptengine_p.h"
47
#include "qscriptvalueimpl_p.h"
48
#include "qscriptcontext_p.h"
49
#include "qscriptmember_p.h"
50
#include "qscriptobject_p.h"
52
#include <QtCore/QStringList>
53
#include <QtCore/QRegExp>
54
#include <QtCore/QtDebug>
58
namespace QScript { namespace Ecma {
60
RegExp::RegExp(QScriptEnginePrivate *eng):
61
Core(eng, QLatin1String("RegExp"), QScriptClassInfo::RegExpType)
63
newRegExp(&publicPrototype, QString(), /*flags=*/0);
65
eng->newConstructor(&ctor, this, publicPrototype);
67
addPrototypeFunction(QLatin1String("exec"), method_exec, 1);
68
addPrototypeFunction(QLatin1String("test"), method_test, 1);
69
addPrototypeFunction(QLatin1String("toString"), method_toString, 1);
76
RegExp::Instance *RegExp::Instance::get(const QScriptValueImpl &object, QScriptClassInfo *klass)
78
if (! klass || klass == object.classInfo())
79
return static_cast<Instance*> (object.objectData());
84
void RegExp::execute(QScriptContextPrivate *context)
86
#ifndef Q_SCRIPT_NO_EVENT_NOTIFY
87
engine()->notifyFunctionEntry(context);
91
QScriptValueImpl pattern = context->argument(0);
92
QScriptValueImpl flags = context->argument(1);
93
if (!context->isCalledAsConstructor()) {
94
if ((pattern.classInfo() == classInfo()) && flags.isUndefined()) {
95
context->m_result = pattern;
99
if (pattern.classInfo() == classInfo()) {
100
if (!flags.isUndefined()) {
101
context->throwTypeError(QString::fromLatin1("cannot specify flags when creating a copy of a RegExp"));
104
Instance *data = Instance::get(pattern, classInfo());
106
P = data->value.pattern();
112
if (!pattern.isUndefined())
113
P = pattern.toString();
115
if (!flags.isUndefined()) {
116
QString flagsStr = flags.toString();
117
for (int i = 0; i < flagsStr.length(); ++i) {
118
int bitflag = flagFromChar(flagsStr.at(i));
121
QScriptContext::SyntaxError,
122
QString::fromUtf8("invalid regular expression flag '%0'")
123
.arg(flagsStr.at(i)));
130
if (context->isCalledAsConstructor()) {
131
QScriptValueImpl &object = context->m_thisObject;
132
object.setClassInfo(classInfo());
133
object.setPrototype(publicPrototype);
135
initRegExp(&object, toRegExp(P, F), F);
137
initRegExp(&object, P, F);
140
newRegExp(&context->m_result, P, F);
143
#ifndef Q_SCRIPT_NO_EVENT_NOTIFY
144
engine()->notifyFunctionExit(context);
148
void RegExp::newRegExp(QScriptValueImpl *result, const QString &pattern, int flags)
151
QRegExp rx = toRegExp(pattern, flags);
152
newRegExp_helper(result, rx, flags);
154
engine()->newObject(result, publicPrototype, classInfo());
155
initRegExp(result, pattern, flags);
156
#endif // QT_NO_REGEXP
160
void RegExp::newRegExp(QScriptValueImpl *result, const QRegExp &rx, int flags)
162
Q_ASSERT(!(flags & IgnoreCase) || (rx.caseSensitivity() == Qt::CaseInsensitive));
163
newRegExp_helper(result, rx, flags);
166
void RegExp::newRegExp_helper(QScriptValueImpl *result, const QRegExp &rx,
169
engine()->newObject(result, publicPrototype, classInfo());
170
initRegExp(result, rx, flags);
173
QRegExp RegExp::toRegExp(const QScriptValueImpl &value) const
175
Instance *rx_data = Instance::get(value, classInfo());
176
Q_ASSERT(rx_data != 0);
177
return rx_data->value;
180
QRegExp RegExp::toRegExp(const QString &pattern, int flags)
182
bool ignoreCase = (flags & IgnoreCase) != 0;
183
return QRegExp(pattern,
184
(ignoreCase ? Qt::CaseInsensitive: Qt::CaseSensitive),
188
#endif // QT_NO_REGEXP
190
void RegExp::initRegExp(QScriptValueImpl *result,
194
const QString &pattern,
198
Instance *instance = new Instance();
200
instance->value = rx;
202
instance->pattern = pattern;
204
instance->flags = flags;
205
result->setObjectData(instance);
207
bool global = (flags & Global) != 0;
208
bool ignoreCase = (flags & IgnoreCase) != 0;
209
bool multiline = (flags & Multiline) != 0;
211
QScriptValue::PropertyFlags propertyFlags = QScriptValue::SkipInEnumeration
212
| QScriptValue::Undeletable
213
| QScriptValue::ReadOnly;
215
result->setProperty(QLatin1String("global"), QScriptValueImpl(global),
217
result->setProperty(QLatin1String("ignoreCase"), QScriptValueImpl(ignoreCase),
219
result->setProperty(QLatin1String("multiline"), QScriptValueImpl(multiline),
222
const QString &pattern = rx.pattern();
224
result->setProperty(QLatin1String("source"), QScriptValueImpl(engine(), pattern),
226
result->setProperty(QLatin1String("lastIndex"), QScriptValueImpl(0),
227
propertyFlags & ~QScriptValue::ReadOnly);
230
int RegExp::flagFromChar(const QChar &ch)
232
static QHash<QChar, int> flagsHash;
233
if (flagsHash.isEmpty()) {
234
flagsHash[QLatin1Char('g')] = Global;
235
flagsHash[QLatin1Char('i')] = IgnoreCase;
236
flagsHash[QLatin1Char('m')] = Multiline;
238
QHash<QChar, int>::const_iterator it;
239
it = flagsHash.constFind(ch);
240
if (it == flagsHash.constEnd())
245
QString RegExp::flagsToString(int flags)
249
result += QLatin1Char('g');
250
if (flags & IgnoreCase)
251
result += QLatin1Char('i');
252
if (flags & Multiline)
253
result += QLatin1Char('m');
257
QScriptValueImpl RegExp::method_exec(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *classInfo)
259
QScriptValueImpl self = context->thisObject();
260
if (self.classInfo() != classInfo) {
261
return throwThisObjectTypeError(
262
context, QLatin1String("RegExp.prototype.exec"));
264
Instance *rx_data = Instance::get(self, classInfo);
265
Q_ASSERT(rx_data != 0);
267
QString S = context->argument(0).toString();
268
int length = S.length();
269
QScriptValueImpl lastIndex = self.property(QLatin1String("lastIndex"));
271
int i = lastIndex.isValid() ? int (lastIndex.toInteger()) : 0;
272
bool global = self.property(QLatin1String("global")).toBoolean();
277
if (i < 0 || i >= length)
278
return (eng->nullValue());
281
int index = rx_data->value.indexIn(S, i);
283
#endif // QT_NO_REGEXP
284
return eng->nullValue();
287
int e = index + rx_data->value.matchedLength();
290
self.setProperty(QLatin1String("lastIndex"), QScriptValueImpl(e));
292
QScript::Array elts(eng);
293
QStringList capturedTexts = rx_data->value.capturedTexts();
294
for (int i = 0; i < capturedTexts.count(); ++i)
295
elts.assign(i, QScriptValueImpl(eng, capturedTexts.at(i)));
297
QScriptValueImpl r = eng->newArray(elts);
299
r.setProperty(QLatin1String("index"), QScriptValueImpl(index));
300
r.setProperty(QLatin1String("input"), QScriptValueImpl(eng, S));
303
#endif // QT_NO_REGEXP
306
QScriptValueImpl RegExp::method_test(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *classInfo)
308
QScriptValueImpl r = method_exec(context, eng, classInfo);
309
return QScriptValueImpl(!r.isNull());
312
QScriptValueImpl RegExp::method_toString(QScriptContextPrivate *context, QScriptEnginePrivate *eng, QScriptClassInfo *classInfo)
314
if (Instance *instance = Instance::get(context->thisObject(), classInfo)) {
316
result += QLatin1Char('/');
318
const QString &pattern = instance->value.pattern();
320
const QString &pattern = instance->pattern;
322
if (pattern.isEmpty())
323
result += QLatin1String("(?:)");
325
result += pattern; // ### quote
326
result += QLatin1Char('/');
327
result += flagsToString(instance->flags);
328
return (QScriptValueImpl(eng, result));
331
return throwThisObjectTypeError(
332
context, QLatin1String("RegExp.prototype.toString"));
335
} } // namespace QScript::Ecma
339
#endif // QT_NO_SCRIPT