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 QV4URLOBJECT_P_H
4#define QV4URLOBJECT_P_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 "qv4object_p.h"
18#include "qv4functionobject_p.h"
19
20#include <QtCore/QString>
21#include <QtCore/QUrl>
22
23QT_BEGIN_NAMESPACE
24
25namespace QV4 {
26namespace Heap {
27// clang-format off
28#define UrlObjectMembers(class, Member) \
29 Member(class, Pointer, String *, hash) \
30 Member(class, Pointer, String *, host) \
31 Member(class, Pointer, String *, hostname) \
32 Member(class, Pointer, String *, href) \
33 Member(class, Pointer, String *, origin) \
34 Member(class, Pointer, String *, password) \
35 Member(class, Pointer, String *, pathname) \
36 Member(class, Pointer, String *, port) \
37 Member(class, Pointer, String *, protocol) \
38 Member(class, Pointer, String *, search) \
39 Member(class, Pointer, String *, username)
40// clang-format on
41
42DECLARE_HEAP_OBJECT(UrlObject, Object)
43{
44 DECLARE_MARKOBJECTS(UrlObject)
45 void init() { Object::init(); }
46};
47
48struct UrlCtor : FunctionObject
49{
50 void init(QV4::ExecutionContext *scope);
51};
52
53// clang-format off
54#define UrlSearchParamsObjectMembers(class, Member) \
55 Member(class, Pointer, ArrayObject *, params) \
56 Member(class, Pointer, ArrayObject *, keys) \
57 Member(class, Pointer, ArrayObject *, values) \
58 Member(class, Pointer, UrlObject *, url)
59// clang-format on
60
61DECLARE_HEAP_OBJECT(UrlSearchParamsObject, Object)
62{
63 DECLARE_MARKOBJECTS(UrlSearchParamsObject)
64 void init() { Object::init(); }
65};
66
67struct UrlSearchParamsCtor : FunctionObject
68{
69 void init(QV4::ExecutionContext *scope);
70};
71}
72
73struct UrlObject : Object
74{
75 V4_OBJECT2(UrlObject, Object)
76 Q_MANAGED_TYPE(UrlObject)
77 V4_PROTOTYPE(urlPrototype)
78
79 QString hash() const { return QLatin1String("#") + toQString(string: d()->hash); }
80 bool setHash(QString hash);
81
82 QString host() const { return toQString(string: d()->host); }
83 bool setHost(QString host);
84
85 QString hostname() const { return toQString(string: d()->hostname); }
86 bool setHostname(QString hostname);
87
88 QString href() const { return toQString(string: d()->href); }
89 bool setHref(QString href);
90
91 QString origin() const { return toQString(string: d()->origin); }
92
93 QString password() const { return toQString(string: d()->password); }
94 bool setPassword(QString password);
95
96 QString pathname() const { return toQString(string: d()->pathname); }
97 bool setPathname(QString pathname);
98
99 QString port() const { return toQString(string: d()->port); }
100 bool setPort(QString port);
101
102 QString protocol() const { return toQString(string: d()->protocol); }
103 bool setProtocol(QString protocol);
104
105 Q_QML_AUTOTEST_EXPORT QString search() const;
106 bool setSearch(QString search);
107
108 QString username() const { return toQString(string: d()->username); }
109 bool setUsername(QString username);
110
111 QUrl toQUrl() const;
112 void setUrl(const QUrl &url);
113
114private:
115 static QString toQString(const Heap::String *string)
116 {
117 return string ? string->toQString() : QString();
118 }
119
120 void updateOrigin();
121 void updateHost();
122};
123
124template<>
125inline const UrlObject *Value::as() const
126{
127 return isManaged() && m()->internalClass->vtable->type == Managed::Type_UrlObject
128 ? static_cast<const UrlObject *>(this)
129 : nullptr;
130}
131
132struct UrlCtor : FunctionObject
133{
134 V4_OBJECT2(UrlCtor, FunctionObject)
135
136 static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv,
137 int argc, const Value *);
138};
139
140struct UrlPrototype : Object
141{
142 V4_PROTOTYPE(objectPrototype)
143
144 void init(ExecutionEngine *engine, Object *ctor);
145
146 static ReturnedValue method_getHash(const FunctionObject *, const Value *thisObject,
147 const Value *argv, int argc);
148 static ReturnedValue method_setHash(const FunctionObject *, const Value *thisObject,
149 const Value *argv, int argc);
150
151 static ReturnedValue method_getHost(const FunctionObject *, const Value *thisObject,
152 const Value *argv, int argc);
153 static ReturnedValue method_setHost(const FunctionObject *, const Value *thisObject,
154 const Value *argv, int argc);
155
156 static ReturnedValue method_getHostname(const FunctionObject *, const Value *thisObject,
157 const Value *argv, int argc);
158 static ReturnedValue method_setHostname(const FunctionObject *, const Value *thisObject,
159 const Value *argv, int argc);
160
161 static ReturnedValue method_getHref(const FunctionObject *, const Value *thisObject,
162 const Value *argv, int argc);
163 static ReturnedValue method_setHref(const FunctionObject *, const Value *thisObject,
164 const Value *argv, int argc);
165
166 static ReturnedValue method_getOrigin(const FunctionObject *, const Value *thisObject,
167 const Value *argv, int argc);
168
169 static ReturnedValue method_getPassword(const FunctionObject *, const Value *thisObject,
170 const Value *argv, int argc);
171 static ReturnedValue method_setPassword(const FunctionObject *, const Value *thisObject,
172 const Value *argv, int argc);
173
174 static ReturnedValue method_getPathname(const FunctionObject *, const Value *thisObject,
175 const Value *argv, int argc);
176 static ReturnedValue method_setPathname(const FunctionObject *, const Value *thisObject,
177 const Value *argv, int argc);
178
179 static ReturnedValue method_getPort(const FunctionObject *, const Value *thisObject,
180 const Value *argv, int argc);
181 static ReturnedValue method_setPort(const FunctionObject *, const Value *thisObject,
182 const Value *argv, int argc);
183
184 static ReturnedValue method_getProtocol(const FunctionObject *, const Value *thisObject,
185 const Value *argv, int argc);
186 static ReturnedValue method_setProtocol(const FunctionObject *, const Value *thisObject,
187 const Value *argv, int argc);
188
189 static ReturnedValue method_getSearch(const FunctionObject *, const Value *thisObject,
190 const Value *argv, int argc);
191 static ReturnedValue method_setSearch(const FunctionObject *, const Value *thisObject,
192 const Value *argv, int argc);
193
194 static ReturnedValue method_getUsername(const FunctionObject *, const Value *thisObject,
195 const Value *argv, int argc);
196 static ReturnedValue method_setUsername(const FunctionObject *, const Value *thisObject,
197 const Value *argv, int argc);
198
199 static ReturnedValue method_getSearchParams(const FunctionObject *, const Value *thisObject,
200 const Value *argv, int argc);
201};
202
203struct UrlSearchParamsObject : Object
204{
205 V4_OBJECT2(UrlSearchParamsObject, Object)
206 Q_MANAGED_TYPE(UrlSearchParamsObject)
207 V4_PROTOTYPE(urlSearchParamsPrototype)
208
209 void initializeParams();
210 void initializeParams(QString params);
211 void initializeParams(ScopedArrayObject& params);
212 void initializeParams(ScopedObject& params);
213
214 QList<QStringList> params() const;
215 void setParams(QList<QStringList> params);
216 Heap::UrlObject *urlObject() const;
217 void setUrlObject(const UrlObject *url);
218
219 QString searchString() const;
220
221 QString nameAt(int index) const;
222 Heap::String * nameAtRaw(int index) const;
223 QString valueAt(int index) const;
224 Heap::String * valueAtRaw(int index) const;
225
226 void append(Heap::String *name, Heap::String *value);
227
228 int indexOf(QString name, int last = -1) const;
229 int length() const;
230
231 using Object::getOwnProperty;
232protected:
233 static OwnPropertyKeyIterator *virtualOwnPropertyKeys(const Object *m, Value *target);
234 static PropertyAttributes virtualGetOwnProperty(const Managed *m, PropertyKey id, Property *p);
235private:
236 QString stringAt(int index, int pairIndex) const;
237 Heap::String * stringAtRaw(int index, int pairIndex) const;
238};
239
240template<>
241inline const UrlSearchParamsObject *Value::as() const
242{
243 return isManaged() && m()->internalClass->vtable->type == Managed::Type_UrlSearchParamsObject
244 ? static_cast<const UrlSearchParamsObject *>(this)
245 : nullptr;
246}
247
248struct UrlSearchParamsCtor : FunctionObject
249{
250 V4_OBJECT2(UrlSearchParamsCtor, FunctionObject)
251
252 static ReturnedValue virtualCallAsConstructor(const FunctionObject *, const Value *argv,
253 int argc, const Value *);
254};
255
256struct UrlSearchParamsPrototype : Object
257{
258 V4_PROTOTYPE(objectPrototype)
259
260 void init(ExecutionEngine *engine, Object *ctor);
261
262 static ReturnedValue method_toString(const FunctionObject *, const Value *thisObject,
263 const Value *argv, int argc);
264 static ReturnedValue method_sort(const FunctionObject *, const Value *thisObject,
265 const Value *argv, int argc);
266 static ReturnedValue method_append(const FunctionObject *, const Value *thisObject,
267 const Value *argv, int argc);
268 static ReturnedValue method_delete(const FunctionObject *, const Value *thisObject,
269 const Value *argv, int argc);
270 static ReturnedValue method_has(const FunctionObject *, const Value *thisObject,
271 const Value *argv, int argc);
272 static ReturnedValue method_set(const FunctionObject *, const Value *thisObject,
273 const Value *argv, int argc);
274 static ReturnedValue method_get(const FunctionObject *, const Value *thisObject,
275 const Value *argv, int argc);
276 static ReturnedValue method_getAll(const FunctionObject *, const Value *thisObject,
277 const Value *argv, int argc);
278 static ReturnedValue method_forEach(const FunctionObject *, const Value *thisObject,
279 const Value *argv, int argc);
280 static ReturnedValue method_entries(const FunctionObject *, const Value *thisObject,
281 const Value *argv, int argc);
282 static ReturnedValue method_keys(const FunctionObject *, const Value *thisObject,
283 const Value *argv, int argc);
284 static ReturnedValue method_values(const FunctionObject *, const Value *thisObject,
285 const Value *argv, int argc);
286
287};
288
289}
290
291QT_END_NAMESPACE
292
293#endif // QV4URLOBJECT_P_H
294

source code of qtdeclarative/src/qml/jsruntime/qv4urlobject_p.h