webassembly/library: Extract and send data to print as UInt8Array.

This allows utf-8 data to work.  It's the receiving layer's responsibility
to deal with decoding the data.
This commit is contained in:
Antonin ENFRUN 2022-11-23 21:41:35 +01:00 committed by Damien George
parent f3d9fe7b2c
commit db19ee7e15
2 changed files with 7 additions and 11 deletions

View File

@ -70,7 +70,7 @@ something to stdout. The following code demonstrates basic functionality:
<script> <script>
document.addEventListener("micropython-print", function(e) { document.addEventListener("micropython-print", function(e) {
let output = document.getElementById("micropython-stdout"); let output = document.getElementById("micropython-stdout");
output.innerText += e.detail; output.innerText += new TextDecoder().decode(e.detail);
}, false); }, false);
var mp_js_startup = Module["onRuntimeInitialized"]; var mp_js_startup = Module["onRuntimeInitialized"];

View File

@ -26,17 +26,13 @@
mergeInto(LibraryManager.library, { mergeInto(LibraryManager.library, {
mp_js_write: function(ptr, len) { mp_js_write: function(ptr, len) {
for (var i = 0; i < len; ++i) { const buffer = HEAPU8.subarray(ptr, ptr + len)
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
var b = Buffer.alloc(1); process.stdout.write(buffer);
b.writeInt8(getValue(ptr + i, 'i8'));
process.stdout.write(b);
} else { } else {
var c = String.fromCharCode(getValue(ptr + i, 'i8')); const printEvent = new CustomEvent('micropython-print', { detail: buffer });
var printEvent = new CustomEvent('micropython-print', { detail: c });
document.dispatchEvent(printEvent); document.dispatchEvent(printEvent);
} }
}
}, },
mp_js_ticks_ms: function() { mp_js_ticks_ms: function() {