File tree Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Expand file tree Collapse file tree 1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,8 @@ export class WASIProcExit extends Error {
1616}
1717
1818export default class WASI {
19+ #freeFds: Array < number > = [ ] ;
20+
1921 args : Array < string > = [ ] ;
2022 env : Array < string > = [ ] ;
2123 fds : Array < Fd > = [ ] ;
@@ -208,6 +210,7 @@ export default class WASI {
208210 if ( self . fds [ fd ] != undefined ) {
209211 const ret = self . fds [ fd ] . fd_close ( ) ;
210212 self . fds [ fd ] = undefined ;
213+ self . #freeFds. push ( fd ) ;
211214 return ret ;
212215 } else {
213216 return wasi . ERRNO_BADF ;
@@ -497,6 +500,7 @@ export default class WASI {
497500 }
498501 self . fds [ to ] = self . fds [ fd ] ;
499502 self . fds [ fd ] = undefined ;
503+ self . #freeFds. push ( fd ) ;
500504 return 0 ;
501505 } else {
502506 return wasi . ERRNO_BADF ;
@@ -691,9 +695,16 @@ export default class WASI {
691695 if ( ret != 0 ) {
692696 return ret ;
693697 }
694- // FIXME use first free fd
695- self . fds . push ( fd_obj ) ;
696- const opened_fd = self . fds . length - 1 ;
698+ const opened_fd = ( ( ) => {
699+ if ( self . #freeFds. length > 0 ) {
700+ const fd = self . #freeFds. pop ( ) ;
701+ self . fds [ fd ] = fd_obj ;
702+ return fd ;
703+ }
704+
705+ self . fds . push ( fd_obj ) ;
706+ return self . fds . length - 1 ;
707+ } ) ( ) ;
697708 buffer . setUint32 ( opened_fd_ptr , opened_fd , true ) ;
698709 return 0 ;
699710 } else {
You can’t perform that action at this time.
0 commit comments