Skip to content

Commit

Permalink
[wasm64] Fix library_websocket.js (#21527)
Browse files Browse the repository at this point in the history
Update `test_websocket_send.c` such that it checks that both text
and binary messages are received.

Also, delete `websocket.c` which I think was supposed to be removed
as part of #7672 (where it was renamed to test_websocket_send.c).

Fixes: #21515
  • Loading branch information
sbc100 authored Mar 14, 2024
1 parent d0c4878 commit 46917a5
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 203 deletions.
16 changes: 16 additions & 0 deletions src/generated_struct_info32.json
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,22 @@
"renderViaOffscreenBackBuffer": 52,
"stencil": 8
},
"EmscriptenWebSocketCloseEvent": {
"__size__": 524,
"code": 8,
"reason": 10,
"wasClean": 4
},
"EmscriptenWebSocketCreateAttributes": {
"__size__": 12,
"protocols": 4
},
"EmscriptenWebSocketMessageEvent": {
"__size__": 16,
"data": 4,
"isText": 12,
"numBytes": 8
},
"EmscriptenWheelEvent": {
"__size__": 104,
"deltaMode": 96,
Expand Down
16 changes: 16 additions & 0 deletions src/generated_struct_info64.json
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,22 @@
"renderViaOffscreenBackBuffer": 52,
"stencil": 8
},
"EmscriptenWebSocketCloseEvent": {
"__size__": 524,
"code": 8,
"reason": 10,
"wasClean": 4
},
"EmscriptenWebSocketCreateAttributes": {
"__size__": 24,
"protocols": 8
},
"EmscriptenWebSocketMessageEvent": {
"__size__": 24,
"data": 8,
"isText": 20,
"numBytes": 16
},
"EmscriptenWheelEvent": {
"__size__": 104,
"deltaMode": 96,
Expand Down
55 changes: 26 additions & 29 deletions src/library_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
var LibraryWebSocket = {
$WS: {
sockets: [null],
socketEvent: null
socketEvent: null,
getSocketEvent(socketId) {
// Singleton event pointer. Use EmscriptenWebSocketCloseEvent, which is
// the largest event struct
this.socketEvent ||= _malloc({{{ C_STRUCTS.EmscriptenWebSocketCloseEvent.__size__ }}});
{{{ makeSetValue('this.socketEvent', 0, 'socketId', 'u32') }}};
return this.socketEvent;
},
},

emscripten_websocket_get_ready_state__deps: ['$WS'],
Expand Down Expand Up @@ -136,9 +143,7 @@ var LibraryWebSocket = {
// TODO:
// if (thread == {{{ cDefs.EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD }}} ||
// (thread == _pthread_self()) return emscripten_websocket_set_onopen_callback_on_calling_thread(socketId, userData, callbackFunc);

WS.socketEvent ||= _malloc(1024); // TODO: sizeof(EmscriptenWebSocketCloseEvent), which is the largest event struct

var eventPtr = WS.getSocketEvent(socketId);
var socket = WS.sockets[socketId];
if (!socket) {
#if WEBSOCKET_DEBUG
Expand All @@ -154,17 +159,15 @@ var LibraryWebSocket = {
#if WEBSOCKET_DEBUG
dbg(`websocket event "open": socketId=${socketId},userData=${userData},callbackFunc=${callbackFunc})`);
#endif
HEAPU32[WS.socketEvent>>2] = socketId;
{{{ makeDynCall('iipp', 'callbackFunc') }}}(0/*TODO*/, WS.socketEvent, userData);
{{{ makeDynCall('iipp', 'callbackFunc') }}}(0/*TODO*/, eventPtr, userData);
}
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
},

emscripten_websocket_set_onerror_callback_on_thread__deps: ['$WS'],
emscripten_websocket_set_onerror_callback_on_thread__proxy: 'sync',
emscripten_websocket_set_onerror_callback_on_thread: (socketId, userData, callbackFunc, thread) => {
WS.socketEvent ||= _malloc(1024); // TODO: sizeof(EmscriptenWebSocketCloseEvent), which is the largest event struct

var eventPtr = WS.getSocketEvent(socketId);
var socket = WS.sockets[socketId];
if (!socket) {
#if WEBSOCKET_DEBUG
Expand All @@ -180,17 +183,15 @@ var LibraryWebSocket = {
#if WEBSOCKET_DEBUG
dbg(`websocket event "error": socketId=${socketId},userData=${userData},callbackFunc=${callbackFunc})`);
#endif
HEAPU32[WS.socketEvent>>2] = socketId;
{{{ makeDynCall('iipp', 'callbackFunc') }}}(0/*TODO*/, WS.socketEvent, userData);
{{{ makeDynCall('iipp', 'callbackFunc') }}}(0/*TODO*/, eventPtr, userData);
}
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
},

emscripten_websocket_set_onclose_callback_on_thread__deps: ['$WS', '$stringToUTF8'],
emscripten_websocket_set_onclose_callback_on_thread__proxy: 'sync',
emscripten_websocket_set_onclose_callback_on_thread: (socketId, userData, callbackFunc, thread) => {
WS.socketEvent ||= _malloc(1024); // TODO: sizeof(EmscriptenWebSocketCloseEvent), which is the largest event struct

var eventPtr = WS.getSocketEvent(socketId);
var socket = WS.sockets[socketId];
if (!socket) {
#if WEBSOCKET_DEBUG
Expand All @@ -206,20 +207,18 @@ var LibraryWebSocket = {
#if WEBSOCKET_DEBUG
dbg(`websocket event "close": socketId=${socketId},userData=${userData},callbackFunc=${callbackFunc})`);
#endif
HEAPU32[WS.socketEvent>>2] = socketId;
HEAPU32[(WS.socketEvent+4)>>2] = e.wasClean;
HEAPU32[(WS.socketEvent+8)>>2] = e.code;
stringToUTF8(e.reason, WS.socketEvent+10, 512);
{{{ makeDynCall('iipp', 'callbackFunc') }}}(0/*TODO*/, WS.socketEvent, userData);
{{{ makeSetValue('eventPtr', C_STRUCTS.EmscriptenWebSocketCloseEvent.wasClean, 'e.wasClean', 'i32') }}},
{{{ makeSetValue('eventPtr', C_STRUCTS.EmscriptenWebSocketCloseEvent.wasClean, 'e.code', 'i16') }}},
stringToUTF8(e.reason, eventPtr + {{{ C_STRUCTS.EmscriptenWebSocketCloseEvent.reason }}}, 512);
{{{ makeDynCall('iipp', 'callbackFunc') }}}(0/*TODO*/, eventPtr, userData);
}
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
},

emscripten_websocket_set_onmessage_callback_on_thread__deps: ['$WS', '$stringToNewUTF8', 'malloc', 'free'],
emscripten_websocket_set_onmessage_callback_on_thread__proxy: 'sync',
emscripten_websocket_set_onmessage_callback_on_thread: (socketId, userData, callbackFunc, thread) => {
WS.socketEvent ||= _malloc(1024); // TODO: sizeof(EmscriptenWebSocketCloseEvent), which is the largest event struct

var eventPtr = WS.getSocketEvent(socketId);
var socket = WS.sockets[socketId];
if (!socket) {
#if WEBSOCKET_DEBUG
Expand All @@ -235,15 +234,14 @@ var LibraryWebSocket = {
#if WEBSOCKET_DEBUG == 2
dbg(`websocket event "message": socketId=${socketId},userData=${userData},callbackFunc=${callbackFunc})`);
#endif
HEAPU32[WS.socketEvent>>2] = socketId;
if (typeof e.data == 'string') {
var isText = typeof e.data == 'string';
if (isText) {
var buf = stringToNewUTF8(e.data);
var len = lengthBytesUTF8(e.data)+1;
#if WEBSOCKET_DEBUG
var s = (e.data.length < 256) ? e.data : (e.data.substr(0, 256) + ` (${e.data.length-256} more characters)`);
dbg(`WebSocket onmessage, received data: "${e.data}", ${e.data.length} chars, ${len} bytes encoded as UTF-8: "${s}"`);
#endif
HEAPU32[(WS.socketEvent+12)>>2] = 1; // text data
} else {
var len = e.data.byteLength;
var buf = _malloc(len);
Expand All @@ -258,11 +256,11 @@ var LibraryWebSocket = {

dbg(s);
#endif
HEAPU32[(WS.socketEvent+12)>>2] = 0; // binary data
}
HEAPU32[(WS.socketEvent+4)>>2] = buf;
HEAPU32[(WS.socketEvent+8)>>2] = len;
{{{ makeDynCall('iipp', 'callbackFunc') }}}(0/*TODO*/, WS.socketEvent, userData);
{{{ makeSetValue('eventPtr', C_STRUCTS.EmscriptenWebSocketMessageEvent.data, 'buf', '*') }}},
{{{ makeSetValue('eventPtr', C_STRUCTS.EmscriptenWebSocketMessageEvent.numBytes, 'len', 'i32') }}},
{{{ makeSetValue('eventPtr', C_STRUCTS.EmscriptenWebSocketMessageEvent.isText, 'isText', 'i32') }}},
{{{ makeDynCall('iipp', 'callbackFunc') }}}(0/*TODO*/, eventPtr, userData);
_free(buf);
}
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
Expand All @@ -284,9 +282,8 @@ var LibraryWebSocket = {
return {{{ cDefs.EMSCRIPTEN_RESULT_INVALID_PARAM }}};
}

var createAttrs = createAttributes>>2;
var url = UTF8ToString(HEAP32[createAttrs]);
var protocols = HEAP32[createAttrs+1];
var url = UTF8ToString({{{ makeGetValue('createAttributes', 0, '*') }}});
var protocols = {{{ makeGetValue('createAttributes', C_STRUCTS.EmscriptenWebSocketCreateAttributes.protocols, '*') }}}
// TODO: Add support for createOnMainThread==false; currently all WebSocket connections are created on the main thread.
// var createOnMainThread = HEAP32[createAttrs+2];

Expand Down
18 changes: 18 additions & 0 deletions src/struct_info.json
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,24 @@
]
}
},
{
"file": "emscripten/websocket.h",
"structs": {
"EmscriptenWebSocketCloseEvent": [
"wasClean",
"code",
"reason"
],
"EmscriptenWebSocketMessageEvent": [
"data",
"numBytes",
"isText"
],
"EmscriptenWebSocketCreateAttributes": [
"protocols"
]
}
},
{
"file": "AL/al.h",
"defines": [
Expand Down
Loading

0 comments on commit 46917a5

Please sign in to comment.