Browse Source

feat: GC使用守护器实现

SongZihuan 3 years ago
parent
commit
15f03556ba
8 changed files with 67 additions and 165 deletions
  1. 2 2
      include/core/env.h
  2. 1 1
      include/core/sig.h
  3. 1 9
      src/core/__env.h
  4. 2 12
      src/core/__gc.h
  5. 37 62
      src/core/env.c
  6. 13 48
      src/core/gc.c
  7. 10 30
      src/core/run.c
  8. 1 1
      test/src/run_code.c

+ 2 - 2
include/core/env.h

@@ -8,7 +8,7 @@ typedef struct af_Environment af_Environment;
 typedef struct af_Message af_Message;
 typedef struct af_ErrorInfo af_ErrorInfo;
 typedef struct af_ImportInfo af_ImportInfo;
-typedef struct af_GuardianList af_GuardianList;
+typedef struct af_GuardianList af_GuardianList, **paf_GuardianList;
 
 /* 顶层消息处理器的处理函数 DLC */
 typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);
@@ -92,7 +92,7 @@ AFUN_CORE_EXPORT void fprintfErrorInfoStdout(af_ErrorInfo *ei);
 AFUN_CORE_EXPORT void pushErrorBacktracking(FileLine line, FilePath file, char *note, af_ErrorInfo *ei);
 
 /* GuardianList 相关操作 */
-af_GuardianList **pushGuardianList(af_Object *func, af_GuardianList **pgl);
+af_GuardianList **pushGuardianList(af_Object *obj, af_Object *func, af_GuardianList **pgl);
 
 /* 环境变量 属性访问 */
 AFUN_CORE_EXPORT char *findEnvVarData(char *name, af_Environment *env);

+ 1 - 1
include/core/sig.h

@@ -1,6 +1,6 @@
 #ifndef AFUN_SIG_H
 #define AFUN_SIG_H
-
+#include <signal.h>
 #include "aFunCoreExport.h"
 
 struct af_SignalInfo {

+ 1 - 9
src/core/__env.h

@@ -73,6 +73,7 @@ struct af_LiteralDataList {
 };
 
 struct af_GuardianList {
+    struct af_Object *obj;
     struct af_Object *func;
     struct af_GuardianList *next;
 };
@@ -84,7 +85,6 @@ struct af_Activity {  // 活动记录器
         act_top = 0,  /* 顶层 永远存在第一层 */
         act_func,  /* 函数调用 */
         act_top_import,  /* 导入 运算结束后global进入msg反 */
-        act_gc,  /* gc机制 只存在一层 */
         act_guardian,  /* 守护器 */
     } type;
 
@@ -103,12 +103,6 @@ struct af_Activity {  // 活动记录器
     bool is_guard;  // 当为true时将不执行守护器
 
     union {
-        struct {  // 仅gc使用
-            struct gc_DestructList *dl;
-            struct gc_DestructList **pdl;  // 执行dl的最末端
-            struct gc_DestructList *dl_next;  // dl执行的位置
-        };
-
         struct {  // 仅守护器使用
             struct af_GuardianList *gl;
             struct af_GuardianList **pgl;  // 执行gl的最末端
@@ -276,9 +270,7 @@ AFUN_CORE_NO_EXPORT bool pushFuncActivity(af_Code *bt, af_Environment *env);
 AFUN_CORE_NO_EXPORT void popActivity(bool is_normal, af_Message *msg, af_Environment *env);
 
 /* 运行时Activity设置函数 (设置Activity) */
-AFUN_CORE_NO_EXPORT bool pushDestructActivity(gc_DestructList *dl, af_Environment *env);
 AFUN_CORE_NO_EXPORT bool pushGuadianFuncActivity(af_GuardianList *gl, af_Environment *env);
-AFUN_CORE_NO_EXPORT void pushGCActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env);
 AFUN_CORE_NO_EXPORT void pushGuardianActivity(af_GuardianList *gl, af_GuardianList **pgl, af_Environment *env);
 AFUN_CORE_NO_EXPORT bool pushVariableActivity(af_Code *bt, af_Object *func, af_Environment *env);
 AFUN_CORE_NO_EXPORT bool pushLiteralActivity(af_Code *bt, char *data, af_Object *func, af_Environment *env);

+ 2 - 12
src/core/__gc.h

@@ -8,7 +8,6 @@ typedef struct GC_Object GC_Object;
 typedef struct GC_ObjectData GC_ObjectData;
 typedef struct af_GcList af_GcList;
 typedef struct gc_Analyzed gc_Analyzed, **pgc_Analyzed;
-typedef struct gc_DestructList gc_DestructList, **pgc_DestructList;
 
 #define GC_FREE_EXCHANGE(obj, Type, Core) do { \
 {if ((obj)->gc.prev != NULL) {(obj)->gc.prev->gc.next = (obj)->gc.next;} \
@@ -70,12 +69,6 @@ struct gc_Analyzed {
     struct gc_Analyzed *next;
 };
 
-struct gc_DestructList {
-    struct af_Object *obj;
-    struct af_Object *func;
-    struct gc_DestructList *next;
-};
-
 /* 重新定义包括af_ObjectData的 gc Reference 函数 */
 #ifdef core_shared_t_EXPORTS
 #undef gc_addReference
@@ -115,17 +108,14 @@ AFUN_CORE_NO_EXPORT void gc_delObjectDataReference(af_ObjectData *obj);
 AFUN_CORE_NO_EXPORT GcCount gc_getObjectDataReference(af_ObjectData *obj);
 
 /* gc 操控函数 : gc的启动由解释器完全管理 */
-AFUN_CORE_NO_EXPORT void gc_RunGC(af_Environment *env);
-AFUN_CORE_NO_EXPORT pgc_DestructList checkAllDestruct(af_Environment *env, pgc_DestructList pdl);
+AFUN_CORE_NO_EXPORT af_GuardianList *gc_RunGC(af_Environment *env);
+AFUN_CORE_NO_EXPORT paf_GuardianList checkAllDestruct(af_Environment *env, paf_GuardianList pgl);
 AFUN_CORE_NO_EXPORT void gc_freeAllValueData(af_Environment *env);
 AFUN_CORE_NO_EXPORT void gc_freeAllValue(af_Environment *env);
 
 /* gc 信息函数 */
 AFUN_CORE_NO_EXPORT void printGCByCore(af_Core *core);
 
-/* gc_DestructList 释放函数*/
-AFUN_CORE_NO_EXPORT void freeAllDestructList(gc_DestructList *dl);
-
 /* gc 运行时函数 */
 AFUN_CORE_NO_EXPORT void resetGC(af_Environment *env);
 

+ 37 - 62
src/core/env.c

@@ -14,7 +14,6 @@ static af_Activity *makeFuncActivity(af_Code *bt_top, af_Code *bt_start, bool re
                                      af_VarSpaceListNode *vsl, af_Object *belong, af_Object *func);
 static af_Activity *makeTopActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong);
 static af_Activity *makeTopImportActivity(af_Code *bt_top, af_Code *bt_start, af_VarSpace *protect, af_Object *belong, char *mark);
-static af_Activity *makeGcActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env);
 static af_Activity *makeGuardianActivity(af_GuardianList *gl, af_GuardianList **pgl, af_Environment *env);
 static af_Activity *freeActivity(af_Activity *activity);
 static void freeActivityTop(af_Activity *activity);
@@ -75,7 +74,7 @@ static void fprintfNoteStderr(char *note);
 static void fprintfNoteStdout(char *note);
 
 /* af_GuardianList 创建与释放 */
-static af_GuardianList *makeGuardianList(af_Object *func);
+static af_GuardianList *makeGuardianList(af_Object *obj, af_Object *func);
 static af_GuardianList *freeGuardianList(af_GuardianList *gl);
 static void freeAllGuardianList(af_GuardianList *gl);
 
@@ -86,6 +85,7 @@ static void mp_IMPORT(af_Message *msg, bool is_top, af_Environment *env);
 
 /* 内置守护器 */
 static bool checkSignal(int signum, char *sig, char *sigcfg, char *sigerr, char err[], af_Environment *env);
+static af_GuardianList *guardian_GC(char *type, bool is_guard, void *data, af_Environment *env);
 static af_GuardianList *guardian_Signal(char *type, bool is_guard, void *data, af_Environment *env);
 
 /* 变量检查函数 */
@@ -250,22 +250,6 @@ static af_Activity *makeTopImportActivity(af_Code *bt_top, af_Code *bt_start, af
     return activity;
 }
 
-static af_Activity *makeGcActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env) {
-    af_Activity *activity = makeActivity(NULL, NULL, env->core->global);
-    activity->type = act_gc;
-
-    activity->var_list = makeVarSpaceList(getProtectVarSpace(env));
-    activity->new_vs_count = 1;
-
-    activity->file = strCopy("gc.aun.sys");
-    activity->line = 1;
-    activity->dl = dl;
-    activity->pdl = pdl;
-    activity->dl_next = dl;
-    activity->is_guard = true;
-    return activity;
-}
-
 static af_Activity *makeGuardianActivity(af_GuardianList *gl, af_GuardianList **pgl, af_Environment *env) {
     af_Activity *activity = makeActivity(NULL, NULL, env->core->global);
     activity->type = act_guardian;
@@ -291,10 +275,7 @@ static af_Activity *freeActivity(af_Activity *activity) {
     freeVarSpaceListCount(activity->new_vs_count, activity->var_list);
     free(activity->file);
 
-    if (activity->type == act_gc) {
-        if (activity->dl != NULL)
-            freeAllDestructList(activity->dl);
-    } else if (activity->type == act_guardian) {
+    if (activity->type == act_guardian) {
         if (activity->gl != NULL)
             freeAllGuardianList(activity->gl);
     } else {
@@ -797,6 +778,22 @@ static bool checkSignal(int signum, char *sig, char *sigcfg, char *sigerr, char
     return true;
 }
 
+/*
+ * 函数名: checkRunGC
+ * 目标: 检查是否该运行gc, 若是则返回true并运行gc, 否则返回false
+ */
+static af_GuardianList *guardian_GC(char *type, bool is_guard, void *data, af_Environment *env) {
+    af_GuardianList *gl = NULL;
+    if (env->core->gc_runtime->num == grt_always ||
+        env->core->gc_runtime->num == grt_count && env->core->gc_count->num >= env->core->gc_max->num) {
+        env->core->gc_count->num = 0;  // 清零
+        gl = gc_RunGC(env);
+        if (gl != NULL)
+            writeDebugLog(aFunCoreLogger, "GC destruct");
+    }
+    return gl;
+}
+
 static af_GuardianList *guardian_Signal(char *type, bool is_guard, void *data, af_Environment *env) {
     char error_msg[218] = {NUL};
     checkSignal(SIGINT, ev_sigint, ev_sigint_cfg, SIGNAL_INT, error_msg, env);
@@ -852,6 +849,10 @@ af_Environment *makeEnvironment(enum GcRunTime grt) {
     addGuardian("SIGNAL", false, 0, func4, NULL, NULL, env);
     FREE_SYMBOL(func4);
 
+    DLC_SYMBOL(GuardianFunc) func5 = MAKE_SYMBOL(guardian_GC, GuardianFunc);
+    addGuardian("GC", true, 0, func5, NULL, NULL, env);
+    FREE_SYMBOL(func5);
+
     env->core->status = core_init;
     env->activity = makeTopActivity(NULL, NULL, env->core->protect, env->core->global);
     return env;
@@ -1135,22 +1136,6 @@ bool pushMacroFuncActivity(af_Object *func, af_Environment *env) {
     return setFuncActivityToArg(func, env);
 }
 
-void pushGCActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *env) {
-    for (af_Activity *tmp = env->activity; tmp != NULL; tmp = tmp->prev) {
-        if (tmp->type == act_gc) {
-            *(tmp->pdl) = dl;
-            tmp->pdl = pdl;
-            if (tmp->dl_next == NULL)  // 原dl_next已经运行到末端
-                tmp->dl_next = dl;
-            return;
-        }
-    }
-
-    /* gc Activity 可能创建为顶层 activity, 故信息不能继承上一级(可能没有上一级) */
-    af_Activity *activity = makeGcActivity(dl, pdl, env);
-    pushActivity(activity, env);
-}
-
 void pushGuardianActivity(af_GuardianList *gl, af_GuardianList **pgl, af_Environment *env) {
     for (af_Activity *tmp = env->activity; tmp != NULL; tmp = tmp->prev) {
         if (tmp->type == act_guardian) {
@@ -1183,23 +1168,13 @@ bool pushImportActivity(af_Code *bt, af_Object **obj, char *mark, af_Environment
     return true;
 }
 
-bool pushDestructActivity(gc_DestructList *dl, af_Environment *env) {
-    env->activity->dl_next = dl->next;
-
-    /* 隐式调用不设置 bt_top */
-    af_Activity *activity = makeFuncActivity(NULL, NULL, false, env->activity->msg_up,
-                                             env->activity->var_list, env->activity->belong, NULL);
-    activity->is_gc_call = true;
-    pushActivity(activity, env);
-    return setFuncActivityToArg(dl->func, env);
-}
-
 bool pushGuadianFuncActivity(af_GuardianList *gl, af_Environment *env) {
     env->activity->gl_next = gl->next;
 
+    af_Object *belong = gl->obj != NULL ? gl->obj : env->core->global;
     /* 隐式调用不设置 bt_top */
     af_Activity *activity = makeFuncActivity(NULL, NULL, false, env->activity->msg_up,
-                                             env->activity->var_list, env->activity->belong, NULL);
+                                             env->activity->var_list, belong, NULL);
     activity->is_guard_call = true;
     pushActivity(activity, env);
     return setFuncActivityToArg(gl->func, env);
@@ -1486,10 +1461,8 @@ void popActivity(bool is_normal, af_Message *msg, af_Environment *env) {
     if (!is_normal)
         freeMark(env->activity);  // 遇到非正常退出时, 释放`mark`
 
-    if (env->activity->type == act_top || env->activity->type == act_gc || env->activity->type == act_guardian) {// 顶层或gc/guardian层
+    if (env->activity->type == act_top || env->activity->type == act_guardian) {// 顶层或gc/guardian层
         runTopMessageProcess((env->activity->type == act_top), env);
-        if (env->activity->type == act_guardian)
-            env->activity->prev->process_msg_first++;  // guardian开始处理时, 已经在原activity中有msg了, 所以要先处理
     } else {
         connectMessage(&(env->activity->msg_down), env->activity->prev->msg_down);
         env->activity->prev->msg_down = env->activity->msg_down;
@@ -1685,10 +1658,7 @@ static char *getActivityInfoToBacktracking(af_Activity *activity){
     char info[512] = "";
 
     /* strcat拼接的字符是可控的, 因此不需要使用安全函数 */
-    if (activity->type == act_gc) {
-        strcat(info, "gc-activity;");
-        return strCopy(info);
-    } else if (activity->type == act_guardian) {
+    if (activity->type == act_guardian) {
         strcat(info, "guardian-activity;");
         return strCopy(info);
     } else if (activity->type == act_top)
@@ -1796,15 +1766,20 @@ void freeImportInfo(af_ImportInfo *ii) {
     free(ii);
 }
 
-static af_GuardianList *makeGuardianList(af_Object *func) {
+static af_GuardianList *makeGuardianList(af_Object *obj, af_Object *func) {
     af_GuardianList *gl = calloc(1, sizeof(af_GuardianList));
+    gl->obj = obj;
     gl->func = func;
-    gc_addReference(gl->func);
+    if (obj != NULL)
+        gc_addReference(obj);
+    gc_addReference(func);
     return gl;
 }
 
 static af_GuardianList *freeGuardianList(af_GuardianList *gl) {
     af_GuardianList *next = gl->next;
+    if (gl->obj != NULL)
+        gc_delReference(gl->obj);
     gc_delReference(gl->func);
     free(gl);
     return next;
@@ -1815,8 +1790,8 @@ static void freeAllGuardianList(af_GuardianList *gl) {
         gl = freeGuardianList(gl);
 }
 
-af_GuardianList **pushGuardianList(af_Object *func, af_GuardianList **pgl) {
-    *pgl = makeGuardianList(func);
+af_GuardianList **pushGuardianList(af_Object *obj, af_Object *func, af_GuardianList **pgl){
+    *pgl = makeGuardianList(obj, func);
     return &((*pgl)->next);
 }
 
@@ -1925,7 +1900,7 @@ af_Object *getImportObject(af_ImportInfo *ii) {
 }
 
 af_VarSpaceListNode *getRunVarSpaceList(af_Environment *env) {
-    if (env->activity->type == act_gc || env->activity->type == act_guardian)
+    if (env->activity->type == act_guardian)
         return env->activity->var_list;
     else
         return env->activity->vsl;

+ 13 - 48
src/core/gc.c

@@ -125,41 +125,6 @@ GcCount gc_getVarSpaceReference(af_VarSpace *obj) {
     return obj->gc.info.reference;
 }
 
-/* gc_DestructList 函数 */
-/* gc_DestructList 创建与释放 */
-static gc_DestructList *makeDestructList(af_ObjectData *od, af_Object *func);
-static gc_DestructList *freeDestructList(gc_DestructList *dl);
-
-/* gc_DestructList 相关操作 */
-static pgc_DestructList pushDestructList(af_ObjectData *od, af_Object *func, pgc_DestructList pdl);
-
-static gc_DestructList *makeDestructList(af_ObjectData *od, af_Object *func) {
-    gc_DestructList *dl = calloc(1, sizeof(gc_DestructList));
-    dl->obj = od->base;
-    dl->func = func;
-    gc_addReference(dl->obj);
-    gc_addReference(dl->func);
-    return dl;
-}
-
-static gc_DestructList *freeDestructList(gc_DestructList *dl) {
-    gc_DestructList *next = dl->next;
-    gc_delReference(dl->obj);
-    gc_delReference(dl->func);
-    free(dl);
-    return next;
-}
-
-void freeAllDestructList(gc_DestructList *dl) {
-    while (dl != NULL)
-        dl = freeDestructList(dl);
-}
-
-static pgc_DestructList pushDestructList(af_ObjectData *od, af_Object *func, pgc_DestructList pdl) {
-    *pdl = makeDestructList(od, func);
-    return &((*pdl)->next);
-}
-
 /* gcList 函数 */
 /* gcList 创建与释放 */
 static af_GcList *freeGcList(af_GcList *gl);
@@ -227,7 +192,7 @@ static pgc_Analyzed reachableObject(struct af_Object *od, pgc_Analyzed plist);
 static void freeValue(af_Environment *env);
 static pgc_Analyzed reachable(af_Activity *activity, pgc_Analyzed plist);
 static pgc_Analyzed iterLinker(af_Core *core, pgc_Analyzed plist);
-static pgc_Analyzed checkDestruct(af_Environment *env, pgc_DestructList *pdl, pgc_Analyzed plist);
+static pgc_Analyzed checkDestruct(af_Environment *env, paf_GuardianList *pdl, pgc_Analyzed plist);
 static pgc_Analyzed checkAnalyzed(gc_Analyzed *analyzed, pgc_Analyzed plist);
 
 // 使用 gc_Analyzed 目的是令可达性分析程序不需要使用递归
@@ -359,7 +324,7 @@ static pgc_Analyzed reachable(af_Activity *activity, pgc_Analyzed plist) {
 
         plist = reachableVarSpaceList(activity->var_list, plist);
 
-        if (activity->type == act_gc || activity->type == act_guardian)  // gc不执行接下来的检查
+        if (activity->type == act_guardian)  // gc不执行接下来的检查
             continue;
 
         if (activity->func != NULL)
@@ -431,50 +396,50 @@ static void freeValue(af_Environment *env) {
     }
 }
 
-static pgc_Analyzed checkDestruct(af_Environment *env, pgc_DestructList *pdl, pgc_Analyzed plist) {
+static pgc_Analyzed checkDestruct(af_Environment *env, paf_GuardianList *pgl, pgc_Analyzed plist) {
     for (af_ObjectData *od = env->core->gc_ObjectData; od != NULL; od = od->gc.next) {
         if (!od->gc.info.reachable && !od->gc.done_destruct) {
             af_Object *func = findObjectAttributesByObjectData(mg_gc_destruct, NULL, od);
             if (func == NULL)
                 continue;
             od->gc.done_destruct = true;
-            *pdl = pushDestructList(od, func, *pdl);
+            printf("od->base = %p\n", od->base);
+            *pgl = pushGuardianList(od->base, func, *pgl);
             plist = reachableObjectData(od, plist);
         }
     }
     return plist;
 }
 
-void gc_RunGC(af_Environment *env) {
+af_GuardianList *gc_RunGC(af_Environment *env) {
     gc_Analyzed *analyzed = NULL;
-    gc_DestructList *dl = NULL;
+    af_GuardianList *gl = NULL;
     pgc_Analyzed plist = &analyzed;
-    pgc_DestructList pdl = &dl;
+    paf_GuardianList pgl = &gl;
     resetGC(env);
 
     plist = iterLinker(env->core, plist);  // 临时量分析 (临时量都是通过reference标记的)
     plist = reachable(env->activity, plist);
     plist = checkAnalyzed(analyzed, plist);  // 先处理剩余的Object
-    plist = checkDestruct(env, &pdl, plist);  // 在检查析构
+    plist = checkDestruct(env, &pgl, plist);  // 在检查析构
     checkAnalyzed(analyzed, plist);  // 在处理 checkDestruct 时产生的新引用
 
     freeValue(env);
     freeAllAnalyzed(analyzed);
-    if (dl != NULL)
-        pushGCActivity(dl, pdl, env);
+    return gl;
 }
 
-pgc_DestructList checkAllDestruct(af_Environment *env, pgc_DestructList pdl) {
+paf_GuardianList checkAllDestruct(af_Environment *env, paf_GuardianList pgl) {
     for (af_ObjectData *od = env->core->gc_ObjectData; od != NULL; od = od->gc.next) {
         if (!od->gc.done_destruct) {
             af_Object *func = findObjectAttributesByObjectData(mg_gc_destruct, NULL, od);
             if (func == NULL)
                 continue;
             od->gc.done_destruct = true;
-            pdl = pushDestructList(od, func, pdl);
+            pgl = pushGuardianList(od->base, func, pgl);
         }
     }
-    return pdl;
+    return pgl;
 }
 
 void gc_freeAllValueData(af_Environment *env) {

+ 10 - 30
src/core/run.c

@@ -13,7 +13,6 @@ static af_Message *getTopMsg(af_Environment *env);
 static bool checkInMsgType(char *type, af_Environment *env);
 static bool checkLiteral(af_Message **msg, af_Environment *env);
 static int checkMacro(af_Message *msg, af_Environment *env);
-static bool checkRunGC(af_Environment *env);
 static int checkMsg(af_Message *msg, af_Environment *env);
 bool checkNormalEnd(af_Message *msg, af_Environment *env);
 static bool checkGetArgEnd(af_Message *msg, af_Environment *env);
@@ -90,20 +89,6 @@ static int checkMacro(af_Message *msg, af_Environment *env) {
     return 0;
 }
 
-/*
- * 函数名: checkRunGC
- * 目标: 检查是否该运行gc, 若是则返回true并运行gc, 否则返回false
- */
-static bool checkRunGC(af_Environment *env) {
-    if (env->core->gc_runtime->num == grt_always ||
-        env->core->gc_runtime->num == grt_count && env->core->gc_count->num >= env->core->gc_max->num) {
-        env->core->gc_count->num = 0;  // 清零
-        gc_RunGC(env);
-        return true;
-    }
-    return false;
-}
-
 /*
  * 函数名: iterCodeInit
  * 目标: 初始化activity和environment (若environment中未存在activity则通过code新增一个TopActivity, 否则沿用原activity)
@@ -142,11 +127,11 @@ static bool iterCodeInit(af_Code *code, int mode, af_Environment *env) {
                 return false;
             env->activity->file = strCopy("top-gc.aun.sys");
 
-            if (env->activity->type == act_gc || !codeSemanticCheck(env->activity->bt_start) || env->activity->bt_next == NULL || code != NULL)
+            if (env->activity->type == act_guardian || !codeSemanticCheck(env->activity->bt_start) || env->activity->bt_next == NULL || code != NULL)
                 return false;
             break;
         case 3:
-            if (env->activity->type != act_gc || code != NULL)
+            if (env->activity->type != act_guardian || code != NULL)
                 return false;
             break;
         default:
@@ -244,6 +229,7 @@ static bool runGuardian(af_Environment *env) {
     }
 
     if (gl != NULL) {
+        env->activity->process_msg_first++;  //  guardian开始处理时, 已经在原activity中有msg了, 所以要先处理
         pushGuardianActivity(gl, pgl, env);
         return true;
     }
@@ -395,17 +381,11 @@ bool iterCode(af_Code *code, int mode, af_Environment *env){
             goto RETURN;
         }
 
-        /* 检查gc机制 */
-        checkRunGC(env);
-        if (env->activity->type == act_gc) {
-            if (env->activity->dl_next == NULL)
-                popActivity(true, NULL, env);  // 结束运行
-            else
-                pushDestructActivity(env->activity->dl_next, env);
-            continue;
-        } else if (env->activity->type == act_guardian) {
+        if (env->activity->type == act_guardian) {
             if (env->activity->gl_next == NULL) {
                 popActivity(true, NULL, env);  // 结束运行
+                if (mode == 3)  // mode = 3, 表示只运行守护器
+                    break;
             } else
                 pushGuadianFuncActivity(env->activity->gl_next, env);
             continue;
@@ -515,13 +495,13 @@ RETURN:
  */
 bool iterDestruct(int deep, af_Environment *env) {
     for (int count = 0; deep == 0 || deep > count; count++) {
-        gc_DestructList *dl = NULL;
-        pgc_DestructList pdl = &dl;
+        af_GuardianList *gl = NULL;
+        paf_GuardianList pdl = &gl;
 
         pdl = checkAllDestruct(env, pdl);
-        if (dl == NULL)
+        if (gl == NULL)
             return true;
-        pushGCActivity(dl, pdl, env);
+        pushGuardianActivity(gl, pdl, env);
         if (!iterCode(NULL, 3, env))
             return false;
     }

+ 1 - 1
test/src/run_code.c

@@ -380,7 +380,7 @@ af_GuardianList *gd_func(char *type, bool is_guard, struct GDData *data, af_Envi
 
     af_GuardianList *gd = NULL;
     data->no_first = true;
-    pushGuardianList(data->func, &gd);
+    pushGuardianList(NULL, data->func, &gd);
     return gd;
 }