wasm: use the fixed length buffer for putchar

Signed-off-by: mathetake <takeshi@tetrate.io>
Этот коммит содержится в:
Takeshi Yoneda 2020-10-24 05:04:32 +09:00 коммит произвёл GitHub
родитель 1dec9dcbc4
коммит ffeff55706
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23

Просмотреть файл

@ -18,21 +18,30 @@ func fd_write(id uint32, iovs *__wasi_iovec_t, iovs_len uint, nwritten *uint) (e
func postinit() {}
const (
putcharBufferSize = 120
stdout = 1
)
// Using global variables to avoid heap allocation.
var (
putcharBuf = byte(0)
putcharIOVec = __wasi_iovec_t{
buf: unsafe.Pointer(&putcharBuf),
bufLen: 1,
putcharBuffer = [putcharBufferSize]byte{}
putcharPosition uint = 0
putcharIOVec = __wasi_iovec_t{
buf: unsafe.Pointer(&putcharBuffer[0]),
}
putcharNWritten uint
)
func putchar(c byte) {
// write to stdout
const stdout = 1
var nwritten uint
putcharBuf = c
fd_write(stdout, &putcharIOVec, 1, &nwritten)
putcharBuffer[putcharPosition] = c
putcharPosition++
if c == '\n' || putcharPosition >= putcharBufferSize {
putcharIOVec.bufLen = putcharPosition
fd_write(stdout, &putcharIOVec, 1, &putcharNWritten)
putcharPosition = 0
}
}
// Abort executes the wasm 'unreachable' instruction.