From b030773e41983464dbffc071ab5603ef2a6062ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Van=20Canneyt?= Date: Sun, 19 May 2024 16:33:58 +0200 Subject: [PATCH] * Solution for FPC flush() mid-codepoint. --- packages/wasi/src/wasienv.pas | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/wasi/src/wasienv.pas b/packages/wasi/src/wasienv.pas index 8dcea0c..45e08c4 100644 --- a/packages/wasi/src/wasienv.pas +++ b/packages/wasi/src/wasienv.pas @@ -1177,12 +1177,27 @@ end; procedure TPas2JSWASIEnvironment.DoConsoleWrite(IsStdErr: Boolean; aBytes: TJSUint8Array); + Function TryConvert : string; + + begin + asm + S=String.fromCharCode.apply(null, aBytes); + end; + end; + Var S : String; Evt : TWASIWriteEvent; 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 evt:=FOnStdErrorWrite else