Skip to content

WASM: bytes in ReadResult #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WASM: bytes in ReadResult
safely return bytes as `Uint8Array` in `ReadResult` via `emscripten::val::global`
  • Loading branch information
Sec-ant committed Jul 14, 2023
commit da4fa3b63a9b44fd94f0f4a2ea944191231faa0c
22 changes: 13 additions & 9 deletions wrappers/wasm/BarcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ReadBarcode.h"

#include <emscripten/bind.h>
#include <emscripten/val.h>
#include <memory>
#include <stdexcept>
#include <string>
Expand All @@ -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<ReadResult> readBarcodes(ImageView iv, bool tryHarder, const std::string& format, int maxSymbols)
Expand All @@ -49,9 +44,18 @@ std::vector<ReadResult> readBarcodes(ImageView iv, bool tryHarder, const std::st
std::vector<ReadResult> 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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! I have one tiny improvement to suggest: this line makes a copy of result.bytes(), which is unnecessary. Either use result.bytes().data() below or change this line to const ByteArray& bytes = result.bytes(). I have not tested your changes myself, will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed now.

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;
Expand Down
10 changes: 1 addition & 9 deletions wrappers/wasm/demo_reader.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@
return htmlStr.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
}

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') + ' ', '');
}
Expand All @@ -97,7 +89,7 @@
const { format, text, bytes, error } = results.get(i);
resultsDiv.innerHTML += "<li>Format: <strong>" + format + "</strong>"
+ "<pre>" + (escapeTags(text) || '<font color="red">Error: ' + error + '</font>') + "</pre>"
+ "<pre>" + u8a2hex(str2u8a(bytes)) + "</pre>"
+ "<pre>" + u8a2hex(bytes) + "</pre>"
+ "</li>";
}
}
Expand Down
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