forked from ncruces/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap.go
More file actions
381 lines (310 loc) · 10.6 KB
/
wrap.go
File metadata and controls
381 lines (310 loc) · 10.6 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Package sqlite3 wraps the C SQLite API.
package sqlite3
import (
"bytes"
"context"
"crypto/rand"
"math"
"math/bits"
"time"
_ "unsafe"
sqlite3_wasm "github.com/ncruces/go-sqlite3-wasm"
"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{}, int32(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{}).(int32); ok {
mem.Max = max
}
env := &env{&sqlite3_wrap.Wrapper{Memory: mem}}
sqlite3_wasm.New(mem, env)
env.X_initialize()
return env.Wrapper, nil
}
func (e *env) Init(m *sqlite3_wasm.Module) { e.Module = m }
// Math functions.
func (*env) Xacos(x float64) float64 { return math.Acos(x) }
func (*env) Xacosh(x float64) float64 { return math.Acosh(x) }
func (*env) Xasin(x float64) float64 { return math.Asin(x) }
func (*env) Xasinh(x float64) float64 { return math.Asinh(x) }
func (*env) Xatan(x float64) float64 { return math.Atan(x) }
func (*env) Xatan2(y, x float64) float64 { return math.Atan2(y, x) }
func (*env) Xatanh(x float64) float64 { return math.Atanh(x) }
func (*env) Xcos(x float64) float64 { return math.Cos(x) }
func (*env) Xcosh(x float64) float64 { return math.Cosh(x) }
func (*env) Xexp(x float64) float64 { return math.Exp(x) }
func (*env) Xfmod(x, y float64) float64 { return math.Mod(x, y) }
func (*env) Xlog(x float64) float64 { return math.Log(x) }
func (*env) Xlog10(x float64) float64 { return math.Log10(x) }
func (*env) Xlog2(x float64) float64 { return math.Log2(x) }
func (*env) Xpow(x, y float64) float64 { return math.Pow(x, y) }
func (*env) Xsin(x float64) float64 { return math.Sin(x) }
func (*env) Xsinh(x float64) float64 { return math.Sinh(x) }
func (*env) Xtan(x float64) float64 { return math.Tan(x) }
func (*env) Xtanh(x float64) float64 { return math.Tanh(x) }
// String functions.
func (e *env) Xmemchr(s, c, n int32) int32 {
m := e.Buf[s:]
if len(m) > int(n) {
m = m[:n]
}
if i := bytes.IndexByte(m, byte(c)); i >= 0 {
return s + int32(i)
}
return 0
}
func (e *env) Xmemcmp(s1, s2, n int32) int32 {
e1, e2 := s1+n, s2+n
m1 := e.Buf[s1:e1]
m2 := e.Buf[s2:e2]
return int32(bytes.Compare(m1, m2))
}
func (e *env) Xstrlen(s int32) int32 {
return int32(bytes.IndexByte(e.Buf[s:], 0))
}
func (e *env) Xstrchr(s, c int32) int32 {
s = e.Xstrchrnul(s, c)
if e.Buf[s] == byte(c) {
return s
}
return 0
}
func (e *env) Xstrchrnul(s, c int32) int32 {
m := e.Buf[s:]
m = m[:bytes.IndexByte(m, 0)]
b := byte(c)
l := len(m)
if c != 0 {
i := bytes.IndexByte(m, b)
if i >= 0 {
l = i
}
}
return s + int32(l)
}
func (e *env) Xstrrchr(s, c int32) int32 {
m := e.Buf[s:]
m = m[:bytes.IndexByte(m, 0)+1]
if i := bytes.LastIndexByte(m, byte(c)); i >= 0 {
return s + int32(i)
}
return 0
}
func (e *env) Xstrcmp(s1, s2 int32) int32 {
m1 := e.Buf[s1:]
m2 := e.Buf[s2:]
m1 = m1[:bytes.IndexByte(m1, 0)]
m2 = m2[:bytes.IndexByte(m2, 0)]
return int32(bytes.Compare(m1, m2))
}
func (e *env) Xstrncmp(s1, s2, n int32) int32 {
m1 := e.Buf[s1:]
m2 := e.Buf[s2:]
m1 = m1[:bytes.IndexByte(m1, 0)]
m2 = m2[:bytes.IndexByte(m2, 0)]
if len(m1) > int(n) {
m1 = m1[:n]
}
if len(m2) > int(n) {
m2 = m2[:n]
}
return int32(bytes.Compare(m1, m2))
}
func (e *env) Xstrspn(s, accept int32) int32 {
m := e.Buf[s:]
a := e.Buf[accept:]
a = a[:bytes.IndexByte(a, 0)]
i := int32(0)
for _, b := range m {
if bytes.IndexByte(a, b) == -1 {
break
}
i++
}
return i
}
func (e *env) Xstrcspn(s, reject int32) int32 {
m := e.Buf[s:]
r := e.Buf[reject:]
r = r[:bytes.IndexByte(r, 0)]
i := int32(0)
for _, b := range m {
if b == 0 || bytes.IndexByte(r, b) != -1 {
break
}
i++
}
return i
}
func (e *env) Xstrstr(haystack, needle int32) int32 {
h := e.Buf[haystack:]
n := e.Buf[needle:]
h = h[:bytes.IndexByte(h, 0)]
n = n[:bytes.IndexByte(n, 0)]
i := bytes.Index(h, n)
if i < 0 {
return 0
}
return haystack + int32(i)
}
func (e *env) Xstrcpy(d, s int32) int32 {
m := e.Buf[s:]
m = m[:bytes.IndexByte(m, 0)+1]
copy(e.Buf[d:], m)
return d
}
// VFS functions.
func (e *env) Xgo_randomness(pVfs, nByte, zByte int32) int32 {
mem := e.Slice(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_localtime(pTm int32, t int64) int32 {
const size = 32 / 8
mem := e.Memory
tm := time.Unix(t, 0)
// https://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html
mem.Write32(ptr_t(pTm+0*size), uint32(tm.Second()))
mem.Write32(ptr_t(pTm+1*size), uint32(tm.Minute()))
mem.Write32(ptr_t(pTm+2*size), uint32(tm.Hour()))
mem.Write32(ptr_t(pTm+3*size), uint32(tm.Day()))
mem.Write32(ptr_t(pTm+4*size), uint32(tm.Month()-time.January))
mem.Write32(ptr_t(pTm+5*size), uint32(tm.Year()-1900))
mem.Write32(ptr_t(pTm+6*size), uint32(tm.Weekday()-time.Sunday))
mem.Write32(ptr_t(pTm+7*size), uint32(tm.YearDay()-1))
mem.WriteBool(ptr_t(pTm+8*size), tm.IsDST())
return _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)
}