Skip to content

Commit ae6bcc9

Browse files
committed
webassembly: Use POSIX write for output and add stderr.
All output is now handled by Emscripten's stdio facility. Signed-off-by: Damien George <[email protected]>
1 parent 8e3b701 commit ae6bcc9

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

ports/webassembly/library.js

-10
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@
2525
*/
2626

2727
mergeInto(LibraryManager.library, {
28-
mp_js_write: function(ptr, len) {
29-
const buffer = HEAPU8.subarray(ptr, ptr + len)
30-
if (ENVIRONMENT_IS_NODE) {
31-
process.stdout.write(buffer);
32-
} else {
33-
const printEvent = new CustomEvent('micropython-print', { detail: buffer });
34-
document.dispatchEvent(printEvent);
35-
}
36-
},
37-
3828
// This string will be emitted directly into the output file by Emscripten.
3929
mp_js_ticks_ms__postset: "var MP_JS_EPOCH = Date.now()",
4030

ports/webassembly/mpconfigport.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
4747
#define MICROPY_ENABLE_DOC_STRING (1)
4848
#define MICROPY_WARNINGS (1)
49+
#define MICROPY_ERROR_PRINTER (&mp_stderr_print)
4950
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_DOUBLE)
5051
#define MICROPY_USE_INTERNAL_ERRNO (1)
5152
#define MICROPY_USE_INTERNAL_PRINTF (0)
@@ -60,7 +61,6 @@
6061
#endif
6162
#define MICROPY_VFS_POSIX (MICROPY_VFS)
6263
#define MICROPY_PY_SYS_PLATFORM "webassembly"
63-
#define MICROPY_PY_SYS_STDFILES (0)
6464

6565
#define MICROPY_EVENT_POLL_HOOK \
6666
do { \
@@ -102,4 +102,6 @@ typedef long mp_off_t;
102102
#define _GNU_SOURCE
103103
#endif
104104

105+
extern const struct _mp_print_t mp_stderr_print;
106+
105107
uint32_t mp_js_random_u32(void);

ports/webassembly/mphalport.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include <unistd.h>
2728
#include "library.h"
2829
#include "mphalport.h"
2930

31+
static void stderr_print_strn(void *env, const char *str, size_t len) {
32+
(void)env;
33+
write(2, str, len);
34+
}
35+
36+
const mp_print_t mp_stderr_print = {NULL, stderr_print_strn};
37+
3038
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
31-
mp_js_write(str, len);
32-
return len;
39+
return write(1, str, len);
3340
}
3441

3542
void mp_hal_delay_ms(mp_uint_t ms) {

0 commit comments

Comments
 (0)