Skip to content

Commit

Permalink
[WASMFS] Switch to using out and err (emscripten-core#15629)
Browse files Browse the repository at this point in the history
Add `emscripten_out` and `emscripten_err` along with `f` versions.
Replaced `emscripten_console_log`/`error` with those two functions respectively in `streams.cpp`.
  • Loading branch information
ethanalee authored and mmarczell-graphisoft committed Jan 5, 2022
1 parent eb384a5 commit 250efd1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -3494,6 +3494,22 @@ LibraryManager.library = {
#endif
},

_emscripten_out__sig: 'vi',
_emscripten_out: function(str) {
#if ASSERTIONS
assert(typeof str === 'number');
#endif
out(UTF8ToString(str));
},

_emscripten_err__sig: 'vi',
_emscripten_err: function(str) {
#if ASSERTIONS
assert(typeof str === 'number');
#endif
err(UTF8ToString(str));
},

emscripten_console_log__sig: 'vi',
emscripten_console_log: function(str) {
#if ASSERTIONS
Expand Down
13 changes: 13 additions & 0 deletions system/include/emscripten/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,23 @@ void emscripten_console_log(const char *utf8String);
void emscripten_console_warn(const char *utf8String);
void emscripten_console_error(const char *utf8String);

// Write to the out() and err() hooks directly (defined in shell.js).
// These have different behavior compared to console.log/err.
// Under node, they write to stdout and stderr which is a more direct way to
// write output especially in worker threads.
// The default behavior of these functions can be overridden by print and
// printErr, if provided on the Module object.
// These functions are mainly intended for internal use.
// See https://github.com/emscripten-core/emscripten/issues/14804
void _emscripten_out(const char *utf8String);
void _emscripten_err(const char *utf8String);

// Similar to the above functions but operate with printf-like semantics.
void emscripten_console_logf(const char *utf8String, ...) __attribute__((__format__(printf, 1, 2)));
void emscripten_console_warnf(const char *utf8String, ...) __attribute__((__format__(printf, 1, 2)));
void emscripten_console_errorf(const char *utf8String, ...)__attribute__((__format__(printf, 1, 2)));
void _emscripten_outf(const char *utf8String, ...) __attribute__((__format__(printf, 1, 2)));
void _emscripten_errf(const char *utf8String, ...) __attribute__((__format__(printf, 1, 2)));

#ifdef __cplusplus
}
Expand Down
14 changes: 14 additions & 0 deletions system/lib/libc/emscripten_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ void emscripten_console_warnf(const char* fmt, ...) {
vlogf(fmt, ap, &emscripten_console_warn);
va_end(ap);
}

void _emscripten_outf(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vlogf(fmt, ap, &_emscripten_out);
va_end(ap);
}

void _emscripten_errf(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vlogf(fmt, ap, &_emscripten_err);
va_end(ap);
}
8 changes: 6 additions & 2 deletions system/lib/wasmfs/streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ std::shared_ptr<StdinFile> StdinFile::getSingleton() {
}

__wasi_errno_t StdoutFile::write(const uint8_t* buf, size_t len, off_t offset) {
return writeStdBuffer(buf, len, &emscripten_console_log, writeBuffer);
// Due to this issue with node and worker threads:
// https://github.com/emscripten-core/emscripten/issues/14804. This function
// will write to out(), which will write directly to stdout in node.
return writeStdBuffer(buf, len, &_emscripten_out, writeBuffer);
}

std::shared_ptr<StdoutFile> StdoutFile::getSingleton() {
Expand All @@ -44,7 +47,8 @@ std::shared_ptr<StdoutFile> StdoutFile::getSingleton() {
}

__wasi_errno_t StderrFile::write(const uint8_t* buf, size_t len, off_t offset) {
return writeStdBuffer(buf, len, &emscripten_console_error, writeBuffer);
// Similar issue with node and worker threads as emscripten_out.
return writeStdBuffer(buf, len, &_emscripten_err, writeBuffer);
}

std::shared_ptr<StderrFile> StderrFile::getSingleton() {
Expand Down

0 comments on commit 250efd1

Please sign in to comment.