@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/runtime/malloc.gr CHANGED
@@ -1,4 +1,5 @@
1
- /* grainc-flags --compilation-mode=runtime */
1
+ @runtimeMode
2
+ module Malloc
2
3
 
3
4
  /*
4
5
  * This module implements a generic memory allocator.
@@ -6,29 +7,30 @@
6
7
  * from pages 185-188 of K&R C (2nd edition).
7
8
  */
8
9
 
9
- import WasmI32, {
10
- add as (+),
11
- sub as (-),
12
- mul as (*),
10
+ from "runtime/unsafe/wasmi32" include WasmI32
11
+ use WasmI32.{
12
+ (+),
13
+ (-),
14
+ (*),
13
15
  divU as (/),
14
16
  gtU as (>),
15
17
  geU as (>=),
16
18
  ltU as (<),
17
- shl as (<<),
18
- shrU as (>>),
19
- eq as (==),
20
- ne as (!=),
21
- } from "runtime/unsafe/wasmi32"
22
- import Exception from "runtime/exception"
19
+ (<<),
20
+ (>>>),
21
+ (==),
22
+ (!=),
23
+ }
24
+ from "runtime/exception" include Exception
23
25
 
24
- primitive memorySize: () -> WasmI32 = "@wasm.memory_size"
25
- primitive memoryGrow: WasmI32 -> WasmI32 = "@wasm.memory_grow"
26
+ primitive memorySize = "@wasm.memory_size"
27
+ primitive memoryGrow = "@wasm.memory_grow"
26
28
 
27
- primitive (!): Bool -> Bool = "@not"
28
- primitive (&&): (Bool, Bool) -> Bool = "@and"
29
- primitive (||): (Bool, Bool) -> Bool = "@or"
29
+ primitive (!) = "@not"
30
+ primitive (&&) = "@and"
31
+ primitive (||) = "@or"
30
32
 
31
- primitive heapBase: WasmI32 = "@heap.base"
33
+ primitive heapStart = "@heap.start"
32
34
 
33
35
  /* UNDERSTANDING THE STRUCTURE OF THE FREE LIST
34
36
  * The original K&R definition for the free list entry type was the following:
@@ -74,13 +76,13 @@ let logMallocHeaderSize = 3n
74
76
  */
75
77
  let mut heapSize = 0n
76
78
 
77
- export let _RESERVED_RUNTIME_SPACE = 0x4000n
79
+ provide let _RESERVED_RUNTIME_SPACE = 0x800n
78
80
 
79
81
  /**
80
82
  * The base the heap. The block at this address will be size 0 and
81
83
  * serve as the root of the free list.
82
84
  */
83
- let _BASE = heapBase + _RESERVED_RUNTIME_SPACE
85
+ let _BASE = heapStart() + _RESERVED_RUNTIME_SPACE
84
86
 
85
87
  /**
86
88
  * The start pointer of the heap.
@@ -129,7 +131,7 @@ let growHeap = (nbytes: WasmI32) => {
129
131
  // More bytes requested than the initial heap size,
130
132
  // so we need to request more anyway.
131
133
  reqSize = nbytes - heapSize
132
- reqSize = reqSize >> 16n
134
+ reqSize = reqSize >>> 16n
133
135
  reqSize += 1n
134
136
  reqResult = memoryGrow(reqSize)
135
137
  if (reqResult == -1n) {
@@ -146,7 +148,7 @@ let growHeap = (nbytes: WasmI32) => {
146
148
  let newRegionStart = memorySize() * _PAGE_SIZE
147
149
 
148
150
  reqSize = nbytes
149
- reqSize = reqSize >> 16n
151
+ reqSize = reqSize >>> 16n
150
152
  reqSize += 1n
151
153
 
152
154
  reqResult = memoryGrow(reqSize)
@@ -164,7 +166,7 @@ let growHeap = (nbytes: WasmI32) => {
164
166
  *
165
167
  * @param ap: The pointer to free
166
168
  */
167
- export let free = (ap: WasmI32) => {
169
+ provide let free = (ap: WasmI32) => {
168
170
  let mut blockPtr = ap - 8n // 8 bytes for malloc header
169
171
  let mut p = freePtr
170
172
 
@@ -238,8 +240,8 @@ let morecore = (nbytes: WasmI32) => {
238
240
  * @param nbytes: The number of bytes to allocate
239
241
  * @returns The pointer to the allocated region (8-byte aligned) or -1 if the allocation failed
240
242
  */
241
- export let malloc = (nb: WasmI32) => {
242
- let mut nbytes = nb
243
+ provide let malloc = (nbytes: WasmI32) => {
244
+ let mut nbytes = nbytes
243
245
  let mut prevp = freePtr
244
246
 
245
247
  // Set nbytes to the next multiple of mallocHeaderSize greater
@@ -258,12 +260,10 @@ export let malloc = (nb: WasmI32) => {
258
260
  let mut ret = -1n
259
261
 
260
262
  // Search the freelist for any blocks large enough.
261
- for (
262
- let mut p = getNext(prevp); ; {
263
- prevp = p
264
- p = getNext(p)
265
- }
266
- ) {
263
+ for (let mut p = getNext(prevp);; {
264
+ prevp = p
265
+ p = getNext(p)
266
+ }) {
267
267
  let size = getSize(p)
268
268
  if (size >= nbytes) {
269
269
  // If this block is big enough, allocate from it.
@@ -305,6 +305,6 @@ export let malloc = (nb: WasmI32) => {
305
305
  *
306
306
  * @returns The free list pointer
307
307
  */
308
- export let getFreePtr = () => {
308
+ provide let getFreePtr = () => {
309
309
  freePtr
310
310
  }
package/runtime/malloc.md CHANGED
@@ -1,3 +1,11 @@
1
+ ---
2
+ title: Malloc
3
+ ---
4
+
5
+ ## Values
6
+
7
+ Functions and constants included in the Malloc module.
8
+
1
9
  ### Malloc.**_RESERVED_RUNTIME_SPACE**
2
10
 
3
11
  ```grain
@@ -7,7 +15,7 @@ _RESERVED_RUNTIME_SPACE : WasmI32
7
15
  ### Malloc.**free**
8
16
 
9
17
  ```grain
10
- free : WasmI32 -> Void
18
+ free : (ap: WasmI32) => Void
11
19
  ```
12
20
 
13
21
  Frees the given allocated pointer.
@@ -21,7 +29,7 @@ Parameters:
21
29
  ### Malloc.**malloc**
22
30
 
23
31
  ```grain
24
- malloc : WasmI32 -> WasmI32
32
+ malloc : (nbytes: WasmI32) => WasmI32
25
33
  ```
26
34
 
27
35
  Allocates the requested number of bytes, returning a pointer.
@@ -41,7 +49,7 @@ Returns:
41
49
  ### Malloc.**getFreePtr**
42
50
 
43
51
  ```grain
44
- getFreePtr : () -> WasmI32
52
+ getFreePtr : () => WasmI32
45
53
  ```
46
54
 
47
55
  Returns the current free list pointer.