Browse Source

* Write string into memory using approach that also works in threads

Michael Van Canneyt 10 months ago
parent
commit
da93fd1eb0
1 changed files with 8 additions and 7 deletions
  1. 8 7
      packages/wasi/src/wasienv.pas

+ 8 - 7
packages/wasi/src/wasienv.pas

@@ -2276,11 +2276,10 @@ begin
   Arr:=UTF8TextEncoder.Encode(AString);
   if (Arr.byteLength>aLen) then
     Result:=-Arr.byteLength
+  else if Arr.byteLength=0 then
+    Result:=0
   else
-    begin
-    Arr:=TJSUint8Array.New(getModuleMemoryDataView.buffer,aLoc,aLen);
-    Result:=UTF8TextEncoder.encodeInto(aString,Arr).Written;
-    end;
+    Result:=SetUTF8StringInMem(aLoc,aLen,Arr);
 end;
 
 function TPas2JSWASIEnvironment.SetUTF8StringInMem(aLoc: TWasmMemoryLocation; aLen: Longint; AStringBuf: TJSUint8Array): Integer;
@@ -2289,9 +2288,11 @@ var
   Arr : TJSUint8Array;
 
 begin
- Arr:=TJSUint8Array.New(getModuleMemoryDataView.buffer,aLoc,aLen);
- Arr._set(aStringBuf);
- Result:=aStringBuf.byteLength;
+  if aStringBuf=Null then
+    exit(0);
+  Arr:=TJSUint8Array.New(getModuleMemoryDataView.buffer,aLoc,aLen);
+  Arr._set(aStringBuf);
+  Result:=aStringBuf.byteLength;
 end;