summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/code
diff options
context:
space:
mode:
authorMorten Sørvig <morten.sorvig@qt.io>2022-11-08 12:26:50 +0100
committerMorten Johan Sørvig <morten.sorvig@qt.io>2022-12-05 13:04:51 +0000
commit01412ff16ecf4a17fde7a49cbc8dacdc2a28da36 (patch)
tree12cfb4ca85e235a60e69934725aedadd739695d8 /src/corelib/doc/snippets/code
parent3aaf5975e4ed426f5902283e1d4139b57bdffc36 (diff)
wasm: add native QByteArray conversion functions
Add functions which converts to and from JavaScript data arrays: static QByteArray::fromUint8Array(emscripten::val array) emscripten::val QByteArray::toUint8Array() const with corresponding internal qstdweb API: static Uint8Array Uint8Array::copyFrom(const QByteArray &buffer) QByteArray Uint8Array::copyToQByteArray() const Both functions will make a copy of the data, i.e. there is no shared reference counting. They take and return Uint8Array typed array views, via emscripten::val JavaScript object references. Unlike other native conversion functions, these have the special property that the data referenced by the native Uint8Array exists outside the heap memory area. This means we can’t e.g. memcpy the data. However, the heap is itself a JavaScript ArrayBuffer, and we can create a Uint8Array view to the buffer owned by the QByteArray, and then use JavaScript API to copy. See the qstdweb::Uint8Array::copy() implementation. That also means that a fromRawUint8Array() variant (which does not copy) is not possible to implement, since we can’t create a pointer to the source data. The inverse toRawUint8Array() is implementable - it would return a Uint8Array view which references the heap’s ArrayBuffer. However, this may turn out to be ill-advised, since Emscripten will create a new ArrayBuffer if/when it resizes the heap. In any case this left for a future expansion. Change-Id: Icaf48fd17ea8686bf04cb523cc1eb581ce63ed34 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
Diffstat (limited to 'src/corelib/doc/snippets/code')
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp
index 8a9cb7c841..2a780f1c49 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp
@@ -461,4 +461,15 @@ QByteArray ba = QByteArrayLiteral("byte array contents");
QByteArray encoded("Qt%20is%20great%33");
QByteArray decoded = encoded.percentDecoded(); // Set to "Qt is great!"
//! [54]
+
+//! [55]
+emscripten::val uint8array = emscripten::val::global("g_uint8array");
+QByteArray byteArray = QByteArray::fromUint8Array(uint8array);
+//! [55]
+
+//! [56]
+QByteArray byteArray = "test";
+emscripten::val uint8array = QByteArray::toUint8Array(byteArray);
+//! [56]
+
}