@grain/stdlib 0.4.6 → 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.
- package/CHANGELOG.md +73 -0
- package/array.gr +18 -18
- package/array.md +18 -18
- package/bigint.gr +497 -0
- package/bigint.md +811 -0
- package/buffer.gr +49 -213
- package/buffer.md +24 -17
- package/bytes.gr +100 -202
- package/bytes.md +19 -0
- package/char.gr +63 -133
- package/exception.md +6 -0
- package/float32.gr +39 -78
- package/float64.gr +43 -78
- package/hash.gr +37 -37
- package/int32.gr +152 -198
- package/int32.md +104 -0
- package/int64.gr +151 -197
- package/int64.md +104 -0
- package/list.gr +467 -70
- package/list.md +1141 -0
- package/map.gr +192 -7
- package/map.md +525 -0
- package/number.gr +30 -54
- package/number.md +3 -3
- package/option.md +1 -1
- package/package.json +3 -3
- package/pervasives.gr +499 -59
- package/pervasives.md +1116 -0
- package/queue.gr +4 -0
- package/queue.md +10 -0
- package/random.gr +196 -0
- package/random.md +179 -0
- package/regex.gr +1833 -842
- package/regex.md +11 -11
- package/result.md +1 -1
- package/runtime/bigint.gr +2045 -0
- package/runtime/bigint.md +326 -0
- package/runtime/dataStructures.gr +99 -278
- package/runtime/dataStructures.md +391 -0
- package/runtime/debug.md +6 -0
- package/runtime/equal.gr +5 -23
- package/runtime/equal.md +6 -0
- package/runtime/exception.md +30 -0
- package/runtime/gc.gr +20 -3
- package/runtime/gc.md +36 -0
- package/runtime/malloc.gr +13 -11
- package/runtime/malloc.md +55 -0
- package/runtime/numberUtils.gr +91 -41
- package/runtime/numberUtils.md +54 -0
- package/runtime/numbers.gr +1043 -391
- package/runtime/numbers.md +300 -0
- package/runtime/string.gr +136 -230
- package/runtime/string.md +24 -0
- package/runtime/stringUtils.gr +58 -38
- package/runtime/stringUtils.md +6 -0
- package/runtime/unsafe/constants.gr +17 -0
- package/runtime/unsafe/constants.md +72 -0
- package/runtime/unsafe/conv.md +71 -0
- package/runtime/unsafe/errors.md +204 -0
- package/runtime/unsafe/memory.md +54 -0
- package/runtime/unsafe/printWasm.md +24 -0
- package/runtime/unsafe/tags.gr +9 -8
- package/runtime/unsafe/tags.md +120 -0
- package/runtime/unsafe/wasmf32.md +168 -0
- package/runtime/unsafe/wasmf64.md +168 -0
- package/runtime/unsafe/wasmi32.md +282 -0
- package/runtime/unsafe/wasmi64.md +300 -0
- package/runtime/utils/printing.gr +62 -0
- package/runtime/utils/printing.md +18 -0
- package/runtime/wasi.gr +1 -1
- package/runtime/wasi.md +839 -0
- package/set.gr +17 -8
- package/set.md +24 -21
- package/stack.gr +3 -3
- package/stack.md +4 -6
- package/string.gr +194 -329
- package/string.md +3 -3
- package/sys/file.gr +245 -429
- package/sys/process.gr +27 -45
- package/sys/random.gr +47 -16
- package/sys/random.md +38 -0
- package/sys/time.gr +11 -27
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
|
-
|
|
201
|
-
|
|
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
|
-
|
|
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
|
-
|
|
243
|
-
|
|
244
|
-
Memory.decRef(WasmI32.fromGrain(hash))
|
|
245
|
-
Memory.decRef(WasmI32.fromGrain(anything))
|
|
246
|
-
|
|
247
|
-
result
|
|
247
|
+
tagSimpleNumber(h)
|
|
248
248
|
}
|