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{
183 handlers,
184 m_blob,
185 QStringLiteral("arrayBuffer"),
186 {
189 }
190 });
192 return ArrayBuffer(buffer);
193}
194
196{
197 return m_blob;
198}
199
201:m_file(file)
202{
203
204}
205
206File::~File() = default;
207
208File::File(const File &other) = default;
209
210File::File(File &&other) = default;
211
212File &File::operator=(const File &other) = default;
213
214File &File::operator=(File &&other) = default;
215
216
218{
219 return Blob(m_file.call<emscripten::val>("slice", uint53_t(begin), uint53_t(end)));
220}
221
223{
224 return m_file["name"].as<std::string>();
225}
226
228{
229 return uint64_t(m_file["size"].as<uint53_t>());
230}
231
233{
234 return m_blob["type"].as<std::string>();
235}
236
237// Streams partial file content into the given buffer asynchronously. The completed
238// callback is called on completion.
244
245// Streams file content into the given buffer asynchronously. The completed
246// callback is called on completion.
247void File::stream(char *buffer, std::function<void()> completed) const
248{
250}
251
253{
254 return m_file["type"].as<std::string>();
255}
256
258{
259 return m_file;
260}
261
263{
265 "createObjectURL", file.file()));
266}
267
269{
270 emscripten::val::global("window")["URL"].call<void>("revokeObjectURL",
272}
273
275
277
283
284int FileList::length() const
285{
286 return m_fileList["length"].as<int>();
287}
288
290{
291 return File(m_fileList[index]);
292}
293
295{
296 return item(index);
297}
298
300{
301 return m_fileList;
302}
303
305{
306 return ArrayBuffer(m_fileReader["result"]);
307}
308
310{
311 m_fileReader.call<void>("readAsArrayBuffer", blob.m_blob);
312}
313
319
325
331
333{
334 return m_fileReader;
335}
336
337// Constructs a Uint8Array which references the given emscripten::val, which must contain a JS Unit8Array
343
344// Constructs a Uint8Array which references an ArrayBuffer
350
351// Constructs a Uint8Array which references a view into an ArrayBuffer
357
358// Constructs a Uint8Array which references an area on the heap.
364
365// Constructs a Uint8Array which allocates and references a new ArrayBuffer with the given size.
371
373{
374 return ArrayBuffer(m_uint8Array["buffer"]);
375}
376
378{
379 return m_uint8Array["length"].as<uint32_t>();
380}
381
383{
384 m_uint8Array.call<void>("set", source.m_uint8Array); // copies source content
385}
386
388{
389 // Note: using uint64_t here errors with "Cannot convert a BigInt value to a number"
390 // (see JS BigInt and Number types). Use uint32_t for now.
391 return Uint8Array(m_uint8Array.call<emscripten::val>("subarray", begin, end));
392}
393
394// Copies the Uint8Array content to a destination on the heap
395void Uint8Array::copyTo(char *destination) const
396{
397 Uint8Array(destination, length()).set(*this);
398}
399
400// Copies the Uint8Array content to a destination QByteArray
411
412// Copies the Uint8Array content to a destination on the heap
417
418// Copies content from a source on the heap to a new Uint8Array object
425
426// Copies content from a QByteArray to a new Uint8Array object
431
433{
434 return m_uint8Array;
435}
436
438{
439 return emscripten::val::global("Uint8Array");
440}
441
448
450
451//
452// When a promise settles, all attached handlers will be called in
453// the order they where added.
454//
455// In particular a finally handler will be called according to its
456// position in the call chain. Which is not necessarily at the end,
457//
458// This makes cleanup difficult. If we cleanup to early, we will remove
459// handlers before they have a chance to be called. This would be the
460// case if we add a finally handler in the Promise constructor.
461//
462// For correct cleanup it is necessary that it happens after the
463// last handler has been called.
464//
465// We choose to implement this by making sure the last handler
466// is always a finally handler.
467//
468// In this case we have multiple finally handlers, so any called
469// handler checks if it is the last handler to be called.
470// If it is, the cleanup is performed, otherwise cleanup
471// is delayed to the last handler.
472//
473// We could try to let the handlers cleanup themselves, but this
474// only works for finally handlers. A then or catch handler is not
475// necessarily called, and would not cleanup itself.
476//
477// We could try to let a (then,catch) pair cleanup both handlers,
478// under the assumption that one of them will always be called.
479// This does not work in the case that we do not have both handlers,
480// or if the then handler throws (both should be called in this case).
481//
496
511
513{
515
517 auto state = m_state;
518
519 std::function<void(emscripten::val)> func =
522
523 finallyFunc();
524
525 // See comment at top, we can only do the cleanup
526 // if we are the last handler in the handler chain
527 if (state->m_handlers.back() == *thisHandler) {
528 auto guard = state; // removeEventHandler will remove also this function
530 for (int i = 0; i < guard->m_handlers.size(); ++i) {
532 guard->m_handlers[i] = (uint32_t)(-1);
533 }
534 }
535 };
536
541 "finally",
544
545 return (*this);
546}
547
552
554{
555 return m_state->m_promise;
556}
557
559{
561 "Promise::adoptPromise", "must provide at least one callback function");
562
564
565 // Registers a possibly-empty callback with suspendresumecontrol. Returns
566 // the the handler index if there was a valid callback, or nullopt.
568 if (!cb)
569 return std::nullopt;
571 };
572
573 // Register callbacks with suspendresumecontrol, so that it can
574 // resume the wasm instance when the promise resolves. The finally
575 // callback is sepecial, since we remove the event handlers there
576 // as cleanup, including the event handler for the cleanup function
577 // itself.
582
583 // 'Finally' callback which performs clean-up and calls the user-provided finally.
586
587 // Clean up event handlers
588 if (thenIndex)
590 if (catchIndex)
592
593 // Call user finally
594 if (finallyFunc)
595 finallyFunc();
596
597 // Remove the finally (this) event handler last
599 };
600
602
603 // Set handlers on the promise
604 if (thenIndex)
605 promise =
607
608 if (catchIndex)
609 promise = promise.call<emscripten::val>("catch",
611
612 promise = promise.call<emscripten::val>("finally",
614
615 if (handlers) {
616 if (thenIndex)
618 if (catchIndex)
621 }
622 return *finallyIndex;
623}
624
631
638
639// Asyncify and thread blocking: Normally, it's not possible to block the main
640// thread, except if asyncify is enabled. Secondary threads can always block.
641//
642// haveAsyncify(): returns true if the main thread can block on QEventLoop::exec(),
643// if either asyncify 1 or 2 (JSPI) is available.
644//
645// haveJspi(): returns true if asyncify 2 (JSPI) is available.
646//
647// canBlockCallingThread(): returns true if the calling thread can block on
648// QEventLoop::exec(), using either asyncify or as a seconarday thread.
650{
651 static bool HaveJspi = jsHaveJspi();
652 return HaveJspi;
653}
654
656{
657 static bool HaveAsyncify = jsHaveAsyncify() || haveJspi();
658 return HaveAsyncify;
659}
660
662{
663 return haveAsyncify() || !emscripten_is_main_runtime_thread();
664}
665
671
673{
675 return false;
676 return QIODevice::open(mode);
677}
678
680{
681 return false;
682}
683
685{
686 return m_blob.size();
687}
688
690{
691 if (pos >= size())
692 return false;
693 return QIODevice::seek(pos);
694}
695
707
709{
711}
712
714 : m_array(array)
715{
716
717}
718
719bool Uint8ArrayIODevice::open(QIODevice::OpenMode mode)
720{
721 return QIODevice::open(mode);
722}
723
725{
726 return false;
727}
728
730{
731 return m_array.length();
732}
733
734bool Uint8ArrayIODevice::seek(qint64 pos)
735{
736 if (pos >= size())
737 return false;
738 return QIODevice::seek(pos);
739}
740
741qint64 Uint8ArrayIODevice::readData(char *data, qint64 maxSize)
742{
743 uint64_t begin = QIODevice::pos();
744 uint64_t end = std::min<uint64_t>(begin + maxSize, size());
745 uint64_t size = end - begin;
746 if (size > 0)
747 m_array.subarray(begin, end).copyTo(data);
748 return size;
749}
750
751qint64 Uint8ArrayIODevice::writeData(const char *data, qint64 maxSize)
752{
753 uint64_t begin = QIODevice::pos();
754 uint64_t end = std::min<uint64_t>(begin + maxSize, size());
755 uint64_t size = end - begin;
756 if (size > 0)
757 m_array.subarray(begin, end).set(Uint8Array(data, size));
758 return size;
759}
760
765
772
774{
775 if (!isOpen()) {
776 QIODevice::close();
777 return;
778 }
779
782 .thenFunc = [](emscripten::val) {
783 }
784 });
786
787 QIODevice::close();
788}
789
791{
792 return false;
793}
794
799
801{
802 bool success = false;
803
805 seekParams.set("type", std::string("seek"));
806 seekParams.set("position", static_cast<double>(pos));
810 success = true;
811 },
812 .catchFunc = [](emscripten::val) {
813 }
814 }, seekParams);
816
817 if (!success)
818 return false;
819
820 return QIODevice::seek(pos);
821}
822
827
829{
830 bool success = false;
831
836 success = true;
837 },
838 .catchFunc = [](emscripten::val) {
839 }
840 }, array.val());
842
843 if (success) {
844 qint64 newPos = pos() + size;
846 return size;
847 }
848 return -1;
849}
850
855
860
865
867{
868 return m_fileHandle["name"].as<std::string>();
869}
870
872{
873 return m_fileHandle["kind"].as<std::string>();
874}
875
877{
878 return m_fileHandle;
879}
880
885
887{
888 if (isOpen())
889 return false;
890
891 // Read mode: get the File and create a BlobIODevice
892 if (mode & QIODevice::ReadOnly) {
893 File file;
894 bool success = false;
895
899 file = File(fileVal);
900 success = true;
901 },
902 .catchFunc = [](emscripten::val) {
903 }
904 });
906
907 if (success) {
909 m_size = file.size();
910
911 if (!m_blobDevice->open(mode))
912 return false;
913 } else {
914 return false;
915 }
916 }
917
918 // Write mode: create a writable stream
919 if (mode & QIODevice::WriteOnly) {
921 bool success = false;
922
924 Promise::make(handlers, m_fileHandle.val(), QStringLiteral("createWritable"), {
927 success = true;
928 },
929 .catchFunc = [](emscripten::val) {
930 }
931 });
933
934 if (success) {
937 return false;
938 } else {
939 return false;
940 }
941 }
942
943 return QIODevice::open(mode);
944}
945
947{
948 if (!isOpen()) {
949 QIODevice::close();
950 return;
951 }
952
953 if (m_writableDevice) {
956 }
957 if (m_blobDevice) {
960 }
961
962 QIODevice::close();
963}
964
966{
967 return false;
968}
969
971{
972 return m_size;
973}
974
976{
977 if (m_blobDevice) {
978 if (!m_blobDevice->seek(pos))
979 return false;
980 }
981 if (m_writableDevice) {
983 return false;
984 }
985 return QIODevice::seek(pos);
986}
987
989{
990 if (!m_blobDevice)
991 return -1;
992
993 return m_blobDevice->read(data, maxSize);
994}
995
997{
998 if (!m_writableDevice)
999 return -1;
1000
1002 if (written > 0) {
1003 qint64 newPos = pos() + written;
1004 m_size = std::max(m_size, newPos);
1005 }
1006 return written;
1007}
1008
1009} // namespace qstdweb
1010
1011QT_END_NAMESPACE
qint64 size() const override
For open random-access devices, this function returns the size of the device.
Definition qstdweb.cpp:729
bool seek(qint64 pos) override
For random-access devices, this function sets the current position to pos, returning true on success,...
Definition qstdweb.cpp:734
Uint8ArrayIODevice(Uint8Array array)
Definition qstdweb.cpp:713
bool isSequential() const override
Returns true if this device is sequential; otherwise returns false.
Definition qstdweb.cpp:724
qint64 writeData(const char *data, qint64 size) override
Writes up to maxSize bytes from data to the device.
Definition qstdweb.cpp:751
bool open(QIODevice::OpenMode mode) override
Definition qstdweb.cpp:719
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:741
bool canBlockCallingThread()
Definition qstdweb.cpp:661
double uint53_t
Definition qstdweb.cpp:46
bool haveAsyncify()
Definition qstdweb.cpp:655
bool haveJspi()
Definition qstdweb.cpp:649
static void usePotentialyUnusedSymbols()
Definition qstdweb.cpp:31