- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebAPI.TypedArrays.pas
More file actions
Latest commit
189 lines (154 loc) · 7.44 KB
/
Copy pathWebAPI.TypedArrays.pas
File metadata and controls
189 lines (154 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
unit WebAPI.TypedArrays;
interface
uses WebAPI.Core;
type
JArrayBuffer = classexternal'ArrayBuffer'
public
byteLength : Integer; // Read Only
functionSlice(aBegin : Integer; aEnd : Integer) : JArrayBuffer; external'slice';
constructor Create(aLength : Integer);
end;
JArrayBufferPromise = classexternal'Promise'
procedure &Then(callback : procedure (buffer : JArrayBuffer)); external'then';
end;
JArrayBufferView = classexternal
public
buffer : JArrayBuffer; // Read Only
byteOffset : Integer; // Read Only
byteLength : Integer; // Read Only
end;
JTypedArray = classexternal (JArrayBufferView)
public
BYTES_PER_ELEMENT : Integer; // Read Only
&length : Integer; // Read Only
procedureSet(aArray : JTypedArray; offset : Integer); overload; external'set';
constructor Create(aLength : Integer); overload;
constructor Create(aArray : JTypedArray); overload;
constructor Create(buffer : JArrayBuffer; byteOffset, aLength : Integer); overload;
constructor Create(buffer : JArrayBuffer); overload;
end;
JIntegerTypedArray = classabstractexternal (JTypedArray)
public
functionGetItems(index : Integer) : Integer; externalarray;
procedureSetItems(index : Integer; value : Integer); externalarray;
property Items[index : Integer] : Integer read getItems write setItems; default;
procedureSet(aArray : arrayof Integer; offset : Integer); overload; external'set';
constructor Create(aArray : arrayof Integer); overload;
end;
JInt8Array = classexternal'Int8Array' (JIntegerTypedArray)
public
const BYTES_PER_ELEMENT = 1;
functionSubArray(aBegin, aEnd : Integer) : JInt8Array; external'subarray';
end;
JUint8Array = classexternal'Uint8Array' (JIntegerTypedArray)
public
const BYTES_PER_ELEMENT = 1;
functionSubArray(aBegin, aEnd : Integer) : JUint8Array; external'subarray';
end;
JUint8ClampedArray = classexternal'Uint8ClampedArray' (JIntegerTypedArray)
public
const BYTES_PER_ELEMENT = 1;
functionSubArray(aBegin, aEnd : Integer) : JUint8ClampedArray; external'subarray';
end;
JInt16Array = classexternal'Int16Array' (JIntegerTypedArray)
public
const BYTES_PER_ELEMENT = 2;
functionSubArray(aBegin, aEnd : Integer) : JInt16Array; external'subarray';
end;
JUint16Array = classexternal'Uint16Array' (JIntegerTypedArray)
public
const BYTES_PER_ELEMENT = 2;
functionSubArray(aBegin, aEnd : Integer) : JUint16Array; external'subarray';
end;
JInt32Array = classexternal'Int32Array' (JIntegerTypedArray)
public
const BYTES_PER_ELEMENT = 4;
functionSubArray(aBegin, aEnd : Integer) : JInt32Array; external'subarray';
end;
JUint32Array = classexternal'Uint32Array' (JIntegerTypedArray)
public
const BYTES_PER_ELEMENT = 4;
functionSubArray(aBegin, aEnd : Integer) : JUint32Array; external'subarray';
end;
JFloatTypedArray = classabstractexternal (JTypedArray)
public
functionGetItems(index : Integer) : Float; externalarray;
procedureSetItems(index : Integer; value : Float); externalarray;
property Items[index : Integer] : Float read getItems write setItems; default;
procedureSet(aArray : arrayof Float; offset : Integer); overload; external'set';
constructor Create(aArray : arrayof Float); overload;
end;
JFloat32Array = classexternal'Float32Array' (JFloatTypedArray)
public
const BYTES_PER_ELEMENT = 4;
functionSubArray(aBegin, aEnd : Integer) : JFloat32Array; external'subarray';
end;
JFloat64Array = classexternal'Float64Array' (JFloatTypedArray)
public
const BYTES_PER_ELEMENT = 8;
functionSubArray(aBegin, aEnd : Integer) : JFloat64Array; external'subarray';
end;
JDataView = classexternal'DataView' (JArrayBufferView)
public
constructor Create(buffer : JArrayBuffer; byteOffset, byteLength : Integer);
// Gets the value of the given type at the specified byte offset
// from the start of the view. There is no alignment constraint;
// multi-byte values may be fetched from any offset.
//
// For multi-byte values, the littleEndian argument
// indicates whether a big-endian or little-endian value should be
// read. If false or undefined, a big-endian value is read.
//
// These methods raise an exception if they would read
// beyond the end of the view.
functiongetInt8(byteOffset : Integer) : Integer;
functiongetUint8(byteOffset : Integer) : Integer;
functiongetInt16(byteOffset : Integer; littleEndian : Boolean) : Integer;
functiongetUint16(byteOffset : Integer; littleEndian : Boolean) : Integer;
functiongetInt32(byteOffset : Integer; littleEndian : Boolean) : Integer;
functiongetUint32(byteOffset : Integer; littleEndian : Boolean) : Integer;
functiongetFloat32(byteOffset : Integer; littleEndian : Boolean) : Float;
functiongetFloat64(byteOffset : Integer; littleEndian : Boolean) : Float;
// Stores a value of the given type at the specified byte offset
// from the start of the view. There is no alignment constraint;
// multi-byte values may be stored at any offset.
//
// For multi-byte values, the littleEndian argument
// indicates whether the value should be stored in big-endian or
// little-endian byte order. If false or undefined, the value is
// stored in big-endian byte order.
//
// These methods raise an exception if they would write
// beyond the end of the view.
proceduresetInt8(byteOffset : Integer; value : Integer);
proceduresetUint8(byteOffset : Integer; value : Integer);
proceduresetInt16(byteOffset : Integer; value : Integer; littleEndian : Boolean);
proceduresetUint16(byteOffset : Integer; value : Integer; littleEndian : Boolean);
proceduresetInt32(byteOffset : Integer; value : Integer; littleEndian : Boolean);
proceduresetUint32(byteOffset : Integer; value : Integer; littleEndian : Boolean);
proceduresetFloat32(byteOffset : Integer; value : Float; littleEndian : Boolean);
proceduresetFloat64(byteOffset : Integer; value : Float; littleEndian : Boolean);
end;
functionStringToUTF8Bytes(s : String) : JUint8Array;
functionBytesToBase64(bytes : JUint8Array) : String;
functionBytesToHex(bytes : JUint8Array) : String;
implementation
functionStringToUTF8Bytes(s: String): JUint8Array;
begin
s := StrToUTF8(s);
Result := new JUint8Array(s.Length);
forvar i := 0to s.Length-1do
Result[i] := Ord(s[i+1]);
end;
functionBytesToBase64(bytes : JUint8Array) : String;
begin
var b := '';
forvar i := 0to bytes.byteLength-1do
b += Chr(bytes[i]);
Result := Window.btoa(b);
end;
functionBytesToHex(bytes: JUint8Array): String;
begin
forvar i := 0to bytes.byteLength-1do
Result += IntToHex(bytes[i], 2);
end;