@grain/stdlib 0.5.13 → 0.6.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 (155) hide show
  1. package/CHANGELOG.md +193 -0
  2. package/LICENSE +1 -1
  3. package/README.md +25 -2
  4. package/array.gr +1512 -199
  5. package/array.md +2032 -94
  6. package/bigint.gr +239 -140
  7. package/bigint.md +450 -106
  8. package/buffer.gr +595 -102
  9. package/buffer.md +903 -145
  10. package/bytes.gr +401 -110
  11. package/bytes.md +551 -63
  12. package/char.gr +228 -49
  13. package/char.md +373 -7
  14. package/exception.gr +26 -12
  15. package/exception.md +29 -5
  16. package/float32.gr +130 -109
  17. package/float32.md +185 -57
  18. package/float64.gr +112 -99
  19. package/float64.md +185 -57
  20. package/hash.gr +47 -37
  21. package/hash.md +21 -3
  22. package/int16.gr +430 -0
  23. package/int16.md +618 -0
  24. package/int32.gr +200 -269
  25. package/int32.md +254 -289
  26. package/int64.gr +142 -225
  27. package/int64.md +254 -289
  28. package/int8.gr +511 -0
  29. package/int8.md +786 -0
  30. package/json.gr +2084 -0
  31. package/json.md +608 -0
  32. package/list.gr +120 -68
  33. package/list.md +125 -80
  34. package/map.gr +560 -57
  35. package/map.md +672 -56
  36. package/marshal.gr +239 -227
  37. package/marshal.md +36 -4
  38. package/number.gr +626 -676
  39. package/number.md +738 -153
  40. package/option.gr +33 -35
  41. package/option.md +58 -42
  42. package/package.json +2 -2
  43. package/path.gr +148 -187
  44. package/path.md +47 -96
  45. package/pervasives.gr +75 -416
  46. package/pervasives.md +85 -180
  47. package/priorityqueue.gr +433 -74
  48. package/priorityqueue.md +422 -54
  49. package/queue.gr +362 -80
  50. package/queue.md +433 -38
  51. package/random.gr +67 -75
  52. package/random.md +68 -40
  53. package/range.gr +135 -63
  54. package/range.md +198 -43
  55. package/rational.gr +284 -0
  56. package/rational.md +545 -0
  57. package/regex.gr +933 -1066
  58. package/regex.md +59 -60
  59. package/result.gr +23 -25
  60. package/result.md +54 -39
  61. package/runtime/atof/common.gr +78 -82
  62. package/runtime/atof/common.md +22 -10
  63. package/runtime/atof/decimal.gr +102 -127
  64. package/runtime/atof/decimal.md +28 -7
  65. package/runtime/atof/lemire.gr +56 -71
  66. package/runtime/atof/lemire.md +9 -1
  67. package/runtime/atof/parse.gr +83 -110
  68. package/runtime/atof/parse.md +12 -2
  69. package/runtime/atof/slow.gr +28 -35
  70. package/runtime/atof/slow.md +9 -1
  71. package/runtime/atof/table.gr +19 -18
  72. package/runtime/atof/table.md +10 -2
  73. package/runtime/atoi/parse.gr +153 -136
  74. package/runtime/atoi/parse.md +50 -1
  75. package/runtime/bigint.gr +410 -517
  76. package/runtime/bigint.md +71 -57
  77. package/runtime/compare.gr +176 -85
  78. package/runtime/compare.md +31 -1
  79. package/runtime/dataStructures.gr +144 -32
  80. package/runtime/dataStructures.md +267 -31
  81. package/runtime/debugPrint.gr +34 -15
  82. package/runtime/debugPrint.md +37 -5
  83. package/runtime/equal.gr +53 -52
  84. package/runtime/equal.md +30 -1
  85. package/runtime/exception.gr +38 -47
  86. package/runtime/exception.md +10 -8
  87. package/runtime/gc.gr +23 -152
  88. package/runtime/gc.md +13 -17
  89. package/runtime/malloc.gr +31 -31
  90. package/runtime/malloc.md +11 -3
  91. package/runtime/numberUtils.gr +191 -172
  92. package/runtime/numberUtils.md +17 -9
  93. package/runtime/numbers.gr +1695 -1021
  94. package/runtime/numbers.md +1098 -134
  95. package/runtime/string.gr +540 -242
  96. package/runtime/string.md +76 -6
  97. package/runtime/unsafe/constants.gr +30 -13
  98. package/runtime/unsafe/constants.md +80 -0
  99. package/runtime/unsafe/conv.gr +55 -28
  100. package/runtime/unsafe/conv.md +41 -9
  101. package/runtime/unsafe/memory.gr +10 -30
  102. package/runtime/unsafe/memory.md +15 -19
  103. package/runtime/unsafe/tags.gr +37 -21
  104. package/runtime/unsafe/tags.md +88 -8
  105. package/runtime/unsafe/wasmf32.gr +30 -36
  106. package/runtime/unsafe/wasmf32.md +64 -56
  107. package/runtime/unsafe/wasmf64.gr +30 -36
  108. package/runtime/unsafe/wasmf64.md +64 -56
  109. package/runtime/unsafe/wasmi32.gr +49 -66
  110. package/runtime/unsafe/wasmi32.md +102 -94
  111. package/runtime/unsafe/wasmi64.gr +52 -79
  112. package/runtime/unsafe/wasmi64.md +108 -100
  113. package/runtime/utils/printing.gr +13 -15
  114. package/runtime/utils/printing.md +11 -3
  115. package/runtime/wasi.gr +294 -295
  116. package/runtime/wasi.md +62 -42
  117. package/set.gr +574 -64
  118. package/set.md +634 -54
  119. package/stack.gr +181 -64
  120. package/stack.md +271 -42
  121. package/string.gr +453 -533
  122. package/string.md +241 -151
  123. package/uint16.gr +369 -0
  124. package/uint16.md +585 -0
  125. package/uint32.gr +470 -0
  126. package/uint32.md +737 -0
  127. package/uint64.gr +471 -0
  128. package/uint64.md +737 -0
  129. package/uint8.gr +369 -0
  130. package/uint8.md +585 -0
  131. package/uri.gr +1093 -0
  132. package/uri.md +477 -0
  133. package/{sys → wasi}/file.gr +914 -500
  134. package/{sys → wasi}/file.md +454 -50
  135. package/wasi/process.gr +292 -0
  136. package/{sys → wasi}/process.md +164 -6
  137. package/wasi/random.gr +77 -0
  138. package/wasi/random.md +80 -0
  139. package/{sys → wasi}/time.gr +15 -22
  140. package/{sys → wasi}/time.md +5 -5
  141. package/immutablearray.gr +0 -929
  142. package/immutablearray.md +0 -1038
  143. package/immutablemap.gr +0 -493
  144. package/immutablemap.md +0 -479
  145. package/immutablepriorityqueue.gr +0 -360
  146. package/immutablepriorityqueue.md +0 -291
  147. package/immutableset.gr +0 -498
  148. package/immutableset.md +0 -449
  149. package/runtime/debug.gr +0 -2
  150. package/runtime/debug.md +0 -6
  151. package/runtime/unsafe/errors.gr +0 -36
  152. package/runtime/unsafe/errors.md +0 -204
  153. package/sys/process.gr +0 -254
  154. package/sys/random.gr +0 -79
  155. package/sys/random.md +0 -66
package/uint8.gr ADDED
@@ -0,0 +1,369 @@
1
+ /**
2
+ * Utilities for working with the Uint8 type.
3
+ * @example from "uint8" include Uint8
4
+ *
5
+ * @since v0.6.0
6
+ */
7
+ module Uint8
8
+
9
+ from "runtime/unsafe/wasmi32" include WasmI32
10
+ use WasmI32.{ (+), (-), (*), (&), (|), (^), (<<), (>>>), (==), (!=) }
11
+ from "runtime/numbers" include Numbers
12
+ use Numbers.{
13
+ coerceNumberToUint8 as fromNumber,
14
+ coerceUint8ToNumber as toNumber,
15
+ }
16
+ from "runtime/dataStructures" include DataStructures
17
+ use DataStructures.{ tagUint8, untagUint8 }
18
+
19
+ @unsafe
20
+ let _TAG_BYTE = 0b11010n
21
+ @unsafe
22
+ let _REMOVE_EXTRA_MASK = 0x0000ffffn
23
+ @unsafe
24
+ let _DATA_MASK = 0xff00n
25
+ @unsafe
26
+ let _UNTAGGED_DATA_MASK = 0xffn
27
+
28
+ provide { fromNumber, toNumber }
29
+
30
+ /**
31
+ * Converts an Int8 to a Uint8.
32
+ *
33
+ * @param number: The value to convert
34
+ * @returns The Int8 represented as a Uint8
35
+ *
36
+ * @since v0.6.0
37
+ */
38
+ @unsafe
39
+ provide let fromInt8 = (number: Int8) => {
40
+ let x = WasmI32.fromGrain(number)
41
+ // Trick: convert from Int8 tag 1010 to Uint8 tag 11010
42
+ let result = x | 0b10000n
43
+ WasmI32.toGrain(result & _REMOVE_EXTRA_MASK): Uint8
44
+ }
45
+
46
+ /**
47
+ * Increments the value by one.
48
+ *
49
+ * @param value: The value to increment
50
+ * @returns The incremented value
51
+ *
52
+ * @since v0.6.0
53
+ */
54
+ @unsafe
55
+ provide let incr = (value: Uint8) => {
56
+ let value = WasmI32.fromGrain(value)
57
+ // Trick: since the data is at offset 8, can just add 1 << 8 == 0x100
58
+ let result = value + 0x100n
59
+ WasmI32.toGrain(result & _REMOVE_EXTRA_MASK): Uint8
60
+ }
61
+
62
+ /**
63
+ * Decrements the value by one.
64
+ *
65
+ * @param value: The value to decrement
66
+ * @returns The decremented value
67
+ *
68
+ * @since v0.6.0
69
+ */
70
+ @unsafe
71
+ provide let decr = (value: Uint8) => {
72
+ let value = WasmI32.fromGrain(value)
73
+ // Trick: since the data is at offset 8, can just subtract 1 << 8 == 0x100
74
+ let result = value - 0x100n
75
+ WasmI32.toGrain(result & _REMOVE_EXTRA_MASK): Uint8
76
+ }
77
+
78
+ /**
79
+ * Computes the sum of its operands.
80
+ *
81
+ * @param x: The first operand
82
+ * @param y: The second operand
83
+ * @returns The sum of the two operands
84
+ *
85
+ * @since v0.6.0
86
+ */
87
+ @unsafe
88
+ provide let (+) = (x: Uint8, y: Uint8) => {
89
+ // Trick: add the values as-is without shifting right 8; this will cause
90
+ // the data to be added correctly but the trailing tag bits will be corrupted:
91
+ // 11010 + 11010 = 110100; xor with 101110 to correct to 11010
92
+ let x = WasmI32.fromGrain(x)
93
+ let y = WasmI32.fromGrain(y)
94
+ let val = x + y
95
+ let tagged = val ^ 0b101110n
96
+ WasmI32.toGrain(tagged & _REMOVE_EXTRA_MASK): Uint8
97
+ }
98
+
99
+ /**
100
+ * Computes the difference of its operands.
101
+ *
102
+ * @param x: The first operand
103
+ * @param y: The second operand
104
+ * @returns The difference of the two operands
105
+ *
106
+ * @since v0.6.0
107
+ */
108
+ @unsafe
109
+ provide let (-) = (x: Uint8, y: Uint8) => {
110
+ let x = WasmI32.fromGrain(x)
111
+ let y = WasmI32.fromGrain(y)
112
+ let val = x - y
113
+ let tagged = val | _TAG_BYTE
114
+ WasmI32.toGrain(tagged & _REMOVE_EXTRA_MASK): Uint8
115
+ }
116
+
117
+ /**
118
+ * Computes the product of its operands.
119
+ *
120
+ * @param x: The first operand
121
+ * @param y: The second operand
122
+ * @returns The product of the two operands
123
+ *
124
+ * @since v0.6.0
125
+ */
126
+ @unsafe
127
+ provide let (*) = (x: Uint8, y: Uint8) => {
128
+ let x = untagUint8(x)
129
+ let y = untagUint8(y)
130
+ let val = x * y & _UNTAGGED_DATA_MASK
131
+ tagUint8(val)
132
+ }
133
+
134
+ /**
135
+ * Computes the quotient of its operands.
136
+ *
137
+ * @param x: The first operand
138
+ * @param y: The second operand
139
+ * @returns The quotient of its operands
140
+ *
141
+ * @since v0.6.0
142
+ */
143
+ @unsafe
144
+ provide let (/) = (x: Uint8, y: Uint8) => {
145
+ let x = untagUint8(x)
146
+ let y = untagUint8(y)
147
+ let val = WasmI32.divU(x, y)
148
+ tagUint8(val)
149
+ }
150
+
151
+ /**
152
+ * Computes the remainder of the division of its operands.
153
+ *
154
+ * @param x: The first operand
155
+ * @param y: The second operand
156
+ * @returns The remainder of its operands
157
+ *
158
+ * @since v0.6.0
159
+ */
160
+ @unsafe
161
+ provide let rem = (x: Uint8, y: Uint8) => {
162
+ let x = untagUint8(x)
163
+ let y = untagUint8(y)
164
+ let val = WasmI32.remU(x, y)
165
+ tagUint8(val)
166
+ }
167
+
168
+ /**
169
+ * Shifts the bits of the value left by the given number of bits.
170
+ *
171
+ * @param value: The value to shift
172
+ * @param amount: The number of bits to shift by
173
+ * @returns The shifted value
174
+ *
175
+ * @since v0.6.0
176
+ */
177
+ @unsafe
178
+ provide let (<<) = (value: Uint8, amount: Uint8) => {
179
+ // Trick: do not shift `value` right, just correct tag afterwards
180
+ let x = WasmI32.fromGrain(value) & _DATA_MASK
181
+ let y = untagUint8(amount)
182
+ let val = x << y
183
+ let tagged = val | _TAG_BYTE
184
+ WasmI32.toGrain(tagged & _REMOVE_EXTRA_MASK): Uint8
185
+ }
186
+
187
+ /**
188
+ * Shifts the bits of the value right by the given number of bits.
189
+ *
190
+ * @param value: The value to shift
191
+ * @param amount: The amount to shift by
192
+ * @returns The shifted value
193
+ *
194
+ * @since v0.6.0
195
+ */
196
+ @unsafe
197
+ provide let (>>>) = (value: Uint8, amount: Uint8) => {
198
+ // Trick: do not shift `value` right, just correct tag afterwards
199
+ let x = WasmI32.fromGrain(value)
200
+ let y = untagUint8(amount)
201
+ let val = x >>> y
202
+ let tagged = val & _DATA_MASK | _TAG_BYTE
203
+ WasmI32.toGrain(tagged): Uint8
204
+ }
205
+
206
+ /**
207
+ * Checks if the first value is equal to the second value.
208
+ *
209
+ * @param x: The first value
210
+ * @param y: The second value
211
+ * @returns `true` if the first value is equal to the second value or `false` otherwise
212
+ *
213
+ * @since v0.6.0
214
+ */
215
+ @unsafe
216
+ provide let (==) = (x: Uint8, y: Uint8) => {
217
+ let x = WasmI32.fromGrain(x)
218
+ let y = WasmI32.fromGrain(y)
219
+ x == y
220
+ }
221
+
222
+ /**
223
+ * Checks if the first value is not equal to the second value.
224
+ *
225
+ * @param x: The first value
226
+ * @param y: The second value
227
+ * @returns `true` if the first value is not equal to the second value or `false` otherwise
228
+ *
229
+ * @since v0.6.0
230
+ */
231
+ @unsafe
232
+ provide let (!=) = (x: Uint8, y: Uint8) => {
233
+ let x = WasmI32.fromGrain(x)
234
+ let y = WasmI32.fromGrain(y)
235
+ x != y
236
+ }
237
+
238
+ /**
239
+ * Checks if the first value is less than the second value.
240
+ *
241
+ * @param x: The first value
242
+ * @param y: The second value
243
+ * @returns `true` if the first value is less than the second value or `false` otherwise
244
+ *
245
+ * @since v0.6.0
246
+ */
247
+ @unsafe
248
+ provide let (<) = (x: Uint8, y: Uint8) => {
249
+ let x = WasmI32.fromGrain(x)
250
+ let y = WasmI32.fromGrain(y)
251
+ WasmI32.ltU(x, y)
252
+ }
253
+
254
+ /**
255
+ * Checks if the first value is greater than the second value.
256
+ *
257
+ * @param x: The first value
258
+ * @param y: The second value
259
+ * @returns `true` if the first value is greater than the second value or `false` otherwise
260
+ *
261
+ * @since v0.6.0
262
+ */
263
+ @unsafe
264
+ provide let (>) = (x: Uint8, y: Uint8) => {
265
+ let x = WasmI32.fromGrain(x)
266
+ let y = WasmI32.fromGrain(y)
267
+ WasmI32.gtU(x, y)
268
+ }
269
+
270
+ /**
271
+ * Checks if the first value is less than or equal to the second value.
272
+ *
273
+ * @param x: The first value
274
+ * @param y: The second value
275
+ * @returns `true` if the first value is less than or equal to the second value or `false` otherwise
276
+ *
277
+ * @since v0.6.0
278
+ */
279
+ @unsafe
280
+ provide let (<=) = (x: Uint8, y: Uint8) => {
281
+ let x = WasmI32.fromGrain(x)
282
+ let y = WasmI32.fromGrain(y)
283
+ WasmI32.leU(x, y)
284
+ }
285
+
286
+ /**
287
+ * Checks if the first value is greater than or equal to the second value.
288
+ *
289
+ * @param x: The first value
290
+ * @param y: The second value
291
+ * @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
292
+ *
293
+ * @since v0.6.0
294
+ */
295
+ @unsafe
296
+ provide let (>=) = (x: Uint8, y: Uint8) => {
297
+ let x = WasmI32.fromGrain(x)
298
+ let y = WasmI32.fromGrain(y)
299
+ WasmI32.geU(x, y)
300
+ }
301
+
302
+ /**
303
+ * Computes the bitwise NOT of the given value.
304
+ *
305
+ * @param value: The given value
306
+ * @returns Containing the inverted bits of the given value
307
+ *
308
+ * @since v0.6.0
309
+ */
310
+ @unsafe
311
+ provide let lnot = (value: Uint8) => {
312
+ let x = WasmI32.fromGrain(value)
313
+ WasmI32.toGrain(x ^ _DATA_MASK): Uint8
314
+ }
315
+
316
+ /**
317
+ * Computes the bitwise AND (`&`) on the given operands.
318
+ *
319
+ * @param x: The first operand
320
+ * @param y: The second operand
321
+ * @returns Containing a `1` in each bit position for which the corresponding bits of both operands are `1`
322
+ *
323
+ * @since v0.6.0
324
+ */
325
+ @unsafe
326
+ provide let (&) = (x: Uint8, y: Uint8) => {
327
+ // Tags getting `and`ed together is not a problem
328
+ let x = WasmI32.fromGrain(x)
329
+ let y = WasmI32.fromGrain(y)
330
+ let val = x & y
331
+ WasmI32.toGrain(val): Uint8
332
+ }
333
+
334
+ /**
335
+ * Computes the bitwise OR (`|`) on the given operands.
336
+ *
337
+ * @param x: The first operand
338
+ * @param y: The second operand
339
+ * @returns Containing a `1` in each bit position for which the corresponding bits of either or both operands are `1`
340
+ *
341
+ * @since v0.6.0
342
+ */
343
+ @unsafe
344
+ provide let (|) = (x: Uint8, y: Uint8) => {
345
+ // Tags getting `or`ed together is not a problem
346
+ let x = WasmI32.fromGrain(x)
347
+ let y = WasmI32.fromGrain(y)
348
+ let val = x | y
349
+ WasmI32.toGrain(val): Uint8
350
+ }
351
+
352
+ /**
353
+ * Computes the bitwise XOR (`^`) on the given operands.
354
+ *
355
+ * @param x: The first operand
356
+ * @param y: The second operand
357
+ * @returns Containing a `1` in each bit position for which the corresponding bits of either but not both operands are `1`
358
+ *
359
+ * @since v0.6.0
360
+ */
361
+ @unsafe
362
+ provide let (^) = (x: Uint8, y: Uint8) => {
363
+ use WasmI32.{ (|) }
364
+ // Tags getting `xor`ed together will cancel each other out; add back tag with or
365
+ let x = WasmI32.fromGrain(x)
366
+ let y = WasmI32.fromGrain(y)
367
+ let val = x ^ y
368
+ WasmI32.toGrain(val | _TAG_BYTE): Uint8
369
+ }