Browse Source

* Getters & Setters for typed arrays

Michaël Van Canneyt 1 year ago
parent
commit
94912ab274
1 changed files with 207 additions and 0 deletions
  1. 207 0
      packages/wasm-job/src/job.js.pas

+ 207 - 0
packages/wasm-job/src/job.js.pas

@@ -739,10 +739,14 @@ type
     ['{6A76602B-9555-4136-A7B7-2E683265EA82}']
     function GetBuffer: IJSArrayBuffer;
     function _GetLength: NativeInt;
+    function _GetByteLength: NativeInt;
+    function _GetByteOffset: NativeInt;
     procedure  set_(aArray : IJSTypedArray; TargetOffset : Integer);
     procedure  set_(aArray : IJSTypedArray);
     property Buffer : IJSArrayBuffer read GetBuffer;
     Property Length: NativeInt Read _GetLength;
+    Property byteLength: NativeInt Read _GetByteLength;
+    Property byteOffset: NativeInt Read _GetByteOffset;
   end;
 
   { TJSTypedArray }
@@ -751,6 +755,8 @@ type
   private
     function GetBuffer: IJSArrayBuffer;
     function _GetLength: NativeInt;
+    function _GetByteLength: NativeInt;
+    function _GetByteOffset: NativeInt;
   public
     constructor Create(aBytes : PByte; aLen : NativeUInt);
     constructor Create(aBytes : TBytes);
@@ -759,127 +765,198 @@ type
     procedure set_(aArray : IJSTypedArray);
     property Buffer : IJSArrayBuffer read GetBuffer;
     Property Length: NativeInt Read _GetLength;
+    Property byteLength: NativeInt Read _GetByteLength;
+    Property byteOffset: NativeInt Read _GetByteOffset;
   end;
 
   { IJSInt8Array }
 
   IJSInt8Array = interface(IJSTypedArray)
     ['{72D65C5E-E18E-4294-8709-D7A63BF12958}']
+    function _GetElement(aIndex : NativeInt) : Shortint;
+    procedure _SetElement(aIndex : NativeInt; aValue : Shortint);
+    property Element[Index: NativeInt]: Shortint read _GetElement write _SetElement; default;
   end;
 
   { TJSInt8Array }
 
   TJSInt8Array = class(TJSTypedArray,IJSInt8Array)
+  Protected
+    function _GetElement(aIndex : NativeInt) : Shortint;
+    procedure _SetElement(aIndex : NativeInt; aValue : Shortint);
   public
     class function JSClassName: UnicodeString; override;
     class function Cast(const Intf: IJSObject): IJSInt8Array; overload;
+    property Element[Index: NativeInt]: Shortint read _GetElement write _SetElement; default;
   end;
 
   { IJSUint8Array }
 
   IJSUint8Array = interface(IJSTypedArray)
     ['{99EC7B3A-30E5-425F-933C-C169B2F4193C}']
+    function _GetElement(aIndex : NativeInt) : Byte;
+    procedure _SetElement(aIndex : NativeInt; aValue : Byte);
+    property Element[Index: NativeInt]: Byte read _GetElement write _SetElement; default;
   end;
 
   { TJSUint8Array }
 
   TJSUint8Array = class(TJSTypedArray,IJSUint8Array)
+  Protected
+    function _GetElement(aIndex : NativeInt) : Byte;
+    procedure _SetElement(aIndex : NativeInt; aValue : Byte);
   public
     Class function GetGlobal : TJSUint8Array;
     class function JSClassName: UnicodeString; override;
     class function Cast(const Intf: IJSObject): IJSUint8Array; overload;
+    property Element[Index: NativeInt]: byte read _GetElement write _SetElement; default;
   end;
 
   { IJSUint8ClampedArray }
 
   IJSUint8ClampedArray = interface(IJSTypedArray)
     ['{A1508D6E-8629-4416-875E-9F669ECDC47F}']
+    function _GetElement(aIndex : NativeInt) : Byte;
+    procedure _SetElement(aIndex : NativeInt; aValue : Byte);
+    property Element[Index: NativeInt]: Byte read _GetElement write _SetElement; default;
   end;
 
   { TJSUint8ClampedArray }
 
   TJSUint8ClampedArray = class(TJSTypedArray,IJSUint8ClampedArray)
+  Protected
+    function _GetElement(aIndex : NativeInt) : Byte;
+    procedure _SetElement(aIndex : NativeInt; aValue : Byte);
   public
     class function JSClassName: UnicodeString; override;
     class function Cast(const Intf: IJSObject): IJSUint8ClampedArray; overload;
+    property Element[Index: NativeInt]: Byte read _GetElement write _SetElement; default;
   end;
 
   { IJSInt16Array }
 
   IJSInt16Array = interface(IJSTypedArray)
     ['{B5FA7A13-D8CA-44E4-ADAE-F10FFFAE46B4}']
+    function _GetElement(aIndex : NativeInt) : SmallInt;
+    procedure _SetElement(aIndex : NativeInt; aValue : SmallInt);
+    property Element[Index: NativeInt]: SmallInt read _GetElement write _SetElement; default;
   end;
 
   { TJSInt16Array }
 
   TJSInt16Array = class(TJSTypedArray,IJSInt16Array)
+  Protected
+    function _GetElement(aIndex : NativeInt) : SmallInt;
+    procedure _SetElement(aIndex : NativeInt; aValue : SmallInt);
   public
     class function Cast(const Intf: IJSObject): IJSInt16Array; overload;
+    class function JSClassName: UnicodeString; override;
+    property Element[Index: NativeInt]: SmallInt read _GetElement write _SetElement; default;
   end;
 
   { IJSUint16Array }
 
   IJSUint16Array = interface(IJSTypedArray)
     ['{6023E2BC-C464-4288-A8DA-4A5D0B2B915E}']
+    function _GetElement(aIndex : NativeInt) : Word;
+    procedure _SetElement(aIndex : NativeInt; aValue : Word);
+    property Element[Index: NativeInt]: Word read _GetElement write _SetElement; default;
   end;
 
   { TJSUint16Array }
 
   TJSUint16Array = class(TJSTypedArray,IJSUint16Array)
+  Protected
+    function _GetElement(aIndex : NativeInt) : Word;
+    procedure _SetElement(aIndex : NativeInt; aValue : Word);
   public
+    class function JSClassName: UnicodeString; override;
     class function Cast(const Intf: IJSObject): IJSUint16Array; overload;
+    property Element[Index: NativeInt]: Word read _GetElement write _SetElement; default;
   end;
 
   { IJSInt32Array }
 
   IJSInt32Array = interface(IJSTypedArray)
     ['{16F1A6FB-2F26-4A64-8A2B-D883DE2F58C4}']
+    function _GetElement(aIndex : NativeInt) : LongInt;
+    procedure _SetElement(aIndex : NativeInt; aValue : LongInt);
+    property Element[Index: NativeInt]: LongInt read _GetElement write _SetElement; default;
   end;
 
   { TJSInt32Array }
 
   TJSInt32Array = class(TJSTypedArray,IJSInt32Array)
+  Protected
+    function _GetElement(aIndex : NativeInt) : LongInt;
+    procedure _SetElement(aIndex : NativeInt; aValue : LongInt);
   public
+    class function JSClassName: UnicodeString; override;
     class function Cast(const Intf: IJSObject): IJSInt32Array; overload;
+    property Element[Index: NativeInt]: LongInt read _GetElement write _SetElement; default;
   end;
 
   { IJSUint32Array }
 
   IJSUint32Array = interface(IJSTypedArray)
     ['{C637B2FA-CED6-4EC7-8D97-C56824EAF8B3}']
+    function _GetElement(aIndex : NativeInt) : Cardinal;
+    procedure _SetElement(aIndex : NativeInt; aValue : Cardinal);
+    property Element[Index: NativeInt]: Cardinal read _GetElement write _SetElement; default;
   end;
 
   { TJSUint32Array }
 
   TJSUint32Array = class(TJSTypedArray,IJSUint32Array)
+  Protected
+    function _GetElement(aIndex : NativeInt) : Cardinal;
+    procedure _SetElement(aIndex : NativeInt; aValue : Cardinal);
   public
+    class function JSClassName: UnicodeString; override;
     class function Cast(const Intf: IJSObject): IJSUint32Array; overload;
+    property Element[Index: NativeInt]: Cardinal read _GetElement write _SetElement; default;
   end;
 
   { IJSFloat32Array }
 
   IJSFloat32Array = interface(IJSTypedArray)
     ['{B5CE57F6-CA7C-4168-AEA3-32EF13DA52D6}']
+    function _GetElement(aIndex : NativeInt) : SIngle;
+    procedure _SetElement(aIndex : NativeInt; aValue : SIngle);
+    property Element[Index: NativeInt]: SIngle read _GetElement write _SetElement; default;
   end;
 
   { TJSFloat32Array }
 
   TJSFloat32Array = class(TJSTypedArray,IJSFloat32Array)
+  Protected
+    function _GetElement(aIndex : NativeInt) : SIngle;
+    procedure _SetElement(aIndex : NativeInt; aValue : SIngle);
   public
+    class function JSClassName: UnicodeString; override;
     class function Cast(const Intf: IJSObject): IJSFloat32Array; overload;
+    property Element[Index: NativeInt]: SIngle read _GetElement write _SetElement; default;
   end;
 
   { IJSFloat64Array }
 
   IJSFloat64Array = interface(IJSTypedArray)
     ['{A7876DC5-9549-4FDA-BE35-A641CE9D9F0B}']
+    function _GetElement(aIndex : NativeInt) : SIngle;
+    procedure _SetElement(aIndex : NativeInt; aValue : SIngle);
+    property Element[Index: NativeInt]: SIngle read _GetElement write _SetElement; default;
   end;
 
   { TJSFloat64Array }
 
   TJSFloat64Array = class(TJSTypedArray,IJSFloat64Array)
+  Protected
+    function _GetElement(aIndex : NativeInt) : SIngle;
+    procedure _SetElement(aIndex : NativeInt; aValue : SIngle);
   public
+    class function JSClassName: UnicodeString; override;
     class function Cast(const Intf: IJSObject): IJSFloat64Array; overload;
+    property Element[Index: NativeInt]: SIngle read _GetElement write _SetElement; default;
   end;
 
   { IJSBufferSource }
@@ -1511,6 +1588,21 @@ end;
 
 { TJSFloat64Array }
 
+function TJSFloat64Array._GetElement(aIndex: NativeInt): SIngle;
+begin
+  Result:=InvokeJSDoubleResult(IntToStr(aIndex),[],jiGet);
+end;
+
+procedure TJSFloat64Array._SetElement(aIndex: NativeInt; aValue: SIngle);
+begin
+  InvokeJSNoResult(IntToStr(aIndex),[aValue],jiSet);
+end;
+
+class function TJSFloat64Array.JSClassName: UnicodeString;
+begin
+  Result:='Float64Array';
+end;
+
 class function TJSFloat64Array.Cast(const Intf: IJSObject): IJSFloat64Array;
 begin
   Result:=TJSFloat64Array.Cast(Intf);
@@ -1518,6 +1610,21 @@ end;
 
 { TJSFloat32Array }
 
+function TJSFloat32Array._GetElement(aIndex: NativeInt): SIngle;
+begin
+  Result:=InvokeJSDoubleResult(IntToStr(aIndex),[],jiGet);
+end;
+
+procedure TJSFloat32Array._SetElement(aIndex: NativeInt; aValue: SIngle);
+begin
+  InvokeJSNoResult(IntToStr(aIndex),[aValue],jiSet);
+end;
+
+class function TJSFloat32Array.JSClassName: UnicodeString;
+begin
+  Result:='Float32Array';
+end;
+
 class function TJSFloat32Array.Cast(const Intf: IJSObject): IJSFloat32Array;
 begin
   Result:=TJSFloat32Array.Cast(Intf);
@@ -1525,6 +1632,21 @@ end;
 
 { TJSUint32Array }
 
+function TJSUint32Array._GetElement(aIndex: NativeInt): Cardinal;
+begin
+  Result:=Cardinal(InvokeJSMaxIntResult(IntToStr(aIndex),[],jiGet));
+end;
+
+procedure TJSUint32Array._SetElement(aIndex: NativeInt; aValue: Cardinal);
+begin
+  InvokeJSNoResult(IntToStr(aIndex),[aValue],jiSet);
+end;
+
+class function TJSUint32Array.JSClassName: UnicodeString;
+begin
+  Result:=inherited JSClassName;
+end;
+
 class function TJSUint32Array.Cast(const Intf: IJSObject): IJSUint32Array;
 begin
   Result:=TJSUint32Array.Cast(Intf);
@@ -1532,6 +1654,21 @@ end;
 
 { TJSInt32Array }
 
+function TJSInt32Array._GetElement(aIndex: NativeInt): LongInt;
+begin
+  Result:=InvokeJSLongintResult(IntToStr(aIndex),[],jiGet);
+end;
+
+procedure TJSInt32Array._SetElement(aIndex: NativeInt; aValue: LongInt);
+begin
+  InvokeJSNoResult(IntToStr(aIndex),[aValue],jiSet);
+end;
+
+class function TJSInt32Array.JSClassName: UnicodeString;
+begin
+  Result:='Int32Array';
+end;
+
 class function TJSInt32Array.Cast(const Intf: IJSObject): IJSInt32Array;
 begin
   Result:=TJSInt32Array.Cast(Intf);
@@ -1539,20 +1676,60 @@ end;
 
 { TJSUint16Array }
 
+class function TJSUint16Array.JSClassName: UnicodeString;
+begin
+  Result:='Uint16Array';
+end;
+
 class function TJSUint16Array.Cast(const Intf: IJSObject): IJSUint16Array;
 begin
   Result:=TJSUint16Array.Cast(Intf);
 end;
 
+function TJSUint16Array._GetElement(aIndex: NativeInt): Word;
+begin
+  Result:=InvokeJSLongintResult(IntToStr(aIndex),[],jiGet);
+end;
+
+procedure TJSUint16Array._SetElement(aIndex: NativeInt; aValue: Word);
+begin
+  InvokeJSNoResult(IntToStr(aIndex),[aValue],jiSet);
+end;
+
 { TJSInt16Array }
 
+function TJSInt16Array._GetElement(aIndex: NativeInt): SmallInt;
+begin
+  Result:=InvokeJSLongintResult(IntToStr(aIndex),[],jiGet);
+end;
+
+procedure TJSInt16Array._SetElement(aIndex: NativeInt; aValue: SmallInt);
+begin
+  InvokeJSNoResult(IntToStr(aIndex),[aValue],jiSet);
+end;
+
 class function TJSInt16Array.Cast(const Intf: IJSObject): IJSInt16Array;
 begin
   Result:=TJSInt16Array.Cast(Intf);
 end;
 
+class function TJSInt16Array.JSClassName: UnicodeString;
+begin
+  Result:='Int16Array';
+end;
+
 { TJSUint8ClampedArray }
 
+function TJSUint8ClampedArray._GetElement(aIndex: NativeInt): Byte;
+begin
+  Result:=InvokeJSLongintResult(IntToStr(aIndex),[],jiGet);
+end;
+
+procedure TJSUint8ClampedArray._SetElement(aIndex: NativeInt; aValue: Byte);
+begin
+  InvokeJSNoResult(IntToStr(aIndex),[aValue],jiSet);
+end;
+
 class function TJSUint8ClampedArray.JSClassName: UnicodeString;
 begin
   Result:='Uint8ClampedArray';
@@ -1566,6 +1743,16 @@ end;
 
 { TJSUInt8Array }
 
+function TJSUint8Array._GetElement(aIndex: NativeInt): Byte;
+begin
+  Result:=InvokeJSLongintResult(IntToStr(aIndex),[],jiGet);
+end;
+
+procedure TJSUint8Array._SetElement(aIndex: NativeInt; aValue: Byte);
+begin
+  InvokeJSNoResult(IntToStr(aIndex),[aValue],jiSet);
+end;
+
 class function TJSUint8Array.GetGlobal: TJSUint8Array;
 begin
   // We must free it.
@@ -1585,6 +1772,16 @@ end;
 
 { TJSInt8Array }
 
+function TJSInt8Array._GetElement(aIndex: NativeInt): Shortint;
+begin
+  Result:=InvokeJSLongintResult(IntToStr(aIndex),[],jiGet);
+end;
+
+procedure TJSInt8Array._SetElement(aIndex: NativeInt; aValue: Shortint);
+begin
+  InvokeJSNoResult(IntToStr(aIndex),[aValue],jiSet);
+end;
+
 class function TJSInt8Array.JSClassName: UnicodeString;
 begin
   Result:='Int8Array';
@@ -1608,6 +1805,16 @@ begin
   Result:=ReadJSPropertyLongInt('length');
 end;
 
+function TJSTypedArray._GetByteLength: NativeInt;
+begin
+  Result:=ReadJSPropertyLongInt('byteLength');
+end;
+
+function TJSTypedArray._GetByteOffset: NativeInt;
+begin
+  Result:=ReadJSPropertyLongInt('byteOffset');
+end;
+
 constructor TJSTypedArray.Create(aBytes: PByte; aLen: NativeUInt);
 
 var