55 */
66
77var Fetch = {
8- // Map of integer fetch id to XHR request object
9- xhrs : { } ,
8+ // HandleAllocator for XHR request object
9+ // xhrs: undefined ,
1010
11- // The web worker that runs proxied file I/O requests. (this field is populated on demand, start as undefined to save code size)
11+ // The web worker that runs proxied file I/O requests. (this field is
12+ // populated on demand, start as undefined to save code size)
1213 // worker: undefined,
1314
1415 // Specifies an instance to the IndexedDB database. The database is opened
15- // as a preload step before the Emscripten application starts. (this field is populated on demand, start as undefined to save code size)
16+ // as a preload step before the Emscripten application starts. (this field is
17+ // populated on demand, start as undefined to save code size)
1618 // dbInstance: undefined,
1719
1820#if FETCH_SUPPORT_INDEXEDDB
@@ -40,8 +42,12 @@ var Fetch = {
4042 } ,
4143#endif
4244
43- staticInit : function ( ) {
45+ init : function ( ) {
46+ Fetch . xhrs = new HandleAllocator ( ) ;
4447#if FETCH_SUPPORT_INDEXEDDB
48+ #if PTHREADS
49+ if ( ENVIRONMENT_IS_PTHREAD ) return ;
50+ #endif
4551 var onsuccess = ( db ) => {
4652#if FETCH_DEBUG
4753 dbg ( 'fetch: IndexedDB successfully opened.' ) ;
@@ -293,8 +299,12 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
293299 xhr . setRequestHeader ( keyStr , valueStr ) ;
294300 }
295301 }
296- var id = { { { makeGetValue ( 'fetch' , C_STRUCTS . emscripten_fetch_t . id , 'u32' ) } } } ;
297- Fetch . xhrs [ id ] = xhr ;
302+
303+ var id = Fetch . xhrs . allocate ( xhr ) ;
304+ #if FETCH_DEBUG
305+ dbg ( 'fetch: id=' + id ) ;
306+ #endif
307+ { { { makeSetValue ( 'fetch' , C_STRUCTS . emscripten_fetch_t . id , 'id' , 'u32' ) } } } ;
298308 var data = ( dataPtr && dataLength ) ? HEAPU8 . slice ( dataPtr , dataPtr + dataLength ) : null ;
299309 // TODO: Support specifying custom headers to the request.
300310
@@ -334,7 +344,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
334344
335345 xhr . onload = ( e ) => {
336346 // check if xhr was aborted by user and don't try to call back
337- if ( ! ( id in Fetch . xhrs ) ) {
347+ if ( ! Fetch . xhrs . has ( id ) ) {
338348 return ;
339349 }
340350 saveResponseAndStatus ( ) ;
@@ -352,7 +362,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
352362 } ;
353363 xhr . onerror = ( e ) => {
354364 // check if xhr was aborted by user and don't try to call back
355- if ( ! ( id in Fetch . xhrs ) ) {
365+ if ( ! Fetch . xhrs . has ( id ) ) {
356366 return ;
357367 }
358368#if FETCH_DEBUG
@@ -363,7 +373,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
363373 } ;
364374 xhr . ontimeout = ( e ) => {
365375 // check if xhr was aborted by user and don't try to call back
366- if ( ! ( id in Fetch . xhrs ) ) {
376+ if ( ! Fetch . xhrs . has ( id ) ) {
367377 return ;
368378 }
369379#if FETCH_DEBUG
@@ -373,7 +383,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
373383 } ;
374384 xhr . onprogress = ( e ) => {
375385 // check if xhr was aborted by user and don't try to call back
376- if ( ! ( id in Fetch . xhrs ) ) {
386+ if ( ! Fetch . xhrs . has ( id ) ) {
377387 return ;
378388 }
379389 var ptrLen = ( fetchAttrLoadToMemory && fetchAttrStreamData && xhr . response ) ? xhr . response . byteLength : 0 ;
@@ -405,7 +415,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
405415 } ;
406416 xhr . onreadystatechange = ( e ) => {
407417 // check if xhr was aborted by user and don't try to call back
408- if ( ! ( id in Fetch . xhrs ) ) {
418+ if ( ! Fetch . xhrs . has ( id ) ) {
409419 { { { runtimeKeepalivePop ( ) } } }
410420 return ;
411421 }
@@ -560,27 +570,27 @@ function startFetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
560570}
561571
562572function fetchGetResponseHeadersLength ( id ) {
563- return lengthBytesUTF8 ( Fetch . xhrs [ id ] . getAllResponseHeaders ( ) ) + 1 ;
573+ return lengthBytesUTF8 ( Fetch . xhrs . get ( id ) . getAllResponseHeaders ( ) ) + 1 ;
564574}
565575
566576function fetchGetResponseHeaders ( id , dst , dstSizeBytes ) {
567- var responseHeaders = Fetch . xhrs [ id ] . getAllResponseHeaders ( ) ;
568- var lengthBytes = lengthBytesUTF8 ( responseHeaders ) + 1 ;
569- stringToUTF8 ( responseHeaders , dst , dstSizeBytes ) ;
570- return Math . min ( lengthBytes , dstSizeBytes ) ;
577+ var responseHeaders = Fetch . xhrs . get ( id ) . getAllResponseHeaders ( ) ;
578+ var lengthBytes = lengthBytesUTF8 ( responseHeaders ) + 1 ;
579+ stringToUTF8 ( responseHeaders , dst , dstSizeBytes ) ;
580+ return Math . min ( lengthBytes , dstSizeBytes ) ;
571581}
572582
573583//Delete the xhr JS object, allowing it to be garbage collected.
574584function fetchFree ( id ) {
575585#if FETCH_DEBUG
576- dbg ( "fetch: Deleting id:" + id + " of " + Fetch . xhrs ) ;
586+ dbg ( "fetch: fetchFree id:" + id ) ;
577587#endif
578- var xhr = Fetch . xhrs [ id ] ;
579- if ( xhr ) {
580- delete Fetch . xhrs [ id ] ;
581- // check if fetch is still in progress and should be aborted
582- if ( xhr . readyState > 0 && xhr . readyState < 4 ) {
583- xhr . abort ( ) ;
584- }
588+ if ( Fetch . xhrs . has ( id ) ) {
589+ var xhr = Fetch . xhrs . get ( id ) ;
590+ Fetch . xhrs . free ( id ) ;
591+ // check if fetch is still in progress and should be aborted
592+ if ( xhr . readyState > 0 && xhr . readyState < 4 ) {
593+ xhr . abort ( ) ;
585594 }
595+ }
586596}
0 commit comments