@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
@@ -1,366 +1,187 @@
1
- /* grainc-flags --compilation-mode=runtime */
2
-
3
- import Memory from "runtime/unsafe/memory"
4
- import Tags from "runtime/unsafe/tags"
5
- import WasmI32, {
6
- add as (+),
7
- mul as (*),
8
- xor as (^),
9
- shl as (<<),
10
- } from "runtime/unsafe/wasmi32"
11
- import WasmI64 from "runtime/unsafe/wasmi64"
12
- import WasmF32 from "runtime/unsafe/wasmf32"
13
- import WasmF64 from "runtime/unsafe/wasmf64"
14
-
15
- //
16
- // For performance reasons, all functions in this module do not
17
- // decRef their arguments. Consequently, callers should not incRef them before calling.
18
- //
1
+ /* grainc-flags --no-pervasives */
19
2
 
20
3
  /**
21
4
  * Allocates a new Grain array.
22
5
  *
23
- * @param {WasmI32} numElts The number of elements to be contained in this array
24
- * @returns {WasmI32} The pointer to the array
25
- */
26
- export let allocateArray = numElts => {
27
- let arr = Memory.malloc((numElts + 2n) * 4n)
28
-
29
- WasmI32.store(arr, Tags._GRAIN_ARRAY_HEAP_TAG, 0n)
30
- WasmI32.store(arr, numElts, 4n)
31
-
32
- arr
33
- }
34
-
35
- /**
36
- * Stores an item in a Grain array.
37
- *
38
- * @param {WasmI32} array The array to store the item in
39
- * @param {WasmI32} idx The index to store the item
40
- * @param {WasmI32} item The item to store
6
+ * @param numElts: The number of elements to be contained in this array
7
+ * @returns The pointer to the array
41
8
  */
42
- export let storeInArray = (arr, idx, item) => {
43
- WasmI32.store(arr + idx * 4n, item, 8n)
44
- }
9
+ @unsafe
10
+ export primitive allocateArray: WasmI32 -> WasmI32 = "@allocate.array"
45
11
 
46
12
  /**
47
13
  * Allocates a new Grain tuple.
48
14
  *
49
- * @param {WasmI32} numElts The number of elements to be contained in this tuple
50
- * @returns {WasmI32} The pointer to the tuple
15
+ * @param numElts: The number of elements to be contained in this tuple
16
+ * @returns The pointer to the tuple
51
17
  */
52
- export let allocateTuple = numElts => {
53
- let tuple = Memory.malloc((numElts + 2n) * 4n)
54
-
55
- WasmI32.store(tuple, Tags._GRAIN_TUPLE_HEAP_TAG, 0n)
56
- WasmI32.store(tuple, numElts, 4n)
57
-
58
- tuple
59
- }
60
-
61
- /**
62
- * Stores an item in a Grain tuple.
63
- *
64
- * @param {WasmI32} tuple The tuple to store the item in
65
- * @param {WasmI32} idx The index to store the item
66
- * @param {WasmI32} item The item to store
67
- */
68
- export let storeInTuple = (tuple, idx, item) => {
69
- WasmI32.store(tuple + idx * 4n, item, 8n)
70
- }
18
+ @unsafe
19
+ export primitive allocateTuple: WasmI32 -> WasmI32 = "@allocate.tuple"
71
20
 
72
21
  /**
73
22
  * Allocates a new Grain bytes.
74
23
  *
75
- * @param {WasmI32} size The number of bytes to be contained in this buffer
76
- * @returns {WasmI32} The pointer to the bytes
24
+ * @param size: The number of bytes to be contained in this buffer
25
+ * @returns The pointer to the bytes
77
26
  */
78
- export let allocateBytes = size => {
79
- let len = size + 8n
80
- let bytes = Memory.malloc(len)
81
- Memory.fill(bytes, 0n, len)
82
- WasmI32.store(bytes, Tags._GRAIN_BYTES_HEAP_TAG, 0n)
83
- WasmI32.store(bytes, size, 4n)
84
- bytes
85
- }
27
+ @unsafe
28
+ export primitive allocateBytes: WasmI32 -> WasmI32 = "@allocate.bytes"
86
29
 
87
30
  /**
88
31
  * Allocates a new Grain string.
89
32
  *
90
- * @param {WasmI32} size The size (in bytes) of the string to allocate
91
- * @returns {WasmI32} The pointer to the string
33
+ * @param size: The size (in bytes) of the string to allocate
34
+ * @returns The pointer to the string
92
35
  */
93
- export let allocateString = size => {
94
- let str = Memory.malloc(size + 8n)
95
-
96
- WasmI32.store(str, Tags._GRAIN_STRING_HEAP_TAG, 0n)
97
- WasmI32.store(str, size, 4n)
36
+ @unsafe
37
+ export primitive allocateString: WasmI32 -> WasmI32 = "@allocate.string"
98
38
 
99
- str
100
- }
39
+ // INT32/INT64
101
40
 
102
41
  /**
103
- * Creates a new Grain string containing the given character
42
+ * Allocates a new Int32.
104
43
  *
105
- * @param {WasmI32} c The character for which to allocate a string
106
- * @returns {WasmI32} The pointer to the string
44
+ * @returns The pointer to the empty Int32
107
45
  */
108
- export let singleByteString = c => {
109
- let str = Memory.malloc(9n)
110
-
111
- WasmI32.store(str, Tags._GRAIN_STRING_HEAP_TAG, 0n)
112
- WasmI32.store(str, 1n, 4n)
113
- WasmI32.store8(str, c, 8n)
114
-
115
- str
116
- }
46
+ @unsafe
47
+ export primitive allocateInt32: () -> WasmI32 = "@allocate.int32"
117
48
 
118
49
  /**
119
- * Allocates a new Grain char.
120
- *
121
- * @returns {WasmI32} The pointer to the char
50
+ * Allocates a new Int32 with a prepopulated value
51
+ * @param value: The value to store
52
+ * @returns The pointer to the Int32
122
53
  */
123
- export let allocateChar = () => {
124
- let char = Memory.malloc(8n)
125
-
126
- WasmI32.store(char, Tags._GRAIN_CHAR_HEAP_TAG, 0n)
127
-
128
- char
129
- }
130
-
131
- // INT32/INT64
54
+ @unsafe
55
+ export primitive newInt32: WasmI32 -> WasmI32 = "@new.int32"
132
56
 
133
57
  /**
134
58
  * Allocates a new Int64.
135
59
  *
136
- * @returns {WasmI32}
60
+ * @returns The pointer to the empty Int64
137
61
  */
138
- export let allocateInt64 = () => {
139
- let ptr = Memory.malloc(16n)
140
-
141
- WasmI32.store(ptr, Tags._GRAIN_BOXED_NUM_HEAP_TAG, 0n)
142
- WasmI32.store(ptr, Tags._GRAIN_INT64_BOXED_NUM_TAG, 4n)
143
-
144
- ptr
145
- }
62
+ @unsafe
63
+ export primitive allocateInt64: () -> WasmI32 = "@allocate.int64"
146
64
 
147
65
  /**
148
66
  * Allocates a new Int64 with a prepopulated value
149
- * @param value The value to store
150
- */
151
- export let newInt64 = value => {
152
- let ptr = allocateInt64()
153
- WasmI64.store(ptr, value, 8n)
154
- ptr
155
- }
156
-
157
- /**
158
- * Returns a pointer to the heap location containing this boxed number's Int64
159
- * @param wrappedInt64 The boxed int64 to return
67
+ * @param value: The value to store
68
+ * @returns The pointer to the Int64
160
69
  */
161
- export let rawInt64Ptr = wrappedInt64 => {
162
- wrappedInt64 + 8n
163
- }
164
-
165
- export let loadInt64 = xptr => {
166
- WasmI64.load(xptr, 8n)
167
- }
168
-
169
- /**
170
- * Allocates a new Int32.
171
- *
172
- * @returns {WasmI32}
173
- */
174
- export let allocateInt32 = () => {
175
- let ptr = Memory.malloc(12n)
176
-
177
- WasmI32.store(ptr, Tags._GRAIN_BOXED_NUM_HEAP_TAG, 0n)
178
- WasmI32.store(ptr, Tags._GRAIN_INT32_BOXED_NUM_TAG, 4n)
179
-
180
- ptr
181
- }
182
-
183
- /**
184
- * Allocates a new Int32 with a prepopulated value
185
- * @param value The value to store
186
- */
187
- export let newInt32 = value => {
188
- let ptr = allocateInt32()
189
- WasmI32.store(ptr, value, 8n)
190
- ptr
191
- }
192
-
193
- /**
194
- * Returns a pointer to the heap location containing this boxed number's Int32
195
- * @param wrappedInt32 The boxed int32 to return
196
- */
197
- export let rawInt32Ptr = wrappedInt32 => {
198
- wrappedInt32 + 8n
199
- }
200
-
201
- export let loadInt32 = xptr => {
202
- WasmI32.load(xptr, 8n)
203
- }
70
+ @unsafe
71
+ export primitive newInt64: WasmI64 -> WasmI32 = "@new.int64"
204
72
 
205
73
  // FLOATS
206
74
 
207
75
  /**
208
76
  * Allocates a new Float32.
209
77
  *
210
- * @returns {WasmI32}
78
+ * @returns The pointer to the empty Float32
211
79
  */
212
- export let allocateFloat32 = () => {
213
- let ptr = Memory.malloc(12n)
214
-
215
- WasmI32.store(ptr, Tags._GRAIN_BOXED_NUM_HEAP_TAG, 0n)
216
- WasmI32.store(ptr, Tags._GRAIN_FLOAT32_BOXED_NUM_TAG, 4n)
217
-
218
- ptr
219
- }
80
+ @unsafe
81
+ export primitive allocateFloat32: () -> WasmI32 = "@allocate.float32"
220
82
 
221
83
  /**
222
84
  * Allocates a new Float32 with a prepopulated value
223
- * @param value The value to store
85
+ * @param value: The value to store
86
+ * @returns the pointer to the Float32
224
87
  */
225
- export let newFloat32 = value => {
226
- let ptr = allocateFloat32()
227
- WasmF32.store(ptr, value, 8n)
228
- ptr
229
- }
230
-
231
- /**
232
- * Returns a pointer to the heap location containing this boxed number's Float32
233
- * @param wrappedFloat32 The boxed float32 to return
234
- */
235
- export let rawFloat32Ptr = wrappedFloat32 => {
236
- wrappedFloat32 + 8n
237
- }
238
-
239
- export let loadFloat32 = xptr => {
240
- WasmF32.load(xptr, 8n)
241
- }
88
+ @unsafe
89
+ export primitive newFloat32: WasmF32 -> WasmI32 = "@new.float32"
242
90
 
243
91
  /**
244
92
  * Allocates a new Float64.
245
93
  *
246
- * @returns {WasmI32}
94
+ * @returns The pointer to the empty Float64
247
95
  */
248
- export let allocateFloat64 = () => {
249
- let ptr = Memory.malloc(16n)
250
-
251
- WasmI32.store(ptr, Tags._GRAIN_BOXED_NUM_HEAP_TAG, 0n)
252
- WasmI32.store(ptr, Tags._GRAIN_FLOAT64_BOXED_NUM_TAG, 4n)
253
-
254
- ptr
255
- }
96
+ @unsafe
97
+ export primitive allocateFloat64: () -> WasmI32 = "@allocate.float64"
256
98
 
257
99
  /**
258
100
  * Allocates a new Float64 with a prepopulated value
259
- * @param value The value to store
260
- */
261
- export let newFloat64 = value => {
262
- let ptr = allocateFloat64()
263
- WasmF64.store(ptr, value, 8n)
264
- ptr
265
- }
266
-
267
- /**
268
- * Returns a pointer to the heap location containing this boxed number's Float64
269
- * @param wrappedFloat64 The boxed float64 to return
101
+ * @param value: The value to store
102
+ * @returns The pointer to the Float64
270
103
  */
271
- export let rawFloat64Ptr = wrappedFloat64 => {
272
- wrappedFloat64 + 8n
273
- }
274
-
275
- export let loadFloat64 = xptr => {
276
- WasmF64.load(xptr, 8n)
277
- }
104
+ @unsafe
105
+ export primitive newFloat64: WasmF64 -> WasmI32 = "@new.float64"
278
106
 
279
107
  // RATIONALS
280
108
 
281
109
  /**
282
110
  * Allocates a new Rational.
283
111
  *
284
- * @returns {WasmI32}
112
+ * @returns The pointer to the empty Rational
285
113
  */
286
- export let allocateRational = () => {
287
- let ptr = Memory.malloc(16n)
288
-
289
- WasmI32.store(ptr, Tags._GRAIN_BOXED_NUM_HEAP_TAG, 0n)
290
- WasmI32.store(ptr, Tags._GRAIN_RATIONAL_BOXED_NUM_TAG, 4n)
291
-
292
- ptr
293
- }
114
+ @unsafe
115
+ export primitive allocateRational: () -> WasmI32 = "@allocate.rational"
294
116
 
295
117
  /**
296
118
  * Allocates a new Rational with a prepopulated value
297
- * @param value The value to store
119
+ * @param value: The numerator value to store
120
+ * @param value: The denominator value to store
121
+ * @returns The pointer to the Rational
298
122
  */
299
- export let newRational = (numerator, denominator) => {
300
- let ptr = allocateRational()
301
- WasmI32.store(ptr, numerator, 8n)
302
- WasmI32.store(ptr, denominator, 12n)
303
- ptr
304
- }
123
+ @unsafe
124
+ export primitive newRational: (WasmI32, WasmI32) -> WasmI32 = "@new.rational"
305
125
 
306
126
  /**
307
- * Returns a pointer to the heap location containing this boxed number's Rational numerator
308
- * @param wrappedRational The boxed rational to return
127
+ * Load the (tagged) variant of an ADT.
128
+ *
129
+ * @param ptr: Untagged pointer to the ADT
130
+ * @returns The (tagged) ADT variant id
309
131
  */
310
- export let rawRationalNumeratorPtr = wrappedRational => {
311
- wrappedRational + 8n
312
- }
132
+ @unsafe
133
+ export primitive loadAdtVariant: WasmI32 -> WasmI32 = "@adt.load_variant"
313
134
 
314
135
  /**
315
- * Returns a pointer to the heap location containing this boxed number's Rational numerator
316
- * @param wrappedRational The boxed rational to return
136
+ * Load an untagged string's size.
137
+ *
138
+ * @param ptr: Untagged pointer to the string
139
+ * @returns The string size (in bytes)
317
140
  */
318
- export let rawRationalDenominatorPtr = wrappedRational => {
319
- wrappedRational + 12n
320
- }
141
+ @unsafe
142
+ export primitive stringSize: WasmI32 -> WasmI32 = "@string.size"
321
143
 
322
- export let loadRationalNumerator = xptr => {
323
- WasmI32.load(xptr, 8n)
324
- }
325
-
326
- export let loadRationalDenominator = xptr => {
327
- WasmI32.load(xptr, 12n)
328
- }
144
+ /**
145
+ * Load an untagged Bytes' size.
146
+ *
147
+ * @param ptr: Untagged pointer to the Bytes
148
+ * @returns The Bytes size (in bytes)
149
+ */
150
+ @unsafe
151
+ export primitive bytesSize: WasmI32 -> WasmI32 = "@bytes.size"
329
152
 
330
153
  /**
331
- * Load a value from an ADT.
154
+ * Tag a simple number.
332
155
  *
333
- * @export
334
- * @param {WasmI32} ptr Untagged pointer to the ADT
335
- * @param {WasmI32} idx Index (from zero) of the item
336
- * @returns {WasmI32} The value located at the index
156
+ * @param num: The number to tag
157
+ * @returns The tagged number
337
158
  */
338
- export let loadAdtVal = (ptr, idx) => {
339
- WasmI32.load(ptr + idx * 4n, 20n)
340
- }
159
+ @unsafe
160
+ export primitive tagSimpleNumber: WasmI32 -> Number = "@tag.simple_number"
341
161
 
342
162
  /**
343
- * Load the (tagged) variant of an ADT.
163
+ * Untag a simple number.
344
164
  *
345
- * @export
346
- * @param {WasmI32} ptr Untagged pointer to the ADT
347
- * @returns {WasmI32} The (tagged) ADT variant id
165
+ * @param num: The number to untag
166
+ * @returns The untagged number
348
167
  */
349
- export let loadAdtVariant = ptr => {
350
- WasmI32.load(ptr, 12n)
351
- }
168
+ @unsafe
169
+ export primitive untagSimpleNumber: Number -> WasmI32 = "@untag.simple_number"
352
170
 
353
171
  /**
354
- * Load an untagged string's size.
172
+ * Tag a char.
355
173
  *
356
- * @export
357
- * @param {WasmI32} ptr Untagged pointer to the string
358
- * @returns {WasmI32} The string size (in bytes)
174
+ * @param num: The usv to tag
175
+ * @returns The tagged char
359
176
  */
360
- export let stringSize = ptr => {
361
- WasmI32.load(ptr, 4n)
362
- }
177
+ @unsafe
178
+ export primitive tagChar: WasmI32 -> Char = "@tag.char"
363
179
 
364
- export let tagSimpleNumber = x => {
365
- WasmI32.toGrain(x << 1n ^ 1n): Number
366
- }
180
+ /**
181
+ * Untag a char.
182
+ *
183
+ * @param num: The char to untag
184
+ * @returns The untagged usv
185
+ */
186
+ @unsafe
187
+ export primitive untagChar: Char -> WasmI32 = "@untag.char"