Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove forward declarations to cgo exported funcs #146

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ https://godoc.org/crawshaw.io/sqlite

## Platform specific considerations

By default it requires some pthreads DLL on Windows. To avoid it, supply `CGOLDFLAGS="-static"` when building your application.
By default it requires some pthreads DLL on Windows. To avoid it, supply `CGO_LDFLAGS="-static"` when building your application.
6 changes: 2 additions & 4 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package sqlite

// #include <stdint.h>
// #include <sqlite3.h>
// extern int go_sqlite_auth_tramp(uintptr_t, int, char*, char*, char*, char*);
// static int c_auth_tramp(void *userData, int action, const char* arg1, const char* arg2, const char* db, const char* trigger) {
// return go_sqlite_auth_tramp((uintptr_t)userData, action, (char*)arg1, (char*)arg2, (char*)db, (char*)trigger);
// }
// #include "wrappers.h"
//
// static int sqlite3_go_set_authorizer(sqlite3* conn, uintptr_t id) {
// return sqlite3_set_authorizer(conn, c_auth_tramp, (void*)id);
// }
Expand Down
22 changes: 9 additions & 13 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ package sqlite
// #include <sqlite3.h>
// #include "wrappers.h"
//
// extern void func_tramp(sqlite3_context*, int, sqlite3_value**);
// extern void step_tramp(sqlite3_context*, int, sqlite3_value**);
// extern void final_tramp(sqlite3_context*);
//
// static int go_sqlite3_create_function_v2(
// sqlite3 *db,
// const char *zFunctionName,
Expand Down Expand Up @@ -167,10 +163,10 @@ func (conn *Conn) CreateFunction(name string, deterministic bool, numArgs int, x

var funcfn, stepfn, finalfn *[0]byte
if xFunc == nil {
stepfn = (*[0]byte)(C.step_tramp)
finalfn = (*[0]byte)(C.final_tramp)
stepfn = (*[0]byte)(C.c_step_tramp)
finalfn = (*[0]byte)(C.c_final_tramp)
} else {
funcfn = (*[0]byte)(C.func_tramp)
funcfn = (*[0]byte)(C.c_func_tramp)
}

res := C.go_sqlite3_create_function_v2(
Expand All @@ -197,8 +193,8 @@ func getxfuncs(ctx *C.sqlite3_context) *xfunc {
return x
}

//export func_tramp
func func_tramp(ctx *C.sqlite3_context, n C.int, valarray **C.sqlite3_value) {
//export go_func_tramp
func go_func_tramp(ctx *C.sqlite3_context, n C.int, valarray **C.sqlite3_value) {
x := getxfuncs(ctx)
var vals []Value
if n > 0 {
Expand All @@ -207,8 +203,8 @@ func func_tramp(ctx *C.sqlite3_context, n C.int, valarray **C.sqlite3_value) {
x.xFunc(Context{ptr: ctx}, vals...)
}

//export step_tramp
func step_tramp(ctx *C.sqlite3_context, n C.int, valarray **C.sqlite3_value) {
//export go_step_tramp
func go_step_tramp(ctx *C.sqlite3_context, n C.int, valarray **C.sqlite3_value) {
x := getxfuncs(ctx)
var vals []Value
if n > 0 {
Expand All @@ -217,8 +213,8 @@ func step_tramp(ctx *C.sqlite3_context, n C.int, valarray **C.sqlite3_value) {
x.xStep(Context{ptr: ctx}, vals...)
}

//export final_tramp
func final_tramp(ctx *C.sqlite3_context) {
//export go_final_tramp
func go_final_tramp(ctx *C.sqlite3_context) {
x := getxfuncs(ctx)
x.xFinal(Context{ptr: ctx})
}
Expand Down
3 changes: 0 additions & 3 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ package sqlite
// #include <sqlite3.h>
// #include "wrappers.h"
//
// extern int go_strm_w_tramp(uintptr_t, char*, int);
// extern int go_strm_r_tramp(uintptr_t, char*, int*);
//
// static int go_sqlite3session_changeset_strm(
// sqlite3_session *pSession,
// int (*xOutput)(void *pOut, const void *pData, int nData),
Expand Down
8 changes: 3 additions & 5 deletions sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ package sqlite
// return sqlite3_bind_blob(stmt, col, p, n, SQLITE_TRANSIENT);
// }
//
// extern void log_fn(void* pArg, int code, char* msg);
// static void enable_logging() {
// sqlite3_config(SQLITE_CONFIG_LOG, log_fn, NULL);
// sqlite3_config(SQLITE_CONFIG_LOG, c_log_fn, NULL);
// }
//
// static int db_config_onoff(sqlite3* db, int op, int onoff) {
Expand Down Expand Up @@ -170,7 +169,6 @@ func openConn(path string, flags ...OpenFlags) (*Conn, error) {
}
}


return conn, nil
}

Expand Down Expand Up @@ -1224,8 +1222,8 @@ func sqliteInitFn() {
}
}

//export log_fn
func log_fn(_ unsafe.Pointer, code C.int, msg *C.char) {
//export go_log_fn
func go_log_fn(_ unsafe.Pointer, code C.int, msg *C.char) {
var msgBytes []byte
if msg != nil {
str := C.GoString(msg) // TODO: do not copy msg.
Expand Down
24 changes: 24 additions & 0 deletions wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,32 @@ int c_xapply_filter_tramp(void* pCtx, const char* zTab) {
return go_xapply_filter_tramp((uintptr_t)pCtx, (char*)zTab);
}

extern void go_func_tramp(sqlite3_context*, int, sqlite3_value**);
void c_func_tramp(sqlite3_context* ctx, int n, sqlite3_value** valarray) {
return go_func_tramp(ctx, n, valarray);
}

extern void go_step_tramp(sqlite3_context*, int, sqlite3_value**);
void c_step_tramp(sqlite3_context* ctx, int n, sqlite3_value** valarray) {
return go_step_tramp(ctx, n, valarray);
}

extern void go_final_tramp(sqlite3_context*);
void c_final_tramp(sqlite3_context* ctx) {
return go_final_tramp(ctx);
}

extern void go_destroy_tramp(uintptr_t);
void c_destroy_tramp(void* ptr) {
return go_destroy_tramp((uintptr_t)ptr);
}

extern int go_sqlite_auth_tramp(uintptr_t, int, char*, char*, char*, char*);
int c_auth_tramp(void *userData, int action, const char* arg1, const char* arg2, const char* db, const char* trigger) {
return go_sqlite_auth_tramp((uintptr_t)userData, action, (char*)arg1, (char*)arg2, (char*)db, (char*)trigger);
}

extern void go_log_fn(void*, int, char*);
void c_log_fn(void* pArg, int code, char* msg) {
return go_log_fn(pArg, code, msg);
}
6 changes: 6 additions & 0 deletions wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ int c_strm_r_tramp(void*, const void*, int*);
int c_xapply_conflict_tramp(void*, int, sqlite3_changeset_iter*);
int c_xapply_filter_tramp(void*, const char*);

void c_log_fn(void*, int, char*);
int c_auth_tramp(void*, int, const char*, const char*, const char*, const char*);

void c_func_tramp(sqlite3_context*, int, sqlite3_value**);
void c_step_tramp(sqlite3_context*, int, sqlite3_value**);
void c_final_tramp(sqlite3_context*);
void c_destroy_tramp(void*);

#endif // WRAPPERS_H