-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathwrap.go
More file actions
215 lines (162 loc) · 6.49 KB
/
wrap.go
File metadata and controls
215 lines (162 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Package sqlite3 wraps the C SQLite API.
package sqlite3
import (
"context"
"crypto/rand"
"math/bits"
"time"
_ "unsafe"
sqlite3_wasm "github.com/ncruces/go-sqlite3-wasm/v2"
"github.com/ncruces/go-sqlite3/internal/errutil"
"github.com/ncruces/go-sqlite3/internal/sqlite3_wrap"
"github.com/ncruces/go-sqlite3/vfs"
"github.com/ncruces/julianday"
)
type configKey struct{}
// WithMaxMemory returns a derived context that configures
// each SQLite connection not to use more than max amount of memory.
func WithMaxMemory(ctx context.Context, max int64) context.Context {
if max < 0 || max > 65536*65536 {
panic(errutil.OOMErr)
}
return context.WithValue(ctx, configKey{}, max/65536)
}
var _ sqlite3_wasm.Xenv = &env{}
type env struct{ *sqlite3_wrap.Wrapper }
func createWrapper(ctx context.Context) (*sqlite3_wrap.Wrapper, error) {
mem := &sqlite3_wrap.Memory{Max: 4096} // 256MB
if bits.UintSize < 64 {
mem.Max = 512 // 32MB
}
if max, ok := ctx.Value(configKey{}).(int64); ok {
mem.Max = max
}
mem.Grow(5, mem.Max) // 320KB
env := &env{&sqlite3_wrap.Wrapper{Memory: mem}}
env.Module = sqlite3_wasm.New(env)
env.X_initialize()
return env.Wrapper, nil
}
func (e *env) Xmemory() sqlite3_wasm.Memory { return e.Memory }
// VFS functions.
func (e *env) Xgo_randomness(pVfs, nByte, zByte int32) int32 {
mem := e.Bytes(ptr_t(zByte), int64(nByte))
n, _ := rand.Reader.Read(mem)
return int32(n)
}
func (e *env) Xgo_sleep(pVfs, nMicro int32) int32 {
time.Sleep(time.Duration(nMicro) * time.Microsecond)
return _OK
}
func (e *env) Xgo_current_time_64(pVfs, nMicro int32) int32 {
day, nsec := julianday.Date(time.Now())
msec := day*86_400_000 + nsec/1_000_000
e.Write64(ptr_t(nMicro), uint64(msec))
return int32(_OK)
}
func (e *env) Xgo_vfs_find(zVfsName int32) int32 {
if vfs.Find(e.ReadString(ptr_t(zVfsName), _MAX_NAME)) != nil {
return 1
}
return 0
}
//go:linkname vfsFullPathname github.com/ncruces/go-sqlite3/vfs.vfsFullPathname
func vfsFullPathname(_ *sqlite3_wrap.Wrapper, v0, v1, v2, v3 int32) int32
func (e *env) Xgo_full_pathname(v0, v1, v2, v3 int32) int32 {
return vfsFullPathname(e.Wrapper, v0, v1, v2, v3)
}
//go:linkname vfsDelete github.com/ncruces/go-sqlite3/vfs.vfsDelete
func vfsDelete(_ *sqlite3_wrap.Wrapper, v0, v1, v2 int32) int32
func (e *env) Xgo_delete(v0, v1, v2 int32) int32 {
return vfsDelete(e.Wrapper, v0, v1, v2)
}
//go:linkname vfsAccess github.com/ncruces/go-sqlite3/vfs.vfsAccess
func vfsAccess(_ *sqlite3_wrap.Wrapper, v0, v1, v2, v3 int32) int32
func (e *env) Xgo_access(v0, v1, v2, v3 int32) int32 {
return vfsAccess(e.Wrapper, v0, v1, v2, v3)
}
//go:linkname vfsOpen github.com/ncruces/go-sqlite3/vfs.vfsOpen
func vfsOpen(_ *sqlite3_wrap.Wrapper, v0, v1, v2, v3, v4, v5 int32) int32
func (e *env) Xgo_open(v0, v1, v2, v3, v4, v5 int32) int32 {
return vfsOpen(e.Wrapper, v0, v1, v2, v3, v4, v5)
}
//go:linkname vfsClose github.com/ncruces/go-sqlite3/vfs.vfsClose
func vfsClose(_ *sqlite3_wrap.Wrapper, v0 int32) int32
func (e *env) Xgo_close(v0 int32) int32 {
return vfsClose(e.Wrapper, v0)
}
//go:linkname vfsRead github.com/ncruces/go-sqlite3/vfs.vfsRead
func vfsRead(_ *sqlite3_wrap.Wrapper, v0, v1, v2 int32, v3 int64) int32
func (e *env) Xgo_read(v0, v1, v2 int32, v3 int64) int32 {
return vfsRead(e.Wrapper, v0, v1, v2, v3)
}
//go:linkname vfsWrite github.com/ncruces/go-sqlite3/vfs.vfsWrite
func vfsWrite(_ *sqlite3_wrap.Wrapper, v0, v1, v2 int32, v3 int64) int32
func (e *env) Xgo_write(v0, v1, v2 int32, v3 int64) int32 {
return vfsWrite(e.Wrapper, v0, v1, v2, v3)
}
//go:linkname vfsTruncate github.com/ncruces/go-sqlite3/vfs.vfsTruncate
func vfsTruncate(_ *sqlite3_wrap.Wrapper, v0 int32, v1 int64) int32
func (e *env) Xgo_truncate(v0 int32, v1 int64) int32 {
return vfsTruncate(e.Wrapper, v0, v1)
}
//go:linkname vfsSync github.com/ncruces/go-sqlite3/vfs.vfsSync
func vfsSync(_ *sqlite3_wrap.Wrapper, v0, v1 int32) int32
func (e *env) Xgo_sync(v0, v1 int32) int32 {
return vfsSync(e.Wrapper, v0, v1)
}
//go:linkname vfsFileSize github.com/ncruces/go-sqlite3/vfs.vfsFileSize
func vfsFileSize(_ *sqlite3_wrap.Wrapper, v0, v1 int32) int32
func (e *env) Xgo_file_size(v0, v1 int32) int32 {
return vfsFileSize(e.Wrapper, v0, v1)
}
//go:linkname vfsLock github.com/ncruces/go-sqlite3/vfs.vfsLock
func vfsLock(_ *sqlite3_wrap.Wrapper, v0, v1 int32) int32
func (e *env) Xgo_lock(v0 int32, v1 int32) int32 {
return vfsLock(e.Wrapper, v0, v1)
}
//go:linkname vfsUnlock github.com/ncruces/go-sqlite3/vfs.vfsUnlock
func vfsUnlock(_ *sqlite3_wrap.Wrapper, v0, v1 int32) int32
func (e *env) Xgo_unlock(v0, v1 int32) int32 {
return vfsUnlock(e.Wrapper, v0, v1)
}
//go:linkname vfsCheckReservedLock github.com/ncruces/go-sqlite3/vfs.vfsCheckReservedLock
func vfsCheckReservedLock(_ *sqlite3_wrap.Wrapper, v0, v1 int32) int32
func (e *env) Xgo_check_reserved_lock(v0, v1 int32) int32 {
return vfsCheckReservedLock(e.Wrapper, v0, v1)
}
//go:linkname vfsFileControl github.com/ncruces/go-sqlite3/vfs.vfsFileControl
func vfsFileControl(_ *sqlite3_wrap.Wrapper, v0, v1, v2 int32) int32
func (e *env) Xgo_file_control(v0, v1, v2 int32) int32 {
return vfsFileControl(e.Wrapper, v0, v1, v2)
}
//go:linkname vfsSectorSize github.com/ncruces/go-sqlite3/vfs.vfsSectorSize
func vfsSectorSize(_ *sqlite3_wrap.Wrapper, v0 int32) int32
func (e *env) Xgo_sector_size(v0 int32) int32 {
return vfsSectorSize(e.Wrapper, v0)
}
//go:linkname vfsDeviceCharacteristics github.com/ncruces/go-sqlite3/vfs.vfsDeviceCharacteristics
func vfsDeviceCharacteristics(_ *sqlite3_wrap.Wrapper, v0 int32) int32
func (e *env) Xgo_device_characteristics(v0 int32) int32 {
return vfsDeviceCharacteristics(e.Wrapper, v0)
}
//go:linkname vfsShmBarrier github.com/ncruces/go-sqlite3/vfs.vfsShmBarrier
func vfsShmBarrier(_ *sqlite3_wrap.Wrapper, v0 int32)
func (e *env) Xgo_shm_barrier(v0 int32) {
vfsShmBarrier(e.Wrapper, v0)
}
//go:linkname vfsShmMap github.com/ncruces/go-sqlite3/vfs.vfsShmMap
func vfsShmMap(_ *sqlite3_wrap.Wrapper, v0, v1, v2, v3, v4 int32) int32
func (e *env) Xgo_shm_map(v0, v1, v2, v3, v4 int32) int32 {
return vfsShmMap(e.Wrapper, v0, v1, v2, v3, v4)
}
//go:linkname vfsShmLock github.com/ncruces/go-sqlite3/vfs.vfsShmLock
func vfsShmLock(_ *sqlite3_wrap.Wrapper, v0, v1, v2, v3 int32) int32
func (e *env) Xgo_shm_lock(v0, v1, v2, v3 int32) int32 {
return vfsShmLock(e.Wrapper, v0, v1, v2, v3)
}
//go:linkname vfsShmUnmap github.com/ncruces/go-sqlite3/vfs.vfsShmUnmap
func vfsShmUnmap(_ *sqlite3_wrap.Wrapper, v0, v1 int32) int32
func (e *env) Xgo_shm_unmap(v0, v1 int32) int32 {
return vfsShmUnmap(e.Wrapper, v0, v1)
}