*** gtp.c 2009-02-18 06:41:31.000000000 +0900 --- gtp.c 2023-11-03 18:49:30.171279432 +0900 *************** *** 45,50 **** --- 45,52 ---- #include "gtp.h" + #include + /* These are copied from gnugo.h. We don't include this file in order * to remain as independent as possible of GNU Go internals. */ *************** static int current_id; *** 75,80 **** --- 77,107 ---- */ FILE *gtp_output_file = NULL; + /* for JavaScript line input for fgets(*,,stdin) only. */ + EM_JS(int, my_fgets, (char *buf, int maxlen), { + return Asyncify.handleAsync(async () => { + const mySleep = function(itvl) { + return new Promise(function(resolve) { + setTimeout(function(){resolve();}, itvl); + }); + }; + const waitBuffer = async function(itvl) { + while(Module.fgets_buffer.length <= 0) { + fgbuf = await mySleep(10); + } + }; + await waitBuffer(); + const input = Module.fgets_buffer.shift(); + const len = Math.min(input.length, maxlen); + if (len === input.length) { + HEAPU8.set(input, buf); + } else { + HEAPU8.set(input.subarray(0, len), buf); + Module.fgets_buffer.unshift(input.subarray(len)); + } + return len; + }); + }); /* Read filehandle gtp_input linewise and interpret as GTP commands. */ void *************** gtp_main_loop(struct gtp_command command *** 92,98 **** while (status == GTP_OK) { /* Read a line from gtp_input. */ ! if (!fgets(line, GTP_BUFSIZE, gtp_input)) break; /* EOF or some error */ if (gtp_dump_commands) { --- 119,125 ---- while (status == GTP_OK) { /* Read a line from gtp_input. */ ! if (!my_fgets(line, GTP_BUFSIZE)) break; /* EOF or some error */ if (gtp_dump_commands) {