@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.
- package/CHANGELOG.md +93 -0
- package/array.gr +18 -18
- package/array.md +18 -18
- package/bigint.gr +497 -0
- package/bigint.md +811 -0
- package/buffer.gr +59 -223
- package/buffer.md +24 -17
- package/bytes.gr +100 -202
- package/bytes.md +19 -0
- package/char.gr +63 -133
- package/exception.gr +28 -2
- package/exception.md +43 -0
- package/float32.gr +76 -95
- package/float32.md +69 -30
- package/float64.gr +81 -95
- package/float64.md +69 -30
- 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 +111 -54
- package/number.md +100 -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 +1049 -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/sys/process.gr
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* grainc-flags --no-gc */
|
|
2
1
|
/**
|
|
3
2
|
* @module Sys/Process: Utilities for accessing functionality and information about the Grain program's process.
|
|
4
3
|
*
|
|
@@ -95,31 +94,20 @@ export enum Signal {
|
|
|
95
94
|
* @section Values: Functions and constants included in the Sys/Process module.
|
|
96
95
|
*/
|
|
97
96
|
|
|
98
|
-
let wasmSafeOk = val => {
|
|
99
|
-
Memory.incRef(WasmI32.fromGrain(Ok))
|
|
100
|
-
Memory.incRef(WasmI32.fromGrain(val))
|
|
101
|
-
Ok(val)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
let wasmSafeErr = err => {
|
|
105
|
-
Memory.incRef(WasmI32.fromGrain(Err))
|
|
106
|
-
Memory.incRef(WasmI32.fromGrain(err))
|
|
107
|
-
Err(err)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
97
|
/**
|
|
111
98
|
* Access command line arguments.
|
|
112
99
|
*
|
|
113
100
|
* @returns `Ok(args)` of an array containing positional string arguments to the process if successful or `Err(exception)` otherwise
|
|
114
101
|
*/
|
|
115
|
-
|
|
102
|
+
@unsafe
|
|
103
|
+
export let argv = () => {
|
|
116
104
|
let argcPtr = Memory.malloc(8n)
|
|
117
105
|
let argvBufSizePtr = argcPtr + 4n
|
|
118
106
|
|
|
119
107
|
let mut err = Wasi.args_sizes_get(argcPtr, argvBufSizePtr)
|
|
120
|
-
|
|
108
|
+
if (err != Wasi._ESUCCESS) {
|
|
121
109
|
Memory.free(argcPtr)
|
|
122
|
-
|
|
110
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
123
111
|
} else {
|
|
124
112
|
let argc = WasmI32.load(argcPtr, 0n)
|
|
125
113
|
let argvBufSize = WasmI32.load(argvBufSizePtr, 0n)
|
|
@@ -132,7 +120,7 @@ export let rec argv = () => {
|
|
|
132
120
|
Memory.free(argcPtr)
|
|
133
121
|
Memory.free(argvPtr)
|
|
134
122
|
Memory.free(argvBufPtr)
|
|
135
|
-
|
|
123
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
136
124
|
} else {
|
|
137
125
|
let arr = allocateArray(argc)
|
|
138
126
|
|
|
@@ -154,11 +142,9 @@ export let rec argv = () => {
|
|
|
154
142
|
Memory.free(argvPtr)
|
|
155
143
|
Memory.free(argvBufPtr)
|
|
156
144
|
|
|
157
|
-
|
|
145
|
+
Ok(WasmI32.toGrain(arr): Array<String>)
|
|
158
146
|
}
|
|
159
147
|
}
|
|
160
|
-
Memory.decRef(WasmI32.fromGrain(argv))
|
|
161
|
-
ret
|
|
162
148
|
}
|
|
163
149
|
|
|
164
150
|
/**
|
|
@@ -166,14 +152,15 @@ export let rec argv = () => {
|
|
|
166
152
|
*
|
|
167
153
|
* @returns `Ok(vars)` of an array containing environment variables supplied to the process if successful or `Err(exception)` otherwise
|
|
168
154
|
*/
|
|
169
|
-
|
|
155
|
+
@unsafe
|
|
156
|
+
export let env = () => {
|
|
170
157
|
let envcPtr = Memory.malloc(8n)
|
|
171
158
|
let envvBufSizePtr = envcPtr + 4n
|
|
172
159
|
|
|
173
160
|
let mut err = Wasi.environ_sizes_get(envcPtr, envvBufSizePtr)
|
|
174
|
-
|
|
161
|
+
if (err != Wasi._ESUCCESS) {
|
|
175
162
|
Memory.free(envcPtr)
|
|
176
|
-
|
|
163
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
177
164
|
} else {
|
|
178
165
|
let envc = WasmI32.load(envcPtr, 0n)
|
|
179
166
|
let envvBufSize = WasmI32.load(envvBufSizePtr, 0n)
|
|
@@ -186,7 +173,7 @@ export let rec env = () => {
|
|
|
186
173
|
Memory.free(envcPtr)
|
|
187
174
|
Memory.free(envvPtr)
|
|
188
175
|
Memory.free(envvBufPtr)
|
|
189
|
-
|
|
176
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
190
177
|
} else {
|
|
191
178
|
let arr = allocateArray(envc)
|
|
192
179
|
|
|
@@ -208,11 +195,9 @@ export let rec env = () => {
|
|
|
208
195
|
Memory.free(envvPtr)
|
|
209
196
|
Memory.free(envvBufPtr)
|
|
210
197
|
|
|
211
|
-
|
|
198
|
+
Ok(WasmI32.toGrain(arr): Array<String>)
|
|
212
199
|
}
|
|
213
200
|
}
|
|
214
|
-
Memory.decRef(WasmI32.fromGrain(env))
|
|
215
|
-
ret
|
|
216
201
|
}
|
|
217
202
|
|
|
218
203
|
/**
|
|
@@ -221,19 +206,18 @@ export let rec env = () => {
|
|
|
221
206
|
* @param code: The value to exit with. An exit code of 0 is considered normal, with other values having meaning depending on the platform
|
|
222
207
|
* @returns `Err(exception)` if unsuccessful. Will not actually return a value if successful, as the process has ended
|
|
223
208
|
*/
|
|
224
|
-
|
|
209
|
+
@unsafe
|
|
210
|
+
export let exit = (code: Number) => {
|
|
225
211
|
let mut code = WasmI32.fromGrain(code)
|
|
226
212
|
|
|
227
|
-
|
|
228
|
-
|
|
213
|
+
if ((code & 1n) == 0n) {
|
|
214
|
+
Err(InvalidArgument("Invalid exit code"))
|
|
229
215
|
} else {
|
|
230
216
|
code = code >> 1n
|
|
231
217
|
Wasi.proc_exit(code)
|
|
232
218
|
// Never actually hit because it exited
|
|
233
|
-
|
|
219
|
+
Ok(void)
|
|
234
220
|
}
|
|
235
|
-
Memory.decRef(WasmI32.fromGrain(exit))
|
|
236
|
-
ret
|
|
237
221
|
}
|
|
238
222
|
|
|
239
223
|
/**
|
|
@@ -242,17 +226,16 @@ export let rec exit = (code: Number) => {
|
|
|
242
226
|
* @param signal: The signal to send
|
|
243
227
|
* @returns `Ok(void)` if successful or `Err(exception)` otherwise
|
|
244
228
|
*/
|
|
245
|
-
|
|
229
|
+
@unsafe
|
|
230
|
+
export let sigRaise = (signalPtr: Signal) => {
|
|
246
231
|
let signal = WasmI32.fromGrain(signalPtr)
|
|
247
232
|
let signal = WasmI32.load(signal, 12n) >> 1n
|
|
248
233
|
let err = Wasi.proc_raise(signal)
|
|
249
|
-
|
|
250
|
-
|
|
234
|
+
if (err != Wasi._ESUCCESS) {
|
|
235
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
251
236
|
} else {
|
|
252
|
-
|
|
237
|
+
Ok(void)
|
|
253
238
|
}
|
|
254
|
-
Memory.decRef(WasmI32.fromGrain(sigRaise))
|
|
255
|
-
ret
|
|
256
239
|
}
|
|
257
240
|
|
|
258
241
|
/**
|
|
@@ -260,13 +243,12 @@ export let rec sigRaise = (signalPtr: Signal) => {
|
|
|
260
243
|
*
|
|
261
244
|
* @returns `Ok(void)` if successful or `Err(exception)` otherwise
|
|
262
245
|
*/
|
|
263
|
-
|
|
246
|
+
@unsafe
|
|
247
|
+
export let schedYield = () => {
|
|
264
248
|
let err = Wasi.sched_yield()
|
|
265
|
-
|
|
266
|
-
|
|
249
|
+
if (err != Wasi._ESUCCESS) {
|
|
250
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
267
251
|
} else {
|
|
268
|
-
|
|
252
|
+
Ok(void)
|
|
269
253
|
}
|
|
270
|
-
Memory.decRef(WasmI32.fromGrain(schedYield))
|
|
271
|
-
ret
|
|
272
254
|
}
|
package/sys/random.gr
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* grainc-flags --no-gc */
|
|
2
1
|
/**
|
|
3
2
|
* @module Sys/Random: System access to random values.
|
|
4
3
|
*
|
|
@@ -6,24 +5,57 @@
|
|
|
6
5
|
*/
|
|
7
6
|
|
|
8
7
|
import WasmI32, { eq as (==), ne as (!=) } from "runtime/unsafe/wasmi32"
|
|
8
|
+
import WasmI64 from "runtime/unsafe/wasmi64"
|
|
9
9
|
import Memory from "runtime/unsafe/memory"
|
|
10
10
|
import Wasi from "runtime/wasi"
|
|
11
|
-
import { tagSimpleNumber } from "runtime/dataStructures"
|
|
11
|
+
import { tagSimpleNumber, newInt32, newInt64 } from "runtime/dataStructures"
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* @section Values: Functions and constants included in the Sys/Random module.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Produce a random 32-bit integer. This function can be slow, so it's best to seed a generator if lots of random data is needed.
|
|
19
|
+
*
|
|
20
|
+
* @returns `Ok(num)` of a random Int32 if successful or `Err(exception)` otherwise
|
|
21
|
+
*
|
|
22
|
+
* @since v0.5.0
|
|
23
|
+
*/
|
|
24
|
+
@unsafe
|
|
25
|
+
export let randomInt32 = () => {
|
|
26
|
+
let buf = Memory.malloc(4n)
|
|
27
|
+
|
|
28
|
+
let err = Wasi.random_get(buf, 4n)
|
|
29
|
+
if (err != Wasi._ESUCCESS) {
|
|
30
|
+
Memory.free(buf)
|
|
31
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
32
|
+
} else {
|
|
33
|
+
let rand = WasmI32.load(buf, 0n)
|
|
34
|
+
Memory.free(buf)
|
|
35
|
+
Ok(WasmI32.toGrain(newInt32(rand)): Int32)
|
|
36
|
+
}
|
|
21
37
|
}
|
|
22
38
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Produce a random 64-bit integer. This function can be slow, so it's best to seed a generator if lots of random data is needed.
|
|
41
|
+
*
|
|
42
|
+
* @returns `Ok(num)` of a random Int64 if successful or `Err(exception)` otherwise
|
|
43
|
+
*
|
|
44
|
+
* @since v0.5.0
|
|
45
|
+
*/
|
|
46
|
+
@unsafe
|
|
47
|
+
export let randomInt64 = () => {
|
|
48
|
+
let buf = Memory.malloc(8n)
|
|
49
|
+
|
|
50
|
+
let err = Wasi.random_get(buf, 8n)
|
|
51
|
+
if (err != Wasi._ESUCCESS) {
|
|
52
|
+
Memory.free(buf)
|
|
53
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
54
|
+
} else {
|
|
55
|
+
let rand = WasmI64.load(buf, 0n)
|
|
56
|
+
Memory.free(buf)
|
|
57
|
+
Ok(WasmI32.toGrain(newInt64(rand)): Int64)
|
|
58
|
+
}
|
|
27
59
|
}
|
|
28
60
|
|
|
29
61
|
/**
|
|
@@ -31,18 +63,17 @@ let wasmSafeErr = err => {
|
|
|
31
63
|
*
|
|
32
64
|
* @returns `Ok(num)` of a random number if successful or `Err(exception)` otherwise
|
|
33
65
|
*/
|
|
34
|
-
|
|
66
|
+
@unsafe
|
|
67
|
+
export let random = () => {
|
|
35
68
|
let buf = Memory.malloc(4n)
|
|
36
69
|
|
|
37
70
|
let err = Wasi.random_get(buf, 4n)
|
|
38
|
-
|
|
71
|
+
if (err != Wasi._ESUCCESS) {
|
|
39
72
|
Memory.free(buf)
|
|
40
|
-
|
|
73
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
41
74
|
} else {
|
|
42
75
|
let rand = WasmI32.load(buf, 0n)
|
|
43
76
|
Memory.free(buf)
|
|
44
|
-
|
|
77
|
+
Ok(tagSimpleNumber(rand))
|
|
45
78
|
}
|
|
46
|
-
Memory.decRef(WasmI32.fromGrain(random))
|
|
47
|
-
ret
|
|
48
79
|
}
|
package/sys/random.md
CHANGED
|
@@ -12,6 +12,44 @@ import Random from "sys/random"
|
|
|
12
12
|
|
|
13
13
|
Functions and constants included in the Sys/Random module.
|
|
14
14
|
|
|
15
|
+
### Random.**randomInt32**
|
|
16
|
+
|
|
17
|
+
<details disabled>
|
|
18
|
+
<summary tabindex="-1">Added in <code>0.5.0</code></summary>
|
|
19
|
+
No other changes yet.
|
|
20
|
+
</details>
|
|
21
|
+
|
|
22
|
+
```grain
|
|
23
|
+
randomInt32 : () -> Result<Int32, Exception>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Produce a random 32-bit integer. This function can be slow, so it's best to seed a generator if lots of random data is needed.
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
|
|
30
|
+
|type|description|
|
|
31
|
+
|----|-----------|
|
|
32
|
+
|`Result<Int32, Exception>`|`Ok(num)` of a random Int32 if successful or `Err(exception)` otherwise|
|
|
33
|
+
|
|
34
|
+
### Random.**randomInt64**
|
|
35
|
+
|
|
36
|
+
<details disabled>
|
|
37
|
+
<summary tabindex="-1">Added in <code>0.5.0</code></summary>
|
|
38
|
+
No other changes yet.
|
|
39
|
+
</details>
|
|
40
|
+
|
|
41
|
+
```grain
|
|
42
|
+
randomInt64 : () -> Result<Int64, Exception>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Produce a random 64-bit integer. This function can be slow, so it's best to seed a generator if lots of random data is needed.
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
|
|
49
|
+
|type|description|
|
|
50
|
+
|----|-----------|
|
|
51
|
+
|`Result<Int64, Exception>`|`Ok(num)` of a random Int64 if successful or `Err(exception)` otherwise|
|
|
52
|
+
|
|
15
53
|
### Random.**random**
|
|
16
54
|
|
|
17
55
|
```grain
|
package/sys/time.gr
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* grainc-flags --no-gc */
|
|
2
1
|
/**
|
|
3
2
|
* @module Sys/Time: Access to system clocks.
|
|
4
3
|
*
|
|
@@ -19,27 +18,16 @@ import { allocateInt64, tagSimpleNumber } from "runtime/dataStructures"
|
|
|
19
18
|
* @section Values: Functions and constants included in the Sys/Time module.
|
|
20
19
|
*/
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
Memory.incRef(WasmI32.fromGrain(Ok))
|
|
24
|
-
Memory.incRef(WasmI32.fromGrain(val))
|
|
25
|
-
Ok(val)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let wasmSafeErr = err => {
|
|
29
|
-
Memory.incRef(WasmI32.fromGrain(Err))
|
|
30
|
-
Memory.incRef(WasmI32.fromGrain(err))
|
|
31
|
-
Err(err)
|
|
32
|
-
}
|
|
33
|
-
|
|
21
|
+
@unsafe
|
|
34
22
|
let getClockTime = (clockid, precision) => {
|
|
35
23
|
let int64Ptr = allocateInt64()
|
|
36
24
|
let timePtr = int64Ptr + 8n
|
|
37
25
|
let err = Wasi.clock_time_get(clockid, precision, timePtr)
|
|
38
26
|
if (err != Wasi._ESUCCESS) {
|
|
39
27
|
Memory.free(int64Ptr)
|
|
40
|
-
|
|
28
|
+
Err(Wasi.SystemError(tagSimpleNumber(err)))
|
|
41
29
|
} else {
|
|
42
|
-
|
|
30
|
+
Ok(WasmI32.toGrain(int64Ptr): Int64)
|
|
43
31
|
}
|
|
44
32
|
}
|
|
45
33
|
|
|
@@ -49,10 +37,9 @@ let getClockTime = (clockid, precision) => {
|
|
|
49
37
|
*
|
|
50
38
|
* @returns `Ok(time)` of the current time if successful or `Err(exception)` otherwise
|
|
51
39
|
*/
|
|
40
|
+
@unsafe
|
|
52
41
|
export let rec realTime = () => {
|
|
53
|
-
|
|
54
|
-
Memory.decRef(WasmI32.fromGrain(realTime))
|
|
55
|
-
ret
|
|
42
|
+
getClockTime(Wasi._CLOCK_REALTIME, 1000N)
|
|
56
43
|
}
|
|
57
44
|
|
|
58
45
|
/**
|
|
@@ -63,10 +50,9 @@ export let rec realTime = () => {
|
|
|
63
50
|
*
|
|
64
51
|
* @returns `Ok(time)` of the current time if successful or `Err(exception)` otherwise
|
|
65
52
|
*/
|
|
53
|
+
@unsafe
|
|
66
54
|
export let rec monotonicTime = () => {
|
|
67
|
-
|
|
68
|
-
Memory.decRef(WasmI32.fromGrain(monotonicTime))
|
|
69
|
-
ret
|
|
55
|
+
getClockTime(Wasi._CLOCK_MONOTONIC, 1N)
|
|
70
56
|
}
|
|
71
57
|
|
|
72
58
|
/**
|
|
@@ -74,10 +60,9 @@ export let rec monotonicTime = () => {
|
|
|
74
60
|
*
|
|
75
61
|
* @returns `Ok(elapsed)` of the elapsed nanoseconds if successful or `Err(exception)` otherwise
|
|
76
62
|
*/
|
|
63
|
+
@unsafe
|
|
77
64
|
export let rec processCpuTime = () => {
|
|
78
|
-
|
|
79
|
-
Memory.decRef(WasmI32.fromGrain(processCpuTime))
|
|
80
|
-
ret
|
|
65
|
+
getClockTime(Wasi._CLOCK_PROCESS_CPUTIME, 1N)
|
|
81
66
|
}
|
|
82
67
|
|
|
83
68
|
/**
|
|
@@ -85,8 +70,7 @@ export let rec processCpuTime = () => {
|
|
|
85
70
|
*
|
|
86
71
|
* @returns `Ok(elapsed)` of the elapsed nanoseconds if successful or `Err(exception)` otherwise
|
|
87
72
|
*/
|
|
73
|
+
@unsafe
|
|
88
74
|
export let rec threadCpuTime = () => {
|
|
89
|
-
|
|
90
|
-
Memory.decRef(WasmI32.fromGrain(threadCpuTime))
|
|
91
|
-
ret
|
|
75
|
+
getClockTime(Wasi._CLOCK_THREAD_CPUTIME, 1N)
|
|
92
76
|
}
|