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() {} func postinit() {}
const (
putcharBufferSize = 120
stdout = 1
)
// Using global variables to avoid heap allocation. // Using global variables to avoid heap allocation.
var ( var (
putcharBuf = byte(0) putcharBuffer = [putcharBufferSize]byte{}
putcharIOVec = __wasi_iovec_t{ putcharPosition uint = 0
buf: unsafe.Pointer(&putcharBuf), putcharIOVec = __wasi_iovec_t{
bufLen: 1, buf: unsafe.Pointer(&putcharBuffer[0]),
} }
putcharNWritten uint
) )
func putchar(c byte) { func putchar(c byte) {
// write to stdout putcharBuffer[putcharPosition] = c
const stdout = 1 putcharPosition++
var nwritten uint
putcharBuf = c if c == '\n' || putcharPosition >= putcharBufferSize {
fd_write(stdout, &putcharIOVec, 1, &nwritten) putcharIOVec.bufLen = putcharPosition
fd_write(stdout, &putcharIOVec, 1, &putcharNWritten)
putcharPosition = 0
}
} }
// Abort executes the wasm 'unreachable' instruction. // Abort executes the wasm 'unreachable' instruction.