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 // Note: it is ok for the Promise object to go out of scope,
241 // the resources will be cleaned up in the finally handler.
242 class Q_CORE_EXPORT Promise {
243 public:
244 template<typename... Args>
249 if (m_state->m_promise.isUndefined() || m_state->m_promise["constructor"]["name"].as<std::string>() != "Promise") {
250 qFatal("This function did not return a promise");
251 }
252 addFinallyFunction([](){});
253 }
254
258 if (m_state->m_promise.isUndefined() || m_state->m_promise["constructor"]["name"].as<std::string>() != "Promise") {
259 qFatal("This function did not return a promise");
260 }
261 addFinallyFunction([](){});
262 }
263
267 for (const auto &p : promises)
269
270 auto arr = emscripten::val::array(all);
272 m_state->m_promise = emscripten::val::global("Promise").call<emscripten::val>("all", arr);
273 addFinallyFunction([](){});
274 }
275
279
280 void suspendExclusive();
281
282 emscripten::val getPromise() const;
283
284 public:
285 class State {
286 private:
287 friend class Promise;
288
289 State(const State&) = delete;
290 State(State&&) = delete;
291 State& operator=(const State&) = delete;
292 State& operator=(State&&) = delete;
293
294 public:
297 static size_t numInstances() { return s_numInstances; }
298
299 private:
302 static size_t s_numInstances;
303 };
304
305 private:
307
308 public:
309 // Deprecated: To be backwards compatible
311
312 template<typename... Args>
316 Args... args)
317 {
320 if (promiseObject.isUndefined() || promiseObject["constructor"]["name"].as<std::string>() != "Promise") {
321 qFatal("This function did not return a promise");
322 }
323
325 }
326
327 template<typename... Args>
328 static void make(
333 Args... args)
334 {
337 if (promiseObject.isUndefined() || promiseObject["constructor"]["name"].as<std::string>() != "Promise") {
338 qFatal("This function did not return a promise");
339 }
340
342 }
343
346 };
347
348 template<class F>
349 decltype(auto) bindForever(F wrappedCallback)
350 {
351 return wrappedCallback;
352 }
353
354 class Q_CORE_EXPORT BlobIODevice: public QIODevice
355 {
356 public:
359 bool isSequential() const override;
360 qint64 size() const override;
361 bool seek(qint64 pos) override;
362
363 protected:
365 qint64 writeData(const char *, qint64) override;
366
367 private:
368 Blob m_blob;
369 };
370
372 {
373 public:
374 Uint8ArrayIODevice(Uint8Array array);
375 bool open(QIODevice::OpenMode mode) override;
376 bool isSequential() const override;
377 qint64 size() const override;
378 bool seek(qint64 pos) override;
379
380 protected:
381 qint64 readData(char *data, qint64 maxSize) override;
382 qint64 writeData(const char *data, qint64 size) override;
383
384 private:
385 Uint8Array m_array;
386 };
387
389 {
390 public:
393 void close() override;
394 bool isSequential() const override;
395 qint64 size() const override;
396 bool seek(qint64 pos) override;
397
398 protected:
400 qint64 writeData(const char *data, qint64 size) override;
401
402 private:
404 qint64 m_size = 0;
405 };
406
427
429 {
430 static emscripten::val savedWindow = emscripten::val::global("window");
431 return savedWindow;
432 }
433
434 bool Q_CORE_EXPORT haveAsyncify();
435 bool Q_CORE_EXPORT haveJspi();
437}
438
439QT_END_NAMESPACE
440
441#endif
qint64 size() const override
For open random-access devices, this function returns the size of the device.
Definition qstdweb.cpp:734
bool seek(qint64 pos) override
For random-access devices, this function sets the current position to pos, returning true on success,...
Definition qstdweb.cpp:739
Uint8ArrayIODevice(Uint8Array array)
Definition qstdweb.cpp:718
bool isSequential() const override
Returns true if this device is sequential; otherwise returns false.
Definition qstdweb.cpp:729
qint64 writeData(const char *data, qint64 size) override
Writes up to maxSize bytes from data to the device.
Definition qstdweb.cpp:756
bool open(QIODevice::OpenMode mode) override
Definition qstdweb.cpp:724
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:746
bool canBlockCallingThread()
Definition qstdweb.cpp:666
emscripten::val window()
Definition qstdweb_p.h:428
double uint53_t
Definition qstdweb.cpp:46
bool haveAsyncify()
Definition qstdweb.cpp:660
bool haveJspi()
Definition qstdweb.cpp:654
const char makeContextfulPromiseFunctionName[]
static void usePotentialyUnusedSymbols()
Definition qstdweb.cpp:31
decltype(auto) bindForever(F wrappedCallback)
Definition qstdweb_p.h:349
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