| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the QtQml module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 | #ifndef QV4REGEXPOBJECT_H | 
| 40 | #define QV4REGEXPOBJECT_H | 
| 41 |  | 
| 42 | // | 
| 43 | //  W A R N I N G | 
| 44 | //  ------------- | 
| 45 | // | 
| 46 | // This file is not part of the Qt API.  It exists purely as an | 
| 47 | // implementation detail.  This header file may change from version to | 
| 48 | // version without notice, or even be removed. | 
| 49 | // | 
| 50 | // We mean it. | 
| 51 | // | 
| 52 |  | 
| 53 | #include "qv4runtime_p.h" | 
| 54 | #include "qv4engine_p.h" | 
| 55 | #include "qv4context_p.h" | 
| 56 | #include "qv4functionobject_p.h" | 
| 57 | #include "qv4string_p.h" | 
| 58 | #include "qv4managed_p.h" | 
| 59 | #include "qv4property_p.h" | 
| 60 | #include "qv4objectiterator_p.h" | 
| 61 | #include "qv4regexp_p.h" | 
| 62 |  | 
| 63 | #include <QtCore/QString> | 
| 64 | #include <QtCore/QHash> | 
| 65 | #include <QtCore/QScopedPointer> | 
| 66 | #include <cstdio> | 
| 67 | #include <cassert> | 
| 68 |  | 
| 69 | QT_BEGIN_NAMESPACE | 
| 70 |  | 
| 71 | namespace QV4 { | 
| 72 |  | 
| 73 | namespace Heap { | 
| 74 |  | 
| 75 | #define RegExpObjectMembers(class, Member) \ | 
| 76 |     Member(class, Pointer, RegExp *, value) | 
| 77 |  | 
| 78 | DECLARE_HEAP_OBJECT(RegExpObject, Object) { | 
| 79 |     DECLARE_MARKOBJECTS(RegExpObject) | 
| 80 |  | 
| 81 |     void init(); | 
| 82 |     void init(QV4::RegExp *value); | 
| 83 |     void init(const QRegExp &re); | 
| 84 | #if QT_CONFIG(regularexpression) | 
| 85 |     void init(const QRegularExpression &re); | 
| 86 | #endif | 
| 87 | }; | 
| 88 |  | 
| 89 | #define RegExpCtorMembers(class, Member) \ | 
| 90 |     Member(class, HeapValue, HeapValue, lastMatch) \ | 
| 91 |     Member(class, Pointer, String *, lastInput) \ | 
| 92 |     Member(class, NoMark, int, lastMatchStart) \ | 
| 93 |     Member(class, NoMark, int, lastMatchEnd) | 
| 94 |  | 
| 95 | DECLARE_HEAP_OBJECT(RegExpCtor, FunctionObject) { | 
| 96 |     DECLARE_MARKOBJECTS(RegExpCtor); | 
| 97 |  | 
| 98 |     void init(QV4::ExecutionContext *scope); | 
| 99 |     void clearLastMatch(); | 
| 100 | }; | 
| 101 |  | 
| 102 | } | 
| 103 |  | 
| 104 | struct Q_QML_PRIVATE_EXPORT RegExpObject: Object { | 
| 105 |     V4_OBJECT2(RegExpObject, Object) | 
| 106 |     Q_MANAGED_TYPE(RegExpObject) | 
| 107 |     V4_INTERNALCLASS(RegExpObject) | 
| 108 |     V4_PROTOTYPE(regExpPrototype) | 
| 109 |  | 
| 110 |     // needs to be compatible with the flags in qv4compileddata_p.h | 
| 111 |     enum Flags { | 
| 112 |         RegExp_Global     = 0x01, | 
| 113 |         RegExp_IgnoreCase = 0x02, | 
| 114 |         RegExp_Multiline  = 0x04, | 
| 115 |         RegExp_Unicode    = 0x08, | 
| 116 |         RegExp_Sticky     = 0x10 | 
| 117 |     }; | 
| 118 |  | 
| 119 |     enum { | 
| 120 |         Index_LastIndex = 0, | 
| 121 |         Index_ArrayIndex = Heap::ArrayObject::LengthPropertyIndex + 1, | 
| 122 |         Index_ArrayInput = Index_ArrayIndex + 1 | 
| 123 |     }; | 
| 124 |  | 
| 125 |     enum { NInlineProperties = 5 }; | 
| 126 |  | 
| 127 |  | 
| 128 |     void initProperties(); | 
| 129 |  | 
| 130 |     int lastIndex() const { | 
| 131 |         Q_ASSERT(internalClass()->verifyIndex(engine()->id_lastIndex()->propertyKey(), Index_LastIndex)); | 
| 132 |         return propertyData(index: Index_LastIndex)->toInt32(); | 
| 133 |     } | 
| 134 |     void setLastIndex(int index) { | 
| 135 |         Q_ASSERT(internalClass()->verifyIndex(engine()->id_lastIndex()->propertyKey(), Index_LastIndex)); | 
| 136 |         if (!internalClass()->propertyData[Index_LastIndex].isWritable()) { | 
| 137 |             engine()->throwTypeError(); | 
| 138 |             return; | 
| 139 |         } | 
| 140 |         return setProperty(index: Index_LastIndex, v: Value::fromInt32(i: index)); | 
| 141 |     } | 
| 142 |  | 
| 143 |     QRegExp toQRegExp() const; | 
| 144 | #if QT_CONFIG(regularexpression) | 
| 145 |     QRegularExpression toQRegularExpression() const; | 
| 146 | #endif | 
| 147 |     QString toString() const; | 
| 148 |     QString source() const | 
| 149 |     { | 
| 150 |         Scope scope(engine()); | 
| 151 |         ScopedValue s(scope, get(name: scope.engine->id_source())); | 
| 152 |         return s->toQString(); | 
| 153 |     } | 
| 154 |  | 
| 155 |     Heap::RegExp *value() const { return d()->value; } | 
| 156 |     uint flags() const { return d()->value->flags; } | 
| 157 |     bool global() const { return d()->value->global(); } | 
| 158 |     bool sticky() const { return d()->value->sticky(); } | 
| 159 |     bool unicode() const { return d()->value->unicode(); } | 
| 160 |  | 
| 161 |     ReturnedValue builtinExec(ExecutionEngine *engine, const String *s); | 
| 162 | }; | 
| 163 |  | 
| 164 | struct RegExpCtor: FunctionObject | 
| 165 | { | 
| 166 |     V4_OBJECT2(RegExpCtor, FunctionObject) | 
| 167 |  | 
| 168 |     Value lastMatch() { return d()->lastMatch; } | 
| 169 |     Heap::String *lastInput() { return d()->lastInput; } | 
| 170 |     int lastMatchStart() { return d()->lastMatchStart; } | 
| 171 |     int lastMatchEnd() { return d()->lastMatchEnd; } | 
| 172 |  | 
| 173 |     static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *); | 
| 174 |     static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); | 
| 175 | }; | 
| 176 |  | 
| 177 | struct RegExpPrototype: Object | 
| 178 | { | 
| 179 |     void init(ExecutionEngine *engine, Object *ctor); | 
| 180 |  | 
| 181 |     static ReturnedValue method_exec(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 182 |     static ReturnedValue method_get_flags(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 183 |     static ReturnedValue method_get_global(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 184 |     static ReturnedValue method_get_ignoreCase(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 185 |     static ReturnedValue method_match(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 186 |     static ReturnedValue method_get_multiline(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 187 |     static ReturnedValue method_replace(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 188 |     static ReturnedValue method_search(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 189 |     static ReturnedValue method_get_source(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 190 |     static ReturnedValue method_split(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 191 |     static ReturnedValue method_get_sticky(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 192 |     static ReturnedValue method_test(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 193 |     static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 194 |     static ReturnedValue method_get_unicode(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 195 |  | 
| 196 |     // Web extension | 
| 197 |     static ReturnedValue method_compile(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 198 |  | 
| 199 |     // properties on the constructor, web extensions | 
| 200 |     template <uint index> | 
| 201 |     static ReturnedValue method_get_lastMatch_n(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 202 |     static ReturnedValue method_get_lastParen(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 203 |     static ReturnedValue method_get_input(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 204 |     static ReturnedValue method_get_leftContext(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 205 |     static ReturnedValue method_get_rightContext(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); | 
| 206 |  | 
| 207 |     static ReturnedValue execFirstMatch(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); | 
| 208 |  | 
| 209 |     static ReturnedValue exec(ExecutionEngine *engine, const Object *o, const String *s); | 
| 210 | }; | 
| 211 |  | 
| 212 | } | 
| 213 |  | 
| 214 | QT_END_NAMESPACE | 
| 215 |  | 
| 216 | #endif // QMLJS_OBJECTS_H | 
| 217 |  |