Skip to content

Commit db19ee7

Browse files
Anton-2dpgeorge
authored andcommitted
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.
1 parent f3d9fe7 commit db19ee7

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

Diff for: ports/webassembly/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ something to stdout. The following code demonstrates basic functionality:
7070
<script>
7171
document.addEventListener("micropython-print", function(e) {
7272
let output = document.getElementById("micropython-stdout");
73-
output.innerText += e.detail;
73+
output.innerText += new TextDecoder().decode(e.detail);
7474
}, false);
7575
7676
var mp_js_startup = Module["onRuntimeInitialized"];

Diff for: ports/webassembly/library.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@
2626

2727
mergeInto(LibraryManager.library, {
2828
mp_js_write: function(ptr, len) {
29-
for (var i = 0; i < len; ++i) {
30-
if (typeof window === 'undefined') {
31-
var b = Buffer.alloc(1);
32-
b.writeInt8(getValue(ptr + i, 'i8'));
33-
process.stdout.write(b);
34-
} else {
35-
var c = String.fromCharCode(getValue(ptr + i, 'i8'));
36-
var printEvent = new CustomEvent('micropython-print', { detail: c });
37-
document.dispatchEvent(printEvent);
38-
}
29+
const buffer = HEAPU8.subarray(ptr, ptr + len)
30+
if (typeof window === 'undefined') {
31+
process.stdout.write(buffer);
32+
} else {
33+
const printEvent = new CustomEvent('micropython-print', { detail: buffer });
34+
document.dispatchEvent(printEvent);
3935
}
4036
},
4137

0 commit comments

Comments
 (0)