Skip to content

Commit

Permalink
fix(stdlib): Implement print using a single element io vec (#2066)
Browse files Browse the repository at this point in the history
Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
  • Loading branch information
alex-snezhko and ospencer committed Mar 12, 2024
1 parent e8d643c commit 9eeb0f2
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions stdlib/runtime/string.gr
Expand Up @@ -861,20 +861,20 @@ provide let print = (value, suffix="\n") => {
// First convert the value to string, if it isn't one already.
let valuePtr = WasmI32.fromGrain(value)
let s = toString(value)
let ptr = WasmI32.fromGrain(s)
let suffixPtr = WasmI32.fromGrain(suffix)
// iov: [<ptr to string> <nbytes of string> <ptr to end sequence> <nbytes of end sequence>] (32 bytes)
let combined = concat(s, suffix)
let ptr = WasmI32.fromGrain(combined)
// iov: [<ptr to string> <nbytes of string>]
// buf: <iov> <written>
// fd_write(STDOUT (1), iov, len(iov), written)
let buf = Memory.malloc(36n)
let buf = Memory.malloc(20n)
let iov = buf
let written = buf + 32n
let written = buf + 16n
WasmI32.store(iov, ptr + 8n, 0n)
WasmI32.store(iov, WasmI32.load(ptr, 4n), 4n)
WasmI32.store(iov, suffixPtr + 8n, 8n)
WasmI32.store(iov, WasmI32.load(suffixPtr, 4n), 12n)
fd_write(1n, iov, 2n, written)
fd_write(1n, iov, 1n, written)
Memory.free(buf)
ignore(value)
ignore(suffix)
void
}

Expand Down

0 comments on commit 9eeb0f2

Please sign in to comment.