Files
sqlite3/mem.go

136 lines
2.2 KiB
Go
Raw Permalink Normal View History

2023-01-26 14:52:38 +00:00
package sqlite3
import (
"bytes"
"math"
"github.com/tetratelabs/wazero/api"
)
type memory struct {
mod api.Module
}
2023-02-27 03:20:23 +00:00
func (m memory) view(ptr uint32, size uint64) []byte {
2023-01-29 02:11:41 +00:00
if ptr == 0 {
2023-01-26 14:52:38 +00:00
panic(nilErr)
}
2023-02-27 03:20:23 +00:00
if size > math.MaxUint32 {
panic(rangeErr)
}
buf, ok := m.mod.Memory().Read(ptr, uint32(size))
2023-01-26 14:52:38 +00:00
if !ok {
panic(rangeErr)
}
return buf
}
func (m memory) readUint8(ptr uint32) uint8 {
if ptr == 0 {
panic(nilErr)
}
v, ok := m.mod.Memory().ReadByte(ptr)
if !ok {
panic(rangeErr)
}
return v
}
func (m memory) writeUint8(ptr uint32, v uint8) {
if ptr == 0 {
panic(nilErr)
}
ok := m.mod.Memory().WriteByte(ptr, v)
if !ok {
panic(rangeErr)
}
}
2023-01-29 02:11:41 +00:00
func (m memory) readUint32(ptr uint32) uint32 {
if ptr == 0 {
2023-01-26 14:52:38 +00:00
panic(nilErr)
}
2023-01-29 02:11:41 +00:00
v, ok := m.mod.Memory().ReadUint32Le(ptr)
2023-01-26 14:52:38 +00:00
if !ok {
panic(rangeErr)
}
return v
}
func (m memory) writeUint32(ptr uint32, v uint32) {
2023-01-29 02:11:41 +00:00
if ptr == 0 {
2023-01-26 14:52:38 +00:00
panic(nilErr)
}
2023-01-29 02:11:41 +00:00
ok := m.mod.Memory().WriteUint32Le(ptr, v)
2023-01-26 14:52:38 +00:00
if !ok {
panic(rangeErr)
}
}
2023-01-29 02:11:41 +00:00
func (m memory) readUint64(ptr uint32) uint64 {
if ptr == 0 {
2023-01-26 14:52:38 +00:00
panic(nilErr)
}
2023-01-29 02:11:41 +00:00
v, ok := m.mod.Memory().ReadUint64Le(ptr)
2023-01-26 14:52:38 +00:00
if !ok {
panic(rangeErr)
}
return v
}
2023-01-29 02:11:41 +00:00
func (m memory) writeUint64(ptr uint32, v uint64) {
if ptr == 0 {
2023-01-26 14:52:38 +00:00
panic(nilErr)
}
2023-01-29 02:11:41 +00:00
ok := m.mod.Memory().WriteUint64Le(ptr, v)
2023-01-26 14:52:38 +00:00
if !ok {
panic(rangeErr)
}
}
2023-01-29 02:11:41 +00:00
func (m memory) readFloat64(ptr uint32) float64 {
return math.Float64frombits(m.readUint64(ptr))
2023-01-26 14:52:38 +00:00
}
2023-01-29 02:11:41 +00:00
func (m memory) writeFloat64(ptr uint32, v float64) {
m.writeUint64(ptr, math.Float64bits(v))
2023-01-26 14:52:38 +00:00
}
func (m memory) readString(ptr, maxlen uint32) string {
2023-01-29 02:11:41 +00:00
if ptr == 0 {
panic(nilErr)
}
2023-01-26 14:52:38 +00:00
switch maxlen {
case 0:
return ""
case math.MaxUint32:
2023-01-29 02:11:41 +00:00
// avoid overflow
2023-01-26 14:52:38 +00:00
default:
maxlen = maxlen + 1
}
2023-01-29 02:11:41 +00:00
mem := m.mod.Memory()
buf, ok := mem.Read(ptr, maxlen)
2023-01-26 14:52:38 +00:00
if !ok {
2023-01-29 02:11:41 +00:00
buf, ok = mem.Read(ptr, mem.Size()-ptr)
if !ok {
panic(rangeErr)
}
2023-01-26 14:52:38 +00:00
}
if i := bytes.IndexByte(buf, 0); i < 0 {
panic(noNulErr)
} else {
return string(buf[:i])
}
}
2023-01-28 12:47:39 +00:00
2023-02-14 11:34:24 +00:00
func (m memory) writeBytes(ptr uint32, b []byte) {
2023-02-27 03:20:23 +00:00
buf := m.view(ptr, uint64(len(b)))
2023-02-14 11:34:24 +00:00
copy(buf, b)
}
2023-01-28 12:47:39 +00:00
func (m memory) writeString(ptr uint32, s string) {
2023-02-27 03:20:23 +00:00
buf := m.view(ptr, uint64(len(s)+1))
2023-01-28 12:47:39 +00:00
buf[len(s)] = 0
copy(buf, s)
}