Skip to content
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

[wasm64] Fix library_websocket.js #21527

Merged
merged 1 commit into from
Mar 14, 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
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
Loading