From 88f1dfa78fd9304309dc32449b96e645e47d3455 Mon Sep 17 00:00:00 2001 From: Thomas Coopman Date: Sat, 12 Feb 2022 09:39:45 +0100 Subject: [PATCH 1/2] flush output_channel if length over max length --- runtime/io.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/runtime/io.js b/runtime/io.js index 53fd5b2762..379be2a5cc 100644 --- a/runtime/io.js +++ b/runtime/io.js @@ -379,8 +379,13 @@ function caml_ml_output_bytes(chanid,buffer,offset,len) { var string = caml_string_of_bytes(bytes); var jsstring = caml_jsbytes_of_string(string); var id = jsstring.lastIndexOf("\n"); - if(id < 0) + var total_length = jsstring.length + chan.buffer.length; + if(id < 0) { chan.buffer+=jsstring; + if (total_length > 65536) { + caml_ml_flush (chanid); + } + } else { chan.buffer+=jsstring.substr(0,id+1); caml_ml_flush (chanid); From 3e94bdc209315ad906f42dbcf01915dbdf5eb5c9 Mon Sep 17 00:00:00 2001 From: Thomas Coopman Date: Sat, 12 Feb 2022 10:13:48 +0100 Subject: [PATCH 2/2] implement my proposed solution --- runtime/io.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/runtime/io.js b/runtime/io.js index 379be2a5cc..9bf9403cc6 100644 --- a/runtime/io.js +++ b/runtime/io.js @@ -366,6 +366,7 @@ function caml_ml_flush (chanid) { //Requires: caml_ml_flush,caml_ml_bytes_length //Requires: caml_create_bytes, caml_blit_bytes, caml_raise_sys_error, caml_ml_channels, caml_string_of_bytes //Requires: caml_jsbytes_of_string +var MAX_LENGTH = 65536; function caml_ml_output_bytes(chanid,buffer,offset,len) { var chan = caml_ml_channels[chanid]; if(! chan.opened) caml_raise_sys_error("Cannot output to a closed channel"); @@ -380,16 +381,15 @@ function caml_ml_output_bytes(chanid,buffer,offset,len) { var jsstring = caml_jsbytes_of_string(string); var id = jsstring.lastIndexOf("\n"); var total_length = jsstring.length + chan.buffer.length; - if(id < 0) { + if (total_length > MAX_LENGTH) { chan.buffer+=jsstring; - if (total_length > 65536) { - caml_ml_flush (chanid); - } - } - else { + caml_ml_flush (chanid); + } else if(id >= 0) { chan.buffer+=jsstring.substr(0,id+1); caml_ml_flush (chanid); chan.buffer += jsstring.substr(id+1); + } else { + chan.buffer+=jsstring; } return 0; }