@grain/stdlib 0.4.5 → 0.5.1

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 (85) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +1 -1
  3. package/array.gr +18 -18
  4. package/array.md +18 -18
  5. package/bigint.gr +497 -0
  6. package/bigint.md +811 -0
  7. package/buffer.gr +49 -213
  8. package/buffer.md +24 -17
  9. package/bytes.gr +100 -202
  10. package/bytes.md +19 -0
  11. package/char.gr +63 -133
  12. package/exception.md +6 -0
  13. package/float32.gr +159 -82
  14. package/float32.md +315 -0
  15. package/float64.gr +163 -82
  16. package/float64.md +315 -0
  17. package/hash.gr +37 -37
  18. package/int32.gr +479 -230
  19. package/int32.md +937 -0
  20. package/int64.gr +479 -230
  21. package/int64.md +937 -0
  22. package/list.gr +467 -70
  23. package/list.md +1141 -0
  24. package/map.gr +192 -7
  25. package/map.md +525 -0
  26. package/number.gr +30 -54
  27. package/number.md +3 -3
  28. package/option.md +1 -1
  29. package/package.json +3 -3
  30. package/pervasives.gr +499 -59
  31. package/pervasives.md +1116 -0
  32. package/queue.gr +4 -0
  33. package/queue.md +10 -0
  34. package/random.gr +196 -0
  35. package/random.md +179 -0
  36. package/regex.gr +1833 -842
  37. package/regex.md +11 -11
  38. package/result.md +1 -1
  39. package/runtime/bigint.gr +2045 -0
  40. package/runtime/bigint.md +326 -0
  41. package/runtime/dataStructures.gr +99 -278
  42. package/runtime/dataStructures.md +391 -0
  43. package/runtime/debug.md +6 -0
  44. package/runtime/equal.gr +5 -23
  45. package/runtime/equal.md +6 -0
  46. package/runtime/exception.md +30 -0
  47. package/runtime/gc.gr +20 -3
  48. package/runtime/gc.md +36 -0
  49. package/runtime/malloc.gr +13 -11
  50. package/runtime/malloc.md +55 -0
  51. package/runtime/numberUtils.gr +93 -41
  52. package/runtime/numberUtils.md +54 -0
  53. package/runtime/numbers.gr +1043 -391
  54. package/runtime/numbers.md +300 -0
  55. package/runtime/string.gr +136 -230
  56. package/runtime/string.md +24 -0
  57. package/runtime/stringUtils.gr +58 -38
  58. package/runtime/stringUtils.md +6 -0
  59. package/runtime/unsafe/constants.gr +17 -0
  60. package/runtime/unsafe/constants.md +72 -0
  61. package/runtime/unsafe/conv.md +71 -0
  62. package/runtime/unsafe/errors.md +204 -0
  63. package/runtime/unsafe/memory.md +54 -0
  64. package/runtime/unsafe/printWasm.md +24 -0
  65. package/runtime/unsafe/tags.gr +9 -8
  66. package/runtime/unsafe/tags.md +120 -0
  67. package/runtime/unsafe/wasmf32.md +168 -0
  68. package/runtime/unsafe/wasmf64.md +168 -0
  69. package/runtime/unsafe/wasmi32.md +282 -0
  70. package/runtime/unsafe/wasmi64.md +300 -0
  71. package/runtime/utils/printing.gr +62 -0
  72. package/runtime/utils/printing.md +18 -0
  73. package/runtime/wasi.gr +1 -1
  74. package/runtime/wasi.md +839 -0
  75. package/set.gr +17 -8
  76. package/set.md +24 -21
  77. package/stack.gr +3 -3
  78. package/stack.md +4 -6
  79. package/string.gr +196 -331
  80. package/string.md +3 -3
  81. package/sys/file.gr +246 -430
  82. package/sys/process.gr +27 -45
  83. package/sys/random.gr +47 -16
  84. package/sys/random.md +38 -0
  85. package/sys/time.gr +11 -27
package/float64.md ADDED
@@ -0,0 +1,315 @@
1
+ ---
2
+ title: Float64
3
+ ---
4
+
5
+ Utilities for working with the Float64 type.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ import Float64 from "float64"
14
+ ```
15
+
16
+ ## Conversions
17
+
18
+ Functions for converting between Numbers and the Float64 type.
19
+
20
+ ### Float64.**fromNumber**
21
+
22
+ <details disabled>
23
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
24
+ No other changes yet.
25
+ </details>
26
+
27
+ ```grain
28
+ fromNumber : Number -> Float64
29
+ ```
30
+
31
+ Converts a Number to a Float64.
32
+
33
+ Parameters:
34
+
35
+ |param|type|description|
36
+ |-----|----|-----------|
37
+ |`number`|`Number`|The value to convert|
38
+
39
+ Returns:
40
+
41
+ |type|description|
42
+ |----|-----------|
43
+ |`Float64`|The Number represented as a Float64|
44
+
45
+ ### Float64.**toNumber**
46
+
47
+ <details disabled>
48
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
49
+ No other changes yet.
50
+ </details>
51
+
52
+ ```grain
53
+ toNumber : Float64 -> Number
54
+ ```
55
+
56
+ Converts a Float64 to a Number.
57
+
58
+ Parameters:
59
+
60
+ |param|type|description|
61
+ |-----|----|-----------|
62
+ |`float`|`Float64`|The value to convert|
63
+
64
+ Returns:
65
+
66
+ |type|description|
67
+ |----|-----------|
68
+ |`Number`|The Float64 represented as a Number|
69
+
70
+ ## Operations
71
+
72
+ Mathematical operations for Float64 values.
73
+
74
+ ### Float64.**add**
75
+
76
+ <details disabled>
77
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
78
+ No other changes yet.
79
+ </details>
80
+
81
+ ```grain
82
+ add : (Float64, Float64) -> Float64
83
+ ```
84
+
85
+ Computes the sum of its operands.
86
+
87
+ Parameters:
88
+
89
+ |param|type|description|
90
+ |-----|----|-----------|
91
+ |`x`|`Float64`|The first operand|
92
+ |`y`|`Float64`|The second operand|
93
+
94
+ Returns:
95
+
96
+ |type|description|
97
+ |----|-----------|
98
+ |`Float64`|The sum of the two operands|
99
+
100
+ ### Float64.**sub**
101
+
102
+ <details disabled>
103
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
104
+ No other changes yet.
105
+ </details>
106
+
107
+ ```grain
108
+ sub : (Float64, Float64) -> Float64
109
+ ```
110
+
111
+ Computes the difference of its operands.
112
+
113
+ Parameters:
114
+
115
+ |param|type|description|
116
+ |-----|----|-----------|
117
+ |`x`|`Float64`|The first operand|
118
+ |`y`|`Float64`|The second operand|
119
+
120
+ Returns:
121
+
122
+ |type|description|
123
+ |----|-----------|
124
+ |`Float64`|The difference of the two operands|
125
+
126
+ ### Float64.**mul**
127
+
128
+ <details disabled>
129
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
130
+ No other changes yet.
131
+ </details>
132
+
133
+ ```grain
134
+ mul : (Float64, Float64) -> Float64
135
+ ```
136
+
137
+ Computes the product of its operands.
138
+
139
+ Parameters:
140
+
141
+ |param|type|description|
142
+ |-----|----|-----------|
143
+ |`x`|`Float64`|The first operand|
144
+ |`y`|`Float64`|The second operand|
145
+
146
+ Returns:
147
+
148
+ |type|description|
149
+ |----|-----------|
150
+ |`Float64`|The product of the two operands|
151
+
152
+ ### Float64.**div**
153
+
154
+ <details disabled>
155
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
156
+ No other changes yet.
157
+ </details>
158
+
159
+ ```grain
160
+ div : (Float64, Float64) -> Float64
161
+ ```
162
+
163
+ Computes the quotient of its operands.
164
+
165
+ Parameters:
166
+
167
+ |param|type|description|
168
+ |-----|----|-----------|
169
+ |`x`|`Float64`|The first operand|
170
+ |`y`|`Float64`|The second operand|
171
+
172
+ Returns:
173
+
174
+ |type|description|
175
+ |----|-----------|
176
+ |`Float64`|The quotient of the two operands|
177
+
178
+ ## Comparisons
179
+
180
+ Functions for comparing Float64 values.
181
+
182
+ ### Float64.**lt**
183
+
184
+ <details disabled>
185
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
186
+ No other changes yet.
187
+ </details>
188
+
189
+ ```grain
190
+ lt : (Float64, Float64) -> Bool
191
+ ```
192
+
193
+ Checks if the first value is less than the second value.
194
+
195
+ Parameters:
196
+
197
+ |param|type|description|
198
+ |-----|----|-----------|
199
+ |`x`|`Float64`|The first value|
200
+ |`y`|`Float64`|The second value|
201
+
202
+ Returns:
203
+
204
+ |type|description|
205
+ |----|-----------|
206
+ |`Bool`|`true` if the first value is less than the second value or `false` otherwise|
207
+
208
+ ### Float64.**gt**
209
+
210
+ <details disabled>
211
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
212
+ No other changes yet.
213
+ </details>
214
+
215
+ ```grain
216
+ gt : (Float64, Float64) -> Bool
217
+ ```
218
+
219
+ Checks if the first value is greater than the second value.
220
+
221
+ Parameters:
222
+
223
+ |param|type|description|
224
+ |-----|----|-----------|
225
+ |`x`|`Float64`|The first value|
226
+ |`y`|`Float64`|The second value|
227
+
228
+ Returns:
229
+
230
+ |type|description|
231
+ |----|-----------|
232
+ |`Bool`|`true` if the first value is greater than the second value or `false` otherwise|
233
+
234
+ ### Float64.**lte**
235
+
236
+ <details disabled>
237
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
238
+ No other changes yet.
239
+ </details>
240
+
241
+ ```grain
242
+ lte : (Float64, Float64) -> Bool
243
+ ```
244
+
245
+ Checks if the first value is less than or equal to the second value.
246
+
247
+ Parameters:
248
+
249
+ |param|type|description|
250
+ |-----|----|-----------|
251
+ |`x`|`Float64`|The first value|
252
+ |`y`|`Float64`|The second value|
253
+
254
+ Returns:
255
+
256
+ |type|description|
257
+ |----|-----------|
258
+ |`Bool`|`true` if the first value is less than or equal to the second value or `false` otherwise|
259
+
260
+ ### Float64.**gte**
261
+
262
+ <details disabled>
263
+ <summary tabindex="-1">Added in <code>0.2.0</code></summary>
264
+ No other changes yet.
265
+ </details>
266
+
267
+ ```grain
268
+ gte : (Float64, Float64) -> Bool
269
+ ```
270
+
271
+ Checks if the first value is greater than or equal to the second value.
272
+
273
+ Parameters:
274
+
275
+ |param|type|description|
276
+ |-----|----|-----------|
277
+ |`x`|`Float64`|The first value|
278
+ |`y`|`Float64`|The second value|
279
+
280
+ Returns:
281
+
282
+ |type|description|
283
+ |----|-----------|
284
+ |`Bool`|`true` if the first value is greater than or equal to the second value or `false` otherwise|
285
+
286
+ ## Constants
287
+
288
+ Float64 constant values.
289
+
290
+ ### Float64.**infinity**
291
+
292
+ <details disabled>
293
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
294
+ No other changes yet.
295
+ </details>
296
+
297
+ ```grain
298
+ infinity : Float64
299
+ ```
300
+
301
+ Infinity represented as a Float64 value.
302
+
303
+ ### Float64.**nan**
304
+
305
+ <details disabled>
306
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
307
+ No other changes yet.
308
+ </details>
309
+
310
+ ```grain
311
+ nan : Float64
312
+ ```
313
+
314
+ NaN (Not a Number) represented as a Float64 value.
315
+
package/hash.gr CHANGED
@@ -1,5 +1,3 @@
1
- /* grainc-flags --no-gc */
2
-
3
1
  /**
4
2
  * @module Hash: Utilities for hashing any value.
5
3
  * @example import Hash from "hash"
@@ -27,34 +25,42 @@ import WasmI32, {
27
25
  gtU as (>),
28
26
  ltU as (<),
29
27
  } from "runtime/unsafe/wasmi32"
28
+ import WasmI64 from "runtime/unsafe/wasmi64"
30
29
  import Tags from "runtime/unsafe/tags"
31
- import Memory from "runtime/unsafe/memory"
32
30
 
33
31
  import { tagSimpleNumber } from "runtime/dataStructures"
34
32
  import { coerceNumberToWasmI32 } from "runtime/numbers"
33
+ import BI from "runtime/bigint"
35
34
 
36
35
  import Random from "sys/random"
37
36
  import Result from "result"
38
37
 
38
+ @unsafe
39
39
  let seed = {
40
- Memory.incRef(WasmI32.fromGrain(Random.random))
41
- Memory.incRef(WasmI32.fromGrain(Result.unwrap))
42
40
  let random = Random.random()
43
- Memory.incRef(WasmI32.fromGrain(random))
44
41
  coerceNumberToWasmI32(Result.unwrap(random))
45
42
  }
46
43
 
44
+ @unsafe
47
45
  let _MAX_HASH_DEPTH = 31n
48
46
 
47
+ @unsafe
49
48
  let c1 = 0xcc9e2d51n
49
+ @unsafe
50
50
  let c2 = 0x1b873593n
51
+ @unsafe
51
52
  let r1 = 15n
53
+ @unsafe
52
54
  let r2 = 13n
55
+ @unsafe
53
56
  let m = 5n
57
+ @unsafe
54
58
  let n = 0xe6546b64n
55
59
 
60
+ @unsafe
56
61
  let mut h = seed
57
62
 
63
+ @unsafe
58
64
  let hash32 = k => {
59
65
  let mut k = k * c1
60
66
  k = WasmI32.rotl(k, r1)
@@ -65,6 +71,14 @@ let hash32 = k => {
65
71
  h = h * m + n
66
72
  }
67
73
 
74
+ @unsafe
75
+ let hash64 = k => {
76
+ // convenience function for hashing 64-bit values
77
+ hash32(WasmI32.wrapI64(k))
78
+ hash32(WasmI32.wrapI64(WasmI64.shrU(k, 32N)))
79
+ }
80
+
81
+ @unsafe
68
82
  let hashRemaining = r => {
69
83
  // Note: wasm is little-endian so no swap is necessary
70
84
 
@@ -75,6 +89,7 @@ let hashRemaining = r => {
75
89
  h = h ^ r
76
90
  }
77
91
 
92
+ @unsafe
78
93
  let finalize = len => {
79
94
  h = h ^ len
80
95
 
@@ -85,6 +100,7 @@ let finalize = len => {
85
100
  h = h ^ h >>> 16n
86
101
  }
87
102
 
103
+ @unsafe
88
104
  let rec hashOne = (val, depth) => {
89
105
  if (depth > _MAX_HASH_DEPTH) {
90
106
  void
@@ -112,22 +128,6 @@ let rec hashOne = (val, depth) => {
112
128
  if (rem != 0n) hashRemaining(rem)
113
129
  finalize(length)
114
130
  },
115
- t when t == Tags._GRAIN_CHAR_HEAP_TAG => {
116
- let word = WasmI32.load(heapPtr, 4n)
117
- // little-endian byte order
118
- let byte = word & 0xFFn
119
- let mut shift = 0n
120
- if ((byte & 0x80n) == 0x00n) {
121
- shift = 24n
122
- } else if ((byte & 0xF0n) == 0xF0n) {
123
- shift = 0n
124
- } else if ((byte & 0xE0n) == 0xE0n) {
125
- shift = 8n
126
- } else {
127
- shift = 16n
128
- }
129
- hash32(word << shift)
130
- },
131
131
  t when t == Tags._GRAIN_ADT_HEAP_TAG => {
132
132
  // moduleId
133
133
  hash32(WasmI32.load(heapPtr, 4n))
@@ -189,6 +189,15 @@ let rec hashOne = (val, depth) => {
189
189
  hash32(WasmI32.load(heapPtr, 8n))
190
190
  hash32(WasmI32.load(heapPtr, 12n))
191
191
  },
192
+ t when t == Tags._GRAIN_BIGINT_BOXED_NUM_TAG => {
193
+ // TODO(#1187): should include fixint size once implemented
194
+ let size = BI.getSize(heapPtr)
195
+ hash32(size)
196
+ hash32(BI.getFlags(heapPtr))
197
+ for (let mut i = 0n; i < size; i += 1n) {
198
+ hash64(BI.getLimb(heapPtr, i))
199
+ }
200
+ },
192
201
  t when t == Tags._GRAIN_FLOAT32_BOXED_NUM_TAG => {
193
202
  hash32(WasmI32.load(heapPtr, 8n))
194
203
  },
@@ -197,8 +206,8 @@ let rec hashOne = (val, depth) => {
197
206
  hash32(WasmI32.load(heapPtr, 12n))
198
207
  },
199
208
  t when t == Tags._GRAIN_RATIONAL_BOXED_NUM_TAG => {
200
- hash32(WasmI32.load(heapPtr, 8n))
201
- hash32(WasmI32.load(heapPtr, 12n))
209
+ hashOne(WasmI32.load(heapPtr, 8n), depth + 1n)
210
+ hashOne(WasmI32.load(heapPtr, 12n), depth + 1n)
202
211
  },
203
212
  _ => {
204
213
  hash32(heapPtr)
@@ -209,13 +218,8 @@ let rec hashOne = (val, depth) => {
209
218
  hash32(heapPtr)
210
219
  },
211
220
  }
212
- } else if (val == WasmI32.fromGrain(true)) {
213
- hash32(val)
214
- } else if (val == WasmI32.fromGrain(false)) {
215
- hash32(val)
216
- } else if (val == WasmI32.fromGrain(void)) {
217
- hash32(val)
218
221
  } else {
222
+ // Handle non-heap values: booleans, chars, void, etc.
219
223
  hash32(val)
220
224
  }
221
225
  }
@@ -231,7 +235,8 @@ let rec hashOne = (val, depth) => {
231
235
  *
232
236
  * @since v0.1.0
233
237
  */
234
- export let rec hash = anything => {
238
+ @unsafe
239
+ export let hash = anything => {
235
240
  h = seed
236
241
 
237
242
  hashOne(WasmI32.fromGrain(anything), 0n)
@@ -239,10 +244,5 @@ export let rec hash = anything => {
239
244
 
240
245
  // Tag the number on the way out.
241
246
  // Since Grain has proper modulus, negative numbers are okay.
242
- let result = tagSimpleNumber(h)
243
-
244
- Memory.decRef(WasmI32.fromGrain(hash))
245
- Memory.decRef(WasmI32.fromGrain(anything))
246
-
247
- result
247
+ tagSimpleNumber(h)
248
248
  }