@grain/stdlib 0.4.2 → 0.4.6
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 +52 -0
- package/LICENSE +1 -1
- package/array.gr +200 -89
- package/array.md +81 -5
- package/buffer.gr +93 -36
- package/bytes.gr +10 -10
- package/char.gr +112 -56
- package/char.md +200 -0
- package/float32.gr +120 -4
- package/float32.md +315 -0
- package/float64.gr +120 -4
- package/float64.md +315 -0
- package/hash.gr +42 -15
- package/hash.md +44 -0
- package/int32.gr +370 -75
- package/int32.md +833 -0
- package/int64.gr +370 -75
- package/int64.md +833 -0
- package/list.gr +121 -50
- package/map.gr +106 -110
- package/number.gr +37 -1
- package/number.md +66 -0
- package/option.gr +260 -53
- package/option.md +579 -0
- package/package.json +1 -1
- package/pervasives.gr +32 -20
- package/queue.gr +102 -30
- package/queue.md +191 -0
- package/range.gr +26 -26
- package/range.md +1 -1
- package/regex.md +9 -9
- package/result.gr +216 -70
- package/result.md +446 -0
- package/runtime/dataStructures.gr +28 -29
- package/runtime/debug.gr +0 -1
- package/runtime/equal.gr +37 -16
- package/runtime/exception.gr +28 -15
- package/runtime/gc.gr +33 -20
- package/runtime/malloc.gr +19 -11
- package/runtime/numberUtils.gr +208 -103
- package/runtime/numbers.gr +217 -118
- package/runtime/string.gr +98 -39
- package/runtime/stringUtils.gr +176 -0
- package/runtime/unsafe/conv.gr +10 -10
- package/runtime/unsafe/memory.gr +14 -3
- package/runtime/unsafe/printWasm.gr +4 -4
- package/runtime/unsafe/tags.gr +2 -2
- package/runtime/unsafe/wasmf32.gr +9 -2
- package/runtime/unsafe/wasmf64.gr +9 -2
- package/runtime/unsafe/wasmi32.gr +65 -47
- package/runtime/unsafe/wasmi64.gr +78 -50
- package/runtime/wasi.gr +199 -45
- package/set.gr +281 -119
- package/set.md +502 -0
- package/stack.gr +26 -26
- package/string.gr +657 -341
- package/string.md +815 -0
- package/sys/file.gr +356 -177
- package/sys/process.gr +10 -6
- package/sys/random.gr +3 -6
- package/sys/time.gr +3 -3
package/result.md
ADDED
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Result
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Utilities for working with the Result data type.
|
|
6
|
+
|
|
7
|
+
The Result type is an enum that represents the possibility of a success case (with the `Ok` variant),
|
|
8
|
+
or an error case (with the `Err` variant). Use a Result as the return type of a function that may return an error.
|
|
9
|
+
|
|
10
|
+
<details disabled>
|
|
11
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
12
|
+
No other changes yet.
|
|
13
|
+
</details>
|
|
14
|
+
|
|
15
|
+
```grain
|
|
16
|
+
import Result from "result"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```grain
|
|
20
|
+
let success = Ok((x) => 1 + x) // Creates a successful Result containing (x) => 1 + x
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```grain
|
|
24
|
+
let failure = Err("Something bad happened") // Creates an unsuccessful Result containing "Something bad happened"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Values
|
|
28
|
+
|
|
29
|
+
Functions for working with the Result data type.
|
|
30
|
+
|
|
31
|
+
### Result.**isOk**
|
|
32
|
+
|
|
33
|
+
<details disabled>
|
|
34
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
35
|
+
No other changes yet.
|
|
36
|
+
</details>
|
|
37
|
+
|
|
38
|
+
```grain
|
|
39
|
+
isOk : Result<a, b> -> Bool
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Checks if the Result is the `Ok` variant.
|
|
43
|
+
|
|
44
|
+
Parameters:
|
|
45
|
+
|
|
46
|
+
|param|type|description|
|
|
47
|
+
|-----|----|-----------|
|
|
48
|
+
|`result`|`Result<a, b>`|The result to check|
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
|
|
52
|
+
|type|description|
|
|
53
|
+
|----|-----------|
|
|
54
|
+
|`Bool`|`true` if the Result is the `Ok` variant or `false` otherwise|
|
|
55
|
+
|
|
56
|
+
### Result.**isErr**
|
|
57
|
+
|
|
58
|
+
<details disabled>
|
|
59
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
60
|
+
No other changes yet.
|
|
61
|
+
</details>
|
|
62
|
+
|
|
63
|
+
```grain
|
|
64
|
+
isErr : Result<a, b> -> Bool
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Checks if the Result is the `Err` variant.
|
|
68
|
+
|
|
69
|
+
Parameters:
|
|
70
|
+
|
|
71
|
+
|param|type|description|
|
|
72
|
+
|-----|----|-----------|
|
|
73
|
+
|`result`|`Result<a, b>`|The result to check|
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
|
|
77
|
+
|type|description|
|
|
78
|
+
|----|-----------|
|
|
79
|
+
|`Bool`|`true` if the Result is the `Err` variant or `false` otherwise|
|
|
80
|
+
|
|
81
|
+
### Result.**toOption**
|
|
82
|
+
|
|
83
|
+
<details disabled>
|
|
84
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
85
|
+
No other changes yet.
|
|
86
|
+
</details>
|
|
87
|
+
|
|
88
|
+
```grain
|
|
89
|
+
toOption : Result<a, b> -> Option<a>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Converts the Result to an Option. An error value is discarded and replaced with `None`.
|
|
93
|
+
|
|
94
|
+
Parameters:
|
|
95
|
+
|
|
96
|
+
|param|type|description|
|
|
97
|
+
|-----|----|-----------|
|
|
98
|
+
|`result`|`Result<a, b>`|The result to convert|
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
|
|
102
|
+
|type|description|
|
|
103
|
+
|----|-----------|
|
|
104
|
+
|`Option<a>`|`Some(value)` if the Result is `Ok(value)` or `None` if the Result is an `Err`|
|
|
105
|
+
|
|
106
|
+
### Result.**flatMap**
|
|
107
|
+
|
|
108
|
+
<details disabled>
|
|
109
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
110
|
+
No other changes yet.
|
|
111
|
+
</details>
|
|
112
|
+
|
|
113
|
+
```grain
|
|
114
|
+
flatMap : ((a -> Result<b, c>), Result<a, c>) -> Result<b, c>
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
If the Result is `Ok(value)`, applies the given function to the `value` to produce a new Result.
|
|
118
|
+
|
|
119
|
+
Parameters:
|
|
120
|
+
|
|
121
|
+
|param|type|description|
|
|
122
|
+
|-----|----|-----------|
|
|
123
|
+
|`fn`|`a -> Result<b, c>`|The function to call on the value of an `Ok` variant|
|
|
124
|
+
|`result`|`Result<a, c>`|The result to map|
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
|
|
128
|
+
|type|description|
|
|
129
|
+
|----|-----------|
|
|
130
|
+
|`Result<b, c>`|A new Result produced by the mapping function if the variant was `Ok` or the unmodified `Err` otherwise|
|
|
131
|
+
|
|
132
|
+
### Result.**flatMapErr**
|
|
133
|
+
|
|
134
|
+
<details disabled>
|
|
135
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
136
|
+
No other changes yet.
|
|
137
|
+
</details>
|
|
138
|
+
|
|
139
|
+
```grain
|
|
140
|
+
flatMapErr : ((a -> Result<b, c>), Result<b, a>) -> Result<b, c>
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
If the Result is an `Err(value)`, applies the given function to the `value` to produce a new Result.
|
|
144
|
+
|
|
145
|
+
Parameters:
|
|
146
|
+
|
|
147
|
+
|param|type|description|
|
|
148
|
+
|-----|----|-----------|
|
|
149
|
+
|`fn`|`a -> Result<b, c>`|The function to call on the value of an `Err` variant|
|
|
150
|
+
|`result`|`Result<b, a>`|The result to map|
|
|
151
|
+
|
|
152
|
+
Returns:
|
|
153
|
+
|
|
154
|
+
|type|description|
|
|
155
|
+
|----|-----------|
|
|
156
|
+
|`Result<b, c>`|A new Result produced by the mapping function if the variant was `Err` or the unmodified `Ok` otherwise|
|
|
157
|
+
|
|
158
|
+
### Result.**map**
|
|
159
|
+
|
|
160
|
+
<details disabled>
|
|
161
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
162
|
+
No other changes yet.
|
|
163
|
+
</details>
|
|
164
|
+
|
|
165
|
+
```grain
|
|
166
|
+
map : ((a -> b), Result<a, c>) -> Result<b, c>
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
If the Result is `Ok(value)`, applies the given function to the `value` and wraps the new value in an `Ok` variant.
|
|
170
|
+
|
|
171
|
+
Parameters:
|
|
172
|
+
|
|
173
|
+
|param|type|description|
|
|
174
|
+
|-----|----|-----------|
|
|
175
|
+
|`fn`|`a -> b`|The function to call on the value of an `Ok` variant|
|
|
176
|
+
|`result`|`Result<a, c>`|The result to map|
|
|
177
|
+
|
|
178
|
+
Returns:
|
|
179
|
+
|
|
180
|
+
|type|description|
|
|
181
|
+
|----|-----------|
|
|
182
|
+
|`Result<b, c>`|A new `Ok` variant produced by the mapping function if the variant was `Ok` or the unmodified `Err` otherwise|
|
|
183
|
+
|
|
184
|
+
### Result.**mapErr**
|
|
185
|
+
|
|
186
|
+
<details disabled>
|
|
187
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
188
|
+
No other changes yet.
|
|
189
|
+
</details>
|
|
190
|
+
|
|
191
|
+
```grain
|
|
192
|
+
mapErr : ((a -> b), Result<c, a>) -> Result<c, b>
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
If the Result is `Err(value)`, applies the given function to the `value` and wraps the new value in an `Err` variant.
|
|
196
|
+
|
|
197
|
+
Parameters:
|
|
198
|
+
|
|
199
|
+
|param|type|description|
|
|
200
|
+
|-----|----|-----------|
|
|
201
|
+
|`fn`|`a -> b`|The function to call on the value of an `Err` variant|
|
|
202
|
+
|`result`|`Result<c, a>`|The result to map|
|
|
203
|
+
|
|
204
|
+
Returns:
|
|
205
|
+
|
|
206
|
+
|type|description|
|
|
207
|
+
|----|-----------|
|
|
208
|
+
|`Result<c, b>`|A new `Err` variant produced by the mapping function if the variant was `Err` or the unmodified `Ok` otherwise|
|
|
209
|
+
|
|
210
|
+
### Result.**mapWithDefault**
|
|
211
|
+
|
|
212
|
+
<details disabled>
|
|
213
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
214
|
+
No other changes yet.
|
|
215
|
+
</details>
|
|
216
|
+
|
|
217
|
+
```grain
|
|
218
|
+
mapWithDefault : ((a -> b), b, Result<a, c>) -> b
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
If the Result is `Ok(value)`, applies the given function to the `value` to produce a new value, otherwise uses the default value.
|
|
222
|
+
Useful for unwrapping a successful Result while providing a fallback for any errors that can occur.
|
|
223
|
+
|
|
224
|
+
Parameters:
|
|
225
|
+
|
|
226
|
+
|param|type|description|
|
|
227
|
+
|-----|----|-----------|
|
|
228
|
+
|`fn`|`a -> b`|The function to call on the value of an `Ok` variant|
|
|
229
|
+
|`def`|`b`|A fallback value for an `Err` variant|
|
|
230
|
+
|`result`|`Result<a, c>`|The result to map|
|
|
231
|
+
|
|
232
|
+
Returns:
|
|
233
|
+
|
|
234
|
+
|type|description|
|
|
235
|
+
|----|-----------|
|
|
236
|
+
|`b`|The value produced by the mapping function if the result is of the `Ok` variant or the default value otherwise|
|
|
237
|
+
|
|
238
|
+
### Result.**mapWithDefaultFn**
|
|
239
|
+
|
|
240
|
+
<details disabled>
|
|
241
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
242
|
+
No other changes yet.
|
|
243
|
+
</details>
|
|
244
|
+
|
|
245
|
+
```grain
|
|
246
|
+
mapWithDefaultFn : ((a -> b), (c -> b), Result<a, c>) -> b
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
If the Result is `Ok(value)`, applies the `fnOk` function to the `value` to produce a new value.
|
|
250
|
+
If the Result is `Err(value)`, applies the `fnErr` function to the `value` to produce a new value.
|
|
251
|
+
Useful for unwrapping a Result into a value, whether it is successful or unsuccessful.
|
|
252
|
+
|
|
253
|
+
Parameters:
|
|
254
|
+
|
|
255
|
+
|param|type|description|
|
|
256
|
+
|-----|----|-----------|
|
|
257
|
+
|`fnOk`|`a -> b`|The function to call on the value of an `Ok` variant|
|
|
258
|
+
|`fnErr`|`c -> b`|The function to call on the value of an `Err` variant|
|
|
259
|
+
|`result`|`Result<a, c>`|The result to map|
|
|
260
|
+
|
|
261
|
+
Returns:
|
|
262
|
+
|
|
263
|
+
|type|description|
|
|
264
|
+
|----|-----------|
|
|
265
|
+
|`b`|The value produced by one of the mapping functions|
|
|
266
|
+
|
|
267
|
+
### Result.**or**
|
|
268
|
+
|
|
269
|
+
<details disabled>
|
|
270
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
271
|
+
No other changes yet.
|
|
272
|
+
</details>
|
|
273
|
+
|
|
274
|
+
```grain
|
|
275
|
+
( or ) : (Result<a, b>, Result<a, b>) -> Result<a, b>
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Behaves like a logical OR (`||`) where the first Result is only returned if it is the `Ok` variant and falling back to the second Result in all other cases.
|
|
279
|
+
|
|
280
|
+
Parameters:
|
|
281
|
+
|
|
282
|
+
|param|type|description|
|
|
283
|
+
|-----|----|-----------|
|
|
284
|
+
|`result1`|`Result<a, b>`|The first result|
|
|
285
|
+
|`result2`|`Result<a, b>`|The second result|
|
|
286
|
+
|
|
287
|
+
Returns:
|
|
288
|
+
|
|
289
|
+
|type|description|
|
|
290
|
+
|----|-----------|
|
|
291
|
+
|`Result<a, b>`|The first Result if it is the `Ok` variant or the second Result otherwise|
|
|
292
|
+
|
|
293
|
+
### Result.**and**
|
|
294
|
+
|
|
295
|
+
<details disabled>
|
|
296
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
297
|
+
No other changes yet.
|
|
298
|
+
</details>
|
|
299
|
+
|
|
300
|
+
```grain
|
|
301
|
+
and : (Result<a, b>, Result<a, b>) -> Result<a, b>
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Behaves like a logical AND (`&&`) where the first Result is only returned if it is the `Err` variant and falling back to the second Result in all other cases.
|
|
305
|
+
|
|
306
|
+
Parameters:
|
|
307
|
+
|
|
308
|
+
|param|type|description|
|
|
309
|
+
|-----|----|-----------|
|
|
310
|
+
|`result1`|`Result<a, b>`|The first result|
|
|
311
|
+
|`result2`|`Result<a, b>`|The second result|
|
|
312
|
+
|
|
313
|
+
Returns:
|
|
314
|
+
|
|
315
|
+
|type|description|
|
|
316
|
+
|----|-----------|
|
|
317
|
+
|`Result<a, b>`|The second Result if both are the `Ok` variant or the first Result otherwise|
|
|
318
|
+
|
|
319
|
+
### Result.**peek**
|
|
320
|
+
|
|
321
|
+
<details disabled>
|
|
322
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
323
|
+
No other changes yet.
|
|
324
|
+
</details>
|
|
325
|
+
|
|
326
|
+
```grain
|
|
327
|
+
peek : ((a -> b), (c -> d), Result<a, c>) -> Void
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
If the Result is `Ok(value)`, applies the `fnOk` function to the `value` without producing a new value.
|
|
331
|
+
If the Result is `Err(value)`, applies the `fnErr` function to the `value` without producing a new value.
|
|
332
|
+
Useful for inspecting Results without changing anything.
|
|
333
|
+
|
|
334
|
+
Parameters:
|
|
335
|
+
|
|
336
|
+
|param|type|description|
|
|
337
|
+
|-----|----|-----------|
|
|
338
|
+
|`fnOk`|`a -> b`|The function to call on the value of an `Ok` variant|
|
|
339
|
+
|`fnErr`|`c -> d`|The function to call on the value of an `Err` variant|
|
|
340
|
+
|`result`|`Result<a, c>`|The result to inspect|
|
|
341
|
+
|
|
342
|
+
### Result.**peekOk**
|
|
343
|
+
|
|
344
|
+
<details disabled>
|
|
345
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
346
|
+
No other changes yet.
|
|
347
|
+
</details>
|
|
348
|
+
|
|
349
|
+
```grain
|
|
350
|
+
peekOk : ((a -> b), Result<a, c>) -> Void
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
If the Result is `Ok(value)`, applies the given function to the `value` without producing a new value.
|
|
354
|
+
|
|
355
|
+
Parameters:
|
|
356
|
+
|
|
357
|
+
|param|type|description|
|
|
358
|
+
|-----|----|-----------|
|
|
359
|
+
|`fn`|`a -> b`|The function to call on the value of an `Ok` variant|
|
|
360
|
+
|`result`|`Result<a, c>`|The result to inspect|
|
|
361
|
+
|
|
362
|
+
### Result.**peekErr**
|
|
363
|
+
|
|
364
|
+
<details disabled>
|
|
365
|
+
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
|
|
366
|
+
No other changes yet.
|
|
367
|
+
</details>
|
|
368
|
+
|
|
369
|
+
```grain
|
|
370
|
+
peekErr : ((a -> b), Result<c, a>) -> Void
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
If the Result is `Err(value)`, applies the given function to the `value` without producing a new value.
|
|
374
|
+
|
|
375
|
+
Parameters:
|
|
376
|
+
|
|
377
|
+
|param|type|description|
|
|
378
|
+
|-----|----|-----------|
|
|
379
|
+
|`fn`|`a -> b`|The function to call on the value of an `Err` variant|
|
|
380
|
+
|`result`|`Result<c, a>`|The result to inspect|
|
|
381
|
+
|
|
382
|
+
### Result.**expect**
|
|
383
|
+
|
|
384
|
+
<details disabled>
|
|
385
|
+
<summary tabindex="-1">Added in <code>0.4.0</code></summary>
|
|
386
|
+
No other changes yet.
|
|
387
|
+
</details>
|
|
388
|
+
|
|
389
|
+
```grain
|
|
390
|
+
expect : (String, Result<a, b>) -> a
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
Extracts the value inside an `Ok` result, otherwise throw an
|
|
394
|
+
exception containing the message and contents of the `Err`.
|
|
395
|
+
|
|
396
|
+
Parameters:
|
|
397
|
+
|
|
398
|
+
|param|type|description|
|
|
399
|
+
|-----|----|-----------|
|
|
400
|
+
|`msg`|`String`|The message to prepend if the result contains an `Err`|
|
|
401
|
+
|`result`|`Result<a, b>`|The result to extract a value from|
|
|
402
|
+
|
|
403
|
+
Returns:
|
|
404
|
+
|
|
405
|
+
|type|description|
|
|
406
|
+
|----|-----------|
|
|
407
|
+
|`a`|The unwrapped value if the Result is the `Ok` variant|
|
|
408
|
+
|
|
409
|
+
Examples:
|
|
410
|
+
|
|
411
|
+
```grain
|
|
412
|
+
Result.expect("Unexpected error", Ok(1234)) + 42
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
### Result.**unwrap**
|
|
416
|
+
|
|
417
|
+
<details disabled>
|
|
418
|
+
<summary tabindex="-1">Added in <code>0.4.0</code></summary>
|
|
419
|
+
No other changes yet.
|
|
420
|
+
</details>
|
|
421
|
+
|
|
422
|
+
```grain
|
|
423
|
+
unwrap : Result<a, b> -> a
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
Extracts the value inside an `Ok` result, otherwise throw an
|
|
427
|
+
exception containing a default message and contents of the `Err`.
|
|
428
|
+
|
|
429
|
+
Parameters:
|
|
430
|
+
|
|
431
|
+
|param|type|description|
|
|
432
|
+
|-----|----|-----------|
|
|
433
|
+
|`result`|`Result<a, b>`|The result to extract a value from|
|
|
434
|
+
|
|
435
|
+
Returns:
|
|
436
|
+
|
|
437
|
+
|type|description|
|
|
438
|
+
|----|-----------|
|
|
439
|
+
|`a`|The unwrapped value if the result is the `Ok` variant|
|
|
440
|
+
|
|
441
|
+
Examples:
|
|
442
|
+
|
|
443
|
+
```grain
|
|
444
|
+
Result.unwrap(Err("This will throw"))
|
|
445
|
+
```
|
|
446
|
+
|
|
@@ -6,7 +6,7 @@ import WasmI32, {
|
|
|
6
6
|
add as (+),
|
|
7
7
|
mul as (*),
|
|
8
8
|
xor as (^),
|
|
9
|
-
shl as (<<)
|
|
9
|
+
shl as (<<),
|
|
10
10
|
} from "runtime/unsafe/wasmi32"
|
|
11
11
|
import WasmI64 from "runtime/unsafe/wasmi64"
|
|
12
12
|
import WasmF32 from "runtime/unsafe/wasmf32"
|
|
@@ -23,7 +23,7 @@ import WasmF64 from "runtime/unsafe/wasmf64"
|
|
|
23
23
|
* @param {WasmI32} numElts The number of elements to be contained in this array
|
|
24
24
|
* @returns {WasmI32} The pointer to the array
|
|
25
25
|
*/
|
|
26
|
-
export let allocateArray =
|
|
26
|
+
export let allocateArray = numElts => {
|
|
27
27
|
let arr = Memory.malloc((numElts + 2n) * 4n)
|
|
28
28
|
|
|
29
29
|
WasmI32.store(arr, Tags._GRAIN_ARRAY_HEAP_TAG, 0n)
|
|
@@ -49,7 +49,7 @@ export let storeInArray = (arr, idx, item) => {
|
|
|
49
49
|
* @param {WasmI32} numElts The number of elements to be contained in this tuple
|
|
50
50
|
* @returns {WasmI32} The pointer to the tuple
|
|
51
51
|
*/
|
|
52
|
-
export let allocateTuple =
|
|
52
|
+
export let allocateTuple = numElts => {
|
|
53
53
|
let tuple = Memory.malloc((numElts + 2n) * 4n)
|
|
54
54
|
|
|
55
55
|
WasmI32.store(tuple, Tags._GRAIN_TUPLE_HEAP_TAG, 0n)
|
|
@@ -75,7 +75,7 @@ export let storeInTuple = (tuple, idx, item) => {
|
|
|
75
75
|
* @param {WasmI32} size The number of bytes to be contained in this buffer
|
|
76
76
|
* @returns {WasmI32} The pointer to the bytes
|
|
77
77
|
*/
|
|
78
|
-
export let allocateBytes =
|
|
78
|
+
export let allocateBytes = size => {
|
|
79
79
|
let len = size + 8n
|
|
80
80
|
let bytes = Memory.malloc(len)
|
|
81
81
|
Memory.fill(bytes, 0n, len)
|
|
@@ -90,7 +90,7 @@ export let allocateBytes = (size) => {
|
|
|
90
90
|
* @param {WasmI32} size The size (in bytes) of the string to allocate
|
|
91
91
|
* @returns {WasmI32} The pointer to the string
|
|
92
92
|
*/
|
|
93
|
-
export let allocateString =
|
|
93
|
+
export let allocateString = size => {
|
|
94
94
|
let str = Memory.malloc(size + 8n)
|
|
95
95
|
|
|
96
96
|
WasmI32.store(str, Tags._GRAIN_STRING_HEAP_TAG, 0n)
|
|
@@ -105,7 +105,7 @@ export let allocateString = (size) => {
|
|
|
105
105
|
* @param {WasmI32} c The character for which to allocate a string
|
|
106
106
|
* @returns {WasmI32} The pointer to the string
|
|
107
107
|
*/
|
|
108
|
-
export let singleByteString =
|
|
108
|
+
export let singleByteString = c => {
|
|
109
109
|
let str = Memory.malloc(9n)
|
|
110
110
|
|
|
111
111
|
WasmI32.store(str, Tags._GRAIN_STRING_HEAP_TAG, 0n)
|
|
@@ -148,7 +148,7 @@ export let allocateInt64 = () => {
|
|
|
148
148
|
* Allocates a new Int64 with a prepopulated value
|
|
149
149
|
* @param value The value to store
|
|
150
150
|
*/
|
|
151
|
-
export let newInt64 =
|
|
151
|
+
export let newInt64 = value => {
|
|
152
152
|
let ptr = allocateInt64()
|
|
153
153
|
WasmI64.store(ptr, value, 8n)
|
|
154
154
|
ptr
|
|
@@ -158,11 +158,11 @@ export let newInt64 = (value) => {
|
|
|
158
158
|
* Returns a pointer to the heap location containing this boxed number's Int64
|
|
159
159
|
* @param wrappedInt64 The boxed int64 to return
|
|
160
160
|
*/
|
|
161
|
-
export let rawInt64Ptr =
|
|
161
|
+
export let rawInt64Ptr = wrappedInt64 => {
|
|
162
162
|
wrappedInt64 + 8n
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
export let loadInt64 =
|
|
165
|
+
export let loadInt64 = xptr => {
|
|
166
166
|
WasmI64.load(xptr, 8n)
|
|
167
167
|
}
|
|
168
168
|
|
|
@@ -184,7 +184,7 @@ export let allocateInt32 = () => {
|
|
|
184
184
|
* Allocates a new Int32 with a prepopulated value
|
|
185
185
|
* @param value The value to store
|
|
186
186
|
*/
|
|
187
|
-
export let newInt32 =
|
|
187
|
+
export let newInt32 = value => {
|
|
188
188
|
let ptr = allocateInt32()
|
|
189
189
|
WasmI32.store(ptr, value, 8n)
|
|
190
190
|
ptr
|
|
@@ -194,11 +194,11 @@ export let newInt32 = (value) => {
|
|
|
194
194
|
* Returns a pointer to the heap location containing this boxed number's Int32
|
|
195
195
|
* @param wrappedInt32 The boxed int32 to return
|
|
196
196
|
*/
|
|
197
|
-
export let rawInt32Ptr =
|
|
197
|
+
export let rawInt32Ptr = wrappedInt32 => {
|
|
198
198
|
wrappedInt32 + 8n
|
|
199
199
|
}
|
|
200
200
|
|
|
201
|
-
export let loadInt32 =
|
|
201
|
+
export let loadInt32 = xptr => {
|
|
202
202
|
WasmI32.load(xptr, 8n)
|
|
203
203
|
}
|
|
204
204
|
|
|
@@ -222,7 +222,7 @@ export let allocateFloat32 = () => {
|
|
|
222
222
|
* Allocates a new Float32 with a prepopulated value
|
|
223
223
|
* @param value The value to store
|
|
224
224
|
*/
|
|
225
|
-
export let newFloat32 =
|
|
225
|
+
export let newFloat32 = value => {
|
|
226
226
|
let ptr = allocateFloat32()
|
|
227
227
|
WasmF32.store(ptr, value, 8n)
|
|
228
228
|
ptr
|
|
@@ -232,11 +232,11 @@ export let newFloat32 = (value) => {
|
|
|
232
232
|
* Returns a pointer to the heap location containing this boxed number's Float32
|
|
233
233
|
* @param wrappedFloat32 The boxed float32 to return
|
|
234
234
|
*/
|
|
235
|
-
export let rawFloat32Ptr =
|
|
235
|
+
export let rawFloat32Ptr = wrappedFloat32 => {
|
|
236
236
|
wrappedFloat32 + 8n
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
-
export let loadFloat32 =
|
|
239
|
+
export let loadFloat32 = xptr => {
|
|
240
240
|
WasmF32.load(xptr, 8n)
|
|
241
241
|
}
|
|
242
242
|
|
|
@@ -258,7 +258,7 @@ export let allocateFloat64 = () => {
|
|
|
258
258
|
* Allocates a new Float64 with a prepopulated value
|
|
259
259
|
* @param value The value to store
|
|
260
260
|
*/
|
|
261
|
-
export let newFloat64 =
|
|
261
|
+
export let newFloat64 = value => {
|
|
262
262
|
let ptr = allocateFloat64()
|
|
263
263
|
WasmF64.store(ptr, value, 8n)
|
|
264
264
|
ptr
|
|
@@ -268,11 +268,11 @@ export let newFloat64 = (value) => {
|
|
|
268
268
|
* Returns a pointer to the heap location containing this boxed number's Float64
|
|
269
269
|
* @param wrappedFloat64 The boxed float64 to return
|
|
270
270
|
*/
|
|
271
|
-
export let rawFloat64Ptr =
|
|
271
|
+
export let rawFloat64Ptr = wrappedFloat64 => {
|
|
272
272
|
wrappedFloat64 + 8n
|
|
273
273
|
}
|
|
274
274
|
|
|
275
|
-
export let loadFloat64 =
|
|
275
|
+
export let loadFloat64 = xptr => {
|
|
276
276
|
WasmF64.load(xptr, 8n)
|
|
277
277
|
}
|
|
278
278
|
|
|
@@ -296,7 +296,7 @@ export let allocateRational = () => {
|
|
|
296
296
|
* Allocates a new Rational with a prepopulated value
|
|
297
297
|
* @param value The value to store
|
|
298
298
|
*/
|
|
299
|
-
export let newRational = (numerator, denominator) =>{
|
|
299
|
+
export let newRational = (numerator, denominator) => {
|
|
300
300
|
let ptr = allocateRational()
|
|
301
301
|
WasmI32.store(ptr, numerator, 8n)
|
|
302
302
|
WasmI32.store(ptr, denominator, 12n)
|
|
@@ -307,7 +307,7 @@ export let newRational = (numerator, denominator) =>{
|
|
|
307
307
|
* Returns a pointer to the heap location containing this boxed number's Rational numerator
|
|
308
308
|
* @param wrappedRational The boxed rational to return
|
|
309
309
|
*/
|
|
310
|
-
export let rawRationalNumeratorPtr =
|
|
310
|
+
export let rawRationalNumeratorPtr = wrappedRational => {
|
|
311
311
|
wrappedRational + 8n
|
|
312
312
|
}
|
|
313
313
|
|
|
@@ -315,19 +315,18 @@ export let rawRationalNumeratorPtr = (wrappedRational) => {
|
|
|
315
315
|
* Returns a pointer to the heap location containing this boxed number's Rational numerator
|
|
316
316
|
* @param wrappedRational The boxed rational to return
|
|
317
317
|
*/
|
|
318
|
-
export let rawRationalDenominatorPtr =
|
|
318
|
+
export let rawRationalDenominatorPtr = wrappedRational => {
|
|
319
319
|
wrappedRational + 12n
|
|
320
320
|
}
|
|
321
321
|
|
|
322
|
-
export let loadRationalNumerator =
|
|
322
|
+
export let loadRationalNumerator = xptr => {
|
|
323
323
|
WasmI32.load(xptr, 8n)
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
export let loadRationalDenominator =
|
|
326
|
+
export let loadRationalDenominator = xptr => {
|
|
327
327
|
WasmI32.load(xptr, 12n)
|
|
328
328
|
}
|
|
329
329
|
|
|
330
|
-
|
|
331
330
|
/**
|
|
332
331
|
* Load a value from an ADT.
|
|
333
332
|
*
|
|
@@ -337,7 +336,7 @@ export let loadRationalDenominator = (xptr) => {
|
|
|
337
336
|
* @returns {WasmI32} The value located at the index
|
|
338
337
|
*/
|
|
339
338
|
export let loadAdtVal = (ptr, idx) => {
|
|
340
|
-
WasmI32.load(ptr +
|
|
339
|
+
WasmI32.load(ptr + idx * 4n, 20n)
|
|
341
340
|
}
|
|
342
341
|
|
|
343
342
|
/**
|
|
@@ -347,7 +346,7 @@ export let loadAdtVal = (ptr, idx) => {
|
|
|
347
346
|
* @param {WasmI32} ptr Untagged pointer to the ADT
|
|
348
347
|
* @returns {WasmI32} The (tagged) ADT variant id
|
|
349
348
|
*/
|
|
350
|
-
export let loadAdtVariant =
|
|
349
|
+
export let loadAdtVariant = ptr => {
|
|
351
350
|
WasmI32.load(ptr, 12n)
|
|
352
351
|
}
|
|
353
352
|
|
|
@@ -358,10 +357,10 @@ export let loadAdtVariant = (ptr) => {
|
|
|
358
357
|
* @param {WasmI32} ptr Untagged pointer to the string
|
|
359
358
|
* @returns {WasmI32} The string size (in bytes)
|
|
360
359
|
*/
|
|
361
|
-
export let stringSize =
|
|
360
|
+
export let stringSize = ptr => {
|
|
362
361
|
WasmI32.load(ptr, 4n)
|
|
363
362
|
}
|
|
364
363
|
|
|
365
|
-
export let tagSimpleNumber =
|
|
366
|
-
WasmI32.toGrain(
|
|
364
|
+
export let tagSimpleNumber = x => {
|
|
365
|
+
WasmI32.toGrain(x << 1n ^ 1n): Number
|
|
367
366
|
}
|
package/runtime/debug.gr
CHANGED