forked from grain-lang/grain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugPrint.gr
70 lines (59 loc) 路 1.49 KB
/
debugPrint.gr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module DebugPrint
from "runtime/numberUtils" include NumberUtils as Utils
from "runtime/unsafe/wasmi32" include WasmI32
use WasmI32.{ (+) }
from "runtime/unsafe/wasmf64" include WasmF64
from "runtime/unsafe/memory" include Memory
foreign wasm fd_write:
(WasmI32, WasmI32, WasmI32, WasmI32) => WasmI32 from "wasi_snapshot_preview1"
@unsafe
provide let print = (s: String) => {
let ptr = WasmI32.fromGrain(s)
// iov: [<ptr to string> <nbytes of string> <ptr to newline> <nbytes of newline>] (32 bytes)
// buf: <iov> <written> <newline char>
// fd_write(STDOUT (1), iov, len(iov), written)
let buf = Memory.malloc(37n)
let iov = buf
let written = buf + 32n
let lf = buf + 36n
WasmI32.store(iov, ptr + 8n, 0n)
WasmI32.store(iov, WasmI32.load(ptr, 4n), 4n)
WasmI32.store8(lf, 10n, 0n)
WasmI32.store(iov, lf, 8n)
WasmI32.store(iov, 1n, 12n)
fd_write(1n, iov, 2n, written)
Memory.free(buf)
void
}
@unsafe
provide let printI32 = val => {
print(Utils.itoa32(val, 10n))
}
@unsafe
provide let printI64 = val => {
print(Utils.itoa64(val, 10n))
}
@unsafe
provide let printF32 = val => {
print(Utils.dtoa(WasmF64.promoteF32(val)))
}
@unsafe
provide let printF64 = val => {
print(Utils.dtoa(val))
}
@unsafe
provide let toStringI32 = val => {
Utils.itoa32(val, 10n)
}
@unsafe
provide let toStringI64 = val => {
Utils.itoa64(val, 10n)
}
@unsafe
provide let toStringF32 = val => {
Utils.dtoa(WasmF64.promoteF32(val))
}
@unsafe
provide let toStringF64 = val => {
Utils.dtoa(val)
}