Skip to content

[library_sockfs.js] Cleanup buffer handling in sendmsg #22987

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 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/library_sockfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,16 +619,13 @@ addToLibrary({
buffer = buffer.buffer;
}

var data;
#if PTHREADS
// WebSockets .send() does not allow passing a SharedArrayBuffer, so clone the portion of the SharedArrayBuffer as a regular
// ArrayBuffer that we want to send.
if (buffer instanceof SharedArrayBuffer) {
data = new Uint8Array(new Uint8Array(buffer.slice(offset, offset + length))).buffer;
} else {
#endif
data = buffer.slice(offset, offset + length);
var data = buffer.slice(offset, offset + length);
#if PTHREADS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separately, should this be #if SHARED_MEMORY so it works with WASM_WORKERS?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed... maybe a followup?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

// WebSockets .send() does not allow passing a SharedArrayBuffer, so
// clone the the SharedArrayBuffer as regular ArrayBuffer before
// sending.
if (data instanceof SharedArrayBuffer) {
data = new Uint8Array(new Uint8Array(data)).buffer;
}
#endif

Expand Down
1 change: 1 addition & 0 deletions test/test_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ def test_enet(self):
'native': [WebsockifyServerHarness, 59160, ['-DTEST_DGRAM=0']],
'tcp': [CompiledServerHarness, 59162, ['-DTEST_DGRAM=0']],
'udp': [CompiledServerHarness, 59164, ['-DTEST_DGRAM=1']],
'pthread': [CompiledServerHarness, 59165, ['-pthread', '-sPROXY_TO_PTHREAD']],
})
def test_nodejs_sockets_echo(self, harness_class, port, args):
if harness_class == WebsockifyServerHarness and common.EMTEST_LACKS_NATIVE_CLANG:
Expand Down