@@ -195,6 +195,10 @@ WaitEventSet *FeBeWaitSet;
195195 * --------------------------------
196196 */
197197
198+ static StringInfoData emscripten_buffer ;
199+ static bool emscripten_buffer_is_initialized = false;
200+ static bool emscripten_buffer_busy = false;
201+
198202#ifdef EMSCRIPTEN
199203EM_JS (void , emscripten_dispatch_result , (char * res , int len ), {
200204 // Dispatch the result to JS land
@@ -207,61 +211,59 @@ EM_JS(void, emscripten_dispatch_result, (char *res, int len), {
207211});
208212#endif
209213
214+ static void emscripten_init_buffer (void ) {
215+ if (!emscripten_buffer_is_initialized ) {
216+ initStringInfo (& emscripten_buffer );
217+ emscripten_buffer_is_initialized = true;
218+ }
219+ }
220+
210221static void emscripten_comm_reset (void ) {
211- printf ("emscripten_comm_reset" );
222+ if (emscripten_buffer_is_initialized ) {
223+ resetStringInfo (& emscripten_buffer );
224+ } else {
225+ emscripten_init_buffer ();
226+ }
212227}
213228
214229static int emscripten_flush (void ) {
215- printf ("emscripten_flush" );
230+ if (emscripten_buffer .len > 0 ) {
231+ emscripten_dispatch_result (emscripten_buffer .data , emscripten_buffer .len );
232+ resetStringInfo (& emscripten_buffer );
233+ }
216234 return 0 ;
217235}
218236
219237static int emscripten_flush_if_writable (void ) {
220- printf ( "emscripten_flush_if_writable" );
238+ return emscripten_flush ( );
221239 return 0 ;
222240}
223241
224242static bool emscripten_is_send_pending (void ) {
225- printf ("emscripten_is_send_pending" );
226- return false;
243+ return emscripten_buffer .len > 0 ;
227244}
228245
229246static int emscripten_putmessage (char msgtype , const char * s , size_t len ) {
230- StringInfoData buf ;
231- uint32 n32 ;
232-
233- Assert (msgtype != 0 );
234-
235- if (PqCommBusy )
236- return 0 ;
237- PqCommBusy = true;
238-
239- // Initialize StringInfoData buffer
240- initStringInfo (& buf );
241-
242- // Append msgtype
243- appendStringInfoChar (& buf , msgtype );
244-
245- // Calculate message length (len + 4) and convert to network byte order
246- n32 = pg_hton32 ((uint32 ) (len + 4 ));
247- // Append length
248- appendBinaryStringInfo (& buf , (char * ) & n32 , 4 );
247+ if (emscripten_buffer_busy )
248+ return 0 ;
249+ emscripten_buffer_busy = true;
249250
250- // Append actual message
251- appendBinaryStringInfo (& buf , s , len );
251+ emscripten_init_buffer ();
252252
253- // Dispatch the result
254- emscripten_dispatch_result ( buf . data , buf . len );
253+ uint32 n32 ;
254+ Assert ( msgtype != 0 );
255255
256- // Free StringInfoData buffer memory
257- pfree (buf .data );
256+ appendStringInfoChar (& emscripten_buffer , msgtype );
257+ n32 = pg_hton32 ((uint32 ) (len + 4 ));
258+ appendBinaryStringInfo (& emscripten_buffer , (char * ) & n32 , 4 );
259+ appendBinaryStringInfo (& emscripten_buffer , s , len );
258260
259- PqCommBusy = false;
260- return 0 ;
261+ emscripten_buffer_busy = false;
262+ return 0 ;
261263
262264fail :
263- PqCommBusy = false;
264- return EOF ;
265+ emscripten_buffer_busy = false;
266+ return EOF ;
265267}
266268
267269static void emscripten_putmessage_noblock (char msgtype , const char * s , size_t len ) {
0 commit comments