@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/CHANGELOG.md +65 -0
- package/LICENSE +21 -0
- package/README.md +34 -0
- package/array.gr +136 -44
- package/array.md +97 -21
- package/buffer.gr +495 -424
- package/buffer.md +850 -0
- package/bytes.gr +512 -407
- package/bytes.md +621 -0
- package/char.gr +11 -3
- package/hash.gr +26 -3
- package/hash.md +44 -0
- package/list.gr +54 -0
- package/number.gr +24 -6
- package/number.md +49 -17
- package/option.gr +244 -37
- package/option.md +579 -0
- package/package.json +33 -29
- package/queue.gr +98 -29
- package/queue.md +191 -0
- package/range.md +1 -1
- package/regex.gr +3055 -0
- package/regex.md +449 -0
- package/result.gr +216 -70
- package/result.md +446 -0
- package/runtime/gc.gr +2 -2
- package/runtime/string.gr +56 -24
- package/runtime/stringUtils.gr +172 -0
- package/runtime/unsafe/conv.gr +43 -0
- package/set.gr +172 -5
- package/set.md +502 -0
- package/stack.md +143 -0
- package/string.gr +444 -230
- package/string.md +815 -0
- package/sys/file.gr +3 -2
- package/sys/file.md +2 -2
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 {
|
|
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 (
|
|
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 =
|
|
71
|
+
let getSize = ptr => WasmI32.load(ptr, _SIZE_OFFSET)
|
|
60
72
|
|
|
61
73
|
/**
|
|
62
|
-
*
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
*
|
|
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
|
|
70
|
-
let
|
|
71
|
-
let
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
88
|
-
|
|
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
|
|
110
|
+
export let rec fromString = (string: String) => {
|
|
92
111
|
let (+) = WasmI32.add
|
|
93
|
-
let
|
|
94
|
-
let size = getSize(
|
|
95
|
-
let
|
|
96
|
-
|
|
97
|
-
let
|
|
98
|
-
|
|
99
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* @param
|
|
109
|
-
* @returns
|
|
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
|
|
131
|
+
export let rec toString = (bytes: Bytes) => {
|
|
113
132
|
let (+) = WasmI32.add
|
|
114
|
-
let
|
|
115
|
-
let size = getSize(
|
|
116
|
-
let
|
|
117
|
-
|
|
118
|
-
let
|
|
119
|
-
|
|
120
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* @param
|
|
130
|
-
* @
|
|
131
|
-
*
|
|
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
|
|
135
|
-
let
|
|
136
|
-
let
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* @param
|
|
153
|
-
* @returns
|
|
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
|
|
169
|
+
export let rec copy = (b: Bytes) => {
|
|
157
170
|
let (+) = WasmI32.add
|
|
158
|
-
let
|
|
159
|
-
let size = getSize(
|
|
160
|
-
let
|
|
161
|
-
|
|
162
|
-
let
|
|
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(
|
|
177
|
+
Memory.decRef(WasmI32.fromGrain(copy))
|
|
167
178
|
ret
|
|
168
179
|
}
|
|
169
180
|
|
|
170
181
|
/**
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* @param
|
|
174
|
-
* @
|
|
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
|
|
192
|
+
export let rec slice = (start: Number, length: Number, bytes: Bytes) => {
|
|
193
|
+
let (>) = WasmI32.gtS
|
|
178
194
|
let (+) = WasmI32.add
|
|
179
|
-
let
|
|
180
|
-
let size = getSize(
|
|
181
|
-
let
|
|
182
|
-
|
|
183
|
-
let
|
|
184
|
-
let
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
* @
|
|
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
|
|
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
|
|
202
|
-
let
|
|
203
|
-
let
|
|
204
|
-
|
|
205
|
-
let
|
|
206
|
-
let
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* @
|
|
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
|
|
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
|
|
224
|
-
let
|
|
225
|
-
let
|
|
226
|
-
|
|
227
|
-
let
|
|
228
|
-
let
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
|
-
*
|
|
237
|
-
*
|
|
238
|
-
* @param
|
|
239
|
-
* @param
|
|
240
|
-
* @returns
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
let
|
|
245
|
-
let
|
|
246
|
-
|
|
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
|
-
*
|
|
260
|
-
*
|
|
261
|
-
* @param
|
|
262
|
-
* @
|
|
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
|
|
358
|
+
export let rec fill = (value: Int32, bytes: Bytes) => {
|
|
266
359
|
let (+) = WasmI32.add
|
|
267
|
-
let
|
|
268
|
-
let size = getSize(
|
|
269
|
-
let
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
-
*
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
*
|
|
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
|
|
384
|
+
export let rec getInt8S = (index: Number, bytes: Bytes) => {
|
|
288
385
|
let (+) = WasmI32.add
|
|
289
|
-
let ptr = WasmI32.fromGrain(
|
|
386
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
290
387
|
let size = getSize(ptr)
|
|
291
|
-
let offset = coerceNumberToWasmI32(
|
|
292
|
-
checkIndexIsInBounds(offset,
|
|
293
|
-
let
|
|
294
|
-
let ret =
|
|
295
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
296
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
297
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
|
304
|
-
*
|
|
305
|
-
* @param
|
|
306
|
-
* @
|
|
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
|
|
408
|
+
export let rec getInt8U = (index: Number, bytes: Bytes) => {
|
|
310
409
|
let (+) = WasmI32.add
|
|
311
|
-
let ptr = WasmI32.fromGrain(
|
|
410
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
312
411
|
let size = getSize(ptr)
|
|
313
|
-
let offset = coerceNumberToWasmI32(
|
|
314
|
-
checkIndexIsInBounds(offset,
|
|
315
|
-
let n =
|
|
316
|
-
let ret = Conv.
|
|
317
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
318
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
319
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
|
325
|
-
*
|
|
326
|
-
* @param
|
|
327
|
-
* @param
|
|
328
|
-
* @
|
|
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
|
|
432
|
+
export let rec setInt8 = (index: Number, value: Int32, bytes: Bytes) => {
|
|
332
433
|
let (+) = WasmI32.add
|
|
333
|
-
let ptr = WasmI32.fromGrain(
|
|
434
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
334
435
|
let size = getSize(ptr)
|
|
335
|
-
let offset = coerceNumberToWasmI32(
|
|
336
|
-
checkIndexIsInBounds(offset,
|
|
337
|
-
let
|
|
338
|
-
let ret =
|
|
339
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
340
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
341
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
342
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
|
348
|
-
*
|
|
349
|
-
* @param
|
|
350
|
-
* @
|
|
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
|
|
457
|
+
export let rec getInt16S = (index: Number, bytes: Bytes) => {
|
|
354
458
|
let (+) = WasmI32.add
|
|
355
|
-
let ptr = WasmI32.fromGrain(
|
|
459
|
+
let ptr = WasmI32.fromGrain(bytes)
|
|
356
460
|
let size = getSize(ptr)
|
|
357
|
-
let offset = coerceNumberToWasmI32(
|
|
358
|
-
checkIndexIsInBounds(offset,
|
|
359
|
-
let n =
|
|
360
|
-
let ret = Conv.
|
|
361
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
362
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
363
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
-
*
|
|
369
|
-
*
|
|
370
|
-
* @
|
|
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
|
|
481
|
+
export let rec getInt16U = (index: Number, bytes: Bytes) => {
|
|
374
482
|
let (+) = WasmI32.add
|
|
375
|
-
let
|
|
376
|
-
let size = getSize(
|
|
377
|
-
let
|
|
378
|
-
|
|
379
|
-
let
|
|
380
|
-
|
|
381
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
-
*
|
|
387
|
-
*
|
|
388
|
-
* @param
|
|
389
|
-
* @param
|
|
390
|
-
* @
|
|
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
|
|
394
|
-
let (>) = WasmI32.gtS
|
|
505
|
+
export let rec setInt16 = (index: Number, value: Int32, bytes: Bytes) => {
|
|
395
506
|
let (+) = WasmI32.add
|
|
396
|
-
let
|
|
397
|
-
let size = getSize(
|
|
398
|
-
let
|
|
399
|
-
|
|
400
|
-
let
|
|
401
|
-
let
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
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
|
-
*
|
|
419
|
-
*
|
|
420
|
-
* @param
|
|
421
|
-
* @param
|
|
422
|
-
* @
|
|
423
|
-
*
|
|
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
|
|
427
|
-
let (<) = WasmI32.ltS
|
|
428
|
-
let (>) = WasmI32.gtS
|
|
530
|
+
export let rec getInt32 = (index: Number, bytes: Bytes) => {
|
|
429
531
|
let (+) = WasmI32.add
|
|
430
|
-
let
|
|
431
|
-
let
|
|
432
|
-
let
|
|
433
|
-
|
|
434
|
-
let
|
|
435
|
-
let
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
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
|
-
*
|
|
471
|
-
*
|
|
472
|
-
* @param
|
|
473
|
-
* @param
|
|
474
|
-
* @param
|
|
475
|
-
*
|
|
476
|
-
* @
|
|
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
|
|
480
|
-
let (>) = WasmI32.gtS
|
|
554
|
+
export let rec setInt32 = (index: Number, value: Int32, bytes: Bytes) => {
|
|
481
555
|
let (+) = WasmI32.add
|
|
482
|
-
let
|
|
483
|
-
let
|
|
484
|
-
let
|
|
485
|
-
|
|
486
|
-
let
|
|
487
|
-
let
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
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
|
-
*
|
|
511
|
-
*
|
|
512
|
-
* @
|
|
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
|
-
|
|
515
|
-
export let rec
|
|
516
|
-
let
|
|
517
|
-
let
|
|
518
|
-
|
|
519
|
-
|
|
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
|
-
*
|
|
525
|
-
*
|
|
526
|
-
* @param
|
|
527
|
-
* @
|
|
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
|
-
|
|
530
|
-
|
|
531
|
-
let
|
|
532
|
-
let
|
|
533
|
-
|
|
534
|
-
|
|
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
|
-
*
|
|
539
|
-
*
|
|
540
|
-
* @
|
|
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
|
|
628
|
+
export let rec getInt64 = (index: Number, bytes: Bytes) => {
|
|
544
629
|
let (+) = WasmI32.add
|
|
545
|
-
let
|
|
546
|
-
let size = getSize(
|
|
547
|
-
let
|
|
548
|
-
|
|
549
|
-
let
|
|
550
|
-
|
|
551
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
-
*
|
|
557
|
-
*
|
|
558
|
-
* @
|
|
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
|
|
652
|
+
export let rec setInt64 = (index: Number, value: Int64, bytes: Bytes) => {
|
|
562
653
|
let (+) = WasmI32.add
|
|
563
|
-
let
|
|
564
|
-
let size = getSize(
|
|
565
|
-
let
|
|
566
|
-
|
|
567
|
-
let
|
|
568
|
-
|
|
569
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
-
*
|
|
575
|
-
*
|
|
576
|
-
* @param
|
|
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
|
|
677
|
+
export let rec getFloat64 = (index: Number, bytes: Bytes) => {
|
|
580
678
|
let (+) = WasmI32.add
|
|
581
|
-
let
|
|
582
|
-
let size = getSize(
|
|
583
|
-
let
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
Memory.decRef(WasmI32.fromGrain(
|
|
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
|
-
*
|
|
593
|
-
*
|
|
594
|
-
* @
|
|
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
|
|
598
|
-
let
|
|
599
|
-
let
|
|
600
|
-
|
|
601
|
-
|
|
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
|
-
|