Skip to content

Commit e1c39bc

Browse files
authored
Use HandleAlloctor in library_websocket.js. NFC (#21531)
1 parent 7c72ad5 commit e1c39bc

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/library.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3561,15 +3561,15 @@ addToLibrary({
35613561
assert(this.allocated[id] !== undefined, `invalid handle: ${id}`);
35623562
#endif
35633563
return this.allocated[id];
3564-
};
3564+
}
35653565
has(id) {
35663566
return this.allocated[id] !== undefined;
3567-
};
3567+
}
35683568
allocate(handle) {
35693569
var id = this.freelist.pop() || this.allocated.length;
35703570
this.allocated[id] = handle;
35713571
return id;
3572-
};
3572+
}
35733573
free(id) {
35743574
#if ASSERTIONS
35753575
assert(this.allocated[id] !== undefined);
@@ -3578,7 +3578,7 @@ addToLibrary({
35783578
// apparently arrays with holes in them can be less efficient.
35793579
this.allocated[id] = undefined;
35803580
this.freelist.push(id);
3581-
};
3581+
}
35823582
},
35833583

35843584
$getNativeTypeSize__deps: ['$POINTER_SIZE'],

src/library_websocket.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
55
*/
66

77
var LibraryWebSocket = {
8+
$webSockets__deps: ['$HandleAllocator'],
9+
$webSockets: "new HandleAllocator();",
10+
11+
$WS__deps: ['$webSockets'],
812
$WS: {
9-
sockets: [null],
1013
socketEvent: null,
14+
getSocket(socketId) {
15+
if (!webSockets.has(socketId)) {
16+
return 0;
17+
}
18+
return webSockets.get(socketId);
19+
},
1120
getSocketEvent(socketId) {
1221
// Singleton event pointer. Use EmscriptenWebSocketCloseEvent, which is
1322
// the largest event struct
@@ -20,7 +29,7 @@ var LibraryWebSocket = {
2029
emscripten_websocket_get_ready_state__deps: ['$WS'],
2130
emscripten_websocket_get_ready_state__proxy: 'sync',
2231
emscripten_websocket_get_ready_state: (socketId, readyState) => {
23-
var socket = WS.sockets[socketId];
32+
var socket = WS.getSocket(socketId);
2433
if (!socket) {
2534
#if WEBSOCKET_DEBUG
2635
dbg(`emscripten_websocket_get_ready_state(): Invalid socket ID ${socketId} specified!`);
@@ -35,7 +44,7 @@ var LibraryWebSocket = {
3544
emscripten_websocket_get_buffered_amount__deps: ['$WS'],
3645
emscripten_websocket_get_buffered_amount__proxy: 'sync',
3746
emscripten_websocket_get_buffered_amount: (socketId, bufferedAmount) => {
38-
var socket = WS.sockets[socketId];
47+
var socket = WS.getSocket(socketId);
3948
if (!socket) {
4049
#if WEBSOCKET_DEBUG
4150
dbg(`emscripten_websocket_get_buffered_amount(): Invalid socket ID ${socketId} specified!`);
@@ -50,7 +59,7 @@ var LibraryWebSocket = {
5059
emscripten_websocket_get_extensions__deps: ['$WS', '$stringToUTF8'],
5160
emscripten_websocket_get_extensions__proxy: 'sync',
5261
emscripten_websocket_get_extensions: (socketId, extensions, extensionsLength) => {
53-
var socket = WS.sockets[socketId];
62+
var socket = WS.getSocket(socketId);
5463
if (!socket) {
5564
#if WEBSOCKET_DEBUG
5665
dbg(`emscripten_websocket_get_extensions(): Invalid socket ID ${socketId} specified!`);
@@ -65,7 +74,7 @@ var LibraryWebSocket = {
6574
emscripten_websocket_get_extensions_length__deps: ['$WS'],
6675
emscripten_websocket_get_extensions_length__proxy: 'sync',
6776
emscripten_websocket_get_extensions_length: (socketId, extensionsLength) => {
68-
var socket = WS.sockets[socketId];
77+
var socket = WS.getSocket(socketId);
6978
if (!socket) {
7079
#if WEBSOCKET_DEBUG
7180
dbg(`emscripten_websocket_get_extensions_length(): Invalid socket ID ${socketId} specified!`);
@@ -80,7 +89,7 @@ var LibraryWebSocket = {
8089
emscripten_websocket_get_protocol__deps: ['$WS', '$stringToUTF8'],
8190
emscripten_websocket_get_protocol__proxy: 'sync',
8291
emscripten_websocket_get_protocol: (socketId, protocol, protocolLength) => {
83-
var socket = WS.sockets[socketId];
92+
var socket = WS.getSocket(socketId);
8493
if (!socket) {
8594
#if WEBSOCKET_DEBUG
8695
dbg(`emscripten_websocket_get_protocol(): Invalid socket ID ${socketId} specified!`);
@@ -95,7 +104,7 @@ var LibraryWebSocket = {
95104
emscripten_websocket_get_protocol_length__deps: ['$WS'],
96105
emscripten_websocket_get_protocol_length__proxy: 'sync',
97106
emscripten_websocket_get_protocol_length: (socketId, protocolLength) => {
98-
var socket = WS.sockets[socketId];
107+
var socket = WS.getSocket(socketId);
99108
if (!socket) {
100109
#if WEBSOCKET_DEBUG
101110
dbg(`emscripten_websocket_get_protocol_length(): Invalid socket ID ${socketId} specified!`);
@@ -110,7 +119,7 @@ var LibraryWebSocket = {
110119
emscripten_websocket_get_url__deps: ['$WS', '$stringToUTF8'],
111120
emscripten_websocket_get_url__proxy: 'sync',
112121
emscripten_websocket_get_url: (socketId, url, urlLength) => {
113-
var socket = WS.sockets[socketId];
122+
var socket = WS.getSocket(socketId);
114123
if (!socket) {
115124
#if WEBSOCKET_DEBUG
116125
dbg(`emscripten_websocket_get_url(): Invalid socket ID ${socketId} specified!`);
@@ -125,7 +134,7 @@ var LibraryWebSocket = {
125134
emscripten_websocket_get_url_length__deps: ['$WS'],
126135
emscripten_websocket_get_url_length__proxy: 'sync',
127136
emscripten_websocket_get_url_length: (socketId, urlLength) => {
128-
var socket = WS.sockets[socketId];
137+
var socket = WS.getSocket(socketId);
129138
if (!socket) {
130139
#if WEBSOCKET_DEBUG
131140
dbg(`emscripten_websocket_get_url_length(): Invalid socket ID ${socketId} specified!`);
@@ -144,7 +153,7 @@ var LibraryWebSocket = {
144153
// if (thread == {{{ cDefs.EM_CALLBACK_THREAD_CONTEXT_CALLING_THREAD }}} ||
145154
// (thread == _pthread_self()) return emscripten_websocket_set_onopen_callback_on_calling_thread(socketId, userData, callbackFunc);
146155
var eventPtr = WS.getSocketEvent(socketId);
147-
var socket = WS.sockets[socketId];
156+
var socket = WS.getSocket(socketId);
148157
if (!socket) {
149158
#if WEBSOCKET_DEBUG
150159
dbg(`emscripten_websocket_set_onopen_callback(): Invalid socket ID ${socketId} specified!`);
@@ -168,7 +177,7 @@ var LibraryWebSocket = {
168177
emscripten_websocket_set_onerror_callback_on_thread__proxy: 'sync',
169178
emscripten_websocket_set_onerror_callback_on_thread: (socketId, userData, callbackFunc, thread) => {
170179
var eventPtr = WS.getSocketEvent(socketId);
171-
var socket = WS.sockets[socketId];
180+
var socket = WS.getSocket(socketId);
172181
if (!socket) {
173182
#if WEBSOCKET_DEBUG
174183
dbg(`emscripten_websocket_set_onerror_callback(): Invalid socket ID ${socketId} specified!`);
@@ -192,7 +201,7 @@ var LibraryWebSocket = {
192201
emscripten_websocket_set_onclose_callback_on_thread__proxy: 'sync',
193202
emscripten_websocket_set_onclose_callback_on_thread: (socketId, userData, callbackFunc, thread) => {
194203
var eventPtr = WS.getSocketEvent(socketId);
195-
var socket = WS.sockets[socketId];
204+
var socket = WS.getSocket(socketId);
196205
if (!socket) {
197206
#if WEBSOCKET_DEBUG
198207
dbg(`emscripten_websocket_set_onclose_callback(): Invalid socket ID ${socketId} specified!`);
@@ -219,7 +228,7 @@ var LibraryWebSocket = {
219228
emscripten_websocket_set_onmessage_callback_on_thread__proxy: 'sync',
220229
emscripten_websocket_set_onmessage_callback_on_thread: (socketId, userData, callbackFunc, thread) => {
221230
var eventPtr = WS.getSocketEvent(socketId);
222-
var socket = WS.sockets[socketId];
231+
var socket = WS.getSocket(socketId);
223232
if (!socket) {
224233
#if WEBSOCKET_DEBUG
225234
dbg(`emscripten_websocket_set_onmessage_callback(): Invalid socket ID ${socketId} specified!`);
@@ -291,8 +300,7 @@ var LibraryWebSocket = {
291300
// We always marshal received WebSocket data back to Wasm, so enable receiving the data as arraybuffers for easy marshalling.
292301
socket.binaryType = 'arraybuffer';
293302
// TODO: While strictly not necessary, this ID would be good to be unique across all threads to avoid confusion.
294-
var socketId = WS.sockets.length;
295-
WS.sockets[socketId] = socket;
303+
var socketId = webSockets.allocate(socket);
296304

297305
#if WEBSOCKET_DEBUG
298306
dbg(`emscripten_websocket_new(url=${url}, protocols=${protocols ? UTF8ToString(protocols).split(',') : 'null'}): created socket ID ${socketId})`);
@@ -303,7 +311,7 @@ var LibraryWebSocket = {
303311
emscripten_websocket_send_utf8_text__deps: ['$WS'],
304312
emscripten_websocket_send_utf8_text__proxy: 'sync',
305313
emscripten_websocket_send_utf8_text: (socketId, textData) => {
306-
var socket = WS.sockets[socketId];
314+
var socket = WS.getSocket(socketId);
307315
if (!socket) {
308316
#if WEBSOCKET_DEBUG
309317
dbg(`emscripten_websocket_send_utf8_text(): Invalid socket ID ${socketId} specified!`);
@@ -326,7 +334,7 @@ var LibraryWebSocket = {
326334
emscripten_websocket_send_binary__deps: ['$WS'],
327335
emscripten_websocket_send_binary__proxy: 'sync',
328336
emscripten_websocket_send_binary: (socketId, binaryData, dataLength) => {
329-
var socket = WS.sockets[socketId];
337+
var socket = WS.getSocket(socketId);
330338
if (!socket) {
331339
#if WEBSOCKET_DEBUG
332340
dbg(`emscripten_websocket_send_binary(): Invalid socket ID ${socketId} specified!`);
@@ -357,7 +365,7 @@ var LibraryWebSocket = {
357365
emscripten_websocket_close__deps: ['$WS'],
358366
emscripten_websocket_close__proxy: 'sync',
359367
emscripten_websocket_close: (socketId, code, reason) => {
360-
var socket = WS.sockets[socketId];
368+
var socket = WS.getSocket(socketId);
361369
if (!socket) {
362370
#if WEBSOCKET_DEBUG
363371
dbg(`emscripten_websocket_close(): Invalid socket ID ${socketId} specified!`);
@@ -383,7 +391,7 @@ var LibraryWebSocket = {
383391
emscripten_websocket_delete__deps: ['$WS'],
384392
emscripten_websocket_delete__proxy: 'sync',
385393
emscripten_websocket_delete: (socketId) => {
386-
var socket = WS.sockets[socketId];
394+
var socket = WS.getSocket(socketId);
387395
if (!socket) {
388396
#if WEBSOCKET_DEBUG
389397
dbg(`emscripten_websocket_delete(): Invalid socket ID ${socketId} specified!`);
@@ -395,7 +403,7 @@ var LibraryWebSocket = {
395403
dbg(`emscripten_websocket_delete(socketId=${socketId})`);
396404
#endif
397405
socket.onopen = socket.onerror = socket.onclose = socket.onmessage = null;
398-
delete WS.sockets[socketId];
406+
webSockets.free(socketId);
399407
return {{{ cDefs.EMSCRIPTEN_RESULT_SUCCESS }}};
400408
},
401409

0 commit comments

Comments
 (0)