@@ -26,11 +26,11 @@ typedef struct {
2626 streams_own_pollable_t read_pollable ;
2727 streams_own_pollable_t write_pollable ;
2828#else
29- filesystem_tuple2_stream_u8_future_result_void_error_code_t read ;
30- filesystem_stream_u8_writer_t write ;
29+ wasip3_io_state_t read ;
30+ filesystem_future_result_void_error_code_t read_result ;
31+ wasip3_io_state_t write ;
3132 wasip3_subtask_t write_subtask ;
3233 filesystem_result_void_error_code_t write_pending_result ;
33- bool write_done ;
3434#endif
3535} file_t ;
3636
@@ -54,17 +54,11 @@ static void file_close_streams(void *data) {
5454 file -> write_stream .__handle = 0 ;
5555 }
5656#else
57- if (file -> read .f0 != 0 ) {
58- filesystem_stream_u8_drop_readable (file -> read .f0 );
59- file -> read .f0 = 0 ;
60- }
61- if (file -> read .f1 != 0 ) {
62- filesystem_future_result_void_error_code_drop_readable (file -> read .f1 );
63- file -> read .f1 = 0 ;
64- }
65- if (file -> write != 0 ) {
66- filesystem_stream_u8_drop_writable (file -> write );
67- file -> write = 0 ;
57+ wasip3_read_state_close (& file -> read );
58+ wasip3_write_state_close (& file -> write );
59+ if (file -> read_result != 0 ) {
60+ filesystem_future_result_void_error_code_drop_readable (file -> read_result );
61+ file -> read_result = 0 ;
6862 }
6963 if (file -> write_subtask != 0 ) {
7064 // TODO: this should use `wasip3_subtask_cancel` but right now that's buggy
@@ -76,7 +70,6 @@ static void file_close_streams(void *data) {
7670 __wasilibc_subtask_block_on_and_drop (file -> write_subtask );
7771 file -> write_subtask = 0 ;
7872 }
79- file -> write_done = false;
8073#endif
8174}
8275
@@ -91,13 +84,13 @@ static void file_free(void *data) {
9184static int file_read_eof (void * data ) {
9285 file_t * file = (file_t * )data ;
9386
94- if (file -> read . f1 != 0 ) {
87+ if (file -> read_result != 0 ) {
9588 filesystem_result_void_error_code_t result ;
96- __wasilibc_future_block_on (
97- filesystem_future_result_void_error_code_read ( file -> read . f1 , & result ),
98- file -> read . f1 );
99- filesystem_future_result_void_error_code_drop_readable (file -> read . f1 );
100- file -> read . f1 = 0 ;
89+ __wasilibc_future_block_on (filesystem_future_result_void_error_code_read (
90+ file -> read_result , & result ),
91+ file -> read_result );
92+ filesystem_future_result_void_error_code_drop_readable (file -> read_result );
93+ file -> read_result = 0 ;
10194 if (result .is_err ) {
10295 translate_error (result .val .err );
10396 return -1 ;
@@ -123,12 +116,15 @@ static int file_get_read_stream(void *data, wasi_read_t *read) {
123116 read -> input = streams_borrow_input_stream (file -> read_stream );
124117 read -> pollable = & file -> read_pollable ;
125118#else
126- if (file -> read .f0 == 0 ) {
119+ if (!wasip3_io_state_present (& file -> read )) {
120+ assert (!file -> read_result );
121+ filesystem_tuple2_stream_u8_future_result_void_error_code_t result ;
127122 filesystem_method_descriptor_read_via_stream (
128- filesystem_borrow_descriptor (file -> file_handle ), file -> offset ,
129- & file -> read );
123+ filesystem_borrow_descriptor (file -> file_handle ), file -> offset , & result );
124+ wasip3_io_state_init (& file -> read , result .f0 );
125+ file -> read_result = result .f1 ;
130126 }
131- read -> stream = file -> read . f0 ;
127+ read -> state = & file -> read ;
132128 read -> eof = file_read_eof ;
133129 read -> eof_data = data ;
134130#endif
@@ -178,9 +174,10 @@ static int file_get_write_stream(void *data, wasi_write_t *write) {
178174 write -> output = streams_borrow_output_stream (file -> write_stream );
179175 write -> pollable = & file -> write_pollable ;
180176#else
181- if (file -> write == 0 ) {
177+ if (! wasip3_io_state_present ( & file -> write ) ) {
182178 assert (file -> write_subtask == 0 );
183- filesystem_stream_u8_t write_read = filesystem_stream_u8_new (& file -> write );
179+ filesystem_stream_u8_writer_t writer ;
180+ filesystem_stream_u8_t write_read = filesystem_stream_u8_new (& writer );
184181 wasip3_subtask_status_t status ;
185182 if (file -> oflag & O_APPEND ) {
186183 status = filesystem_method_descriptor_append_via_stream (
@@ -193,11 +190,11 @@ static int file_get_write_stream(void *data, wasi_write_t *write) {
193190 }
194191 if (WASIP3_SUBTASK_STATE (status ) != WASIP3_SUBTASK_RETURNED )
195192 file -> write_subtask = WASIP3_SUBTASK_HANDLE (status );
193+ wasip3_io_state_init (& file -> write , writer );
196194 }
197- write -> output = file -> write ;
195+ write -> state = & file -> write ;
198196 write -> eof = file_write_eof ;
199197 write -> eof_data = data ;
200- write -> done = & file -> write_done ;
201198#endif
202199 write -> offset = & file -> offset ;
203200 write -> timeout = 0 ;
0 commit comments