@grain/stdlib 0.5.12 → 0.6.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 (155) hide show
  1. package/CHANGELOG.md +200 -0
  2. package/LICENSE +1 -1
  3. package/README.md +25 -2
  4. package/array.gr +1512 -199
  5. package/array.md +2032 -94
  6. package/bigint.gr +239 -140
  7. package/bigint.md +450 -106
  8. package/buffer.gr +595 -102
  9. package/buffer.md +903 -145
  10. package/bytes.gr +401 -110
  11. package/bytes.md +551 -63
  12. package/char.gr +228 -49
  13. package/char.md +373 -7
  14. package/exception.gr +26 -12
  15. package/exception.md +29 -5
  16. package/float32.gr +130 -109
  17. package/float32.md +185 -57
  18. package/float64.gr +112 -99
  19. package/float64.md +185 -57
  20. package/hash.gr +47 -37
  21. package/hash.md +21 -3
  22. package/int16.gr +430 -0
  23. package/int16.md +618 -0
  24. package/int32.gr +200 -269
  25. package/int32.md +254 -289
  26. package/int64.gr +142 -225
  27. package/int64.md +254 -289
  28. package/int8.gr +511 -0
  29. package/int8.md +786 -0
  30. package/json.gr +2084 -0
  31. package/json.md +608 -0
  32. package/list.gr +120 -68
  33. package/list.md +125 -80
  34. package/map.gr +560 -57
  35. package/map.md +672 -56
  36. package/marshal.gr +239 -227
  37. package/marshal.md +36 -4
  38. package/number.gr +626 -676
  39. package/number.md +738 -153
  40. package/option.gr +33 -35
  41. package/option.md +58 -42
  42. package/package.json +2 -2
  43. package/path.gr +148 -187
  44. package/path.md +47 -96
  45. package/pervasives.gr +75 -416
  46. package/pervasives.md +85 -180
  47. package/priorityqueue.gr +433 -74
  48. package/priorityqueue.md +422 -54
  49. package/queue.gr +362 -80
  50. package/queue.md +433 -38
  51. package/random.gr +67 -75
  52. package/random.md +68 -40
  53. package/range.gr +135 -63
  54. package/range.md +198 -43
  55. package/rational.gr +284 -0
  56. package/rational.md +545 -0
  57. package/regex.gr +933 -1066
  58. package/regex.md +59 -60
  59. package/result.gr +23 -25
  60. package/result.md +54 -39
  61. package/runtime/atof/common.gr +78 -82
  62. package/runtime/atof/common.md +22 -10
  63. package/runtime/atof/decimal.gr +102 -127
  64. package/runtime/atof/decimal.md +28 -7
  65. package/runtime/atof/lemire.gr +56 -71
  66. package/runtime/atof/lemire.md +9 -1
  67. package/runtime/atof/parse.gr +83 -110
  68. package/runtime/atof/parse.md +12 -2
  69. package/runtime/atof/slow.gr +28 -35
  70. package/runtime/atof/slow.md +9 -1
  71. package/runtime/atof/table.gr +19 -18
  72. package/runtime/atof/table.md +10 -2
  73. package/runtime/atoi/parse.gr +153 -136
  74. package/runtime/atoi/parse.md +50 -1
  75. package/runtime/bigint.gr +410 -517
  76. package/runtime/bigint.md +71 -57
  77. package/runtime/compare.gr +176 -85
  78. package/runtime/compare.md +31 -1
  79. package/runtime/dataStructures.gr +144 -32
  80. package/runtime/dataStructures.md +267 -31
  81. package/runtime/debugPrint.gr +34 -15
  82. package/runtime/debugPrint.md +37 -5
  83. package/runtime/equal.gr +53 -52
  84. package/runtime/equal.md +30 -1
  85. package/runtime/exception.gr +38 -47
  86. package/runtime/exception.md +10 -8
  87. package/runtime/gc.gr +23 -152
  88. package/runtime/gc.md +13 -17
  89. package/runtime/malloc.gr +31 -31
  90. package/runtime/malloc.md +11 -3
  91. package/runtime/numberUtils.gr +191 -172
  92. package/runtime/numberUtils.md +17 -9
  93. package/runtime/numbers.gr +1695 -1021
  94. package/runtime/numbers.md +1098 -134
  95. package/runtime/string.gr +540 -242
  96. package/runtime/string.md +76 -6
  97. package/runtime/unsafe/constants.gr +30 -13
  98. package/runtime/unsafe/constants.md +80 -0
  99. package/runtime/unsafe/conv.gr +55 -28
  100. package/runtime/unsafe/conv.md +41 -9
  101. package/runtime/unsafe/memory.gr +10 -30
  102. package/runtime/unsafe/memory.md +15 -19
  103. package/runtime/unsafe/tags.gr +37 -21
  104. package/runtime/unsafe/tags.md +88 -8
  105. package/runtime/unsafe/wasmf32.gr +30 -36
  106. package/runtime/unsafe/wasmf32.md +64 -56
  107. package/runtime/unsafe/wasmf64.gr +30 -36
  108. package/runtime/unsafe/wasmf64.md +64 -56
  109. package/runtime/unsafe/wasmi32.gr +49 -66
  110. package/runtime/unsafe/wasmi32.md +102 -94
  111. package/runtime/unsafe/wasmi64.gr +52 -79
  112. package/runtime/unsafe/wasmi64.md +108 -100
  113. package/runtime/utils/printing.gr +13 -15
  114. package/runtime/utils/printing.md +11 -3
  115. package/runtime/wasi.gr +294 -295
  116. package/runtime/wasi.md +62 -42
  117. package/set.gr +574 -64
  118. package/set.md +634 -54
  119. package/stack.gr +181 -64
  120. package/stack.md +271 -42
  121. package/string.gr +453 -533
  122. package/string.md +241 -151
  123. package/uint16.gr +369 -0
  124. package/uint16.md +585 -0
  125. package/uint32.gr +470 -0
  126. package/uint32.md +737 -0
  127. package/uint64.gr +471 -0
  128. package/uint64.md +737 -0
  129. package/uint8.gr +369 -0
  130. package/uint8.md +585 -0
  131. package/uri.gr +1093 -0
  132. package/uri.md +477 -0
  133. package/{sys → wasi}/file.gr +914 -500
  134. package/{sys → wasi}/file.md +454 -50
  135. package/wasi/process.gr +292 -0
  136. package/{sys → wasi}/process.md +164 -6
  137. package/wasi/random.gr +77 -0
  138. package/wasi/random.md +80 -0
  139. package/{sys → wasi}/time.gr +15 -22
  140. package/{sys → wasi}/time.md +5 -5
  141. package/immutablearray.gr +0 -929
  142. package/immutablearray.md +0 -1038
  143. package/immutablemap.gr +0 -493
  144. package/immutablemap.md +0 -479
  145. package/immutablepriorityqueue.gr +0 -360
  146. package/immutablepriorityqueue.md +0 -291
  147. package/immutableset.gr +0 -498
  148. package/immutableset.md +0 -449
  149. package/runtime/debug.gr +0 -2
  150. package/runtime/debug.md +0 -6
  151. package/runtime/unsafe/errors.gr +0 -36
  152. package/runtime/unsafe/errors.md +0 -204
  153. package/sys/process.gr +0 -254
  154. package/sys/random.gr +0 -79
  155. package/sys/random.md +0 -66
@@ -0,0 +1,292 @@
1
+ /**
2
+ * Utilities for accessing functionality and information about the Grain program's process.
3
+ *
4
+ * This includes things like accessing environment variables and sending signals.
5
+ *
6
+ * @example from "wasi/process" include Process
7
+ */
8
+ module Process
9
+
10
+ from "runtime/unsafe/wasmi32" include WasmI32
11
+ use WasmI32.{ (+), (*), (>>), (>>>), (&), (==), (!=), (<) }
12
+ from "runtime/unsafe/memory" include Memory
13
+ from "runtime/wasi" include Wasi
14
+ from "runtime/dataStructures" include DataStructures
15
+ use DataStructures.{ tagSimpleNumber, allocateArray, allocateString }
16
+
17
+ /**
18
+ * Signals that can be sent to the host system.
19
+ */
20
+ provide enum Signal {
21
+ /**
22
+ * Hangup.
23
+ */
24
+ HUP,
25
+ /**
26
+ * Terminate interrupt signal.
27
+ */
28
+ INT,
29
+ /**
30
+ * Terminal quit signal.
31
+ */
32
+ QUIT,
33
+ /**
34
+ * Illegal instruction.
35
+ */
36
+ ILL,
37
+ /**
38
+ * Trace/breakpoint trap.
39
+ */
40
+ TRAP,
41
+ /**
42
+ * Process abort signal.
43
+ */
44
+ ABRT,
45
+ /**
46
+ * Access to an undefined portion of a memory object.
47
+ */
48
+ BUS,
49
+ /**
50
+ * Erroneous arithmetic operation.
51
+ */
52
+ FPE,
53
+ /**
54
+ * Kill.
55
+ */
56
+ KILL,
57
+ /**
58
+ * User-defined signal 1.
59
+ */
60
+ USR1,
61
+ /**
62
+ * Invalid memory reference.
63
+ */
64
+ SEGV,
65
+ /**
66
+ * User-defined signal 2.
67
+ */
68
+ USR2,
69
+ /**
70
+ * Write on a pipe with no one to read it.
71
+ */
72
+ PIPE,
73
+ /**
74
+ * Alarm clock.
75
+ */
76
+ ALRM,
77
+ /**
78
+ * Termination signal.
79
+ */
80
+ TERM,
81
+ /**
82
+ * Child process terminated, stopped, or continued.
83
+ */
84
+ CHLD,
85
+ /**
86
+ * Continue executing, if stopped.
87
+ */
88
+ CONT,
89
+ /**
90
+ * Stop executing.
91
+ */
92
+ STOP,
93
+ /**
94
+ * Terminal stop signal.
95
+ */
96
+ TSTP,
97
+ /**
98
+ * Background process attempting read.
99
+ */
100
+ TTIN,
101
+ /**
102
+ * Background process attempting write.
103
+ */
104
+ TTOU,
105
+ /**
106
+ * High bandwidth data is available at a socket.
107
+ */
108
+ URG,
109
+ /**
110
+ * CPU time limit exceeded.
111
+ */
112
+ XCPU,
113
+ /**
114
+ * File size limit exceeded.
115
+ */
116
+ XFSZ,
117
+ /**
118
+ * Virtual timer expired.
119
+ */
120
+ VTALRM,
121
+ PROF,
122
+ WINCH,
123
+ POLL,
124
+ PWR,
125
+ /**
126
+ * Bad system call.
127
+ */
128
+ SYS,
129
+ }
130
+
131
+ /**
132
+ * Access command line arguments.
133
+ *
134
+ * @returns `Ok(args)` of an array containing positional string arguments to the process if successful or `Err(exception)` otherwise
135
+ */
136
+ @unsafe
137
+ provide let argv = () => {
138
+ let argcPtr = Memory.malloc(8n)
139
+ let argvBufSizePtr = argcPtr + 4n
140
+
141
+ let err = Wasi.args_sizes_get(argcPtr, argvBufSizePtr)
142
+ if (err != Wasi._ESUCCESS) {
143
+ Memory.free(argcPtr)
144
+ return Err(Wasi.SystemError(tagSimpleNumber(err)))
145
+ }
146
+
147
+ let argc = WasmI32.load(argcPtr, 0n)
148
+ Memory.free(argcPtr)
149
+
150
+ let argsLength = argc * 4n
151
+ let arr = allocateArray(argc)
152
+
153
+ if (WasmI32.eqz(argsLength)) {
154
+ return Ok(WasmI32.toGrain(arr): Array<String>)
155
+ }
156
+
157
+ let argvBufSize = WasmI32.load(argvBufSizePtr, 0n)
158
+
159
+ let argvPtr = Memory.malloc(argc * 4n)
160
+ let argvBufPtr = Memory.malloc(argvBufSize)
161
+
162
+ let err = Wasi.args_get(argvPtr, argvBufPtr)
163
+ if (err != Wasi._ESUCCESS) {
164
+ Memory.free(argvPtr)
165
+ Memory.free(argvBufPtr)
166
+ return Err(Wasi.SystemError(tagSimpleNumber(err)))
167
+ }
168
+
169
+ for (let mut i = 0n; i < argsLength; i += 4n) {
170
+ let strPtr = WasmI32.load(argvPtr + i, 0n)
171
+ let mut strLength = 0n
172
+ while (WasmI32.load8U(strPtr + strLength, 0n) != 0n) {
173
+ strLength += 1n
174
+ }
175
+
176
+ let grainStrPtr = allocateString(strLength)
177
+ Memory.copy(grainStrPtr + 8n, strPtr, strLength)
178
+
179
+ WasmI32.store(arr + i, grainStrPtr, 8n)
180
+ }
181
+
182
+ Memory.free(argvPtr)
183
+ Memory.free(argvBufPtr)
184
+
185
+ return Ok(WasmI32.toGrain(arr): Array<String>)
186
+ }
187
+
188
+ /**
189
+ * Access environment variables.
190
+ *
191
+ * @returns `Ok(vars)` of an array containing environment variables supplied to the process if successful or `Err(exception)` otherwise
192
+ */
193
+ @unsafe
194
+ provide let env = () => {
195
+ let envcPtr = Memory.malloc(8n)
196
+ let envvBufSizePtr = envcPtr + 4n
197
+
198
+ let err = Wasi.environ_sizes_get(envcPtr, envvBufSizePtr)
199
+ if (err != Wasi._ESUCCESS) {
200
+ Memory.free(envcPtr)
201
+ return Err(Wasi.SystemError(tagSimpleNumber(err)))
202
+ }
203
+ let envc = WasmI32.load(envcPtr, 0n)
204
+ let envvBufSize = WasmI32.load(envvBufSizePtr, 0n)
205
+
206
+ if (WasmI32.eqz(envc)) {
207
+ Memory.free(envcPtr)
208
+ return Ok([>]: Array<String>)
209
+ }
210
+
211
+ let envvPtr = Memory.malloc(envc * 4n)
212
+ let envvBufPtr = Memory.malloc(envvBufSize)
213
+
214
+ let err = Wasi.environ_get(envvPtr, envvBufPtr)
215
+ if (err != Wasi._ESUCCESS) {
216
+ Memory.free(envcPtr)
217
+ Memory.free(envvPtr)
218
+ Memory.free(envvBufPtr)
219
+ return Err(Wasi.SystemError(tagSimpleNumber(err)))
220
+ }
221
+ let arr = allocateArray(envc)
222
+
223
+ let envsLength = envc * 4n
224
+ for (let mut i = 0n; i < envsLength; i += 4n) {
225
+ let strPtr = WasmI32.load(envvPtr + i, 0n)
226
+ let mut strLength = 0n
227
+ while (WasmI32.load8U(strPtr + strLength, 0n) != 0n) {
228
+ strLength += 1n
229
+ }
230
+
231
+ let grainStrPtr = allocateString(strLength)
232
+ Memory.copy(grainStrPtr + 8n, strPtr, strLength)
233
+
234
+ WasmI32.store(arr + i, grainStrPtr, 8n)
235
+ }
236
+
237
+ Memory.free(envcPtr)
238
+ Memory.free(envvPtr)
239
+ Memory.free(envvBufPtr)
240
+
241
+ return Ok(WasmI32.toGrain(arr): Array<String>)
242
+ }
243
+
244
+ /**
245
+ * Terminate the process normally.
246
+ *
247
+ * @param code: The value to exit with. An exit code of 0 is considered normal, with other values having meaning depending on the platform
248
+ * @returns `Err(exception)` if unsuccessful. Will not actually return a value if successful, as the process has ended
249
+ */
250
+ @unsafe
251
+ provide let exit = (code: Number) => {
252
+ let mut code = WasmI32.fromGrain(code)
253
+
254
+ if ((code & 1n) == 0n) {
255
+ return Err(InvalidArgument("Invalid exit code"))
256
+ }
257
+ code = code >> 1n
258
+ Wasi.proc_exit(code)
259
+ // Never actually hit because it exited
260
+ return Ok(void)
261
+ }
262
+
263
+ /**
264
+ * Send a signal to the process of the calling thread.
265
+ *
266
+ * @param signal: The signal to send
267
+ * @returns `Ok(void)` if successful or `Err(exception)` otherwise
268
+ */
269
+ @unsafe
270
+ provide let sigRaise = (signal: Signal) => {
271
+ let signal = WasmI32.fromGrain(signal)
272
+ let signal = WasmI32.load(signal, 12n) >> 1n
273
+ let err = Wasi.proc_raise(signal)
274
+ if (err != Wasi._ESUCCESS) {
275
+ return Err(Wasi.SystemError(tagSimpleNumber(err)))
276
+ }
277
+ return Ok(void)
278
+ }
279
+
280
+ /**
281
+ * Yield execution to the calling thread.
282
+ *
283
+ * @returns `Ok(void)` if successful or `Err(exception)` otherwise
284
+ */
285
+ @unsafe
286
+ provide let schedYield = () => {
287
+ let err = Wasi.sched_yield()
288
+ if (err != Wasi._ESUCCESS) {
289
+ return Err(Wasi.SystemError(tagSimpleNumber(err)))
290
+ }
291
+ return Ok(void)
292
+ }
@@ -7,7 +7,7 @@ Utilities for accessing functionality and information about the Grain program's
7
7
  This includes things like accessing environment variables and sending signals.
8
8
 
9
9
  ```grain
10
- import Process from "sys/process"
10
+ from "wasi/process" include Process
11
11
  ```
12
12
 
13
13
  ## Types
@@ -53,6 +53,164 @@ enum Signal {
53
53
 
54
54
  Signals that can be sent to the host system.
55
55
 
56
+ Variants:
57
+
58
+ ```grain
59
+ HUP
60
+ ```
61
+
62
+ Hangup.
63
+
64
+ ```grain
65
+ INT
66
+ ```
67
+
68
+ Terminate interrupt signal.
69
+
70
+ ```grain
71
+ QUIT
72
+ ```
73
+
74
+ Terminal quit signal.
75
+
76
+ ```grain
77
+ ILL
78
+ ```
79
+
80
+ Illegal instruction.
81
+
82
+ ```grain
83
+ TRAP
84
+ ```
85
+
86
+ Trace/breakpoint trap.
87
+
88
+ ```grain
89
+ ABRT
90
+ ```
91
+
92
+ Process abort signal.
93
+
94
+ ```grain
95
+ BUS
96
+ ```
97
+
98
+ Access to an undefined portion of a memory object.
99
+
100
+ ```grain
101
+ FPE
102
+ ```
103
+
104
+ Erroneous arithmetic operation.
105
+
106
+ ```grain
107
+ KILL
108
+ ```
109
+
110
+ Kill.
111
+
112
+ ```grain
113
+ USR1
114
+ ```
115
+
116
+ User-defined signal 1.
117
+
118
+ ```grain
119
+ SEGV
120
+ ```
121
+
122
+ Invalid memory reference.
123
+
124
+ ```grain
125
+ USR2
126
+ ```
127
+
128
+ User-defined signal 2.
129
+
130
+ ```grain
131
+ PIPE
132
+ ```
133
+
134
+ Write on a pipe with no one to read it.
135
+
136
+ ```grain
137
+ ALRM
138
+ ```
139
+
140
+ Alarm clock.
141
+
142
+ ```grain
143
+ TERM
144
+ ```
145
+
146
+ Termination signal.
147
+
148
+ ```grain
149
+ CHLD
150
+ ```
151
+
152
+ Child process terminated, stopped, or continued.
153
+
154
+ ```grain
155
+ CONT
156
+ ```
157
+
158
+ Continue executing, if stopped.
159
+
160
+ ```grain
161
+ STOP
162
+ ```
163
+
164
+ Stop executing.
165
+
166
+ ```grain
167
+ TSTP
168
+ ```
169
+
170
+ Terminal stop signal.
171
+
172
+ ```grain
173
+ TTIN
174
+ ```
175
+
176
+ Background process attempting read.
177
+
178
+ ```grain
179
+ TTOU
180
+ ```
181
+
182
+ Background process attempting write.
183
+
184
+ ```grain
185
+ URG
186
+ ```
187
+
188
+ High bandwidth data is available at a socket.
189
+
190
+ ```grain
191
+ XCPU
192
+ ```
193
+
194
+ CPU time limit exceeded.
195
+
196
+ ```grain
197
+ XFSZ
198
+ ```
199
+
200
+ File size limit exceeded.
201
+
202
+ ```grain
203
+ VTALRM
204
+ ```
205
+
206
+ Virtual timer expired.
207
+
208
+ ```grain
209
+ SYS
210
+ ```
211
+
212
+ Bad system call.
213
+
56
214
  ## Values
57
215
 
58
216
  Functions and constants included in the Process module.
@@ -60,7 +218,7 @@ Functions and constants included in the Process module.
60
218
  ### Process.**argv**
61
219
 
62
220
  ```grain
63
- argv : () -> Result<Array<String>, Exception>
221
+ argv : () => Result<Array<String>, Exception>
64
222
  ```
65
223
 
66
224
  Access command line arguments.
@@ -74,7 +232,7 @@ Returns:
74
232
  ### Process.**env**
75
233
 
76
234
  ```grain
77
- env : () -> Result<Array<String>, Exception>
235
+ env : () => Result<Array<String>, Exception>
78
236
  ```
79
237
 
80
238
  Access environment variables.
@@ -88,7 +246,7 @@ Returns:
88
246
  ### Process.**exit**
89
247
 
90
248
  ```grain
91
- exit : Number -> Result<Void, Exception>
249
+ exit : (code: Number) => Result<Void, Exception>
92
250
  ```
93
251
 
94
252
  Terminate the process normally.
@@ -108,7 +266,7 @@ Returns:
108
266
  ### Process.**sigRaise**
109
267
 
110
268
  ```grain
111
- sigRaise : Signal -> Result<Void, Exception>
269
+ sigRaise : (signal: Signal) => Result<Void, Exception>
112
270
  ```
113
271
 
114
272
  Send a signal to the process of the calling thread.
@@ -128,7 +286,7 @@ Returns:
128
286
  ### Process.**schedYield**
129
287
 
130
288
  ```grain
131
- schedYield : () -> Result<Void, Exception>
289
+ schedYield : () => Result<Void, Exception>
132
290
  ```
133
291
 
134
292
  Yield execution to the calling thread.
package/wasi/random.gr ADDED
@@ -0,0 +1,77 @@
1
+ /**
2
+ * System access to random values.
3
+ *
4
+ * @example from "wasi/random" include Random
5
+ */
6
+ module Random
7
+
8
+ from "runtime/unsafe/wasmi32" include WasmI32
9
+ use WasmI32.{ (==), (!=) }
10
+ from "runtime/unsafe/wasmi64" include WasmI64
11
+ from "runtime/unsafe/memory" include Memory
12
+ from "runtime/wasi" include Wasi
13
+ from "runtime/dataStructures" include DataStructures
14
+ use DataStructures.{ tagSimpleNumber, newUint32, newUint64 }
15
+
16
+ /**
17
+ * 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.
18
+ *
19
+ * @returns `Ok(num)` of a random Uint32 if successful or `Err(exception)` otherwise
20
+ *
21
+ * @since v0.6.0
22
+ * @history v0.5.0: Originally named `randomInt32`
23
+ */
24
+ @unsafe
25
+ provide let randomUint32 = () => {
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
+ return Err(Wasi.SystemError(tagSimpleNumber(err)))
32
+ }
33
+ let rand = WasmI32.load(buf, 0n)
34
+ Memory.free(buf)
35
+ return Ok(WasmI32.toGrain(newUint32(rand)): Uint32)
36
+ }
37
+
38
+ /**
39
+ * 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.
40
+ *
41
+ * @returns `Ok(num)` of a random Uint64 if successful or `Err(exception)` otherwise
42
+ *
43
+ * @since v0.6.0
44
+ * @history v0.5.0: Originally named `randomInt64`
45
+ */
46
+ @unsafe
47
+ provide let randomUint64 = () => {
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
+ return Err(Wasi.SystemError(tagSimpleNumber(err)))
54
+ }
55
+ let rand = WasmI64.load(buf, 0n)
56
+ Memory.free(buf)
57
+ return Ok(WasmI32.toGrain(newUint64(rand)): Uint64)
58
+ }
59
+
60
+ /**
61
+ * Produce a random number. This function can be slow, so it's best to seed a generator if lots of random data is needed.
62
+ *
63
+ * @returns `Ok(num)` of a random number if successful or `Err(exception)` otherwise
64
+ */
65
+ @unsafe
66
+ provide let random = () => {
67
+ let buf = Memory.malloc(4n)
68
+
69
+ let err = Wasi.random_get(buf, 4n)
70
+ if (err != Wasi._ESUCCESS) {
71
+ Memory.free(buf)
72
+ return Err(Wasi.SystemError(tagSimpleNumber(err)))
73
+ }
74
+ let rand = WasmI32.load(buf, 0n)
75
+ Memory.free(buf)
76
+ return Ok(tagSimpleNumber(rand))
77
+ }
package/wasi/random.md ADDED
@@ -0,0 +1,80 @@
1
+ ---
2
+ title: Random
3
+ ---
4
+
5
+ System access to random values.
6
+
7
+ ```grain
8
+ from "wasi/random" include Random
9
+ ```
10
+
11
+ ## Values
12
+
13
+ Functions and constants included in the Random module.
14
+
15
+ ### Random.**randomUint32**
16
+
17
+ <details>
18
+ <summary>Added in <code>0.6.0</code></summary>
19
+ <table>
20
+ <thead>
21
+ <tr><th>version</th><th>changes</th></tr>
22
+ </thead>
23
+ <tbody>
24
+ <tr><td><code>0.5.0</code></td><td>Originally named `randomInt32`</td></tr>
25
+ </tbody>
26
+ </table>
27
+ </details>
28
+
29
+ ```grain
30
+ randomUint32 : () => Result<Uint32, Exception>
31
+ ```
32
+
33
+ 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.
34
+
35
+ Returns:
36
+
37
+ |type|description|
38
+ |----|-----------|
39
+ |`Result<Uint32, Exception>`|`Ok(num)` of a random Uint32 if successful or `Err(exception)` otherwise|
40
+
41
+ ### Random.**randomUint64**
42
+
43
+ <details>
44
+ <summary>Added in <code>0.6.0</code></summary>
45
+ <table>
46
+ <thead>
47
+ <tr><th>version</th><th>changes</th></tr>
48
+ </thead>
49
+ <tbody>
50
+ <tr><td><code>0.5.0</code></td><td>Originally named `randomInt64`</td></tr>
51
+ </tbody>
52
+ </table>
53
+ </details>
54
+
55
+ ```grain
56
+ randomUint64 : () => Result<Uint64, Exception>
57
+ ```
58
+
59
+ 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.
60
+
61
+ Returns:
62
+
63
+ |type|description|
64
+ |----|-----------|
65
+ |`Result<Uint64, Exception>`|`Ok(num)` of a random Uint64 if successful or `Err(exception)` otherwise|
66
+
67
+ ### Random.**random**
68
+
69
+ ```grain
70
+ random : () => Result<Number, Exception>
71
+ ```
72
+
73
+ Produce a random number. This function can be slow, so it's best to seed a generator if lots of random data is needed.
74
+
75
+ Returns:
76
+
77
+ |type|description|
78
+ |----|-----------|
79
+ |`Result<Number, Exception>`|`Ok(num)` of a random number if successful or `Err(exception)` otherwise|
80
+