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 // EventCallback here for source compatibility; prefer using QWasmEventHandler directly
199 class Q_CORE_EXPORT EventCallback : public QWasmEventHandler
200 {
201 public:
202 EventCallback() = default;
203 EventCallback(EventCallback const&) = delete;
206 const std::function<void(emscripten::val)> &fn);
207 };
208
215
216 namespace Promise {
218
219 template<typename... Args>
220 void make(emscripten::val target,
221 QString methodName,
222 PromiseCallbacks callbacks,
223 Args... args)
224 {
225 emscripten::val promiseObject = target.call<emscripten::val>(
226 methodName.toStdString().c_str(), std::forward<Args>(args)...);
227 if (promiseObject.isUndefined() || promiseObject["constructor"]["name"].as<std::string>() != "Promise") {
228 qFatal("This function did not return a promise");
229 }
230
231 adoptPromise(std::move(promiseObject), std::move(callbacks));
232 }
233
235 };
236
237 template<class F>
238 decltype(auto) bindForever(F wrappedCallback)
239 {
240 return wrappedCallback;
241 }
242
243 class Q_CORE_EXPORT BlobIODevice: public QIODevice
244 {
245 public:
248 bool isSequential() const override;
249 qint64 size() const override;
250 bool seek(qint64 pos) override;
251
252 protected:
254 qint64 writeData(const char *, qint64) override;
255
256 private:
257 Blob m_blob;
258 };
259
261 {
262 public:
263 Uint8ArrayIODevice(Uint8Array array);
264 bool open(QIODevice::OpenMode mode) override;
265 bool isSequential() const override;
266 qint64 size() const override;
267 bool seek(qint64 pos) override;
268
269 protected:
270 qint64 readData(char *data, qint64 maxSize) override;
271 qint64 writeData(const char *data, qint64 size) override;
272
273 private:
274 Uint8Array m_array;
275 };
276
278 {
279 static emscripten::val savedWindow = emscripten::val::global("window");
280 return savedWindow;
281 }
282
283 bool Q_CORE_EXPORT haveAsyncify();
284 bool Q_CORE_EXPORT haveJspi();
286}
287
288QT_END_NAMESPACE
289
290#endif
qint64 size() const override
For open random-access devices, this function returns the size of the device.
Definition qstdweb.cpp:601
bool seek(qint64 pos) override
For random-access devices, this function sets the current position to pos, returning true on success,...
Definition qstdweb.cpp:606
Uint8ArrayIODevice(Uint8Array array)
Definition qstdweb.cpp:585
bool isSequential() const override
Returns true if this device is sequential; otherwise returns false.
Definition qstdweb.cpp:596
qint64 writeData(const char *data, qint64 size) override
Writes up to maxSize bytes from data to the device.
Definition qstdweb.cpp:623
bool open(QIODevice::OpenMode mode) override
Definition qstdweb.cpp:591
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:613
void make(emscripten::val target, QString methodName, PromiseCallbacks callbacks, Args... args)
Definition qstdweb_p.h:220
bool canBlockCallingThread()
Definition qstdweb.cpp:533
emscripten::val window()
Definition qstdweb_p.h:277
double uint53_t
Definition qstdweb.cpp:46
bool haveAsyncify()
Definition qstdweb.cpp:527
bool haveJspi()
Definition qstdweb.cpp:521
const char makeContextfulPromiseFunctionName[]
static void usePotentialyUnusedSymbols()
Definition qstdweb.cpp:31
decltype(auto) bindForever(F wrappedCallback)
Definition qstdweb_p.h:238
std::function< void(emscripten::val)> thenFunc
Definition qstdweb_p.h:211
std::function< void()> finallyFunc
Definition qstdweb_p.h:213
std::function< void(emscripten::val)> catchFunc
Definition qstdweb_p.h:212