grain
grain copied to clipboard
print without new line
I found no way in standard library to print a string without appending a new line.
So I implemented one for my needs for now based on print(), but I'm not sure it's correct as I have virtually no experience in writing wasm bytecode. Is it desirable to have such a function in standard library? Should I make a pull request?
@disableGC
export let printNoNewLine = (value) => {
let ptr = WasmI32.fromGrain(String.toString(value))
let buf = Memory.malloc(37n)
let iov = buf
let written = buf + 32n
WasmI32.store(iov, ptr + 8n, 0n)
WasmI32.store(iov, WasmI32.load(ptr, 4n), 4n)
WasmI32.store(iov, 1n, 8n)
fd_write(1n, iov, 1n, written) // originally the 3rd param is 2n in String.print. What does it do?
Memory.free(buf)
}
Yeah, we'll definitely want a way to be able to do this. I imagine that we'll have a Printf
library, though there are some considerations to be made there. I'll see what the core team thinks about it.
import File from "sys/file" File.fdWrite(File.stdout, stringvalue) void
As @bluebat mentioned, this has been possible for some time.