@grain/stdlib 0.4.6 → 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 (82) hide show
  1. package/CHANGELOG.md +73 -0
  2. package/array.gr +18 -18
  3. package/array.md +18 -18
  4. package/bigint.gr +497 -0
  5. package/bigint.md +811 -0
  6. package/buffer.gr +49 -213
  7. package/buffer.md +24 -17
  8. package/bytes.gr +100 -202
  9. package/bytes.md +19 -0
  10. package/char.gr +63 -133
  11. package/exception.md +6 -0
  12. package/float32.gr +39 -78
  13. package/float64.gr +43 -78
  14. package/hash.gr +37 -37
  15. package/int32.gr +152 -198
  16. package/int32.md +104 -0
  17. package/int64.gr +151 -197
  18. package/int64.md +104 -0
  19. package/list.gr +467 -70
  20. package/list.md +1141 -0
  21. package/map.gr +192 -7
  22. package/map.md +525 -0
  23. package/number.gr +30 -54
  24. package/number.md +3 -3
  25. package/option.md +1 -1
  26. package/package.json +3 -3
  27. package/pervasives.gr +499 -59
  28. package/pervasives.md +1116 -0
  29. package/queue.gr +4 -0
  30. package/queue.md +10 -0
  31. package/random.gr +196 -0
  32. package/random.md +179 -0
  33. package/regex.gr +1833 -842
  34. package/regex.md +11 -11
  35. package/result.md +1 -1
  36. package/runtime/bigint.gr +2045 -0
  37. package/runtime/bigint.md +326 -0
  38. package/runtime/dataStructures.gr +99 -278
  39. package/runtime/dataStructures.md +391 -0
  40. package/runtime/debug.md +6 -0
  41. package/runtime/equal.gr +5 -23
  42. package/runtime/equal.md +6 -0
  43. package/runtime/exception.md +30 -0
  44. package/runtime/gc.gr +20 -3
  45. package/runtime/gc.md +36 -0
  46. package/runtime/malloc.gr +13 -11
  47. package/runtime/malloc.md +55 -0
  48. package/runtime/numberUtils.gr +91 -41
  49. package/runtime/numberUtils.md +54 -0
  50. package/runtime/numbers.gr +1043 -391
  51. package/runtime/numbers.md +300 -0
  52. package/runtime/string.gr +136 -230
  53. package/runtime/string.md +24 -0
  54. package/runtime/stringUtils.gr +58 -38
  55. package/runtime/stringUtils.md +6 -0
  56. package/runtime/unsafe/constants.gr +17 -0
  57. package/runtime/unsafe/constants.md +72 -0
  58. package/runtime/unsafe/conv.md +71 -0
  59. package/runtime/unsafe/errors.md +204 -0
  60. package/runtime/unsafe/memory.md +54 -0
  61. package/runtime/unsafe/printWasm.md +24 -0
  62. package/runtime/unsafe/tags.gr +9 -8
  63. package/runtime/unsafe/tags.md +120 -0
  64. package/runtime/unsafe/wasmf32.md +168 -0
  65. package/runtime/unsafe/wasmf64.md +168 -0
  66. package/runtime/unsafe/wasmi32.md +282 -0
  67. package/runtime/unsafe/wasmi64.md +300 -0
  68. package/runtime/utils/printing.gr +62 -0
  69. package/runtime/utils/printing.md +18 -0
  70. package/runtime/wasi.gr +1 -1
  71. package/runtime/wasi.md +839 -0
  72. package/set.gr +17 -8
  73. package/set.md +24 -21
  74. package/stack.gr +3 -3
  75. package/stack.md +4 -6
  76. package/string.gr +194 -329
  77. package/string.md +3 -3
  78. package/sys/file.gr +245 -429
  79. package/sys/process.gr +27 -45
  80. package/sys/random.gr +47 -16
  81. package/sys/random.md +38 -0
  82. package/sys/time.gr +11 -27
package/char.gr CHANGED
@@ -9,11 +9,12 @@
9
9
  */
10
10
 
11
11
  import WasmI32 from "runtime/unsafe/wasmi32"
12
- import Memory from "runtime/unsafe/memory"
13
12
  import Errors from "runtime/unsafe/errors"
13
+ import Tags from "runtime/unsafe/tags"
14
14
  import {
15
15
  tagSimpleNumber,
16
- allocateChar,
16
+ tagChar,
17
+ untagChar,
17
18
  allocateString,
18
19
  } from "runtime/dataStructures"
19
20
 
@@ -58,71 +59,13 @@ export let isValid = charCode => {
58
59
  *
59
60
  * @since 0.3.0
60
61
  */
61
- @disableGC
62
- export let rec code = (char: Char) => {
63
- // Algorithm from https://encoding.spec.whatwg.org/#utf-8-decoder
62
+ @unsafe
63
+ export let code = (char: Char) => {
64
+ let usv = untagChar(char)
64
65
 
65
- let char = WasmI32.fromGrain(char)
66
-
67
- let (+) = WasmI32.add
68
- let (==) = WasmI32.eq
69
- let (>=) = WasmI32.geU
70
- let (<=) = WasmI32.leU
71
- let (<<) = WasmI32.shl
72
- let (&) = WasmI32.and
73
- let (|) = WasmI32.or
74
-
75
- let mut codePoint = 0n
76
- let mut bytesSeen = 0n
77
- let mut bytesNeeded = 0n
78
- let mut lowerBoundary = 0x80n
79
- let mut upperBoundary = 0xBFn
80
-
81
- let mut offset = 0n
82
-
83
- let mut result = 0n
84
-
85
- while (true) {
86
- let byte = WasmI32.load8U(char + offset, 4n)
87
- offset += 1n
88
- if (bytesNeeded == 0n) {
89
- if (byte >= 0x00n && byte <= 0x7Fn) {
90
- result = byte
91
- break
92
- } else if (byte >= 0xC2n && byte <= 0xDFn) {
93
- bytesNeeded = 1n
94
- codePoint = byte & 0x1Fn
95
- } else if (byte >= 0xE0n && byte <= 0xEFn) {
96
- if (byte == 0xE0n) lowerBoundary = 0xA0n
97
- if (byte == 0xEDn) upperBoundary = 0x9Fn
98
- bytesNeeded = 2n
99
- codePoint = byte & 0xFn
100
- } else if (byte >= 0xF0n && byte <= 0xF4n) {
101
- if (byte == 0xF0n) lowerBoundary = 0x90n
102
- if (byte == 0xF4n) upperBoundary = 0x8Fn
103
- bytesNeeded = 3n
104
- codePoint = byte & 0x7n
105
- } else {
106
- throw MalformedUtf8
107
- }
108
- continue
109
- }
110
- if (!(lowerBoundary <= byte && byte <= upperBoundary)) {
111
- throw MalformedUtf8
112
- }
113
- lowerBoundary = 0x80n
114
- upperBoundary = 0xBFn
115
- codePoint = codePoint << 6n | byte & 0x3Fn
116
- bytesSeen += 1n
117
- if (bytesSeen == bytesNeeded) {
118
- result = codePoint
119
- break
120
- }
121
- }
122
-
123
- Memory.decRef(char)
124
- Memory.decRef(WasmI32.fromGrain(code))
125
- tagSimpleNumber(result)
66
+ // This could save an instruction by combining the two tagging operations,
67
+ // though we stick with tagSimpleNumber for simplicity.
68
+ tagSimpleNumber(usv)
126
69
  }
127
70
 
128
71
  /**
@@ -134,62 +77,26 @@ export let rec code = (char: Char) => {
134
77
  *
135
78
  * @since 0.3.0
136
79
  */
137
- @disableGC
138
- export let rec fromCode = (usv: Number) => {
139
- // Algorithm from https://encoding.spec.whatwg.org/#utf-8-encoder
140
-
141
- let (+) = WasmI32.add
80
+ @unsafe
81
+ export let fromCode = (usv: Number) => {
142
82
  let (-) = WasmI32.sub
143
- let (*) = WasmI32.mul
144
- let (==) = WasmI32.eq
145
- let (>) = WasmI32.gtU
146
- let (<=) = WasmI32.leU
147
- let (<) = WasmI32.ltU
148
- let (>>>) = WasmI32.shrU
149
- let (&) = WasmI32.and
150
- let (|) = WasmI32.or
83
+ let (<<) = WasmI32.shl
151
84
 
152
- let usv = WasmI32.fromGrain(usv)
153
- if ((usv & 1n) == 0n) {
85
+ if (!isValid(usv)) {
154
86
  throw InvalidArgument("Invalid character code")
155
87
  }
156
88
 
157
- let usv = usv >>> 1n
158
- let result = if (usv < 0x80n) {
159
- let char = allocateChar()
160
- WasmI32.store8(char, usv, 4n)
161
- WasmI32.toGrain(char): Char
162
- } else {
163
- let mut count = 0n
164
- let mut offset = 0n
165
- if (usv <= 0x07FFn) {
166
- count = 1n
167
- offset = 0xC0n
168
- } else if (usv <= 0xFFFFn) {
169
- count = 2n
170
- offset = 0xE0n
171
- } else {
172
- count = 3n
173
- offset = 0xF0n
174
- }
175
- let char = allocateChar()
176
- WasmI32.store8(char, (usv >>> 6n * count) + offset, 4n)
177
-
178
- let mut n = 0n
179
- while (count > 0n) {
180
- n += 1n
181
- let temp = usv >>> 6n * (count - 1n)
182
- WasmI32.store8(char + n, 0x80n | temp & 0x3Fn, 4n)
183
- count -= 1n
184
- }
89
+ // usv is now guaranteed to be a simple number
90
+ let usv = WasmI32.fromGrain(usv)
185
91
 
186
- WasmI32.toGrain(char): Char
187
- }
92
+ // Here we use a math trick to avoid fully untagging and retagging.
93
+ // Simple numbers are represented as 2n + 1 and chars are represented as
94
+ // 8n + 2. Quick reminder that shifting left is the equivalent of multiplying
95
+ // by 2, and that _GRAIN_CHAR_TAG_TYPE is equal to 2:
96
+ // 4(2n + 1) - 2 = 8n + 2
97
+ let char = (usv << 2n) - Tags._GRAIN_CHAR_TAG_TYPE
188
98
 
189
- // We've asserted that the original `code` was a stack allocated number so
190
- // no need to decRef it
191
- Memory.decRef(WasmI32.fromGrain(fromCode))
192
- result
99
+ WasmI32.toGrain(char): Char
193
100
  }
194
101
 
195
102
  /**
@@ -240,27 +147,50 @@ export let pred = char => {
240
147
  *
241
148
  * @since 0.3.0
242
149
  */
243
- @disableGC
244
- export let rec toString = (char: Char) => {
150
+ @unsafe
151
+ export let toString = (char: Char) => {
245
152
  let (+) = WasmI32.add
153
+ let (-) = WasmI32.sub
154
+ let (*) = WasmI32.mul
246
155
  let (&) = WasmI32.and
247
- let (==) = WasmI32.eq
156
+ let (|) = WasmI32.or
157
+ let (>>>) = WasmI32.shrU
158
+ let (<) = WasmI32.ltU
159
+ let (>) = WasmI32.gtU
160
+ let (<=) = WasmI32.leU
161
+
162
+ let usv = untagChar(char)
248
163
 
249
- let char = WasmI32.fromGrain(char)
250
- let byte = WasmI32.load8U(char, 4n)
251
- let n = if ((byte & 0x80n) == 0x00n) {
252
- 1n
253
- } else if ((byte & 0xF0n) == 0xF0n) {
254
- 4n
255
- } else if ((byte & 0xE0n) == 0xE0n) {
256
- 3n
164
+ let result = if (usv < 0x80n) {
165
+ let string = allocateString(1n)
166
+ WasmI32.store8(string, usv, 8n)
167
+ WasmI32.toGrain(string): String
257
168
  } else {
258
- 2n
169
+ let mut count = 0n
170
+ let mut offset = 0n
171
+ if (usv <= 0x07FFn) {
172
+ count = 1n
173
+ offset = 0xC0n
174
+ } else if (usv <= 0xFFFFn) {
175
+ count = 2n
176
+ offset = 0xE0n
177
+ } else {
178
+ count = 3n
179
+ offset = 0xF0n
180
+ }
181
+ let string = allocateString(count + 1n)
182
+ WasmI32.store8(string, (usv >>> 6n * count) + offset, 8n)
183
+
184
+ let mut n = 0n
185
+ while (count > 0n) {
186
+ n += 1n
187
+ let temp = usv >>> 6n * (count - 1n)
188
+ WasmI32.store8(string + n, 0x80n | temp & 0x3Fn, 8n)
189
+ count -= 1n
190
+ }
191
+
192
+ WasmI32.toGrain(string): String
259
193
  }
260
- let str = allocateString(n)
261
- Memory.copy(str + 8n, char + 4n, n)
262
- let ret = WasmI32.toGrain(str): String
263
- Memory.decRef(WasmI32.fromGrain(char))
264
- Memory.decRef(WasmI32.fromGrain(toString))
265
- ret
194
+
195
+ result
266
196
  }
package/exception.md ADDED
@@ -0,0 +1,6 @@
1
+ ### Exception.**registerPrinter**
2
+
3
+ ```grain
4
+ registerPrinter : (Exception -> Option<String>) -> Void
5
+ ```
6
+
package/float32.gr CHANGED
@@ -6,14 +6,11 @@
6
6
  */
7
7
  import WasmI32 from "runtime/unsafe/wasmi32"
8
8
  import WasmF32 from "runtime/unsafe/wasmf32"
9
- import Memory from "runtime/unsafe/memory"
10
- import {
11
- newFloat32
12
- } from "runtime/dataStructures"
9
+ import { newFloat32 } from "runtime/dataStructures"
13
10
 
14
11
  import {
15
12
  coerceNumberToFloat32 as fromNumber,
16
- coerceFloat32ToNumber as toNumber
13
+ coerceFloat32ToNumber as toNumber,
17
14
  } from "runtime/numbers"
18
15
 
19
16
  /**
@@ -53,16 +50,12 @@ export toNumber
53
50
  *
54
51
  * @since v0.2.0
55
52
  */
56
- @disableGC
57
- export let rec add = (x: Float32, y: Float32) => {
53
+ @unsafe
54
+ export let add = (x: Float32, y: Float32) => {
58
55
  let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
59
56
  let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
60
57
  let ptr = newFloat32(WasmF32.add(xv, yv))
61
- let ret = WasmI32.toGrain(ptr) : Float32
62
- Memory.decRef(WasmI32.fromGrain(x))
63
- Memory.decRef(WasmI32.fromGrain(y))
64
- Memory.decRef(WasmI32.fromGrain(add))
65
- ret
58
+ WasmI32.toGrain(ptr): Float32
66
59
  }
67
60
 
68
61
  /**
@@ -74,16 +67,12 @@ export let rec add = (x: Float32, y: Float32) => {
74
67
  *
75
68
  * @since v0.2.0
76
69
  */
77
- @disableGC
78
- export let rec sub = (x: Float32, y: Float32) => {
70
+ @unsafe
71
+ export let sub = (x: Float32, y: Float32) => {
79
72
  let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
80
73
  let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
81
74
  let ptr = newFloat32(WasmF32.sub(xv, yv))
82
- let ret = WasmI32.toGrain(ptr) : Float32
83
- Memory.decRef(WasmI32.fromGrain(x))
84
- Memory.decRef(WasmI32.fromGrain(y))
85
- Memory.decRef(WasmI32.fromGrain(sub))
86
- ret
75
+ WasmI32.toGrain(ptr): Float32
87
76
  }
88
77
 
89
78
  /**
@@ -95,16 +84,12 @@ export let rec sub = (x: Float32, y: Float32) => {
95
84
  *
96
85
  * @since v0.2.0
97
86
  */
98
- @disableGC
99
- export let rec mul = (x: Float32, y: Float32) => {
87
+ @unsafe
88
+ export let mul = (x: Float32, y: Float32) => {
100
89
  let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
101
90
  let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
102
91
  let ptr = newFloat32(WasmF32.mul(xv, yv))
103
- let ret = WasmI32.toGrain(ptr) : Float32
104
- Memory.decRef(WasmI32.fromGrain(x))
105
- Memory.decRef(WasmI32.fromGrain(y))
106
- Memory.decRef(WasmI32.fromGrain(mul))
107
- ret
92
+ WasmI32.toGrain(ptr): Float32
108
93
  }
109
94
 
110
95
  /**
@@ -116,16 +101,12 @@ export let rec mul = (x: Float32, y: Float32) => {
116
101
  *
117
102
  * @since v0.2.0
118
103
  */
119
- @disableGC
120
- export let rec div = (x: Float32, y: Float32) => {
104
+ @unsafe
105
+ export let div = (x: Float32, y: Float32) => {
121
106
  let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
122
107
  let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
123
108
  let ptr = newFloat32(WasmF32.div(xv, yv))
124
- let ret = WasmI32.toGrain(ptr) : Float32
125
- Memory.decRef(WasmI32.fromGrain(x))
126
- Memory.decRef(WasmI32.fromGrain(y))
127
- Memory.decRef(WasmI32.fromGrain(div))
128
- ret
109
+ WasmI32.toGrain(ptr): Float32
129
110
  }
130
111
 
131
112
  /**
@@ -141,15 +122,11 @@ export let rec div = (x: Float32, y: Float32) => {
141
122
  *
142
123
  * @since v0.2.0
143
124
  */
144
- @disableGC
145
- export let rec lt = (x: Float32, y: Float32) => {
125
+ @unsafe
126
+ export let lt = (x: Float32, y: Float32) => {
146
127
  let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
147
128
  let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
148
- let ret = WasmF32.lt(xv, yv)
149
- Memory.decRef(WasmI32.fromGrain(x))
150
- Memory.decRef(WasmI32.fromGrain(y))
151
- Memory.decRef(WasmI32.fromGrain(lt))
152
- ret
129
+ WasmF32.lt(xv, yv)
153
130
  }
154
131
 
155
132
  /**
@@ -161,15 +138,11 @@ export let rec lt = (x: Float32, y: Float32) => {
161
138
  *
162
139
  * @since v0.2.0
163
140
  */
164
- @disableGC
165
- export let rec gt = (x: Float32, y: Float32) => {
141
+ @unsafe
142
+ export let gt = (x: Float32, y: Float32) => {
166
143
  let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
167
144
  let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
168
- let ret = WasmF32.gt(xv, yv)
169
- Memory.decRef(WasmI32.fromGrain(x))
170
- Memory.decRef(WasmI32.fromGrain(y))
171
- Memory.decRef(WasmI32.fromGrain(gt))
172
- ret
145
+ WasmF32.gt(xv, yv)
173
146
  }
174
147
 
175
148
  /**
@@ -181,15 +154,11 @@ export let rec gt = (x: Float32, y: Float32) => {
181
154
  *
182
155
  * @since v0.2.0
183
156
  */
184
- @disableGC
185
- export let rec lte = (x: Float32, y: Float32) => {
157
+ @unsafe
158
+ export let lte = (x: Float32, y: Float32) => {
186
159
  let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
187
160
  let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
188
- let ret = WasmF32.le(xv, yv)
189
- Memory.decRef(WasmI32.fromGrain(x))
190
- Memory.decRef(WasmI32.fromGrain(y))
191
- Memory.decRef(WasmI32.fromGrain(lte))
192
- ret
161
+ WasmF32.le(xv, yv)
193
162
  }
194
163
 
195
164
  /**
@@ -201,42 +170,28 @@ export let rec lte = (x: Float32, y: Float32) => {
201
170
  *
202
171
  * @since v0.2.0
203
172
  */
204
- @disableGC
205
- export let rec gte = (x: Float32, y: Float32) => {
173
+ @unsafe
174
+ export let gte = (x: Float32, y: Float32) => {
206
175
  let xv = WasmF32.load(WasmI32.fromGrain(x), 8n)
207
176
  let yv = WasmF32.load(WasmI32.fromGrain(y), 8n)
208
- let ret = WasmF32.ge(xv, yv)
209
- Memory.decRef(WasmI32.fromGrain(x))
210
- Memory.decRef(WasmI32.fromGrain(y))
211
- Memory.decRef(WasmI32.fromGrain(gte))
212
- ret
177
+ WasmF32.ge(xv, yv)
213
178
  }
214
179
 
215
180
  /**
216
181
  * @section Constants: Float32 constant values.
217
182
  */
218
183
 
219
- @disableGC
220
- let rec makeInfinity = () => {
221
- let ptr = newFloat32(WasmF32.reinterpretI32(0b01111111100000000000000000000000n))
222
- let ret = WasmI32.toGrain(ptr) : Float32
223
- Memory.decRef(WasmI32.fromGrain(makeInfinity))
224
- ret
225
- }
226
-
227
184
  /**
228
185
  * Infinity represented as a Float32 value.
229
186
  *
230
187
  * @since v0.4.0
231
188
  */
232
- export let infinity = makeInfinity()
233
-
234
- @disableGC
235
- let rec makeNaN = () => {
236
- let ptr = newFloat32(WasmF32.reinterpretI32(0b01111111100000000000000000000001n))
237
- let ret = WasmI32.toGrain(ptr) : Float32
238
- Memory.decRef(WasmI32.fromGrain(makeNaN))
239
- ret
189
+ @unsafe
190
+ export let infinity = {
191
+ let ptr = newFloat32(
192
+ WasmF32.reinterpretI32(0b01111111100000000000000000000000n)
193
+ )
194
+ WasmI32.toGrain(ptr): Float32
240
195
  }
241
196
 
242
197
  /**
@@ -244,4 +199,10 @@ let rec makeNaN = () => {
244
199
  *
245
200
  * @since v0.4.0
246
201
  */
247
- export let nan = makeNaN()
202
+ @unsafe
203
+ export let nan = {
204
+ let ptr = newFloat32(
205
+ WasmF32.reinterpretI32(0b01111111100000000000000000000001n)
206
+ )
207
+ WasmI32.toGrain(ptr): Float32
208
+ }
package/float64.gr CHANGED
@@ -6,14 +6,11 @@
6
6
  */
7
7
  import WasmI32 from "runtime/unsafe/wasmi32"
8
8
  import WasmF64 from "runtime/unsafe/wasmf64"
9
- import Memory from "runtime/unsafe/memory"
10
- import {
11
- newFloat64
12
- } from "runtime/dataStructures"
9
+ import { newFloat64 } from "runtime/dataStructures"
13
10
 
14
11
  import {
15
12
  coerceNumberToFloat64 as fromNumber,
16
- coerceFloat64ToNumber as toNumber
13
+ coerceFloat64ToNumber as toNumber,
17
14
  } from "runtime/numbers"
18
15
 
19
16
  /**
@@ -53,16 +50,12 @@ export toNumber
53
50
  *
54
51
  * @since v0.2.0
55
52
  */
56
- @disableGC
57
- export let rec add = (x: Float64, y: Float64) => {
53
+ @unsafe
54
+ export let add = (x: Float64, y: Float64) => {
58
55
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
59
56
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
60
57
  let ptr = newFloat64(WasmF64.add(xv, yv))
61
- let ret = WasmI32.toGrain(ptr) : Float64
62
- Memory.decRef(WasmI32.fromGrain(x))
63
- Memory.decRef(WasmI32.fromGrain(y))
64
- Memory.decRef(WasmI32.fromGrain(add))
65
- ret
58
+ WasmI32.toGrain(ptr): Float64
66
59
  }
67
60
 
68
61
  /**
@@ -74,16 +67,12 @@ export let rec add = (x: Float64, y: Float64) => {
74
67
  *
75
68
  * @since v0.2.0
76
69
  */
77
- @disableGC
78
- export let rec sub = (x: Float64, y: Float64) => {
70
+ @unsafe
71
+ export let sub = (x: Float64, y: Float64) => {
79
72
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
80
73
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
81
74
  let ptr = newFloat64(WasmF64.sub(xv, yv))
82
- let ret = WasmI32.toGrain(ptr) : Float64
83
- Memory.decRef(WasmI32.fromGrain(x))
84
- Memory.decRef(WasmI32.fromGrain(y))
85
- Memory.decRef(WasmI32.fromGrain(sub))
86
- ret
75
+ WasmI32.toGrain(ptr): Float64
87
76
  }
88
77
 
89
78
  /**
@@ -95,16 +84,12 @@ export let rec sub = (x: Float64, y: Float64) => {
95
84
  *
96
85
  * @since v0.2.0
97
86
  */
98
- @disableGC
99
- export let rec mul = (x: Float64, y: Float64) => {
87
+ @unsafe
88
+ export let mul = (x: Float64, y: Float64) => {
100
89
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
101
90
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
102
91
  let ptr = newFloat64(WasmF64.mul(xv, yv))
103
- let ret = WasmI32.toGrain(ptr) : Float64
104
- Memory.decRef(WasmI32.fromGrain(x))
105
- Memory.decRef(WasmI32.fromGrain(y))
106
- Memory.decRef(WasmI32.fromGrain(mul))
107
- ret
92
+ WasmI32.toGrain(ptr): Float64
108
93
  }
109
94
 
110
95
  /**
@@ -116,16 +101,12 @@ export let rec mul = (x: Float64, y: Float64) => {
116
101
  *
117
102
  * @since v0.2.0
118
103
  */
119
- @disableGC
120
- export let rec div = (x: Float64, y: Float64) => {
104
+ @unsafe
105
+ export let div = (x: Float64, y: Float64) => {
121
106
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
122
107
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
123
108
  let ptr = newFloat64(WasmF64.div(xv, yv))
124
- let ret = WasmI32.toGrain(ptr) : Float64
125
- Memory.decRef(WasmI32.fromGrain(x))
126
- Memory.decRef(WasmI32.fromGrain(y))
127
- Memory.decRef(WasmI32.fromGrain(div))
128
- ret
109
+ WasmI32.toGrain(ptr): Float64
129
110
  }
130
111
 
131
112
  /**
@@ -141,15 +122,11 @@ export let rec div = (x: Float64, y: Float64) => {
141
122
  *
142
123
  * @since v0.2.0
143
124
  */
144
- @disableGC
145
- export let rec lt = (x: Float64, y: Float64) => {
125
+ @unsafe
126
+ export let lt = (x: Float64, y: Float64) => {
146
127
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
147
128
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
148
- let ret = WasmF64.lt(xv, yv)
149
- Memory.decRef(WasmI32.fromGrain(x))
150
- Memory.decRef(WasmI32.fromGrain(y))
151
- Memory.decRef(WasmI32.fromGrain(lt))
152
- ret
129
+ WasmF64.lt(xv, yv)
153
130
  }
154
131
 
155
132
  /**
@@ -161,15 +138,11 @@ export let rec lt = (x: Float64, y: Float64) => {
161
138
  *
162
139
  * @since v0.2.0
163
140
  */
164
- @disableGC
165
- export let rec gt = (x: Float64, y: Float64) => {
141
+ @unsafe
142
+ export let gt = (x: Float64, y: Float64) => {
166
143
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
167
144
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
168
- let ret = WasmF64.gt(xv, yv)
169
- Memory.decRef(WasmI32.fromGrain(x))
170
- Memory.decRef(WasmI32.fromGrain(y))
171
- Memory.decRef(WasmI32.fromGrain(gt))
172
- ret
145
+ WasmF64.gt(xv, yv)
173
146
  }
174
147
 
175
148
  /**
@@ -181,15 +154,11 @@ export let rec gt = (x: Float64, y: Float64) => {
181
154
  *
182
155
  * @since v0.2.0
183
156
  */
184
- @disableGC
185
- export let rec lte = (x: Float64, y: Float64) => {
157
+ @unsafe
158
+ export let lte = (x: Float64, y: Float64) => {
186
159
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
187
160
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
188
- let ret = WasmF64.le(xv, yv)
189
- Memory.decRef(WasmI32.fromGrain(x))
190
- Memory.decRef(WasmI32.fromGrain(y))
191
- Memory.decRef(WasmI32.fromGrain(lte))
192
- ret
161
+ WasmF64.le(xv, yv)
193
162
  }
194
163
 
195
164
  /**
@@ -201,42 +170,30 @@ export let rec lte = (x: Float64, y: Float64) => {
201
170
  *
202
171
  * @since v0.2.0
203
172
  */
204
- @disableGC
205
- export let rec gte = (x: Float64, y: Float64) => {
173
+ @unsafe
174
+ export let gte = (x: Float64, y: Float64) => {
206
175
  let xv = WasmF64.load(WasmI32.fromGrain(x), 8n)
207
176
  let yv = WasmF64.load(WasmI32.fromGrain(y), 8n)
208
- let ret = WasmF64.ge(xv, yv)
209
- Memory.decRef(WasmI32.fromGrain(x))
210
- Memory.decRef(WasmI32.fromGrain(y))
211
- Memory.decRef(WasmI32.fromGrain(gte))
212
- ret
177
+ WasmF64.ge(xv, yv)
213
178
  }
214
179
 
215
180
  /**
216
181
  * @section Constants: Float64 constant values.
217
182
  */
218
183
 
219
- @disableGC
220
- let rec makeInfinity = () => {
221
- let ptr = newFloat64(WasmF64.reinterpretI64(0b0111111111110000000000000000000000000000000000000000000000000000N))
222
- let ret = WasmI32.toGrain(ptr) : Float64
223
- Memory.decRef(WasmI32.fromGrain(makeInfinity))
224
- ret
225
- }
226
-
227
184
  /**
228
185
  * Infinity represented as a Float64 value.
229
186
  *
230
187
  * @since v0.4.0
231
188
  */
232
- export let infinity = makeInfinity()
233
-
234
- @disableGC
235
- let rec makeNaN = () => {
236
- let ptr = newFloat64(WasmF64.reinterpretI64(0b0111111111110000000000000000000000000000000000000000000000000001N))
237
- let ret = WasmI32.toGrain(ptr) : Float64
238
- Memory.decRef(WasmI32.fromGrain(makeNaN))
239
- ret
189
+ @unsafe
190
+ export let infinity = {
191
+ let ptr = newFloat64(
192
+ WasmF64.reinterpretI64(
193
+ 0b0111111111110000000000000000000000000000000000000000000000000000N
194
+ )
195
+ )
196
+ WasmI32.toGrain(ptr): Float64
240
197
  }
241
198
 
242
199
  /**
@@ -244,4 +201,12 @@ let rec makeNaN = () => {
244
201
  *
245
202
  * @since v0.4.0
246
203
  */
247
- export let nan = makeNaN()
204
+ @unsafe
205
+ export let nan = {
206
+ let ptr = newFloat64(
207
+ WasmF64.reinterpretI64(
208
+ 0b0111111111110000000000000000000000000000000000000000000000000001N
209
+ )
210
+ )
211
+ WasmI32.toGrain(ptr): Float64
212
+ }