Przeglądaj źródła

feat: 添加守护器模块

SongZihuan 3 lat temu
rodzic
commit
2ba583813c
5 zmienionych plików z 94 dodań i 3 usunięć
  1. 7 0
      include/core/env.h
  2. 3 0
      include/core/info/obj_api.h
  3. 11 0
      src/core/__env.h
  4. 63 2
      src/core/env.c
  5. 10 1
      src/core/run.c

+ 7 - 0
include/core/env.h

@@ -13,6 +13,9 @@ typedef struct af_ImportInfo af_ImportInfo;
 typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);
 DEFINE_DLC_SYMBOL(TopMsgProcessFunc);
 
+typedef void GuardianFunc(af_Message *msg, af_Environment *env);
+DEFINE_DLC_SYMBOL(GuardianFunc);
+
 enum GcRunTime {
     grt_always = 0,  // 总是运行
     grt_count,  // 累计式运行
@@ -68,6 +71,10 @@ AFUN_CORE_EXPORT void setEnvVarNumber(char *name, int32_t data, af_Environment *
 /* 顶层消息处理器 相关操作 */
 AFUN_CORE_EXPORT bool addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func, af_Environment *env);
 
+/* 顶层消息处理器 相关操作 */
+AFUN_CORE_EXPORT bool addGuardian(char *type, DLC_SYMBOL(GuardianFunc) func, af_Environment *env);
+AFUN_CORE_EXPORT bool popGuardian(char *type, af_Environment *env);
+
 /* LiteralRegex 相关操作 */
 AFUN_CORE_EXPORT bool pushLiteralRegex(char *pattern, char *func, bool in_protect, af_Environment *env);
 

+ 3 - 0
include/core/info/obj_api.h

@@ -22,6 +22,9 @@ typedef void objectAPIFunc();  // 位于object.h (所有Object API函数指针
 /* 顶层信号处理器函数 */
 typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);  // 位于env.h
 
+/* 守护器处理函数 */
+typedef void GuardianFunc(af_Message *msg, af_Environment *env);
+
 /* 回调C函数 */
 typedef struct CallFuncInfo CallFuncInfo;
 typedef struct af_FuncBody *callFuncBody(CallFuncInfo *info, af_Environment *env);  // 位于env.h

+ 11 - 0
src/core/__env.h

@@ -8,6 +8,7 @@ typedef struct af_ActivityTrackBack af_ActivityTrackBack;
 typedef struct af_EnvVarSpace af_EnvVarSpace;
 typedef struct af_EnvVar af_EnvVar;
 typedef struct af_TopMsgProcess af_TopMsgProcess;
+typedef struct af_Guardian af_Guardian;
 typedef struct af_LiteralDataList af_LiteralDataList;
 typedef struct af_LiteralRegex af_LiteralRegex;
 typedef struct af_ErrorBacktracking af_ErrorBacktracking;
@@ -182,12 +183,21 @@ struct af_ActivityTrackBack {
 typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);
 NEW_DLC_SYMBOL(TopMsgProcessFunc, TopMsgProcessFunc);
 
+typedef void GuardianFunc(af_Message *msg, af_Environment *env);
+NEW_DLC_SYMBOL(GuardianFunc, GuardianFunc);
+
 struct af_TopMsgProcess {  // 顶层msg处理器
     char *type;
     DLC_SYMBOL(TopMsgProcessFunc) func;
     struct af_TopMsgProcess *next;
 };
 
+struct af_Guardian {  // 守护器
+    char *type;
+    DLC_SYMBOL(GuardianFunc) func;
+    struct af_Guardian *next;
+};
+
 struct af_EnvVar {  // 环境变量
     char *name;
     char *data;
@@ -205,6 +215,7 @@ struct af_Environment {  // 运行环境
     struct af_EnvVarSpace *esv;
     struct af_Activity *activity;
     struct af_TopMsgProcess *process;
+    struct af_Guardian *guardian;
     bool in_run;
 };
 

+ 63 - 2
src/core/env.c

@@ -42,6 +42,14 @@ static void freeAllTopMsgProcess(af_TopMsgProcess *mp);
 /* 顶层消息处理器 处理函数 */
 static af_TopMsgProcess *findTopMsgProcessFunc(char *type, af_Environment *env);
 
+/* 守护器 创建与释放 */
+static af_Guardian *makeGuardian(char *type, DLC_SYMBOL(GuardianFunc) func);
+static af_Guardian *freeGuardian(af_Guardian *gd);
+static void freeAllGuardian(af_Guardian *gd);
+
+/* 守护器 处理函数 */
+static af_Guardian *findGuardian(char *type, af_Environment *env);
+
 /* LiteralData 创建与释放 */
 static af_LiteralDataList *makeLiteralDataList(char *data);
 static af_LiteralDataList *freeLiteralData_Pri(af_LiteralDataList *ld);
@@ -767,6 +775,7 @@ void freeEnvironment(af_Environment *env) {
     freeCore(env);
     freeEnvVarSpace(env->esv);
     freeAllTopMsgProcess(env->process);
+    freeAllGuardian(env->guardian);
 
     if (!res)
         writeErrorLog(aFunCoreLogger, "Run iterDestruct error.");
@@ -802,8 +811,7 @@ static af_TopMsgProcess *findTopMsgProcessFunc(char *type, af_Environment *env)
     return NULL;
 }
 
-bool addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
-                      af_Environment *env) {
+bool addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func, af_Environment *env) {
     af_TopMsgProcess *mp = findTopMsgProcessFunc(type, env);
     if (mp != NULL)
         return false;
@@ -814,6 +822,59 @@ bool addTopMsgProcess(char *type, DLC_SYMBOL(TopMsgProcessFunc) func,
     return true;
 }
 
+static af_Guardian *makeGuardian(char *type, DLC_SYMBOL(GuardianFunc) func) {
+    af_Guardian *gd = calloc(1, sizeof(af_Guardian));
+    gd->type = strCopy(type);
+    gd->func = COPY_SYMBOL(func, GuardianFunc);
+    return gd;
+}
+
+static af_Guardian *freeGuardian(af_Guardian *gd) {
+    af_Guardian *next = gd->next;
+    free(gd->type);
+    FREE_SYMBOL(gd->func);
+    free(gd);
+    return next;
+}
+
+static void freeAllGuardian(af_Guardian *gd) {
+    while (gd != NULL)
+        gd = freeGuardian(gd);
+}
+
+static af_Guardian *findGuardian(char *type, af_Environment *env) {
+    af_Guardian *gd = env->guardian;
+    for (NULL; gd != NULL; gd = gd->next) {
+        if (EQ_STR(type, gd->type))
+            return gd;
+    }
+    return NULL;
+}
+
+bool addGuardian(char *type, DLC_SYMBOL(GuardianFunc) func,
+                 af_Environment *env) {
+    af_Guardian *gd = findGuardian(type, env);
+    if (gd != NULL)
+        return false;
+
+    gd = makeGuardian(type, func);
+    gd->next = env->guardian;
+    env->guardian = gd;
+    return true;
+}
+
+bool popGuardian(char *type, af_Environment *env) {
+    af_Guardian **gd = &env->guardian;
+    for (NULL; *gd != NULL; gd = &((*gd)->next)) {
+        if (EQ_STR(type, (*gd)->type)) {
+            *gd = freeGuardian(*gd);
+            return true;
+        }
+    }
+
+    return false;
+}
+
 static void newActivity(af_Code *bt, const af_Code *next, bool return_first, af_Environment *env){
     if (next == NULL && env->activity->body_next == NULL && env->activity->type == act_func) {
         writeTrackLog(aFunCoreLogger, "Tail call optimization");

+ 10 - 1
src/core/run.c

@@ -17,10 +17,12 @@ 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);
+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);
 
 /*
  * 函数名: checkInMsgType
@@ -230,6 +232,11 @@ static bool codeBlock(af_Code *code, af_Environment *env) {
         return pushFuncActivity(env->activity->bt_next, env);
 }
 
+static void runGuardian(af_Environment *env) {
+    for (af_Guardian *gd = env->guardian; gd != NULL; gd = gd->next) {
+        GET_SYMBOL(gd->func)(env->activity->msg_down, env);
+    }
+}
 
 /*
  * 函数名: getTopMsg
@@ -371,7 +378,6 @@ bool iterCode(af_Code *code, int mode, af_Environment *env){
 
         /* 检查gc机制 */
         checkRunGC(env);
-
         if (env->activity->type == act_gc) {
             if (env->activity->dl_next == NULL)
                 popActivity(true, NULL, env);  // 结束运行
@@ -425,6 +431,9 @@ bool iterCode(af_Code *code, int mode, af_Environment *env){
         if (pass_msg)
             continue;  // 后面的代码不再运行
 
+        /* 执行守护器 */
+        runGuardian(env);
+
         /* 处理msg */
         af_Message *msg = getTopMsg(env);
         switch (checkMsg(msg, env)) {