@grain/stdlib 0.4.6 → 0.5.2

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 +93 -0
  2. package/array.gr +18 -18
  3. package/array.md +18 -18
  4. package/bigint.gr +497 -0
  5. package/bigint.md +811 -0
  6. package/buffer.gr +59 -223
  7. package/buffer.md +24 -17
  8. package/bytes.gr +100 -202
  9. package/bytes.md +19 -0
  10. package/char.gr +63 -133
  11. package/exception.gr +28 -2
  12. package/exception.md +43 -0
  13. package/float32.gr +76 -95
  14. package/float32.md +69 -30
  15. package/float64.gr +81 -95
  16. package/float64.md +69 -30
  17. package/hash.gr +37 -37
  18. package/int32.gr +152 -198
  19. package/int32.md +104 -0
  20. package/int64.gr +151 -197
  21. package/int64.md +104 -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 +111 -54
  27. package/number.md +100 -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 +91 -41
  52. package/runtime/numberUtils.md +54 -0
  53. package/runtime/numbers.gr +1049 -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 +194 -329
  80. package/string.md +3 -3
  81. package/sys/file.gr +245 -429
  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.gr CHANGED
@@ -6,16 +6,68 @@
6
6
  */
7
7
  import WasmI32 from "runtime/unsafe/wasmi32"
8
8
  import WasmF64 from "runtime/unsafe/wasmf64"
9
- import Memory from "runtime/unsafe/memory"
10
- import {
11
- newFloat64
12
- } from "runtime/dataStructures"
9
+ import { newFloat64 } from "runtime/dataStructures"
13
10
 
14
11
  import {
15
12
  coerceNumberToFloat64 as fromNumber,
16
- coerceFloat64ToNumber as toNumber
13
+ coerceFloat64ToNumber as toNumber,
17
14
  } from "runtime/numbers"
18
15
 
16
+ /**
17
+ * @section Constants: Float64 constant values.
18
+ */
19
+
20
+ /**
21
+ * Infinity represented as a Float64 value.
22
+ *
23
+ * @since v0.4.0
24
+ */
25
+ @unsafe
26
+ export let infinity = {
27
+ let ptr = newFloat64(
28
+ WasmF64.reinterpretI64(
29
+ 0b0111111111110000000000000000000000000000000000000000000000000000N
30
+ )
31
+ )
32
+ WasmI32.toGrain(ptr): Float64
33
+ }
34
+
35
+ /**
36
+ * NaN (Not a Number) represented as a Float64 value.
37
+ *
38
+ * @since v0.4.0
39
+ */
40
+ @unsafe
41
+ export let nan = {
42
+ let ptr = newFloat64(
43
+ WasmF64.reinterpretI64(
44
+ 0b0111111111110000000000000000000000000000000000000000000000000001N
45
+ )
46
+ )
47
+ WasmI32.toGrain(ptr): Float64
48
+ }
49
+
50
+ /**
51
+ * Pi represented as a Float64 value.
52
+ *
53
+ * @since v0.5.2
54
+ */
55
+ export let pi = 3.141592653589793d
56
+
57
+ /**
58
+ * Tau represented as a Float64 value.
59
+ *
60
+ * @since v0.5.2
61
+ */
62
+ export let tau = 6.283185307179586d
63
+
64
+ /**
65
+ * Euler's number represented as a Float64 value.
66
+ *
67
+ * @since v0.5.2
68
+ */
69
+ export let e = 2.718281828459045d
70
+
19
71
  /**
20
72
  * @section Conversions: Functions for converting between Numbers and the Float64 type.
21
73
  */
@@ -53,16 +105,12 @@ export toNumber
53
105
  *
54
106
  * @since v0.2.0
55
107
  */
56
- @disableGC
57
- export let rec add = (x: Float64, y: Float64) => {
108
+ @unsafe
109
+ export let add = (x: Float64, y: Float64) => {
58
110
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
59
111
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
60
112
  let ptr = newFloat64(WasmF64.add(xv, yv))
61
- let ret = WasmI32.toGrain(ptr) : Float64
62
- Memory.decRef(WasmI32.fromGrain(x))
63
- Memory.decRef(WasmI32.fromGrain(y))
64
- Memory.decRef(WasmI32.fromGrain(add))
65
- ret
113
+ WasmI32.toGrain(ptr): Float64
66
114
  }
67
115
 
68
116
  /**
@@ -74,16 +122,12 @@ export let rec add = (x: Float64, y: Float64) => {
74
122
  *
75
123
  * @since v0.2.0
76
124
  */
77
- @disableGC
78
- export let rec sub = (x: Float64, y: Float64) => {
125
+ @unsafe
126
+ export let sub = (x: Float64, y: Float64) => {
79
127
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
80
128
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
81
129
  let ptr = newFloat64(WasmF64.sub(xv, yv))
82
- let ret = WasmI32.toGrain(ptr) : Float64
83
- Memory.decRef(WasmI32.fromGrain(x))
84
- Memory.decRef(WasmI32.fromGrain(y))
85
- Memory.decRef(WasmI32.fromGrain(sub))
86
- ret
130
+ WasmI32.toGrain(ptr): Float64
87
131
  }
88
132
 
89
133
  /**
@@ -95,16 +139,12 @@ export let rec sub = (x: Float64, y: Float64) => {
95
139
  *
96
140
  * @since v0.2.0
97
141
  */
98
- @disableGC
99
- export let rec mul = (x: Float64, y: Float64) => {
142
+ @unsafe
143
+ export let mul = (x: Float64, y: Float64) => {
100
144
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
101
145
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
102
146
  let ptr = newFloat64(WasmF64.mul(xv, yv))
103
- let ret = WasmI32.toGrain(ptr) : Float64
104
- Memory.decRef(WasmI32.fromGrain(x))
105
- Memory.decRef(WasmI32.fromGrain(y))
106
- Memory.decRef(WasmI32.fromGrain(mul))
107
- ret
147
+ WasmI32.toGrain(ptr): Float64
108
148
  }
109
149
 
110
150
  /**
@@ -116,16 +156,12 @@ export let rec mul = (x: Float64, y: Float64) => {
116
156
  *
117
157
  * @since v0.2.0
118
158
  */
119
- @disableGC
120
- export let rec div = (x: Float64, y: Float64) => {
159
+ @unsafe
160
+ export let div = (x: Float64, y: Float64) => {
121
161
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
122
162
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
123
163
  let ptr = newFloat64(WasmF64.div(xv, yv))
124
- let ret = WasmI32.toGrain(ptr) : Float64
125
- Memory.decRef(WasmI32.fromGrain(x))
126
- Memory.decRef(WasmI32.fromGrain(y))
127
- Memory.decRef(WasmI32.fromGrain(div))
128
- ret
164
+ WasmI32.toGrain(ptr): Float64
129
165
  }
130
166
 
131
167
  /**
@@ -141,15 +177,11 @@ export let rec div = (x: Float64, y: Float64) => {
141
177
  *
142
178
  * @since v0.2.0
143
179
  */
144
- @disableGC
145
- export let rec lt = (x: Float64, y: Float64) => {
180
+ @unsafe
181
+ export let lt = (x: Float64, y: Float64) => {
146
182
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
147
183
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
148
- let ret = WasmF64.lt(xv, yv)
149
- Memory.decRef(WasmI32.fromGrain(x))
150
- Memory.decRef(WasmI32.fromGrain(y))
151
- Memory.decRef(WasmI32.fromGrain(lt))
152
- ret
184
+ WasmF64.lt(xv, yv)
153
185
  }
154
186
 
155
187
  /**
@@ -161,15 +193,11 @@ export let rec lt = (x: Float64, y: Float64) => {
161
193
  *
162
194
  * @since v0.2.0
163
195
  */
164
- @disableGC
165
- export let rec gt = (x: Float64, y: Float64) => {
196
+ @unsafe
197
+ export let gt = (x: Float64, y: Float64) => {
166
198
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
167
199
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
168
- let ret = WasmF64.gt(xv, yv)
169
- Memory.decRef(WasmI32.fromGrain(x))
170
- Memory.decRef(WasmI32.fromGrain(y))
171
- Memory.decRef(WasmI32.fromGrain(gt))
172
- ret
200
+ WasmF64.gt(xv, yv)
173
201
  }
174
202
 
175
203
  /**
@@ -181,15 +209,11 @@ export let rec gt = (x: Float64, y: Float64) => {
181
209
  *
182
210
  * @since v0.2.0
183
211
  */
184
- @disableGC
185
- export let rec lte = (x: Float64, y: Float64) => {
212
+ @unsafe
213
+ export let lte = (x: Float64, y: Float64) => {
186
214
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
187
215
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
188
- let ret = WasmF64.le(xv, yv)
189
- Memory.decRef(WasmI32.fromGrain(x))
190
- Memory.decRef(WasmI32.fromGrain(y))
191
- Memory.decRef(WasmI32.fromGrain(lte))
192
- ret
216
+ WasmF64.le(xv, yv)
193
217
  }
194
218
 
195
219
  /**
@@ -201,47 +225,9 @@ export let rec lte = (x: Float64, y: Float64) => {
201
225
  *
202
226
  * @since v0.2.0
203
227
  */
204
- @disableGC
205
- export let rec gte = (x: Float64, y: Float64) => {
228
+ @unsafe
229
+ export let gte = (x: Float64, y: Float64) => {
206
230
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
207
231
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
208
- let ret = WasmF64.ge(xv, yv)
209
- Memory.decRef(WasmI32.fromGrain(x))
210
- Memory.decRef(WasmI32.fromGrain(y))
211
- Memory.decRef(WasmI32.fromGrain(gte))
212
- ret
232
+ WasmF64.ge(xv, yv)
213
233
  }
214
-
215
- /**
216
- * @section Constants: Float64 constant values.
217
- */
218
-
219
- @disableGC
220
- let rec makeInfinity = () => {
221
- let ptr = newFloat64(WasmF64.reinterpretI64(0b0111111111110000000000000000000000000000000000000000000000000000N))
222
- let ret = WasmI32.toGrain(ptr) : Float64
223
- Memory.decRef(WasmI32.fromGrain(makeInfinity))
224
- ret
225
- }
226
-
227
- /**
228
- * Infinity represented as a Float64 value.
229
- *
230
- * @since v0.4.0
231
- */
232
- export let infinity = makeInfinity()
233
-
234
- @disableGC
235
- let rec makeNaN = () => {
236
- let ptr = newFloat64(WasmF64.reinterpretI64(0b0111111111110000000000000000000000000000000000000000000000000001N))
237
- let ret = WasmI32.toGrain(ptr) : Float64
238
- Memory.decRef(WasmI32.fromGrain(makeNaN))
239
- ret
240
- }
241
-
242
- /**
243
- * NaN (Not a Number) represented as a Float64 value.
244
- *
245
- * @since v0.4.0
246
- */
247
- export let nan = makeNaN()
package/float64.md CHANGED
@@ -13,6 +13,75 @@ No other changes yet.
13
13
  import Float64 from "float64"
14
14
  ```
15
15
 
16
+ ## Constants
17
+
18
+ Float64 constant values.
19
+
20
+ ### Float64.**infinity**
21
+
22
+ <details disabled>
23
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
24
+ No other changes yet.
25
+ </details>
26
+
27
+ ```grain
28
+ infinity : Float64
29
+ ```
30
+
31
+ Infinity represented as a Float64 value.
32
+
33
+ ### Float64.**nan**
34
+
35
+ <details disabled>
36
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
37
+ No other changes yet.
38
+ </details>
39
+
40
+ ```grain
41
+ nan : Float64
42
+ ```
43
+
44
+ NaN (Not a Number) represented as a Float64 value.
45
+
46
+ ### Float64.**pi**
47
+
48
+ <details disabled>
49
+ <summary tabindex="-1">Added in <code>next</code></summary>
50
+ No other changes yet.
51
+ </details>
52
+
53
+ ```grain
54
+ pi : Float64
55
+ ```
56
+
57
+ Pi represented as a Float64 value.
58
+
59
+ ### Float64.**tau**
60
+
61
+ <details disabled>
62
+ <summary tabindex="-1">Added in <code>next</code></summary>
63
+ No other changes yet.
64
+ </details>
65
+
66
+ ```grain
67
+ tau : Float64
68
+ ```
69
+
70
+ Tau represented as a Float64 value.
71
+
72
+ ### Float64.**e**
73
+
74
+ <details disabled>
75
+ <summary tabindex="-1">Added in <code>next</code></summary>
76
+ No other changes yet.
77
+ </details>
78
+
79
+ ```grain
80
+ e : Float64
81
+ ```
82
+
83
+ Euler's number represented as a Float64 value.
84
+
16
85
  ## Conversions
17
86
 
18
87
  Functions for converting between Numbers and the Float64 type.
@@ -283,33 +352,3 @@ Returns:
283
352
  |----|-----------|
284
353
  |`Bool`|`true` if the first value is greater than or equal to the second value or `false` otherwise|
285
354
 
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
  }