@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.
Files changed (155) hide show
  1. package/CHANGELOG.md +193 -0
  2. package/LICENSE +1 -1
  3. package/README.md +25 -2
  4. package/array.gr +1512 -199
  5. package/array.md +2032 -94
  6. package/bigint.gr +239 -140
  7. package/bigint.md +450 -106
  8. package/buffer.gr +595 -102
  9. package/buffer.md +903 -145
  10. package/bytes.gr +401 -110
  11. package/bytes.md +551 -63
  12. package/char.gr +228 -49
  13. package/char.md +373 -7
  14. package/exception.gr +26 -12
  15. package/exception.md +29 -5
  16. package/float32.gr +130 -109
  17. package/float32.md +185 -57
  18. package/float64.gr +112 -99
  19. package/float64.md +185 -57
  20. package/hash.gr +47 -37
  21. package/hash.md +21 -3
  22. package/int16.gr +430 -0
  23. package/int16.md +618 -0
  24. package/int32.gr +200 -269
  25. package/int32.md +254 -289
  26. package/int64.gr +142 -225
  27. package/int64.md +254 -289
  28. package/int8.gr +511 -0
  29. package/int8.md +786 -0
  30. package/json.gr +2084 -0
  31. package/json.md +608 -0
  32. package/list.gr +120 -68
  33. package/list.md +125 -80
  34. package/map.gr +560 -57
  35. package/map.md +672 -56
  36. package/marshal.gr +239 -227
  37. package/marshal.md +36 -4
  38. package/number.gr +626 -676
  39. package/number.md +738 -153
  40. package/option.gr +33 -35
  41. package/option.md +58 -42
  42. package/package.json +2 -2
  43. package/path.gr +148 -187
  44. package/path.md +47 -96
  45. package/pervasives.gr +75 -416
  46. package/pervasives.md +85 -180
  47. package/priorityqueue.gr +433 -74
  48. package/priorityqueue.md +422 -54
  49. package/queue.gr +362 -80
  50. package/queue.md +433 -38
  51. package/random.gr +67 -75
  52. package/random.md +68 -40
  53. package/range.gr +135 -63
  54. package/range.md +198 -43
  55. package/rational.gr +284 -0
  56. package/rational.md +545 -0
  57. package/regex.gr +933 -1066
  58. package/regex.md +59 -60
  59. package/result.gr +23 -25
  60. package/result.md +54 -39
  61. package/runtime/atof/common.gr +78 -82
  62. package/runtime/atof/common.md +22 -10
  63. package/runtime/atof/decimal.gr +102 -127
  64. package/runtime/atof/decimal.md +28 -7
  65. package/runtime/atof/lemire.gr +56 -71
  66. package/runtime/atof/lemire.md +9 -1
  67. package/runtime/atof/parse.gr +83 -110
  68. package/runtime/atof/parse.md +12 -2
  69. package/runtime/atof/slow.gr +28 -35
  70. package/runtime/atof/slow.md +9 -1
  71. package/runtime/atof/table.gr +19 -18
  72. package/runtime/atof/table.md +10 -2
  73. package/runtime/atoi/parse.gr +153 -136
  74. package/runtime/atoi/parse.md +50 -1
  75. package/runtime/bigint.gr +410 -517
  76. package/runtime/bigint.md +71 -57
  77. package/runtime/compare.gr +176 -85
  78. package/runtime/compare.md +31 -1
  79. package/runtime/dataStructures.gr +144 -32
  80. package/runtime/dataStructures.md +267 -31
  81. package/runtime/debugPrint.gr +34 -15
  82. package/runtime/debugPrint.md +37 -5
  83. package/runtime/equal.gr +53 -52
  84. package/runtime/equal.md +30 -1
  85. package/runtime/exception.gr +38 -47
  86. package/runtime/exception.md +10 -8
  87. package/runtime/gc.gr +23 -152
  88. package/runtime/gc.md +13 -17
  89. package/runtime/malloc.gr +31 -31
  90. package/runtime/malloc.md +11 -3
  91. package/runtime/numberUtils.gr +191 -172
  92. package/runtime/numberUtils.md +17 -9
  93. package/runtime/numbers.gr +1695 -1021
  94. package/runtime/numbers.md +1098 -134
  95. package/runtime/string.gr +540 -242
  96. package/runtime/string.md +76 -6
  97. package/runtime/unsafe/constants.gr +30 -13
  98. package/runtime/unsafe/constants.md +80 -0
  99. package/runtime/unsafe/conv.gr +55 -28
  100. package/runtime/unsafe/conv.md +41 -9
  101. package/runtime/unsafe/memory.gr +10 -30
  102. package/runtime/unsafe/memory.md +15 -19
  103. package/runtime/unsafe/tags.gr +37 -21
  104. package/runtime/unsafe/tags.md +88 -8
  105. package/runtime/unsafe/wasmf32.gr +30 -36
  106. package/runtime/unsafe/wasmf32.md +64 -56
  107. package/runtime/unsafe/wasmf64.gr +30 -36
  108. package/runtime/unsafe/wasmf64.md +64 -56
  109. package/runtime/unsafe/wasmi32.gr +49 -66
  110. package/runtime/unsafe/wasmi32.md +102 -94
  111. package/runtime/unsafe/wasmi64.gr +52 -79
  112. package/runtime/unsafe/wasmi64.md +108 -100
  113. package/runtime/utils/printing.gr +13 -15
  114. package/runtime/utils/printing.md +11 -3
  115. package/runtime/wasi.gr +294 -295
  116. package/runtime/wasi.md +62 -42
  117. package/set.gr +574 -64
  118. package/set.md +634 -54
  119. package/stack.gr +181 -64
  120. package/stack.md +271 -42
  121. package/string.gr +453 -533
  122. package/string.md +241 -151
  123. package/uint16.gr +369 -0
  124. package/uint16.md +585 -0
  125. package/uint32.gr +470 -0
  126. package/uint32.md +737 -0
  127. package/uint64.gr +471 -0
  128. package/uint64.md +737 -0
  129. package/uint8.gr +369 -0
  130. package/uint8.md +585 -0
  131. package/uri.gr +1093 -0
  132. package/uri.md +477 -0
  133. package/{sys → wasi}/file.gr +914 -500
  134. package/{sys → wasi}/file.md +454 -50
  135. package/wasi/process.gr +292 -0
  136. package/{sys → wasi}/process.md +164 -6
  137. package/wasi/random.gr +77 -0
  138. package/wasi/random.md +80 -0
  139. package/{sys → wasi}/time.gr +15 -22
  140. package/{sys → wasi}/time.md +5 -5
  141. package/immutablearray.gr +0 -929
  142. package/immutablearray.md +0 -1038
  143. package/immutablemap.gr +0 -493
  144. package/immutablemap.md +0 -479
  145. package/immutablepriorityqueue.gr +0 -360
  146. package/immutablepriorityqueue.md +0 -291
  147. package/immutableset.gr +0 -498
  148. package/immutableset.md +0 -449
  149. package/runtime/debug.gr +0 -2
  150. package/runtime/debug.md +0 -6
  151. package/runtime/unsafe/errors.gr +0 -36
  152. package/runtime/unsafe/errors.md +0 -204
  153. package/sys/process.gr +0 -254
  154. package/sys/random.gr +0 -79
  155. package/sys/random.md +0 -66
package/buffer.gr CHANGED
@@ -1,33 +1,40 @@
1
1
  /**
2
- * @module Buffer: Utilities for working with buffers.
2
+ * Utilities for working with buffers.
3
3
  *
4
4
  * Buffers are data structures that automatically expand as more data is appended. They are useful for storing and operating on an unknown number of bytes. All set or append operations mutate the buffer.
5
- * @example import Buffer from "buffer"
5
+ *
6
+ * @example from "buffer" include Buffer
7
+ *
8
+ * @example Buffer.make(64)
6
9
  *
7
10
  * @since v0.4.0
8
11
  */
9
-
10
- import Memory from "runtime/unsafe/memory"
11
- import WasmI32 from "runtime/unsafe/wasmi32"
12
- import Conv from "runtime/unsafe/conv"
13
- import Exception from "runtime/exception"
14
- import Int32 from "int32"
15
- import Bytes from "bytes"
16
- import String from "string"
17
- import Char from "char"
18
- import { coerceNumberToWasmI32 } from "runtime/numbers"
19
-
20
- record Buffer {
12
+ module Buffer
13
+
14
+ from "runtime/unsafe/memory" include Memory
15
+ from "runtime/unsafe/wasmi32" include WasmI32
16
+ from "runtime/unsafe/conv" include Conv
17
+ from "runtime/exception" include Exception
18
+ from "runtime/dataStructures" include DataStructures
19
+ use DataStructures.{ untagChar }
20
+ from "int32" include Int32
21
+ from "bytes" include Bytes
22
+ from "string" include String
23
+ from "char" include Char
24
+ from "runtime/numbers" include Numbers
25
+ use Numbers.{ coerceNumberToWasmI32 }
26
+
27
+ abstract record Buffer {
21
28
  mut len: Number,
22
29
  initialSize: Number,
23
30
  mut data: Bytes,
24
31
  }
25
32
 
26
33
  @unsafe
27
- let mut _SIZE_OFFSET = 4n
34
+ let _SIZE_OFFSET = 4n
28
35
 
29
36
  @unsafe
30
- let mut _VALUE_OFFSET = 8n
37
+ let _VALUE_OFFSET = 8n
31
38
 
32
39
  let _8BIT_LEN = 1
33
40
 
@@ -67,7 +74,7 @@ let autogrow = (len, buf) => {
67
74
  /* Memcopies bytes from a source byte sequence to a destination byte sequence via pointers */
68
75
  @unsafe
69
76
  let appendBytes = (srcOff, dstOff, len, src, dst) => {
70
- let (+) = WasmI32.add
77
+ use WasmI32.{ (+) }
71
78
  Memory.copy(dst + _VALUE_OFFSET + dstOff, src + _VALUE_OFFSET + srcOff, len)
72
79
  }
73
80
 
@@ -76,8 +83,8 @@ let appendBytes = (srcOff, dstOff, len, src, dst) => {
76
83
  * Throws if a given index + len is not within the bounds of a buffer's current length
77
84
  */
78
85
  let checkIsIndexInBounds = (i, len, buf) => {
79
- if (i < 0) throw Exception.IndexOutOfBounds
80
- if (i >= buf.len || i + len > buf.len) throw Exception.IndexOutOfBounds
86
+ if (i < 0) throw IndexOutOfBounds
87
+ if (i >= buf.len || i + len > buf.len) throw IndexOutOfBounds
81
88
  }
82
89
 
83
90
  let addInt8help = (value, buffer) => {
@@ -87,6 +94,13 @@ let addInt8help = (value, buffer) => {
87
94
  Bytes.setInt8(index, value, buffer.data)
88
95
  }
89
96
 
97
+ let addUint8help = (value, buffer) => {
98
+ autogrow(_8BIT_LEN, buffer)
99
+ let index = buffer.len
100
+ buffer.len += _8BIT_LEN
101
+ Bytes.setUint8(index, value, buffer.data)
102
+ }
103
+
90
104
  let addInt16help = (value, buffer) => {
91
105
  autogrow(_16BIT_LEN, buffer)
92
106
  let index = buffer.len
@@ -94,6 +108,13 @@ let addInt16help = (value, buffer) => {
94
108
  Bytes.setInt16(index, value, buffer.data)
95
109
  }
96
110
 
111
+ let addUint16help = (value, buffer) => {
112
+ autogrow(_16BIT_LEN, buffer)
113
+ let index = buffer.len
114
+ buffer.len += _16BIT_LEN
115
+ Bytes.setUint16(index, value, buffer.data)
116
+ }
117
+
97
118
  let addInt32help = (value, buffer) => {
98
119
  autogrow(_32BIT_LEN, buffer)
99
120
  let index = buffer.len
@@ -101,9 +122,12 @@ let addInt32help = (value, buffer) => {
101
122
  Bytes.setInt32(index, value, buffer.data)
102
123
  }
103
124
 
104
- /**
105
- * @section Values: Functions for working with the Buffer data type.
106
- */
125
+ let addUint32help = (value, buffer) => {
126
+ autogrow(_32BIT_LEN, buffer)
127
+ let index = buffer.len
128
+ buffer.len += _32BIT_LEN
129
+ Bytes.setUint32(index, value, buffer.data)
130
+ }
107
131
 
108
132
  /**
109
133
  * Creates a fresh buffer, initially empty.
@@ -116,9 +140,12 @@ let addInt32help = (value, buffer) => {
116
140
  *
117
141
  * @throws InvalidArgument(String): When the `initialSize` is a negative number
118
142
  *
143
+ * @example Buffer.make(0)
144
+ * @example Buffer.make(64)
145
+ *
119
146
  * @since v0.4.0
120
147
  */
121
- export let make = initialSize => {
148
+ provide let make = initialSize => {
122
149
  if (initialSize < 0)
123
150
  throw Exception.InvalidArgument("Buffers size must be >= 0")
124
151
  { len: 0, initialSize, data: Bytes.make(initialSize) }
@@ -130,9 +157,15 @@ export let make = initialSize => {
130
157
  * @param buffer: The buffer to access
131
158
  * @returns The length of the buffer in bytes
132
159
  *
160
+ * @example Buffer.length(Buffer.make(32)) == 0
161
+ * @example
162
+ * let buf = Buffer.make(32)
163
+ * Buffer.addInt32(1l, buf)
164
+ * assert Buffer.length(buf) == 4
165
+ *
133
166
  * @since v0.4.0
134
167
  */
135
- export let length = buffer => buffer.len
168
+ provide let length = buffer => buffer.len
136
169
 
137
170
  /**
138
171
  * Clears data in the buffer and sets its length to zero.
@@ -141,9 +174,16 @@ export let length = buffer => buffer.len
141
174
  *
142
175
  * @param buffer: The buffer to clear
143
176
  *
177
+ * @example
178
+ * let buf = Buffer.make(0)
179
+ * Buffer.addInt32(1l, buf)
180
+ * assert Buffer.length(buf) == 4
181
+ * Buffer.clear(buf)
182
+ * assert Buffer.length(buf) == 0
183
+ *
144
184
  * @since v0.4.0
145
185
  */
146
- export let clear = buffer => {
186
+ provide let clear = buffer => {
147
187
  Bytes.clear(buffer.data)
148
188
  buffer.len = 0
149
189
  }
@@ -155,9 +195,16 @@ export let clear = buffer => {
155
195
  *
156
196
  * @param buffer: The buffer to reset
157
197
  *
198
+ * @example
199
+ * let buf = Buffer.make(0)
200
+ * Buffer.addInt32(1l, buf)
201
+ * assert Buffer.length(buf) == 4
202
+ * Buffer.reset(buf)
203
+ * assert Buffer.length(buf) == 0
204
+ *
158
205
  * @since v0.4.0
159
206
  */
160
- export let reset = buffer => {
207
+ provide let reset = buffer => {
161
208
  buffer.data = Bytes.make(buffer.initialSize)
162
209
  buffer.len = 0
163
210
  }
@@ -173,17 +220,24 @@ export let reset = buffer => {
173
220
  * @throws IndexOutOfBounds: When the `length` is negative
174
221
  * @throws IndexOutOfBounds: When the `length` is greater than the buffer size
175
222
  *
223
+ * @example
224
+ * let buf = Buffer.make(0)
225
+ * Buffer.addInt32(1l, buf)
226
+ * assert Buffer.length(buf) == 4
227
+ * Buffer.truncate(1, buf)
228
+ * assert Buffer.length(buf) == 1
229
+ *
176
230
  * @since v0.4.0
177
231
  */
178
232
  @unsafe
179
- export let truncate = (length, buffer) => {
180
- if (length < 0 || length > buffer.len) throw Exception.IndexOutOfBounds
233
+ provide let truncate = (length, buffer) => {
234
+ if (length < 0 || length > buffer.len) throw IndexOutOfBounds
181
235
 
182
- let (+) = WasmI32.add
236
+ use WasmI32.{ (+), (-) }
183
237
  let src = WasmI32.fromGrain(buffer.data)
184
238
  let size = getSize(src)
185
239
  let off = coerceNumberToWasmI32(length)
186
- let ret = Memory.fill(src + _VALUE_OFFSET + off, 0n, size)
240
+ let ret = Memory.fill(src + _VALUE_OFFSET + off, 0n, size - off)
187
241
  buffer.len = length
188
242
  }
189
243
 
@@ -193,9 +247,14 @@ export let truncate = (length, buffer) => {
193
247
  * @param buffer: The buffer to copy into a byte sequence
194
248
  * @returns A byte sequence made from copied buffer data
195
249
  *
250
+ * @example
251
+ * let buf = Buffer.make(0)
252
+ * Buffer.addString("test", buf)
253
+ * assert Buffer.toBytes(buf) == b"test"
254
+ *
196
255
  * @since v0.4.0
197
256
  */
198
- export let toBytes = buffer => {
257
+ provide let toBytes = buffer => {
199
258
  Bytes.slice(0, buffer.len, buffer.data)
200
259
  }
201
260
 
@@ -211,9 +270,14 @@ export let toBytes = buffer => {
211
270
  * @throws IndexOutOfBounds: When `start` is greater than or equal to the buffer size
212
271
  * @throws IndexOutOfBounds: When `start + length` is greater than the buffer size
213
272
  *
273
+ * @example
274
+ * let buf = Buffer.make(0)
275
+ * Buffer.addString("HelloWorld", buf)
276
+ * assert Buffer.toBytesSlice(0, 5, buf) == b"Hello"
277
+ *
214
278
  * @since v0.4.0
215
279
  */
216
- export let toBytesSlice = (start, length, buffer) => {
280
+ provide let toBytesSlice = (start, length, buffer) => {
217
281
  checkIsIndexInBounds(start, length, buffer)
218
282
  Bytes.slice(start, length, buffer.data)
219
283
  }
@@ -224,9 +288,14 @@ export let toBytesSlice = (start, length, buffer) => {
224
288
  * @param buffer: The buffer to stringify
225
289
  * @returns A string made with data copied from the buffer
226
290
  *
291
+ * @example
292
+ * let buf = Buffer.make(0)
293
+ * Buffer.addString("HelloWorld", buf)
294
+ * assert Buffer.toString(buf) == "HelloWorld"
295
+ *
227
296
  * @since v0.4.0
228
297
  */
229
- export let toString = buffer => {
298
+ provide let toString = buffer => {
230
299
  Bytes.toString(toBytes(buffer))
231
300
  }
232
301
 
@@ -238,9 +307,14 @@ export let toString = buffer => {
238
307
  * @param buffer: The buffer to copy from
239
308
  * @returns A string made with a subset of data copied from the buffer
240
309
  *
310
+ * @example
311
+ * let buf = Buffer.make(0)
312
+ * Buffer.addString("HelloWorld", buf)
313
+ * assert Buffer.toStringSlice(0, 5, buf) == "Hello"
314
+ *
241
315
  * @since v0.4.0
242
316
  */
243
- export let toStringSlice = (start, length, buffer) => {
317
+ provide let toStringSlice = (start, length, buffer) => {
244
318
  Bytes.toString(toBytesSlice(start, length, buffer))
245
319
  }
246
320
 
@@ -250,10 +324,15 @@ export let toStringSlice = (start, length, buffer) => {
250
324
  * @param bytes: The byte sequence to append
251
325
  * @param buffer: The buffer to mutate
252
326
  *
327
+ * @example
328
+ * let buf = Buffer.make(0)
329
+ * Buffer.addBytes(b"test", buf)
330
+ * assert Buffer.toBytes(buf) == b"test"
331
+ *
253
332
  * @since v0.4.0
254
333
  */
255
334
  @unsafe
256
- export let addBytes = (bytes, buffer) => {
335
+ provide let addBytes = (bytes, buffer) => {
257
336
  let bytelen = Bytes.length(bytes)
258
337
 
259
338
  autogrow(bytelen, buffer)
@@ -273,10 +352,15 @@ export let addBytes = (bytes, buffer) => {
273
352
  * @param string: The string to append
274
353
  * @param buffer: The buffer to mutate
275
354
  *
355
+ * @example
356
+ * let buf = Buffer.make(0)
357
+ * Buffer.addString("Hello", buf)
358
+ * assert Buffer.toString(buf) == "Hello"
359
+ *
276
360
  * @since v0.4.0
277
361
  */
278
362
  @unsafe
279
- export let addString = (string, buffer) => {
363
+ provide let addString = (string, buffer) => {
280
364
  let bytelen = String.byteLength(string)
281
365
 
282
366
  autogrow(bytelen, buffer)
@@ -291,15 +375,79 @@ export let addString = (string, buffer) => {
291
375
  }
292
376
 
293
377
  /**
294
- * Appends the bytes of a char to a buffer.
378
+ * Appends the bytes of a character to a buffer.
295
379
  *
296
380
  * @param char: The character to append to the buffer
297
381
  * @param buffer: The buffer to mutate
298
382
  *
383
+ * @example
384
+ * let buf = Buffer.make(0)
385
+ * Buffer.addChar('H', buf)
386
+ * assert Buffer.toString(buf) == "H"
387
+ *
299
388
  * @since v0.4.0
300
389
  */
301
- export let addChar = (char, buffer) => {
302
- addString(Char.toString(char), buffer)
390
+ @unsafe
391
+ provide let addChar = (char, buffer) => {
392
+ use WasmI32.{ (-), (*), (&), (|), (>>>), ltU as (<), gtU as (>), leU as (<=) }
393
+ let usv = untagChar(char)
394
+
395
+ let bytelen = if (usv < 0x80n) {
396
+ autogrow(1, buffer)
397
+ use WasmI32.{ (+) }
398
+ let off = coerceNumberToWasmI32(buffer.len)
399
+ let dst = WasmI32.fromGrain(buffer.data) + _VALUE_OFFSET
400
+ WasmI32.store8(dst, usv, off)
401
+ 1
402
+ } else {
403
+ let mut count = 0n
404
+ let mut bytelen = 0
405
+ let mut offset = 0n
406
+ if (usv <= 0x07FFn) {
407
+ count = 1n
408
+ bytelen = 2
409
+ offset = 0xC0n
410
+ } else if (usv <= 0xFFFFn) {
411
+ count = 2n
412
+ bytelen = 3
413
+ offset = 0xE0n
414
+ } else {
415
+ count = 3n
416
+ bytelen = 4
417
+ offset = 0xF0n
418
+ }
419
+ use WasmI32.{ (+) }
420
+ autogrow(bytelen, buffer)
421
+ let off = coerceNumberToWasmI32(buffer.len)
422
+ let dst = WasmI32.fromGrain(buffer.data) + _VALUE_OFFSET
423
+ WasmI32.store8(dst, (usv >>> (6n * count)) + offset, off)
424
+ let mut n = 0n
425
+ while (count > 0n) {
426
+ n += 1n
427
+ let temp = usv >>> (6n * (count - 1n))
428
+ WasmI32.store8(dst + n, 0x80n | temp & 0x3Fn, off)
429
+ count -= 1n
430
+ }
431
+ bytelen
432
+ }
433
+ buffer.len += bytelen
434
+ }
435
+
436
+ /**
437
+ * Appends a character represented by a code point to a buffer.
438
+ *
439
+ * @param codePoint: The code point to append to the buffer
440
+ * @param buffer: The buffer to mutate
441
+ *
442
+ * @example
443
+ * let buf = Buffer.make(0)
444
+ * Buffer.addCharFromCodePoint(72, buf)
445
+ * assert Buffer.toString(buf) == "H"
446
+ *
447
+ * @since v0.6.0
448
+ */
449
+ provide let addCharFromCodePoint = (codePoint: Number, buffer) => {
450
+ addChar(Char.fromCode(codePoint), buffer)
303
451
  }
304
452
 
305
453
  /**
@@ -310,12 +458,17 @@ export let addChar = (char, buffer) => {
310
458
  * @param string: The string to append
311
459
  * @param buffer: The buffer to mutate
312
460
  *
461
+ * @example
462
+ * let buf = Buffer.make(0)
463
+ * Buffer.addStringSlice(0, 5, "HelloWorld", buf)
464
+ * assert Buffer.toString(buf) == "Hello"
465
+ *
313
466
  * @since v0.4.0
314
467
  * @history v0.5.0: Now takes the end offset instead of length
315
468
  */
316
469
  @unsafe
317
- export let addStringSlice = (start: Number, end, string, buffer) => {
318
- let slice = String.slice(start, end, string)
470
+ provide let addStringSlice = (start: Number, end, string, buffer) => {
471
+ let slice = String.slice(start, end=end, string)
319
472
 
320
473
  let bytelen = String.byteLength(slice)
321
474
 
@@ -343,33 +496,34 @@ export let addStringSlice = (start: Number, end, string, buffer) => {
343
496
  * @throws IndexOutOfBounds: When the `length` is negative
344
497
  * @throws IndexOutOfBounds: When the `length` is greater than the `bytes` length minus `start`
345
498
  *
499
+ * @example
500
+ * let buf = Buffer.make(0)
501
+ * Buffer.addBytesSlice(0, 5, b"HelloWorld", buf)
502
+ * assert Buffer.toString(buf) == "Hello"
503
+ *
346
504
  * @since v0.4.0
347
505
  */
348
506
  @unsafe
349
- export let addBytesSlice =
350
- (
351
- start: Number,
352
- length: Number,
353
- bytes: Bytes,
354
- buffer: Buffer,
355
- ) => {
507
+ provide let addBytesSlice = (
508
+ start: Number,
509
+ length: Number,
510
+ bytes: Bytes,
511
+ buffer: Buffer,
512
+ ) => {
356
513
  if (length != 0) {
357
- let (-) = WasmI32.sub
358
- let (<) = WasmI32.ltS
359
- let (>) = WasmI32.gtS
360
- let (>=) = WasmI32.geS
514
+ use WasmI32.{ (-), (<), (>), (>=) }
361
515
 
362
516
  // bounds check start
363
517
  let bytelen = WasmI32.load(WasmI32.fromGrain(bytes), 4n)
364
518
  let srcOff = coerceNumberToWasmI32(start)
365
519
  if (srcOff < 0n || srcOff >= bytelen) {
366
- throw Exception.IndexOutOfBounds
520
+ throw IndexOutOfBounds
367
521
  }
368
522
 
369
523
  // bounds check length
370
524
  let len = coerceNumberToWasmI32(length)
371
525
  if (len < 0n || len > bytelen - srcOff) {
372
- throw Exception.IndexOutOfBounds
526
+ throw IndexOutOfBounds
373
527
  }
374
528
 
375
529
  autogrow(length, buffer)
@@ -391,9 +545,17 @@ export let addBytesSlice =
391
545
  * @param srcBuffer: The buffer to append
392
546
  * @param dstBuffer: The buffer to mutate
393
547
  *
548
+ * @example
549
+ * let buf1 = Buffer.make(0)
550
+ * Buffer.addString("Hello", buf1)
551
+ * let buf2 = Buffer.make(0)
552
+ * Buffer.addString("World", buf2)
553
+ * Buffer.addBuffer(buf2, buf1)
554
+ * assert Buffer.toString(buf1) == "HelloWorld"
555
+ *
394
556
  * @since v0.4.0
395
557
  */
396
- export let addBuffer = (srcBuffer, dstBuffer) => {
558
+ provide let addBuffer = (srcBuffer, dstBuffer) => {
397
559
  addBytesSlice(0, srcBuffer.len, srcBuffer.data, dstBuffer)
398
560
  }
399
561
 
@@ -407,32 +569,85 @@ export let addBuffer = (srcBuffer, dstBuffer) => {
407
569
  * @param srcBuffer: The buffer to append
408
570
  * @param dstBuffer: The buffer to mutate
409
571
  *
572
+ * @example
573
+ * let buf1 = Buffer.make(0)
574
+ * Buffer.addString("Hello", buf1)
575
+ * let buf2 = Buffer.make(0)
576
+ * Buffer.addString("HiWorld", buf2)
577
+ * Buffer.addBufferSlice(2, 5, buf2, buf1)
578
+ * assert Buffer.toString(buf1) == "HelloWorld"
579
+ *
410
580
  * @since v0.4.0
411
581
  */
412
- export let addBufferSlice = (start, length, srcBuffer, dstBuffer) => {
582
+ provide let addBufferSlice = (start, length, srcBuffer, dstBuffer) => {
413
583
  addBytesSlice(start, length, srcBuffer.data, dstBuffer)
414
584
  }
415
585
 
416
- /**
417
- * @section Binary operations on integers: Functions for encoding and decoding integers stored in a buffer.
418
- */
419
-
420
586
  /**
421
587
  * Gets a signed 8-bit integer starting at the given byte index.
422
588
  *
423
589
  * @param index: The byte index to access
424
590
  * @param buffer: The buffer to access
425
- * @returns A 32-bit integer representing a signed 8-bit integer that starts at the given index
591
+ * @returns A signed 8-bit integer that starts at the given index
592
+ *
593
+ * @throws IndexOutOfBounds: When `index` is negative
594
+ * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
595
+ * @throws IndexOutOfBounds: When `index + 1` is greater than the buffer size
596
+ *
597
+ * @example
598
+ * let buf = Buffer.make(0)
599
+ * Buffer.addInt8(1s, buf)
600
+ * assert Buffer.getInt8(0, buf) == 1s
601
+ *
602
+ * @since v0.6.0
603
+ * @history v0.4.0: Originally called `getInt8S`, returning an `Int32`
604
+ */
605
+ provide let getInt8 = (index, buffer) => {
606
+ checkIsIndexInBounds(index, _8BIT_LEN, buffer)
607
+ Bytes.getInt8(index, buffer.data)
608
+ }
609
+
610
+ /**
611
+ * Sets a signed 8-bit integer starting at the given byte index.
612
+ *
613
+ * @param index: The byte index to update
614
+ * @param value: The value to set
615
+ * @param buffer: The buffer to mutate
426
616
  *
427
617
  * @throws IndexOutOfBounds: When `index` is negative
428
618
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
429
619
  * @throws IndexOutOfBounds: When `index + 1` is greater than the buffer size
430
620
  *
621
+ * @example
622
+ * let buf = Buffer.make(32)
623
+ * Buffer.addString("Hello World", buf)
624
+ * Buffer.setInt8(0, 3s, buf)
625
+ * assert Buffer.getInt8(0, buf) == 3s
626
+ *
431
627
  * @since v0.4.0
628
+ * @history v0.6.0: `value` argument type changed to `Int8`
432
629
  */
433
- export let getInt8S = (index, buffer) => {
630
+ provide let setInt8 = (index, value, buffer) => {
434
631
  checkIsIndexInBounds(index, _8BIT_LEN, buffer)
435
- Bytes.getInt8S(index, buffer.data)
632
+ Bytes.setInt8(index, value, buffer.data)
633
+ }
634
+
635
+ /**
636
+ * Appends a signed 8-bit integer to a buffer.
637
+ *
638
+ * @param value: The value to append
639
+ * @param buffer: The buffer to mutate
640
+ *
641
+ * @example
642
+ * let buf = Buffer.make(0)
643
+ * Buffer.addInt8(2s, buf)
644
+ * assert Buffer.getInt8(0, buf) == 2s
645
+ *
646
+ * @since v0.4.0
647
+ * @history v0.6.0: `value` argument type changed to `Int8`
648
+ */
649
+ provide let addInt8 = (value, buffer) => {
650
+ addInt8help(value, buffer)
436
651
  }
437
652
 
438
653
  /**
@@ -440,21 +655,27 @@ export let getInt8S = (index, buffer) => {
440
655
  *
441
656
  * @param index: The byte index to access
442
657
  * @param buffer: The buffer to access
443
- * @returns A 32-bit integer representing an unsigned 8-bit integer that starts at the given index
658
+ * @returns An unsigned 8-bit integer that starts at the given index
444
659
  *
445
660
  * @throws IndexOutOfBounds: When `index` is negative
446
661
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
447
662
  * @throws IndexOutOfBounds: When `index + 1` is greater than the buffer size
448
663
  *
449
- * @since v0.4.0
664
+ * @example
665
+ * let buf = Buffer.make(32)
666
+ * Buffer.addUint8(3us, buf)
667
+ * assert Buffer.getUint8(0, buf) == 3us
668
+ *
669
+ * @since v0.6.0
670
+ * @history v0.4.0: Originally called `getInt8U`, returning an `Int32`
450
671
  */
451
- export let getInt8U = (index, buffer) => {
672
+ provide let getUint8 = (index, buffer) => {
452
673
  checkIsIndexInBounds(index, _8BIT_LEN, buffer)
453
- Bytes.getInt8U(index, buffer.data)
674
+ Bytes.getUint8(index, buffer.data)
454
675
  }
455
676
 
456
677
  /**
457
- * Sets a signed 8-bit integer starting at the given byte index.
678
+ * Sets an unsigned 8-bit integer starting at the given byte index.
458
679
  *
459
680
  * @param index: The byte index to update
460
681
  * @param value: The value to set
@@ -464,23 +685,34 @@ export let getInt8U = (index, buffer) => {
464
685
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
465
686
  * @throws IndexOutOfBounds: When `index + 1` is greater than the buffer size
466
687
  *
467
- * @since v0.4.0
688
+ * @example
689
+ * let buf = Buffer.make(32)
690
+ * Buffer.addString("Hello World", buf)
691
+ * Buffer.setUint8(4us, buf)
692
+ * assert Buffer.getUint8(0, buf) == 4us
693
+ *
694
+ * @since v0.6.0
468
695
  */
469
- export let setInt8 = (index, value, buffer) => {
696
+ provide let setUint8 = (index, value, buffer) => {
470
697
  checkIsIndexInBounds(index, _8BIT_LEN, buffer)
471
- Bytes.setInt8(index, value, buffer.data)
698
+ Bytes.setUint8(index, value, buffer.data)
472
699
  }
473
700
 
474
701
  /**
475
- * Appends a signed 8-bit integer to a buffer.
702
+ * Appends an unsigned 8-bit integer to a buffer.
476
703
  *
477
704
  * @param value: The value to append
478
705
  * @param buffer: The buffer to mutate
479
706
  *
480
- * @since v0.4.0
707
+ * @example
708
+ * let buf = Buffer.make(32)
709
+ * Buffer.addUint8(0us, buf)
710
+ * assert Buffer.getUint8(0, buf) == 0us
711
+ *
712
+ * @since v0.6.0
481
713
  */
482
- export let addInt8 = (value, buffer) => {
483
- addInt8help(value, buffer)
714
+ provide let addUint8 = (value, buffer) => {
715
+ addUint8help(value, buffer)
484
716
  }
485
717
 
486
718
  /**
@@ -488,17 +720,66 @@ export let addInt8 = (value, buffer) => {
488
720
  *
489
721
  * @param index: The byte index to access
490
722
  * @param buffer: The buffer to access
491
- * @returns A 32-bit integer representing a signed 16-bit integer that starts at the given index
723
+ * @returns A signed 16-bit integer that starts at the given index
724
+ *
725
+ * @throws IndexOutOfBounds: When `index` is negative
726
+ * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
727
+ * @throws IndexOutOfBounds: When `index + 2` is greater than the buffer size
728
+ *
729
+ * @example
730
+ * let buf = Buffer.make(32)
731
+ * Buffer.addInt16(1S, buf)
732
+ * assert Buffer.getInt16(0, buf) == 1S
733
+ *
734
+ * @since v0.6.0
735
+ * @history v0.4.0: Originally called `getInt16S`, returning an `Int32`
736
+ */
737
+ provide let getInt16 = (index, buffer) => {
738
+ checkIsIndexInBounds(index, _16BIT_LEN, buffer)
739
+ Bytes.getInt16(index, buffer.data)
740
+ }
741
+
742
+ /**
743
+ * Sets a signed 16-bit integer starting at the given byte index.
744
+ *
745
+ * @param index: The byte index to update
746
+ * @param value: The value to set
747
+ * @param buffer: The buffer to mutate
492
748
  *
493
749
  * @throws IndexOutOfBounds: When `index` is negative
494
750
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
495
751
  * @throws IndexOutOfBounds: When `index + 2` is greater than the buffer size
496
752
  *
753
+ * @example
754
+ * let buf = Buffer.make(32)
755
+ * Buffer.addString("Hello World", buf)
756
+ * Buffer.setInt16(5, 1S, buf)
757
+ * assert Buffer.getInt16(5, buf) == 1S
758
+ *
497
759
  * @since v0.4.0
760
+ * @history v0.6.0: `value` argument type changed to `Int16`
498
761
  */
499
- export let getInt16S = (index, buffer) => {
762
+ provide let setInt16 = (index, value, buffer) => {
500
763
  checkIsIndexInBounds(index, _16BIT_LEN, buffer)
501
- Bytes.getInt16S(index, buffer.data)
764
+ Bytes.setInt16(index, value, buffer.data)
765
+ }
766
+
767
+ /**
768
+ * Appends a signed 16-bit integer to a buffer.
769
+ *
770
+ * @param value: The value to append
771
+ * @param buffer: The buffer to mutate
772
+ *
773
+ * @example
774
+ * let buf = Buffer.make(0)
775
+ * Buffer.addInt16(2S, buf)
776
+ * assert Buffer.getInt16(0, buf) == 2S
777
+ *
778
+ * @since v0.4.0
779
+ * @history v0.6.0: `value` argument type changed to `Int16`
780
+ */
781
+ provide let addInt16 = (value, buffer) => {
782
+ addInt16help(value, buffer)
502
783
  }
503
784
 
504
785
  /**
@@ -506,21 +787,27 @@ export let getInt16S = (index, buffer) => {
506
787
  *
507
788
  * @param index: The byte index to access
508
789
  * @param buffer: The buffer to access
509
- * @returns A 32-bit integer representing an unsigned 16-bit integer that starts at the given index
790
+ * @returns An unsigned 16-bit integer that starts at the given index
510
791
  *
511
792
  * @throws IndexOutOfBounds: When `index` is negative
512
793
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
513
794
  * @throws IndexOutOfBounds: When `index + 2` is greater than the buffer size
514
795
  *
515
- * @since v0.4.0
796
+ * @example
797
+ * let buf = Buffer.make(0)
798
+ * Buffer.addUint16(1uS, buf)
799
+ * assert Buffer.getUint16(0, buf) == 1uS
800
+ *
801
+ * @since v0.6.0
802
+ * @history v0.4.0: Originally called `getInt16U`, returning an `Int32`
516
803
  */
517
- export let getInt16U = (index, buffer) => {
804
+ provide let getUint16 = (index, buffer) => {
518
805
  checkIsIndexInBounds(index, _16BIT_LEN, buffer)
519
- Bytes.getInt16U(index, buffer.data)
806
+ Bytes.getUint16(index, buffer.data)
520
807
  }
521
808
 
522
809
  /**
523
- * Sets a signed 16-bit integer starting at the given byte index.
810
+ * Sets an unsigned 16-bit integer starting at the given byte index.
524
811
  *
525
812
  * @param index: The byte index to update
526
813
  * @param value: The value to set
@@ -530,23 +817,34 @@ export let getInt16U = (index, buffer) => {
530
817
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
531
818
  * @throws IndexOutOfBounds: When `index + 2` is greater than the buffer size
532
819
  *
533
- * @since v0.4.0
820
+ * @example
821
+ * let buf = Buffer.make(32)
822
+ * Buffer.addString("Hello World", buf)
823
+ * Buffer.setUint16(0, 1uS, buf)
824
+ * assert Buffer.getUint16(0, buf) == 1uS
825
+ *
826
+ * @since v0.6.0
534
827
  */
535
- export let setInt16 = (index, value, buffer) => {
828
+ provide let setUint16 = (index, value, buffer) => {
536
829
  checkIsIndexInBounds(index, _16BIT_LEN, buffer)
537
- Bytes.setInt16(index, value, buffer.data)
830
+ Bytes.setUint16(index, value, buffer.data)
538
831
  }
539
832
 
540
833
  /**
541
- * Appends a signed 16-bit integer to a buffer.
834
+ * Appends an unsigned 16-bit integer to a buffer.
542
835
  *
543
836
  * @param value: The value to append
544
837
  * @param buffer: The buffer to mutate
545
838
  *
546
- * @since v0.4.0
839
+ * @example
840
+ * let buf = Buffer.make(0)
841
+ * Buffer.addUint16(0, 2uS, buf)
842
+ * assert Buffer.getUint16(0, buf) == 2uS
843
+ *
844
+ * @since v0.6.0
547
845
  */
548
- export let addInt16 = (value, buffer) => {
549
- addInt16help(value, buffer)
846
+ provide let addUint16 = (value, buffer) => {
847
+ addUint16help(value, buffer)
550
848
  }
551
849
 
552
850
  /**
@@ -560,9 +858,14 @@ export let addInt16 = (value, buffer) => {
560
858
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
561
859
  * @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
562
860
  *
861
+ * @example
862
+ * let buf = Buffer.make(0)
863
+ * Buffer.addInt32(1l, buf)
864
+ * assert Buffer.getInt32(0, buf) == 1l
865
+ *
563
866
  * @since v0.4.0
564
867
  */
565
- export let getInt32 = (index, buffer) => {
868
+ provide let getInt32 = (index, buffer) => {
566
869
  checkIsIndexInBounds(index, _32BIT_LEN, buffer)
567
870
  Bytes.getInt32(index, buffer.data)
568
871
  }
@@ -578,9 +881,15 @@ export let getInt32 = (index, buffer) => {
578
881
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
579
882
  * @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
580
883
  *
884
+ * @example
885
+ * let buf = Buffer.make(64)
886
+ * Buffer.addString("Hello World", buf)
887
+ * Buffer.setInt32(3, 1l, buf)
888
+ * assert Buffer.getInt32(3, buf) == 1l
889
+ *
581
890
  * @since v0.4.0
582
891
  */
583
- export let setInt32 = (index, value, buffer) => {
892
+ provide let setInt32 = (index, value, buffer) => {
584
893
  checkIsIndexInBounds(index, _32BIT_LEN, buffer)
585
894
  Bytes.setInt32(index, value, buffer.data)
586
895
  }
@@ -591,12 +900,81 @@ export let setInt32 = (index, value, buffer) => {
591
900
  * @param value: The value to append
592
901
  * @param buffer: The buffer to mutate
593
902
  *
903
+ * @example
904
+ * let buf = Buffer.make(64)
905
+ * Buffer.addInt32(1l, buf)
906
+ * assert Buffer.getInt32(0, buf) == 1l
907
+ *
594
908
  * @since v0.4.0
595
909
  */
596
- export let addInt32 = (value, buffer) => {
910
+ provide let addInt32 = (value, buffer) => {
597
911
  addInt32help(value, buffer)
598
912
  }
599
913
 
914
+ /**
915
+ * Gets an unsigned 32-bit integer starting at the given byte index.
916
+ *
917
+ * @param index: The byte index to access
918
+ * @param buffer: The buffer to access
919
+ * @returns An unsigned 32-bit integer that starts at the given index
920
+ *
921
+ * @throws IndexOutOfBounds: When `index` is negative
922
+ * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
923
+ * @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
924
+ *
925
+ * @example
926
+ * let buf = Buffer.make(32)
927
+ * Buffer.addUint32(1ul, buf)
928
+ * assert Buffer.getUint32(0, buf) == 1ul
929
+ *
930
+ * @since v0.6.0
931
+ */
932
+ provide let getUint32 = (index, buffer) => {
933
+ checkIsIndexInBounds(index, _32BIT_LEN, buffer)
934
+ Bytes.getUint32(index, buffer.data)
935
+ }
936
+
937
+ /**
938
+ * Sets an unsigned 32-bit integer starting at the given byte index.
939
+ *
940
+ * @param index: The byte index to update
941
+ * @param value: The value to set
942
+ * @param buffer: The buffer to mutate
943
+ *
944
+ * @throws IndexOutOfBounds: When `index` is negative
945
+ * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
946
+ * @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
947
+ *
948
+ * @example
949
+ * let buf = Buffer.make(32)
950
+ * Buffer.addString("Hello World", buf)
951
+ * Buffer.setUint32(0, 1ul, buf)
952
+ * assert Buffer.getUint32(0, buf) == 1ul
953
+ *
954
+ * @since v0.6.0
955
+ */
956
+ provide let setUint32 = (index, value, buffer) => {
957
+ checkIsIndexInBounds(index, _32BIT_LEN, buffer)
958
+ Bytes.setUint32(index, value, buffer.data)
959
+ }
960
+
961
+ /**
962
+ * Appends an unsigned 32-bit integer to a buffer.
963
+ *
964
+ * @param value: The value to append
965
+ * @param buffer: The buffer to mutate
966
+ *
967
+ * @example
968
+ * let buf = Buffer.make(32)
969
+ * Buffer.addUint32(1ul, buf)
970
+ * assert Buffer.getUint32(0, buf) == 1ul
971
+ *
972
+ * @since v0.6.0
973
+ */
974
+ provide let addUint32 = (value, buffer) => {
975
+ addUint32help(value, buffer)
976
+ }
977
+
600
978
  /**
601
979
  * Gets a 32-bit float starting at the given byte index.
602
980
  *
@@ -608,9 +986,14 @@ export let addInt32 = (value, buffer) => {
608
986
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
609
987
  * @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
610
988
  *
989
+ * @example
990
+ * let buf = Buffer.make(32)
991
+ * Buffer.addFloat32(1.0f, buf)
992
+ * assert Buffer.getFloat32(0, buf) == 1.0f
993
+ *
611
994
  * @since v0.4.0
612
995
  */
613
- export let getFloat32 = (index, buffer) => {
996
+ provide let getFloat32 = (index, buffer) => {
614
997
  checkIsIndexInBounds(index, _32BIT_LEN, buffer)
615
998
  Bytes.getFloat32(index, buffer.data)
616
999
  }
@@ -626,9 +1009,15 @@ export let getFloat32 = (index, buffer) => {
626
1009
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
627
1010
  * @throws IndexOutOfBounds: When `index + 4` is greater than the buffer size
628
1011
  *
1012
+ * @example
1013
+ * let buf = Buffer.make(32)
1014
+ * Buffer.addString("Hello World", buf)
1015
+ * Buffer.setFloat32(0, 1.0f, buf)
1016
+ * assert Buffer.getFloat32(0, buf) == 1.0f
1017
+ *
629
1018
  * @since v0.4.0
630
1019
  */
631
- export let setFloat32 = (index, value, buffer) => {
1020
+ provide let setFloat32 = (index, value, buffer) => {
632
1021
  checkIsIndexInBounds(index, _32BIT_LEN, buffer)
633
1022
  Bytes.setFloat32(index, value, buffer.data)
634
1023
  }
@@ -639,9 +1028,14 @@ export let setFloat32 = (index, value, buffer) => {
639
1028
  * @param value: The value to append
640
1029
  * @param buffer: The buffer to mutate
641
1030
  *
1031
+ * @example
1032
+ * let buf = Buffer.make(32)
1033
+ * Buffer.addFloat32(1.0f, buf)
1034
+ * assert Buffer.getFloat32(0, buf) == 1.0f
1035
+ *
642
1036
  * @since v0.4.0
643
1037
  */
644
- export let addFloat32 = (value, buffer) => {
1038
+ provide let addFloat32 = (value, buffer) => {
645
1039
  autogrow(_32BIT_LEN, buffer)
646
1040
  let index = buffer.len
647
1041
  buffer.len += _32BIT_LEN
@@ -659,9 +1053,14 @@ export let addFloat32 = (value, buffer) => {
659
1053
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
660
1054
  * @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
661
1055
  *
1056
+ * @example
1057
+ * let buf = Buffer.make(32)
1058
+ * Buffer.addInt64(1L, buf)
1059
+ * assert Buffer.getInt64(0, buf) == 1L
1060
+ *
662
1061
  * @since v0.4.0
663
1062
  */
664
- export let getInt64 = (index, buffer) => {
1063
+ provide let getInt64 = (index, buffer) => {
665
1064
  checkIsIndexInBounds(index, _64BIT_LEN, buffer)
666
1065
  Bytes.getInt64(index, buffer.data)
667
1066
  }
@@ -677,9 +1076,15 @@ export let getInt64 = (index, buffer) => {
677
1076
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
678
1077
  * @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
679
1078
  *
1079
+ * @example
1080
+ * let buf = Buffer.make(32)
1081
+ * Buffer.addString("Hello World", buf)
1082
+ * Buffer.setInt64(0, 1L, buf)
1083
+ * assert Buffer.getInt64(0, buf) == 1L
1084
+ *
680
1085
  * @since v0.4.0
681
1086
  */
682
- export let setInt64 = (index, value, buffer) => {
1087
+ provide let setInt64 = (index, value, buffer) => {
683
1088
  checkIsIndexInBounds(index, _64BIT_LEN, buffer)
684
1089
  Bytes.setInt64(index, value, buffer.data)
685
1090
  }
@@ -690,15 +1095,87 @@ export let setInt64 = (index, value, buffer) => {
690
1095
  * @param value: The value to set
691
1096
  * @param buffer: The buffer to mutate
692
1097
  *
1098
+ * @example
1099
+ * let buf = Buffer.make(32)
1100
+ * Buffer.addInt64(1L, buf)
1101
+ * assert Buffer.getInt64(0, buf) == 1L
1102
+ *
693
1103
  * @since v0.4.0
694
1104
  */
695
- export let addInt64 = (value, buffer) => {
1105
+ provide let addInt64 = (value, buffer) => {
696
1106
  autogrow(_64BIT_LEN, buffer)
697
1107
  let index = buffer.len
698
1108
  buffer.len += _64BIT_LEN
699
1109
  setInt64(index, value, buffer)
700
1110
  }
701
1111
 
1112
+ /**
1113
+ * Gets an unsigned 64-bit integer starting at the given byte index.
1114
+ *
1115
+ * @param index: The byte index to access
1116
+ * @param buffer: The buffer to access
1117
+ * @returns An unsigned 64-bit integer that starts at the given index
1118
+ *
1119
+ * @throws IndexOutOfBounds: When `index` is negative
1120
+ * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
1121
+ * @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
1122
+ *
1123
+ * @example
1124
+ * let buf = Buffer.make(32)
1125
+ * Buffer.addUint64(1uL, buf)
1126
+ * assert Buffer.getUint64(0, buf) == 1uL
1127
+ *
1128
+ * @since v0.6.0
1129
+ */
1130
+ provide let getUint64 = (index, buffer) => {
1131
+ checkIsIndexInBounds(index, _64BIT_LEN, buffer)
1132
+ Bytes.getUint64(index, buffer.data)
1133
+ }
1134
+
1135
+ /**
1136
+ * Sets an unsigned 64-bit integer starting at the given byte index.
1137
+ *
1138
+ * @param index: The byte index to update
1139
+ * @param value: The value to set
1140
+ * @param buffer: The buffer to mutate
1141
+ *
1142
+ * @throws IndexOutOfBounds: When `index` is negative
1143
+ * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
1144
+ * @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
1145
+ *
1146
+ * @example
1147
+ * let buf = Buffer.make(32)
1148
+ * Buffer.addString("Hello World", buf)
1149
+ * Buffer.setUint64(0, 1uL, buf)
1150
+ * assert Buffer.getUint64(0, buf) == 1uL
1151
+ *
1152
+ * @since v0.6.0
1153
+ */
1154
+ provide let setUint64 = (index, value, buffer) => {
1155
+ checkIsIndexInBounds(index, _64BIT_LEN, buffer)
1156
+ Bytes.setUint64(index, value, buffer.data)
1157
+ }
1158
+
1159
+ /**
1160
+ * Appends an unsigned 64-bit integer to a buffer.
1161
+ *
1162
+ * @param value: The value to set
1163
+ * @param buffer: The buffer to mutate
1164
+ *
1165
+ * @example
1166
+ * let buf = Buffer.make(32)
1167
+ * Buffer.addUint64(1uL, buf)
1168
+ * assert Buffer.getUint64(0, buf) == 1uL
1169
+ *
1170
+ * @since v0.6.0
1171
+ */
1172
+ provide let addUint64 = (value, buffer) => {
1173
+ autogrow(_64BIT_LEN, buffer)
1174
+ let index = buffer.len
1175
+ buffer.len += _64BIT_LEN
1176
+ setUint64(index, value, buffer)
1177
+ }
1178
+
702
1179
  /**
703
1180
  * Gets a 64-bit float starting at the given byte index.
704
1181
  *
@@ -710,9 +1187,14 @@ export let addInt64 = (value, buffer) => {
710
1187
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
711
1188
  * @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
712
1189
  *
1190
+ * @example
1191
+ * let buf = Buffer.make(32)
1192
+ * Buffer.addFloat64(1.0F, buf)
1193
+ * assert Buffer.getFloat64(0, buf) == 1.0F
1194
+ *
713
1195
  * @since v0.4.0
714
1196
  */
715
- export let getFloat64 = (index, buffer) => {
1197
+ provide let getFloat64 = (index, buffer) => {
716
1198
  checkIsIndexInBounds(index, _64BIT_LEN, buffer)
717
1199
  Bytes.getFloat64(index, buffer.data)
718
1200
  }
@@ -728,9 +1210,15 @@ export let getFloat64 = (index, buffer) => {
728
1210
  * @throws IndexOutOfBounds: When `index` is greater than or equal to the buffer size
729
1211
  * @throws IndexOutOfBounds: When `index + 8` is greater than the buffer size
730
1212
  *
1213
+ * @example
1214
+ * let buf = Buffer.make(32)
1215
+ * Buffer.addString("Hello World", buf)
1216
+ * Buffer.setFloat64(0, 1.0F, buf)
1217
+ * assert Buffer.getFloat64(0, buf) == 1.0F
1218
+ *
731
1219
  * @since v0.4.0
732
1220
  */
733
- export let setFloat64 = (index, value, buffer) => {
1221
+ provide let setFloat64 = (index, value, buffer) => {
734
1222
  checkIsIndexInBounds(index, _64BIT_LEN, buffer)
735
1223
  Bytes.setFloat64(index, value, buffer.data)
736
1224
  }
@@ -741,9 +1229,14 @@ export let setFloat64 = (index, value, buffer) => {
741
1229
  * @param value: The value to append
742
1230
  * @param buffer: The buffer to mutate
743
1231
  *
1232
+ * @example
1233
+ * let buf = Buffer.make(32)
1234
+ * Buffer.addFloat64(1.0F, buf)
1235
+ * assert Buffer.getFloat64(0, buf) == 1.0F
1236
+ *
744
1237
  * @since v0.4.0
745
1238
  */
746
- export let addFloat64 = (value, buffer) => {
1239
+ provide let addFloat64 = (value, buffer) => {
747
1240
  autogrow(_64BIT_LEN, buffer)
748
1241
  let index = buffer.len
749
1242
  buffer.len += _64BIT_LEN