瀏覽代碼

feat: 守护器支持执行函数

SongZihuan 3 年之前
父節點
當前提交
abd91b8e52
共有 6 個文件被更改,包括 144 次插入14 次删除
  1. 5 1
      include/core/env.h
  2. 1 1
      include/core/info/obj_api.h
  3. 20 1
      src/core/__env.h
  4. 93 5
      src/core/env.c
  5. 1 1
      src/core/gc.c
  6. 24 5
      src/core/run.c

+ 5 - 1
include/core/env.h

@@ -8,12 +8,13 @@ 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;
 
 /* 顶层消息处理器的处理函数 DLC */
 typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);
 DEFINE_DLC_SYMBOL(TopMsgProcessFunc);
 
-typedef void GuardianFunc(char *type, bool is_guard, void *data, af_Environment *env);
+typedef af_GuardianList *GuardianFunc(char *type, bool is_guard, void *data, af_Environment *env);
 DEFINE_DLC_SYMBOL(GuardianFunc);
 
 typedef void GuardianDestruct(char *type, void *data, af_Environment *env);
@@ -90,6 +91,9 @@ AFUN_CORE_EXPORT void fprintfErrorInfoStdout(af_ErrorInfo *ei);
 /* ErrorBacktracking 相关操作 */
 AFUN_CORE_EXPORT void pushErrorBacktracking(FileLine line, FilePath file, char *note, af_ErrorInfo *ei);
 
+/* GuardianList 相关操作 */
+af_GuardianList **pushGuardianList(af_Object *func, af_GuardianList **pgl);
+
 /* 环境变量 属性访问 */
 AFUN_CORE_EXPORT char *findEnvVarData(char *name, af_Environment *env);
 

+ 1 - 1
include/core/info/obj_api.h

@@ -23,7 +23,7 @@ typedef void objectAPIFunc();  // 位于object.h (所有Object API函数指针
 typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);  // 位于env.h
 
 /* 守护器处理函数 */
-typedef void GuardianFunc(char *type, bool is_guard, void *data, af_Environment *env);
+typedef af_GuardianList *GuardianFunc(char *type, bool is_guard, void *data, af_Environment *env);
 typedef void GuardianDestruct(char *type, void *data, af_Environment *env);
 
 /* 回调C函数 */

+ 20 - 1
src/core/__env.h

@@ -72,6 +72,11 @@ struct af_LiteralDataList {
     struct af_LiteralDataList *next;
 };
 
+struct af_GuardianList {
+    struct af_Object *func;
+    struct af_GuardianList *next;
+};
+
 struct af_Activity {  // 活动记录器
     struct af_Activity *prev;  // 上一个活动记录器
 
@@ -80,6 +85,7 @@ struct af_Activity {  // 活动记录器
         act_func,  /* 函数调用 */
         act_top_import,  /* 导入 运算结束后global进入msg反 */
         act_gc,  /* gc机制 只存在一层 */
+        act_guardian,  /* 守护器 */
     } type;
 
     struct af_Object *belong;  // 属对象 (belong通常为func的belong)
@@ -103,6 +109,12 @@ struct af_Activity {  // 活动记录器
             struct gc_DestructList *dl_next;  // dl执行的位置
         };
 
+        struct {  // 仅守护器使用
+            struct af_GuardianList *gl;
+            struct af_GuardianList **pgl;  // 执行gl的最末端
+            struct af_GuardianList *gl_next;  // gl执行的位置
+        };
+
         struct {  // gc以外的其他内容使用
             enum af_ActivityStatus {
                 act_func_get = 0,
@@ -141,6 +153,7 @@ struct af_Activity {  // 活动记录器
 
             /* 函数调用: 析构函数 在错误回溯时使用, 是个标记*/
             bool is_gc_call;
+            bool is_guard_call;
 
             /* 字面量 */
             bool is_literal;  // 处于字面量运算 意味着函数调用结束后会调用指定API
@@ -185,7 +198,7 @@ struct af_ActivityTrackBack {
 typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);
 NEW_DLC_SYMBOL(TopMsgProcessFunc, TopMsgProcessFunc);
 
-typedef void GuardianFunc(char *type, bool is_guard, void *data, af_Environment *env);
+typedef af_GuardianList *GuardianFunc(char *type, bool is_guard, void *data, af_Environment *env);
 NEW_DLC_SYMBOL(GuardianFunc, GuardianFunc);
 
 typedef void GuardianDestruct(char *type, void *data, af_Environment *env);
@@ -264,7 +277,9 @@ AFUN_CORE_NO_EXPORT void popActivity(bool is_normal, af_Message *msg, af_Environ
 
 /* 运行时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);
 AFUN_CORE_NO_EXPORT bool pushMacroFuncActivity(af_Object *func, af_Environment *env);
@@ -298,4 +313,8 @@ AFUN_CORE_NO_EXPORT void connectMessage(af_Message **base, af_Message *msg);
 /* 环境变量管理函数 */
 AFUN_CORE_NO_EXPORT af_EnvVar *setEnvVarNumber_(char *name, int32_t data, af_Environment *env);
 AFUN_CORE_NO_EXPORT af_EnvVar *setEnvVarData_(char *name, char *data, af_Environment *env);
+
+/* af_GuardianList管理函数 */
+AFUN_CORE_NO_EXPORT af_GuardianList **contectGuardianList(af_GuardianList *new, af_GuardianList **base);
+
 #endif //AFUN_ENV_H_

+ 93 - 5
src/core/env.c

@@ -15,6 +15,7 @@ static af_Activity *makeFuncActivity(af_Code *bt_top, af_Code *bt_start, bool re
 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);
 static void freeAllActivity(af_Activity *activity);
@@ -73,6 +74,11 @@ static void fprintfNote(FILE *file, char *note);
 static void fprintfNoteStderr(char *note);
 static void fprintfNoteStdout(char *note);
 
+/* af_GuardianList 创建与释放 */
+static af_GuardianList *makeGuardianList(af_Object *func);
+static af_GuardianList *freeGuardianList(af_GuardianList *gl);
+static void freeAllGuardianList(af_GuardianList *gl);
+
 /* 内置顶层消息处理器 */
 static void mp_NORMAL(af_Message *msg, bool is_top, af_Environment *env);
 static void mp_ERROR(af_Message *msg, bool is_top, af_Environment *env);
@@ -80,7 +86,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 void guardian_Signal(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);
 
 /* 变量检查函数 */
 static bool isInfixFunc(af_Code *code, af_Environment *env);
@@ -260,6 +266,22 @@ static af_Activity *makeGcActivity(gc_DestructList *dl, gc_DestructList **pdl, a
     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;
+
+    activity->var_list = makeVarSpaceList(getProtectVarSpace(env));
+    activity->new_vs_count = 1;
+
+    activity->file = strCopy("guardian.aun.sys");
+    activity->line = 1;
+    activity->gl = gl;
+    activity->pgl = pgl;
+    activity->gl_next = gl;
+    activity->is_guard = true;
+    return activity;
+}
+
 static af_Activity *freeActivity(af_Activity *activity) {
     af_Activity *prev = activity->prev;
 
@@ -272,6 +294,9 @@ static af_Activity *freeActivity(af_Activity *activity) {
     if (activity->type == act_gc) {
         if (activity->dl != NULL)
             freeAllDestructList(activity->dl);
+    } else if (activity->type == act_guardian) {
+        if (activity->gl != NULL)
+            freeAllGuardianList(activity->gl);
     } else {
         // vsl 是引用自 var_list和func_var_list的 故不释放
         // func_var_list 是引用自函数的 故不释放
@@ -772,7 +797,7 @@ static bool checkSignal(int signum, char *sig, char *sigcfg, char *sigerr, char
     return true;
 }
 
-static void guardian_Signal(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) {
     char error_msg[218] = {NUL};
     checkSignal(SIGINT, ev_sigint, ev_sigint_cfg, SIGNAL_INT, error_msg, env);
     checkSignal(SIGTERM, ev_sigterm, ev_sigterm_cfg, SIGNAL_TERM, error_msg, env);
@@ -798,7 +823,7 @@ static void guardian_Signal(char *type, bool is_guard, void *data, af_Environmen
 
         pushMessageDown(makeERRORMessage(SIGNAL_EXCEPTION, error_msg, env), env);
     }
-
+    return NULL;
 }
 
 af_Environment *makeEnvironment(enum GcRunTime grt) {
@@ -1126,6 +1151,22 @@ void pushGCActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *
     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) {
+            *(tmp->pgl) = gl;
+            tmp->pgl = pgl;
+            if (tmp->gl_next == NULL)  // 原dl_next已经运行到末端
+                tmp->gl_next = gl;
+            return;
+        }
+    }
+
+    /* gc Activity 可能创建为顶层 activity, 故信息不能继承上一级(可能没有上一级) */
+    af_Activity *activity = makeGuardianActivity(gl, pgl, env);
+    pushActivity(activity, env);
+}
+
 bool pushImportActivity(af_Code *bt, af_Object **obj, char *mark, af_Environment *env) {
     af_Object *tmp = NULL;
     if (obj == NULL)
@@ -1153,6 +1194,17 @@ bool pushDestructActivity(gc_DestructList *dl, af_Environment *env) {
     return setFuncActivityToArg(dl->func, env);
 }
 
+bool pushGuadianFuncActivity(af_GuardianList *gl, af_Environment *env) {
+    env->activity->gl_next = gl->next;
+
+    /* 隐式调用不设置 bt_top */
+    af_Activity *activity = makeFuncActivity(NULL, NULL, false, env->activity->msg_up,
+                                             env->activity->var_list, env->activity->belong, NULL);
+    activity->is_guard_call = true;
+    pushActivity(activity, env);
+    return setFuncActivityToArg(gl->func, env);
+}
+
 void setArgCodeListToActivity(af_ArgCodeList *acl, af_Environment *env) {
     if (acl != NULL) {
         setActivityBtStart(acl->code, env->activity);
@@ -1434,7 +1486,7 @@ 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) // 顶层或gc层
+    if (env->activity->type == act_top || env->activity->type == act_gc || env->activity->type == act_guardian) // 顶层或gc/guardian
         runTopMessageProcess((env->activity->type == act_top), env);
     else {
         connectMessage(&(env->activity->msg_down), env->activity->prev->msg_down);
@@ -1634,6 +1686,9 @@ static char *getActivityInfoToBacktracking(af_Activity *activity){
     if (activity->type == act_gc) {
         strcat(info, "gc-activity;");
         return strCopy(info);
+    } else if (activity->type == act_guardian) {
+        strcat(info, "guardian-activity;");
+        return strCopy(info);
     } else if (activity->type == act_top)
         strcat(info, "top-activity;");
     else if (activity->type == act_top_import)
@@ -1739,6 +1794,39 @@ void freeImportInfo(af_ImportInfo *ii) {
     free(ii);
 }
 
+static af_GuardianList *makeGuardianList(af_Object *func) {
+    af_GuardianList *gl = calloc(1, sizeof(af_GuardianList));
+    gl->func = func;
+    gc_addReference(gl->func);
+    return gl;
+}
+
+static af_GuardianList *freeGuardianList(af_GuardianList *gl) {
+    af_GuardianList *next = gl->next;
+    gc_delReference(gl->func);
+    free(gl);
+    return next;
+}
+
+static void freeAllGuardianList(af_GuardianList *gl) {
+    while (gl != NULL)
+        gl = freeGuardianList(gl);
+}
+
+af_GuardianList **pushGuardianList(af_Object *func, af_GuardianList **pgl) {
+    *pgl = makeGuardianList(func);
+    return &((*pgl)->next);
+}
+
+af_GuardianList **contectGuardianList(af_GuardianList *new, af_GuardianList **base) {
+    while ((*base) != NULL)
+        base = &((*base)->next);
+    *base = new;
+    while ((*base) != NULL)
+        base = &((*base)->next);
+    return base;
+}
+
 void setGcMax(int32_t max, af_Environment *env) {
     env->core->gc_max->num = max;
 }
@@ -1835,7 +1923,7 @@ af_Object *getImportObject(af_ImportInfo *ii) {
 }
 
 af_VarSpaceListNode *getRunVarSpaceList(af_Environment *env) {
-    if (env->activity->type == act_gc)
+    if (env->activity->type == act_gc || env->activity->type == act_guardian)
         return env->activity->var_list;
     else
         return env->activity->vsl;

+ 1 - 1
src/core/gc.c

@@ -273,7 +273,7 @@ static pgc_Analyzed reachable(af_Activity *activity, pgc_Analyzed plist) {
 
         plist = reachableVarSpaceList(activity->var_list, plist);
 
-        if (activity->type == act_gc)  // gc不执行接下来的检查
+        if (activity->type == act_gc || activity->type == act_guardian)  // gc不执行接下来的检查
             continue;
 
         if (activity->func != NULL)

+ 24 - 5
src/core/run.c

@@ -22,7 +22,7 @@ static bool checkStop(af_Environment *env);
 /* Code 执行函数 */
 static bool codeElement(af_Code *code, af_Environment *env);
 static bool codeBlock(af_Code *code, af_Environment *env);
-static void runGuardian(af_Environment *env);
+static bool runGuardian(af_Environment *env);
 
 /*
  * 函数名: checkInMsgType
@@ -232,11 +232,23 @@ static bool codeBlock(af_Code *code, af_Environment *env) {
         return pushFuncActivity(env->activity->bt_next, env);
 }
 
-static void runGuardian(af_Environment *env) {
+static bool runGuardian(af_Environment *env) {
+    af_GuardianList *gl = NULL;
+    af_GuardianList **pgl = ≷
     for (af_Guardian *gd = env->guardian; gd != NULL; gd = gd->next) {
-        if (gd->always || !env->activity->is_guard)  // guardian被标记为一直执行, 或者非is_guard模式
-            GET_SYMBOL(gd->func)(gd->type, env->activity->is_guard, gd->data, env);
+        if (gd->always || !env->activity->is_guard) { // guardian被标记为一直执行, 或者非is_guard模式
+            af_GuardianList *new = GET_SYMBOL(gd->func)(gd->type, env->activity->is_guard, gd->data, env);
+            if (new != NULL)
+                pgl = contectGuardianList(new, pgl);
+        }
     }
+
+    if (gl != NULL) {
+        pushGuardianActivity(gl, pgl, env);
+        return true;
+    }
+
+    return false;
 }
 
 /*
@@ -391,6 +403,12 @@ bool iterCode(af_Code *code, int mode, af_Environment *env){
             else
                 pushDestructActivity(env->activity->dl_next, env);
             continue;
+        } else if (env->activity->type == act_guardian) {
+            if (env->activity->dl_next == NULL)
+                popActivity(true, NULL, env);  // 结束运行
+            else
+                pushGuadianFuncActivity(env->activity->gl_next, env);
+            continue;
         }
 
         /* 切换执行的var_list */
@@ -439,7 +457,8 @@ bool iterCode(af_Code *code, int mode, af_Environment *env){
             continue;  // 后面的代码不再运行
 
         /* 执行守护器 */
-        runGuardian(env);
+        if (runGuardian(env))
+            continue;  // 需要执行守护器
 
         /* 处理msg */
         af_Message *msg = getTopMsg(env);