wasm: use the fixed length buffer for putchar
Signed-off-by: mathetake <takeshi@tetrate.io>
Этот коммит содержится в:
родитель
1dec9dcbc4
коммит
ffeff55706
1 изменённых файлов: 18 добавлений и 9 удалений
|
@ -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.
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче