Browse Source

* Solution for FPC flush() mid-codepoint.

Michaël Van Canneyt 1 year ago
parent
commit
b030773e41
1 changed files with 16 additions and 1 deletions
  1. 16 1
      packages/wasi/src/wasienv.pas

+ 16 - 1
packages/wasi/src/wasienv.pas

@@ -1177,12 +1177,27 @@ end;
 
 
 procedure TPas2JSWASIEnvironment.DoConsoleWrite(IsStdErr: Boolean; aBytes: TJSUint8Array);
 procedure TPas2JSWASIEnvironment.DoConsoleWrite(IsStdErr: Boolean; aBytes: TJSUint8Array);
 
 
+  Function TryConvert : string;
+
+  begin
+    asm
+      S=String.fromCharCode.apply(null, aBytes);
+    end;
+  end;
+
 Var
 Var
   S : String;
   S : String;
   Evt : TWASIWriteEvent;
   Evt : TWASIWriteEvent;
 
 
 begin
 begin
-  S:=UTF8TextDecoder.decode(aBytes);
+  try
+    S:=UTF8TextDecoder.decode(aBytes);
+  except
+    // Depending on buffer size, FPC can do a flush mid-codepoint.
+    // The resulting bytes will not form a complete codepoint at the end.
+    // So we try to convert what is possible...
+    S:=TryConvert
+  end;
   if IsStdErr then
   if IsStdErr then
     evt:=FOnStdErrorWrite
     evt:=FOnStdErrorWrite
   else
   else