From d0c597c68e63bf2ec6dafa2ec907825c11634418 Mon Sep 17 00:00:00 2001 From: Michael Van Canneyt Date: Thu, 6 Feb 2025 11:26:38 +0100 Subject: [PATCH] * Use GetUTF8StringFromArray --- packages/wasi/src/wasienv.pas | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/wasi/src/wasienv.pas b/packages/wasi/src/wasienv.pas index aa719bf..727b011 100644 --- a/packages/wasi/src/wasienv.pas +++ b/packages/wasi/src/wasienv.pas @@ -202,6 +202,7 @@ type Destructor Destroy; override; function GetUTF8ByteLength(const AString: String): Integer; Function GetUTF8StringFromMem(aLoc, aLen : Longint) : String; + function GetUTF8StringFromArray(aSourceArray: TJSUint8Array): String; // Write string as UTF8 string in memory at aLoc, with max aLen bytes. // Return number of bytes written, or -NeededLen if not enough room. function SetUTF8StringInMem(aLoc: TWasmMemoryLocation; aLen: Longint; AString: String): Integer; @@ -1392,7 +1393,7 @@ Var begin try - S:=UTF8TextDecoder.decode(aBytes); + S:=GetUTF8StringFromArray(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. @@ -2316,6 +2317,27 @@ begin end; +function TPas2JSWASIEnvironment.GetUTF8StringFromArray(aSourceArray:TJSUint8Array): String; + +var + src : TJSSharedArrayBuffer; + tmpBuf: TJSAbstractArrayBuffer; + SrcBytes,tmpBytes : TJSUint8Array; + +begin + if isDefined(Self_['SharedArrayBuffer']) and (aSourceArray.bufferObj is TJSSharedArrayBuffer) then + begin + src:=TJSSharedArrayBuffer(aSourceArray.bufferObj); + SrcBytes:=TJSUint8Array.new(src); + tmpBuf:=TJSArrayBuffer.new(Src.byteLength); + tmpBytes:=TJSUint8Array.new(tmpBuf); + tmpBytes._set(SrcBytes); + Result:=UTF8TextDecoder.Decode(tmpBytes); + end + else + Result:=UTF8TextDecoder.Decode(aSourceArray.Buffer); +end; + function TPas2JSWASIEnvironment.GetUTF8StringFromMem(aLoc, aLen: Longint): String; var