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-01-29 02:11:41 +00:00
|
|
|
func (m memory) view(ptr, size uint32) []byte {
|
|
|
|
|
if ptr == 0 {
|
2023-01-26 14:52:38 +00:00
|
|
|
panic(nilErr)
|
|
|
|
|
}
|
2023-01-29 02:11:41 +00:00
|
|
|
buf, ok := m.mod.Memory().Read(ptr, size)
|
2023-01-26 14:52:38 +00:00
|
|
|
if !ok {
|
|
|
|
|
panic(rangeErr)
|
|
|
|
|
}
|
|
|
|
|
return buf
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-29 02:11:41 +00:00
|
|
|
func (m memory) writeUint32(ptr, v uint32) {
|
|
|
|
|
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) {
|
|
|
|
|
buf := m.view(ptr, uint32(len(b)))
|
|
|
|
|
copy(buf, b)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 12:47:39 +00:00
|
|
|
func (m memory) writeString(ptr uint32, s string) {
|
2023-02-14 11:34:24 +00:00
|
|
|
buf := m.view(ptr, uint32(len(s)+1))
|
2023-01-28 12:47:39 +00:00
|
|
|
buf[len(s)] = 0
|
|
|
|
|
copy(buf, s)
|
|
|
|
|
}
|