MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

write stream edge case fixes

+26 -20
+26 -20
src/streams/writable.c
··· 102 102 return js_get_slot(ctrl_obj, SLOT_RS_SIZE); 103 103 } 104 104 105 + static inline ant_value_t ws_ctrl_sink(ant_value_t ctrl_obj) { 106 + return js_get_slot(ctrl_obj, SLOT_CTOR); 107 + } 108 + 105 109 static inline ant_value_t ws_ctrl_queue(ant_value_t ctrl_obj) { 106 110 return js_get_slot(ctrl_obj, SLOT_BUFFER); 107 111 } ··· 208 212 } 209 213 210 214 static void ws_writer_ensure_ready_promise_rejected(ant_t *js, ant_value_t writer_obj, ant_value_t error) { 211 - ant_value_t ready = ws_writer_ready(writer_obj); 212 - if (is_undefined(ready)) { 213 - ready = js_mkpromise(js); 214 - js_set_slot(writer_obj, SLOT_WS_READY, ready); 215 - } 215 + ant_value_t ready = js_mkpromise(js); 216 216 js_reject_promise(js, ready, error); 217 + js_set_slot(writer_obj, SLOT_WS_READY, ready); 217 218 } 218 219 219 220 static void ws_writer_ensure_closed_promise_rejected(ant_t *js, ant_value_t writer_obj, ant_value_t error) { 220 - ant_value_t closed = ws_writer_closed(writer_obj); 221 - if (is_undefined(closed)) { 222 - closed = js_mkpromise(js); 223 - js_set_slot(writer_obj, SLOT_RS_CLOSED, closed); 224 - } 221 + ant_value_t closed = js_mkpromise(js); 225 222 js_reject_promise(js, closed, error); 223 + js_set_slot(writer_obj, SLOT_RS_CLOSED, closed); 226 224 } 227 225 228 226 static void writable_stream_start_erroring(ant_t *js, ant_value_t stream_obj, ant_value_t reason) { ··· 281 279 282 280 ant_value_t ctrl_obj = ws_stream_controller(stream_obj); 283 281 ws_controller_t *ctrl = ws_get_controller(ctrl_obj); 282 + 283 + ant_value_t saved_abort_fn = ws_ctrl_abort_fn(ctrl_obj); 284 + ant_value_t saved_sink = ws_ctrl_sink(ctrl_obj); 285 + 284 286 if (ctrl) { 285 287 ctrl->queue_total_size = 0; 286 288 ctrl->queue_sizes_len = 0; 287 289 } 288 - 289 290 ws_default_controller_clear_algorithms(ctrl_obj); 290 291 ant_value_t stored_error = ws_stream_stored_error(stream_obj); 291 292 ··· 309 310 return; 310 311 } 311 312 312 - ant_value_t abort_fn = ws_ctrl_abort_fn(ctrl_obj); 313 313 ant_value_t result = js_mkundef(); 314 - if (is_callable(abort_fn)) { 314 + if (is_callable(saved_abort_fn)) { 315 315 ant_value_t abort_args[1] = { stored_error }; 316 - result = sv_vm_call(js->vm, js, abort_fn, js_mkundef(), abort_args, 1, NULL, false); 316 + result = sv_vm_call(js->vm, js, saved_abort_fn, saved_sink, abort_args, 1, NULL, false); 317 317 } 318 318 319 319 if (is_err(result)) { ··· 472 472 writable_stream_mark_first_write_in_flight(js, stream_obj); 473 473 474 474 ant_value_t write_fn = ws_ctrl_write_fn(ctrl_obj); 475 + ant_value_t sink = ws_ctrl_sink(ctrl_obj); 475 476 ant_value_t result = js_mkundef(); 476 477 if (is_callable(write_fn)) { 477 478 ant_value_t write_args[2] = { chunk, ctrl_obj }; 478 - result = sv_vm_call(js->vm, js, write_fn, js_mkundef(), write_args, 2, NULL, false); 479 + result = sv_vm_call(js->vm, js, write_fn, sink, write_args, 2, NULL, false); 479 480 } 480 481 481 482 if (is_err(result)) { ··· 531 532 } 532 533 533 534 ant_value_t close_fn = ws_ctrl_close_fn(ctrl_obj); 535 + ant_value_t sink = ws_ctrl_sink(ctrl_obj); 534 536 ws_default_controller_clear_algorithms(ctrl_obj); 535 537 536 538 ant_value_t result = js_mkundef(); 537 539 if (is_callable(close_fn)) 538 - result = sv_vm_call(js->vm, js, close_fn, js_mkundef(), NULL, 0, NULL, false); 540 + result = sv_vm_call(js->vm, js, close_fn, sink, NULL, 0, NULL, false); 539 541 540 542 if (is_err(result)) { 541 543 ant_value_t thrown = js->thrown_value; ··· 1001 1003 } 1002 1004 1003 1005 static ant_value_t setup_ws_default_controller( 1004 - ant_t *js, ant_value_t stream_obj, 1006 + ant_t *js, ant_value_t stream_obj, ant_value_t underlying_sink, 1005 1007 ant_value_t write_fn, ant_value_t close_fn, ant_value_t abort_fn, 1006 1008 ant_value_t size_fn, double hwm 1007 1009 ) { ··· 1017 1019 js_set_slot(ctrl_obj, SLOT_WS_CLOSE, close_fn); 1018 1020 js_set_slot(ctrl_obj, SLOT_WS_ABORT, abort_fn); 1019 1021 js_set_slot(ctrl_obj, SLOT_RS_SIZE, size_fn); 1022 + js_set_slot(ctrl_obj, SLOT_CTOR, underlying_sink); 1020 1023 js_set_slot(ctrl_obj, SLOT_BUFFER, js_mkarr(js)); 1021 1024 js_set_finalizer(ctrl_obj, ws_controller_finalize); 1022 1025 1023 1026 ant_value_t ac_ctor = js_get(js, js_glob(js), "AbortController"); 1024 1027 ant_value_t ac = js_mkundef(); 1025 1028 if (is_callable(ac_ctor)) { 1029 + ant_value_t ac_proto = js_get(js, ac_ctor, "prototype"); 1030 + ac = js_mkobj(js); 1031 + if (is_object_type(ac_proto)) js_set_proto_init(ac, ac_proto); 1026 1032 ant_value_t saved = js->new_target; 1027 1033 js->new_target = ac_ctor; 1028 - ac = sv_vm_call(js->vm, js, ac_ctor, js_mkundef(), NULL, 0, NULL, false); 1034 + ant_value_t result = sv_vm_call(js->vm, js, ac_ctor, ac, NULL, 0, NULL, false); 1029 1035 js->new_target = saved; 1030 - if (is_err(ac)) ac = js_mkundef(); 1036 + if (is_err(result)) ac = js_mkundef(); 1031 1037 } 1032 1038 1033 1039 js_set_slot(ctrl_obj, SLOT_WS_SIGNAL, ac); ··· 1123 1129 } 1124 1130 } 1125 1131 1126 - ant_value_t ctrl_obj = setup_ws_default_controller(js, obj, write_fn, close_fn, abort_fn, size_fn, hwm); 1132 + ant_value_t ctrl_obj = setup_ws_default_controller(js, obj, underlying_sink, write_fn, close_fn, abort_fn, size_fn, hwm); 1127 1133 if (is_err(ctrl_obj)) return ctrl_obj; 1128 1134 1129 1135 if (is_callable(start_fn)) {