@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/bytes.gr CHANGED
@@ -1,44 +1,56 @@
1
+ /**
2
+ * @module Bytes: Utilities for working with byte sequences.
3
+ *
4
+ * @example import Bytes from "bytes"
5
+ *
6
+ * @since v0.3.2
7
+ */
8
+
1
9
  import Memory from "runtime/unsafe/memory"
2
10
  import WasmI32 from "runtime/unsafe/wasmi32"
3
11
  import WasmI64 from "runtime/unsafe/wasmi64"
4
12
  import WasmF32 from "runtime/unsafe/wasmf32"
5
13
  import WasmF64 from "runtime/unsafe/wasmf64"
6
14
  import Conv from "runtime/unsafe/conv"
7
- import { tagSimpleNumber, allocateBytes, allocateString } from "runtime/dataStructures"
15
+ import {
16
+ tagSimpleNumber,
17
+ allocateBytes,
18
+ allocateString
19
+ } from "runtime/dataStructures"
8
20
  import Exception from "runtime/exception"
9
21
  import Int32 from "int32"
10
22
  import { coerceNumberToWasmI32 } from "runtime/numbers"
11
23
 
12
24
  // hack to avoid incRef on these variables
13
25
  @disableGC
14
- let mut _SIZE_OFFSET = 1n;
26
+ let mut _SIZE_OFFSET = 1n
15
27
  @disableGC
16
- let mut _VALUE_OFFSET = 1n;
28
+ let mut _VALUE_OFFSET = 1n
17
29
  @disableGC
18
- let mut _INT8_BYTE_SIZE = 1n;
30
+ let mut _INT8_BYTE_SIZE = 1n
19
31
  @disableGC
20
- let mut _INT16_BYTE_SIZE = 1n;
32
+ let mut _INT16_BYTE_SIZE = 1n
21
33
  @disableGC
22
- let mut _INT32_BYTE_SIZE = 1n;
34
+ let mut _INT32_BYTE_SIZE = 1n
23
35
  @disableGC
24
- let mut _FLOAT32_BYTE_SIZE = 1n;
36
+ let mut _FLOAT32_BYTE_SIZE = 1n
25
37
  @disableGC
26
- let mut _INT64_BYTE_SIZE = 1n;
38
+ let mut _INT64_BYTE_SIZE = 1n
27
39
  @disableGC
28
- let mut _FLOAT64_BYTE_SIZE = 1n;
40
+ let mut _FLOAT64_BYTE_SIZE = 1n
29
41
 
30
42
  @disableGC
31
43
  let initVals = () => {
32
- _SIZE_OFFSET = 4n;
33
- _VALUE_OFFSET = 8n;
34
- _INT8_BYTE_SIZE = 1n;
35
- _INT16_BYTE_SIZE = 2n;
36
- _INT32_BYTE_SIZE = 4n;
37
- _FLOAT32_BYTE_SIZE = 4n;
38
- _INT64_BYTE_SIZE = 8n;
39
- _FLOAT64_BYTE_SIZE = 8n;
44
+ _SIZE_OFFSET = 4n
45
+ _VALUE_OFFSET = 8n
46
+ _INT8_BYTE_SIZE = 1n
47
+ _INT16_BYTE_SIZE = 2n
48
+ _INT32_BYTE_SIZE = 4n
49
+ _FLOAT32_BYTE_SIZE = 4n
50
+ _INT64_BYTE_SIZE = 8n
51
+ _FLOAT64_BYTE_SIZE = 8n
40
52
  }
41
- initVals();
53
+ initVals()
42
54
 
43
55
  /** Throws an exception if the index specified is out-of-bounds */
44
56
  @disableGC
@@ -49,561 +61,654 @@ let checkIndexIsInBounds = (i, byteSize, max) => {
49
61
  if (i < 0n) {
50
62
  throw Exception.IndexOutOfBounds
51
63
  }
52
- if ((i + byteSize) > max) {
64
+ if (i + byteSize > max) {
53
65
  throw Exception.IndexOutOfBounds
54
66
  }
55
67
  }
56
68
 
57
69
  /** Gets the size of a Bytes via its ptr */
58
70
  @disableGC
59
- let getSize = (ptr) => WasmI32.load(ptr, _SIZE_OFFSET)
71
+ let getSize = ptr => WasmI32.load(ptr, _SIZE_OFFSET)
60
72
 
61
73
  /**
62
- * Sets a signed 8-bit integer starting at the given byte index.
63
- * @param i: Number - The byte index
64
- * @param v: Int32 - The value to set
65
- * @param b: Bytes - The byte sequence
66
- * @returns Void
74
+ * @section Values: Functions for working with the Bytes data type.
75
+ */
76
+
77
+ /**
78
+ * Creates a new byte sequence of the input size.
79
+ *
80
+ * @param size: The number of bytes to store
81
+ * @returns The new byte sequence
82
+ *
83
+ * @since v0.3.2
67
84
  */
68
85
  @disableGC
69
- export let rec setInt8 = (i: Number, v: Int32, b: Bytes) => {
70
- let (+) = WasmI32.add
71
- let ptr = WasmI32.fromGrain(b)
72
- let size = getSize(ptr)
73
- let offset = coerceNumberToWasmI32(i)
74
- checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
75
- let value = Conv.fromInt32(v)
76
- let ret = WasmI32.store8(ptr + offset, value, _VALUE_OFFSET)
77
- Memory.decRef(WasmI32.fromGrain(i))
78
- Memory.decRef(WasmI32.fromGrain(v))
79
- Memory.decRef(WasmI32.fromGrain(b))
80
- Memory.decRef(WasmI32.fromGrain(setInt8))
86
+ export let rec make = (size: Number) => {
87
+ let bytes = allocateBytes(coerceNumberToWasmI32(size))
88
+ let ret = WasmI32.toGrain(bytes): (Bytes)
89
+ Memory.decRef(WasmI32.fromGrain(size))
90
+ Memory.decRef(WasmI32.fromGrain(make))
81
91
  ret
82
92
  }
83
93
 
84
94
  /**
85
- * Gets a signed 8-bit integer starting at the given byte index.
86
- * @param i: Number - The byte index
87
- * @param b: Bytes - The byte sequence
88
- * @returns Int32
95
+ * An empty byte sequence.
96
+ *
97
+ * @since v0.3.2
98
+ */
99
+ export let empty = make(0)
100
+
101
+ /**
102
+ * Creates a new byte sequence from the input string.
103
+ *
104
+ * @param string: The string to copy into a byte sequence
105
+ * @returns The new byte sequence
106
+ *
107
+ * @since v0.3.2
89
108
  */
90
109
  @disableGC
91
- export let rec getInt8S = (i: Number, b: Bytes) => {
110
+ export let rec fromString = (string: String) => {
92
111
  let (+) = WasmI32.add
93
- let ptr = WasmI32.fromGrain(b)
94
- let size = getSize(ptr)
95
- let offset = coerceNumberToWasmI32(i)
96
- checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
97
- let n = WasmI32.load8S(ptr + offset, _VALUE_OFFSET)
98
- let ret = Conv.toInt32(n)
99
- Memory.decRef(WasmI32.fromGrain(i))
100
- Memory.decRef(WasmI32.fromGrain(b))
101
- Memory.decRef(WasmI32.fromGrain(getInt8S))
112
+ let src = WasmI32.fromGrain(string)
113
+ let size = getSize(src)
114
+ let dst = allocateBytes(size)
115
+ Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
116
+ let ret = WasmI32.toGrain(dst): (Bytes)
117
+ Memory.decRef(WasmI32.fromGrain(string))
118
+ Memory.decRef(WasmI32.fromGrain(fromString))
102
119
  ret
103
120
  }
104
121
 
105
122
  /**
106
- * Gets an unsigned 8-bit integer starting at the given byte index.
107
- * @param i: Number - The byte index
108
- * @param b: Bytes - The byte sequence
109
- * @returns Int32
123
+ * Creates a new string from the input bytes.
124
+ *
125
+ * @param bytes: The source byte sequence
126
+ * @returns The string representation of the bytes
127
+ *
128
+ * @since v0.3.2
110
129
  */
111
130
  @disableGC
112
- export let rec getInt8U = (i: Number, b: Bytes) => {
131
+ export let rec toString = (bytes: Bytes) => {
113
132
  let (+) = WasmI32.add
114
- let ptr = WasmI32.fromGrain(b)
115
- let size = getSize(ptr)
116
- let offset = coerceNumberToWasmI32(i)
117
- checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
118
- let n = WasmI32.load8U(ptr + offset, _VALUE_OFFSET)
119
- let ret = Conv.toInt32(n)
120
- Memory.decRef(WasmI32.fromGrain(i))
121
- Memory.decRef(WasmI32.fromGrain(b))
122
- Memory.decRef(WasmI32.fromGrain(getInt8U))
133
+ let src = WasmI32.fromGrain(bytes)
134
+ let size = getSize(src)
135
+ let dst = allocateString(size)
136
+ Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
137
+ let ret = WasmI32.toGrain(dst): (String)
138
+ Memory.decRef(WasmI32.fromGrain(bytes))
139
+ Memory.decRef(WasmI32.fromGrain(toString))
123
140
  ret
124
141
  }
125
142
 
126
143
  /**
127
- * Sets a signed 16-bit integer starting at the given byte index.
128
- * @param i: Number - The byte index
129
- * @param v: Int32 - The value to set
130
- * @param b: Bytes - The byte sequence
131
- * @returns Void
144
+ * Returns the length of a byte sequence.
145
+ *
146
+ * @param bytes: The byte sequence to inspect
147
+ * @returns The number of bytes
148
+ *
149
+ * @since v0.3.2
132
150
  */
133
151
  @disableGC
134
- export let rec setInt16 = (i: Number, v: Int32, b: Bytes) => {
135
- let (+) = WasmI32.add
136
- let ptr = WasmI32.fromGrain(b)
137
- let size = getSize(ptr)
138
- let offset = coerceNumberToWasmI32(i)
139
- checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
140
- let value = Conv.fromInt32(v)
141
- let ret = WasmI32.store16(ptr + offset, value, _VALUE_OFFSET)
142
- Memory.decRef(WasmI32.fromGrain(i))
143
- Memory.decRef(WasmI32.fromGrain(v))
144
- Memory.decRef(WasmI32.fromGrain(b))
145
- Memory.decRef(WasmI32.fromGrain(setInt16))
152
+ export let rec length = (bytes: Bytes) => {
153
+ let b = WasmI32.fromGrain(bytes)
154
+ let ret = tagSimpleNumber(getSize(b))
155
+ Memory.decRef(WasmI32.fromGrain(bytes))
156
+ Memory.decRef(WasmI32.fromGrain(length))
146
157
  ret
147
158
  }
148
159
 
149
160
  /**
150
- * Gets a signed 16-bit integer starting at the given byte index.
151
- * @param i: Number - The byte index
152
- * @param b: Bytes - The byte sequence
153
- * @returns Int32
161
+ * Creates a new byte sequence that contains the same bytes as the input byte sequence.
162
+ *
163
+ * @param bytes: The byte sequence to copy
164
+ * @returns The new byte sequence
165
+ *
166
+ * @since v0.3.2
154
167
  */
155
168
  @disableGC
156
- export let rec getInt16S = (i: Number, b: Bytes) => {
169
+ export let rec copy = (b: Bytes) => {
157
170
  let (+) = WasmI32.add
158
- let ptr = WasmI32.fromGrain(b)
159
- let size = getSize(ptr)
160
- let offset = coerceNumberToWasmI32(i)
161
- checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
162
- let n = WasmI32.load16S(ptr + offset, _VALUE_OFFSET)
163
- let ret = Conv.toInt32(n)
164
- Memory.decRef(WasmI32.fromGrain(i))
171
+ let src = WasmI32.fromGrain(b)
172
+ let size = getSize(src)
173
+ let dst = allocateBytes(size)
174
+ Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
175
+ let ret = WasmI32.toGrain(dst): (Bytes)
165
176
  Memory.decRef(WasmI32.fromGrain(b))
166
- Memory.decRef(WasmI32.fromGrain(getInt16S))
177
+ Memory.decRef(WasmI32.fromGrain(copy))
167
178
  ret
168
179
  }
169
180
 
170
181
  /**
171
- * Gets an unsigned 16-bit integer starting at the given byte index.
172
- * @param i: Number - The byte index
173
- * @param b: Bytes - The byte sequence
174
- * @returns Int32
182
+ * Returns a copy of a subset of the input byte sequence.
183
+ *
184
+ * @param start: The start index
185
+ * @param length: The number of bytes to include after the starting index
186
+ * @param bytes: The byte sequence to copy from
187
+ * @returns A byte sequence with of the copied bytes
188
+ *
189
+ * @since v0.3.2
175
190
  */
176
191
  @disableGC
177
- export let rec getInt16U = (i: Number, b: Bytes) => {
192
+ export let rec slice = (start: Number, length: Number, bytes: Bytes) => {
193
+ let (>) = WasmI32.gtS
178
194
  let (+) = WasmI32.add
179
- let ptr = WasmI32.fromGrain(b)
180
- let size = getSize(ptr)
181
- let offset = coerceNumberToWasmI32(i)
182
- checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
183
- let n = WasmI32.load16U(ptr + offset, _VALUE_OFFSET)
184
- let ret = Conv.toInt32(n)
185
- Memory.decRef(WasmI32.fromGrain(i))
186
- Memory.decRef(WasmI32.fromGrain(b))
187
- Memory.decRef(WasmI32.fromGrain(getInt16U))
195
+ let src = WasmI32.fromGrain(bytes)
196
+ let size = getSize(src)
197
+ let iOrig = start
198
+ let lenOrig = length
199
+ let start = coerceNumberToWasmI32(start)
200
+ let length = coerceNumberToWasmI32(length)
201
+ if (start + length > size) {
202
+ throw Exception.InvalidArgument(
203
+ "The given index and length do not specify a valid range of bytes",
204
+ )
205
+ }
206
+ let dst = allocateBytes(length)
207
+ let offset = start
208
+ Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET + start, length)
209
+ let ret = WasmI32.toGrain(dst): (Bytes)
210
+ Memory.decRef(WasmI32.fromGrain(iOrig))
211
+ Memory.decRef(WasmI32.fromGrain(lenOrig))
212
+ Memory.decRef(WasmI32.fromGrain(bytes))
213
+ Memory.decRef(WasmI32.fromGrain(slice))
188
214
  ret
189
215
  }
190
216
 
191
217
  /**
192
- * Sets a signed 32-bit integer starting at the given byte index.
193
- * @param i: Number - The byte index
194
- * @param v: Int32 - The value to set
195
- * @param b: Bytes - The byte sequence
196
- * @returns Void
218
+ * Returns a copy of a byte sequence with bytes added or removed from the beginning and/or end.
219
+ *
220
+ * A positive number represents bytes to add, while a negative number represents bytes to remove.
221
+ *
222
+ * @param left: The number of uninitialized bytes to prepend
223
+ * @param right: The number of uninitialized bytes to append
224
+ * @param bytes: The byte sequence get a subset of bytes from
225
+ * @returns A resized byte sequence
226
+ *
227
+ * @since v0.3.2
197
228
  */
198
229
  @disableGC
199
- export let rec setInt32 = (i: Number, v: Int32, b: Bytes) => {
230
+ export let rec resize = (left: Number, right: Number, bytes: Bytes) => {
231
+ let (<) = WasmI32.ltS
232
+ let (>) = WasmI32.gtS
200
233
  let (+) = WasmI32.add
201
- let ptr = WasmI32.fromGrain(b)
202
- let size = getSize(ptr)
203
- let offset = coerceNumberToWasmI32(i)
204
- checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
205
- let value = Conv.fromInt32(v)
206
- let ret = WasmI32.store(ptr + offset, value, _VALUE_OFFSET)
207
- Memory.decRef(WasmI32.fromGrain(i))
208
- Memory.decRef(WasmI32.fromGrain(v))
209
- Memory.decRef(WasmI32.fromGrain(b))
210
- Memory.decRef(WasmI32.fromGrain(setInt32))
234
+ let (-) = WasmI32.sub
235
+ let (*) = WasmI32.mul
236
+ let src = WasmI32.fromGrain(bytes)
237
+ let size = getSize(src)
238
+ let leftOrig = left
239
+ let rightOrig = right
240
+ let left = coerceNumberToWasmI32(left)
241
+ let right = coerceNumberToWasmI32(right)
242
+ let newSize = size + left + right
243
+ if (newSize < 0n) {
244
+ throw Exception.InvalidArgument("The resulting length is less than 0")
245
+ }
246
+ let dst = allocateBytes(newSize)
247
+ let mut srcOffset = 0n
248
+ let mut dstOffset = 0n
249
+ if (left < 0n) {
250
+ srcOffset = left * -1n
251
+ dstOffset = 0n
252
+ }
253
+ if (left > 0n) {
254
+ srcOffset = 0n
255
+ dstOffset = left
256
+ }
257
+ let len = if (right < 0n) {
258
+ size + right - srcOffset
259
+ } else {
260
+ size - srcOffset
261
+ }
262
+ if (len > 0n) {
263
+ Memory.copy(
264
+ dst + _VALUE_OFFSET + dstOffset,
265
+ src + _VALUE_OFFSET + srcOffset,
266
+ len,
267
+ )
268
+ }
269
+ let ret = WasmI32.toGrain(dst): (Bytes)
270
+ Memory.decRef(WasmI32.fromGrain(leftOrig))
271
+ Memory.decRef(WasmI32.fromGrain(rightOrig))
272
+ Memory.decRef(WasmI32.fromGrain(bytes))
273
+ Memory.decRef(WasmI32.fromGrain(resize))
211
274
  ret
212
275
  }
213
276
 
214
277
  /**
215
- * Gets a signed 32-bit integer starting at the given byte index.
216
- * @param i: Number - The byte index
217
- * @param b: Bytes - The byte sequence
218
- * @returns Int32
278
+ * Copies a range of bytes from a source byte sequence to a given location
279
+ * in a destination byte sequence.
280
+ *
281
+ * @param srcIndex: The starting index to copy bytes from
282
+ * @param dstIndex: The starting index to copy bytes into
283
+ * @param length: The amount of bytes to copy from the source buffer
284
+ * @param src: The source byte sequence
285
+ * @param dst: The destination byte sequence
286
+ *
287
+ * @since v0.3.2
219
288
  */
220
289
  @disableGC
221
- export let rec getInt32 = (i: Number, b: Bytes) => {
290
+ export let rec move =
291
+ (
292
+ srcIndex: Number,
293
+ dstIndex: Number,
294
+ length: Number,
295
+ src: Bytes,
296
+ dst: Bytes,
297
+ ) => {
298
+ let (>) = WasmI32.gtS
222
299
  let (+) = WasmI32.add
223
- let ptr = WasmI32.fromGrain(b)
224
- let size = getSize(ptr)
225
- let offset = coerceNumberToWasmI32(i)
226
- checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
227
- let n = WasmI32.load(ptr + offset, _VALUE_OFFSET)
228
- let ret = Conv.toInt32(n)
229
- Memory.decRef(WasmI32.fromGrain(i))
230
- Memory.decRef(WasmI32.fromGrain(b))
231
- Memory.decRef(WasmI32.fromGrain(getInt32))
300
+ let srcIndexOrig = srcIndex
301
+ let dstIndexOrig = dstIndex
302
+ let lenthOrig = length
303
+ let src = WasmI32.fromGrain(src)
304
+ let srcSize = getSize(src)
305
+ let srcIndex = coerceNumberToWasmI32(srcIndex)
306
+ let dst = WasmI32.fromGrain(dst)
307
+ let dstSize = getSize(dst)
308
+ let dstIndex = coerceNumberToWasmI32(dstIndex)
309
+ let length = coerceNumberToWasmI32(length)
310
+ if (srcIndex + length > srcSize) {
311
+ throw Exception.InvalidArgument("Invalid source bytes range")
312
+ }
313
+ if (dstIndex + length > dstSize) {
314
+ throw Exception.InvalidArgument("Invalid destination bytes range")
315
+ }
316
+ let end = srcIndex + length
317
+ let ret = Memory.copy(
318
+ dst + _VALUE_OFFSET + dstIndex,
319
+ src + _VALUE_OFFSET + srcIndex,
320
+ length,
321
+ )
322
+
323
+ Memory.decRef(WasmI32.fromGrain(srcIndexOrig))
324
+ Memory.decRef(WasmI32.fromGrain(dstIndexOrig))
325
+ Memory.decRef(WasmI32.fromGrain(lenthOrig))
326
+ Memory.decRef(WasmI32.fromGrain(src))
327
+ Memory.decRef(WasmI32.fromGrain(dst))
328
+ Memory.decRef(WasmI32.fromGrain(move))
232
329
  ret
233
330
  }
234
331
 
235
332
  /**
236
- * Sets a 32-bit float starting at the given byte index.
237
- * @param i: Number - The byte index
238
- * @param v: Float32 - The value to set
239
- * @param b: Bytes - The byte sequence
240
- * @returns Void
333
+ * Creates a new byte sequence that contains the bytes of both byte sequences.
334
+ *
335
+ * @param bytes1: The beginning byte sequence
336
+ * @param bytes2: The ending byte sequence
337
+ * @returns The new byte sequence
338
+ *
339
+ * @since v0.3.2
241
340
  */
242
- @disableGC
243
- export let rec setFloat32 = (i: Number, v: Float32, b: Bytes) => {
244
- let (+) = WasmI32.add
245
- let ptr = WasmI32.fromGrain(b)
246
- let size = getSize(ptr)
247
- let offset = coerceNumberToWasmI32(i)
248
- checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
249
- let value = Conv.fromFloat32(v)
250
- let ret = WasmF32.store(ptr + offset, value, _VALUE_OFFSET)
251
- Memory.decRef(WasmI32.fromGrain(i))
252
- Memory.decRef(WasmI32.fromGrain(v))
253
- Memory.decRef(WasmI32.fromGrain(b))
254
- Memory.decRef(WasmI32.fromGrain(setFloat32))
341
+ export let concat = (bytes1: Bytes, bytes2: Bytes) => {
342
+ let len1 = length(bytes1)
343
+ let len2 = length(bytes2)
344
+ let ret = resize(0, len2, bytes1)
345
+ move(0, len1, len2, bytes2, ret)
255
346
  ret
256
347
  }
257
348
 
258
349
  /**
259
- * Gets a 32-bit float starting at the given byte index.
260
- * @param i: Number - The byte index
261
- * @param b: Bytes - The byte sequence
262
- * @returns Float32
350
+ * Replaces all bytes in a byte sequnce with the new value provided.
351
+ *
352
+ * @param value: The value replacing each byte
353
+ * @param bytes: The byte sequence to update
354
+ *
355
+ * @since v0.3.2
263
356
  */
264
357
  @disableGC
265
- export let rec getFloat32 = (i: Number, b: Bytes) => {
358
+ export let rec fill = (value: Int32, bytes: Bytes) => {
266
359
  let (+) = WasmI32.add
267
- let ptr = WasmI32.fromGrain(b)
268
- let size = getSize(ptr)
269
- let offset = coerceNumberToWasmI32(i)
270
- checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
271
- let n = WasmF32.load(ptr + offset, _VALUE_OFFSET)
272
- let ret = Conv.toFloat32(n)
273
- Memory.decRef(WasmI32.fromGrain(i))
274
- Memory.decRef(WasmI32.fromGrain(b))
275
- Memory.decRef(WasmI32.fromGrain(getFloat32))
360
+ let src = WasmI32.fromGrain(bytes)
361
+ let size = getSize(src)
362
+ let v = Conv.fromInt32(value)
363
+ let ret = Memory.fill(src + _VALUE_OFFSET, v, size)
364
+ Memory.decRef(WasmI32.fromGrain(value))
365
+ Memory.decRef(WasmI32.fromGrain(bytes))
366
+ Memory.decRef(WasmI32.fromGrain(fill))
276
367
  ret
277
368
  }
278
369
 
279
370
  /**
280
- * Sets a signed 64-bit integer starting at the given byte index.
281
- * @param i: Number - The byte index
282
- * @param v: Int64 - The value to set
283
- * @param b: Bytes - The byte sequence
284
- * @returns Void
371
+ * @section Binary operations on integers: Functions for encoding and decoding integers stored in a byte sequence.
372
+ */
373
+
374
+ /**
375
+ * Gets a signed 8-bit integer starting at the given byte index.
376
+ *
377
+ * @param index: The byte index to access
378
+ * @param bytes: The byte sequence to access
379
+ * @returns A 32-bit integer representing a signed 8-bit integer that starts at the given index
380
+ *
381
+ * @since v0.3.2
285
382
  */
286
383
  @disableGC
287
- export let rec setInt64 = (i: Number, v: Int64, b: Bytes) => {
384
+ export let rec getInt8S = (index: Number, bytes: Bytes) => {
288
385
  let (+) = WasmI32.add
289
- let ptr = WasmI32.fromGrain(b)
386
+ let ptr = WasmI32.fromGrain(bytes)
290
387
  let size = getSize(ptr)
291
- let offset = coerceNumberToWasmI32(i)
292
- checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, size)
293
- let value = Conv.fromInt64(v)
294
- let ret = WasmI64.store(ptr + offset, value, _VALUE_OFFSET)
295
- Memory.decRef(WasmI32.fromGrain(i))
296
- Memory.decRef(WasmI32.fromGrain(v))
297
- Memory.decRef(WasmI32.fromGrain(b))
298
- Memory.decRef(WasmI32.fromGrain(setInt64))
388
+ let offset = coerceNumberToWasmI32(index)
389
+ checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
390
+ let n = WasmI32.load8S(ptr + offset, _VALUE_OFFSET)
391
+ let ret = Conv.toInt32(n)
392
+ Memory.decRef(WasmI32.fromGrain(index))
393
+ Memory.decRef(WasmI32.fromGrain(bytes))
394
+ Memory.decRef(WasmI32.fromGrain(getInt8S))
299
395
  ret
300
396
  }
301
397
 
302
398
  /**
303
- * Gets a signed 64-bit integer starting at the given byte index.
304
- * @param i: Number - The byte index
305
- * @param b: Bytes - The byte sequence
306
- * @returns Int64
399
+ * Gets an unsigned 8-bit integer starting at the given byte index.
400
+ *
401
+ * @param index: The byte index to access
402
+ * @param bytes: The byte sequence to access
403
+ * @returns A 32-bit integer representing an unsigned 8-bit integer that starts at the given index
404
+ *
405
+ * @since v0.3.2
307
406
  */
308
407
  @disableGC
309
- export let rec getInt64 = (i: Number, b: Bytes) => {
408
+ export let rec getInt8U = (index: Number, bytes: Bytes) => {
310
409
  let (+) = WasmI32.add
311
- let ptr = WasmI32.fromGrain(b)
410
+ let ptr = WasmI32.fromGrain(bytes)
312
411
  let size = getSize(ptr)
313
- let offset = coerceNumberToWasmI32(i)
314
- checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, size)
315
- let n = WasmI64.load(ptr + offset, _VALUE_OFFSET)
316
- let ret = Conv.toInt64(n)
317
- Memory.decRef(WasmI32.fromGrain(i))
318
- Memory.decRef(WasmI32.fromGrain(b))
319
- Memory.decRef(WasmI32.fromGrain(getInt64))
412
+ let offset = coerceNumberToWasmI32(index)
413
+ checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
414
+ let n = WasmI32.load8U(ptr + offset, _VALUE_OFFSET)
415
+ let ret = Conv.toInt32(n)
416
+ Memory.decRef(WasmI32.fromGrain(index))
417
+ Memory.decRef(WasmI32.fromGrain(bytes))
418
+ Memory.decRef(WasmI32.fromGrain(getInt8U))
320
419
  ret
321
420
  }
322
421
 
323
422
  /**
324
- * Sets a 64-bit float starting at the given byte index.
325
- * @param i: Number - The byte index
326
- * @param v: Float64 - The value to set
327
- * @param b: Bytes - The byte sequence
328
- * @returns Void
423
+ * Sets a signed 8-bit integer starting at the given byte index.
424
+ *
425
+ * @param index: The byte index to update
426
+ * @param value: The value to set
427
+ * @param bytes: The byte sequence to mutate
428
+ *
429
+ * @since v0.3.2
329
430
  */
330
431
  @disableGC
331
- export let rec setFloat64 = (i: Number, v: Float64, b: Bytes) => {
432
+ export let rec setInt8 = (index: Number, value: Int32, bytes: Bytes) => {
332
433
  let (+) = WasmI32.add
333
- let ptr = WasmI32.fromGrain(b)
434
+ let ptr = WasmI32.fromGrain(bytes)
334
435
  let size = getSize(ptr)
335
- let offset = coerceNumberToWasmI32(i)
336
- checkIndexIsInBounds(offset, _FLOAT64_BYTE_SIZE, size)
337
- let value = Conv.fromFloat64(v)
338
- let ret = WasmF64.store(ptr + offset, value, _VALUE_OFFSET)
339
- Memory.decRef(WasmI32.fromGrain(i))
340
- Memory.decRef(WasmI32.fromGrain(v))
341
- Memory.decRef(WasmI32.fromGrain(b))
342
- Memory.decRef(WasmI32.fromGrain(setFloat64))
436
+ let offset = coerceNumberToWasmI32(index)
437
+ checkIndexIsInBounds(offset, _INT8_BYTE_SIZE, size)
438
+ let v = Conv.fromInt32(value)
439
+ let ret = WasmI32.store8(ptr + offset, v, _VALUE_OFFSET)
440
+ Memory.decRef(WasmI32.fromGrain(index))
441
+ Memory.decRef(WasmI32.fromGrain(value))
442
+ Memory.decRef(WasmI32.fromGrain(bytes))
443
+ Memory.decRef(WasmI32.fromGrain(setInt8))
343
444
  ret
344
445
  }
345
446
 
346
447
  /**
347
- * Gets a 64-bit float starting at the given byte index.
348
- * @param i: Number - The byte index
349
- * @param b: Bytes - The byte sequence
350
- * @returns Float64
448
+ * Gets a signed 16-bit integer starting at the given byte index.
449
+ *
450
+ * @param index: The byte index to access
451
+ * @param bytes: The byte sequence to access
452
+ * @returns A 32-bit integer representing a signed 16-bit integer that starts at the given index
453
+ *
454
+ * @since v0.3.2
351
455
  */
352
456
  @disableGC
353
- export let rec getFloat64 = (i: Number, b: Bytes) => {
457
+ export let rec getInt16S = (index: Number, bytes: Bytes) => {
354
458
  let (+) = WasmI32.add
355
- let ptr = WasmI32.fromGrain(b)
459
+ let ptr = WasmI32.fromGrain(bytes)
356
460
  let size = getSize(ptr)
357
- let offset = coerceNumberToWasmI32(i)
358
- checkIndexIsInBounds(offset, _FLOAT64_BYTE_SIZE, size)
359
- let n = WasmF64.load(ptr + offset, _VALUE_OFFSET)
360
- let ret = Conv.toFloat64(n)
361
- Memory.decRef(WasmI32.fromGrain(i))
362
- Memory.decRef(WasmI32.fromGrain(b))
363
- Memory.decRef(WasmI32.fromGrain(getFloat64))
461
+ let offset = coerceNumberToWasmI32(index)
462
+ checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
463
+ let n = WasmI32.load16S(ptr + offset, _VALUE_OFFSET)
464
+ let ret = Conv.toInt32(n)
465
+ Memory.decRef(WasmI32.fromGrain(index))
466
+ Memory.decRef(WasmI32.fromGrain(bytes))
467
+ Memory.decRef(WasmI32.fromGrain(getInt16S))
364
468
  ret
365
469
  }
366
470
 
367
471
  /**
368
- * Return a new byte sequence that contains the same bytes as the argument.
369
- * @param b: Bytes - The byte sequence to copy
370
- * @returns Bytes
472
+ * Gets an unsigned 16-bit integer starting at the given byte index.
473
+ *
474
+ * @param index: The byte index to access
475
+ * @param bytes: The byte sequence to access
476
+ * @returns A 32-bit integer representing an unsigned 16-bit integer that starts at the given index
477
+ *
478
+ * @since v0.3.2
371
479
  */
372
480
  @disableGC
373
- export let rec copy = (b: Bytes) => {
481
+ export let rec getInt16U = (index: Number, bytes: Bytes) => {
374
482
  let (+) = WasmI32.add
375
- let src = WasmI32.fromGrain(b)
376
- let size = getSize(src)
377
- let dst = allocateBytes(size)
378
- Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
379
- let ret = WasmI32.toGrain(dst): Bytes
380
- Memory.decRef(WasmI32.fromGrain(b))
381
- Memory.decRef(WasmI32.fromGrain(copy))
483
+ let ptr = WasmI32.fromGrain(bytes)
484
+ let size = getSize(ptr)
485
+ let offset = coerceNumberToWasmI32(index)
486
+ checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
487
+ let n = WasmI32.load16U(ptr + offset, _VALUE_OFFSET)
488
+ let ret = Conv.toInt32(n)
489
+ Memory.decRef(WasmI32.fromGrain(index))
490
+ Memory.decRef(WasmI32.fromGrain(bytes))
491
+ Memory.decRef(WasmI32.fromGrain(getInt16U))
382
492
  ret
383
493
  }
384
494
 
385
495
  /**
386
- * Returns a new byte sequence containing a subset of the original byte sequence.
387
- * @param i: Number - The start position to copy from
388
- * @param len: Number - The number of bytes to copy
389
- * @param b: Bytes - The byte sequence get a subset of bytes from
390
- * @returns Bytes
496
+ * Sets a signed 16-bit integer starting at the given byte index.
497
+ *
498
+ * @param index: The byte index to update
499
+ * @param value: The value to set
500
+ * @param bytes: The byte sequence to mutate
501
+ *
502
+ * @since v0.3.2
391
503
  */
392
504
  @disableGC
393
- export let rec slice = (i: Number, len: Number, b: Bytes) => {
394
- let (>) = WasmI32.gtS
505
+ export let rec setInt16 = (index: Number, value: Int32, bytes: Bytes) => {
395
506
  let (+) = WasmI32.add
396
- let src = WasmI32.fromGrain(b)
397
- let size = getSize(src)
398
- let iOrig = i
399
- let lenOrig = len
400
- let i = coerceNumberToWasmI32(i)
401
- let len = coerceNumberToWasmI32(len)
402
- if ((i + len) > size) {
403
- throw Exception.InvalidArgument("The given index and length do not specify a valid range of bytes")
404
- }
405
- let dst = allocateBytes(len)
406
- let offset = i
407
- Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET + i, len)
408
- let ret = WasmI32.toGrain(dst): Bytes
409
- Memory.decRef(WasmI32.fromGrain(iOrig))
410
- Memory.decRef(WasmI32.fromGrain(lenOrig))
411
- Memory.decRef(WasmI32.fromGrain(b))
412
- Memory.decRef(WasmI32.fromGrain(slice))
507
+ let ptr = WasmI32.fromGrain(bytes)
508
+ let size = getSize(ptr)
509
+ let offset = coerceNumberToWasmI32(index)
510
+ checkIndexIsInBounds(offset, _INT16_BYTE_SIZE, size)
511
+ let v = Conv.fromInt32(value)
512
+ let ret = WasmI32.store16(ptr + offset, v, _VALUE_OFFSET)
513
+ Memory.decRef(WasmI32.fromGrain(index))
514
+ Memory.decRef(WasmI32.fromGrain(value))
515
+ Memory.decRef(WasmI32.fromGrain(bytes))
516
+ Memory.decRef(WasmI32.fromGrain(setInt16))
413
517
  ret
414
518
  }
415
519
 
416
-
417
520
  /**
418
- * Add or remove bytes from the start and/or end of a byte sequence.
419
- * A positive number represents bytes to add, while a negative number represents bytes to remove.
420
- * @param left: Number - The number of uninitialized bytes to prepend
421
- * @param right: Number - The number of uninitialized bytes to append
422
- * @param b: Bytes - The byte sequence get a subset of bytes from
423
- * @returns Bytes
521
+ * Gets a signed 32-bit integer starting at the given byte index.
522
+ *
523
+ * @param index: The byte index to access
524
+ * @param bytes: The byte sequence to access
525
+ * @returns A signed 32-bit integer that starts at the given index
526
+ *
527
+ * @since v0.3.2
424
528
  */
425
529
  @disableGC
426
- export let rec resize = (left: Number, right: Number, b: Bytes) => {
427
- let (<) = WasmI32.ltS
428
- let (>) = WasmI32.gtS
530
+ export let rec getInt32 = (index: Number, bytes: Bytes) => {
429
531
  let (+) = WasmI32.add
430
- let (-) = WasmI32.sub
431
- let (*) = WasmI32.mul
432
- let src = WasmI32.fromGrain(b)
433
- let size = getSize(src)
434
- let leftOrig = left
435
- let rightOrig = right
436
- let left = coerceNumberToWasmI32(left)
437
- let right = coerceNumberToWasmI32(right)
438
- let newSize = size + left + right
439
- if (newSize < 0n) {
440
- throw Exception.InvalidArgument("The resulting length is less than 0")
441
- }
442
- let dst = allocateBytes(newSize)
443
- let mut srcOffset = 0n;
444
- let mut dstOffset = 0n;
445
- if (left < 0n) {
446
- srcOffset = left * -1n;
447
- dstOffset = 0n;
448
- }
449
- if (left > 0n) {
450
- srcOffset = 0n;
451
- dstOffset = left;
452
- }
453
- let len = if (right < 0n) {
454
- (size + right) - srcOffset
455
- } else {
456
- size - srcOffset
457
- }
458
- if (len > 0n) {
459
- Memory.copy(dst + _VALUE_OFFSET + dstOffset, src + _VALUE_OFFSET + srcOffset, len)
460
- }
461
- let ret = WasmI32.toGrain(dst): Bytes
462
- Memory.decRef(WasmI32.fromGrain(leftOrig))
463
- Memory.decRef(WasmI32.fromGrain(rightOrig))
464
- Memory.decRef(WasmI32.fromGrain(b))
465
- Memory.decRef(WasmI32.fromGrain(resize))
532
+ let ptr = WasmI32.fromGrain(bytes)
533
+ let size = getSize(ptr)
534
+ let offset = coerceNumberToWasmI32(index)
535
+ checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
536
+ let n = WasmI32.load(ptr + offset, _VALUE_OFFSET)
537
+ let ret = Conv.toInt32(n)
538
+ Memory.decRef(WasmI32.fromGrain(index))
539
+ Memory.decRef(WasmI32.fromGrain(bytes))
540
+ Memory.decRef(WasmI32.fromGrain(getInt32))
466
541
  ret
467
542
  }
468
543
 
469
544
  /**
470
- * Copies a range of bytes from a source buffer to a given location in a destination buffer.
471
- * @param srcPos: Number - The starting byte index to copy bytes from
472
- * @param dstPos: Number - The starting byte index to copy bytes into
473
- * @param len: Number - The amount of bytes to copy from the source buffer
474
- * @param src: Bytes - The source buffer
475
- * @param dst: Bytes - The destination buffer
476
- * @returns Void
545
+ * Sets a signed 32-bit integer starting at the given byte index.
546
+ *
547
+ * @param index: The byte index to update
548
+ * @param value: The value to set
549
+ * @param bytes: The byte sequence to mutate
550
+ *
551
+ * @since v0.3.2
477
552
  */
478
553
  @disableGC
479
- export let rec move = (srcPos: Number, dstPos: Number, len: Number, src: Bytes, dst: Bytes) => {
480
- let (>) = WasmI32.gtS
554
+ export let rec setInt32 = (index: Number, value: Int32, bytes: Bytes) => {
481
555
  let (+) = WasmI32.add
482
- let srcPosOrig = srcPos
483
- let dstPosOrig = dstPos
484
- let lenOrig = len
485
- let src = WasmI32.fromGrain(src)
486
- let srcSize = getSize(src)
487
- let srcPos = coerceNumberToWasmI32(srcPos)
488
- let dst = WasmI32.fromGrain(dst)
489
- let dstSize = getSize(dst)
490
- let dstPos = coerceNumberToWasmI32(dstPos)
491
- let len = coerceNumberToWasmI32(len)
492
- if ((srcPos + len) > srcSize) {
493
- throw Exception.InvalidArgument("Invalid source bytes range")
494
- }
495
- if ((dstPos + len) > dstSize) {
496
- throw Exception.InvalidArgument("Invalid destination bytes range")
497
- }
498
- let end = srcPos + len
499
- let ret = Memory.copy(dst + _VALUE_OFFSET + dstPos, src + _VALUE_OFFSET + srcPos, len)
500
- Memory.decRef(WasmI32.fromGrain(srcPosOrig))
501
- Memory.decRef(WasmI32.fromGrain(dstPosOrig))
502
- Memory.decRef(WasmI32.fromGrain(lenOrig))
503
- Memory.decRef(WasmI32.fromGrain(src))
504
- Memory.decRef(WasmI32.fromGrain(dst))
505
- Memory.decRef(WasmI32.fromGrain(move))
556
+ let ptr = WasmI32.fromGrain(bytes)
557
+ let size = getSize(ptr)
558
+ let offset = coerceNumberToWasmI32(index)
559
+ checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
560
+ let v = Conv.fromInt32(value)
561
+ let ret = WasmI32.store(ptr + offset, v, _VALUE_OFFSET)
562
+ Memory.decRef(WasmI32.fromGrain(index))
563
+ Memory.decRef(WasmI32.fromGrain(value))
564
+ Memory.decRef(WasmI32.fromGrain(bytes))
565
+ Memory.decRef(WasmI32.fromGrain(setInt32))
506
566
  ret
507
567
  }
508
568
 
509
569
  /**
510
- * Get the byte length of a byte sequence.
511
- * @param b: Bytes - The byte sequence to check
512
- * @returns Number
570
+ * Gets a 32-bit float starting at the given byte index.
571
+ *
572
+ * @param index: The byte index to access
573
+ * @param bytes: The byte sequence to access
574
+ * @returns A 32-bit float that starts at the given index
575
+ *
576
+ * @since v0.3.2
513
577
  */
514
- @disableGC
515
- export let rec length = (b: Bytes) => {
516
- let b = WasmI32.fromGrain(b)
517
- let ret = tagSimpleNumber(getSize(b))
518
- Memory.decRef(WasmI32.fromGrain(b))
519
- Memory.decRef(WasmI32.fromGrain(length))
578
+ @disableGC
579
+ export let rec getFloat32 = (index: Number, bytes: Bytes) => {
580
+ let (+) = WasmI32.add
581
+ let ptr = WasmI32.fromGrain(bytes)
582
+ let size = getSize(ptr)
583
+ let offset = coerceNumberToWasmI32(index)
584
+ checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
585
+ let n = WasmF32.load(ptr + offset, _VALUE_OFFSET)
586
+ let ret = Conv.toFloat32(n)
587
+ Memory.decRef(WasmI32.fromGrain(index))
588
+ Memory.decRef(WasmI32.fromGrain(bytes))
589
+ Memory.decRef(WasmI32.fromGrain(getFloat32))
520
590
  ret
521
591
  }
522
592
 
523
593
  /**
524
- * Creates a new byte sequence that contains the bytes of both buffers a and b.
525
- * @param a: Bytes - The buffer to be copied first
526
- * @param b: Bytes - The buffer to be copied last
527
- * @returns Bytes
594
+ * Sets a 32-bit float starting at the given byte index.
595
+ *
596
+ * @param index: The byte index to update
597
+ * @param value: The value to set
598
+ * @param bytes: The byte sequence to mutate
599
+ *
600
+ * @since v0.3.2
528
601
  */
529
- export let concat = (a: Bytes, b: Bytes) => {
530
- let alen = length(a)
531
- let blen = length(b)
532
- let c = resize(0, blen, a)
533
- move(0, alen, blen, b, c)
534
- c
602
+ @disableGC
603
+ export let rec setFloat32 = (index: Number, value: Float32, bytes: Bytes) => {
604
+ let (+) = WasmI32.add
605
+ let ptr = WasmI32.fromGrain(bytes)
606
+ let size = getSize(ptr)
607
+ let offset = coerceNumberToWasmI32(index)
608
+ checkIndexIsInBounds(offset, _INT32_BYTE_SIZE, size)
609
+ let v = Conv.fromFloat32(value)
610
+ let ret = WasmF32.store(ptr + offset, v, _VALUE_OFFSET)
611
+ Memory.decRef(WasmI32.fromGrain(index))
612
+ Memory.decRef(WasmI32.fromGrain(value))
613
+ Memory.decRef(WasmI32.fromGrain(bytes))
614
+ Memory.decRef(WasmI32.fromGrain(setFloat32))
615
+ ret
535
616
  }
536
617
 
537
618
  /**
538
- * Creates a new String from a byte sequence.
539
- * @param b: Bytes - The source buffer
540
- * @returns String
619
+ * Gets a signed 64-bit integer starting at the given byte index.
620
+ *
621
+ * @param index: The byte index to access
622
+ * @param bytes: The byte sequence to access
623
+ * @returns A signed 64-bit integer that starts at the given index
624
+ *
625
+ * @since v0.3.2
541
626
  */
542
627
  @disableGC
543
- export let rec toString = (b: Bytes) => {
628
+ export let rec getInt64 = (index: Number, bytes: Bytes) => {
544
629
  let (+) = WasmI32.add
545
- let src = WasmI32.fromGrain(b)
546
- let size = getSize(src)
547
- let dst = allocateString(size)
548
- Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
549
- let ret = WasmI32.toGrain(dst): String
550
- Memory.decRef(WasmI32.fromGrain(b))
551
- Memory.decRef(WasmI32.fromGrain(toString))
630
+ let ptr = WasmI32.fromGrain(bytes)
631
+ let size = getSize(ptr)
632
+ let offset = coerceNumberToWasmI32(index)
633
+ checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, size)
634
+ let n = WasmI64.load(ptr + offset, _VALUE_OFFSET)
635
+ let ret = Conv.toInt64(n)
636
+ Memory.decRef(WasmI32.fromGrain(index))
637
+ Memory.decRef(WasmI32.fromGrain(bytes))
638
+ Memory.decRef(WasmI32.fromGrain(getInt64))
552
639
  ret
553
640
  }
554
641
 
555
642
  /**
556
- * Creates a new byte sequence from a String.
557
- * @param str: String - The String to copy into a byte sequence
558
- * @returns Bytes
643
+ * Sets a signed 64-bit integer starting at the given byte index.
644
+ *
645
+ * @param index: The byte index to update
646
+ * @param value: The value to set
647
+ * @param bytes: The byte sequence to mutate
648
+ *
649
+ * @since v0.3.2
559
650
  */
560
651
  @disableGC
561
- export let rec fromString = (str: String) => {
652
+ export let rec setInt64 = (index: Number, value: Int64, bytes: Bytes) => {
562
653
  let (+) = WasmI32.add
563
- let src = WasmI32.fromGrain(str)
564
- let size = getSize(src)
565
- let dst = allocateBytes(size)
566
- Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
567
- let ret = WasmI32.toGrain(dst): Bytes
568
- Memory.decRef(WasmI32.fromGrain(str))
569
- Memory.decRef(WasmI32.fromGrain(fromString))
654
+ let ptr = WasmI32.fromGrain(bytes)
655
+ let size = getSize(ptr)
656
+ let offset = coerceNumberToWasmI32(index)
657
+ checkIndexIsInBounds(offset, _INT64_BYTE_SIZE, size)
658
+ let v = Conv.fromInt64(value)
659
+ let ret = WasmI64.store(ptr + offset, v, _VALUE_OFFSET)
660
+ Memory.decRef(WasmI32.fromGrain(index))
661
+ Memory.decRef(WasmI32.fromGrain(value))
662
+ Memory.decRef(WasmI32.fromGrain(bytes))
663
+ Memory.decRef(WasmI32.fromGrain(setInt64))
570
664
  ret
571
665
  }
572
666
 
573
667
  /**
574
- * Fills a byte sequence with a given value.
575
- * @param v: Int32 - The value to fill the byte sequence with
576
- * @param b: Bytes - The byte sequence to fill
668
+ * Gets a 64-bit float starting at the given byte index.
669
+ *
670
+ * @param index: The byte index to access
671
+ * @param bytes: The byte sequence to access
672
+ * @returns A 64-bit float that starts at the given index
673
+ *
674
+ * @since v0.3.2
577
675
  */
578
676
  @disableGC
579
- export let rec fill = (v: Int32, b: Bytes) => {
677
+ export let rec getFloat64 = (index: Number, bytes: Bytes) => {
580
678
  let (+) = WasmI32.add
581
- let src = WasmI32.fromGrain(b)
582
- let size = getSize(src)
583
- let value = Conv.fromInt32(v)
584
- let ret = Memory.fill(src + _VALUE_OFFSET, value, size)
585
- Memory.decRef(WasmI32.fromGrain(v))
586
- Memory.decRef(WasmI32.fromGrain(b))
587
- Memory.decRef(WasmI32.fromGrain(fill))
679
+ let ptr = WasmI32.fromGrain(bytes)
680
+ let size = getSize(ptr)
681
+ let offset = coerceNumberToWasmI32(index)
682
+ checkIndexIsInBounds(offset, _FLOAT64_BYTE_SIZE, size)
683
+ let n = WasmF64.load(ptr + offset, _VALUE_OFFSET)
684
+ let ret = Conv.toFloat64(n)
685
+ Memory.decRef(WasmI32.fromGrain(index))
686
+ Memory.decRef(WasmI32.fromGrain(bytes))
687
+ Memory.decRef(WasmI32.fromGrain(getFloat64))
588
688
  ret
589
689
  }
590
690
 
591
691
  /**
592
- * Make a new byte sequence of n-bytes size.
593
- * @param n: Number - The number of bytes to store
594
- * @returns Bytes
692
+ * Sets a 64-bit float starting at the given byte index.
693
+ *
694
+ * @param index: The byte index to update
695
+ * @param value: The value to set
696
+ * @param bytes: The byte sequence to mutate
697
+ *
698
+ * @since v0.3.2
595
699
  */
596
700
  @disableGC
597
- export let rec make = (n: Number) => {
598
- let bytes = allocateBytes(coerceNumberToWasmI32(n))
599
- let ret = WasmI32.toGrain(bytes): Bytes
600
- Memory.decRef(WasmI32.fromGrain(n))
601
- Memory.decRef(WasmI32.fromGrain(make))
701
+ export let rec setFloat64 = (index: Number, value: Float64, bytes: Bytes) => {
702
+ let (+) = WasmI32.add
703
+ let ptr = WasmI32.fromGrain(bytes)
704
+ let size = getSize(ptr)
705
+ let offset = coerceNumberToWasmI32(index)
706
+ checkIndexIsInBounds(offset, _FLOAT64_BYTE_SIZE, size)
707
+ let v = Conv.fromFloat64(value)
708
+ let ret = WasmF64.store(ptr + offset, v, _VALUE_OFFSET)
709
+ Memory.decRef(WasmI32.fromGrain(index))
710
+ Memory.decRef(WasmI32.fromGrain(value))
711
+ Memory.decRef(WasmI32.fromGrain(bytes))
712
+ Memory.decRef(WasmI32.fromGrain(setFloat64))
602
713
  ret
603
714
  }
604
-
605
- /**
606
- * An empty byte sequence
607
- */
608
- export let empty = make(0)
609
-