@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
package/sys/process.gr DELETED
@@ -1,254 +0,0 @@
1
- /**
2
- * @module Process: 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 import Process from "sys/process"
7
- */
8
-
9
- import WasmI32, {
10
- add as (+),
11
- mul as (*),
12
- shrS as (>>),
13
- shrU as (>>>),
14
- and as (&),
15
- eq as (==),
16
- ne as (!=),
17
- ltS as (<),
18
- } from "runtime/unsafe/wasmi32"
19
- import Memory from "runtime/unsafe/memory"
20
- import Wasi from "runtime/wasi"
21
- import {
22
- tagSimpleNumber,
23
- allocateArray,
24
- allocateString,
25
- } from "runtime/dataStructures"
26
-
27
- /**
28
- * @section Types: Type declarations included in the Process module.
29
- */
30
-
31
- /**
32
- * Signals that can be sent to the host system.
33
- */
34
- export enum Signal {
35
- // Hangup.
36
- HUP,
37
- // Terminate interrupt signal.
38
- INT,
39
- // Terminal quit signal.
40
- QUIT,
41
- // Illegal instruction.
42
- ILL,
43
- // Trace/breakpoint trap.
44
- TRAP,
45
- // Process abort signal.
46
- ABRT,
47
- // Access to an undefined portion of a memory object.
48
- BUS,
49
- // Erroneous arithmetic operation.
50
- FPE,
51
- // Kill.
52
- KILL,
53
- // User-defined signal 1.
54
- USR1,
55
- // Invalid memory reference.
56
- SEGV,
57
- // User-defined signal 2.
58
- USR2,
59
- // Write on a pipe with no one to read it.
60
- PIPE,
61
- // Alarm clock.
62
- ALRM,
63
- // Termination signal.
64
- TERM,
65
- // Child process terminated, stopped, or continued.
66
- CHLD,
67
- // Continue executing, if stopped.
68
- CONT,
69
- // Stop executing.
70
- STOP,
71
- // Terminal stop signal.
72
- TSTP,
73
- // Background process attempting read.
74
- TTIN,
75
- // Background process attempting write.
76
- TTOU,
77
- // High bandwidth data is available at a socket.
78
- URG,
79
- // CPU time limit exceeded.
80
- XCPU,
81
- // File size limit exceeded.
82
- XFSZ,
83
- // Virtual timer expired.
84
- VTALRM,
85
- PROF,
86
- WINCH,
87
- POLL,
88
- PWR,
89
- // Bad system call.
90
- SYS,
91
- }
92
-
93
- /**
94
- * @section Values: Functions and constants included in the Process module.
95
- */
96
-
97
- /**
98
- * Access command line arguments.
99
- *
100
- * @returns `Ok(args)` of an array containing positional string arguments to the process if successful or `Err(exception)` otherwise
101
- */
102
- @unsafe
103
- export let argv = () => {
104
- let argcPtr = Memory.malloc(8n)
105
- let argvBufSizePtr = argcPtr + 4n
106
-
107
- let mut err = Wasi.args_sizes_get(argcPtr, argvBufSizePtr)
108
- if (err != Wasi._ESUCCESS) {
109
- Memory.free(argcPtr)
110
- Err(Wasi.SystemError(tagSimpleNumber(err)))
111
- } else {
112
- let argc = WasmI32.load(argcPtr, 0n)
113
- let argvBufSize = WasmI32.load(argvBufSizePtr, 0n)
114
-
115
- let argvPtr = Memory.malloc(argc * 4n)
116
- let argvBufPtr = Memory.malloc(argvBufSize)
117
-
118
- err = Wasi.args_get(argvPtr, argvBufPtr)
119
- if (err != Wasi._ESUCCESS) {
120
- Memory.free(argcPtr)
121
- Memory.free(argvPtr)
122
- Memory.free(argvBufPtr)
123
- Err(Wasi.SystemError(tagSimpleNumber(err)))
124
- } else {
125
- let arr = allocateArray(argc)
126
-
127
- let argsLength = argc * 4n
128
- for (let mut i = 0n; i < argsLength; i += 4n) {
129
- let strPtr = WasmI32.load(argvPtr + i, 0n)
130
- let mut strLength = 0n
131
- while (WasmI32.load8U(strPtr + strLength, 0n) != 0n) {
132
- strLength += 1n
133
- }
134
-
135
- let grainStrPtr = allocateString(strLength)
136
- Memory.copy(grainStrPtr + 8n, strPtr, strLength)
137
-
138
- WasmI32.store(arr + i, grainStrPtr, 8n)
139
- }
140
-
141
- Memory.free(argcPtr)
142
- Memory.free(argvPtr)
143
- Memory.free(argvBufPtr)
144
-
145
- Ok(WasmI32.toGrain(arr): Array<String>)
146
- }
147
- }
148
- }
149
-
150
- /**
151
- * Access environment variables.
152
- *
153
- * @returns `Ok(vars)` of an array containing environment variables supplied to the process if successful or `Err(exception)` otherwise
154
- */
155
- @unsafe
156
- export let env = () => {
157
- let envcPtr = Memory.malloc(8n)
158
- let envvBufSizePtr = envcPtr + 4n
159
-
160
- let mut err = Wasi.environ_sizes_get(envcPtr, envvBufSizePtr)
161
- if (err != Wasi._ESUCCESS) {
162
- Memory.free(envcPtr)
163
- Err(Wasi.SystemError(tagSimpleNumber(err)))
164
- } else {
165
- let envc = WasmI32.load(envcPtr, 0n)
166
- let envvBufSize = WasmI32.load(envvBufSizePtr, 0n)
167
-
168
- let envvPtr = Memory.malloc(envc * 4n)
169
- let envvBufPtr = Memory.malloc(envvBufSize)
170
-
171
- err = Wasi.environ_get(envvPtr, envvBufPtr)
172
- if (err != Wasi._ESUCCESS) {
173
- Memory.free(envcPtr)
174
- Memory.free(envvPtr)
175
- Memory.free(envvBufPtr)
176
- Err(Wasi.SystemError(tagSimpleNumber(err)))
177
- } else {
178
- let arr = allocateArray(envc)
179
-
180
- let envsLength = envc * 4n
181
- for (let mut i = 0n; i < envsLength; i += 4n) {
182
- let strPtr = WasmI32.load(envvPtr + i, 0n)
183
- let mut strLength = 0n
184
- while (WasmI32.load8U(strPtr + strLength, 0n) != 0n) {
185
- strLength += 1n
186
- }
187
-
188
- let grainStrPtr = allocateString(strLength)
189
- Memory.copy(grainStrPtr + 8n, strPtr, strLength)
190
-
191
- WasmI32.store(arr + i, grainStrPtr, 8n)
192
- }
193
-
194
- Memory.free(envcPtr)
195
- Memory.free(envvPtr)
196
- Memory.free(envvBufPtr)
197
-
198
- Ok(WasmI32.toGrain(arr): Array<String>)
199
- }
200
- }
201
- }
202
-
203
- /**
204
- * Terminate the process normally.
205
- *
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
207
- * @returns `Err(exception)` if unsuccessful. Will not actually return a value if successful, as the process has ended
208
- */
209
- @unsafe
210
- export let exit = (code: Number) => {
211
- let mut code = WasmI32.fromGrain(code)
212
-
213
- if ((code & 1n) == 0n) {
214
- Err(InvalidArgument("Invalid exit code"))
215
- } else {
216
- code = code >> 1n
217
- Wasi.proc_exit(code)
218
- // Never actually hit because it exited
219
- Ok(void)
220
- }
221
- }
222
-
223
- /**
224
- * Send a signal to the process of the calling thread.
225
- *
226
- * @param signal: The signal to send
227
- * @returns `Ok(void)` if successful or `Err(exception)` otherwise
228
- */
229
- @unsafe
230
- export let sigRaise = (signalPtr: Signal) => {
231
- let signal = WasmI32.fromGrain(signalPtr)
232
- let signal = WasmI32.load(signal, 12n) >> 1n
233
- let err = Wasi.proc_raise(signal)
234
- if (err != Wasi._ESUCCESS) {
235
- Err(Wasi.SystemError(tagSimpleNumber(err)))
236
- } else {
237
- Ok(void)
238
- }
239
- }
240
-
241
- /**
242
- * Yield execution to the calling thread.
243
- *
244
- * @returns `Ok(void)` if successful or `Err(exception)` otherwise
245
- */
246
- @unsafe
247
- export let schedYield = () => {
248
- let err = Wasi.sched_yield()
249
- if (err != Wasi._ESUCCESS) {
250
- Err(Wasi.SystemError(tagSimpleNumber(err)))
251
- } else {
252
- Ok(void)
253
- }
254
- }
package/sys/random.gr DELETED
@@ -1,79 +0,0 @@
1
- /**
2
- * @module Random: System access to random values.
3
- *
4
- * @example import Random from "sys/random"
5
- */
6
-
7
- import WasmI32, { eq as (==), ne as (!=) } from "runtime/unsafe/wasmi32"
8
- import WasmI64 from "runtime/unsafe/wasmi64"
9
- import Memory from "runtime/unsafe/memory"
10
- import Wasi from "runtime/wasi"
11
- import { tagSimpleNumber, newInt32, newInt64 } from "runtime/dataStructures"
12
-
13
- /**
14
- * @section Values: Functions and constants included in the Random module.
15
- */
16
-
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
- }
37
- }
38
-
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
- }
59
- }
60
-
61
- /**
62
- * Produce a random number. This function can be slow, so it's best to seed a generator if lots of random data is needed.
63
- *
64
- * @returns `Ok(num)` of a random number if successful or `Err(exception)` otherwise
65
- */
66
- @unsafe
67
- export let random = () => {
68
- let buf = Memory.malloc(4n)
69
-
70
- let err = Wasi.random_get(buf, 4n)
71
- if (err != Wasi._ESUCCESS) {
72
- Memory.free(buf)
73
- Err(Wasi.SystemError(tagSimpleNumber(err)))
74
- } else {
75
- let rand = WasmI32.load(buf, 0n)
76
- Memory.free(buf)
77
- Ok(tagSimpleNumber(rand))
78
- }
79
- }
package/sys/random.md DELETED
@@ -1,66 +0,0 @@
1
- ---
2
- title: Random
3
- ---
4
-
5
- System access to random values.
6
-
7
- ```grain
8
- import Random from "sys/random"
9
- ```
10
-
11
- ## Values
12
-
13
- Functions and constants included in the Random module.
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
-
53
- ### Random.**random**
54
-
55
- ```grain
56
- random : () -> Result<Number, Exception>
57
- ```
58
-
59
- Produce a random number. 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<Number, Exception>`|`Ok(num)` of a random number if successful or `Err(exception)` otherwise|
66
-