1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | #ifndef QV4REGEXPOBJECT_H |
4 | #define QV4REGEXPOBJECT_H |
5 | |
6 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists purely as an |
11 | // implementation detail. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <private/qv4context_p.h> |
18 | #include <private/qv4engine_p.h> |
19 | #include <private/qv4functionobject_p.h> |
20 | #include <private/qv4managed_p.h> |
21 | |
22 | #include <QtCore/qhash.h> |
23 | #include <QtCore/qstring.h> |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | namespace QV4 { |
28 | |
29 | namespace Heap { |
30 | |
31 | #define RegExpObjectMembers(class, Member) \ |
32 | Member(class, Pointer, RegExp *, value) |
33 | |
34 | DECLARE_HEAP_OBJECT(RegExpObject, Object) { |
35 | DECLARE_MARKOBJECTS(RegExpObject) |
36 | |
37 | void init(); |
38 | void init(QV4::RegExp *value); |
39 | #if QT_CONFIG(regularexpression) |
40 | void init(const QRegularExpression &re); |
41 | #endif |
42 | }; |
43 | |
44 | #define RegExpCtorMembers(class, Member) \ |
45 | Member(class, HeapValue, HeapValue, lastMatch) \ |
46 | Member(class, Pointer, String *, lastInput) \ |
47 | Member(class, NoMark, int, lastMatchStart) \ |
48 | Member(class, NoMark, int, lastMatchEnd) |
49 | |
50 | DECLARE_HEAP_OBJECT(RegExpCtor, FunctionObject) { |
51 | DECLARE_MARKOBJECTS(RegExpCtor) |
52 | |
53 | void init(QV4::ExecutionContext *scope); |
54 | void clearLastMatch(); |
55 | }; |
56 | |
57 | } |
58 | |
59 | struct Q_QML_PRIVATE_EXPORT RegExpObject: Object { |
60 | V4_OBJECT2(RegExpObject, Object) |
61 | Q_MANAGED_TYPE(RegExpObject) |
62 | V4_INTERNALCLASS(RegExpObject) |
63 | V4_PROTOTYPE(regExpPrototype) |
64 | |
65 | // needs to be compatible with the flags in qv4compileddata_p.h |
66 | enum Flags { |
67 | RegExp_Global = 0x01, |
68 | RegExp_IgnoreCase = 0x02, |
69 | RegExp_Multiline = 0x04, |
70 | RegExp_Unicode = 0x08, |
71 | RegExp_Sticky = 0x10 |
72 | }; |
73 | |
74 | enum { |
75 | Index_LastIndex = 0, |
76 | Index_ArrayIndex = Heap::ArrayObject::LengthPropertyIndex + 1, |
77 | Index_ArrayInput = Index_ArrayIndex + 1 |
78 | }; |
79 | |
80 | enum { NInlineProperties = 5 }; |
81 | |
82 | |
83 | void initProperties(); |
84 | |
85 | int lastIndex() const { |
86 | Q_ASSERT(internalClass()->verifyIndex(engine()->id_lastIndex()->propertyKey(), Index_LastIndex)); |
87 | return propertyData(index: Index_LastIndex)->toInt32(); |
88 | } |
89 | void setLastIndex(int index) { |
90 | Q_ASSERT(internalClass()->verifyIndex(engine()->id_lastIndex()->propertyKey(), Index_LastIndex)); |
91 | if (!internalClass()->propertyData[Index_LastIndex].isWritable()) { |
92 | engine()->throwTypeError(); |
93 | return; |
94 | } |
95 | return setProperty(index: Index_LastIndex, v: Value::fromInt32(i: index)); |
96 | } |
97 | |
98 | #if QT_CONFIG(regularexpression) |
99 | QRegularExpression toQRegularExpression() const; |
100 | #endif |
101 | QString toString() const; |
102 | QString source() const |
103 | { |
104 | Scope scope(engine()); |
105 | ScopedValue s(scope, get(name: scope.engine->id_source())); |
106 | return s->toQString(); |
107 | } |
108 | |
109 | // We cannot name Heap::RegExp here since we don't want to include qv4regexp_p.h but we still |
110 | // want to keep the methods inline. We shift the requirement to name the type to the caller by |
111 | // making it a template. |
112 | template<typename RegExp = Heap::RegExp> |
113 | RegExp *value() const { return d()->value; } |
114 | template<typename RegExp = Heap::RegExp> |
115 | uint flags() const { return value<RegExp>()->flags; } |
116 | template<typename RegExp = Heap::RegExp> |
117 | bool global() const { return value<RegExp>()->global(); } |
118 | template<typename RegExp = Heap::RegExp> |
119 | bool sticky() const { return value<RegExp>()->sticky(); } |
120 | template<typename RegExp = Heap::RegExp> |
121 | bool unicode() const { return value<RegExp>()->unicode(); } |
122 | |
123 | ReturnedValue builtinExec(ExecutionEngine *engine, const String *s); |
124 | }; |
125 | |
126 | struct RegExpCtor: FunctionObject |
127 | { |
128 | V4_OBJECT2(RegExpCtor, FunctionObject) |
129 | |
130 | Value lastMatch() { return d()->lastMatch; } |
131 | Heap::String *lastInput() { return d()->lastInput; } |
132 | int lastMatchStart() { return d()->lastMatchStart; } |
133 | int lastMatchEnd() { return d()->lastMatchEnd; } |
134 | |
135 | static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *); |
136 | static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); |
137 | }; |
138 | |
139 | struct RegExpPrototype: Object |
140 | { |
141 | void init(ExecutionEngine *engine, Object *ctor); |
142 | |
143 | static ReturnedValue method_exec(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
144 | static ReturnedValue method_get_flags(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
145 | static ReturnedValue method_get_global(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
146 | static ReturnedValue method_get_ignoreCase(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
147 | static ReturnedValue method_match(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
148 | static ReturnedValue method_get_multiline(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
149 | static ReturnedValue method_replace(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
150 | static ReturnedValue method_search(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
151 | static ReturnedValue method_get_source(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
152 | static ReturnedValue method_split(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
153 | static ReturnedValue method_get_sticky(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
154 | static ReturnedValue method_test(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
155 | static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
156 | static ReturnedValue method_get_unicode(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
157 | |
158 | // Web extension |
159 | static ReturnedValue method_compile(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
160 | |
161 | // properties on the constructor, web extensions |
162 | template <uint index> |
163 | static ReturnedValue method_get_lastMatch_n(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
164 | static ReturnedValue method_get_lastParen(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
165 | static ReturnedValue method_get_input(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
166 | static ReturnedValue method_get_leftContext(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
167 | static ReturnedValue method_get_rightContext(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); |
168 | |
169 | static ReturnedValue execFirstMatch(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); |
170 | |
171 | static ReturnedValue exec(ExecutionEngine *engine, const Object *o, const String *s); |
172 | }; |
173 | |
174 | } |
175 | |
176 | QT_END_NAMESPACE |
177 | |
178 | #endif // QMLJS_OBJECTS_H |
179 | |