@grain/stdlib 0.6.5 → 0.7.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.
- package/CHANGELOG.md +64 -0
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/array.gr +55 -7
- package/array.md +123 -77
- package/bigint.md +30 -30
- package/buffer.gr +20 -53
- package/buffer.md +47 -47
- package/bytes.gr +111 -35
- package/bytes.md +111 -32
- package/char.gr +201 -99
- package/char.md +361 -34
- package/exception.gr +11 -11
- package/exception.md +26 -1
- package/float32.gr +327 -3
- package/float32.md +606 -19
- package/float64.gr +320 -3
- package/float64.md +606 -19
- package/fs.gr +1082 -0
- package/fs.md +630 -0
- package/hash.gr +142 -88
- package/hash.md +102 -14
- package/int16.md +23 -23
- package/int32.gr +25 -4
- package/int32.md +65 -30
- package/int64.gr +26 -1
- package/int64.md +65 -30
- package/int8.md +23 -23
- package/json.gr +366 -51
- package/json.md +418 -2
- package/list.gr +387 -49
- package/list.md +492 -69
- package/map.gr +20 -12
- package/map.md +44 -38
- package/marshal.gr +41 -40
- package/marshal.md +2 -2
- package/number.gr +159 -30
- package/number.md +215 -38
- package/option.md +21 -21
- package/package.json +5 -3
- package/path.gr +48 -0
- package/path.md +103 -12
- package/pervasives.gr +2 -2
- package/pervasives.md +37 -37
- package/priorityqueue.gr +7 -7
- package/priorityqueue.md +19 -19
- package/queue.gr +183 -29
- package/queue.md +296 -40
- package/random.md +6 -6
- package/range.gr +4 -4
- package/range.md +6 -6
- package/rational.md +16 -16
- package/regex.gr +52 -51
- package/regex.md +11 -11
- package/result.md +16 -16
- package/runtime/atof/common.md +39 -39
- package/runtime/atof/decimal.gr +6 -6
- package/runtime/atof/decimal.md +8 -8
- package/runtime/atof/lemire.gr +5 -5
- package/runtime/atof/lemire.md +1 -1
- package/runtime/atof/parse.gr +16 -16
- package/runtime/atof/parse.md +2 -2
- package/runtime/atof/slow.md +1 -1
- package/runtime/atof/table.md +2 -2
- package/runtime/atoi/parse.gr +3 -3
- package/runtime/atoi/parse.md +1 -1
- package/runtime/bigint.gr +15 -47
- package/runtime/bigint.md +54 -60
- package/runtime/compare.gr +2 -2
- package/runtime/compare.md +1 -1
- package/runtime/dataStructures.md +33 -33
- package/runtime/debugPrint.gr +4 -1
- package/runtime/debugPrint.md +9 -9
- package/runtime/equal.gr +99 -77
- package/runtime/equal.md +1 -1
- package/runtime/exception.gr +62 -82
- package/runtime/exception.md +62 -11
- package/runtime/gc.gr +39 -45
- package/runtime/gc.md +4 -4
- package/runtime/malloc.gr +7 -7
- package/runtime/malloc.md +4 -4
- package/runtime/math/kernel/cos.gr +70 -0
- package/runtime/math/kernel/cos.md +14 -0
- package/runtime/math/kernel/sin.gr +65 -0
- package/runtime/math/kernel/sin.md +14 -0
- package/runtime/math/kernel/tan.gr +136 -0
- package/runtime/math/kernel/tan.md +14 -0
- package/runtime/math/rempio2.gr +244 -0
- package/runtime/math/rempio2.md +14 -0
- package/runtime/math/trig.gr +130 -0
- package/runtime/math/trig.md +28 -0
- package/runtime/math/umuldi.gr +26 -0
- package/runtime/math/umuldi.md +14 -0
- package/runtime/numberUtils.gr +29 -29
- package/runtime/numberUtils.md +12 -12
- package/runtime/numbers.gr +373 -381
- package/runtime/numbers.md +79 -73
- package/runtime/string.gr +37 -105
- package/runtime/string.md +3 -9
- package/runtime/unsafe/constants.md +24 -24
- package/runtime/unsafe/conv.md +13 -13
- package/runtime/unsafe/memory.gr +24 -20
- package/runtime/unsafe/memory.md +27 -7
- package/runtime/unsafe/offsets.gr +36 -0
- package/runtime/unsafe/offsets.md +88 -0
- package/runtime/unsafe/panic.gr +28 -0
- package/runtime/unsafe/panic.md +14 -0
- package/runtime/unsafe/tags.md +32 -32
- package/runtime/unsafe/wasmf32.md +28 -28
- package/runtime/unsafe/wasmf64.md +28 -28
- package/runtime/unsafe/wasmi32.md +47 -47
- package/runtime/unsafe/wasmi64.md +50 -50
- package/runtime/utf8.gr +189 -0
- package/runtime/utf8.md +117 -0
- package/runtime/wasi.gr +4 -2
- package/runtime/wasi.md +138 -138
- package/set.gr +18 -11
- package/set.md +42 -36
- package/stack.gr +171 -2
- package/stack.md +297 -15
- package/string.gr +352 -557
- package/string.md +77 -34
- package/uint16.gr +81 -0
- package/uint16.md +183 -22
- package/uint32.gr +25 -4
- package/uint32.md +63 -28
- package/uint64.gr +25 -5
- package/uint64.md +63 -28
- package/uint8.gr +81 -0
- package/uint8.md +183 -22
- package/uri.gr +57 -53
- package/uri.md +11 -12
- package/wasi/file.gr +67 -59
- package/wasi/file.md +39 -39
- package/wasi/process.md +5 -5
- package/wasi/random.md +3 -3
- package/wasi/time.md +4 -4
- package/runtime/utils/printing.gr +0 -60
- package/runtime/utils/printing.md +0 -26
package/runtime/string.gr
CHANGED
|
@@ -27,7 +27,8 @@ from "runtime/bigint" include Bigint as BI
|
|
|
27
27
|
from "runtime/unsafe/memory" include Memory
|
|
28
28
|
from "runtime/unsafe/tags" include Tags
|
|
29
29
|
from "runtime/numberUtils" include NumberUtils
|
|
30
|
-
|
|
30
|
+
from "runtime/utf8" include Utf8
|
|
31
|
+
use Utf8.{ usvEncodeLength, writeUtf8CodePoint }
|
|
31
32
|
from "runtime/dataStructures" include DataStructures
|
|
32
33
|
use DataStructures.{ allocateString, allocateArray, untagSimpleNumber }
|
|
33
34
|
|
|
@@ -39,9 +40,6 @@ primitive (&&) = "@and"
|
|
|
39
40
|
primitive (||) = "@or"
|
|
40
41
|
primitive builtinId = "@builtin.id"
|
|
41
42
|
primitive ignore = "@ignore"
|
|
42
|
-
primitive throw = "@throw"
|
|
43
|
-
|
|
44
|
-
exception MalformedUnicode
|
|
45
43
|
|
|
46
44
|
@unsafe
|
|
47
45
|
primitive typeMetadata = "@heap.type_metadata"
|
|
@@ -71,12 +69,16 @@ let _LIST_ID = untagSimpleNumber(builtinId("List"))
|
|
|
71
69
|
let _OPTION_ID = untagSimpleNumber(builtinId("Option"))
|
|
72
70
|
@unsafe
|
|
73
71
|
let _RESULT_ID = untagSimpleNumber(builtinId("Result"))
|
|
72
|
+
@unsafe
|
|
73
|
+
let _RANGE_ID = untagSimpleNumber(builtinId("Range"))
|
|
74
74
|
|
|
75
75
|
let _SOME = "Some"
|
|
76
76
|
let _NONE = "None"
|
|
77
77
|
let _OK = "Ok"
|
|
78
78
|
let _ERR = "Err"
|
|
79
79
|
|
|
80
|
+
let _RANGE_FIELDS = [> "rangeStart", "rangeEnd"]
|
|
81
|
+
|
|
80
82
|
// Resizable arrays: <num items> <capacity> <...data>
|
|
81
83
|
@unsafe
|
|
82
84
|
let _VEC_LEN_OFFSET = 0n
|
|
@@ -163,6 +165,12 @@ let isListVariant = variant => {
|
|
|
163
165
|
typeId == _LIST_ID
|
|
164
166
|
}
|
|
165
167
|
|
|
168
|
+
@unsafe
|
|
169
|
+
let isRangeRecord = record_ => {
|
|
170
|
+
let typeId = WasmI32.load(record_, 8n) >> 1n
|
|
171
|
+
typeId == _RANGE_ID
|
|
172
|
+
}
|
|
173
|
+
|
|
166
174
|
@unsafe
|
|
167
175
|
let getBuiltinVariantName = variant => {
|
|
168
176
|
let typeId = WasmI32.load(variant, 8n) >> 1n
|
|
@@ -231,14 +239,18 @@ let getVariantMetadata = variant => {
|
|
|
231
239
|
@unsafe
|
|
232
240
|
let getRecordFieldNames = record_ => {
|
|
233
241
|
let typeHash = WasmI32.load(record_, 4n) >> 1n
|
|
234
|
-
|
|
242
|
+
if (isRangeRecord(record_)) {
|
|
243
|
+
return Memory.incRef(WasmI32.fromGrain(_RANGE_FIELDS))
|
|
244
|
+
} else {
|
|
245
|
+
let arity = WasmI32.load(record_, 12n)
|
|
235
246
|
|
|
236
|
-
|
|
247
|
+
let mut fields = findTypeMetadata(typeHash)
|
|
237
248
|
|
|
238
|
-
|
|
249
|
+
if (fields == -1n) return -1n
|
|
239
250
|
|
|
240
|
-
|
|
241
|
-
|
|
251
|
+
fields += 4n
|
|
252
|
+
return getFieldArray(fields, arity)
|
|
253
|
+
}
|
|
242
254
|
}
|
|
243
255
|
|
|
244
256
|
@unsafe
|
|
@@ -333,9 +345,10 @@ let escape = (ptr, isString) => {
|
|
|
333
345
|
for (let mut i = 0n; i < size; i += 1n) {
|
|
334
346
|
let byte = WasmI32.load8U(ptr + i, startOffset)
|
|
335
347
|
if (
|
|
336
|
-
byte >= _SEQ_B && byte <= _SEQ_R
|
|
337
|
-
|
|
338
|
-
byte ==
|
|
348
|
+
byte >= _SEQ_B && byte <= _SEQ_R
|
|
349
|
+
|| /* b, f, n, r, t, v */
|
|
350
|
+
byte == _SEQ_SLASH
|
|
351
|
+
|| byte == _SEQ_QUOTE
|
|
339
352
|
) {
|
|
340
353
|
newSize += 2n
|
|
341
354
|
} else {
|
|
@@ -351,9 +364,10 @@ let escape = (ptr, isString) => {
|
|
|
351
364
|
for (let mut i = 0n; i < size; i += 1n) {
|
|
352
365
|
let byte = WasmI32.load8U(ptr + i, startOffset)
|
|
353
366
|
if (
|
|
354
|
-
byte >= _SEQ_B && byte <= _SEQ_R
|
|
355
|
-
|
|
356
|
-
byte ==
|
|
367
|
+
byte >= _SEQ_B && byte <= _SEQ_R
|
|
368
|
+
|| /* b, f, n, r, t, v */
|
|
369
|
+
byte == _SEQ_SLASH
|
|
370
|
+
|| byte == _SEQ_QUOTE
|
|
357
371
|
) {
|
|
358
372
|
WasmI32.store8(escapedString + j, _SEQ_SLASH, stringOffset)
|
|
359
373
|
j += 1n
|
|
@@ -390,40 +404,6 @@ let escapeChar = (s: String) => {
|
|
|
390
404
|
escape(WasmI32.fromGrain(s), false)
|
|
391
405
|
}
|
|
392
406
|
|
|
393
|
-
@unsafe
|
|
394
|
-
let usvToString = usv => {
|
|
395
|
-
if (usv < 0x80n) {
|
|
396
|
-
let string = allocateString(1n)
|
|
397
|
-
WasmI32.store8(string, usv, 8n)
|
|
398
|
-
WasmI32.toGrain(string): String
|
|
399
|
-
} else {
|
|
400
|
-
let mut count = 0n
|
|
401
|
-
let mut offset = 0n
|
|
402
|
-
if (usv <= 0x07FFn) {
|
|
403
|
-
count = 1n
|
|
404
|
-
offset = 0xC0n
|
|
405
|
-
} else if (usv <= 0xFFFFn) {
|
|
406
|
-
count = 2n
|
|
407
|
-
offset = 0xE0n
|
|
408
|
-
} else {
|
|
409
|
-
count = 3n
|
|
410
|
-
offset = 0xF0n
|
|
411
|
-
}
|
|
412
|
-
let string = allocateString(count + 1n)
|
|
413
|
-
WasmI32.store8(string, (usv >>> (6n * count)) + offset, 8n)
|
|
414
|
-
|
|
415
|
-
let mut n = 0n
|
|
416
|
-
while (count > 0n) {
|
|
417
|
-
n += 1n
|
|
418
|
-
let temp = usv >>> (6n * (count - 1n))
|
|
419
|
-
WasmI32.store8(string + n, 0x80n | temp & 0x3Fn, 8n)
|
|
420
|
-
count -= 1n
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
WasmI32.toGrain(string): String
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
|
|
427
407
|
@unsafe
|
|
428
408
|
let reportCycle = (ptr, cycles) => {
|
|
429
409
|
let mut cycleNum = vecFindIndex(cycles, ptr)
|
|
@@ -693,20 +673,23 @@ and toStringHelp = (grainValue, extraIndents, toplevel, cycles) => {
|
|
|
693
673
|
let shortVal = grainValue >> 8n
|
|
694
674
|
let shortValTag = (grainValue & 0xF8n) >> 3n
|
|
695
675
|
if (shortValTag == Tags._GRAIN_CHAR_SHORTVAL_TAG) {
|
|
696
|
-
let
|
|
676
|
+
let byteCount = usvEncodeLength(shortVal)
|
|
677
|
+
let string = allocateString(byteCount)
|
|
678
|
+
writeUtf8CodePoint(string + 8n, shortVal)
|
|
679
|
+
let string = WasmI32.toGrain(string): String
|
|
697
680
|
if (toplevel) {
|
|
698
681
|
string
|
|
699
682
|
} else {
|
|
700
683
|
escapeChar(string)
|
|
701
684
|
}
|
|
702
685
|
} else if (
|
|
703
|
-
shortValTag == Tags._GRAIN_INT8_SHORTVAL_TAG
|
|
704
|
-
shortValTag == Tags._GRAIN_INT16_SHORTVAL_TAG
|
|
686
|
+
shortValTag == Tags._GRAIN_INT8_SHORTVAL_TAG
|
|
687
|
+
|| shortValTag == Tags._GRAIN_INT16_SHORTVAL_TAG
|
|
705
688
|
) {
|
|
706
689
|
NumberUtils.itoa32(shortVal, 10n)
|
|
707
690
|
} else if (
|
|
708
|
-
shortValTag == Tags._GRAIN_UINT8_SHORTVAL_TAG
|
|
709
|
-
shortValTag == Tags._GRAIN_UINT16_SHORTVAL_TAG
|
|
691
|
+
shortValTag == Tags._GRAIN_UINT8_SHORTVAL_TAG
|
|
692
|
+
|| shortValTag == Tags._GRAIN_UINT16_SHORTVAL_TAG
|
|
710
693
|
) {
|
|
711
694
|
NumberUtils.utoa32(shortVal, 10n)
|
|
712
695
|
} else {
|
|
@@ -878,54 +861,3 @@ provide let print = (value, suffix="\n") => {
|
|
|
878
861
|
ignore(suffix)
|
|
879
862
|
void
|
|
880
863
|
}
|
|
881
|
-
|
|
882
|
-
@unsafe
|
|
883
|
-
provide let getCodePoint = (ptr: WasmI32) => {
|
|
884
|
-
// Algorithm from https://encoding.spec.whatwg.org/#utf-8-decoder
|
|
885
|
-
use WasmI32.{ (+), (&), (|), (<<), leU as (<=), geU as (>=), (==) }
|
|
886
|
-
|
|
887
|
-
let mut codePoint = 0n
|
|
888
|
-
let mut bytesSeen = 0n
|
|
889
|
-
let mut bytesNeeded = 0n
|
|
890
|
-
let mut lowerBoundary = 0x80n
|
|
891
|
-
let mut upperBoundary = 0xBFn
|
|
892
|
-
|
|
893
|
-
let mut offset = 0n
|
|
894
|
-
|
|
895
|
-
while (true) {
|
|
896
|
-
let byte = WasmI32.load8U(ptr + offset, 0n)
|
|
897
|
-
offset += 1n
|
|
898
|
-
if (bytesNeeded == 0n) {
|
|
899
|
-
if (byte >= 0x00n && byte <= 0x7Fn) {
|
|
900
|
-
return byte
|
|
901
|
-
} else if (byte >= 0xC2n && byte <= 0xDFn) {
|
|
902
|
-
bytesNeeded = 1n
|
|
903
|
-
codePoint = byte & 0x1Fn
|
|
904
|
-
} else if (byte >= 0xE0n && byte <= 0xEFn) {
|
|
905
|
-
if (byte == 0xE0n) lowerBoundary = 0xA0n
|
|
906
|
-
if (byte == 0xEDn) upperBoundary = 0x9Fn
|
|
907
|
-
bytesNeeded = 2n
|
|
908
|
-
codePoint = byte & 0xFn
|
|
909
|
-
} else if (byte >= 0xF0n && byte <= 0xF4n) {
|
|
910
|
-
if (byte == 0xF0n) lowerBoundary = 0x90n
|
|
911
|
-
if (byte == 0xF4n) upperBoundary = 0x8Fn
|
|
912
|
-
bytesNeeded = 3n
|
|
913
|
-
codePoint = byte & 0x7n
|
|
914
|
-
} else {
|
|
915
|
-
throw MalformedUnicode
|
|
916
|
-
}
|
|
917
|
-
continue
|
|
918
|
-
}
|
|
919
|
-
if (!(lowerBoundary <= byte && byte <= upperBoundary)) {
|
|
920
|
-
throw MalformedUnicode
|
|
921
|
-
}
|
|
922
|
-
lowerBoundary = 0x80n
|
|
923
|
-
upperBoundary = 0xBFn
|
|
924
|
-
codePoint = codePoint << 6n | byte & 0x3Fn
|
|
925
|
-
bytesSeen += 1n
|
|
926
|
-
if (bytesSeen == bytesNeeded) {
|
|
927
|
-
return codePoint
|
|
928
|
-
}
|
|
929
|
-
}
|
|
930
|
-
return 0n
|
|
931
|
-
}
|
package/runtime/string.md
CHANGED
|
@@ -14,7 +14,7 @@ No other changes yet.
|
|
|
14
14
|
</details>
|
|
15
15
|
|
|
16
16
|
```grain
|
|
17
|
-
concat
|
|
17
|
+
concat: (str1: String, str2: String) => String
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
Concatenate two strings.
|
|
@@ -46,7 +46,7 @@ No other changes yet.
|
|
|
46
46
|
</details>
|
|
47
47
|
|
|
48
48
|
```grain
|
|
49
|
-
toString
|
|
49
|
+
toString: (value: a) => String
|
|
50
50
|
```
|
|
51
51
|
|
|
52
52
|
Converts the given operand to a string.
|
|
@@ -79,7 +79,7 @@ Returns:
|
|
|
79
79
|
</details>
|
|
80
80
|
|
|
81
81
|
```grain
|
|
82
|
-
print
|
|
82
|
+
print: (value: a, ?suffix: String) => Void
|
|
83
83
|
```
|
|
84
84
|
|
|
85
85
|
Prints the given operand to the console. Works for any type. Internally, calls `toString`
|
|
@@ -93,9 +93,3 @@ Parameters:
|
|
|
93
93
|
|`value`|`a`|The operand|
|
|
94
94
|
|`?suffix`|`String`|The string to print after the argument|
|
|
95
95
|
|
|
96
|
-
### String.**getCodePoint**
|
|
97
|
-
|
|
98
|
-
```grain
|
|
99
|
-
getCodePoint : (ptr: WasmI32) => WasmI32
|
|
100
|
-
```
|
|
101
|
-
|
|
@@ -9,144 +9,144 @@ Functions and constants included in the Constants module.
|
|
|
9
9
|
### Constants.**_SMIN_I32**
|
|
10
10
|
|
|
11
11
|
```grain
|
|
12
|
-
_SMIN_I32
|
|
12
|
+
_SMIN_I32: WasmI32
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
### Constants.**_SMAX_I32**
|
|
16
16
|
|
|
17
17
|
```grain
|
|
18
|
-
_SMAX_I32
|
|
18
|
+
_SMAX_I32: WasmI32
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
### Constants.**_UMIN_I32**
|
|
22
22
|
|
|
23
23
|
```grain
|
|
24
|
-
_UMIN_I32
|
|
24
|
+
_UMIN_I32: WasmI32
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
### Constants.**_UMAX_I32**
|
|
28
28
|
|
|
29
29
|
```grain
|
|
30
|
-
_UMAX_I32
|
|
30
|
+
_UMAX_I32: WasmI32
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
### Constants.**_SMIN8_I32**
|
|
34
34
|
|
|
35
35
|
```grain
|
|
36
|
-
_SMIN8_I32
|
|
36
|
+
_SMIN8_I32: WasmI32
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
### Constants.**_SMAX8_I32**
|
|
40
40
|
|
|
41
41
|
```grain
|
|
42
|
-
_SMAX8_I32
|
|
42
|
+
_SMAX8_I32: WasmI32
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
### Constants.**_UMAX8_I32**
|
|
46
46
|
|
|
47
47
|
```grain
|
|
48
|
-
_UMAX8_I32
|
|
48
|
+
_UMAX8_I32: WasmI32
|
|
49
49
|
```
|
|
50
50
|
|
|
51
51
|
### Constants.**_SMIN16_I32**
|
|
52
52
|
|
|
53
53
|
```grain
|
|
54
|
-
_SMIN16_I32
|
|
54
|
+
_SMIN16_I32: WasmI32
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
### Constants.**_SMAX16_I32**
|
|
58
58
|
|
|
59
59
|
```grain
|
|
60
|
-
_SMAX16_I32
|
|
60
|
+
_SMAX16_I32: WasmI32
|
|
61
61
|
```
|
|
62
62
|
|
|
63
63
|
### Constants.**_UMAX16_I32**
|
|
64
64
|
|
|
65
65
|
```grain
|
|
66
|
-
_UMAX16_I32
|
|
66
|
+
_UMAX16_I32: WasmI32
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
### Constants.**_SMIN_I64**
|
|
70
70
|
|
|
71
71
|
```grain
|
|
72
|
-
_SMIN_I64
|
|
72
|
+
_SMIN_I64: WasmI64
|
|
73
73
|
```
|
|
74
74
|
|
|
75
75
|
### Constants.**_SMAX_I64**
|
|
76
76
|
|
|
77
77
|
```grain
|
|
78
|
-
_SMAX_I64
|
|
78
|
+
_SMAX_I64: WasmI64
|
|
79
79
|
```
|
|
80
80
|
|
|
81
81
|
### Constants.**_UMIN_I64**
|
|
82
82
|
|
|
83
83
|
```grain
|
|
84
|
-
_UMIN_I64
|
|
84
|
+
_UMIN_I64: WasmI64
|
|
85
85
|
```
|
|
86
86
|
|
|
87
87
|
### Constants.**_UMAX_I64**
|
|
88
88
|
|
|
89
89
|
```grain
|
|
90
|
-
_UMAX_I64
|
|
90
|
+
_UMAX_I64: WasmI64
|
|
91
91
|
```
|
|
92
92
|
|
|
93
93
|
### Constants.**_SMIN8_I64**
|
|
94
94
|
|
|
95
95
|
```grain
|
|
96
|
-
_SMIN8_I64
|
|
96
|
+
_SMIN8_I64: WasmI64
|
|
97
97
|
```
|
|
98
98
|
|
|
99
99
|
### Constants.**_SMAX8_I64**
|
|
100
100
|
|
|
101
101
|
```grain
|
|
102
|
-
_SMAX8_I64
|
|
102
|
+
_SMAX8_I64: WasmI64
|
|
103
103
|
```
|
|
104
104
|
|
|
105
105
|
### Constants.**_UMAX8_I64**
|
|
106
106
|
|
|
107
107
|
```grain
|
|
108
|
-
_UMAX8_I64
|
|
108
|
+
_UMAX8_I64: WasmI64
|
|
109
109
|
```
|
|
110
110
|
|
|
111
111
|
### Constants.**_SMIN16_I64**
|
|
112
112
|
|
|
113
113
|
```grain
|
|
114
|
-
_SMIN16_I64
|
|
114
|
+
_SMIN16_I64: WasmI64
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
### Constants.**_SMAX16_I64**
|
|
118
118
|
|
|
119
119
|
```grain
|
|
120
|
-
_SMAX16_I64
|
|
120
|
+
_SMAX16_I64: WasmI64
|
|
121
121
|
```
|
|
122
122
|
|
|
123
123
|
### Constants.**_UMAX16_I64**
|
|
124
124
|
|
|
125
125
|
```grain
|
|
126
|
-
_UMAX16_I64
|
|
126
|
+
_UMAX16_I64: WasmI64
|
|
127
127
|
```
|
|
128
128
|
|
|
129
129
|
### Constants.**_SMIN32_I64**
|
|
130
130
|
|
|
131
131
|
```grain
|
|
132
|
-
_SMIN32_I64
|
|
132
|
+
_SMIN32_I64: WasmI64
|
|
133
133
|
```
|
|
134
134
|
|
|
135
135
|
### Constants.**_SMAX32_I64**
|
|
136
136
|
|
|
137
137
|
```grain
|
|
138
|
-
_SMAX32_I64
|
|
138
|
+
_SMAX32_I64: WasmI64
|
|
139
139
|
```
|
|
140
140
|
|
|
141
141
|
### Constants.**_UMIN32_I64**
|
|
142
142
|
|
|
143
143
|
```grain
|
|
144
|
-
_UMIN32_I64
|
|
144
|
+
_UMIN32_I64: WasmI64
|
|
145
145
|
```
|
|
146
146
|
|
|
147
147
|
### Constants.**_UMAX32_I64**
|
|
148
148
|
|
|
149
149
|
```grain
|
|
150
|
-
_UMAX32_I64
|
|
150
|
+
_UMAX32_I64: WasmI64
|
|
151
151
|
```
|
|
152
152
|
|
package/runtime/unsafe/conv.md
CHANGED
|
@@ -9,79 +9,79 @@ Functions and constants included in the Conv module.
|
|
|
9
9
|
### Conv.**toInt32**
|
|
10
10
|
|
|
11
11
|
```grain
|
|
12
|
-
toInt32
|
|
12
|
+
toInt32: (n: WasmI32) => Int32
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
### Conv.**toUint32**
|
|
16
16
|
|
|
17
17
|
```grain
|
|
18
|
-
toUint32
|
|
18
|
+
toUint32: (n: WasmI32) => Uint32
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
### Conv.**fromInt32**
|
|
22
22
|
|
|
23
23
|
```grain
|
|
24
|
-
fromInt32
|
|
24
|
+
fromInt32: (n: Int32) => WasmI32
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
### Conv.**fromUint32**
|
|
28
28
|
|
|
29
29
|
```grain
|
|
30
|
-
fromUint32
|
|
30
|
+
fromUint32: (n: Uint32) => WasmI32
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
### Conv.**toInt64**
|
|
34
34
|
|
|
35
35
|
```grain
|
|
36
|
-
toInt64
|
|
36
|
+
toInt64: (n: WasmI64) => Int64
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
### Conv.**toUint64**
|
|
40
40
|
|
|
41
41
|
```grain
|
|
42
|
-
toUint64
|
|
42
|
+
toUint64: (n: WasmI64) => Uint64
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
### Conv.**fromInt64**
|
|
46
46
|
|
|
47
47
|
```grain
|
|
48
|
-
fromInt64
|
|
48
|
+
fromInt64: (n: Int64) => WasmI64
|
|
49
49
|
```
|
|
50
50
|
|
|
51
51
|
### Conv.**fromUint64**
|
|
52
52
|
|
|
53
53
|
```grain
|
|
54
|
-
fromUint64
|
|
54
|
+
fromUint64: (n: Uint64) => WasmI64
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
### Conv.**toFloat32**
|
|
58
58
|
|
|
59
59
|
```grain
|
|
60
|
-
toFloat32
|
|
60
|
+
toFloat32: (n: WasmF32) => Float32
|
|
61
61
|
```
|
|
62
62
|
|
|
63
63
|
### Conv.**fromFloat32**
|
|
64
64
|
|
|
65
65
|
```grain
|
|
66
|
-
fromFloat32
|
|
66
|
+
fromFloat32: (n: Float32) => WasmF32
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
### Conv.**toFloat64**
|
|
70
70
|
|
|
71
71
|
```grain
|
|
72
|
-
toFloat64
|
|
72
|
+
toFloat64: (n: WasmF64) => Float64
|
|
73
73
|
```
|
|
74
74
|
|
|
75
75
|
### Conv.**fromFloat64**
|
|
76
76
|
|
|
77
77
|
```grain
|
|
78
|
-
fromFloat64
|
|
78
|
+
fromFloat64: (n: Float64) => WasmF64
|
|
79
79
|
```
|
|
80
80
|
|
|
81
81
|
### Conv.**wasmI32ToNumber**
|
|
82
82
|
|
|
83
83
|
```grain
|
|
84
|
-
wasmI32ToNumber
|
|
84
|
+
wasmI32ToNumber: (n: WasmI32) => Number
|
|
85
85
|
```
|
|
86
86
|
|
|
87
87
|
Converts a WasmI32 value to Number.
|
package/runtime/unsafe/memory.gr
CHANGED
|
@@ -4,38 +4,42 @@ module Memory
|
|
|
4
4
|
from "runtime/gc" include GC
|
|
5
5
|
use GC.{ malloc, free, incRef, decRef }
|
|
6
6
|
from "runtime/unsafe/wasmi32" include WasmI32
|
|
7
|
-
use WasmI32.{ (+), (-), (
|
|
7
|
+
use WasmI32.{ (+), (-), (!=), ltU as (<), gtU as (>) }
|
|
8
8
|
|
|
9
9
|
provide { malloc, free, incRef, decRef }
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Copies the source memory region to the destination memory region. Regions may overlap.
|
|
13
|
+
*
|
|
14
|
+
* @param dest: The destination memory region
|
|
15
|
+
* @param src: The source memory region
|
|
16
|
+
* @param length: The length of the memory region to copy
|
|
17
|
+
*/
|
|
18
|
+
provide let copy = (dest, src, length) => {
|
|
15
19
|
if (dest != src) {
|
|
16
20
|
if (dest < src) {
|
|
17
|
-
|
|
18
|
-
WasmI32.store8(dest, WasmI32.load8U(src,
|
|
19
|
-
dest += 1n
|
|
20
|
-
src += 1n
|
|
21
|
-
n -= 1n
|
|
21
|
+
for (let mut i = 0n; i < length; i += 1n) {
|
|
22
|
+
WasmI32.store8(dest, WasmI32.load8U(src, i), i)
|
|
22
23
|
}
|
|
23
24
|
} else {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
WasmI32.store8(dest + n, WasmI32.load8U(src + n, 0n), 0n)
|
|
25
|
+
// Copy backwards to ensure we do not overwrite on overlapping regions
|
|
26
|
+
for (let mut n = length; n > 0n; n -= 1n) {
|
|
27
|
+
WasmI32.store8(dest + n - 1n, WasmI32.load8U(src + n - 1n, 0n), 0n)
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Fills the given memory region with the given 1-byte value. Values larger than 1 byte will be truncated.
|
|
35
|
+
*
|
|
36
|
+
* @param dest: The destination memory region
|
|
37
|
+
* @param value: The value to fill the memory region with
|
|
38
|
+
* @param length: The length of the memory region to fill
|
|
39
|
+
*/
|
|
40
|
+
provide let fill = (dest, value, length) => {
|
|
41
|
+
for (let mut i = 0n; i < length; i += 1n) {
|
|
42
|
+
WasmI32.store8(dest, value, i)
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
|
package/runtime/unsafe/memory.md
CHANGED
|
@@ -9,42 +9,62 @@ Functions and constants included in the Memory module.
|
|
|
9
9
|
### Memory.**malloc**
|
|
10
10
|
|
|
11
11
|
```grain
|
|
12
|
-
malloc
|
|
12
|
+
malloc: (size: WasmI32) => WasmI32
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
### Memory.**free**
|
|
16
16
|
|
|
17
17
|
```grain
|
|
18
|
-
free
|
|
18
|
+
free: (userPtr: WasmI32) => Void
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
### Memory.**incRef**
|
|
22
22
|
|
|
23
23
|
```grain
|
|
24
|
-
incRef
|
|
24
|
+
incRef: (userPtr: WasmI32) => WasmI32
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
### Memory.**decRef**
|
|
28
28
|
|
|
29
29
|
```grain
|
|
30
|
-
decRef
|
|
30
|
+
decRef: (userPtr: WasmI32) => WasmI32
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
### Memory.**copy**
|
|
34
34
|
|
|
35
35
|
```grain
|
|
36
|
-
copy
|
|
36
|
+
copy: (dest: WasmI32, src: WasmI32, length: WasmI32) => Void
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
+
Copies the source memory region to the destination memory region. Regions may overlap.
|
|
40
|
+
|
|
41
|
+
Parameters:
|
|
42
|
+
|
|
43
|
+
|param|type|description|
|
|
44
|
+
|-----|----|-----------|
|
|
45
|
+
|`dest`|`WasmI32`|The destination memory region|
|
|
46
|
+
|`src`|`WasmI32`|The source memory region|
|
|
47
|
+
|`length`|`WasmI32`|The length of the memory region to copy|
|
|
48
|
+
|
|
39
49
|
### Memory.**fill**
|
|
40
50
|
|
|
41
51
|
```grain
|
|
42
|
-
fill
|
|
52
|
+
fill: (dest: WasmI32, value: WasmI32, length: WasmI32) => Void
|
|
43
53
|
```
|
|
44
54
|
|
|
55
|
+
Fills the given memory region with the given 1-byte value. Values larger than 1 byte will be truncated.
|
|
56
|
+
|
|
57
|
+
Parameters:
|
|
58
|
+
|
|
59
|
+
|param|type|description|
|
|
60
|
+
|-----|----|-----------|
|
|
61
|
+
|`dest`|`WasmI32`|The destination memory region|
|
|
62
|
+
|`value`|`WasmI32`|The value to fill the memory region with|
|
|
63
|
+
|`length`|`WasmI32`|The length of the memory region to fill|
|
|
64
|
+
|
|
45
65
|
### Memory.**compare**
|
|
46
66
|
|
|
47
67
|
```grain
|
|
48
|
-
compare
|
|
68
|
+
compare: (ptr1: WasmI32, ptr2: WasmI32, length: WasmI32) => WasmI32
|
|
49
69
|
```
|
|
50
70
|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Various common offsets for data structures throughout the runtime.
|
|
3
|
+
*/
|
|
4
|
+
@runtimeMode
|
|
5
|
+
module Offsets
|
|
6
|
+
|
|
7
|
+
// String
|
|
8
|
+
provide let _STR_LEN_OFFSET = 4n
|
|
9
|
+
provide let _STR_DATA_OFFSET = 8n
|
|
10
|
+
|
|
11
|
+
// Array
|
|
12
|
+
provide let _ARRAY_LEN_OFFSET = 4n
|
|
13
|
+
provide let _ARRAY_DATA_OFFSET = 8n
|
|
14
|
+
provide let _ARRAY_ITEM_SIZE = 4n
|
|
15
|
+
|
|
16
|
+
// Bytes
|
|
17
|
+
provide let _BYTES_LEN_OFFSET = 4n
|
|
18
|
+
provide let _BYTES_DATA_OFFSET = 8n
|
|
19
|
+
|
|
20
|
+
// Int32
|
|
21
|
+
provide let _INT32_VALUE_OFFSET = 4n
|
|
22
|
+
|
|
23
|
+
// Uint32
|
|
24
|
+
provide let _UINT32_VALUE_OFFSET = 4n
|
|
25
|
+
|
|
26
|
+
// Float32
|
|
27
|
+
provide let _FLOAT32_VALUE_OFFSET = 4n
|
|
28
|
+
|
|
29
|
+
// Int64
|
|
30
|
+
provide let _INT64_VALUE_OFFSET = 8n
|
|
31
|
+
|
|
32
|
+
// Uint64
|
|
33
|
+
provide let _UINT64_VALUE_OFFSET = 8n
|
|
34
|
+
|
|
35
|
+
// Float64
|
|
36
|
+
provide let _FLOAT64_VALUE_OFFSET = 8n
|