-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Port over stdout implementation to wasmfs.cpp #15104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6c05f32
stdout in wasmfs
ethanalee 7b70690
emcc updated
ethanalee 6939f12
add library_wasmfs
ethanalee f2ce6b4
add library_wasmfs to emcc.py
ethanalee d1b1e81
whitespace
ethanalee f26043d
remove default list
ethanalee 84d4bc5
add blank lines
ethanalee 92d5304
addressed comments
ethanalee 3972910
white space
ethanalee a301fac
remove parens
ethanalee 117be99
grammar and tests fix
ethanalee 9d4bdab
extracted writing to stdout to library_wasmfs.js
ethanalee 28bcfcb
changed layout of library_wasmfs, removed EM_ASM
ethanalee d31597f
change name to _printbuffer
ethanalee 0ee2972
use emscripten_console_log
ethanalee 6236f37
Merge remote-tracking branch 'origin/main' into wasmFSTemplate
ethanalee 47d4392
add html5.h header
ethanalee 13ef89f
removed errant #define, update test
ethanalee 8ce49c4
edited tests, comments
ethanalee d86d655
if WASMFS is off, do not link wasmfs.cpp
ethanalee d9936bf
remove whitespace
ethanalee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| var WasmfsLibrary = { | ||
| $wasmfsBuffers: [null, [], []], | ||
| emscripten_wasmfs_printbuffer__deps: ['$wasmfsBuffers'], | ||
| emscripten_wasmfs_printbuffer: function(stream, ptr, len) { | ||
| for (var j = 0; j < len; j++) { | ||
| if (HEAPU8[ptr+j] === 0 || HEAPU8[ptr+j] === 10) { | ||
| (stream === 1 ? out : err)(UTF8ArrayToString(wasmfsBuffers[stream], 0)); | ||
| wasmfsBuffers[stream].length = 0; | ||
| } else { | ||
| wasmfsBuffers[stream].push(HEAPU8[ptr+j]); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| mergeInto(LibraryManager.library, WasmfsLibrary); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright 2021 The Emscripten Authors. All rights reserved. | ||
| // Emscripten is available under two separate licenses, the MIT license and the | ||
| // University of Illinois/NCSA Open Source License. Both these licenses can be | ||
| // found in the LICENSE file. | ||
| // wasmfs.cpp will implement a new file system that replaces the existing JS filesystem. | ||
| // Current Status: Work in Progress. | ||
| // See https://github.com/emscripten-core/emscripten/issues/15041. | ||
|
|
||
| #include <emscripten/emscripten.h> | ||
| #include <emscripten/html5.h> | ||
| #include <stdlib.h> | ||
| #include <wasi/api.h> | ||
|
|
||
| extern "C" { | ||
|
|
||
| int emscripten_wasmfs_printbuffer(__wasi_fd_t fd, const uint8_t* ptr, __wasi_size_t len); | ||
|
|
||
| __wasi_errno_t __wasi_fd_write( | ||
| __wasi_fd_t fd, const __wasi_ciovec_t* iovs, size_t iovs_len, __wasi_size_t* nwritten) { | ||
| // FD 1 = STDOUT and FD 2 = STDERR. | ||
| // Temporary hardcoding of filedescriptor values. | ||
| // TODO: May not want to proxy stderr (fd == 2) to the main thread. | ||
| // This will not not show in HTML, a console.warn in a worker is suffficient. | ||
| // This would be a change from the current FS. | ||
| if (fd == 1 || fd == 2) { | ||
|
kripken marked this conversation as resolved.
|
||
| __wasi_size_t num = 0; | ||
| for (size_t i = 0; i < iovs_len; i++) { | ||
| const uint8_t* ptr = iovs[i].buf; | ||
| __wasi_size_t len = iovs[i].buf_len; | ||
| emscripten_wasmfs_printbuffer(fd, ptr, len); | ||
| num += len; | ||
| } | ||
| *nwritten = num; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| __wasi_errno_t __wasi_fd_seek( | ||
| __wasi_fd_t fd, __wasi_filedelta_t offset, __wasi_whence_t whence, __wasi_filesize_t* newoffset) { | ||
| emscripten_console_log("__wasi_fd_seek has been temporarily stubbed and is inert"); | ||
| abort(); | ||
|
ethanalee marked this conversation as resolved.
|
||
| } | ||
|
|
||
| __wasi_errno_t __wasi_fd_close(__wasi_fd_t fd) { | ||
| emscripten_console_log("__wasi_fd_close has been temporarily stubbed and is inert"); | ||
| abort(); | ||
| } | ||
|
|
||
| __wasi_errno_t __wasi_fd_read( | ||
| __wasi_fd_t fd, const __wasi_iovec_t* iovs, size_t iovs_len, __wasi_size_t* nread) { | ||
| emscripten_console_log("__wasi_fd_read has been temporarily stubbed and is inert"); | ||
| abort(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend implementing this whole function in C/C++. That way the state will be multithreading safe from the get-go (e.g. for behavior like
fflush()ing stdout/stderr from another thread than the one(s) that have done partial printing). That method could use e.g.emscripten_console_log()andemscripten_console_error()for the printing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the general case I agree, but for stdout I think we need to proxy to the main thread anyhow - so that we can print it out on the page? If so, then I'm not sure how to make that multithreaded?