Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qstdweb_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 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
4#ifndef QSTDWEB_P_H
5#define QSTDWEB_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <private/qglobal_p.h>
19#include <QtCore/qglobal.h>
20#include "QtCore/qhash.h"
21#include "QtCore/qiodevice.h"
22#include "QtCore/private/qwasmsuspendresumecontrol_p.h"
23
24#include <emscripten/val.h>
25
26#include <cstdint>
27#include <functional>
28#include <initializer_list>
29#include <memory>
30#include <string>
31#include <utility>
32
33#if QT_CONFIG(thread)
34#include <emscripten/proxying.h>
35#include <emscripten/threading.h>
36#endif // #if QT_CONFIG(thread)
37
38#if QT_CONFIG(wasm_jspi)
39# define QT_WASM_EMSCRIPTEN_ASYNC ,emscripten::async()
40#else
41# define QT_WASM_EMSCRIPTEN_ASYNC
42#endif
43
44QT_BEGIN_NAMESPACE
45
46class QMimeData;
47
48namespace qstdweb {
49 extern const char makeContextfulPromiseFunctionName[];
50
51 // DOM API in C++, implemented using emscripten val.h and bind.h.
52 // This is private API and can be extended and changed as needed.
53 // The API mirrors that of the native API, with some extensions
54 // to ease usage from C++ code.
55
56 class ArrayBuffer;
57 class Blob;
58 class File;
59 class FileList;
60 class FileReader;
61 class Uint8Array;
62 class EventCallback;
63
64 class Q_CORE_EXPORT ArrayBuffer {
65 public:
66 explicit ArrayBuffer(uint32_t size);
67 explicit ArrayBuffer(const emscripten::val &arrayBuffer);
68 uint32_t byteLength() const;
70 emscripten::val val() const;
71
72 private:
73 friend class Uint8Array;
75 };
76
77 class Q_CORE_EXPORT Blob {
78 public:
79 explicit Blob(const emscripten::val &blob);
81 uint32_t size() const;
82 static Blob copyFrom(const char *buffer, uint32_t size, std::string mimeType);
83 static Blob copyFrom(const char *buffer, uint32_t size);
86 emscripten::val val() const;
87 std::string type() const;
88
89 private:
90 friend class FileReader;
92 };
93
94 class Q_CORE_EXPORT File {
95 public:
96 File() = default;
97 explicit File(const emscripten::val &file);
98 ~File();
99
100 File(const File &other);
104
106 std::string name() const;
107 uint64_t size() const;
108 std::string type() const;
110 std::function<void()> completed) const;
111 void stream(char *buffer, std::function<void()> completed) const;
112 emscripten::val val() const;
114 const QString &fileUrlPath() const { return m_urlPath; }
115 emscripten::val file() const { return m_file; }
116
117 private:
120 };
121
138
140
141 class Q_CORE_EXPORT FileList {
142 public:
143 FileList() = default;
144 explicit FileList(const emscripten::val &fileList);
145
146 int length() const;
147 File item(int index) const;
148 File operator[](int index) const;
149 emscripten::val val() const;
150
151 private:
153 };
154
155 class Q_CORE_EXPORT FileReader {
156 public:
157 ArrayBuffer result() const;
158 void readAsArrayBuffer(const Blob &blob) const;
159
160 void onLoad(const std::function<void(emscripten::val)> &onLoad);
161 void onError(const std::function<void(emscripten::val)> &onError);
162 void onAbort(const std::function<void(emscripten::val)> &onAbort);
163 emscripten::val val() const;
164
165 private:
166 emscripten::val m_fileReader = emscripten::val::global("FileReader").new_();
170 };
171
172 class Q_CORE_EXPORT Uint8Array {
173 public:
174 explicit Uint8Array(const emscripten::val &uint8Array);
175 explicit Uint8Array(const ArrayBuffer &buffer);
176 explicit Uint8Array(uint32_t size);
178 Uint8Array(const char *buffer, uint32_t size);
179
180 ArrayBuffer buffer() const;
181 uint32_t length() const;
182 void set(const Uint8Array &source);
184
185 void copyTo(char *destination) const;
187
188 static void copy(char *destination, const Uint8Array &source);
189 static Uint8Array copyFrom(const char *buffer, uint32_t size);
190 static Uint8Array copyFrom(const QByteArray &buffer);
191 emscripten::val val() const;
192
193 private:
194 static emscripten::val constructor_();
196 };
197
198 class Q_CORE_EXPORT FileSystemWritableFileStream {
199 public:
202 emscripten::val val() const;
203
204 private:
206 };
207
208 class Q_CORE_EXPORT FileSystemFileHandle {
209 public:
212
213 std::string name() const;
214 std::string kind() const;
215
216 emscripten::val val() const;
217
218 private:
220 };
221
222 // EventCallback here for source compatibility; prefer using QWasmEventHandler directly
223 class Q_CORE_EXPORT EventCallback : public QWasmEventHandler
224 {
225 public:
226 EventCallback() = default;
227 EventCallback(EventCallback const&) = delete;
230 const std::function<void(emscripten::val)> &fn);
231 };
232
239
240 namespace Promise {
242
243 template<typename... Args>
244 uint32_t make(emscripten::val target,
245 QString methodName,
246 PromiseCallbacks callbacks,
247 Args... args)
248 {
249 emscripten::val promiseObject = target.call<emscripten::val>(
250 methodName.toStdString().c_str(), std::forward<Args>(args)...);
251 if (promiseObject.isUndefined() || promiseObject["constructor"]["name"].as<std::string>() != "Promise") {
252 qFatal("This function did not return a promise");
253 }
254
255 return adoptPromise(std::move(promiseObject), std::move(callbacks));
256 }
257
258 void Q_CORE_EXPORT suspendExclusive(uint32_t handlerIndex);
260 };
261
262 template<class F>
263 decltype(auto) bindForever(F wrappedCallback)
264 {
265 return wrappedCallback;
266 }
267
268 class Q_CORE_EXPORT BlobIODevice: public QIODevice
269 {
270 public:
273 bool isSequential() const override;
274 qint64 size() const override;
275 bool seek(qint64 pos) override;
276
277 protected:
279 qint64 writeData(const char *, qint64) override;
280
281 private:
282 Blob m_blob;
283 };
284
286 {
287 public:
288 Uint8ArrayIODevice(Uint8Array array);
289 bool open(QIODevice::OpenMode mode) override;
290 bool isSequential() const override;
291 qint64 size() const override;
292 bool seek(qint64 pos) override;
293
294 protected:
295 qint64 readData(char *data, qint64 maxSize) override;
296 qint64 writeData(const char *data, qint64 size) override;
297
298 private:
299 Uint8Array m_array;
300 };
301
303 {
304 public:
307 void close() override;
308 bool isSequential() const override;
309 qint64 size() const override;
310 bool seek(qint64 pos) override;
311
312 protected:
314 qint64 writeData(const char *data, qint64 size) override;
315
316 private:
318 qint64 m_size = 0;
319 };
320
341
343 {
344 static emscripten::val savedWindow = emscripten::val::global("window");
345 return savedWindow;
346 }
347
348 bool Q_CORE_EXPORT haveAsyncify();
349 bool Q_CORE_EXPORT haveJspi();
351}
352
353QT_END_NAMESPACE
354
355#endif
qint64 size() const override
For open random-access devices, this function returns the size of the device.
Definition qstdweb.cpp:609
bool seek(qint64 pos) override
For random-access devices, this function sets the current position to pos, returning true on success,...
Definition qstdweb.cpp:614
Uint8ArrayIODevice(Uint8Array array)
Definition qstdweb.cpp:593
bool isSequential() const override
Returns true if this device is sequential; otherwise returns false.
Definition qstdweb.cpp:604
qint64 writeData(const char *data, qint64 size) override
Writes up to maxSize bytes from data to the device.
Definition qstdweb.cpp:631
bool open(QIODevice::OpenMode mode) override
Definition qstdweb.cpp:599
qint64 readData(char *data, qint64 maxSize) override
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an...
Definition qstdweb.cpp:621
uint32_t make(emscripten::val target, QString methodName, PromiseCallbacks callbacks, Args... args)
Definition qstdweb_p.h:244
bool canBlockCallingThread()
Definition qstdweb.cpp:541
emscripten::val window()
Definition qstdweb_p.h:342
double uint53_t
Definition qstdweb.cpp:46
bool haveAsyncify()
Definition qstdweb.cpp:535
bool haveJspi()
Definition qstdweb.cpp:529
const char makeContextfulPromiseFunctionName[]
static void usePotentialyUnusedSymbols()
Definition qstdweb.cpp:31
decltype(auto) bindForever(F wrappedCallback)
Definition qstdweb_p.h:263
std::function< void(emscripten::val)> thenFunc
Definition qstdweb_p.h:235
std::function< void()> finallyFunc
Definition qstdweb_p.h:237
std::function< void(emscripten::val)> catchFunc
Definition qstdweb_p.h:236