@grain/stdlib 0.4.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +1 -1
  3. package/array.gr +92 -73
  4. package/array.md +18 -18
  5. package/bigint.gr +497 -0
  6. package/bigint.md +811 -0
  7. package/buffer.gr +56 -217
  8. package/buffer.md +24 -17
  9. package/bytes.gr +103 -205
  10. package/bytes.md +19 -0
  11. package/char.gr +152 -166
  12. package/char.md +200 -0
  13. package/exception.md +6 -0
  14. package/float32.gr +159 -82
  15. package/float32.md +315 -0
  16. package/float64.gr +163 -82
  17. package/float64.md +315 -0
  18. package/hash.gr +53 -49
  19. package/int32.gr +479 -230
  20. package/int32.md +937 -0
  21. package/int64.gr +479 -230
  22. package/int64.md +937 -0
  23. package/list.gr +530 -116
  24. package/list.md +1141 -0
  25. package/map.gr +302 -121
  26. package/map.md +525 -0
  27. package/number.gr +51 -57
  28. package/number.md +37 -3
  29. package/option.gr +25 -25
  30. package/option.md +1 -1
  31. package/package.json +3 -3
  32. package/pervasives.gr +504 -52
  33. package/pervasives.md +1116 -0
  34. package/queue.gr +8 -1
  35. package/queue.md +10 -0
  36. package/random.gr +196 -0
  37. package/random.md +179 -0
  38. package/range.gr +26 -26
  39. package/regex.gr +1833 -842
  40. package/regex.md +11 -11
  41. package/result.md +1 -1
  42. package/runtime/bigint.gr +2045 -0
  43. package/runtime/bigint.md +326 -0
  44. package/runtime/dataStructures.gr +99 -279
  45. package/runtime/dataStructures.md +391 -0
  46. package/runtime/debug.gr +0 -1
  47. package/runtime/debug.md +6 -0
  48. package/runtime/equal.gr +40 -37
  49. package/runtime/equal.md +6 -0
  50. package/runtime/exception.gr +28 -15
  51. package/runtime/exception.md +30 -0
  52. package/runtime/gc.gr +50 -20
  53. package/runtime/gc.md +36 -0
  54. package/runtime/malloc.gr +32 -22
  55. package/runtime/malloc.md +55 -0
  56. package/runtime/numberUtils.gr +297 -142
  57. package/runtime/numberUtils.md +54 -0
  58. package/runtime/numbers.gr +1204 -453
  59. package/runtime/numbers.md +300 -0
  60. package/runtime/string.gr +193 -228
  61. package/runtime/string.md +24 -0
  62. package/runtime/stringUtils.gr +62 -38
  63. package/runtime/stringUtils.md +6 -0
  64. package/runtime/unsafe/constants.gr +17 -0
  65. package/runtime/unsafe/constants.md +72 -0
  66. package/runtime/unsafe/conv.gr +10 -10
  67. package/runtime/unsafe/conv.md +71 -0
  68. package/runtime/unsafe/errors.md +204 -0
  69. package/runtime/unsafe/memory.gr +14 -3
  70. package/runtime/unsafe/memory.md +54 -0
  71. package/runtime/unsafe/printWasm.gr +4 -4
  72. package/runtime/unsafe/printWasm.md +24 -0
  73. package/runtime/unsafe/tags.gr +11 -10
  74. package/runtime/unsafe/tags.md +120 -0
  75. package/runtime/unsafe/wasmf32.gr +9 -2
  76. package/runtime/unsafe/wasmf32.md +168 -0
  77. package/runtime/unsafe/wasmf64.gr +9 -2
  78. package/runtime/unsafe/wasmf64.md +168 -0
  79. package/runtime/unsafe/wasmi32.gr +65 -47
  80. package/runtime/unsafe/wasmi32.md +282 -0
  81. package/runtime/unsafe/wasmi64.gr +78 -50
  82. package/runtime/unsafe/wasmi64.md +300 -0
  83. package/runtime/utils/printing.gr +62 -0
  84. package/runtime/utils/printing.md +18 -0
  85. package/runtime/wasi.gr +200 -46
  86. package/runtime/wasi.md +839 -0
  87. package/set.gr +125 -121
  88. package/set.md +24 -21
  89. package/stack.gr +29 -29
  90. package/stack.md +4 -6
  91. package/string.gr +434 -415
  92. package/string.md +3 -3
  93. package/sys/file.gr +477 -482
  94. package/sys/process.gr +33 -47
  95. package/sys/random.gr +48 -20
  96. package/sys/random.md +38 -0
  97. package/sys/time.gr +12 -28
@@ -0,0 +1,391 @@
1
+ ### DataStructures.**allocateArray**
2
+
3
+ ```grain
4
+ allocateArray : WasmI32 -> WasmI32
5
+ ```
6
+
7
+ Allocates a new Grain array.
8
+
9
+ Parameters:
10
+
11
+ |param|type|description|
12
+ |-----|----|-----------|
13
+ |`numElts`|`WasmI32`|The number of elements to be contained in this array|
14
+
15
+ Returns:
16
+
17
+ |type|description|
18
+ |----|-----------|
19
+ |`WasmI32`|The pointer to the array|
20
+
21
+ ### DataStructures.**allocateTuple**
22
+
23
+ ```grain
24
+ allocateTuple : WasmI32 -> WasmI32
25
+ ```
26
+
27
+ Allocates a new Grain tuple.
28
+
29
+ Parameters:
30
+
31
+ |param|type|description|
32
+ |-----|----|-----------|
33
+ |`numElts`|`WasmI32`|The number of elements to be contained in this tuple|
34
+
35
+ Returns:
36
+
37
+ |type|description|
38
+ |----|-----------|
39
+ |`WasmI32`|The pointer to the tuple|
40
+
41
+ ### DataStructures.**allocateBytes**
42
+
43
+ ```grain
44
+ allocateBytes : WasmI32 -> WasmI32
45
+ ```
46
+
47
+ Allocates a new Grain bytes.
48
+
49
+ Parameters:
50
+
51
+ |param|type|description|
52
+ |-----|----|-----------|
53
+ |`size`|`WasmI32`|The number of bytes to be contained in this buffer|
54
+
55
+ Returns:
56
+
57
+ |type|description|
58
+ |----|-----------|
59
+ |`WasmI32`|The pointer to the bytes|
60
+
61
+ ### DataStructures.**allocateString**
62
+
63
+ ```grain
64
+ allocateString : WasmI32 -> WasmI32
65
+ ```
66
+
67
+ Allocates a new Grain string.
68
+
69
+ Parameters:
70
+
71
+ |param|type|description|
72
+ |-----|----|-----------|
73
+ |`size`|`WasmI32`|The size (in bytes) of the string to allocate|
74
+
75
+ Returns:
76
+
77
+ |type|description|
78
+ |----|-----------|
79
+ |`WasmI32`|The pointer to the string|
80
+
81
+ ### DataStructures.**allocateInt32**
82
+
83
+ ```grain
84
+ allocateInt32 : () -> WasmI32
85
+ ```
86
+
87
+ Allocates a new Int32.
88
+
89
+ Returns:
90
+
91
+ |type|description|
92
+ |----|-----------|
93
+ |`WasmI32`|The pointer to the empty Int32|
94
+
95
+ ### DataStructures.**newInt32**
96
+
97
+ ```grain
98
+ newInt32 : WasmI32 -> WasmI32
99
+ ```
100
+
101
+ Allocates a new Int32 with a prepopulated value
102
+
103
+ Parameters:
104
+
105
+ |param|type|description|
106
+ |-----|----|-----------|
107
+ |`value`|`WasmI32`|The value to store|
108
+
109
+ Returns:
110
+
111
+ |type|description|
112
+ |----|-----------|
113
+ |`WasmI32`|The pointer to the Int32|
114
+
115
+ ### DataStructures.**allocateInt64**
116
+
117
+ ```grain
118
+ allocateInt64 : () -> WasmI32
119
+ ```
120
+
121
+ Allocates a new Int64.
122
+
123
+ Returns:
124
+
125
+ |type|description|
126
+ |----|-----------|
127
+ |`WasmI32`|The pointer to the empty Int64|
128
+
129
+ ### DataStructures.**newInt64**
130
+
131
+ ```grain
132
+ newInt64 : WasmI64 -> WasmI32
133
+ ```
134
+
135
+ Allocates a new Int64 with a prepopulated value
136
+
137
+ Parameters:
138
+
139
+ |param|type|description|
140
+ |-----|----|-----------|
141
+ |`value`|`WasmI64`|The value to store|
142
+
143
+ Returns:
144
+
145
+ |type|description|
146
+ |----|-----------|
147
+ |`WasmI32`|The pointer to the Int64|
148
+
149
+ ### DataStructures.**allocateFloat32**
150
+
151
+ ```grain
152
+ allocateFloat32 : () -> WasmI32
153
+ ```
154
+
155
+ Allocates a new Float32.
156
+
157
+ Returns:
158
+
159
+ |type|description|
160
+ |----|-----------|
161
+ |`WasmI32`|The pointer to the empty Float32|
162
+
163
+ ### DataStructures.**newFloat32**
164
+
165
+ ```grain
166
+ newFloat32 : WasmF32 -> WasmI32
167
+ ```
168
+
169
+ Allocates a new Float32 with a prepopulated value
170
+
171
+ Parameters:
172
+
173
+ |param|type|description|
174
+ |-----|----|-----------|
175
+ |`value`|`WasmF32`|The value to store|
176
+
177
+ Returns:
178
+
179
+ |type|description|
180
+ |----|-----------|
181
+ |`WasmI32`|the pointer to the Float32|
182
+
183
+ ### DataStructures.**allocateFloat64**
184
+
185
+ ```grain
186
+ allocateFloat64 : () -> WasmI32
187
+ ```
188
+
189
+ Allocates a new Float64.
190
+
191
+ Returns:
192
+
193
+ |type|description|
194
+ |----|-----------|
195
+ |`WasmI32`|The pointer to the empty Float64|
196
+
197
+ ### DataStructures.**newFloat64**
198
+
199
+ ```grain
200
+ newFloat64 : WasmF64 -> WasmI32
201
+ ```
202
+
203
+ Allocates a new Float64 with a prepopulated value
204
+
205
+ Parameters:
206
+
207
+ |param|type|description|
208
+ |-----|----|-----------|
209
+ |`value`|`WasmF64`|The value to store|
210
+
211
+ Returns:
212
+
213
+ |type|description|
214
+ |----|-----------|
215
+ |`WasmI32`|The pointer to the Float64|
216
+
217
+ ### DataStructures.**allocateRational**
218
+
219
+ ```grain
220
+ allocateRational : () -> WasmI32
221
+ ```
222
+
223
+ Allocates a new Rational.
224
+
225
+ Returns:
226
+
227
+ |type|description|
228
+ |----|-----------|
229
+ |`WasmI32`|The pointer to the empty Rational|
230
+
231
+ ### DataStructures.**newRational**
232
+
233
+ ```grain
234
+ newRational : (WasmI32, WasmI32) -> WasmI32
235
+ ```
236
+
237
+ Allocates a new Rational with a prepopulated value
238
+
239
+ Parameters:
240
+
241
+ |param|type|description|
242
+ |-----|----|-----------|
243
+ |`value`|`WasmI32`|The numerator value to store|
244
+ |`value`|`WasmI32`|The denominator value to store|
245
+
246
+ Returns:
247
+
248
+ |type|description|
249
+ |----|-----------|
250
+ |`WasmI32`|The pointer to the Rational|
251
+
252
+ ### DataStructures.**loadAdtVariant**
253
+
254
+ ```grain
255
+ loadAdtVariant : WasmI32 -> WasmI32
256
+ ```
257
+
258
+ Load the (tagged) variant of an ADT.
259
+
260
+ Parameters:
261
+
262
+ |param|type|description|
263
+ |-----|----|-----------|
264
+ |`ptr`|`WasmI32`|Untagged pointer to the ADT|
265
+
266
+ Returns:
267
+
268
+ |type|description|
269
+ |----|-----------|
270
+ |`WasmI32`|The (tagged) ADT variant id|
271
+
272
+ ### DataStructures.**stringSize**
273
+
274
+ ```grain
275
+ stringSize : WasmI32 -> WasmI32
276
+ ```
277
+
278
+ Load an untagged string's size.
279
+
280
+ Parameters:
281
+
282
+ |param|type|description|
283
+ |-----|----|-----------|
284
+ |`ptr`|`WasmI32`|Untagged pointer to the string|
285
+
286
+ Returns:
287
+
288
+ |type|description|
289
+ |----|-----------|
290
+ |`WasmI32`|The string size (in bytes)|
291
+
292
+ ### DataStructures.**bytesSize**
293
+
294
+ ```grain
295
+ bytesSize : WasmI32 -> WasmI32
296
+ ```
297
+
298
+ Load an untagged Bytes' size.
299
+
300
+ Parameters:
301
+
302
+ |param|type|description|
303
+ |-----|----|-----------|
304
+ |`ptr`|`WasmI32`|Untagged pointer to the Bytes|
305
+
306
+ Returns:
307
+
308
+ |type|description|
309
+ |----|-----------|
310
+ |`WasmI32`|The Bytes size (in bytes)|
311
+
312
+ ### DataStructures.**tagSimpleNumber**
313
+
314
+ ```grain
315
+ tagSimpleNumber : WasmI32 -> Number
316
+ ```
317
+
318
+ Tag a simple number.
319
+
320
+ Parameters:
321
+
322
+ |param|type|description|
323
+ |-----|----|-----------|
324
+ |`num`|`WasmI32`|The number to tag|
325
+
326
+ Returns:
327
+
328
+ |type|description|
329
+ |----|-----------|
330
+ |`Number`|The tagged number|
331
+
332
+ ### DataStructures.**untagSimpleNumber**
333
+
334
+ ```grain
335
+ untagSimpleNumber : Number -> WasmI32
336
+ ```
337
+
338
+ Untag a simple number.
339
+
340
+ Parameters:
341
+
342
+ |param|type|description|
343
+ |-----|----|-----------|
344
+ |`num`|`Number`|The number to untag|
345
+
346
+ Returns:
347
+
348
+ |type|description|
349
+ |----|-----------|
350
+ |`WasmI32`|The untagged number|
351
+
352
+ ### DataStructures.**tagChar**
353
+
354
+ ```grain
355
+ tagChar : WasmI32 -> Char
356
+ ```
357
+
358
+ Tag a char.
359
+
360
+ Parameters:
361
+
362
+ |param|type|description|
363
+ |-----|----|-----------|
364
+ |`num`|`WasmI32`|The usv to tag|
365
+
366
+ Returns:
367
+
368
+ |type|description|
369
+ |----|-----------|
370
+ |`Char`|The tagged char|
371
+
372
+ ### DataStructures.**untagChar**
373
+
374
+ ```grain
375
+ untagChar : Char -> WasmI32
376
+ ```
377
+
378
+ Untag a char.
379
+
380
+ Parameters:
381
+
382
+ |param|type|description|
383
+ |-----|----|-----------|
384
+ |`num`|`Char`|The char to untag|
385
+
386
+ Returns:
387
+
388
+ |type|description|
389
+ |----|-----------|
390
+ |`WasmI32`|The untagged usv|
391
+
package/runtime/debug.gr CHANGED
@@ -1,3 +1,2 @@
1
1
  /* grainc-flags --compilation-mode=runtime */
2
2
  export foreign wasm debug: a -> Void from "console"
3
-
@@ -0,0 +1,6 @@
1
+ ### Debug.**debug**
2
+
3
+ ```grain
4
+ debug : a -> Void
5
+ ```
6
+
package/runtime/equal.gr CHANGED
@@ -1,4 +1,4 @@
1
- /* grainc-flags --compilation-mode=runtime */
1
+ /* grainc-flags --no-pervasives */
2
2
 
3
3
  import WasmI32, {
4
4
  eq as (==),
@@ -10,20 +10,21 @@ import WasmI32, {
10
10
  mul as (*),
11
11
  ltS as (<),
12
12
  remS as (%),
13
- shl as (<<)
13
+ shl as (<<),
14
14
  } from "runtime/unsafe/wasmi32"
15
15
  import WasmI64 from "runtime/unsafe/wasmi64"
16
16
  import Tags from "runtime/unsafe/tags"
17
- import Memory from "runtime/unsafe/memory"
18
17
 
19
- primitive (!) : Bool -> Bool = "@not"
20
- primitive (||) : (Bool, Bool) -> Bool = "@or"
21
- primitive (&&) : (Bool, Bool) -> Bool = "@and"
18
+ primitive (!): Bool -> Bool = "@not"
19
+ primitive (||): (Bool, Bool) -> Bool = "@or"
20
+ primitive (&&): (Bool, Bool) -> Bool = "@and"
22
21
 
23
22
  import { isNumber, numberEqual } from "runtime/numbers"
24
23
 
24
+ @unsafe
25
25
  let cycleMarker = 0x80000000n
26
26
 
27
+ @unsafe
27
28
  let rec heapEqualHelp = (heapTag, xptr, yptr) => {
28
29
  match (heapTag) {
29
30
  t when t == Tags._GRAIN_ADT_HEAP_TAG => {
@@ -45,7 +46,12 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
45
46
 
46
47
  let bytes = xarity * 4n
47
48
  for (let mut i = 0n; i < bytes; i += 4n) {
48
- if (!equalHelp(WasmI32.load(xptr + i, 20n), WasmI32.load(yptr + i, 20n))) {
49
+ if (
50
+ !equalHelp(
51
+ WasmI32.load(xptr + i, 20n),
52
+ WasmI32.load(yptr + i, 20n)
53
+ )
54
+ ) {
49
55
  result = false
50
56
  break
51
57
  }
@@ -72,7 +78,9 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
72
78
 
73
79
  let bytes = xlength * 4n
74
80
  for (let mut i = 0n; i < bytes; i += 4n) {
75
- if (!equalHelp(WasmI32.load(xptr + i, 16n), WasmI32.load(yptr + i, 16n))) {
81
+ if (
82
+ !equalHelp(WasmI32.load(xptr + i, 16n), WasmI32.load(yptr + i, 16n))
83
+ ) {
76
84
  result = false
77
85
  break
78
86
  }
@@ -91,7 +99,7 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
91
99
  if (xlength != ylength) {
92
100
  false
93
101
  } else if ((xlength & cycleMarker) == cycleMarker) {
94
- // Cycle check
102
+ // Cycle check
95
103
  true
96
104
  } else {
97
105
  WasmI32.store(xptr, xlength ^ cycleMarker, 4n)
@@ -100,7 +108,9 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
100
108
  let mut result = true
101
109
  let bytes = xlength * 4n
102
110
  for (let mut i = 0n; i < bytes; i += 4n) {
103
- if (!equalHelp(WasmI32.load(xptr + i, 8n), WasmI32.load(yptr + i, 8n))) {
111
+ if (
112
+ !equalHelp(WasmI32.load(xptr + i, 8n), WasmI32.load(yptr + i, 8n))
113
+ ) {
104
114
  result = false
105
115
  break
106
116
  }
@@ -112,7 +122,9 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
112
122
  result
113
123
  }
114
124
  },
115
- t when t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG => {
125
+ t when (
126
+ t == Tags._GRAIN_STRING_HEAP_TAG || t == Tags._GRAIN_BYTES_HEAP_TAG
127
+ ) => {
116
128
  let xlength = WasmI32.load(xptr, 4n)
117
129
  let ylength = WasmI32.load(yptr, 4n)
118
130
 
@@ -124,14 +136,19 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
124
136
  let first = xlength - extra
125
137
  let mut result = true
126
138
  for (let mut i = 0n; i < first; i += 8n) {
127
- if (WasmI64.ne(WasmI64.load(xptr + i, 8n), WasmI64.load(yptr + i, 8n))) {
139
+ if (
140
+ WasmI64.ne(WasmI64.load(xptr + i, 8n), WasmI64.load(yptr + i, 8n))
141
+ ) {
128
142
  result = false
129
143
  break
130
144
  }
131
145
  }
132
146
  if (result) {
133
147
  for (let mut i = 0n; i < extra; i += 1n) {
134
- if (WasmI32.load8U(xptr + first + i, 8n) != WasmI32.load8U(yptr + first + i, 8n)) {
148
+ if (
149
+ WasmI32.load8U(xptr + first + i, 8n) !=
150
+ WasmI32.load8U(yptr + first + i, 8n)
151
+ ) {
135
152
  result = false
136
153
  break
137
154
  }
@@ -141,22 +158,6 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
141
158
  result
142
159
  }
143
160
  },
144
- t when t == Tags._GRAIN_CHAR_HEAP_TAG => {
145
- let byte = WasmI32.load8U(xptr, 4n)
146
- let n = if ((byte & 0x80n) == 0x00n) {
147
- 1n
148
- } else if ((byte & 0xF0n) == 0xF0n) {
149
- 4n
150
- } else if ((byte & 0xE0n) == 0xE0n) {
151
- 3n
152
- } else {
153
- 2n
154
- }
155
- // WebAssembly is little-endian, so bytes are in reverse order
156
- let x = WasmI32.load(xptr, 4n) << ((4n - n) * 8n)
157
- let y = WasmI32.load(yptr, 4n) << ((4n - n) * 8n)
158
- x == y
159
- },
160
161
  t when t == Tags._GRAIN_TUPLE_HEAP_TAG => {
161
162
  let xsize = WasmI32.load(xptr, 4n)
162
163
  let ysize = WasmI32.load(yptr, 4n)
@@ -170,7 +171,9 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
170
171
  let mut result = true
171
172
  let bytes = xsize * 4n
172
173
  for (let mut i = 0n; i < bytes; i += 4n) {
173
- if (!equalHelp(WasmI32.load(xptr + i, 8n), WasmI32.load(yptr + i, 8n))) {
174
+ if (
175
+ !equalHelp(WasmI32.load(xptr + i, 8n), WasmI32.load(yptr + i, 8n))
176
+ ) {
174
177
  result = false
175
178
  break
176
179
  }
@@ -185,10 +188,13 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
185
188
  _ => {
186
189
  // No other implementation
187
190
  xptr == yptr
188
- }
191
+ },
189
192
  }
190
193
  }, equalHelp = (x, y) => {
191
- if (((x & Tags._GRAIN_GENERIC_TAG_MASK) != 0n) && ((y & Tags._GRAIN_GENERIC_TAG_MASK) != 0n)) {
194
+ if (
195
+ (x & Tags._GRAIN_GENERIC_TAG_MASK) != 0n &&
196
+ (y & Tags._GRAIN_GENERIC_TAG_MASK) != 0n
197
+ ) {
192
198
  // Short circuit for non-pointer values
193
199
  x == y
194
200
  } else if (isNumber(x)) {
@@ -201,10 +207,7 @@ let rec heapEqualHelp = (heapTag, xptr, yptr) => {
201
207
  }
202
208
  }
203
209
 
210
+ @unsafe
204
211
  export let rec equal = (x: a, y: a) => {
205
- let ret = equalHelp(WasmI32.fromGrain(x), WasmI32.fromGrain(y))
206
- Memory.decRef(WasmI32.fromGrain(x))
207
- Memory.decRef(WasmI32.fromGrain(y))
208
- Memory.decRef(WasmI32.fromGrain(equal))
209
- ret
212
+ equalHelp(WasmI32.fromGrain(x), WasmI32.fromGrain(y))
210
213
  }
@@ -0,0 +1,6 @@
1
+ ### Equal.**equal**
2
+
3
+ ```grain
4
+ equal : (a, a) -> Bool
5
+ ```
6
+
@@ -1,13 +1,18 @@
1
1
  /* grainc-flags --compilation-mode=runtime */
2
2
 
3
- import WasmI32, {
4
- eq as (==),
5
- add as (+)
6
- } from "runtime/unsafe/wasmi32"
3
+ import WasmI32, { eq as (==), add as (+) } from "runtime/unsafe/wasmi32"
7
4
 
8
- import foreign wasm fd_write: (WasmI32, WasmI32, WasmI32, WasmI32) -> WasmI32 from "wasi_snapshot_preview1"
5
+ import foreign wasm fd_write: (
6
+ WasmI32,
7
+ WasmI32,
8
+ WasmI32,
9
+ WasmI32,
10
+ ) -> WasmI32 from "wasi_snapshot_preview1"
9
11
 
10
- enum Option<a> { Some(a), None }
12
+ enum Option<a> {
13
+ Some(a),
14
+ None,
15
+ }
11
16
 
12
17
  export let mut printers = 0n
13
18
 
@@ -15,11 +20,14 @@ export let mut printers = 0n
15
20
  // no GC operations. As such, they should only be called by this module and/or
16
21
  // modules that understand these restrictions, namely Pervasives.
17
22
 
18
- export let dangerouslyRegisterBasePrinter = (f) => {
23
+ export let dangerouslyRegisterBasePrinter = f => {
19
24
  let mut current = printers
20
25
  while (true) {
21
26
  // There will be at least one printer registered by the time this is called
22
- let (_, next) = WasmI32.toGrain(current) : (Exception -> Option<String>, WasmI32)
27
+ let (_, next) = WasmI32.toGrain(current): (
28
+ Exception -> Option<String>,
29
+ WasmI32
30
+ )
23
31
  if (next == 0n) {
24
32
  // Using a tuple in runtime mode is typically disallowed as there is no way
25
33
  // to reclaim the memory, but this function is only called once
@@ -34,7 +42,7 @@ export let dangerouslyRegisterBasePrinter = (f) => {
34
42
  void
35
43
  }
36
44
 
37
- export let dangerouslyRegisterPrinter = (f) => {
45
+ export let dangerouslyRegisterPrinter = f => {
38
46
  printers = WasmI32.fromGrain((f, printers))
39
47
  // We don't decRef the closure or arguments here to avoid a cyclic dep. on Memory.
40
48
  // This is fine, as this function is only called seldomly.
@@ -50,13 +58,16 @@ let exceptionToString = (e: Exception) => {
50
58
  if (current == 0n) {
51
59
  break
52
60
  }
53
- let (printer, next) = WasmI32.toGrain(current) : (Exception -> Option<String>, WasmI32)
61
+ let (printer, next) = WasmI32.toGrain(current): (
62
+ Exception -> Option<String>,
63
+ WasmI32
64
+ )
54
65
  match (printer(e)) {
55
66
  Some(str) => {
56
67
  result = str
57
68
  break
58
69
  },
59
- None => void
70
+ None => void,
60
71
  }
61
72
  current = next
62
73
  }
@@ -100,19 +111,21 @@ export exception AssertionError(String)
100
111
  export exception InvalidArgument(String)
101
112
  export exception OutOfMemory
102
113
 
103
- let runtimeErrorPrinter = (e) => {
114
+ let runtimeErrorPrinter = e => {
104
115
  match (e) {
105
116
  IndexOutOfBounds => Some("IndexOutOfBounds: Index out of bounds"),
106
117
  DivisionByZero => Some("DivisionByZero: Division by zero"),
107
118
  ModuloByZero => Some("ModuloByZero: Modulo by zero"),
108
119
  Overflow => Some("Overflow: Number overflow"),
109
- NumberNotIntlike => Some("NumberNotIntlike: Can't coerce number to integer"),
110
- NumberNotRational => Some("NumberNotRational: Can't coerce number to rational"),
120
+ NumberNotIntlike =>
121
+ Some("NumberNotIntlike: Can't coerce number to integer"),
122
+ NumberNotRational =>
123
+ Some("NumberNotRational: Can't coerce number to rational"),
111
124
  MatchFailure => Some("MatchFailure: No matching pattern"),
112
125
  AssertionError(s) => Some(s),
113
126
  OutOfMemory => Some("OutOfMemory: Maximum memory size exceeded"),
114
127
  InvalidArgument(msg) => Some(msg),
115
- _ => None
128
+ _ => None,
116
129
  }
117
130
  }
118
131
 
@@ -0,0 +1,30 @@
1
+ ### Exception.**Option**
2
+
3
+ ```grain
4
+ type Option<a>
5
+ ```
6
+
7
+ ### Exception.**printers**
8
+
9
+ ```grain
10
+ printers : WasmI32
11
+ ```
12
+
13
+ ### Exception.**dangerouslyRegisterBasePrinter**
14
+
15
+ ```grain
16
+ dangerouslyRegisterBasePrinter : a -> Void
17
+ ```
18
+
19
+ ### Exception.**dangerouslyRegisterPrinter**
20
+
21
+ ```grain
22
+ dangerouslyRegisterPrinter : a -> Void
23
+ ```
24
+
25
+ ### Exception.**printException**
26
+
27
+ ```grain
28
+ printException : Exception -> Void
29
+ ```
30
+