@grain/stdlib 0.5.13 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +193 -0
- package/LICENSE +1 -1
- package/README.md +25 -2
- package/array.gr +1512 -199
- package/array.md +2032 -94
- package/bigint.gr +239 -140
- package/bigint.md +450 -106
- package/buffer.gr +595 -102
- package/buffer.md +903 -145
- package/bytes.gr +401 -110
- package/bytes.md +551 -63
- package/char.gr +228 -49
- package/char.md +373 -7
- package/exception.gr +26 -12
- package/exception.md +29 -5
- package/float32.gr +130 -109
- package/float32.md +185 -57
- package/float64.gr +112 -99
- package/float64.md +185 -57
- package/hash.gr +47 -37
- package/hash.md +21 -3
- package/int16.gr +430 -0
- package/int16.md +618 -0
- package/int32.gr +200 -269
- package/int32.md +254 -289
- package/int64.gr +142 -225
- package/int64.md +254 -289
- package/int8.gr +511 -0
- package/int8.md +786 -0
- package/json.gr +2084 -0
- package/json.md +608 -0
- package/list.gr +120 -68
- package/list.md +125 -80
- package/map.gr +560 -57
- package/map.md +672 -56
- package/marshal.gr +239 -227
- package/marshal.md +36 -4
- package/number.gr +626 -676
- package/number.md +738 -153
- package/option.gr +33 -35
- package/option.md +58 -42
- package/package.json +2 -2
- package/path.gr +148 -187
- package/path.md +47 -96
- package/pervasives.gr +75 -416
- package/pervasives.md +85 -180
- package/priorityqueue.gr +433 -74
- package/priorityqueue.md +422 -54
- package/queue.gr +362 -80
- package/queue.md +433 -38
- package/random.gr +67 -75
- package/random.md +68 -40
- package/range.gr +135 -63
- package/range.md +198 -43
- package/rational.gr +284 -0
- package/rational.md +545 -0
- package/regex.gr +933 -1066
- package/regex.md +59 -60
- package/result.gr +23 -25
- package/result.md +54 -39
- package/runtime/atof/common.gr +78 -82
- package/runtime/atof/common.md +22 -10
- package/runtime/atof/decimal.gr +102 -127
- package/runtime/atof/decimal.md +28 -7
- package/runtime/atof/lemire.gr +56 -71
- package/runtime/atof/lemire.md +9 -1
- package/runtime/atof/parse.gr +83 -110
- package/runtime/atof/parse.md +12 -2
- package/runtime/atof/slow.gr +28 -35
- package/runtime/atof/slow.md +9 -1
- package/runtime/atof/table.gr +19 -18
- package/runtime/atof/table.md +10 -2
- package/runtime/atoi/parse.gr +153 -136
- package/runtime/atoi/parse.md +50 -1
- package/runtime/bigint.gr +410 -517
- package/runtime/bigint.md +71 -57
- package/runtime/compare.gr +176 -85
- package/runtime/compare.md +31 -1
- package/runtime/dataStructures.gr +144 -32
- package/runtime/dataStructures.md +267 -31
- package/runtime/debugPrint.gr +34 -15
- package/runtime/debugPrint.md +37 -5
- package/runtime/equal.gr +53 -52
- package/runtime/equal.md +30 -1
- package/runtime/exception.gr +38 -47
- package/runtime/exception.md +10 -8
- package/runtime/gc.gr +23 -152
- package/runtime/gc.md +13 -17
- package/runtime/malloc.gr +31 -31
- package/runtime/malloc.md +11 -3
- package/runtime/numberUtils.gr +191 -172
- package/runtime/numberUtils.md +17 -9
- package/runtime/numbers.gr +1695 -1021
- package/runtime/numbers.md +1098 -134
- package/runtime/string.gr +540 -242
- package/runtime/string.md +76 -6
- package/runtime/unsafe/constants.gr +30 -13
- package/runtime/unsafe/constants.md +80 -0
- package/runtime/unsafe/conv.gr +55 -28
- package/runtime/unsafe/conv.md +41 -9
- package/runtime/unsafe/memory.gr +10 -30
- package/runtime/unsafe/memory.md +15 -19
- package/runtime/unsafe/tags.gr +37 -21
- package/runtime/unsafe/tags.md +88 -8
- package/runtime/unsafe/wasmf32.gr +30 -36
- package/runtime/unsafe/wasmf32.md +64 -56
- package/runtime/unsafe/wasmf64.gr +30 -36
- package/runtime/unsafe/wasmf64.md +64 -56
- package/runtime/unsafe/wasmi32.gr +49 -66
- package/runtime/unsafe/wasmi32.md +102 -94
- package/runtime/unsafe/wasmi64.gr +52 -79
- package/runtime/unsafe/wasmi64.md +108 -100
- package/runtime/utils/printing.gr +13 -15
- package/runtime/utils/printing.md +11 -3
- package/runtime/wasi.gr +294 -295
- package/runtime/wasi.md +62 -42
- package/set.gr +574 -64
- package/set.md +634 -54
- package/stack.gr +181 -64
- package/stack.md +271 -42
- package/string.gr +453 -533
- package/string.md +241 -151
- package/uint16.gr +369 -0
- package/uint16.md +585 -0
- package/uint32.gr +470 -0
- package/uint32.md +737 -0
- package/uint64.gr +471 -0
- package/uint64.md +737 -0
- package/uint8.gr +369 -0
- package/uint8.md +585 -0
- package/uri.gr +1093 -0
- package/uri.md +477 -0
- package/{sys → wasi}/file.gr +914 -500
- package/{sys → wasi}/file.md +454 -50
- package/wasi/process.gr +292 -0
- package/{sys → wasi}/process.md +164 -6
- package/wasi/random.gr +77 -0
- package/wasi/random.md +80 -0
- package/{sys → wasi}/time.gr +15 -22
- package/{sys → wasi}/time.md +5 -5
- package/immutablearray.gr +0 -929
- package/immutablearray.md +0 -1038
- package/immutablemap.gr +0 -493
- package/immutablemap.md +0 -479
- package/immutablepriorityqueue.gr +0 -360
- package/immutablepriorityqueue.md +0 -291
- package/immutableset.gr +0 -498
- package/immutableset.md +0 -449
- package/runtime/debug.gr +0 -2
- package/runtime/debug.md +0 -6
- package/runtime/unsafe/errors.gr +0 -36
- package/runtime/unsafe/errors.md +0 -204
- package/sys/process.gr +0 -254
- package/sys/random.gr +0 -79
- package/sys/random.md +0 -66
package/bytes.gr
CHANGED
|
@@ -1,21 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Utilities for working with byte sequences.
|
|
3
3
|
*
|
|
4
|
-
* @example
|
|
4
|
+
* @example from "bytes" include Bytes
|
|
5
|
+
*
|
|
6
|
+
* @example b"\x00"
|
|
7
|
+
* @example Bytes.make(1)
|
|
5
8
|
*
|
|
6
9
|
* @since v0.3.2
|
|
7
10
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
module Bytes
|
|
12
|
+
|
|
13
|
+
from "runtime/unsafe/memory" include Memory
|
|
14
|
+
from "runtime/unsafe/wasmi32" include WasmI32
|
|
15
|
+
from "runtime/unsafe/wasmi64" include WasmI64
|
|
16
|
+
from "runtime/unsafe/wasmf32" include WasmF32
|
|
17
|
+
from "runtime/unsafe/wasmf64" include WasmF64
|
|
18
|
+
from "runtime/unsafe/conv" include Conv
|
|
19
|
+
from "runtime/dataStructures" include DataStructures
|
|
20
|
+
use DataStructures.{
|
|
21
|
+
tagInt8,
|
|
22
|
+
tagUint8,
|
|
23
|
+
tagInt16,
|
|
24
|
+
tagUint16,
|
|
25
|
+
untagInt8,
|
|
26
|
+
untagUint8,
|
|
27
|
+
untagInt16,
|
|
28
|
+
untagUint16,
|
|
29
|
+
allocateBytes,
|
|
30
|
+
allocateString,
|
|
31
|
+
}
|
|
32
|
+
from "runtime/exception" include Exception
|
|
33
|
+
from "int32" include Int32
|
|
34
|
+
from "runtime/numbers" include Numbers
|
|
35
|
+
use Numbers.{ coerceNumberToWasmI32 }
|
|
19
36
|
|
|
20
37
|
@unsafe
|
|
21
38
|
let _SIZE_OFFSET = 4n
|
|
@@ -37,14 +54,12 @@ let _FLOAT64_BYTE_SIZE = 8n
|
|
|
37
54
|
/** Throws an exception if the index specified is out-of-bounds */
|
|
38
55
|
@unsafe
|
|
39
56
|
let checkIndexIsInBounds = (i, byteSize, max) => {
|
|
40
|
-
|
|
41
|
-
let (<) = WasmI32.ltS
|
|
42
|
-
let (>) = WasmI32.gtS
|
|
57
|
+
use WasmI32.{ (+), (<), (>) }
|
|
43
58
|
if (i < 0n) {
|
|
44
|
-
throw
|
|
59
|
+
throw IndexOutOfBounds
|
|
45
60
|
}
|
|
46
61
|
if (i + byteSize > max) {
|
|
47
|
-
throw
|
|
62
|
+
throw IndexOutOfBounds
|
|
48
63
|
}
|
|
49
64
|
}
|
|
50
65
|
|
|
@@ -52,30 +67,34 @@ let checkIndexIsInBounds = (i, byteSize, max) => {
|
|
|
52
67
|
@unsafe
|
|
53
68
|
let getSize = ptr => WasmI32.load(ptr, _SIZE_OFFSET)
|
|
54
69
|
|
|
55
|
-
/**
|
|
56
|
-
* @section Values: Functions for working with the Bytes data type.
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
70
|
/**
|
|
60
71
|
* Creates a new byte sequence of the input size.
|
|
61
72
|
*
|
|
62
73
|
* @param size: The number of bytes to store
|
|
63
74
|
* @returns The new byte sequence
|
|
64
75
|
*
|
|
76
|
+
* @example Bytes.make(0) == b"""
|
|
77
|
+
* @example Bytes.make(1) == b"\x00"
|
|
78
|
+
*
|
|
65
79
|
* @since v0.3.2
|
|
66
80
|
*/
|
|
67
81
|
@unsafe
|
|
68
|
-
|
|
69
|
-
|
|
82
|
+
provide let make = (size: Number) => {
|
|
83
|
+
use WasmI32.{ (+) }
|
|
84
|
+
let size = coerceNumberToWasmI32(size)
|
|
85
|
+
let bytes = allocateBytes(size)
|
|
86
|
+
Memory.fill(bytes + 8n, 0n, size)
|
|
70
87
|
WasmI32.toGrain(bytes): Bytes
|
|
71
88
|
}
|
|
72
89
|
|
|
73
90
|
/**
|
|
74
91
|
* An empty byte sequence.
|
|
75
92
|
*
|
|
93
|
+
* @example Bytes.empty == b""
|
|
94
|
+
*
|
|
76
95
|
* @since v0.3.2
|
|
77
96
|
*/
|
|
78
|
-
|
|
97
|
+
provide let empty = make(0)
|
|
79
98
|
|
|
80
99
|
/**
|
|
81
100
|
* Creates a new byte sequence from the input string.
|
|
@@ -83,11 +102,13 @@ export let empty = make(0)
|
|
|
83
102
|
* @param string: The string to copy into a byte sequence
|
|
84
103
|
* @returns The new byte sequence
|
|
85
104
|
*
|
|
105
|
+
* @example Bytes.fromString("\x00\x00") == b"\x00\x00"
|
|
106
|
+
*
|
|
86
107
|
* @since v0.3.2
|
|
87
108
|
*/
|
|
88
109
|
@unsafe
|
|
89
|
-
|
|
90
|
-
|
|
110
|
+
provide let fromString = (string: String) => {
|
|
111
|
+
use WasmI32.{ (+) }
|
|
91
112
|
let src = WasmI32.fromGrain(string)
|
|
92
113
|
let size = getSize(src)
|
|
93
114
|
let dst = allocateBytes(size)
|
|
@@ -101,11 +122,14 @@ export let fromString = (string: String) => {
|
|
|
101
122
|
* @param bytes: The source byte sequence
|
|
102
123
|
* @returns The string representation of the bytes
|
|
103
124
|
*
|
|
125
|
+
* @example Bytes.toString(b"\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64") == "Hello World"
|
|
126
|
+
* @example Bytes.toString(b"Hello World") == "Hello World"
|
|
127
|
+
*
|
|
104
128
|
* @since v0.3.2
|
|
105
129
|
*/
|
|
106
130
|
@unsafe
|
|
107
|
-
|
|
108
|
-
|
|
131
|
+
provide let toString = (bytes: Bytes) => {
|
|
132
|
+
use WasmI32.{ (+) }
|
|
109
133
|
let src = WasmI32.fromGrain(bytes)
|
|
110
134
|
let size = getSize(src)
|
|
111
135
|
let dst = allocateString(size)
|
|
@@ -119,10 +143,13 @@ export let toString = (bytes: Bytes) => {
|
|
|
119
143
|
* @param bytes: The byte sequence to inspect
|
|
120
144
|
* @returns The number of bytes
|
|
121
145
|
*
|
|
146
|
+
* @example Bytes.length(b"") == 0
|
|
147
|
+
* @example Bytes.length(b"\x48") == 1
|
|
148
|
+
*
|
|
122
149
|
* @since v0.3.2
|
|
123
150
|
*/
|
|
124
151
|
@unsafe
|
|
125
|
-
|
|
152
|
+
provide let length = (bytes: Bytes) => {
|
|
126
153
|
let b = WasmI32.fromGrain(bytes)
|
|
127
154
|
Conv.wasmI32ToNumber(getSize(b))
|
|
128
155
|
}
|
|
@@ -133,12 +160,14 @@ export let length = (bytes: Bytes) => {
|
|
|
133
160
|
* @param bytes: The byte sequence to copy
|
|
134
161
|
* @returns The new byte sequence
|
|
135
162
|
*
|
|
163
|
+
* @example Bytes.copy(b"\x48") == b"\x48"
|
|
164
|
+
*
|
|
136
165
|
* @since v0.3.2
|
|
137
166
|
*/
|
|
138
167
|
@unsafe
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
let src = WasmI32.fromGrain(
|
|
168
|
+
provide let copy = (bytes: Bytes) => {
|
|
169
|
+
use WasmI32.{ (+) }
|
|
170
|
+
let src = WasmI32.fromGrain(bytes)
|
|
142
171
|
let size = getSize(src)
|
|
143
172
|
let dst = allocateBytes(size)
|
|
144
173
|
Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
|
|
@@ -155,12 +184,16 @@ export let copy = (b: Bytes) => {
|
|
|
155
184
|
*
|
|
156
185
|
* @throws InvalidArgument(String): When `start + length` is greater than the bytes size
|
|
157
186
|
*
|
|
187
|
+
* @example
|
|
188
|
+
* assert Bytes.toString(
|
|
189
|
+
* Bytes.slice(0, 5, b"\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64")
|
|
190
|
+
* ) == "Hello"
|
|
191
|
+
*
|
|
158
192
|
* @since v0.3.2
|
|
159
193
|
*/
|
|
160
194
|
@unsafe
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
let (+) = WasmI32.add
|
|
195
|
+
provide let slice = (start: Number, length: Number, bytes: Bytes) => {
|
|
196
|
+
use WasmI32.{ (+), (>) }
|
|
164
197
|
let src = WasmI32.fromGrain(bytes)
|
|
165
198
|
let size = getSize(src)
|
|
166
199
|
let iOrig = start
|
|
@@ -169,7 +202,7 @@ export let slice = (start: Number, length: Number, bytes: Bytes) => {
|
|
|
169
202
|
let length = coerceNumberToWasmI32(length)
|
|
170
203
|
if (start + length > size) {
|
|
171
204
|
throw Exception.InvalidArgument(
|
|
172
|
-
"The given index and length do not specify a valid range of bytes"
|
|
205
|
+
"The given index and length do not specify a valid range of bytes",
|
|
173
206
|
)
|
|
174
207
|
}
|
|
175
208
|
let dst = allocateBytes(length)
|
|
@@ -190,15 +223,13 @@ export let slice = (start: Number, length: Number, bytes: Bytes) => {
|
|
|
190
223
|
*
|
|
191
224
|
* @throws InvalidArgument(String): When the new size is negative
|
|
192
225
|
*
|
|
226
|
+
* @example Bytes.length(Bytes.resize(0, 3, b"")) == 3
|
|
227
|
+
*
|
|
193
228
|
* @since v0.3.2
|
|
194
229
|
*/
|
|
195
230
|
@unsafe
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
let (>) = WasmI32.gtS
|
|
199
|
-
let (+) = WasmI32.add
|
|
200
|
-
let (-) = WasmI32.sub
|
|
201
|
-
let (*) = WasmI32.mul
|
|
231
|
+
provide let resize = (left: Number, right: Number, bytes: Bytes) => {
|
|
232
|
+
use WasmI32.{ (+), (-), (*), (<), (>) }
|
|
202
233
|
let src = WasmI32.fromGrain(bytes)
|
|
203
234
|
let size = getSize(src)
|
|
204
235
|
let leftOrig = left
|
|
@@ -241,26 +272,29 @@ export let resize = (left: Number, right: Number, bytes: Bytes) => {
|
|
|
241
272
|
*
|
|
242
273
|
* @param srcIndex: The starting index to copy bytes from
|
|
243
274
|
* @param dstIndex: The starting index to copy bytes into
|
|
244
|
-
* @param length: The amount of bytes to copy from the source
|
|
275
|
+
* @param length: The amount of bytes to copy from the source byte sequence
|
|
245
276
|
* @param src: The source byte sequence
|
|
246
277
|
* @param dst: The destination byte sequence
|
|
247
278
|
*
|
|
248
279
|
* @throws InvalidArgument(String): When `srcIndex + length` is greater than the `src` bytes size
|
|
249
280
|
* @throws InvalidArgument(String): When the `dstIndex + length` is greater than the `dst` bytes size
|
|
250
281
|
*
|
|
282
|
+
* @example
|
|
283
|
+
* let bytes = Bytes.make(5)
|
|
284
|
+
* Bytes.move(0, 0, 5, b"\x48\x64\x6c\x6f\x20\x57\x6f\x72\x6c\x64", bytes)
|
|
285
|
+
* assert Bytes.toString(bytes) == "Hello"
|
|
286
|
+
*
|
|
251
287
|
* @since v0.3.2
|
|
252
288
|
*/
|
|
253
289
|
@unsafe
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
)
|
|
262
|
-
let (>) = WasmI32.gtS
|
|
263
|
-
let (+) = WasmI32.add
|
|
290
|
+
provide let move = (
|
|
291
|
+
srcIndex: Number,
|
|
292
|
+
dstIndex: Number,
|
|
293
|
+
length: Number,
|
|
294
|
+
src: Bytes,
|
|
295
|
+
dst: Bytes,
|
|
296
|
+
) => {
|
|
297
|
+
use WasmI32.{ (+), (>) }
|
|
264
298
|
let srcIndexOrig = srcIndex
|
|
265
299
|
let dstIndexOrig = dstIndex
|
|
266
300
|
let lenthOrig = length
|
|
@@ -292,9 +326,14 @@ export let move =
|
|
|
292
326
|
* @param bytes2: The ending byte sequence
|
|
293
327
|
* @returns The new byte sequence
|
|
294
328
|
*
|
|
329
|
+
* @example
|
|
330
|
+
* let helloBytes = Bytes.fromString("Hello ")
|
|
331
|
+
* let worldBytes = Bytes.fromString("World")
|
|
332
|
+
* assert Bytes.toString(Bytes.concat(helloBytes, worldBytes)) == "Hello World"
|
|
333
|
+
*
|
|
295
334
|
* @since v0.3.2
|
|
296
335
|
*/
|
|
297
|
-
|
|
336
|
+
provide let concat = (bytes1: Bytes, bytes2: Bytes) => {
|
|
298
337
|
let len1 = length(bytes1)
|
|
299
338
|
let len2 = length(bytes2)
|
|
300
339
|
let ret = resize(0, len2, bytes1)
|
|
@@ -308,14 +347,20 @@ export let concat = (bytes1: Bytes, bytes2: Bytes) => {
|
|
|
308
347
|
* @param value: The value replacing each byte
|
|
309
348
|
* @param bytes: The byte sequence to update
|
|
310
349
|
*
|
|
350
|
+
* @example
|
|
351
|
+
* let bytes = Bytes.make(5)
|
|
352
|
+
* Bytes.fill(1us, bytes)
|
|
353
|
+
* assert bytes == b"\x01\x01\x01\x01\x01"
|
|
354
|
+
*
|
|
311
355
|
* @since v0.3.2
|
|
356
|
+
* @history v0.6.0: `value` argument type changed to `Uint8`
|
|
312
357
|
*/
|
|
313
358
|
@unsafe
|
|
314
|
-
|
|
315
|
-
|
|
359
|
+
provide let fill = (value: Uint8, bytes: Bytes) => {
|
|
360
|
+
use WasmI32.{ (+) }
|
|
316
361
|
let src = WasmI32.fromGrain(bytes)
|
|
317
362
|
let size = getSize(src)
|
|
318
|
-
let v =
|
|
363
|
+
let v = untagUint8(value)
|
|
319
364
|
Memory.fill(src + _VALUE_OFFSET, v, size)
|
|
320
365
|
}
|
|
321
366
|
|
|
@@ -324,41 +369,78 @@ export let fill = (value: Int32, bytes: Bytes) => {
|
|
|
324
369
|
*
|
|
325
370
|
* @param bytes: The byte sequence to clear
|
|
326
371
|
*
|
|
372
|
+
* @example
|
|
373
|
+
* let bytes = Bytes.make(5)
|
|
374
|
+
* Bytes.fill(1us, bytes)
|
|
375
|
+
* Bytes.clear(bytes)
|
|
376
|
+
* assert bytes == b"\x00\x00\x00\x00\x00"
|
|
377
|
+
*
|
|
327
378
|
* @since v0.5.0
|
|
328
379
|
*/
|
|
329
380
|
@unsafe
|
|
330
|
-
|
|
331
|
-
|
|
381
|
+
provide let clear = (bytes: Bytes) => {
|
|
382
|
+
use WasmI32.{ (+) }
|
|
332
383
|
let src = WasmI32.fromGrain(bytes)
|
|
333
384
|
let size = getSize(src)
|
|
334
385
|
Memory.fill(src + _VALUE_OFFSET, 0n, size)
|
|
335
386
|
}
|
|
336
387
|
|
|
337
|
-
/**
|
|
338
|
-
* @section Binary operations on integers: Functions for encoding and decoding integers stored in a byte sequence.
|
|
339
|
-
*/
|
|
340
|
-
|
|
341
388
|
/**
|
|
342
389
|
* Gets a signed 8-bit integer starting at the given byte index.
|
|
343
390
|
*
|
|
344
391
|
* @param index: The byte index to access
|
|
345
392
|
* @param bytes: The byte sequence to access
|
|
346
|
-
* @returns A
|
|
393
|
+
* @returns A signed 8-bit integer that starts at the given index
|
|
347
394
|
*
|
|
348
395
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
349
396
|
* @throws IndexOutOfBounds: When `index + 1` is greater than the bytes size
|
|
350
397
|
*
|
|
351
|
-
* @
|
|
398
|
+
* @example
|
|
399
|
+
* let bytes = Bytes.make(1)
|
|
400
|
+
* Bytes.setInt8(0, 1s, bytes)
|
|
401
|
+
* assert Bytes.getInt8(0, bytes) == 1s
|
|
402
|
+
*
|
|
403
|
+
* @since v0.6.0
|
|
404
|
+
* @history v0.3.2: Originally called `getInt8S`, returning an `Int32`
|
|
352
405
|
*/
|
|
353
406
|
@unsafe
|
|
354
|
-
|
|
355
|
-
|
|
407
|
+
provide let getInt8 = (index: Number, bytes: Bytes) => {
|
|
408
|
+
use WasmI32.{ (+) }
|
|
356
409
|
let ptr = WasmI32.fromGrain(bytes)
|
|
357
410
|
let size = getSize(ptr)
|
|
358
411
|
let offset = coerceNumberToWasmI32(index)
|
|
359
412
|
checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
|
|
360
413
|
let n = WasmI32.load8S(ptr + offset, _VALUE_OFFSET)
|
|
361
|
-
|
|
414
|
+
tagInt8(n)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Sets a signed 8-bit integer starting at the given byte index.
|
|
419
|
+
*
|
|
420
|
+
* @param index: The byte index to update
|
|
421
|
+
* @param value: The value to set
|
|
422
|
+
* @param bytes: The byte sequence to mutate
|
|
423
|
+
*
|
|
424
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
425
|
+
* @throws IndexOutOfBounds: When `index + 1` is greater than the bytes size
|
|
426
|
+
*
|
|
427
|
+
* @example
|
|
428
|
+
* let bytes = Bytes.make(1)
|
|
429
|
+
* Bytes.setInt8(0, 2s, bytes)
|
|
430
|
+
* assert Bytes.getInt8(0, bytes) == 2s
|
|
431
|
+
*
|
|
432
|
+
* @since v0.3.2
|
|
433
|
+
* @history v0.6.0: `value` argument type changed to `Int8`
|
|
434
|
+
*/
|
|
435
|
+
@unsafe
|
|
436
|
+
provide let setInt8 = (index: Number, value: Int8, bytes: Bytes) => {
|
|
437
|
+
use WasmI32.{ (+) }
|
|
438
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
439
|
+
let size = getSize(ptr)
|
|
440
|
+
let offset = coerceNumberToWasmI32(index)
|
|
441
|
+
checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
|
|
442
|
+
let v = untagInt8(value)
|
|
443
|
+
WasmI32.store8(ptr + offset, v, _VALUE_OFFSET)
|
|
362
444
|
}
|
|
363
445
|
|
|
364
446
|
/**
|
|
@@ -366,26 +448,32 @@ export let getInt8S = (index: Number, bytes: Bytes) => {
|
|
|
366
448
|
*
|
|
367
449
|
* @param index: The byte index to access
|
|
368
450
|
* @param bytes: The byte sequence to access
|
|
369
|
-
* @returns
|
|
451
|
+
* @returns An unsigned 8-bit integer that starts at the given index
|
|
370
452
|
*
|
|
371
453
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
372
454
|
* @throws IndexOutOfBounds: When `index + 1` is greater than the bytes size
|
|
373
455
|
*
|
|
374
|
-
* @
|
|
456
|
+
* @example
|
|
457
|
+
* let bytes = Bytes.make(1)
|
|
458
|
+
* Bytes.setUint8(0, 1us, bytes)
|
|
459
|
+
* assert Bytes.getUint8(0, bytes) == 1us
|
|
460
|
+
*
|
|
461
|
+
* @since v0.6.0
|
|
462
|
+
* @history v0.3.2: Originally called `getInt8U`, returning an `Int32`
|
|
375
463
|
*/
|
|
376
464
|
@unsafe
|
|
377
|
-
|
|
378
|
-
|
|
465
|
+
provide let getUint8 = (index: Number, bytes: Bytes) => {
|
|
466
|
+
use WasmI32.{ (+) }
|
|
379
467
|
let ptr = WasmI32.fromGrain(bytes)
|
|
380
468
|
let size = getSize(ptr)
|
|
381
469
|
let offset = coerceNumberToWasmI32(index)
|
|
382
470
|
checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
|
|
383
471
|
let n = WasmI32.load8U(ptr + offset, _VALUE_OFFSET)
|
|
384
|
-
|
|
472
|
+
tagUint8(n)
|
|
385
473
|
}
|
|
386
474
|
|
|
387
475
|
/**
|
|
388
|
-
* Sets
|
|
476
|
+
* Sets an unsigned 8-bit integer starting at the given byte index.
|
|
389
477
|
*
|
|
390
478
|
* @param index: The byte index to update
|
|
391
479
|
* @param value: The value to set
|
|
@@ -394,16 +482,21 @@ export let getInt8U = (index: Number, bytes: Bytes) => {
|
|
|
394
482
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
395
483
|
* @throws IndexOutOfBounds: When `index + 1` is greater than the bytes size
|
|
396
484
|
*
|
|
397
|
-
* @
|
|
485
|
+
* @example
|
|
486
|
+
* let bytes = Bytes.make(2)
|
|
487
|
+
* Bytes.setUint8(1, 2us, bytes)
|
|
488
|
+
* assert Bytes.getUint8(1, bytes) == 2us
|
|
489
|
+
*
|
|
490
|
+
* @since v0.6.0
|
|
398
491
|
*/
|
|
399
492
|
@unsafe
|
|
400
|
-
|
|
401
|
-
|
|
493
|
+
provide let setUint8 = (index: Number, value: Uint8, bytes: Bytes) => {
|
|
494
|
+
use WasmI32.{ (+) }
|
|
402
495
|
let ptr = WasmI32.fromGrain(bytes)
|
|
403
496
|
let size = getSize(ptr)
|
|
404
497
|
let offset = coerceNumberToWasmI32(index)
|
|
405
498
|
checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
|
|
406
|
-
let v =
|
|
499
|
+
let v = untagUint8(value)
|
|
407
500
|
WasmI32.store8(ptr + offset, v, _VALUE_OFFSET)
|
|
408
501
|
}
|
|
409
502
|
|
|
@@ -412,22 +505,57 @@ export let setInt8 = (index: Number, value: Int32, bytes: Bytes) => {
|
|
|
412
505
|
*
|
|
413
506
|
* @param index: The byte index to access
|
|
414
507
|
* @param bytes: The byte sequence to access
|
|
415
|
-
* @returns A
|
|
508
|
+
* @returns A signed 16-bit integer that starts at the given index
|
|
416
509
|
*
|
|
417
510
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
418
511
|
* @throws IndexOutOfBounds: When `index + 2` is greater than the bytes size
|
|
419
512
|
*
|
|
420
|
-
* @
|
|
513
|
+
* @example
|
|
514
|
+
* let bytes = Bytes.make(2)
|
|
515
|
+
* Bytes.setInt16(0, -2S, bytes)
|
|
516
|
+
* assert Bytes.getInt16(0, bytes) == -2S
|
|
517
|
+
*
|
|
518
|
+
* @since v0.6.0
|
|
519
|
+
* @history v0.3.2: Originally called `getInt16S`, returning an `Int32`
|
|
421
520
|
*/
|
|
422
521
|
@unsafe
|
|
423
|
-
|
|
424
|
-
|
|
522
|
+
provide let getInt16 = (index: Number, bytes: Bytes) => {
|
|
523
|
+
use WasmI32.{ (+) }
|
|
425
524
|
let ptr = WasmI32.fromGrain(bytes)
|
|
426
525
|
let size = getSize(ptr)
|
|
427
526
|
let offset = coerceNumberToWasmI32(index)
|
|
428
527
|
checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
|
|
429
528
|
let n = WasmI32.load16S(ptr + offset, _VALUE_OFFSET)
|
|
430
|
-
|
|
529
|
+
tagInt16(n)
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Sets a signed 16-bit integer starting at the given byte index.
|
|
534
|
+
*
|
|
535
|
+
* @param index: The byte index to update
|
|
536
|
+
* @param value: The value to set
|
|
537
|
+
* @param bytes: The byte sequence to mutate
|
|
538
|
+
*
|
|
539
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
540
|
+
* @throws IndexOutOfBounds: When `index + 2` is greater than the bytes size
|
|
541
|
+
*
|
|
542
|
+
* @example
|
|
543
|
+
* let bytes = Bytes.make(2)
|
|
544
|
+
* Bytes.setInt16(0, -1S, bytes)
|
|
545
|
+
* assert Bytes.getInt16(0, bytes) == -1S
|
|
546
|
+
*
|
|
547
|
+
* @since v0.3.2
|
|
548
|
+
* @history v0.6.0: `value` argument type changed to `Int16`
|
|
549
|
+
*/
|
|
550
|
+
@unsafe
|
|
551
|
+
provide let setInt16 = (index: Number, value: Int16, bytes: Bytes) => {
|
|
552
|
+
use WasmI32.{ (+) }
|
|
553
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
554
|
+
let size = getSize(ptr)
|
|
555
|
+
let offset = coerceNumberToWasmI32(index)
|
|
556
|
+
checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
|
|
557
|
+
let v = untagInt16(value)
|
|
558
|
+
WasmI32.store16(ptr + offset, v, _VALUE_OFFSET)
|
|
431
559
|
}
|
|
432
560
|
|
|
433
561
|
/**
|
|
@@ -435,26 +563,32 @@ export let getInt16S = (index: Number, bytes: Bytes) => {
|
|
|
435
563
|
*
|
|
436
564
|
* @param index: The byte index to access
|
|
437
565
|
* @param bytes: The byte sequence to access
|
|
438
|
-
* @returns
|
|
566
|
+
* @returns An unsigned 16-bit integer that starts at the given index
|
|
439
567
|
*
|
|
440
568
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
441
569
|
* @throws IndexOutOfBounds: When `index + 2` is greater than the bytes size
|
|
442
570
|
*
|
|
443
|
-
* @
|
|
571
|
+
* @example
|
|
572
|
+
* let bytes = Bytes.make(2)
|
|
573
|
+
* Bytes.setUint16(0, 2uS, bytes)
|
|
574
|
+
* assert Bytes.getUint16(0, bytes) == 2uS
|
|
575
|
+
*
|
|
576
|
+
* @since v0.6.0
|
|
577
|
+
* @history v0.3.2: Originally called `getInt16U`, returning an `Int32`
|
|
444
578
|
*/
|
|
445
579
|
@unsafe
|
|
446
|
-
|
|
447
|
-
|
|
580
|
+
provide let getUint16 = (index: Number, bytes: Bytes) => {
|
|
581
|
+
use WasmI32.{ (+) }
|
|
448
582
|
let ptr = WasmI32.fromGrain(bytes)
|
|
449
583
|
let size = getSize(ptr)
|
|
450
584
|
let offset = coerceNumberToWasmI32(index)
|
|
451
585
|
checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
|
|
452
586
|
let n = WasmI32.load16U(ptr + offset, _VALUE_OFFSET)
|
|
453
|
-
|
|
587
|
+
tagUint16(n)
|
|
454
588
|
}
|
|
455
589
|
|
|
456
590
|
/**
|
|
457
|
-
* Sets
|
|
591
|
+
* Sets an unsigned 16-bit integer starting at the given byte index.
|
|
458
592
|
*
|
|
459
593
|
* @param index: The byte index to update
|
|
460
594
|
* @param value: The value to set
|
|
@@ -463,16 +597,21 @@ export let getInt16U = (index: Number, bytes: Bytes) => {
|
|
|
463
597
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
464
598
|
* @throws IndexOutOfBounds: When `index + 2` is greater than the bytes size
|
|
465
599
|
*
|
|
466
|
-
* @
|
|
600
|
+
* @example
|
|
601
|
+
* let bytes = Bytes.make(2)
|
|
602
|
+
* Bytes.setUint16(0, 2uS, bytes)
|
|
603
|
+
* assert Bytes.getUint16(0, bytes) == 2uS
|
|
604
|
+
*
|
|
605
|
+
* @since v0.6.0
|
|
467
606
|
*/
|
|
468
607
|
@unsafe
|
|
469
|
-
|
|
470
|
-
|
|
608
|
+
provide let setUint16 = (index: Number, value: Uint16, bytes: Bytes) => {
|
|
609
|
+
use WasmI32.{ (+) }
|
|
471
610
|
let ptr = WasmI32.fromGrain(bytes)
|
|
472
611
|
let size = getSize(ptr)
|
|
473
612
|
let offset = coerceNumberToWasmI32(index)
|
|
474
613
|
checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
|
|
475
|
-
let v =
|
|
614
|
+
let v = untagUint16(value)
|
|
476
615
|
WasmI32.store16(ptr + offset, v, _VALUE_OFFSET)
|
|
477
616
|
}
|
|
478
617
|
|
|
@@ -486,11 +625,16 @@ export let setInt16 = (index: Number, value: Int32, bytes: Bytes) => {
|
|
|
486
625
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
487
626
|
* @throws IndexOutOfBounds: When `index + 4` is greater than the bytes size
|
|
488
627
|
*
|
|
628
|
+
* @example
|
|
629
|
+
* let bytes = Bytes.make(4)
|
|
630
|
+
* Bytes.setInt32(0, 1l, bytes)
|
|
631
|
+
* assert Bytes.getInt32(0, bytes) == 1l
|
|
632
|
+
*
|
|
489
633
|
* @since v0.3.2
|
|
490
634
|
*/
|
|
491
635
|
@unsafe
|
|
492
|
-
|
|
493
|
-
|
|
636
|
+
provide let getInt32 = (index: Number, bytes: Bytes) => {
|
|
637
|
+
use WasmI32.{ (+) }
|
|
494
638
|
let ptr = WasmI32.fromGrain(bytes)
|
|
495
639
|
let size = getSize(ptr)
|
|
496
640
|
let offset = coerceNumberToWasmI32(index)
|
|
@@ -509,11 +653,16 @@ export let getInt32 = (index: Number, bytes: Bytes) => {
|
|
|
509
653
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
510
654
|
* @throws IndexOutOfBounds: When `index + 4` is greater than the bytes size
|
|
511
655
|
*
|
|
656
|
+
* @example
|
|
657
|
+
* let bytes = Bytes.make(4)
|
|
658
|
+
* Bytes.setInt32(0, 1l, bytes)
|
|
659
|
+
* assert Bytes.getInt32(0, bytes) == 1l
|
|
660
|
+
*
|
|
512
661
|
* @since v0.3.2
|
|
513
662
|
*/
|
|
514
663
|
@unsafe
|
|
515
|
-
|
|
516
|
-
|
|
664
|
+
provide let setInt32 = (index: Number, value: Int32, bytes: Bytes) => {
|
|
665
|
+
use WasmI32.{ (+) }
|
|
517
666
|
let ptr = WasmI32.fromGrain(bytes)
|
|
518
667
|
let size = getSize(ptr)
|
|
519
668
|
let offset = coerceNumberToWasmI32(index)
|
|
@@ -522,6 +671,62 @@ export let setInt32 = (index: Number, value: Int32, bytes: Bytes) => {
|
|
|
522
671
|
WasmI32.store(ptr + offset, v, _VALUE_OFFSET)
|
|
523
672
|
}
|
|
524
673
|
|
|
674
|
+
/**
|
|
675
|
+
* Gets an unsigned 32-bit integer starting at the given byte index.
|
|
676
|
+
*
|
|
677
|
+
* @param index: The byte index to access
|
|
678
|
+
* @param bytes: The byte sequence to access
|
|
679
|
+
* @returns An unsigned 32-bit integer that starts at the given index
|
|
680
|
+
*
|
|
681
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
682
|
+
* @throws IndexOutOfBounds: When `index + 4` is greater than the bytes size
|
|
683
|
+
*
|
|
684
|
+
* @example
|
|
685
|
+
* let bytes = Bytes.make(4)
|
|
686
|
+
* Bytes.setUint32(0, 1ul, bytes)
|
|
687
|
+
* assert Bytes.getUint32(0, bytes) == 1ul
|
|
688
|
+
*
|
|
689
|
+
* @since v0.6.0
|
|
690
|
+
*/
|
|
691
|
+
@unsafe
|
|
692
|
+
provide let getUint32 = (index: Number, bytes: Bytes) => {
|
|
693
|
+
use WasmI32.{ (+) }
|
|
694
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
695
|
+
let size = getSize(ptr)
|
|
696
|
+
let offset = coerceNumberToWasmI32(index)
|
|
697
|
+
checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
|
|
698
|
+
let n = WasmI32.load(ptr + offset, _VALUE_OFFSET)
|
|
699
|
+
Conv.toUint32(n)
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Sets an unsigned 32-bit integer starting at the given byte index.
|
|
704
|
+
*
|
|
705
|
+
* @param index: The byte index to update
|
|
706
|
+
* @param value: The value to set
|
|
707
|
+
* @param bytes: The byte sequence to mutate
|
|
708
|
+
*
|
|
709
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
710
|
+
* @throws IndexOutOfBounds: When `index + 4` is greater than the bytes size
|
|
711
|
+
*
|
|
712
|
+
* @example
|
|
713
|
+
* let bytes = Bytes.make(4)
|
|
714
|
+
* Bytes.setUint32(0, 1ul, bytes)
|
|
715
|
+
* assert Bytes.getUint32(0, bytes) == 1ul
|
|
716
|
+
*
|
|
717
|
+
* @since v0.6.0
|
|
718
|
+
*/
|
|
719
|
+
@unsafe
|
|
720
|
+
provide let setUint32 = (index: Number, value: Uint32, bytes: Bytes) => {
|
|
721
|
+
use WasmI32.{ (+) }
|
|
722
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
723
|
+
let size = getSize(ptr)
|
|
724
|
+
let offset = coerceNumberToWasmI32(index)
|
|
725
|
+
checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
|
|
726
|
+
let v = Conv.fromUint32(value)
|
|
727
|
+
WasmI32.store(ptr + offset, v, _VALUE_OFFSET)
|
|
728
|
+
}
|
|
729
|
+
|
|
525
730
|
/**
|
|
526
731
|
* Gets a 32-bit float starting at the given byte index.
|
|
527
732
|
*
|
|
@@ -532,11 +737,16 @@ export let setInt32 = (index: Number, value: Int32, bytes: Bytes) => {
|
|
|
532
737
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
533
738
|
* @throws IndexOutOfBounds: When `index + 4` is greater than the bytes size
|
|
534
739
|
*
|
|
740
|
+
* @example
|
|
741
|
+
* let bytes = Bytes.make(4)
|
|
742
|
+
* Bytes.setFloat32(0, 1.0f, bytes)
|
|
743
|
+
* assert Bytes.getFloat32(0, bytes) == 1.0f
|
|
744
|
+
*
|
|
535
745
|
* @since v0.3.2
|
|
536
746
|
*/
|
|
537
747
|
@unsafe
|
|
538
|
-
|
|
539
|
-
|
|
748
|
+
provide let getFloat32 = (index: Number, bytes: Bytes) => {
|
|
749
|
+
use WasmI32.{ (+) }
|
|
540
750
|
let ptr = WasmI32.fromGrain(bytes)
|
|
541
751
|
let size = getSize(ptr)
|
|
542
752
|
let offset = coerceNumberToWasmI32(index)
|
|
@@ -555,11 +765,16 @@ export let getFloat32 = (index: Number, bytes: Bytes) => {
|
|
|
555
765
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
556
766
|
* @throws IndexOutOfBounds: When `index + 4` is greater than the bytes size
|
|
557
767
|
*
|
|
768
|
+
* @example
|
|
769
|
+
* let bytes = Bytes.make(4)
|
|
770
|
+
* Bytes.setFloat32(0, 1.0f, bytes)
|
|
771
|
+
* assert Bytes.getFloat32(0, bytes) == 1.0f
|
|
772
|
+
*
|
|
558
773
|
* @since v0.3.2
|
|
559
774
|
*/
|
|
560
775
|
@unsafe
|
|
561
|
-
|
|
562
|
-
|
|
776
|
+
provide let setFloat32 = (index: Number, value: Float32, bytes: Bytes) => {
|
|
777
|
+
use WasmI32.{ (+) }
|
|
563
778
|
let ptr = WasmI32.fromGrain(bytes)
|
|
564
779
|
let size = getSize(ptr)
|
|
565
780
|
let offset = coerceNumberToWasmI32(index)
|
|
@@ -578,11 +793,16 @@ export let setFloat32 = (index: Number, value: Float32, bytes: Bytes) => {
|
|
|
578
793
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
579
794
|
* @throws IndexOutOfBounds: When `index + 8` is greater than the bytes size
|
|
580
795
|
*
|
|
796
|
+
* @example
|
|
797
|
+
* let bytes = Bytes.make(8)
|
|
798
|
+
* Bytes.setInt64(0, 1L, bytes)
|
|
799
|
+
* assert Bytes.getInt64(0, bytes) == 1L
|
|
800
|
+
*
|
|
581
801
|
* @since v0.3.2
|
|
582
802
|
*/
|
|
583
803
|
@unsafe
|
|
584
|
-
|
|
585
|
-
|
|
804
|
+
provide let getInt64 = (index: Number, bytes: Bytes) => {
|
|
805
|
+
use WasmI32.{ (+) }
|
|
586
806
|
let ptr = WasmI32.fromGrain(bytes)
|
|
587
807
|
let size = getSize(ptr)
|
|
588
808
|
let offset = coerceNumberToWasmI32(index)
|
|
@@ -601,11 +821,16 @@ export let getInt64 = (index: Number, bytes: Bytes) => {
|
|
|
601
821
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
602
822
|
* @throws IndexOutOfBounds: When `index + 8` is greater than the bytes size
|
|
603
823
|
*
|
|
824
|
+
* @example
|
|
825
|
+
* let bytes = Bytes.make(8)
|
|
826
|
+
* Bytes.setInt64(0, 1L, bytes)
|
|
827
|
+
* assert Bytes.getInt64(0, bytes) == 1L
|
|
828
|
+
*
|
|
604
829
|
* @since v0.3.2
|
|
605
830
|
*/
|
|
606
831
|
@unsafe
|
|
607
|
-
|
|
608
|
-
|
|
832
|
+
provide let setInt64 = (index: Number, value: Int64, bytes: Bytes) => {
|
|
833
|
+
use WasmI32.{ (+) }
|
|
609
834
|
let ptr = WasmI32.fromGrain(bytes)
|
|
610
835
|
let size = getSize(ptr)
|
|
611
836
|
let offset = coerceNumberToWasmI32(index)
|
|
@@ -614,6 +839,62 @@ export let setInt64 = (index: Number, value: Int64, bytes: Bytes) => {
|
|
|
614
839
|
WasmI64.store(ptr + offset, v, _VALUE_OFFSET)
|
|
615
840
|
}
|
|
616
841
|
|
|
842
|
+
/**
|
|
843
|
+
* Gets an unsigned 64-bit integer starting at the given byte index.
|
|
844
|
+
*
|
|
845
|
+
* @param index: The byte index to access
|
|
846
|
+
* @param bytes: The byte sequence to access
|
|
847
|
+
* @returns An unsigned 64-bit integer that starts at the given index
|
|
848
|
+
*
|
|
849
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
850
|
+
* @throws IndexOutOfBounds: When `index + 8` is greater than the bytes size
|
|
851
|
+
*
|
|
852
|
+
* @example
|
|
853
|
+
* let bytes = Bytes.make(8)
|
|
854
|
+
* Bytes.setUint64(0, 1uL, bytes)
|
|
855
|
+
* assert Bytes.getUint64(0, bytes) == 1uL
|
|
856
|
+
*
|
|
857
|
+
* @since v0.6.0
|
|
858
|
+
*/
|
|
859
|
+
@unsafe
|
|
860
|
+
provide let getUint64 = (index: Number, bytes: Bytes) => {
|
|
861
|
+
use WasmI32.{ (+) }
|
|
862
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
863
|
+
let size = getSize(ptr)
|
|
864
|
+
let offset = coerceNumberToWasmI32(index)
|
|
865
|
+
checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, size)
|
|
866
|
+
let n = WasmI64.load(ptr + offset, _VALUE_OFFSET)
|
|
867
|
+
Conv.toUint64(n)
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* Sets an unsigned 64-bit integer starting at the given byte index.
|
|
872
|
+
*
|
|
873
|
+
* @param index: The byte index to update
|
|
874
|
+
* @param value: The value to set
|
|
875
|
+
* @param bytes: The byte sequence to mutate
|
|
876
|
+
*
|
|
877
|
+
* @throws IndexOutOfBounds: When `index` is negative
|
|
878
|
+
* @throws IndexOutOfBounds: When `index + 8` is greater than the bytes size
|
|
879
|
+
*
|
|
880
|
+
* @example
|
|
881
|
+
* let bytes = Bytes.make(8)
|
|
882
|
+
* Bytes.setUint64(0, 1uL, bytes)
|
|
883
|
+
* assert Bytes.getUint64(0, bytes) == 1uL
|
|
884
|
+
*
|
|
885
|
+
* @since v0.6.0
|
|
886
|
+
*/
|
|
887
|
+
@unsafe
|
|
888
|
+
provide let setUint64 = (index: Number, value: Uint64, bytes: Bytes) => {
|
|
889
|
+
use WasmI32.{ (+) }
|
|
890
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
891
|
+
let size = getSize(ptr)
|
|
892
|
+
let offset = coerceNumberToWasmI32(index)
|
|
893
|
+
checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, size)
|
|
894
|
+
let v = Conv.fromUint64(value)
|
|
895
|
+
WasmI64.store(ptr + offset, v, _VALUE_OFFSET)
|
|
896
|
+
}
|
|
897
|
+
|
|
617
898
|
/**
|
|
618
899
|
* Gets a 64-bit float starting at the given byte index.
|
|
619
900
|
*
|
|
@@ -624,11 +905,16 @@ export let setInt64 = (index: Number, value: Int64, bytes: Bytes) => {
|
|
|
624
905
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
625
906
|
* @throws IndexOutOfBounds: When `index + 8` is greater than the bytes size
|
|
626
907
|
*
|
|
908
|
+
* @example
|
|
909
|
+
* let bytes = Bytes.make(8)
|
|
910
|
+
* Bytes.setFloat64(0, 1.0d, bytes)
|
|
911
|
+
* assert Bytes.getFloat64(0, bytes) == 1.0d
|
|
912
|
+
*
|
|
627
913
|
* @since v0.3.2
|
|
628
914
|
*/
|
|
629
915
|
@unsafe
|
|
630
|
-
|
|
631
|
-
|
|
916
|
+
provide let getFloat64 = (index: Number, bytes: Bytes) => {
|
|
917
|
+
use WasmI32.{ (+) }
|
|
632
918
|
let ptr = WasmI32.fromGrain(bytes)
|
|
633
919
|
let size = getSize(ptr)
|
|
634
920
|
let offset = coerceNumberToWasmI32(index)
|
|
@@ -647,11 +933,16 @@ export let getFloat64 = (index: Number, bytes: Bytes) => {
|
|
|
647
933
|
* @throws IndexOutOfBounds: When `index` is negative
|
|
648
934
|
* @throws IndexOutOfBounds: When `index + 8` is greater than the bytes size
|
|
649
935
|
*
|
|
936
|
+
* @example
|
|
937
|
+
* let bytes = Bytes.make(8)
|
|
938
|
+
* Bytes.setFloat64(0, 1.0d, bytes)
|
|
939
|
+
* assert Bytes.getFloat64(0, bytes) == 1.0d
|
|
940
|
+
*
|
|
650
941
|
* @since v0.3.2
|
|
651
942
|
*/
|
|
652
943
|
@unsafe
|
|
653
|
-
|
|
654
|
-
|
|
944
|
+
provide let setFloat64 = (index: Number, value: Float64, bytes: Bytes) => {
|
|
945
|
+
use WasmI32.{ (+) }
|
|
655
946
|
let ptr = WasmI32.fromGrain(bytes)
|
|
656
947
|
let size = getSize(ptr)
|
|
657
948
|
let offset = coerceNumberToWasmI32(index)
|