summaryrefslogtreecommitdiff
path: root/vendor/github.com/ncruces/go-sqlite3/internal/util/mem.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2024-05-27 15:46:15 +0000
committerLibravatar GitHub <noreply@github.com>2024-05-27 17:46:15 +0200
commit1e7b32490dfdccddd04f46d4b0416b48d749d51b (patch)
tree62a11365933a5a11e0800af64cbdf9172e5e6e7a /vendor/github.com/ncruces/go-sqlite3/internal/util/mem.go
parent[chore] Small styling + link issues (#2933) (diff)
downloadgotosocial-1e7b32490dfdccddd04f46d4b0416b48d749d51b.tar.xz
[experiment] add alternative wasm sqlite3 implementation available via build-tag (#2863)
This allows for building GoToSocial with [SQLite transpiled to WASM](https://github.com/ncruces/go-sqlite3) and accessed through [Wazero](https://wazero.io/).
Diffstat (limited to 'vendor/github.com/ncruces/go-sqlite3/internal/util/mem.go')
-rw-r--r--vendor/github.com/ncruces/go-sqlite3/internal/util/mem.go134
1 files changed, 134 insertions, 0 deletions
diff --git a/vendor/github.com/ncruces/go-sqlite3/internal/util/mem.go b/vendor/github.com/ncruces/go-sqlite3/internal/util/mem.go
new file mode 100644
index 000000000..a09523fd1
--- /dev/null
+++ b/vendor/github.com/ncruces/go-sqlite3/internal/util/mem.go
@@ -0,0 +1,134 @@
+package util
+
+import (
+ "bytes"
+ "math"
+
+ "github.com/tetratelabs/wazero/api"
+)
+
+func View(mod api.Module, ptr uint32, size uint64) []byte {
+ if ptr == 0 {
+ panic(NilErr)
+ }
+ if size > math.MaxUint32 {
+ panic(RangeErr)
+ }
+ if size == 0 {
+ return nil
+ }
+ buf, ok := mod.Memory().Read(ptr, uint32(size))
+ if !ok {
+ panic(RangeErr)
+ }
+ return buf
+}
+
+func ReadUint8(mod api.Module, ptr uint32) uint8 {
+ if ptr == 0 {
+ panic(NilErr)
+ }
+ v, ok := mod.Memory().ReadByte(ptr)
+ if !ok {
+ panic(RangeErr)
+ }
+ return v
+}
+
+func ReadUint32(mod api.Module, ptr uint32) uint32 {
+ if ptr == 0 {
+ panic(NilErr)
+ }
+ v, ok := mod.Memory().ReadUint32Le(ptr)
+ if !ok {
+ panic(RangeErr)
+ }
+ return v
+}
+
+func WriteUint8(mod api.Module, ptr uint32, v uint8) {
+ if ptr == 0 {
+ panic(NilErr)
+ }
+ ok := mod.Memory().WriteByte(ptr, v)
+ if !ok {
+ panic(RangeErr)
+ }
+}
+
+func WriteUint32(mod api.Module, ptr uint32, v uint32) {
+ if ptr == 0 {
+ panic(NilErr)
+ }
+ ok := mod.Memory().WriteUint32Le(ptr, v)
+ if !ok {
+ panic(RangeErr)
+ }
+}
+
+func ReadUint64(mod api.Module, ptr uint32) uint64 {
+ if ptr == 0 {
+ panic(NilErr)
+ }
+ v, ok := mod.Memory().ReadUint64Le(ptr)
+ if !ok {
+ panic(RangeErr)
+ }
+ return v
+}
+
+func WriteUint64(mod api.Module, ptr uint32, v uint64) {
+ if ptr == 0 {
+ panic(NilErr)
+ }
+ ok := mod.Memory().WriteUint64Le(ptr, v)
+ if !ok {
+ panic(RangeErr)
+ }
+}
+
+func ReadFloat64(mod api.Module, ptr uint32) float64 {
+ return math.Float64frombits(ReadUint64(mod, ptr))
+}
+
+func WriteFloat64(mod api.Module, ptr uint32, v float64) {
+ WriteUint64(mod, ptr, math.Float64bits(v))
+}
+
+func ReadString(mod api.Module, ptr, maxlen uint32) string {
+ if ptr == 0 {
+ panic(NilErr)
+ }
+ switch maxlen {
+ case 0:
+ return ""
+ case math.MaxUint32:
+ // avoid overflow
+ default:
+ maxlen = maxlen + 1
+ }
+ mem := mod.Memory()
+ buf, ok := mem.Read(ptr, maxlen)
+ if !ok {
+ buf, ok = mem.Read(ptr, mem.Size()-ptr)
+ if !ok {
+ panic(RangeErr)
+ }
+ }
+ if i := bytes.IndexByte(buf, 0); i < 0 {
+ panic(NoNulErr)
+ } else {
+ return string(buf[:i])
+ }
+}
+
+func WriteBytes(mod api.Module, ptr uint32, b []byte) {
+ buf := View(mod, ptr, uint64(len(b)))
+ copy(buf, b)
+}
+
+func WriteString(mod api.Module, ptr uint32, s string) {
+ buf := View(mod, ptr, uint64(len(s)+1))
+ buf[len(s)] = 0
+ copy(buf, s)
+}