From e8030a95795cb7e18e50c901f92e1b15a726bbc5 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 20 May 2017 15:13:16 +1200 Subject: [PATCH] cgo: change interface to not return already-collected error interfaces --- main.go | 38 +++++++++++++++++++++----------------- qbolt.h | 10 ++++------ qbolt/boltdb.cpp | 12 ++++++------ qbolt/interop.cpp | 9 --------- qbolt/interop.h | 2 -- 5 files changed, 31 insertions(+), 40 deletions(-) diff --git a/main.go b/main.go index c9a1dfc..1d01aaf 100644 --- a/main.go +++ b/main.go @@ -34,24 +34,24 @@ func Recall(f func(a int64) int64) int64 { } //export Bolt_Open -func Bolt_Open(path string, mode uint32, opts ObjectReference) (ObjectReference, error) { +func Bolt_Open(path string, mode uint32, opts ObjectReference) (ObjectReference, string) { optsIFC, ok := gms.Get(opts) if !ok { - return 0, NullObjectReference + return 0, "" } ptrBoltOps, ok := optsIFC.(*bolt.Options) if !ok { - return 0, NullObjectReference + return 0, "" } ptrDB, err := bolt.Open(path, os.FileMode(mode), ptrBoltOps) if err != nil { - return 0, err + return 0, err.Error() } dbRef := gms.Put(ptrDB) - return dbRef, nil + return dbRef, "" } func withBoltDBReference(b ObjectReference, fn func(db *bolt.DB) error) error { @@ -68,13 +68,8 @@ func withBoltDBReference(b ObjectReference, fn func(db *bolt.DB) error) error { return fn(ptrDB) } -//export Error_Error -func Error_Error(e error) string { - return e.Error() -} - //export Bolt_ListBuckets -func Bolt_ListBuckets(b ObjectReference, browse []string, withEach func(string)) error { +func Bolt_ListBuckets(b ObjectReference, browse []string, withEach func(string)) string { if len(browse) == 0 { return Bolt_ListBucketsAtRoot(b, withEach) } @@ -102,11 +97,16 @@ func Bolt_ListBuckets(b ObjectReference, browse []string, withEach func(string)) }) }) - return err + + if err != nil { + return err.Error() + } + + return "" } //export Bolt_ListBucketsAtRoot -func Bolt_ListBucketsAtRoot(b ObjectReference, withEach func(string)) error { +func Bolt_ListBucketsAtRoot(b ObjectReference, withEach func(string)) string { err := withBoltDBReference(b, func(db *bolt.DB) error { return db.View(func(tx *bolt.Tx) error { @@ -118,21 +118,25 @@ func Bolt_ListBucketsAtRoot(b ObjectReference, withEach func(string)) error { }) }) - return err + + if err != nil { + return err.Error() + } + return "" } //export Bolt_Close -func Bolt_Close(b ObjectReference) error { +func Bolt_Close(b ObjectReference) string { err := withBoltDBReference(b, func(db *bolt.DB) error { return db.Close() }) if err != nil { - return err + return err.Error() } gms.Delete(b) - return nil + return "" } func main() { diff --git a/qbolt.h b/qbolt.h index 8ca0ed9..9a8fdc8 100644 --- a/qbolt.h +++ b/qbolt.h @@ -63,18 +63,16 @@ extern GoInt64 Recall(void* p0); /* Return type for Bolt_Open */ struct Bolt_Open_return { GoInt64 r0; - GoInterface r1; + GoString r1; }; extern struct Bolt_Open_return Bolt_Open(GoString p0, GoUint32 p1, GoInt64 p2); -extern GoString Error_Error(GoInterface p0); +extern GoString Bolt_ListBuckets(GoInt64 p0, GoSlice p1, void* p2); -extern GoInterface Bolt_ListBuckets(GoInt64 p0, GoSlice p1, void* p2); +extern GoString Bolt_ListBucketsAtRoot(GoInt64 p0, void* p1); -extern GoInterface Bolt_ListBucketsAtRoot(GoInt64 p0, void* p1); - -extern GoInterface Bolt_Close(GoInt64 p0); +extern GoString Bolt_Close(GoInt64 p0); #ifdef __cplusplus } diff --git a/qbolt/boltdb.cpp b/qbolt/boltdb.cpp index 1772a70..3bc4a09 100644 --- a/qbolt/boltdb.cpp +++ b/qbolt/boltdb.cpp @@ -13,8 +13,8 @@ BoltDB* BoltDB::createFrom(QString filePath, QString &errorOut) GoString filePathGS = Interop::toGoString_WeakRef(&filePathBytes); auto open_ret = Bolt_Open(filePathGS, 0444, opts); - if (open_ret.r1.v != nullptr) { - errorOut = Interop::GetError(open_ret.r1); + if (open_ret.r1.n != 0) { + errorOut = QString(open_ret.r1.p); return nullptr; } @@ -25,9 +25,9 @@ BoltDB* BoltDB::createFrom(QString filePath, QString &errorOut) BoltDB::~BoltDB() { - auto err = Bolt_Close(this->gmsDbRef); - if (err.v != nullptr) { - // Error closing database: - // errorOut = Interop::GetError(err); + GoString err = Bolt_Close(this->gmsDbRef); + if (err.n != 0) { + // Error closing database! + // Need to display an alert... somewhere } } diff --git a/qbolt/interop.cpp b/qbolt/interop.cpp index 5ecb514..fe8da7c 100644 --- a/qbolt/interop.cpp +++ b/qbolt/interop.cpp @@ -5,15 +5,6 @@ Interop::Interop() } -QString Interop::GetError(GoInterface err) { - if (err.v == nullptr) { - return ""; // no error - } - - GoString gs = Error_Error(err); - return QString::fromUtf8(gs.p, gs.n); -} - GoString Interop::toGoString_WeakRef(QByteArray *qba) { return GoString{qba->data(), qba->length()}; } diff --git a/qbolt/interop.h b/qbolt/interop.h index 6278e01..f637fbc 100644 --- a/qbolt/interop.h +++ b/qbolt/interop.h @@ -9,8 +9,6 @@ class Interop public: Interop(); - static QString GetError(GoInterface err); - static GoString toGoString_WeakRef(QByteArray *qba); static int64_t GetMagic();