@grain/stdlib 0.4.0 → 0.4.4

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/buffer.gr CHANGED
@@ -16,24 +16,20 @@ import Bytes from "bytes"
16
16
  import String from "string"
17
17
  import { coerceNumberToWasmI32 } from "runtime/numbers"
18
18
 
19
- record Buffer {
20
- mut len: Number,
21
- initialSize: Number,
22
- mut data: Bytes,
23
- }
19
+ record Buffer { mut len: Number, initialSize: Number, mut data: Bytes }
24
20
 
25
21
  @disableGC
26
- let mut _SIZE_OFFSET = 1n;
22
+ let mut _SIZE_OFFSET = 1n
27
23
 
28
24
  @disableGC
29
- let mut _VALUE_OFFSET = 1n;
25
+ let mut _VALUE_OFFSET = 1n
30
26
 
31
27
  @disableGC
32
28
  let initOffsets = () => {
33
- _SIZE_OFFSET = 4n;
34
- _VALUE_OFFSET = 8n;
29
+ _SIZE_OFFSET = 4n
30
+ _VALUE_OFFSET = 8n
35
31
  }
36
- initOffsets();
32
+ initOffsets()
37
33
 
38
34
  let _8BIT_LEN = 1
39
35
 
@@ -45,27 +41,28 @@ let _64BIT_LEN = 8
45
41
 
46
42
  /* Gets the size of a Bytes via its ptr */
47
43
  @disableGC
48
- let getSize = (ptr) => WasmI32.load(ptr, _SIZE_OFFSET)
44
+ let getSize = ptr => WasmI32.load(ptr, _SIZE_OFFSET)
49
45
 
50
46
  /* Doubles the size of buffer's underlying byte sequence, if the given size is larger than the size of a buffer's underlying byte sequence */
51
47
  let autogrow = (len, buf) => {
52
48
  while (buf.len + len > Bytes.length(buf.data)) {
53
49
  let mut n = Bytes.length(buf.data)
54
- if (n == 0) n = 4 // Make sure bytes of 0 length grow too
50
+ if (n == 0) n = 4
51
+ // Make sure bytes of 0 length grow too
55
52
  buf.data = Bytes.resize(0, n, buf.data)
56
53
  }
57
54
  }
58
55
 
59
56
  /* Gets the pointer for a char's bytes following the char's tag */
60
57
  @disableGC
61
- let rec getCharAsWasmI32 = (char) => {
58
+ let rec getCharAsWasmI32 = char => {
62
59
  let c = WasmI32.fromGrain(char)
63
60
  WasmI32.load(c, 4n)
64
61
  }
65
62
 
66
63
  /* Gets the UTF-8 byte length of a char */
67
64
  @disableGC
68
- let rec getCharByteLength = (byte) => {
65
+ let rec getCharByteLength = byte => {
69
66
  let (+) = WasmI32.add
70
67
  let (&) = WasmI32.and
71
68
  let (==) = WasmI32.eq
@@ -96,269 +93,505 @@ let checkIsIndexInBounds = (i, len, buf) => {
96
93
  if (i >= buf.len || i + len > buf.len) throw Exception.IndexOutOfBounds
97
94
  }
98
95
 
96
+ let addInt8help = (value, buffer) => {
97
+ autogrow(_8BIT_LEN, buffer)
98
+ let index = buffer.len
99
+ buffer.len = buffer.len + _8BIT_LEN
100
+ Bytes.setInt8(index, value, buffer.data)
101
+ }
102
+
103
+ let addInt16help = (value, buffer) => {
104
+ autogrow(_16BIT_LEN, buffer)
105
+ let index = buffer.len
106
+ buffer.len = buffer.len + _16BIT_LEN
107
+ Bytes.setInt16(index, value, buffer.data)
108
+ }
109
+
110
+ let addInt32help = (value, buffer) => {
111
+ autogrow(_32BIT_LEN, buffer)
112
+ let index = buffer.len
113
+ buffer.len = buffer.len + _32BIT_LEN
114
+ Bytes.setInt32(index, value, buffer.data)
115
+ }
116
+
99
117
  /**
100
118
  * @section Values: Functions for working with the Buffer data type.
101
119
  */
102
120
 
103
121
  /**
104
- * Gets a signed 8-bit integer starting at the given byte index.
122
+ * Creates a fresh buffer, initially empty.
105
123
  *
106
- * @param index: The byte index to access
107
- * @param buffer: The buffer to access
108
- * @returns A 32-bit integer representing a signed 8-bit integer that starts at the given index
124
+ * The `initialSize` parameter is the initial size of the internal byte sequence that holds the buffer contents.
125
+ * That byte sequence is automatically reallocated when more than `initialSize` bytes are stored in the buffer, but shrinks back to `initialSize` characters when reset is called.
126
+ *
127
+ * @param initialSize: The initial size of the buffer
128
+ * @returns The new buffer
109
129
  *
110
130
  * @since v0.4.0
111
131
  */
112
- export let getInt8S = (index, buffer) => {
113
- checkIsIndexInBounds(index, _8BIT_LEN, buffer)
114
- Bytes.getInt8S(index, buffer.data)
132
+ export let make = initialSize => {
133
+ if (initialSize < 0) throw Exception.InvalidArgument(
134
+ "Buffers size must be >= 0",
135
+ )
136
+ { len: 0, initialSize, data: Bytes.make(initialSize) }
115
137
  }
116
138
 
117
139
  /**
118
- * Gets an unsigned 8-bit integer starting at the given byte index.
140
+ * Gets the number of bytes currently contained in a buffer.
119
141
  *
120
- * @param index: The byte index to access
121
142
  * @param buffer: The buffer to access
122
- * @returns A 32-bit integer representing an unsigned 8-bit integer that starts at the given index
143
+ * @returns The length of the buffer in bytes
123
144
  *
124
145
  * @since v0.4.0
125
146
  */
126
- export let getInt8U = (index, buffer) => {
127
- checkIsIndexInBounds(index, _8BIT_LEN, buffer)
128
- Bytes.getInt8U(index, buffer.data)
129
- }
147
+ export let length = buffer => buffer.len
130
148
 
131
149
  /**
132
- * Sets a signed 8-bit integer starting at the given byte index.
150
+ * Clears data in the buffer and sets its length to zero.
133
151
  *
134
- * @param index: The byte index to update
135
- * @param value: The value to set
136
- * @param buffer: The buffer to mutate
152
+ * This operation does not resize the underlying byte sequence.
153
+ *
154
+ * @param buffer: The buffer to clear
137
155
  *
138
156
  * @since v0.4.0
139
157
  */
140
- export let setInt8 = (index, value, buffer) => {
141
- checkIsIndexInBounds(index, _8BIT_LEN, buffer)
142
- Bytes.setInt8(index, value, buffer.data)
158
+ export let clear = buffer => {
159
+ Bytes.fill(0x0l, buffer.data)
160
+ buffer.len = 0
143
161
  }
144
162
 
145
163
  /**
146
- * Appends a signed 8-bit integer to a buffer.
164
+ * Empty a buffer and deallocate the internal byte sequence holding the buffer contents.
147
165
  *
148
- * @param value: The value to append
149
- * @param buffer: The buffer to mutate
166
+ * This operation resizes the underlying byte sequence to the initial size of the buffer.
167
+ *
168
+ * @param buffer: The buffer to reset
150
169
  *
151
170
  * @since v0.4.0
152
171
  */
153
- export let addInt8 = (value, buffer) => {
154
- autogrow(_8BIT_LEN, buffer)
155
- let index = buffer.len
156
- buffer.len = buffer.len + _8BIT_LEN
157
- setInt8(index, value, buffer)
172
+ export let reset = buffer => {
173
+ buffer.data = Bytes.make(buffer.initialSize)
174
+ buffer.len = 0
158
175
  }
159
176
 
160
177
  /**
161
- * Gets a signed 16-bit integer starting at the given byte index.
178
+ * Shortens a buffer to the given length.
162
179
  *
163
- * @param index: The byte index to access
164
- * @param buffer: The buffer to access
165
- * @returns A 32-bit integer representing a signed 16-bit integer that starts at the given index
180
+ * This operation does not resize the underlying byte sequence.
181
+ *
182
+ * @param length: The number of bytes to truncate the buffer to
183
+ * @param buffer: The buffer to truncate
166
184
  *
167
185
  * @since v0.4.0
168
186
  */
169
- export let getInt16S = (index, buffer) => {
170
- checkIsIndexInBounds(index, _16BIT_LEN, buffer)
171
- Bytes.getInt16S(index, buffer.data)
187
+ @disableGC
188
+ export let rec truncate = (length, buffer) => {
189
+ Memory.incRef(WasmI32.fromGrain((<)))
190
+ Memory.incRef(WasmI32.fromGrain((>)))
191
+ if (length < 0 || length > buffer.len) throw Exception.IndexOutOfBounds
192
+
193
+ let (+) = WasmI32.add
194
+ let src = WasmI32.fromGrain(buffer.data)
195
+ let size = getSize(src)
196
+ let off = coerceNumberToWasmI32(length)
197
+ let ret = Memory.fill(src + _VALUE_OFFSET + off, 0n, size)
198
+ buffer.len = length
199
+
200
+ Memory.decRef(WasmI32.fromGrain(length))
201
+ Memory.decRef(WasmI32.fromGrain(buffer))
202
+ Memory.decRef(WasmI32.fromGrain(truncate))
203
+ void
172
204
  }
173
205
 
174
206
  /**
175
- * Gets an unsigned 16-bit integer starting at the given byte index.
207
+ * Returns a copy of the current contents of the buffer as a byte sequence.
176
208
  *
177
- * @param index: The byte index to access
178
- * @param buffer: The buffer to access
179
- * @returns A 32-bit integer representing an unsigned 16-bit integer that starts at the given index
209
+ * @param buffer: The buffer to copy into a byte sequence
210
+ * @returns A byte sequence made from copied buffer data
180
211
  *
181
212
  * @since v0.4.0
182
213
  */
183
- export let getInt16U = (index, buffer) => {
184
- checkIsIndexInBounds(index, _16BIT_LEN, buffer)
185
- Bytes.getInt16U(index, buffer.data)
214
+ export let toBytes = buffer => {
215
+ let len = Bytes.length(buffer.data)
216
+ if (buffer.len == len) {
217
+ buffer.data
218
+ } else {
219
+ Bytes.slice(0, buffer.len, buffer.data)
220
+ }
186
221
  }
187
222
 
188
223
  /**
189
- * Sets a signed 16-bit integer starting at the given byte index.
224
+ * Returns a slice of the current contents of the buffer as a byte sequence.
190
225
  *
191
- * @param index: The byte index to update
192
- * @param value: The value to set
193
- * @param buffer: The buffer to mutate
226
+ * @param start: The start index
227
+ * @param length: The number of bytes to include after the starting index
228
+ * @param buffer: The buffer to copy from
229
+ * @returns A byte sequence with bytes copied from the buffer
194
230
  *
195
231
  * @since v0.4.0
196
232
  */
197
- export let setInt16 = (index, value, buffer) => {
198
- checkIsIndexInBounds(index, _16BIT_LEN, buffer)
199
- Bytes.setInt16(index, value, buffer.data)
233
+ export let toBytesSlice = (start, length, buffer) => {
234
+ checkIsIndexInBounds(start, length, buffer)
235
+ Bytes.slice(start, length, buffer.data)
200
236
  }
201
237
 
202
238
  /**
203
- * Appends a signed 16-bit integer to a buffer.
239
+ * Returns a copy of the current contents of the buffer as a string.
204
240
  *
205
- * @param value: The value to append
206
- * @param buffer: The buffer to mutate
241
+ * @param buffer: The buffer to stringify
242
+ * @returns A string made with data copied from the buffer
207
243
  *
208
244
  * @since v0.4.0
209
245
  */
210
- export let addInt16 = (value, buffer) => {
211
- autogrow(_16BIT_LEN, buffer)
212
- let index = buffer.len
213
- buffer.len = buffer.len + _16BIT_LEN
214
- setInt16(index, value, buffer)
246
+ export let toString = buffer => {
247
+ Bytes.toString(toBytes(buffer))
215
248
  }
216
249
 
217
250
  /**
218
- * Gets a signed 32-bit integer starting at the given byte index.
251
+ * Returns a copy of a subset of the current contents of the buffer as a string.
219
252
  *
220
- * @param index: The byte index to access
221
- * @param buffer: The buffer to access
222
- * @returns A signed 32-bit integer that starts at the given index
253
+ * @param start: The start index
254
+ * @param length: The number of bytes to include after the starting index
255
+ * @param buffer: The buffer to copy from
256
+ * @returns A string made with a subset of data copied from the buffer
223
257
  *
224
258
  * @since v0.4.0
225
259
  */
226
- export let getInt32 = (index, buffer) => {
227
- checkIsIndexInBounds(index, _32BIT_LEN, buffer)
228
- Bytes.getInt32(index, buffer.data)
260
+ export let toStringSlice = (start, length, buffer) => {
261
+ Bytes.toString(toBytesSlice(start, length, buffer))
229
262
  }
230
263
 
231
264
  /**
232
- * Sets a signed 32-bit integer starting at the given byte index.
265
+ * Appends the bytes of a char to a buffer.
233
266
  *
234
- * @param index: The byte index to update
235
- * @param value: The value to set
267
+ * @param char: The character to append to the buffer
236
268
  * @param buffer: The buffer to mutate
237
269
  *
238
270
  * @since v0.4.0
239
271
  */
240
- export let setInt32 = (index, value, buffer) => {
241
- checkIsIndexInBounds(index, _32BIT_LEN, buffer)
242
- Bytes.setInt32(index, value, buffer.data)
272
+ @disableGC
273
+ export let rec addChar = (char: Char, buffer: Buffer) => {
274
+ let n = getCharAsWasmI32(char)
275
+ let bytelen = getCharByteLength(n)
276
+ match (bytelen) {
277
+ 1n => {
278
+ let c = Conv.toInt32(n)
279
+ Memory.incRef(WasmI32.fromGrain(addInt8help))
280
+ Memory.incRef(WasmI32.fromGrain(c))
281
+ Memory.incRef(WasmI32.fromGrain(buffer))
282
+ addInt8help(c, buffer)
283
+ Memory.decRef(WasmI32.fromGrain(c))
284
+ void
285
+ },
286
+ 2n => {
287
+ let c = Conv.toInt32(n)
288
+ Memory.incRef(WasmI32.fromGrain(addInt16help))
289
+ Memory.incRef(WasmI32.fromGrain(c))
290
+ Memory.incRef(WasmI32.fromGrain(buffer))
291
+ addInt16help(c, buffer)
292
+ Memory.decRef(WasmI32.fromGrain(c))
293
+ void
294
+ },
295
+ 3n => {
296
+ let (<) = WasmI32.ltU
297
+ let (+) = WasmI32.add
298
+ let (*) = WasmI32.mul
299
+ let (&) = WasmI32.and
300
+ let (>>) = WasmI32.shrU
301
+ for (let mut i = 0n; i < 3n; i += 1n) {
302
+ let c = Conv.toInt32(n >> i * 8n & 0xffn)
303
+ Memory.incRef(WasmI32.fromGrain(addInt8help))
304
+ Memory.incRef(WasmI32.fromGrain(c))
305
+ Memory.incRef(WasmI32.fromGrain(buffer))
306
+ addInt8help(c, buffer)
307
+ Memory.decRef(WasmI32.fromGrain(c))
308
+ void
309
+ }
310
+ },
311
+ _ => {
312
+ let c = Conv.toInt32(n)
313
+ Memory.incRef(WasmI32.fromGrain(addInt32help))
314
+ Memory.incRef(WasmI32.fromGrain(c))
315
+ Memory.incRef(WasmI32.fromGrain(buffer))
316
+ addInt32help(c, buffer)
317
+ Memory.decRef(WasmI32.fromGrain(c))
318
+ void
319
+ },
320
+ }
321
+
322
+ Memory.decRef(WasmI32.fromGrain(char))
323
+ Memory.decRef(WasmI32.fromGrain(buffer))
324
+ Memory.decRef(WasmI32.fromGrain(addChar))
325
+ void
243
326
  }
244
327
 
245
328
  /**
246
- * Appends a signed 32-bit integer to a buffer.
329
+ * Appends a byte sequence to a buffer.
247
330
  *
248
- * @param value: The value to append
331
+ * @param bytes: The byte sequence to append
249
332
  * @param buffer: The buffer to mutate
250
333
  *
251
334
  * @since v0.4.0
252
335
  */
253
- export let addInt32 = (value, buffer) => {
254
- autogrow(_32BIT_LEN, buffer)
255
- let index = buffer.len
256
- buffer.len = buffer.len + _32BIT_LEN
257
- setInt32(index, value, buffer)
258
- }
336
+ @disableGC
337
+ export let rec addBytes = (bytes, buffer) => {
338
+ Memory.incRef(WasmI32.fromGrain(Bytes.length))
339
+ Memory.incRef(WasmI32.fromGrain(bytes))
340
+ let bytelen = Bytes.length(bytes)
259
341
 
260
- /**
261
- * Gets a 32-bit float starting at the given byte index.
262
- *
263
- * @param index: The byte index to access
264
- * @param buffer: The buffer to access
265
- * @returns A 32-bit float that starts at the given index
266
- *
267
- * @since v0.4.0
268
- */
269
- export let getFloat32 = (index, buffer) => {
270
- checkIsIndexInBounds(index, _32BIT_LEN, buffer)
271
- Bytes.getFloat32(index, buffer.data)
342
+ Memory.incRef(WasmI32.fromGrain(autogrow))
343
+ Memory.incRef(WasmI32.fromGrain(bytelen))
344
+ Memory.incRef(WasmI32.fromGrain(buffer))
345
+ autogrow(bytelen, buffer)
346
+
347
+ let off = coerceNumberToWasmI32(buffer.len)
348
+ let len = getSize(WasmI32.fromGrain(bytes))
349
+ let src = WasmI32.fromGrain(bytes)
350
+ let dst = WasmI32.fromGrain(buffer.data)
351
+ appendBytes(0n, off, len, src, dst)
352
+
353
+ Memory.incRef(WasmI32.fromGrain((+)))
354
+ Memory.incRef(WasmI32.fromGrain(buffer.len))
355
+ Memory.incRef(WasmI32.fromGrain(bytelen))
356
+ buffer.len = buffer.len + bytelen
357
+
358
+ Memory.decRef(WasmI32.fromGrain(bytes))
359
+ Memory.decRef(WasmI32.fromGrain(buffer))
360
+ Memory.decRef(WasmI32.fromGrain(addBytes))
361
+ void
272
362
  }
273
363
 
274
364
  /**
275
- * Sets a 32-bit float starting at the given byte index.
365
+ * Appends the bytes of a string to a buffer.
276
366
  *
277
- * @param index: The byte index to update
278
- * @param value: The value to set
367
+ * @param string: The string to append
279
368
  * @param buffer: The buffer to mutate
280
369
  *
281
370
  * @since v0.4.0
282
371
  */
283
- export let setFloat32 = (index, value, buffer) => {
284
- checkIsIndexInBounds(index, _32BIT_LEN, buffer)
285
- Bytes.setFloat32(index, value, buffer.data)
286
- }
372
+ @disableGC
373
+ export let rec addString = (string, buffer) => {
374
+ Memory.incRef(WasmI32.fromGrain(String.byteLength))
375
+ Memory.incRef(WasmI32.fromGrain(string))
376
+ let bytelen = String.byteLength(string)
287
377
 
288
- /**
289
- * Appends a 32-bit float to a buffer.
378
+ Memory.incRef(WasmI32.fromGrain(autogrow))
379
+ Memory.incRef(WasmI32.fromGrain(bytelen))
380
+ Memory.incRef(WasmI32.fromGrain(buffer))
381
+ autogrow(bytelen, buffer)
382
+
383
+ let off = coerceNumberToWasmI32(buffer.len)
384
+ let len = getSize(WasmI32.fromGrain(string))
385
+ let src = WasmI32.fromGrain(string)
386
+ let dst = WasmI32.fromGrain(buffer.data)
387
+ appendBytes(0n, off, len, src, dst)
388
+
389
+ Memory.incRef(WasmI32.fromGrain((+)))
390
+ Memory.incRef(WasmI32.fromGrain(buffer.len))
391
+ Memory.incRef(WasmI32.fromGrain(bytelen))
392
+ buffer.len = buffer.len + bytelen
393
+
394
+ Memory.decRef(WasmI32.fromGrain(string))
395
+ Memory.decRef(WasmI32.fromGrain(buffer))
396
+ Memory.decRef(WasmI32.fromGrain(addString))
397
+ void
398
+ }
399
+
400
+ /**
401
+ * Appends the bytes of a subset of a string to a buffer.
290
402
  *
291
- * @param value: The value to append
403
+ * @param start: The char offset into the string
404
+ * @param length: The number of bytes to append
405
+ * @param string: The string to append
292
406
  * @param buffer: The buffer to mutate
293
407
  *
294
408
  * @since v0.4.0
295
409
  */
296
- export let addFloat32 = (value, buffer) => {
297
- autogrow(_32BIT_LEN, buffer)
298
- let index = buffer.len
299
- buffer.len = buffer.len + _32BIT_LEN
300
- setFloat32(index, value, buffer)
410
+ @disableGC
411
+ export let rec addStringSlice = (start: Number, length, string, buffer) => {
412
+ // Handle negative start index (needed before #1071 is fixed)
413
+ let start = {
414
+ // this is a block to avoid making all of these operators
415
+ // overriden in the whole function
416
+ let (<<) = WasmI32.shl
417
+ let (>>) = WasmI32.shrS
418
+ let (+) = WasmI32.add
419
+ let (!=) = WasmI32.ne
420
+ let (&) = WasmI32.and
421
+ Memory.incRef(WasmI32.fromGrain(String.length))
422
+ Memory.incRef(WasmI32.fromGrain(string))
423
+ let strlen = WasmI32.fromGrain(String.length(string)) >> 1n
424
+ let mut startW = WasmI32.fromGrain(start)
425
+ if ((startW & 1n) != 1n) {
426
+ throw InvalidArgument("Invalid start index")
427
+ }
428
+ startW = startW >> 1n
429
+ if (WasmI32.ltS(startW, 0n)) {
430
+ WasmI32.toGrain(WasmI32.shl(startW + strlen, 1n) + 1n)
431
+ } else {
432
+ start
433
+ }
434
+ }
435
+ let end = start + length
436
+ Memory.incRef(WasmI32.fromGrain(String.slice))
437
+ // no incref for start since we know it's a simple num. Add back for good measure after #1071 is fixed!
438
+ Memory.incRef(WasmI32.fromGrain(end))
439
+ Memory.incRef(WasmI32.fromGrain(string))
440
+ let slice = String.slice(start, end, string)
441
+
442
+ Memory.incRef(WasmI32.fromGrain(String.byteLength))
443
+ Memory.incRef(WasmI32.fromGrain(slice))
444
+ let bytelen = String.byteLength(slice)
445
+
446
+ Memory.incRef(WasmI32.fromGrain(autogrow))
447
+ Memory.incRef(WasmI32.fromGrain(bytelen))
448
+ Memory.incRef(WasmI32.fromGrain(buffer))
449
+ autogrow(bytelen, buffer)
450
+
451
+ let srcOff = 0n
452
+ let dstOff = coerceNumberToWasmI32(buffer.len)
453
+ let src = WasmI32.fromGrain(slice)
454
+ let dst = WasmI32.fromGrain(buffer.data)
455
+ appendBytes(srcOff, dstOff, coerceNumberToWasmI32(bytelen), src, dst)
456
+ Memory.decRef(WasmI32.fromGrain(slice))
457
+
458
+ Memory.incRef(WasmI32.fromGrain((+)))
459
+ Memory.incRef(WasmI32.fromGrain(buffer.len))
460
+ Memory.incRef(WasmI32.fromGrain(bytelen))
461
+ buffer.len = buffer.len + bytelen
462
+ Memory.decRef(WasmI32.fromGrain(bytelen))
463
+
464
+ Memory.decRef(WasmI32.fromGrain(start))
465
+ Memory.decRef(WasmI32.fromGrain(length))
466
+ Memory.decRef(WasmI32.fromGrain(string))
467
+ Memory.decRef(WasmI32.fromGrain(buffer))
468
+ Memory.decRef(WasmI32.fromGrain(addStringSlice))
469
+ void
301
470
  }
302
471
 
303
472
  /**
304
- * Gets a signed 64-bit integer starting at the given byte index.
473
+ * Appends the bytes of a subset of a byte seuqnece to a buffer.
305
474
  *
306
- * @param index: The byte index to access
307
- * @param buffer: The buffer to access
308
- * @returns A signed 64-bit integer that starts at the given index
475
+ * @param start: The byte offset into the byte sequence
476
+ * @param length: The number of bytes to append
477
+ * @param bytes: The byte sequence to append
478
+ * @param buffer: The buffer to mutate
309
479
  *
310
480
  * @since v0.4.0
311
481
  */
312
- export let getInt64 = (index, buffer) => {
313
- checkIsIndexInBounds(index, _64BIT_LEN, buffer)
314
- Bytes.getInt64(index, buffer.data)
482
+ @disableGC
483
+ export let rec addBytesSlice =
484
+ (
485
+ start: Number,
486
+ length: Number,
487
+ bytes: Bytes,
488
+ buffer: Buffer,
489
+ ) => {
490
+ let (-) = WasmI32.sub
491
+ let (<) = WasmI32.ltS
492
+ let (>) = WasmI32.gtS
493
+ let (>=) = WasmI32.geS
494
+
495
+ // bounds check start
496
+ let bytelen = WasmI32.load(WasmI32.fromGrain(bytes), 4n)
497
+ let srcOff = coerceNumberToWasmI32(start)
498
+ if (srcOff < 0n || srcOff >= bytelen) {
499
+ throw Exception.IndexOutOfBounds
500
+ }
501
+
502
+ // bounds check length
503
+ let len = coerceNumberToWasmI32(length)
504
+ if (len < 0n || len > bytelen - srcOff) {
505
+ throw Exception.IndexOutOfBounds
506
+ }
507
+
508
+ Memory.incRef(WasmI32.fromGrain(autogrow))
509
+ Memory.incRef(WasmI32.fromGrain(length))
510
+ Memory.incRef(WasmI32.fromGrain(buffer))
511
+ autogrow(length, buffer)
512
+
513
+ let dstOff = coerceNumberToWasmI32(buffer.len)
514
+ let src = WasmI32.fromGrain(bytes)
515
+ let dst = WasmI32.fromGrain(buffer.data)
516
+ appendBytes(srcOff, dstOff, len, src, dst)
517
+
518
+ Memory.incRef(WasmI32.fromGrain((+)))
519
+ Memory.incRef(WasmI32.fromGrain(buffer.len))
520
+ Memory.incRef(WasmI32.fromGrain(length))
521
+ buffer.len = buffer.len + length
522
+
523
+ Memory.decRef(WasmI32.fromGrain(start))
524
+ Memory.decRef(WasmI32.fromGrain(length))
525
+ Memory.decRef(WasmI32.fromGrain(bytes))
526
+ Memory.decRef(WasmI32.fromGrain(buffer))
527
+ Memory.decRef(WasmI32.fromGrain(addBytesSlice))
528
+ void
315
529
  }
316
530
 
317
531
  /**
318
- * Sets a signed 64-bit integer starting at the given byte index.
532
+ * Appends the bytes of a source buffer to destination buffer.
319
533
  *
320
- * @param index: The byte index to update
321
- * @param value: The value to set
322
- * @param buffer: The buffer to mutate
534
+ * The source buffer is not mutated by this operation. The destination buffer, however, is mutated.
535
+ *
536
+ * @param srcBuffer: The buffer to append
537
+ * @param dstBuffer: The buffer to mutate
323
538
  *
324
539
  * @since v0.4.0
325
540
  */
326
- export let setInt64 = (index, value, buffer) => {
327
- checkIsIndexInBounds(index, _64BIT_LEN, buffer)
328
- Bytes.setInt64(index, value, buffer.data)
541
+ export let addBuffer = (srcBuffer, dstBuffer) => {
542
+ addBytesSlice(0, srcBuffer.len, srcBuffer.data, dstBuffer)
329
543
  }
330
544
 
331
545
  /**
332
- * Appends a signed 64-bit integer to a buffer.
546
+ * Appends the bytes of a part of a buffer to the end of the buffer
333
547
  *
334
- * @param value: The value to set
335
- * @param buffer: The buffer to mutate
548
+ * The source buffer is not mutated by this operation. The destination buffer, however, is mutated.
549
+ *
550
+ * @param start: The byte offset into the buffer
551
+ * @param length: The number of bytes to append
552
+ * @param srcBuffer: The buffer to append
553
+ * @param dstBuffer: The buffer to mutate
336
554
  *
337
555
  * @since v0.4.0
338
556
  */
339
- export let addInt64 = (value, buffer) => {
340
- autogrow(_64BIT_LEN, buffer)
341
- let index = buffer.len
342
- buffer.len = buffer.len + _64BIT_LEN
343
- setInt64(index, value, buffer)
557
+ export let addBufferSlice = (start, length, srcBuffer, dstBuffer) => {
558
+ addBytesSlice(start, length, srcBuffer.data, dstBuffer)
344
559
  }
345
560
 
346
561
  /**
347
- * Gets a 64-bit float starting at the given byte index.
562
+ * @section Binary operations on integers: Functions for encoding and decoding integers stored in a buffer.
563
+ */
564
+
565
+ /**
566
+ * Gets a signed 8-bit integer starting at the given byte index.
348
567
  *
349
568
  * @param index: The byte index to access
350
569
  * @param buffer: The buffer to access
351
- * @returns A 64-bit float that starts at the given index
570
+ * @returns A 32-bit integer representing a signed 8-bit integer that starts at the given index
352
571
  *
353
572
  * @since v0.4.0
354
573
  */
355
- export let getFloat64 = (index, buffer) => {
356
- checkIsIndexInBounds(index, _64BIT_LEN, buffer)
357
- Bytes.getFloat64(index, buffer.data)
574
+ export let getInt8S = (index, buffer) => {
575
+ checkIsIndexInBounds(index, _8BIT_LEN, buffer)
576
+ Bytes.getInt8S(index, buffer.data)
358
577
  }
359
578
 
360
579
  /**
361
- * Sets a 64-bit float starting at the given byte index.
580
+ * Gets an unsigned 8-bit integer starting at the given byte index.
581
+ *
582
+ * @param index: The byte index to access
583
+ * @param buffer: The buffer to access
584
+ * @returns A 32-bit integer representing an unsigned 8-bit integer that starts at the given index
585
+ *
586
+ * @since v0.4.0
587
+ */
588
+ export let getInt8U = (index, buffer) => {
589
+ checkIsIndexInBounds(index, _8BIT_LEN, buffer)
590
+ Bytes.getInt8U(index, buffer.data)
591
+ }
592
+
593
+ /**
594
+ * Sets a signed 8-bit integer starting at the given byte index.
362
595
  *
363
596
  * @param index: The byte index to update
364
597
  * @param value: The value to set
@@ -366,404 +599,242 @@ export let getFloat64 = (index, buffer) => {
366
599
  *
367
600
  * @since v0.4.0
368
601
  */
369
- export let setFloat64 = (index, value, buffer) => {
370
- checkIsIndexInBounds(index, _64BIT_LEN, buffer)
371
- Bytes.setFloat64(index, value, buffer.data)
602
+ export let setInt8 = (index, value, buffer) => {
603
+ checkIsIndexInBounds(index, _8BIT_LEN, buffer)
604
+ Bytes.setInt8(index, value, buffer.data)
372
605
  }
373
606
 
374
607
  /**
375
- * Appends a 64-bit float to a buffer.
608
+ * Appends a signed 8-bit integer to a buffer.
376
609
  *
377
610
  * @param value: The value to append
378
611
  * @param buffer: The buffer to mutate
379
612
  *
380
613
  * @since v0.4.0
381
614
  */
382
- export let addFloat64 = (value, buffer) => {
383
- autogrow(_64BIT_LEN, buffer)
384
- let index = buffer.len
385
- buffer.len = buffer.len + _64BIT_LEN
386
- setFloat64(index, value, buffer)
615
+ export let addInt8 = (value, buffer) => {
616
+ addInt8help(value, buffer)
387
617
  }
388
618
 
389
619
  /**
390
- * Creates a fresh buffer, initially empty.
391
- *
392
- * The `initialSize` parameter is the initial size of the internal byte sequence that holds the buffer contents.
393
- * That byte sequence is automatically reallocated when more than `initialSize` bytes are stored in the buffer, but shrinks back to `initialSize` characters when reset is called.
620
+ * Gets a signed 16-bit integer starting at the given byte index.
394
621
  *
395
- * @param initialSize: The initial size of the buffer
396
- * @returns The new buffer
622
+ * @param index: The byte index to access
623
+ * @param buffer: The buffer to access
624
+ * @returns A 32-bit integer representing a signed 16-bit integer that starts at the given index
397
625
  *
398
626
  * @since v0.4.0
399
627
  */
400
- export let make = (initialSize) => {
401
- if (initialSize < 0) throw Exception.InvalidArgument("Buffers size must be >= 0")
402
- {
403
- len: 0,
404
- initialSize,
405
- data: Bytes.make(initialSize),
406
- }
628
+ export let getInt16S = (index, buffer) => {
629
+ checkIsIndexInBounds(index, _16BIT_LEN, buffer)
630
+ Bytes.getInt16S(index, buffer.data)
407
631
  }
408
632
 
409
633
  /**
410
- * Gets the number of bytes currently contained in a buffer.
634
+ * Gets an unsigned 16-bit integer starting at the given byte index.
411
635
  *
636
+ * @param index: The byte index to access
412
637
  * @param buffer: The buffer to access
413
- * @returns The length of the buffer in bytes
638
+ * @returns A 32-bit integer representing an unsigned 16-bit integer that starts at the given index
414
639
  *
415
640
  * @since v0.4.0
416
641
  */
417
- export let length = (buffer) => buffer.len
642
+ export let getInt16U = (index, buffer) => {
643
+ checkIsIndexInBounds(index, _16BIT_LEN, buffer)
644
+ Bytes.getInt16U(index, buffer.data)
645
+ }
418
646
 
419
647
  /**
420
- * Clears data in the buffer and sets its length to zero.
421
- *
422
- * This operation does not resize the underlying byte sequence.
648
+ * Sets a signed 16-bit integer starting at the given byte index.
423
649
  *
424
- * @param buffer: The buffer to clear
650
+ * @param index: The byte index to update
651
+ * @param value: The value to set
652
+ * @param buffer: The buffer to mutate
425
653
  *
426
654
  * @since v0.4.0
427
655
  */
428
- export let clear = (buffer) => {
429
- Bytes.fill(0x0l, buffer.data)
430
- buffer.len = 0
656
+ export let setInt16 = (index, value, buffer) => {
657
+ checkIsIndexInBounds(index, _16BIT_LEN, buffer)
658
+ Bytes.setInt16(index, value, buffer.data)
431
659
  }
432
660
 
433
661
  /**
434
- * Empty a buffer and deallocate the internal byte sequence holding the buffer contents.
435
- *
436
- * This operation resizes the underlying byte sequence to the initial size of the buffer.
662
+ * Appends a signed 16-bit integer to a buffer.
437
663
  *
438
- * @param buffer: The buffer to reset
664
+ * @param value: The value to append
665
+ * @param buffer: The buffer to mutate
439
666
  *
440
667
  * @since v0.4.0
441
668
  */
442
- export let reset = (buffer) => {
443
- buffer.data = Bytes.make(buffer.initialSize)
444
- buffer.len = 0
669
+ export let addInt16 = (value, buffer) => {
670
+ addInt16help(value, buffer)
445
671
  }
446
672
 
447
673
  /**
448
- * Shortens a buffer to the given length.
449
- *
450
- * This operation does not resize the underlying byte sequence.
674
+ * Gets a signed 32-bit integer starting at the given byte index.
451
675
  *
452
- * @param length: The number of bytes to truncate the buffer to
453
- * @param buffer: The buffer to truncate
676
+ * @param index: The byte index to access
677
+ * @param buffer: The buffer to access
678
+ * @returns A signed 32-bit integer that starts at the given index
454
679
  *
455
680
  * @since v0.4.0
456
681
  */
457
- @disableGC
458
- export let rec truncate = (length, buffer) => {
459
- Memory.incRef(WasmI32.fromGrain((<)))
460
- Memory.incRef(WasmI32.fromGrain((||)))
461
- Memory.incRef(WasmI32.fromGrain((>)))
462
- if (length < 0 || length > buffer.len) throw Exception.IndexOutOfBounds
463
-
464
- let (+) = WasmI32.add
465
- let src = WasmI32.fromGrain(buffer.data)
466
- let size = getSize(src)
467
- let off = coerceNumberToWasmI32(length)
468
- let ret = Memory.fill(src + _VALUE_OFFSET + off, 0n, size)
469
- buffer.len = length
470
-
471
- Memory.decRef(WasmI32.fromGrain(length))
472
- Memory.decRef(WasmI32.fromGrain(buffer))
473
- Memory.decRef(WasmI32.fromGrain(truncate))
474
- void
682
+ export let getInt32 = (index, buffer) => {
683
+ checkIsIndexInBounds(index, _32BIT_LEN, buffer)
684
+ Bytes.getInt32(index, buffer.data)
475
685
  }
476
686
 
477
687
  /**
478
- * Returns a copy of the current contents of the buffer as a byte sequence.
688
+ * Sets a signed 32-bit integer starting at the given byte index.
479
689
  *
480
- * @param buffer: The buffer to copy into a byte sequence
481
- * @returns A byte sequence made from copied buffer data
690
+ * @param index: The byte index to update
691
+ * @param value: The value to set
692
+ * @param buffer: The buffer to mutate
482
693
  *
483
694
  * @since v0.4.0
484
695
  */
485
- export let toBytes = (buffer) => {
486
- let len = Bytes.length(buffer.data)
487
- if (buffer.len == len) {
488
- buffer.data
489
- } else {
490
- Bytes.slice(0, buffer.len, buffer.data)
491
- }
696
+ export let setInt32 = (index, value, buffer) => {
697
+ checkIsIndexInBounds(index, _32BIT_LEN, buffer)
698
+ Bytes.setInt32(index, value, buffer.data)
492
699
  }
493
700
 
494
701
  /**
495
- * Returns a slice of the current contents of the buffer as a byte sequence.
702
+ * Appends a signed 32-bit integer to a buffer.
496
703
  *
497
- * @param start: The start index
498
- * @param length: The number of bytes to include after the starting index
499
- * @param buffer: The buffer to copy from
500
- * @returns A byte sequence with bytes copied from the buffer
704
+ * @param value: The value to append
705
+ * @param buffer: The buffer to mutate
501
706
  *
502
707
  * @since v0.4.0
503
708
  */
504
- export let toBytesSlice = (start, length, buffer) => {
505
- checkIsIndexInBounds(start, length, buffer)
506
- Bytes.slice(start, length, buffer.data)
709
+ export let addInt32 = (value, buffer) => {
710
+ addInt32help(value, buffer)
507
711
  }
508
712
 
509
713
  /**
510
- * Returns a copy of the current contents of the buffer as a string.
714
+ * Gets a 32-bit float starting at the given byte index.
511
715
  *
512
- * @param buffer: The buffer to stringify
513
- * @returns A string made with data copied from the buffer
716
+ * @param index: The byte index to access
717
+ * @param buffer: The buffer to access
718
+ * @returns A 32-bit float that starts at the given index
514
719
  *
515
720
  * @since v0.4.0
516
721
  */
517
- export let toString = (buffer) => {
518
- Bytes.toString(toBytes(buffer))
722
+ export let getFloat32 = (index, buffer) => {
723
+ checkIsIndexInBounds(index, _32BIT_LEN, buffer)
724
+ Bytes.getFloat32(index, buffer.data)
519
725
  }
520
726
 
521
727
  /**
522
- * Returns a copy of a subset of the current contents of the buffer as a string.
728
+ * Sets a 32-bit float starting at the given byte index.
523
729
  *
524
- * @param start: The start index
525
- * @param length: The number of bytes to include after the starting index
526
- * @param buffer: The buffer to copy from
527
- * @returns A string made with a subset of data copied from the buffer
730
+ * @param index: The byte index to update
731
+ * @param value: The value to set
732
+ * @param buffer: The buffer to mutate
528
733
  *
529
734
  * @since v0.4.0
530
735
  */
531
- export let toStringSlice = (start, length, buffer) => {
532
- Bytes.toString(toBytesSlice(start, length, buffer))
736
+ export let setFloat32 = (index, value, buffer) => {
737
+ checkIsIndexInBounds(index, _32BIT_LEN, buffer)
738
+ Bytes.setFloat32(index, value, buffer.data)
533
739
  }
534
740
 
535
741
  /**
536
- * Appends the bytes of a char to a buffer.
742
+ * Appends a 32-bit float to a buffer.
537
743
  *
538
- * @param char: The character to append to the buffer
744
+ * @param value: The value to append
539
745
  * @param buffer: The buffer to mutate
540
746
  *
541
747
  * @since v0.4.0
542
748
  */
543
- @disableGC
544
- export let rec addChar = (char, buffer) => {
545
- let n = getCharAsWasmI32(char)
546
- let bytelen = getCharByteLength(n)
547
- match (bytelen) {
548
- 1n => {
549
- let c = Conv.toInt32(n)
550
- Memory.incRef(WasmI32.fromGrain(addInt8))
551
- Memory.incRef(WasmI32.fromGrain(c))
552
- Memory.incRef(WasmI32.fromGrain(buffer))
553
- addInt8(c, buffer)
554
- },
555
- 2n => {
556
- let c = Conv.toInt32(n)
557
- Memory.incRef(WasmI32.fromGrain(addInt16))
558
- Memory.incRef(WasmI32.fromGrain(c))
559
- Memory.incRef(WasmI32.fromGrain(buffer))
560
- addInt16(c, buffer)
561
- },
562
- 3n => {
563
- let (<) = WasmI32.ltU
564
- let (+) = WasmI32.add
565
- let (*) = WasmI32.mul
566
- let (&) = WasmI32.and
567
- let (>>) = WasmI32.shrU
568
- for (let mut i = 0n; i < 3n; i = i + 1n) {
569
- let c = Conv.toInt32(n >> (i * 8n) & 0xffn)
570
- Memory.incRef(WasmI32.fromGrain(addInt8))
571
- Memory.incRef(WasmI32.fromGrain(c))
572
- Memory.incRef(WasmI32.fromGrain(buffer))
573
- addInt8(c, buffer)
574
- }
575
- },
576
- _ => {
577
- let c = Conv.toInt32(n)
578
- Memory.incRef(WasmI32.fromGrain(addInt32))
579
- Memory.incRef(WasmI32.fromGrain(c))
580
- Memory.incRef(WasmI32.fromGrain(buffer))
581
- addInt32(c, buffer)
582
- },
583
- }
584
-
585
- Memory.decRef(WasmI32.fromGrain(char))
586
- Memory.decRef(WasmI32.fromGrain(buffer))
587
- Memory.decRef(WasmI32.fromGrain(addChar))
588
- void
749
+ export let addFloat32 = (value, buffer) => {
750
+ autogrow(_32BIT_LEN, buffer)
751
+ let index = buffer.len
752
+ buffer.len = buffer.len + _32BIT_LEN
753
+ setFloat32(index, value, buffer)
589
754
  }
590
755
 
591
756
  /**
592
- * Appends a byte sequence to a buffer.
757
+ * Gets a signed 64-bit integer starting at the given byte index.
593
758
  *
594
- * @param bytes: The byte sequence to append
595
- * @param buffer: The buffer to mutate
759
+ * @param index: The byte index to access
760
+ * @param buffer: The buffer to access
761
+ * @returns A signed 64-bit integer that starts at the given index
596
762
  *
597
763
  * @since v0.4.0
598
764
  */
599
- @disableGC
600
- export let rec addBytes = (bytes, buffer) => {
601
- Memory.incRef(WasmI32.fromGrain(Bytes.length))
602
- Memory.incRef(WasmI32.fromGrain(bytes))
603
- let bytelen = Bytes.length(bytes)
604
-
605
- Memory.incRef(WasmI32.fromGrain(autogrow))
606
- Memory.incRef(WasmI32.fromGrain(bytelen))
607
- Memory.incRef(WasmI32.fromGrain(buffer))
608
- autogrow(bytelen, buffer)
609
-
610
- let off = coerceNumberToWasmI32(buffer.len)
611
- let len = getSize(WasmI32.fromGrain(bytes))
612
- let src = WasmI32.fromGrain(bytes)
613
- let dst = WasmI32.fromGrain(buffer.data)
614
- appendBytes(0n, off, len, src, dst)
615
-
616
- Memory.incRef(WasmI32.fromGrain((+)))
617
- Memory.incRef(WasmI32.fromGrain(buffer.len))
618
- Memory.incRef(WasmI32.fromGrain(bytelen))
619
- buffer.len = buffer.len + bytelen
620
-
621
- Memory.decRef(WasmI32.fromGrain(bytes))
622
- Memory.decRef(WasmI32.fromGrain(buffer))
623
- Memory.decRef(WasmI32.fromGrain(addBytes))
624
- void
765
+ export let getInt64 = (index, buffer) => {
766
+ checkIsIndexInBounds(index, _64BIT_LEN, buffer)
767
+ Bytes.getInt64(index, buffer.data)
625
768
  }
626
769
 
627
770
  /**
628
- * Appends the bytes of a string to a buffer.
771
+ * Sets a signed 64-bit integer starting at the given byte index.
629
772
  *
630
- * @param string: The string to append
773
+ * @param index: The byte index to update
774
+ * @param value: The value to set
631
775
  * @param buffer: The buffer to mutate
632
776
  *
633
777
  * @since v0.4.0
634
778
  */
635
- @disableGC
636
- export let rec addString = (string, buffer) => {
637
- Memory.incRef(WasmI32.fromGrain(String.byteLength))
638
- Memory.incRef(WasmI32.fromGrain(string))
639
- let bytelen = String.byteLength(string)
640
-
641
- Memory.incRef(WasmI32.fromGrain(autogrow))
642
- Memory.incRef(WasmI32.fromGrain(bytelen))
643
- Memory.incRef(WasmI32.fromGrain(buffer))
644
- autogrow(bytelen, buffer)
645
-
646
- let off = coerceNumberToWasmI32(buffer.len)
647
- let len = getSize(WasmI32.fromGrain(string))
648
- let src = WasmI32.fromGrain(string)
649
- let dst = WasmI32.fromGrain(buffer.data)
650
- appendBytes(0n, off, len, src, dst)
651
-
652
- Memory.incRef(WasmI32.fromGrain((+)))
653
- Memory.incRef(WasmI32.fromGrain(buffer.len))
654
- Memory.incRef(WasmI32.fromGrain(bytelen))
655
- buffer.len = buffer.len + bytelen
656
-
657
- Memory.decRef(WasmI32.fromGrain(string))
658
- Memory.decRef(WasmI32.fromGrain(buffer))
659
- Memory.decRef(WasmI32.fromGrain(addString))
660
- void
779
+ export let setInt64 = (index, value, buffer) => {
780
+ checkIsIndexInBounds(index, _64BIT_LEN, buffer)
781
+ Bytes.setInt64(index, value, buffer.data)
661
782
  }
662
783
 
663
784
  /**
664
- * Appends the bytes of a subset of a string to a buffer.
785
+ * Appends a signed 64-bit integer to a buffer.
665
786
  *
666
- * @param start: The char offset into the string
667
- * @param length: The number of bytes to append
668
- * @param string: The string to append
787
+ * @param value: The value to set
669
788
  * @param buffer: The buffer to mutate
670
789
  *
671
790
  * @since v0.4.0
672
791
  */
673
- @disableGC
674
- export let rec addStringSlice = (start, length, string, buffer) => {
675
- Memory.incRef(WasmI32.fromGrain(String.byteLength))
676
- Memory.incRef(WasmI32.fromGrain(start))
677
- Memory.incRef(WasmI32.fromGrain(length))
678
- Memory.incRef(WasmI32.fromGrain(string))
679
- let bytelen = String.byteLength(String.slice(start, length, string))
680
-
681
- Memory.incRef(WasmI32.fromGrain(autogrow))
682
- Memory.incRef(WasmI32.fromGrain(bytelen))
683
- Memory.incRef(WasmI32.fromGrain(buffer))
684
- autogrow(bytelen, buffer)
685
-
686
- let srcOff = coerceNumberToWasmI32(start)
687
- let dstOff = coerceNumberToWasmI32(buffer.len)
688
- let src = WasmI32.fromGrain(string)
689
- let dst = WasmI32.fromGrain(buffer.data)
690
- appendBytes(srcOff, dstOff, coerceNumberToWasmI32(bytelen), src, dst)
691
-
692
- Memory.incRef(WasmI32.fromGrain((+)))
693
- Memory.incRef(WasmI32.fromGrain(buffer.len))
694
- Memory.incRef(WasmI32.fromGrain(bytelen))
695
- buffer.len = buffer.len + bytelen
696
-
697
- Memory.decRef(WasmI32.fromGrain(start))
698
- Memory.decRef(WasmI32.fromGrain(length))
699
- Memory.decRef(WasmI32.fromGrain(string))
700
- Memory.decRef(WasmI32.fromGrain(buffer))
701
- Memory.decRef(WasmI32.fromGrain(addStringSlice))
702
- void
792
+ export let addInt64 = (value, buffer) => {
793
+ autogrow(_64BIT_LEN, buffer)
794
+ let index = buffer.len
795
+ buffer.len = buffer.len + _64BIT_LEN
796
+ setInt64(index, value, buffer)
703
797
  }
704
798
 
705
799
  /**
706
- * Appends the bytes of a subset of a byte seuqnece to a buffer.
800
+ * Gets a 64-bit float starting at the given byte index.
707
801
  *
708
- * @param start: The byte offset into the byte sequence
709
- * @param length: The number of bytes to append
710
- * @param bytes: The byte sequence to append
711
- * @param buffer: The buffer to mutate
802
+ * @param index: The byte index to access
803
+ * @param buffer: The buffer to access
804
+ * @returns A 64-bit float that starts at the given index
712
805
  *
713
806
  * @since v0.4.0
714
807
  */
715
- @disableGC
716
- export let rec addBytesSlice = (start, length, bytes, buffer) => {
717
- Memory.incRef(WasmI32.fromGrain(autogrow))
718
- Memory.incRef(WasmI32.fromGrain(length))
719
- Memory.incRef(WasmI32.fromGrain(buffer))
720
- autogrow(length, buffer)
721
-
722
- let srcOff = coerceNumberToWasmI32(start)
723
- let dstOff = coerceNumberToWasmI32(buffer.len)
724
- let src = WasmI32.fromGrain(bytes)
725
- let dst = WasmI32.fromGrain(buffer.data)
726
- appendBytes(srcOff, dstOff, coerceNumberToWasmI32(length), src, dst)
727
-
728
- Memory.incRef(WasmI32.fromGrain((+)))
729
- Memory.incRef(WasmI32.fromGrain(buffer.len))
730
- Memory.incRef(WasmI32.fromGrain(length))
731
- buffer.len = buffer.len + length
732
-
733
- Memory.decRef(WasmI32.fromGrain(start))
734
- Memory.decRef(WasmI32.fromGrain(length))
735
- Memory.decRef(WasmI32.fromGrain(bytes))
736
- Memory.decRef(WasmI32.fromGrain(buffer))
737
- Memory.decRef(WasmI32.fromGrain(addBytesSlice))
738
- void
808
+ export let getFloat64 = (index, buffer) => {
809
+ checkIsIndexInBounds(index, _64BIT_LEN, buffer)
810
+ Bytes.getFloat64(index, buffer.data)
739
811
  }
740
812
 
741
813
  /**
742
- * Appends the bytes of a source buffer to destination buffer.
743
- *
744
- * The source buffer is not mutated by this operation. The destination buffer, however, is mutated.
814
+ * Sets a 64-bit float starting at the given byte index.
745
815
  *
746
- * @param srcBuffer: The buffer to append
747
- * @param dstBuffer: The buffer to mutate
816
+ * @param index: The byte index to update
817
+ * @param value: The value to set
818
+ * @param buffer: The buffer to mutate
748
819
  *
749
820
  * @since v0.4.0
750
821
  */
751
- export let addBuffer = (srcBuffer, dstBuffer) => {
752
- addBytesSlice(0, srcBuffer.len, srcBuffer.data, dstBuffer)
822
+ export let setFloat64 = (index, value, buffer) => {
823
+ checkIsIndexInBounds(index, _64BIT_LEN, buffer)
824
+ Bytes.setFloat64(index, value, buffer.data)
753
825
  }
754
826
 
755
827
  /**
756
- * Appends the bytes of a part of a buffer to the end of the buffer
757
- *
758
- * The source buffer is not mutated by this operation. The destination buffer, however, is mutated.
828
+ * Appends a 64-bit float to a buffer.
759
829
  *
760
- * @param start: The byte offset into the buffer
761
- * @param length: The number of bytes to append
762
- * @param srcBuffer: The buffer to append
763
- * @param dstBuffer: The buffer to mutate
830
+ * @param value: The value to append
831
+ * @param buffer: The buffer to mutate
764
832
  *
765
833
  * @since v0.4.0
766
834
  */
767
- export let addBufferSlice = (start, length, srcBuffer, dstBuffer) => {
768
- addBytesSlice(start, length, srcBuffer.data, dstBuffer)
835
+ export let addFloat64 = (value, buffer) => {
836
+ autogrow(_64BIT_LEN, buffer)
837
+ let index = buffer.len
838
+ buffer.len = buffer.len + _64BIT_LEN
839
+ setFloat64(index, value, buffer)
769
840
  }