From eee71e06aa1bf73793d098ded46633ab87616985 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Thu, 25 May 2023 13:17:44 +0100 Subject: [PATCH] Tweak calling convention. --- .github/FUNDING.yml | 2 +- backup.go | 16 +++++++------- blob.go | 18 +++++++-------- conn.go | 18 +++++++-------- error_test.go | 4 ++-- module.go | 24 ++++++++------------ stmt.go | 54 ++++++++++++++++++++++----------------------- 7 files changed, 65 insertions(+), 71 deletions(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 2955ba5..cb7e48e 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1 +1 @@ -custom: https://www.paypal.com/donate/buttons/manage/33P59ELZWGMK6 \ No newline at end of file +custom: https://www.paypal.com/donate?hosted_button_id=33P59ELZWGMK6 \ No newline at end of file diff --git a/backup.go b/backup.go index 429a0fa..27a71a9 100644 --- a/backup.go +++ b/backup.go @@ -74,16 +74,16 @@ func (c *Conn) backupInit(dst uint32, dstName string, src uint32, srcName string r := c.call(c.api.backupInit, uint64(dst), uint64(dstPtr), uint64(src), uint64(srcPtr)) - if r[0] == 0 { + if r == 0 { defer c.closeDB(other) r = c.call(c.api.errcode, uint64(dst)) - return nil, c.module.error(r[0], dst) + return nil, c.module.error(r, dst) } return &Backup{ c: c, otherc: other, - handle: uint32(r[0]), + handle: uint32(r), }, nil } @@ -100,7 +100,7 @@ func (b *Backup) Close() error { r := b.c.call(b.c.api.backupFinish, uint64(b.handle)) b.c.closeDB(b.otherc) b.handle = 0 - return b.c.error(r[0]) + return b.c.error(r) } // Step copies up to nPage pages between the source and destination databases. @@ -109,10 +109,10 @@ func (b *Backup) Close() error { // https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupstep func (b *Backup) Step(nPage int) (done bool, err error) { r := b.c.call(b.c.api.backupStep, uint64(b.handle), uint64(nPage)) - if r[0] == _DONE { + if r == _DONE { return true, nil } - return false, b.c.error(r[0]) + return false, b.c.error(r) } // Remaining returns the number of pages still to be backed up @@ -121,7 +121,7 @@ func (b *Backup) Step(nPage int) (done bool, err error) { // https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupremaining func (b *Backup) Remaining() int { r := b.c.call(b.c.api.backupRemaining, uint64(b.handle)) - return int(r[0]) + return int(r) } // PageCount returns the total number of pages in the source database @@ -130,5 +130,5 @@ func (b *Backup) Remaining() int { // https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backuppagecount func (b *Backup) PageCount() int { r := b.c.call(b.c.api.backupPageCount, uint64(b.handle)) - return int(r[0]) + return int(r) } diff --git a/blob.go b/blob.go index 3ff9416..a30abf6 100644 --- a/blob.go +++ b/blob.go @@ -45,13 +45,13 @@ func (c *Conn) OpenBlob(db, table, column string, row int64, write bool) (*Blob, uint64(dbPtr), uint64(tablePtr), uint64(columnPtr), uint64(row), flags, uint64(blobPtr)) - if err := c.error(r[0]); err != nil { + if err := c.error(r); err != nil { return nil, err } blob := Blob{c: c} blob.handle = util.ReadUint32(c.mod, blobPtr) - blob.bytes = int64(c.call(c.api.blobBytes, uint64(blob.handle))[0]) + blob.bytes = int64(c.call(c.api.blobBytes, uint64(blob.handle))) return &blob, nil } @@ -68,7 +68,7 @@ func (b *Blob) Close() error { r := b.c.call(b.c.api.blobClose, uint64(b.handle)) b.handle = 0 - return b.c.error(r[0]) + return b.c.error(r) } // Size returns the size of the BLOB in bytes. @@ -97,7 +97,7 @@ func (b *Blob) Read(p []byte) (n int, err error) { r := b.c.call(b.c.api.blobRead, uint64(b.handle), uint64(ptr), uint64(want), uint64(b.offset)) - err = b.c.error(r[0]) + err = b.c.error(r) if err != nil { return 0, err } @@ -130,7 +130,7 @@ func (b *Blob) WriteTo(w io.Writer) (n int64, err error) { for want > 0 { r := b.c.call(b.c.api.blobRead, uint64(b.handle), uint64(ptr), uint64(want), uint64(b.offset)) - err = b.c.error(r[0]) + err = b.c.error(r) if err != nil { return n, err } @@ -163,7 +163,7 @@ func (b *Blob) Write(p []byte) (n int, err error) { r := b.c.call(b.c.api.blobWrite, uint64(b.handle), uint64(ptr), uint64(len(p)), uint64(b.offset)) - err = b.c.error(r[0]) + err = b.c.error(r) if err != nil { return 0, err } @@ -193,7 +193,7 @@ func (b *Blob) ReadFrom(r io.Reader) (n int64, err error) { if m > 0 { r := b.c.call(b.c.api.blobWrite, uint64(b.handle), uint64(ptr), uint64(m), uint64(b.offset)) - err := b.c.error(r[0]) + err := b.c.error(r) if err != nil { return n, err } @@ -240,8 +240,8 @@ func (b *Blob) Seek(offset int64, whence int) (int64, error) { // // https://www.sqlite.org/c3ref/blob_reopen.html func (b *Blob) Reopen(row int64) error { - err := b.c.error(b.c.call(b.c.api.blobReopen, uint64(b.handle), uint64(row))[0]) - b.bytes = int64(b.c.call(b.c.api.blobBytes, uint64(b.handle))[0]) + err := b.c.error(b.c.call(b.c.api.blobReopen, uint64(b.handle), uint64(row))) + b.bytes = int64(b.c.call(b.c.api.blobBytes, uint64(b.handle))) b.offset = 0 return err } diff --git a/conn.go b/conn.go index 2590b30..6be6590 100644 --- a/conn.go +++ b/conn.go @@ -80,7 +80,7 @@ func (c *Conn) openDB(filename string, flags OpenFlag) (uint32, error) { r := c.call(c.api.open, uint64(namePtr), uint64(connPtr), uint64(flags), 0) handle := util.ReadUint32(c.mod, connPtr) - if err := c.module.error(r[0], handle); err != nil { + if err := c.module.error(r, handle); err != nil { c.closeDB(handle) return 0, err } @@ -99,7 +99,7 @@ func (c *Conn) openDB(filename string, flags OpenFlag) (uint32, error) { c.arena.reset() pragmaPtr := c.arena.string(pragmas.String()) r := c.call(c.api.exec, uint64(handle), uint64(pragmaPtr), 0, 0, 0) - if err := c.module.error(r[0], handle, pragmas.String()); err != nil { + if err := c.module.error(r, handle, pragmas.String()); err != nil { if errors.Is(err, ERROR) { err = fmt.Errorf("sqlite3: invalid _pragma: %w", err) } @@ -113,7 +113,7 @@ func (c *Conn) openDB(filename string, flags OpenFlag) (uint32, error) { func (c *Conn) closeDB(handle uint32) { r := c.call(c.api.closeZombie, uint64(handle)) - if err := c.module.error(r[0], handle); err != nil { + if err := c.module.error(r, handle); err != nil { panic(err) } } @@ -137,7 +137,7 @@ func (c *Conn) Close() error { c.pending = nil r := c.call(c.api.close, uint64(c.handle)) - if err := c.error(r[0]); err != nil { + if err := c.error(r); err != nil { return err } @@ -156,7 +156,7 @@ func (c *Conn) Exec(sql string) error { sqlPtr := c.arena.string(sql) r := c.call(c.api.exec, uint64(c.handle), uint64(sqlPtr), 0, 0, 0) - return c.error(r[0]) + return c.error(r) } // Prepare calls [Conn.PrepareFlags] with no flags. @@ -189,7 +189,7 @@ func (c *Conn) PrepareFlags(sql string, flags PrepareFlag) (stmt *Stmt, tail str i := util.ReadUint32(c.mod, tailPtr) tail = sql[i-sqlPtr:] - if err := c.error(r[0], sql); err != nil { + if err := c.error(r, sql); err != nil { return nil, "", err } if stmt.handle == 0 { @@ -203,7 +203,7 @@ func (c *Conn) PrepareFlags(sql string, flags PrepareFlag) (stmt *Stmt, tail str // https://www.sqlite.org/c3ref/get_autocommit.html func (c *Conn) GetAutocommit() bool { r := c.call(c.api.autocommit, uint64(c.handle)) - return r[0] != 0 + return r != 0 } // LastInsertRowID returns the rowid of the most recent successful INSERT @@ -212,7 +212,7 @@ func (c *Conn) GetAutocommit() bool { // https://www.sqlite.org/c3ref/last_insert_rowid.html func (c *Conn) LastInsertRowID() int64 { r := c.call(c.api.lastRowid, uint64(c.handle)) - return int64(r[0]) + return int64(r) } // Changes returns the number of rows modified, inserted or deleted @@ -222,7 +222,7 @@ func (c *Conn) LastInsertRowID() int64 { // https://www.sqlite.org/c3ref/changes.html func (c *Conn) Changes() int64 { r := c.call(c.api.changes, uint64(c.handle)) - return int64(r[0]) + return int64(r) } // SetInterrupt interrupts a long-running query when a context is done. diff --git a/error_test.go b/error_test.go index b56fb38..4cfe7a6 100644 --- a/error_test.go +++ b/error_test.go @@ -122,7 +122,7 @@ func Test_ErrorCode_Error(t *testing.T) { for i := 0; i == int(ErrorCode(i)); i++ { want := "sqlite3: " r := db.call(db.api.errstr, uint64(i)) - want += util.ReadString(db.mod, uint32(r[0]), _MAX_STRING) + want += util.ReadString(db.mod, uint32(r), _MAX_STRING) got := ErrorCode(i).Error() if got != want { @@ -144,7 +144,7 @@ func Test_ExtendedErrorCode_Error(t *testing.T) { for i := 0; i == int(ExtendedErrorCode(i)); i++ { want := "sqlite3: " r := db.call(db.api.errstr, uint64(i)) - want += util.ReadString(db.mod, uint32(r[0]), _MAX_STRING) + want += util.ReadString(db.mod, uint32(r), _MAX_STRING) got := ExtendedErrorCode(i).Error() if got != want { diff --git a/module.go b/module.go index 0aadf67..5871247 100644 --- a/module.go +++ b/module.go @@ -177,22 +177,17 @@ func (m *module) error(rc uint64, handle uint32, sql ...string) error { panic(util.OOMErr) } - var r []uint64 - - r = m.call(m.api.errstr, rc) - if r != nil { - err.str = util.ReadString(m.mod, uint32(r[0]), _MAX_STRING) + if r := m.call(m.api.errstr, rc); r != 0 { + err.str = util.ReadString(m.mod, uint32(r), _MAX_STRING) } - r = m.call(m.api.errmsg, uint64(handle)) - if r != nil { - err.msg = util.ReadString(m.mod, uint32(r[0]), _MAX_STRING) + if r := m.call(m.api.errmsg, uint64(handle)); r != 0 { + err.msg = util.ReadString(m.mod, uint32(r), _MAX_STRING) } if sql != nil { - r = m.call(m.api.erroff, uint64(handle)) - if r != nil && r[0] != math.MaxUint32 { - err.sql = sql[0][r[0]:] + if r := m.call(m.api.erroff, uint64(handle)); r != math.MaxUint32 { + err.sql = sql[0][r:] } } @@ -203,7 +198,7 @@ func (m *module) error(rc uint64, handle uint32, sql ...string) error { return &err } -func (m *module) call(fn api.Function, params ...uint64) []uint64 { +func (m *module) call(fn api.Function, params ...uint64) uint64 { copy(m.arg[:], params) err := fn.CallWithStack(m.ctx, m.arg[:]) if err != nil { @@ -211,7 +206,7 @@ func (m *module) call(fn api.Function, params ...uint64) []uint64 { m.vfs.Close() panic(err) } - return m.arg[:] + return m.arg[0] } func (m *module) free(ptr uint32) { @@ -225,8 +220,7 @@ func (m *module) new(size uint64) uint32 { if size > _MAX_ALLOCATION_SIZE { panic(util.OOMErr) } - r := m.call(m.api.malloc, size) - ptr := uint32(r[0]) + ptr := uint32(m.call(m.api.malloc, size)) if ptr == 0 && size != 0 { panic(util.OOMErr) } diff --git a/stmt.go b/stmt.go index 9121d86..2fae0b4 100644 --- a/stmt.go +++ b/stmt.go @@ -29,7 +29,7 @@ func (s *Stmt) Close() error { r := s.c.call(s.c.api.finalize, uint64(s.handle)) s.handle = 0 - return s.c.error(r[0]) + return s.c.error(r) } // Reset resets the prepared statement object. @@ -38,7 +38,7 @@ func (s *Stmt) Close() error { func (s *Stmt) Reset() error { r := s.c.call(s.c.api.reset, uint64(s.handle)) s.err = nil - return s.c.error(r[0]) + return s.c.error(r) } // ClearBindings resets all bindings on the prepared statement. @@ -46,7 +46,7 @@ func (s *Stmt) Reset() error { // https://www.sqlite.org/c3ref/clear_bindings.html func (s *Stmt) ClearBindings() error { r := s.c.call(s.c.api.clearBindings, uint64(s.handle)) - return s.c.error(r[0]) + return s.c.error(r) } // Step evaluates the SQL statement. @@ -61,13 +61,13 @@ func (s *Stmt) ClearBindings() error { func (s *Stmt) Step() bool { s.c.checkInterrupt() r := s.c.call(s.c.api.step, uint64(s.handle)) - if r[0] == _ROW { + if r == _ROW { return true } - if r[0] == _DONE { + if r == _DONE { s.err = nil } else { - s.err = s.c.error(r[0]) + s.err = s.c.error(r) } return false } @@ -94,7 +94,7 @@ func (s *Stmt) Exec() error { func (s *Stmt) BindCount() int { r := s.c.call(s.c.api.bindCount, uint64(s.handle)) - return int(r[0]) + return int(r) } // BindIndex returns the index of a parameter in the prepared statement @@ -106,7 +106,7 @@ func (s *Stmt) BindIndex(name string) int { namePtr := s.c.arena.string(name) r := s.c.call(s.c.api.bindIndex, uint64(s.handle), uint64(namePtr)) - return int(r[0]) + return int(r) } // BindName returns the name of a parameter in the prepared statement. @@ -117,7 +117,7 @@ func (s *Stmt) BindName(param int) string { r := s.c.call(s.c.api.bindName, uint64(s.handle), uint64(param)) - ptr := uint32(r[0]) + ptr := uint32(r) if ptr == 0 { return "" } @@ -152,7 +152,7 @@ func (s *Stmt) BindInt(param int, value int) error { func (s *Stmt) BindInt64(param int, value int64) error { r := s.c.call(s.c.api.bindInteger, uint64(s.handle), uint64(param), uint64(value)) - return s.c.error(r[0]) + return s.c.error(r) } // BindFloat binds a float64 to the prepared statement. @@ -162,7 +162,7 @@ func (s *Stmt) BindInt64(param int, value int64) error { func (s *Stmt) BindFloat(param int, value float64) error { r := s.c.call(s.c.api.bindFloat, uint64(s.handle), uint64(param), math.Float64bits(value)) - return s.c.error(r[0]) + return s.c.error(r) } // BindText binds a string to the prepared statement. @@ -175,7 +175,7 @@ func (s *Stmt) BindText(param int, value string) error { uint64(s.handle), uint64(param), uint64(ptr), uint64(len(value)), uint64(s.c.api.destructor), _UTF8) - return s.c.error(r[0]) + return s.c.error(r) } // BindBlob binds a []byte to the prepared statement. @@ -189,7 +189,7 @@ func (s *Stmt) BindBlob(param int, value []byte) error { uint64(s.handle), uint64(param), uint64(ptr), uint64(len(value)), uint64(s.c.api.destructor)) - return s.c.error(r[0]) + return s.c.error(r) } // BindZeroBlob binds a zero-filled, length n BLOB to the prepared statement. @@ -199,7 +199,7 @@ func (s *Stmt) BindBlob(param int, value []byte) error { func (s *Stmt) BindZeroBlob(param int, n int64) error { r := s.c.call(s.c.api.bindZeroBlob, uint64(s.handle), uint64(param), uint64(n)) - return s.c.error(r[0]) + return s.c.error(r) } // BindNull binds a NULL to the prepared statement. @@ -209,7 +209,7 @@ func (s *Stmt) BindZeroBlob(param int, n int64) error { func (s *Stmt) BindNull(param int) error { r := s.c.call(s.c.api.bindNull, uint64(s.handle), uint64(param)) - return s.c.error(r[0]) + return s.c.error(r) } // BindTime binds a [time.Time] to the prepared statement. @@ -244,7 +244,7 @@ func (s *Stmt) bindRFC3339Nano(param int, value time.Time) error { uint64(s.handle), uint64(param), uint64(ptr), uint64(len(buf)), uint64(s.c.api.destructor), _UTF8) - return s.c.error(r[0]) + return s.c.error(r) } // ColumnCount returns the number of columns in a result set. @@ -253,7 +253,7 @@ func (s *Stmt) bindRFC3339Nano(param int, value time.Time) error { func (s *Stmt) ColumnCount() int { r := s.c.call(s.c.api.columnCount, uint64(s.handle)) - return int(r[0]) + return int(r) } // ColumnName returns the name of the result column. @@ -264,7 +264,7 @@ func (s *Stmt) ColumnName(col int) string { r := s.c.call(s.c.api.columnName, uint64(s.handle), uint64(col)) - ptr := uint32(r[0]) + ptr := uint32(r) if ptr == 0 { panic(util.OOMErr) } @@ -278,7 +278,7 @@ func (s *Stmt) ColumnName(col int) string { func (s *Stmt) ColumnType(col int) Datatype { r := s.c.call(s.c.api.columnType, uint64(s.handle), uint64(col)) - return Datatype(r[0]) + return Datatype(r) } // ColumnBool returns the value of the result column as a bool. @@ -310,7 +310,7 @@ func (s *Stmt) ColumnInt(col int) int { func (s *Stmt) ColumnInt64(col int) int64 { r := s.c.call(s.c.api.columnInteger, uint64(s.handle), uint64(col)) - return int64(r[0]) + return int64(r) } // ColumnFloat returns the value of the result column as a float64. @@ -320,7 +320,7 @@ func (s *Stmt) ColumnInt64(col int) int64 { func (s *Stmt) ColumnFloat(col int) float64 { r := s.c.call(s.c.api.columnFloat, uint64(s.handle), uint64(col)) - return math.Float64frombits(r[0]) + return math.Float64frombits(r) } // ColumnTime returns the value of the result column as a [time.Time]. @@ -375,17 +375,17 @@ func (s *Stmt) ColumnRawText(col int) []byte { r := s.c.call(s.c.api.columnText, uint64(s.handle), uint64(col)) - ptr := uint32(r[0]) + ptr := uint32(r) if ptr == 0 { r = s.c.call(s.c.api.errcode, uint64(s.c.handle)) - s.err = s.c.error(r[0]) + s.err = s.c.error(r) return nil } r = s.c.call(s.c.api.columnBytes, uint64(s.handle), uint64(col)) - return util.View(s.c.mod, ptr, r[0]) + return util.View(s.c.mod, ptr, r) } // ColumnRawBlob returns the value of the result column as a []byte. @@ -398,17 +398,17 @@ func (s *Stmt) ColumnRawBlob(col int) []byte { r := s.c.call(s.c.api.columnBlob, uint64(s.handle), uint64(col)) - ptr := uint32(r[0]) + ptr := uint32(r) if ptr == 0 { r = s.c.call(s.c.api.errcode, uint64(s.c.handle)) - s.err = s.c.error(r[0]) + s.err = s.c.error(r) return nil } r = s.c.call(s.c.api.columnBytes, uint64(s.handle), uint64(col)) - return util.View(s.c.mod, ptr, r[0]) + return util.View(s.c.mod, ptr, r) } // Return true if stmt is an empty SQL statement.