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.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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// Qt-Security score:critical reason:data-parser
4
5#include "qstdweb_p.h"
8
9#include <QtCore/qcoreapplication.h>
10#include <QtCore/qfile.h>
11#include <QtCore/qmimedata.h>
12
13#include <emscripten/emscripten.h>
14#include <emscripten/bind.h>
15#include <emscripten/val.h>
16#include <emscripten/html5.h>
17#include <emscripten/threading.h>
18
19#include <cstdint>
20#include <iostream>
21
22#include <unordered_map>
23
24QT_BEGIN_NAMESPACE
25
26using namespace Qt::StringLiterals;
27using emscripten::val;
28
29namespace qstdweb {
30
32{
33 // Using this adds a reference on JSEvents and specialHTMLTargets which are always exported.
34 // This hack is needed as it is currently impossible to specify a dollar sign in
35 // target_link_options. The following is impossible:
36 // DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$JSEvents
37 // TODO(mikolajboc): QTBUG-108444, review this when cmake gets fixed.
38 // Volatile is to make this unoptimizable, so that the function is referenced, but is not
39 // called at runtime.
40 volatile bool doIt = false;
41 if (doIt)
42 emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, 0, NULL);
43}
44
45Q_CONSTRUCTOR_FUNCTION(usePotentialyUnusedSymbols)
46typedef double uint53_t; // see Number.MAX_SAFE_INTEGER
47namespace {
48// Reads file in chunks in order to avoid holding two copies in memory at the same time
49struct ChunkedFileReader
50{
51public:
52 static void read(File file, char *buffer, uint32_t offset, uint32_t end,
53 std::function<void()> onCompleted)
54 {
55 (new ChunkedFileReader(end, std::move(onCompleted), std::move(file)))
56 ->readNextChunk(offset, buffer);
57 }
58
59private:
60 ChunkedFileReader(uint32_t end, std::function<void()> onCompleted, File file)
61 : end(end), onCompleted(std::move(onCompleted)), file(std::move(file))
62 {
63 }
64
65 void readNextChunk(uint32_t chunkBegin, char *chunkBuffer)
66 {
67 // Copy current chunk from JS memory to Wasm memory
68 qstdweb::ArrayBuffer result = fileReader.result();
69 qstdweb::Uint8Array(result).copyTo(chunkBuffer);
70
71 // Read next chunk if not at buffer end
72 const uint32_t nextChunkBegin = std::min(chunkBegin + result.byteLength(), end);
73 if (nextChunkBegin == end) {
74 onCompleted();
75 delete this;
76 return;
77 }
78 char *nextChunkBuffer = chunkBuffer + result.byteLength();
79 fileReader.onLoad([this, nextChunkBegin, nextChunkBuffer](emscripten::val) {
80 readNextChunk(nextChunkBegin, nextChunkBuffer);
81 });
82 const uint32_t nextChunkEnd = std::min(nextChunkBegin + chunkSize, end);
83 qstdweb::Blob blob = file.slice(nextChunkBegin, nextChunkEnd);
84 fileReader.readAsArrayBuffer(blob);
85 }
86
87 static constexpr uint32_t chunkSize = 256 * 1024;
88
89 qstdweb::FileReader fileReader;
90 uint32_t end;
91 std::function<void()> onCompleted;
92 File file;
93};
94
95#if defined(QT_STATIC)
96
97EM_JS(bool, jsHaveAsyncify, (), { return typeof Asyncify !== "undefined"; });
98EM_JS(bool, jsHaveJspi, (),
99 { return typeof Asyncify !== "undefined" && !!Asyncify.makeAsyncFunction && (!!WebAssembly.Function || !!WebAssembly.Suspending); });
100
101#else
102
103bool jsHaveAsyncify() { return false; }
104bool jsHaveJspi() { return false; }
105
106#endif
107} // namespace
108
113
119
121{
123 return 0;
124
125 return m_arrayBuffer["byteLength"].as<uint32_t>();
126}
127
132
134{
135 return m_arrayBuffer;
136}
137
139 :m_blob(blob)
140{
141
142}
143
145{
146 auto array = emscripten::val::array();
147 array.call<void>("push", arrayBuffer.val());
148 return Blob(emscripten::val::global("Blob").new_(array));
149}
150
152{
153 return m_blob["size"].as<uint32_t>();
154}
155
166
167// Copies content from the given buffer into a Blob object
169{
170 return copyFrom(buffer, size, "application/octet-stream");
171}
172
174{
175 return Blob(m_blob.call<emscripten::val>("slice", begin, end));
176}
177
179{
182 qstdweb::Promise::make(m_blob, QStringLiteral("arrayBuffer"), {
185 loop.quit();
186 }
187 });
188 loop.exec();
189 return ArrayBuffer(buffer);
190}
191
193{
194 return m_blob;
195}
196
198:m_file(file)
199{
200
201}
202
203File::~File() = default;
204
205File::File(const File &other) = default;
206
207File::File(File &&other) = default;
208
209File &File::operator=(const File &other) = default;
210
211File &File::operator=(File &&other) = default;
212
213
215{
216 return Blob(m_file.call<emscripten::val>("slice", uint53_t(begin), uint53_t(end)));
217}
218
220{
221 return m_file["name"].as<std::string>();
222}
223
225{
226 return uint64_t(m_file["size"].as<uint53_t>());
227}
228
230{
231 return m_blob["type"].as<std::string>();
232}
233
234// Streams partial file content into the given buffer asynchronously. The completed
235// callback is called on completion.
241
242// Streams file content into the given buffer asynchronously. The completed
243// callback is called on completion.
244void File::stream(char *buffer, std::function<void()> completed) const
245{
247}
248
250{
251 return m_file["type"].as<std::string>();
252}
253
255{
256 return m_file;
257}
258
260{
262 "createObjectURL", file.file()));
263}
264
266{
267 emscripten::val::global("window")["URL"].call<void>("revokeObjectURL",
269}
270
272
274
280
281int FileList::length() const
282{
283 return m_fileList["length"].as<int>();
284}
285
287{
288 return File(m_fileList[index]);
289}
290
292{
293 return item(index);
294}
295
297{
298 return m_fileList;
299}
300
302{
303 return ArrayBuffer(m_fileReader["result"]);
304}
305
307{
308 m_fileReader.call<void>("readAsArrayBuffer", blob.m_blob);
309}
310
316
322
328
330{
331 return m_fileReader;
332}
333
334// Constructs a Uint8Array which references the given emscripten::val, which must contain a JS Unit8Array
340
341// Constructs a Uint8Array which references an ArrayBuffer
347
348// Constructs a Uint8Array which references a view into an ArrayBuffer
354
355// Constructs a Uint8Array which references an area on the heap.
361
362// Constructs a Uint8Array which allocates and references a new ArrayBuffer with the given size.
368
370{
371 return ArrayBuffer(m_uint8Array["buffer"]);
372}
373
375{
376 return m_uint8Array["length"].as<uint32_t>();
377}
378
380{
381 m_uint8Array.call<void>("set", source.m_uint8Array); // copies source content
382}
383
385{
386 // Note: using uint64_t here errors with "Cannot convert a BigInt value to a number"
387 // (see JS BigInt and Number types). Use uint32_t for now.
388 return Uint8Array(m_uint8Array.call<emscripten::val>("subarray", begin, end));
389}
390
391// Copies the Uint8Array content to a destination on the heap
392void Uint8Array::copyTo(char *destination) const
393{
394 Uint8Array(destination, length()).set(*this);
395}
396
397// Copies the Uint8Array content to a destination QByteArray
408
409// Copies the Uint8Array content to a destination on the heap
414
415// Copies content from a source on the heap to a new Uint8Array object
422
423// Copies content from a QByteArray to a new Uint8Array object
428
430{
431 return m_uint8Array;
432}
433
435{
436 return emscripten::val::global("Uint8Array");
437}
438
445
446void Promise::adoptPromise(emscripten::val promise, PromiseCallbacks callbacks)
447{
448 Q_ASSERT_X(!!callbacks.catchFunc || !!callbacks.finallyFunc || !!callbacks.thenFunc,
449 "Promise::adoptPromise", "must provide at least one callback function");
450
451 QWasmSuspendResumeControl *suspendResume = QWasmSuspendResumeControl::get();
452 Q_ASSERT(suspendResume);
453
454 // Registers a possibly-empty callback with suspendresumecontrol. Returns
455 // the the handler index if there was a valid callback, or nullopt.
456 auto registerCallback = [suspendResume](std::function<void(emscripten::val)> cb) -> std::optional<uint32_t>{
457 if (!cb)
458 return std::nullopt;
459 return std::optional<uint32_t>{suspendResume->registerEventHandler(std::move(cb))};
460 };
461
462 // Register callbacks with suspendresumecontrol, so that it can
463 // resume the wasm instance when the promise resolves. The finally
464 // callback is sepecial, since we remove the event handlers there
465 // as cleanup, including the event handler for the cleanup function
466 // itself.
467 std::optional<uint32_t> thenIndex = registerCallback(std::move(callbacks.thenFunc));
468 std::optional<uint32_t> catchIndex = registerCallback(std::move(callbacks.catchFunc));
469 std::shared_ptr<uint32_t> finallyIndex = std::make_shared<uint32_t>();;
470 auto finallyFunc = callbacks.finallyFunc;
471
472 // 'Finally' callback which performs clean-up and calls the user-provided finally.
473 auto finally = [suspendResume, thenIndex, catchIndex, finallyIndex, finallyFunc](emscripten::val){
474
475 // Clean up event handlers
476 if (thenIndex)
477 suspendResume->removeEventHandler(*thenIndex);
478 if (catchIndex)
479 suspendResume->removeEventHandler(*catchIndex);
480
481 // Call user finally
482 if (finallyFunc)
483 finallyFunc();
484
485 // Remove the finally (this) event handler last
486 suspendResume->removeEventHandler(*finallyIndex);
487 };
488
489 *finallyIndex = suspendResume->registerEventHandler(std::move(finally));
490
491 // Set handlers on the promise
492 if (thenIndex)
493 promise =
494 promise.call<emscripten::val>("then", suspendResume->jsEventHandlerAt(*thenIndex));
495
496 if (catchIndex)
497 promise = promise.call<emscripten::val>("catch",
498 suspendResume->jsEventHandlerAt(*catchIndex));
499
500 promise = promise.call<emscripten::val>("finally",
501 suspendResume->jsEventHandlerAt(*finallyIndex));
502}
503
504void Promise::all(std::vector<emscripten::val> promises, PromiseCallbacks callbacks)
505{
506 auto arr = emscripten::val::array(promises);
507 auto all = val::global("Promise").call<emscripten::val>("all", arr);
508 return adoptPromise(all, callbacks);
509}
510
511// Asyncify and thread blocking: Normally, it's not possible to block the main
512// thread, except if asyncify is enabled. Secondary threads can always block.
513//
514// haveAsyncify(): returns true if the main thread can block on QEventLoop::exec(),
515// if either asyncify 1 or 2 (JSPI) is available.
516//
517// haveJspi(): returns true if asyncify 2 (JSPI) is available.
518//
519// canBlockCallingThread(): returns true if the calling thread can block on
520// QEventLoop::exec(), using either asyncify or as a seconarday thread.
522{
523 static bool HaveJspi = jsHaveJspi();
524 return HaveJspi;
525}
526
528{
529 static bool HaveAsyncify = jsHaveAsyncify() || haveJspi();
530 return HaveAsyncify;
531}
532
534{
535 return haveAsyncify() || !emscripten_is_main_runtime_thread();
536}
537
543
545{
547 return false;
548 return QIODevice::open(mode);
549}
550
552{
553 return false;
554}
555
557{
558 return m_blob.size();
559}
560
562{
563 if (pos >= size())
564 return false;
565 return QIODevice::seek(pos);
566}
567
579
581{
583}
584
586 : m_array(array)
587{
588
589}
590
591bool Uint8ArrayIODevice::open(QIODevice::OpenMode mode)
592{
593 return QIODevice::open(mode);
594}
595
597{
598 return false;
599}
600
602{
603 return m_array.length();
604}
605
606bool Uint8ArrayIODevice::seek(qint64 pos)
607{
608 if (pos >= size())
609 return false;
610 return QIODevice::seek(pos);
611}
612
613qint64 Uint8ArrayIODevice::readData(char *data, qint64 maxSize)
614{
615 uint64_t begin = QIODevice::pos();
616 uint64_t end = std::min<uint64_t>(begin + maxSize, size());
617 uint64_t size = end - begin;
618 if (size > 0)
619 m_array.subarray(begin, end).copyTo(data);
620 return size;
621}
622
623qint64 Uint8ArrayIODevice::writeData(const char *data, qint64 maxSize)
624{
625 uint64_t begin = QIODevice::pos();
626 uint64_t end = std::min<uint64_t>(begin + maxSize, size());
627 uint64_t size = end - begin;
628 if (size > 0)
629 m_array.subarray(begin, end).set(Uint8Array(data, size));
630 return size;
631}
632
633} // namespace qstdweb
634
635QT_END_NAMESPACE
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
bool canBlockCallingThread()
Definition qstdweb.cpp:533
double uint53_t
Definition qstdweb.cpp:46
bool haveAsyncify()
Definition qstdweb.cpp:527
bool haveJspi()
Definition qstdweb.cpp:521
static void usePotentialyUnusedSymbols()
Definition qstdweb.cpp:31