Skip to content

Commit 4dd7bd0

Browse files
committed
More type safe.
1 parent 975feb2 commit 4dd7bd0

File tree

8 files changed

+49
-67
lines changed

8 files changed

+49
-67
lines changed

config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c *Conn) Config(op DBConfig, arg ...bool) (bool, error) {
4545

4646
rc := res_t(c.call("sqlite3_db_config", stk_t(c.handle),
4747
stk_t(op), stk_t(argsPtr)))
48-
return util.Read32[uint32](c.mod, argsPtr) != 0, c.error(rc)
48+
return util.ReadBool(c.mod, argsPtr), c.error(rc)
4949
}
5050

5151
// ConfigLog sets up the error logging callback for the connection.
@@ -116,7 +116,7 @@ func (c *Conn) FileControl(schema string, op FcntlOpcode, arg ...any) (any, erro
116116
rc = res_t(c.call("sqlite3_file_control",
117117
stk_t(c.handle), stk_t(schemaPtr),
118118
stk_t(op), stk_t(ptr)))
119-
ret = util.Read32[uint32](c.mod, ptr) != 0
119+
ret = util.ReadBool(c.mod, ptr)
120120

121121
case FCNTL_CHUNK_SIZE:
122122
util.Write32(c.mod, ptr, int32(arg[0].(int)))

conn.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,9 @@ func (c *Conn) TableColumnMetadata(schema, table, column string) (declType, coll
492492
if ptr := util.Read32[ptr_t](c.mod, collSeqPtr); ptr != 0 {
493493
collSeq = util.ReadString(c.mod, ptr, _MAX_NAME)
494494
}
495-
notNull = util.Read32[uint32](c.mod, notNullPtr) != 0
496-
autoInc = util.Read32[uint32](c.mod, autoIncPtr) != 0
497-
primaryKey = util.Read32[uint32](c.mod, primaryKeyPtr) != 0
495+
notNull = util.ReadBool(c.mod, notNullPtr)
496+
autoInc = util.ReadBool(c.mod, autoIncPtr)
497+
primaryKey = util.ReadBool(c.mod, primaryKeyPtr)
498498
}
499499
return
500500
}

internal/util/mem.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ func View(mod api.Module, ptr Ptr_t, size int64) []byte {
2626
if ptr == 0 {
2727
panic(NilErr)
2828
}
29-
if size == 0 {
30-
return nil
31-
}
3229
if uint64(size) > math.MaxUint32 {
3330
panic(RangeErr)
3431
}
@@ -110,6 +107,18 @@ func WriteFloat64(mod api.Module, ptr Ptr_t, v float64) {
110107
Write64(mod, ptr, math.Float64bits(v))
111108
}
112109

110+
func ReadBool(mod api.Module, ptr Ptr_t) bool {
111+
return Read32[int32](mod, ptr) != 0
112+
}
113+
114+
func WriteBool(mod api.Module, ptr Ptr_t, v bool) {
115+
var i int32
116+
if v {
117+
i = 1
118+
}
119+
Write32(mod, ptr, i)
120+
}
121+
113122
func ReadString(mod api.Module, ptr Ptr_t, maxlen int64) string {
114123
if ptr == 0 {
115124
panic(NilErr)

sqlite_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ func Test_sqlite_newBytes(t *testing.T) {
130130
if ptr == 0 {
131131
t.Fatal("got nullptr, want a pointer")
132132
}
133-
134-
if got := util.View(sqlite.mod, ptr, 0); got != nil {
135-
t.Errorf("got %q, want nil", got)
136-
}
137133
}
138134

139135
func Test_sqlite_newString(t *testing.T) {

vfs/lock_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ func Test_vfsLock(t *testing.T) {
4747
if rc != _OK {
4848
t.Fatal("returned", rc)
4949
}
50-
if got := util.Read32[LockLevel](mod, pOutput); got != LOCK_NONE {
50+
if got := util.ReadBool(mod, pOutput); got {
5151
t.Error("file was locked")
5252
}
5353
rc = vfsCheckReservedLock(ctx, mod, pFile2, pOutput)
5454
if rc != _OK {
5555
t.Fatal("returned", rc)
5656
}
57-
if got := util.Read32[LockLevel](mod, pOutput); got != LOCK_NONE {
57+
if got := util.ReadBool(mod, pOutput); got {
5858
t.Error("file was locked")
5959
}
6060
rc = vfsFileControl(ctx, mod, pFile2, _FCNTL_LOCKSTATE, pOutput)
@@ -74,14 +74,14 @@ func Test_vfsLock(t *testing.T) {
7474
if rc != _OK {
7575
t.Fatal("returned", rc)
7676
}
77-
if got := util.Read32[LockLevel](mod, pOutput); got != LOCK_NONE {
77+
if got := util.ReadBool(mod, pOutput); got {
7878
t.Error("file was locked")
7979
}
8080
rc = vfsCheckReservedLock(ctx, mod, pFile2, pOutput)
8181
if rc != _OK {
8282
t.Fatal("returned", rc)
8383
}
84-
if got := util.Read32[LockLevel](mod, pOutput); got != LOCK_NONE {
84+
if got := util.ReadBool(mod, pOutput); got {
8585
t.Error("file was locked")
8686
}
8787
rc = vfsFileControl(ctx, mod, pFile2, _FCNTL_LOCKSTATE, pOutput)
@@ -105,14 +105,14 @@ func Test_vfsLock(t *testing.T) {
105105
if rc != _OK {
106106
t.Fatal("returned", rc)
107107
}
108-
if got := util.Read32[LockLevel](mod, pOutput); got == LOCK_NONE {
108+
if got := util.ReadBool(mod, pOutput); !got {
109109
t.Log("file wasn't locked, locking is incompatible with SQLite")
110110
}
111111
rc = vfsCheckReservedLock(ctx, mod, pFile2, pOutput)
112112
if rc != _OK {
113113
t.Fatal("returned", rc)
114114
}
115-
if got := util.Read32[LockLevel](mod, pOutput); got == LOCK_NONE {
115+
if got := util.ReadBool(mod, pOutput); !got {
116116
t.Error("file wasn't locked")
117117
}
118118
rc = vfsFileControl(ctx, mod, pFile2, _FCNTL_LOCKSTATE, pOutput)
@@ -132,14 +132,14 @@ func Test_vfsLock(t *testing.T) {
132132
if rc != _OK {
133133
t.Fatal("returned", rc)
134134
}
135-
if got := util.Read32[LockLevel](mod, pOutput); got == LOCK_NONE {
135+
if got := util.ReadBool(mod, pOutput); !got {
136136
t.Log("file wasn't locked, locking is incompatible with SQLite")
137137
}
138138
rc = vfsCheckReservedLock(ctx, mod, pFile2, pOutput)
139139
if rc != _OK {
140140
t.Fatal("returned", rc)
141141
}
142-
if got := util.Read32[LockLevel](mod, pOutput); got == LOCK_NONE {
142+
if got := util.ReadBool(mod, pOutput); !got {
143143
t.Error("file wasn't locked")
144144
}
145145
rc = vfsFileControl(ctx, mod, pFile2, _FCNTL_LOCKSTATE, pOutput)
@@ -159,14 +159,14 @@ func Test_vfsLock(t *testing.T) {
159159
if rc != _OK {
160160
t.Fatal("returned", rc)
161161
}
162-
if got := util.Read32[LockLevel](mod, pOutput); got == LOCK_NONE {
162+
if got := util.ReadBool(mod, pOutput); !got {
163163
t.Log("file wasn't locked, locking is incompatible with SQLite")
164164
}
165165
rc = vfsCheckReservedLock(ctx, mod, pFile2, pOutput)
166166
if rc != _OK {
167167
t.Fatal("returned", rc)
168168
}
169-
if got := util.Read32[LockLevel](mod, pOutput); got == LOCK_NONE {
169+
if got := util.ReadBool(mod, pOutput); !got {
170170
t.Error("file wasn't locked")
171171
}
172172
rc = vfsFileControl(ctx, mod, pFile1, _FCNTL_LOCKSTATE, pOutput)
@@ -186,14 +186,14 @@ func Test_vfsLock(t *testing.T) {
186186
if rc != _OK {
187187
t.Fatal("returned", rc)
188188
}
189-
if got := util.Read32[LockLevel](mod, pOutput); got != LOCK_NONE {
189+
if got := util.ReadBool(mod, pOutput); got {
190190
t.Error("file was locked")
191191
}
192192
rc = vfsCheckReservedLock(ctx, mod, pFile2, pOutput)
193193
if rc != _OK {
194194
t.Fatal("returned", rc)
195195
}
196-
if got := util.Read32[LockLevel](mod, pOutput); got != LOCK_NONE {
196+
if got := util.ReadBool(mod, pOutput); got {
197197
t.Error("file was locked")
198198
}
199199

vfs/vfs.go

+14-37
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,8 @@ func vfsFind(ctx context.Context, mod api.Module, zVfsName ptr_t) uint32 {
5858
}
5959

6060
func vfsLocaltime(ctx context.Context, mod api.Module, pTm ptr_t, t int64) _ErrorCode {
61-
tm := time.Unix(t, 0)
62-
var isdst int32
63-
if tm.IsDST() {
64-
isdst = 1
65-
}
66-
6761
const size = 32 / 8
62+
tm := time.Unix(t, 0)
6863
// https://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html
6964
util.Write32(mod, pTm+0*size, int32(tm.Second()))
7065
util.Write32(mod, pTm+1*size, int32(tm.Minute()))
@@ -74,7 +69,7 @@ func vfsLocaltime(ctx context.Context, mod api.Module, pTm ptr_t, t int64) _Erro
7469
util.Write32(mod, pTm+5*size, int32(tm.Year()-1900))
7570
util.Write32(mod, pTm+6*size, int32(tm.Weekday()-time.Sunday))
7671
util.Write32(mod, pTm+7*size, int32(tm.YearDay()-1))
77-
util.Write32(mod, pTm+8*size, isdst)
72+
util.WriteBool(mod, pTm+8*size, tm.IsDST())
7873
return _OK
7974
}
8075

@@ -123,11 +118,7 @@ func vfsAccess(ctx context.Context, mod api.Module, pVfs, zPath ptr_t, flags Acc
123118
path := util.ReadString(mod, zPath, _MAX_PATHNAME)
124119

125120
ok, err := vfs.Access(path, flags)
126-
var res int32
127-
if ok {
128-
res = 1
129-
}
130-
util.Write32(mod, pResOut, res)
121+
util.WriteBool(mod, pResOut, ok)
131122
return vfsErrorCode(err, _IOERR_ACCESS)
132123
}
133124

@@ -151,9 +142,8 @@ func vfsOpen(ctx context.Context, mod api.Module, pVfs, zPath, pFile ptr_t, flag
151142
file.SetPowersafeOverwrite(b)
152143
}
153144
}
154-
if file, ok := file.(FileSharedMemory); ok &&
155-
pOutVFS != 0 && file.SharedMemory() != nil {
156-
util.Write32(mod, pOutVFS, int32(1))
145+
if file, ok := file.(FileSharedMemory); ok && pOutVFS != 0 {
146+
util.WriteBool(mod, pOutVFS, file.SharedMemory() != nil)
157147
}
158148
if pOutFlags != 0 {
159149
util.Write32(mod, pOutFlags, flags)
@@ -225,12 +215,7 @@ func vfsUnlock(ctx context.Context, mod api.Module, pFile ptr_t, eLock LockLevel
225215
func vfsCheckReservedLock(ctx context.Context, mod api.Module, pFile, pResOut ptr_t) _ErrorCode {
226216
file := vfsFileGet(ctx, mod, pFile).(File)
227217
locked, err := file.CheckReservedLock()
228-
229-
var res int32
230-
if locked {
231-
res = 1
232-
}
233-
util.Write32(mod, pResOut, res)
218+
util.WriteBool(mod, pResOut, locked)
234219
return vfsErrorCode(err, _IOERR_CHECKRESERVEDLOCK)
235220
}
236221

@@ -254,24 +239,20 @@ func vfsFileControlImpl(ctx context.Context, mod api.Module, file File, op _Fcnt
254239

255240
case _FCNTL_PERSIST_WAL:
256241
if file, ok := file.(FilePersistWAL); ok {
257-
if i := util.Read32[int32](mod, pArg); i >= 0 {
258-
file.SetPersistWAL(i != 0)
259-
} else if file.PersistWAL() {
260-
util.Write32(mod, pArg, int32(1))
242+
if i := util.Read32[int32](mod, pArg); i < 0 {
243+
util.WriteBool(mod, pArg, file.PersistWAL())
261244
} else {
262-
util.Write32(mod, pArg, int32(0))
245+
file.SetPersistWAL(i != 0)
263246
}
264247
return _OK
265248
}
266249

267250
case _FCNTL_POWERSAFE_OVERWRITE:
268251
if file, ok := file.(FilePowersafeOverwrite); ok {
269-
if i := util.Read32[int32](mod, pArg); i >= 0 {
270-
file.SetPowersafeOverwrite(i != 0)
271-
} else if file.PowersafeOverwrite() {
272-
util.Write32(mod, pArg, int32(1))
252+
if i := util.Read32[int32](mod, pArg); i < 0 {
253+
util.WriteBool(mod, pArg, file.PowersafeOverwrite())
273254
} else {
274-
util.Write32(mod, pArg, int32(0))
255+
file.SetPowersafeOverwrite(i != 0)
275256
}
276257
return _OK
277258
}
@@ -293,11 +274,7 @@ func vfsFileControlImpl(ctx context.Context, mod api.Module, file File, op _Fcnt
293274
case _FCNTL_HAS_MOVED:
294275
if file, ok := file.(FileHasMoved); ok {
295276
moved, err := file.HasMoved()
296-
var val uint32
297-
if moved {
298-
val = 1
299-
}
300-
util.Write32(mod, pArg, val)
277+
util.WriteBool(mod, pArg, moved)
301278
return vfsErrorCode(err, _IOERR_FSTAT)
302279
}
303280

@@ -394,7 +371,7 @@ func vfsFileControlImpl(ctx context.Context, mod api.Module, file File, op _Fcnt
394371
case _FCNTL_LOCK_TIMEOUT:
395372
if file, ok := file.(FileSharedMemory); ok {
396373
if shm, ok := file.SharedMemory().(blockingSharedMemory); ok {
397-
shm.shmEnableBlocking(util.Read32[uint32](mod, pArg) != 0)
374+
shm.shmEnableBlocking(util.ReadBool(mod, pArg))
398375
return _OK
399376
}
400377
}

vfs/vfs_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,15 @@ func Test_vfsAccess(t *testing.T) {
172172
if rc != _OK {
173173
t.Fatal("returned", rc)
174174
}
175-
if got := util.Read32[int32](mod, 4); got != 1 {
175+
if got := util.ReadBool(mod, 4); !got {
176176
t.Error("directory did not exist")
177177
}
178178

179179
rc = vfsAccess(ctx, mod, 0, 8, ACCESS_READWRITE, 4)
180180
if rc != _OK {
181181
t.Fatal("returned", rc)
182182
}
183-
if got := util.Read32[int32](mod, 4); got != 1 {
183+
if got := util.ReadBool(mod, 4); !got {
184184
t.Error("can't access directory")
185185
}
186186

@@ -189,7 +189,7 @@ func Test_vfsAccess(t *testing.T) {
189189
if rc != _OK {
190190
t.Fatal("returned", rc)
191191
}
192-
if got := util.Read32[int32](mod, 4); got != 1 {
192+
if got := util.ReadBool(mod, 4); !got {
193193
t.Error("can't access file")
194194
}
195195

@@ -207,7 +207,7 @@ func Test_vfsAccess(t *testing.T) {
207207
if rc != _OK {
208208
t.Fatal("returned", rc)
209209
}
210-
if got := util.Read32[int32](mod, 4); got != 0 {
210+
if got := util.ReadBool(mod, 4); got {
211211
t.Error("can access file")
212212
}
213213
}

vtab.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,10 @@ func (idx *IndexInfo) save() {
399399
util.Write32(mod, ptr+20, int32(idx.IdxNum))
400400
if idx.IdxStr != "" {
401401
util.Write32(mod, ptr+24, idx.c.newString(idx.IdxStr))
402-
util.Write32(mod, ptr+28, int32(1)) // needToFreeIdxStr
402+
util.WriteBool(mod, ptr+28, true) // needToFreeIdxStr
403403
}
404404
if idx.OrderByConsumed {
405-
util.Write32(mod, ptr+32, int32(1))
405+
util.WriteBool(mod, ptr+32, true)
406406
}
407407
util.WriteFloat64(mod, ptr+40, idx.EstimatedCost)
408408
util.Write64(mod, ptr+48, idx.EstimatedRows)

0 commit comments

Comments
 (0)