Browse Source

Add Get/Set UTF16string

Michael Van Canneyt 4 months ago
parent
commit
49b891c041
1 changed files with 31 additions and 0 deletions
  1. 31 0
      packages/wasi/src/wasienv.pas

+ 31 - 0
packages/wasi/src/wasienv.pas

@@ -206,10 +206,12 @@ type
     function GetUTF8ByteLength(const AString: String): Integer;
     function GetUTF8ByteLength(const AString: String): Integer;
     Function GetUTF8StringFromMem(aLoc, aLen : Longint) : String;
     Function GetUTF8StringFromMem(aLoc, aLen : Longint) : String;
     function GetUTF8StringFromArray(aSourceArray: TJSUint8Array): String;
     function GetUTF8StringFromArray(aSourceArray: TJSUint8Array): String;
+    function GetUTF16StringFromMem(p : TWasmPointer; LenInChars : Integer) : String;
     // Write string as UTF8 string in memory at aLoc, with max aLen bytes.
     // Write string as UTF8 string in memory at aLoc, with max aLen bytes.
     // Return number of bytes written, or -NeededLen if not enough room.
     // Return number of bytes written, or -NeededLen if not enough room.
     function SetUTF8StringInMem(aLoc: TWasmMemoryLocation; aLen: Longint; AString: String): Integer;
     function SetUTF8StringInMem(aLoc: TWasmMemoryLocation; aLen: Longint; AString: String): Integer;
     function SetUTF8StringInMem(aLoc: TWasmMemoryLocation; aLen: Longint; AStringBuf: TJSUint8Array): Integer;
     function SetUTF8StringInMem(aLoc: TWasmMemoryLocation; aLen: Longint; AStringBuf: TJSUint8Array): Integer;
+    Function SetUTF16StringInMem(p : TWasmPointer; const aString : String) : integer;
     function SetMemInfoInt8(aLoc : TWasmMemoryLocation; aValue : ShortInt) : TWasmMemoryLocation;
     function SetMemInfoInt8(aLoc : TWasmMemoryLocation; aValue : ShortInt) : TWasmMemoryLocation;
     function SetMemInfoInt16(aLoc : TWasmMemoryLocation; aValue : SmallInt) : TWasmMemoryLocation;
     function SetMemInfoInt16(aLoc : TWasmMemoryLocation; aValue : SmallInt) : TWasmMemoryLocation;
     function SetMemInfoInt32(aLoc : TWasmMemoryLocation; aValue : Longint) : TWasmMemoryLocation;
     function SetMemInfoInt32(aLoc : TWasmMemoryLocation; aValue : Longint) : TWasmMemoryLocation;
@@ -2344,6 +2346,21 @@ begin
   Result:=UTF8TextDecoder.Decode(tmpBytes);
   Result:=UTF8TextDecoder.Decode(tmpBytes);
 end;
 end;
 
 
+function TPas2JSWASIEnvironment.GetUTF16StringFromMem(p: TWasmPointer; LenInChars: Integer): String;
+
+  function String_fromCharCode_apply(aThis : TJSObject; aBuffer : TJSUint16Array) : String; external name 'String.fromCharCode.apply';
+
+var
+  bytes : TJSUint16Array;
+
+begin
+  Result:='';
+  if LenInChars <=0 then
+    exit;
+  bytes := TJSUint16Array.new(Memory.buffer, p, LenInChars);
+  Result := String_fromCharCode_apply(nil, bytes);
+end;
+
 function TPas2JSWASIEnvironment.GetUTF8StringFromMem(aLoc, aLen: Longint): String;
 function TPas2JSWASIEnvironment.GetUTF8StringFromMem(aLoc, aLen: Longint): String;
 
 
 var
 var
@@ -2392,6 +2409,20 @@ begin
   Result:=aStringBuf.byteLength;
   Result:=aStringBuf.byteLength;
 end;
 end;
 
 
+function TPas2JSWASIEnvironment.SetUTF16StringInMem(p: TWasmPointer; const aString: String) : Integer;
+var
+  i : Integer;
+  view : TJSDataView;
+begin
+  view := GetModuleMemoryDataView;
+  for i := 0 to Length(aString)-1 do
+    begin
+    view.setUint16(p, TJSString(aString).charCodeAt(i), IsLittleEndian);
+    inc(p,SizeUInt16);
+    end;
+  Result:=Length(aString)*SizeUInt16;
+end;
+
 
 
 function TPas2JSWASIEnvironment.SetMemInfoInt8(aLoc: TWasmMemoryLocation; aValue: ShortInt
 function TPas2JSWASIEnvironment.SetMemInfoInt8(aLoc: TWasmMemoryLocation; aValue: ShortInt
   ): TWasmMemoryLocation;
   ): TWasmMemoryLocation;