Selaa lähdekoodia

Always release all callbacks before set a new one

mingodad 9 vuotta sitten
vanhempi
sitoutus
65e4a8cdc6
1 muutettua tiedostoa jossa 57 lisäystä ja 57 poistoa
  1. 57 57
      SquiLu-ext/sq_sqlite3.cpp

+ 57 - 57
SquiLu-ext/sq_sqlite3.cpp

@@ -1426,6 +1426,7 @@ static SQRESULT sq_sqlite3_close_release(HSQUIRRELVM v, sq_sqlite3_sdb *sdb)
     if(sdb && sdb->db)
     if(sdb && sdb->db)
     {
     {
         sqlite3 *db = sdb->db;
         sqlite3 *db = sdb->db;
+        sdb->db = NULL; //prevent double entry/free, see sq_pushregistrytable(sdb->v) bellow
         /*
         /*
         sqlite3_stmt* statement = NULL;
         sqlite3_stmt* statement = NULL;
         int count = 0;
         int count = 0;
@@ -1444,11 +1445,15 @@ static SQRESULT sq_sqlite3_close_release(HSQUIRRELVM v, sq_sqlite3_sdb *sdb)
         {
         {
             rc = SQ_OK;
             rc = SQ_OK;
 
 
+            //we do not need remove the weak reference
+            //it's done automatically
+            /*
             //remove waekref from registrytable
             //remove waekref from registrytable
-            sq_pushregistrytable(sdb->v);
+            sq_pushregistrytable(sdb->v); //here we enter a loop that call us again
             sq_pushuserpointer(sdb->v, db);
             sq_pushuserpointer(sdb->v, db);
             sq_deleteslot(sdb->v, -2, SQFalse);
             sq_deleteslot(sdb->v, -2, SQFalse);
             sq_poptop(sdb->v);
             sq_poptop(sdb->v);
+            */
 
 
             if(sdb->func)
             if(sdb->func)
             {
             {
@@ -1790,7 +1795,7 @@ static int db_progress_callback(void *udata)
     sq_pushroottable(v); //this
     sq_pushroottable(v); //this
     sq_pushobject(v, sdb->progress_udata);
     sq_pushobject(v, sdb->progress_udata);
 
 
-    /* call lua function */
+    /* call squilu function */
     if (sq_call(v, 2, SQTrue, SQFalse) == SQ_OK)
     if (sq_call(v, 2, SQTrue, SQFalse) == SQ_OK)
         sq_getinteger(v, -1, &result);
         sq_getinteger(v, -1, &result);
 
 
@@ -1803,18 +1808,17 @@ static SQRESULT sq_sqlite3_progress_handler(HSQUIRRELVM v)
     SQ_FUNC_VARS(v);
     SQ_FUNC_VARS(v);
     GET_sqlite3_INSTANCE();
     GET_sqlite3_INSTANCE();
 
 
-    if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
-    {
-        sq_release(v, &sdb->progress_cb);
-        sq_release(v, &sdb->progress_udata);
+    /* clear progress handler */
+    sqlite3_progress_handler(self, 0, NULL, NULL);
 
 
-        sq_resetobject(&sdb->progress_cb);
-        sq_resetobject(&sdb->progress_udata);
+    sq_release(v, &sdb->progress_cb);
+    sq_release(v, &sdb->progress_udata);
 
 
-        /* clear busy handler */
-        sqlite3_progress_handler(self, 0, NULL, NULL);
-    }
-    else
+    sq_resetobject(&sdb->progress_cb);
+    sq_resetobject(&sdb->progress_udata);
+
+    SQObjectType otype = sq_gettype(v, 2);
+    if (_top_ > 1 && (otype != OT_NULL))
     {
     {
         SQ_GET_INTEGER(v, 2, nop);
         SQ_GET_INTEGER(v, 2, nop);
         if(sq_gettype(v, 3) != OT_CLOSURE)
         if(sq_gettype(v, 3) != OT_CLOSURE)
@@ -1876,18 +1880,17 @@ static SQRESULT sq_sqlite3_trace(HSQUIRRELVM v)
     SQ_FUNC_VARS(v);
     SQ_FUNC_VARS(v);
     GET_sqlite3_INSTANCE();
     GET_sqlite3_INSTANCE();
 
 
-    if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
-    {
-        sq_release(v, &sdb->trace_cb);
-        sq_release(v, &sdb->trace_udata);
+    /* clear trace handler */
+    sqlite3_trace(self, NULL, NULL);
 
 
-        sq_resetobject(&sdb->trace_cb);
-        sq_resetobject(&sdb->trace_udata);
+    sq_release(v, &sdb->trace_cb);
+    sq_release(v, &sdb->trace_udata);
 
 
-        /* clear trace handler */
-        sqlite3_trace(self, NULL, NULL);
-    }
-    else
+    sq_resetobject(&sdb->trace_cb);
+    sq_resetobject(&sdb->trace_udata);
+
+    SQObjectType otype = sq_gettype(v, 2);
+    if (_top_ > 1 && (otype != OT_NULL))
     {
     {
         if(sq_gettype(v, 2) != OT_CLOSURE)
         if(sq_gettype(v, 2) != OT_CLOSURE)
             return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
             return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
@@ -1948,18 +1951,17 @@ static SQRESULT sq_sqlite3_update_hook(HSQUIRRELVM v)
     SQ_FUNC_VARS(v);
     SQ_FUNC_VARS(v);
     GET_sqlite3_INSTANCE();
     GET_sqlite3_INSTANCE();
 
 
-    if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
-    {
-        sq_release(v, &sdb->update_hook_cb);
-        sq_release(v, &sdb->update_hook_udata);
+    /* clear update handler */
+    sqlite3_update_hook(self, NULL, NULL);
 
 
-        sq_resetobject(&sdb->update_hook_cb);
-        sq_resetobject(&sdb->update_hook_udata);
+    sq_release(v, &sdb->update_hook_cb);
+    sq_release(v, &sdb->update_hook_udata);
 
 
-        /* clear trace handler */
-        sqlite3_update_hook(self, NULL, NULL);
-    }
-    else
+    sq_resetobject(&sdb->update_hook_cb);
+    sq_resetobject(&sdb->update_hook_udata);
+
+    SQObjectType otype = sq_gettype(v, 2);
+    if (_top_ > 1 && (otype != OT_NULL))
     {
     {
         if(sq_gettype(v, 2) != OT_CLOSURE)
         if(sq_gettype(v, 2) != OT_CLOSURE)
             return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
             return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
@@ -2042,7 +2044,7 @@ static int db_authorizer_hook_callback(void *udata, int action_code,
     sq_pushstring(v, param5, -1);
     sq_pushstring(v, param5, -1);
     sq_pushstring(v, param6, -1);
     sq_pushstring(v, param6, -1);
 
 
-    /* call lua function */
+    /* call squilu function */
     /* ignore any error generated by this function */
     /* ignore any error generated by this function */
     if (sq_call(v, 7, SQTrue, SQFalse) == SQ_OK)
     if (sq_call(v, 7, SQTrue, SQFalse) == SQ_OK)
         sq_getinteger(v, -1, &result);
         sq_getinteger(v, -1, &result);
@@ -2056,18 +2058,17 @@ static SQRESULT sq_sqlite3_set_authorizer(HSQUIRRELVM v)
     SQ_FUNC_VARS(v);
     SQ_FUNC_VARS(v);
     GET_sqlite3_INSTANCE();
     GET_sqlite3_INSTANCE();
 
 
-    if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
-    {
-        sq_release(v, &sdb->authorizer_hook_cb);
-        sq_release(v, &sdb->authorizer_hook_udata);
+    /* clear trace handler */
+    sqlite3_set_authorizer(self, NULL, NULL);
 
 
-        sq_resetobject(&sdb->authorizer_hook_cb);
-        sq_resetobject(&sdb->authorizer_hook_udata);
+    sq_release(v, &sdb->authorizer_hook_cb);
+    sq_release(v, &sdb->authorizer_hook_udata);
 
 
-        /* clear trace handler */
-        sqlite3_set_authorizer(self, NULL, NULL);
-    }
-    else
+    sq_resetobject(&sdb->authorizer_hook_cb);
+    sq_resetobject(&sdb->authorizer_hook_udata);
+
+    SQObjectType otype = sq_gettype(v, 2);
+    if (_top_ > 1 && (otype != OT_NULL))
     {
     {
         if(sq_gettype(v, 2) != OT_CLOSURE)
         if(sq_gettype(v, 2) != OT_CLOSURE)
             return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
             return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
@@ -2107,7 +2108,7 @@ static int db_busy_callback(void *udata, int tries)
     sq_pushobject(v, sdb->busy_udata);
     sq_pushobject(v, sdb->busy_udata);
     sq_pushinteger(v, tries);
     sq_pushinteger(v, tries);
 
 
-    /* call lua function */
+    /* call squilu function */
     if (sq_call(v, 3, SQTrue, SQFalse) == SQ_OK)
     if (sq_call(v, 3, SQTrue, SQFalse) == SQ_OK)
         sq_getbool(v, -1, &retry);
         sq_getbool(v, -1, &retry);
 
 
@@ -2120,18 +2121,17 @@ static SQRESULT sq_sqlite3_busy_handler(HSQUIRRELVM v)
     SQ_FUNC_VARS(v);
     SQ_FUNC_VARS(v);
     GET_sqlite3_INSTANCE();
     GET_sqlite3_INSTANCE();
 
 
-    if (_top_ < 2 || sq_gettype(v, 2) == OT_NULL)
-    {
-        sq_release(v, &sdb->busy_cb);
-        sq_release(v, &sdb->busy_udata);
+    /* clear busy handler */
+    sqlite3_busy_handler(self, NULL, NULL);
 
 
-        sq_resetobject(&sdb->busy_cb);
-        sq_resetobject(&sdb->busy_udata);
+    sq_release(v, &sdb->busy_cb);
+    sq_release(v, &sdb->busy_udata);
 
 
-        /* clear busy handler */
-        sqlite3_busy_handler(self, NULL, NULL);
-    }
-    else
+    sq_resetobject(&sdb->busy_cb);
+    sq_resetobject(&sdb->busy_udata);
+
+    SQObjectType otype = sq_gettype(v, 2);
+    if (_top_ > 1 && (otype != OT_NULL))
     {
     {
         if(sq_gettype(v, 2) != OT_CLOSURE)
         if(sq_gettype(v, 2) != OT_CLOSURE)
             return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
             return sq_throwerror(v, _SC("invalid fisrt parameter expected closure"));
@@ -2409,7 +2409,7 @@ static void sqlite3_push_value(HSQUIRRELVM v, sqlite3_value *value)
         break;
         break;
 
 
     default:
     default:
-        /* things done properly (SQLite + Lua SQLite)
+        /* things done properly (SQLite + SquiLu SQLite)
         ** this should never happen */
         ** this should never happen */
         push_sqlite3_null(v);
         push_sqlite3_null(v);
         break;
         break;
@@ -2455,7 +2455,7 @@ static void db_sql_normal_function(sqlite3_context *context, int argc, sqlite3_v
         /* reuse context userdata value */
         /* reuse context userdata value */
         void *p = sqlite3_aggregate_context(context, 1);
         void *p = sqlite3_aggregate_context(context, 1);
         /* i think it is OK to use assume that using a light user data
         /* i think it is OK to use assume that using a light user data
-        ** as an entry on LUA REGISTRY table will be unique */
+        ** as an entry on SquiLu REGISTRY table will be unique */
         sq_pushregistrytable(v);
         sq_pushregistrytable(v);
         sq_pushuserpointer(v, p);
         sq_pushuserpointer(v, p);
         /* context table */
         /* context table */
@@ -2509,7 +2509,7 @@ static void db_sql_finalize_function(sqlite3_context *context)
     sq_pushroottable(v);
     sq_pushroottable(v);
 
 
     /* i think it is OK to use assume that using a light user data
     /* i think it is OK to use assume that using a light user data
-    ** as an entry on LUA REGISTRY table will be unique */
+    ** as an entry on SquiLu REGISTRY table will be unique */
     sq_pushregistrytable(v);
     sq_pushregistrytable(v);
     sq_pushuserpointer(v, p);
     sq_pushuserpointer(v, p);
     /* remove it from registry but we'll use it last time here */
     /* remove it from registry but we'll use it last time here */