From ae6bcc9d2345d73016fd22f9cc22b56c73bae432 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 31 May 2023 11:44:45 +1000 Subject: [PATCH] webassembly: Use POSIX write for output and add stderr. All output is now handled by Emscripten's stdio facility. Signed-off-by: Damien George --- ports/webassembly/library.js | 10 ---------- ports/webassembly/mpconfigport.h | 4 +++- ports/webassembly/mphalport.c | 11 +++++++++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/ports/webassembly/library.js b/ports/webassembly/library.js index d1266598da..db5bac2691 100644 --- a/ports/webassembly/library.js +++ b/ports/webassembly/library.js @@ -25,16 +25,6 @@ */ mergeInto(LibraryManager.library, { - mp_js_write: function(ptr, len) { - const buffer = HEAPU8.subarray(ptr, ptr + len) - if (ENVIRONMENT_IS_NODE) { - process.stdout.write(buffer); - } else { - const printEvent = new CustomEvent('micropython-print', { detail: buffer }); - document.dispatchEvent(printEvent); - } - }, - // This string will be emitted directly into the output file by Emscripten. mp_js_ticks_ms__postset: "var MP_JS_EPOCH = Date.now()", diff --git a/ports/webassembly/mpconfigport.h b/ports/webassembly/mpconfigport.h index 79537dd72d..6b0f2753a9 100644 --- a/ports/webassembly/mpconfigport.h +++ b/ports/webassembly/mpconfigport.h @@ -46,6 +46,7 @@ #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_ENABLE_DOC_STRING (1) #define MICROPY_WARNINGS (1) +#define MICROPY_ERROR_PRINTER (&mp_stderr_print) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE) #define MICROPY_USE_INTERNAL_ERRNO (1) #define MICROPY_USE_INTERNAL_PRINTF (0) @@ -60,7 +61,6 @@ #endif #define MICROPY_VFS_POSIX (MICROPY_VFS) #define MICROPY_PY_SYS_PLATFORM "webassembly" -#define MICROPY_PY_SYS_STDFILES (0) #define MICROPY_EVENT_POLL_HOOK \ do { \ @@ -102,4 +102,6 @@ typedef long mp_off_t; #define _GNU_SOURCE #endif +extern const struct _mp_print_t mp_stderr_print; + uint32_t mp_js_random_u32(void); diff --git a/ports/webassembly/mphalport.c b/ports/webassembly/mphalport.c index 72a326e30d..9ab47762e3 100644 --- a/ports/webassembly/mphalport.c +++ b/ports/webassembly/mphalport.c @@ -24,12 +24,19 @@ * THE SOFTWARE. */ +#include #include "library.h" #include "mphalport.h" +static void stderr_print_strn(void *env, const char *str, size_t len) { + (void)env; + write(2, str, len); +} + +const mp_print_t mp_stderr_print = {NULL, stderr_print_strn}; + mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) { - mp_js_write(str, len); - return len; + return write(1, str, len); } void mp_hal_delay_ms(mp_uint_t ms) {