2
0
Эх сурвалжийг харах

feat: core添加了停止与退出机制

SongZihuan 3 жил өмнө
parent
commit
d45db0c626

+ 5 - 0
include/env.h

@@ -35,6 +35,11 @@ bool addVarToProtectVarSpace(af_Var *var, af_Environment *env);
 af_Object *getBaseObject(char *name, af_Environment *env);
 af_VarSpace *getProtectVarSpace(af_Environment *env);
 
+/* Core 退出与停止 */
+void setCoreStop(af_Environment *env);
+void setCoreExit(int exit_code, af_Environment *env);
+void setCoreNormal(af_Environment *env);
+
 /* 消息创建与释放函数 */
 af_Message *makeMessage(char *type, size_t size);
 af_Message *freeMessage(af_Message *msg);

+ 5 - 0
src/core/__env.h

@@ -30,6 +30,8 @@ struct af_Core {  // 解释器核心
         core_creat = 0,
         core_init,  // 执行.i.af
         core_normal,  // 正常执行
+        core_srop,  // 当前运算退出
+        core_exit,  // 解释器退出
     } status;
 
     /* GC基本信息 */
@@ -49,6 +51,9 @@ struct af_Core {  // 解释器核心
 
     /* 字面量基本信息 */
     af_LiteralRegex *lr;
+
+    /* exit */
+    int exit_code;  // 退出代码
 };
 
 struct af_Message {

+ 17 - 0
src/core/env.c

@@ -135,6 +135,23 @@ af_Object *getBaseObject(char *name, af_Environment *env) {
     return getBaseObjectFromCore(name, env->core);
 }
 
+void setCoreStop(af_Environment *env) {
+    if (env->core->status != core_exit)
+        env->core->status = core_srop;
+}
+
+void setCoreExit(int exit_code, af_Environment *env) {
+    env->core->status = core_exit;
+    env->core->exit_code = exit_code;
+}
+
+void setCoreNormal(af_Environment *env) {
+    if (env->core->status == core_exit || env->core->status == core_srop) {
+        env->core->status = core_normal;
+        env->core->exit_code = 0;
+    }
+}
+
 static af_Activity *makeActivity(af_Message *msg_up, af_VarSpaceListNode *vsl, af_Object *belong) {
     af_Activity *activity = calloc(1, sizeof(af_Activity));
     activity->msg_up = msg_up;

+ 1 - 1
src/core/object.c

@@ -84,7 +84,7 @@ af_Object *makeObject(char *id, bool free_api, af_ObjectAPI *api, bool allow_inh
             belong = env->activity->belong;
         else if (env->core->status == core_init)  // init模式生成: global
             belong = env->core->global;
-        else if (env->core->status == core_normal)
+        else if (env->core->status != core_creat)  // 只有creat可以使用belong=NULL
             return NULL;
     }
 

+ 10 - 1
src/core/run.c

@@ -107,6 +107,10 @@ static bool checkRunGC(af_Environment *env) {
  * 目标: 初始化activity和environment (若environment中未存在activity则通过code新增一个TopActivity, 否则沿用原activity)
  */
 static bool iterCodeInit(af_Code *code, af_Environment *env) {
+    if (env == NULL || env->core == NULL || env->core->status == core_exit)
+        return false;
+    if (env->core->status == core_srop)
+        env->core->status = core_normal;
     if (env->activity == NULL && code == NULL || env->activity != NULL && code != NULL)
         return false;
     if (code != NULL && !addTopActivity(code, env))  // 初始化环境
@@ -390,8 +394,13 @@ bool iterCode(af_Code *code, af_Environment *env) {
     for (NULL; env->activity != NULL; ) {
         af_Message *msg = NULL;
         bool run_code = false;
-        checkRunGC(env);
+        if (env->core->status == core_srop || env->core->status == core_exit) {
+            for (NULL; env->activity != NULL;)
+                popActivity(false, NULL, env);  // is_normal=false, 非正常退出, 释放mark
+            return false;
+        }
 
+        checkRunGC(env);
         if (env->activity->type == act_gc) {  // gc 模式
             if (env->activity->dl_next == NULL)
                 popActivity(true, NULL, env);  // 结束运行