From d8d3e3e05c8e21471fd0e70766b31c9eb7e9c119 Mon Sep 17 00:00:00 2001 From: Ze-Zheng Wu Date: Fri, 14 Jul 2023 11:43:06 +0800 Subject: [PATCH 1/3] fix: safely return bytes as `Uint8Array` in `ReadResult` via `emscripten::val::global` --- wrappers/wasm/BarcodeReader.cpp | 22 +++++++++++++--------- wrappers/wasm/demo_reader.html | 10 +--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/wrappers/wasm/BarcodeReader.cpp b/wrappers/wasm/BarcodeReader.cpp index d749aec4cf..e256902c7e 100644 --- a/wrappers/wasm/BarcodeReader.cpp +++ b/wrappers/wasm/BarcodeReader.cpp @@ -7,6 +7,7 @@ #include "ReadBarcode.h" #include +#include #include #include #include @@ -20,16 +21,10 @@ struct ReadResult { std::string format{}; std::string text{}; - std::string bytes{}; + emscripten::val bytes; std::string error{}; Position position{}; std::string symbologyIdentifier{}; - -// The following seemed like the way to go, because the bytes member on the JS side would then automatically be a Uint8Array -// but unfortunatelly, I don't understand something about the memory management, because the resulting array could contain 8 bytes of garbage. -// ByteArray bytes; -// emscripten::val get_bytes() const { return emscripten::val(emscripten::typed_memory_view(bytes.size(), bytes.data())); } -// void set_bytes(emscripten::val) {} // dummy setter }; std::vector readBarcodes(ImageView iv, bool tryHarder, const std::string& format, int maxSymbols) @@ -49,9 +44,18 @@ std::vector readBarcodes(ImageView iv, bool tryHarder, const std::st std::vector readResults{}; readResults.reserve(results.size()); + thread_local const emscripten::val Uint8Array = emscripten::val::global("Uint8Array"); + for (auto&& result : results) { - readResults.push_back({ToString(result.format()), result.text(), std::string(result.bytes().asString()), - ToString(result.error()), result.position(), result.symbologyIdentifier()}); + ByteArray bytes = result.bytes(); + readResults.push_back({ + ToString(result.format()), + result.text(), + Uint8Array.new_(emscripten::typed_memory_view(bytes.size(), bytes.data())), + ToString(result.error()), + result.position(), + result.symbologyIdentifier() + }); } return readResults; diff --git a/wrappers/wasm/demo_reader.html b/wrappers/wasm/demo_reader.html index 99829f0a23..86b6af1358 100644 --- a/wrappers/wasm/demo_reader.html +++ b/wrappers/wasm/demo_reader.html @@ -74,14 +74,6 @@ return htmlStr.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } -function str2u8a(str) { - var bufView = new Uint8Array(new ArrayBuffer(str.length)); - for (var i = 0; i < bufView.length; i++) { - bufView[i] = str.charCodeAt(i); - } - return bufView; -} - function u8a2hex(bytes) { return bytes.reduce((a, b) => a + b.toString(16).padStart(2, '0') + ' ', ''); } @@ -97,7 +89,7 @@ const { format, text, bytes, error } = results.get(i); resultsDiv.innerHTML += "
  • Format: " + format + "" + "
    " + (escapeTags(text) || 'Error: ' + error + '') + "
    " - + "
    " + u8a2hex(str2u8a(bytes)) + "
    " + + "
    " + u8a2hex(bytes) + "
    " + "
  • "; } } From da4fa3b63a9b44fd94f0f4a2ea944191231faa0c Mon Sep 17 00:00:00 2001 From: Ze-Zheng Wu Date: Fri, 14 Jul 2023 11:43:06 +0800 Subject: [PATCH 2/3] WASM: bytes in `ReadResult` safely return bytes as `Uint8Array` in `ReadResult` via `emscripten::val::global` --- wrappers/wasm/BarcodeReader.cpp | 22 +++++++++++++--------- wrappers/wasm/demo_reader.html | 10 +--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/wrappers/wasm/BarcodeReader.cpp b/wrappers/wasm/BarcodeReader.cpp index d749aec4cf..e256902c7e 100644 --- a/wrappers/wasm/BarcodeReader.cpp +++ b/wrappers/wasm/BarcodeReader.cpp @@ -7,6 +7,7 @@ #include "ReadBarcode.h" #include +#include #include #include #include @@ -20,16 +21,10 @@ struct ReadResult { std::string format{}; std::string text{}; - std::string bytes{}; + emscripten::val bytes; std::string error{}; Position position{}; std::string symbologyIdentifier{}; - -// The following seemed like the way to go, because the bytes member on the JS side would then automatically be a Uint8Array -// but unfortunatelly, I don't understand something about the memory management, because the resulting array could contain 8 bytes of garbage. -// ByteArray bytes; -// emscripten::val get_bytes() const { return emscripten::val(emscripten::typed_memory_view(bytes.size(), bytes.data())); } -// void set_bytes(emscripten::val) {} // dummy setter }; std::vector readBarcodes(ImageView iv, bool tryHarder, const std::string& format, int maxSymbols) @@ -49,9 +44,18 @@ std::vector readBarcodes(ImageView iv, bool tryHarder, const std::st std::vector readResults{}; readResults.reserve(results.size()); + thread_local const emscripten::val Uint8Array = emscripten::val::global("Uint8Array"); + for (auto&& result : results) { - readResults.push_back({ToString(result.format()), result.text(), std::string(result.bytes().asString()), - ToString(result.error()), result.position(), result.symbologyIdentifier()}); + ByteArray bytes = result.bytes(); + readResults.push_back({ + ToString(result.format()), + result.text(), + Uint8Array.new_(emscripten::typed_memory_view(bytes.size(), bytes.data())), + ToString(result.error()), + result.position(), + result.symbologyIdentifier() + }); } return readResults; diff --git a/wrappers/wasm/demo_reader.html b/wrappers/wasm/demo_reader.html index 99829f0a23..86b6af1358 100644 --- a/wrappers/wasm/demo_reader.html +++ b/wrappers/wasm/demo_reader.html @@ -74,14 +74,6 @@ return htmlStr.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); } -function str2u8a(str) { - var bufView = new Uint8Array(new ArrayBuffer(str.length)); - for (var i = 0; i < bufView.length; i++) { - bufView[i] = str.charCodeAt(i); - } - return bufView; -} - function u8a2hex(bytes) { return bytes.reduce((a, b) => a + b.toString(16).padStart(2, '0') + ' ', ''); } @@ -97,7 +89,7 @@ const { format, text, bytes, error } = results.get(i); resultsDiv.innerHTML += "
  • Format: " + format + "" + "
    " + (escapeTags(text) || 'Error: ' + error + '') + "
    " - + "
    " + u8a2hex(str2u8a(bytes)) + "
    " + + "
    " + u8a2hex(bytes) + "
    " + "
  • "; } } From 65a7cb64d78e8f6d86c9d1eb9e7063f9667c7171 Mon Sep 17 00:00:00 2001 From: Ze-Zheng Wu Date: Fri, 14 Jul 2023 20:37:06 +0800 Subject: [PATCH 3/3] fix: use reference to prevent unnecessary copy --- wrappers/wasm/BarcodeReader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wrappers/wasm/BarcodeReader.cpp b/wrappers/wasm/BarcodeReader.cpp index e256902c7e..2f216d5f3f 100644 --- a/wrappers/wasm/BarcodeReader.cpp +++ b/wrappers/wasm/BarcodeReader.cpp @@ -47,7 +47,7 @@ std::vector readBarcodes(ImageView iv, bool tryHarder, const std::st thread_local const emscripten::val Uint8Array = emscripten::val::global("Uint8Array"); for (auto&& result : results) { - ByteArray bytes = result.bytes(); + const ByteArray& bytes = result.bytes(); readResults.push_back({ ToString(result.format()), result.text(), pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy