diff options
author | Antonin ENFRUN <antonin.e@me.com> | 2022-11-23 21:41:35 +0100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2022-12-13 17:16:37 +1100 |
commit | db19ee7e15309a7b59756d5176ff20e5ade8dfa1 (patch) | |
tree | 15ed973ef35115bbca603b9013321ac4c2fb7b76 | |
parent | f3d9fe7b2cd9884bbe9459b39024902ba5e960a6 (diff) | |
download | micropython-db19ee7e15309a7b59756d5176ff20e5ade8dfa1.tar.gz micropython-db19ee7e15309a7b59756d5176ff20e5ade8dfa1.zip |
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.
-rw-r--r-- | ports/webassembly/README.md | 2 | ||||
-rw-r--r-- | ports/webassembly/library.js | 16 |
2 files changed, 7 insertions, 11 deletions
diff --git a/ports/webassembly/README.md b/ports/webassembly/README.md index 89b8b1def7..5019d6f905 100644 --- a/ports/webassembly/README.md +++ b/ports/webassembly/README.md @@ -70,7 +70,7 @@ something to stdout. The following code demonstrates basic functionality: <script> document.addEventListener("micropython-print", function(e) { let output = document.getElementById("micropython-stdout"); - output.innerText += e.detail; + output.innerText += new TextDecoder().decode(e.detail); }, false); var mp_js_startup = Module["onRuntimeInitialized"]; diff --git a/ports/webassembly/library.js b/ports/webassembly/library.js index fb31b0fe56..fc5b542488 100644 --- a/ports/webassembly/library.js +++ b/ports/webassembly/library.js @@ -26,16 +26,12 @@ mergeInto(LibraryManager.library, { mp_js_write: function(ptr, len) { - for (var i = 0; i < len; ++i) { - if (typeof window === 'undefined') { - var b = Buffer.alloc(1); - b.writeInt8(getValue(ptr + i, 'i8')); - process.stdout.write(b); - } else { - var c = String.fromCharCode(getValue(ptr + i, 'i8')); - var printEvent = new CustomEvent('micropython-print', { detail: c }); - document.dispatchEvent(printEvent); - } + const buffer = HEAPU8.subarray(ptr, ptr + len) + if (typeof window === 'undefined') { + process.stdout.write(buffer); + } else { + const printEvent = new CustomEvent('micropython-print', { detail: buffer }); + document.dispatchEvent(printEvent); } }, |