@grain/stdlib 0.4.4 → 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.
Files changed (97) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +1 -1
  3. package/array.gr +92 -73
  4. package/array.md +18 -18
  5. package/bigint.gr +497 -0
  6. package/bigint.md +811 -0
  7. package/buffer.gr +56 -217
  8. package/buffer.md +24 -17
  9. package/bytes.gr +103 -205
  10. package/bytes.md +19 -0
  11. package/char.gr +152 -166
  12. package/char.md +200 -0
  13. package/exception.md +6 -0
  14. package/float32.gr +159 -82
  15. package/float32.md +315 -0
  16. package/float64.gr +163 -82
  17. package/float64.md +315 -0
  18. package/hash.gr +53 -49
  19. package/int32.gr +479 -230
  20. package/int32.md +937 -0
  21. package/int64.gr +479 -230
  22. package/int64.md +937 -0
  23. package/list.gr +530 -116
  24. package/list.md +1141 -0
  25. package/map.gr +302 -121
  26. package/map.md +525 -0
  27. package/number.gr +51 -57
  28. package/number.md +37 -3
  29. package/option.gr +25 -25
  30. package/option.md +1 -1
  31. package/package.json +3 -3
  32. package/pervasives.gr +504 -52
  33. package/pervasives.md +1116 -0
  34. package/queue.gr +8 -1
  35. package/queue.md +10 -0
  36. package/random.gr +196 -0
  37. package/random.md +179 -0
  38. package/range.gr +26 -26
  39. package/regex.gr +1833 -842
  40. package/regex.md +11 -11
  41. package/result.md +1 -1
  42. package/runtime/bigint.gr +2045 -0
  43. package/runtime/bigint.md +326 -0
  44. package/runtime/dataStructures.gr +99 -279
  45. package/runtime/dataStructures.md +391 -0
  46. package/runtime/debug.gr +0 -1
  47. package/runtime/debug.md +6 -0
  48. package/runtime/equal.gr +40 -37
  49. package/runtime/equal.md +6 -0
  50. package/runtime/exception.gr +28 -15
  51. package/runtime/exception.md +30 -0
  52. package/runtime/gc.gr +50 -20
  53. package/runtime/gc.md +36 -0
  54. package/runtime/malloc.gr +32 -22
  55. package/runtime/malloc.md +55 -0
  56. package/runtime/numberUtils.gr +297 -142
  57. package/runtime/numberUtils.md +54 -0
  58. package/runtime/numbers.gr +1204 -453
  59. package/runtime/numbers.md +300 -0
  60. package/runtime/string.gr +193 -228
  61. package/runtime/string.md +24 -0
  62. package/runtime/stringUtils.gr +62 -38
  63. package/runtime/stringUtils.md +6 -0
  64. package/runtime/unsafe/constants.gr +17 -0
  65. package/runtime/unsafe/constants.md +72 -0
  66. package/runtime/unsafe/conv.gr +10 -10
  67. package/runtime/unsafe/conv.md +71 -0
  68. package/runtime/unsafe/errors.md +204 -0
  69. package/runtime/unsafe/memory.gr +14 -3
  70. package/runtime/unsafe/memory.md +54 -0
  71. package/runtime/unsafe/printWasm.gr +4 -4
  72. package/runtime/unsafe/printWasm.md +24 -0
  73. package/runtime/unsafe/tags.gr +11 -10
  74. package/runtime/unsafe/tags.md +120 -0
  75. package/runtime/unsafe/wasmf32.gr +9 -2
  76. package/runtime/unsafe/wasmf32.md +168 -0
  77. package/runtime/unsafe/wasmf64.gr +9 -2
  78. package/runtime/unsafe/wasmf64.md +168 -0
  79. package/runtime/unsafe/wasmi32.gr +65 -47
  80. package/runtime/unsafe/wasmi32.md +282 -0
  81. package/runtime/unsafe/wasmi64.gr +78 -50
  82. package/runtime/unsafe/wasmi64.md +300 -0
  83. package/runtime/utils/printing.gr +62 -0
  84. package/runtime/utils/printing.md +18 -0
  85. package/runtime/wasi.gr +200 -46
  86. package/runtime/wasi.md +839 -0
  87. package/set.gr +125 -121
  88. package/set.md +24 -21
  89. package/stack.gr +29 -29
  90. package/stack.md +4 -6
  91. package/string.gr +434 -415
  92. package/string.md +3 -3
  93. package/sys/file.gr +477 -482
  94. package/sys/process.gr +33 -47
  95. package/sys/random.gr +48 -20
  96. package/sys/random.md +38 -0
  97. package/sys/time.gr +12 -28
@@ -1,367 +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
160
- */
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
67
+ * @param value: The value to store
68
+ * @returns The pointer to the Int64
186
69
  */
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
101
+ * @param value: The value to store
102
+ * @returns The pointer to the Float64
260
103
  */
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
270
- */
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
- }
321
-
322
- export let loadRationalNumerator = (xptr) => {
323
- WasmI32.load(xptr, 8n)
324
- }
325
-
326
- export let loadRationalDenominator = (xptr) => {
327
- WasmI32.load(xptr, 12n)
328
- }
141
+ @unsafe
142
+ export primitive stringSize: WasmI32 -> WasmI32 = "@string.size"
329
143
 
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"
330
152
 
331
153
  /**
332
- * Load a value from an ADT.
154
+ * Tag a simple number.
333
155
  *
334
- * @export
335
- * @param {WasmI32} ptr Untagged pointer to the ADT
336
- * @param {WasmI32} idx Index (from zero) of the item
337
- * @returns {WasmI32} The value located at the index
156
+ * @param num: The number to tag
157
+ * @returns The tagged number
338
158
  */
339
- export let loadAdtVal = (ptr, idx) => {
340
- WasmI32.load(ptr + (idx * 4n), 20n)
341
- }
159
+ @unsafe
160
+ export primitive tagSimpleNumber: WasmI32 -> Number = "@tag.simple_number"
342
161
 
343
162
  /**
344
- * Load the (tagged) variant of an ADT.
163
+ * Untag a simple number.
345
164
  *
346
- * @export
347
- * @param {WasmI32} ptr Untagged pointer to the ADT
348
- * @returns {WasmI32} The (tagged) ADT variant id
165
+ * @param num: The number to untag
166
+ * @returns The untagged number
349
167
  */
350
- export let loadAdtVariant = (ptr) => {
351
- WasmI32.load(ptr, 12n)
352
- }
168
+ @unsafe
169
+ export primitive untagSimpleNumber: Number -> WasmI32 = "@untag.simple_number"
353
170
 
354
171
  /**
355
- * Load an untagged string's size.
172
+ * Tag a char.
356
173
  *
357
- * @export
358
- * @param {WasmI32} ptr Untagged pointer to the string
359
- * @returns {WasmI32} The string size (in bytes)
174
+ * @param num: The usv to tag
175
+ * @returns The tagged char
360
176
  */
361
- export let stringSize = (ptr) => {
362
- WasmI32.load(ptr, 4n)
363
- }
177
+ @unsafe
178
+ export primitive tagChar: WasmI32 -> Char = "@tag.char"
364
179
 
365
- export let tagSimpleNumber = (x) => {
366
- WasmI32.toGrain((x << 1n) ^ 1n): Number
367
- }
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"