@@ -1647,26 +1647,28 @@ void Backup(const FunctionCallbackInfo<Value>& args) {
16471647 job->ScheduleBackup ();
16481648}
16491649
1650+ struct ConflictCallbackContext {
1651+ std::function<bool (std::string)> filterCallback;
1652+ std::function<int (int )> conflictCallback;
1653+ };
1654+
16501655// the reason for using static functions here is that SQLite needs a
16511656// function pointer
1652- static std::function<int (int )> conflictCallback;
16531657
16541658static int xConflict (void * pCtx, int eConflict, sqlite3_changeset_iter* pIter) {
1655- if (!conflictCallback) return SQLITE_CHANGESET_ABORT;
1656- return conflictCallback (eConflict);
1659+ auto ctx = static_cast <ConflictCallbackContext*>(pCtx);
1660+ if (!ctx->conflictCallback ) return SQLITE_CHANGESET_ABORT;
1661+ return ctx->conflictCallback (eConflict);
16571662}
16581663
1659- static std::function<bool (std::string)> filterCallback;
1660-
16611664static int xFilter (void * pCtx, const char * zTab) {
1662- if (!filterCallback) return 1 ;
1663-
1664- return filterCallback (zTab) ? 1 : 0 ;
1665+ auto ctx = static_cast <ConflictCallbackContext*>(pCtx) ;
1666+ if (!ctx-> filterCallback ) return 1 ;
1667+ return ctx-> filterCallback (zTab) ? 1 : 0 ;
16651668}
16661669
16671670void DatabaseSync::ApplyChangeset (const FunctionCallbackInfo<Value>& args) {
1668- conflictCallback = nullptr ;
1669- filterCallback = nullptr ;
1671+ ConflictCallbackContext context;
16701672
16711673 DatabaseSync* db;
16721674 ASSIGN_OR_RETURN_UNWRAP (&db, args.This ());
@@ -1702,7 +1704,7 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
17021704 return ;
17031705 }
17041706 Local<Function> conflictFunc = conflictValue.As <Function>();
1705- conflictCallback = [env, conflictFunc](int conflictType) -> int {
1707+ context. conflictCallback = [env, conflictFunc](int conflictType) -> int {
17061708 Local<Value> argv[] = {Integer::New (env->isolate (), conflictType)};
17071709 TryCatch try_catch (env->isolate ());
17081710 Local<Value> result =
@@ -1740,7 +1742,7 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
17401742
17411743 Local<Function> filterFunc = filterValue.As <Function>();
17421744
1743- filterCallback = [env, filterFunc](std::string item) -> bool {
1745+ context. filterCallback = [env, filterFunc](std::string item) -> bool {
17441746 // TODO(@jasnell): The use of ToLocalChecked here means that if
17451747 // the filter function throws an error the process will crash.
17461748 // The filterCallback should be updated to avoid the check and
@@ -1764,7 +1766,7 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
17641766 const_cast <void *>(static_cast <const void *>(buf.data ())),
17651767 xFilter,
17661768 xConflict,
1767- nullptr );
1769+ static_cast < void *>(&context) );
17681770 if (r == SQLITE_OK) {
17691771 args.GetReturnValue ().Set (true );
17701772 return ;
0 commit comments