Forráskód Böngészése

feat: 添加ImportInfo

SongZihuan 3 éve
szülő
commit
3ccc2f5908
4 módosított fájl, 116 hozzáadás és 15 törlés
  1. 7 1
      include/core/README
  2. 12 0
      include/core/env.h
  3. 9 1
      src/core/__env.h
  4. 88 13
      src/core/env.c

+ 7 - 1
include/core/README

@@ -198,8 +198,11 @@ af_ErrorInfo 描述错误信息
 * makeNORMALMessage
 * makeERRORMessage
 * makeERRORMessageFormat
+* makeIMPORTMessage
 * makeErrorInfo
 * freeErrorInfo
+* makeImportInfo
+* freeImportInfo
 
 相关操作:
 * enableEnvironment
@@ -238,5 +241,8 @@ af_ErrorInfo 描述错误信息
 * findEnvVar
 * getMsgNormalData
 * getMsgErrorInfo
+* getMsgImportInfo
 * getErrorType
-* getError
+* getError
+* getImportMark
+* getImportObject

+ 12 - 0
include/core/env.h

@@ -7,6 +7,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;
 
 /* 顶层消息处理器的处理函数 DLC */
 typedef void TopMsgProcessFunc(af_Message *msg, bool is_gc, af_Environment *env);
@@ -32,11 +33,16 @@ AFUN_CORE_EXPORT bool freeMessageCount(size_t count, af_Message *msg);
 AFUN_CORE_EXPORT af_Message *makeNORMALMessage(af_Object *obj);
 AFUN_CORE_EXPORT af_Message *makeERRORMessage(char *type, char *error, af_Environment *env);
 AFUN_CORE_EXPORT af_Message *makeERRORMessageFormat(char *type, af_Environment *env, const char *format, ...);
+AFUN_CORE_EXPORT af_Message *makeIMPORTMessage(char *mark, af_Object *obj);
 
 /* ErrorInfo 创建与释放 */
 AFUN_CORE_EXPORT af_ErrorInfo *makeErrorInfo(char *type, char *error, char *note, FileLine line, FilePath path);
 AFUN_CORE_EXPORT void freeErrorInfo(af_ErrorInfo *ei);
 
+/* ImportInfo 创建与释放 */
+af_ImportInfo *makeImportInfo(char *mark, af_Object *obj);
+void freeImportInfo(af_ImportInfo *ii);
+
 /* 运行环境 相关操作 */
 AFUN_CORE_EXPORT void enableEnvironment(af_Environment *env);
 AFUN_CORE_EXPORT void setGcMax(size_t max, af_Environment *env);
@@ -90,8 +96,14 @@ AFUN_CORE_EXPORT FileLine getActivityLine(af_Environment *env);
 /* 消息 属性访问 */
 AFUN_CORE_EXPORT af_Object *getMsgNormalData(af_Message *msg);
 AFUN_CORE_EXPORT af_ErrorInfo *getMsgErrorInfo(af_Message *msg);
+AFUN_CORE_EXPORT af_ImportInfo *getMsgImportInfo(af_Message *msg);
 
 /* ErrorInfo 属性访问 */
 AFUN_CORE_EXPORT char *getErrorType(af_ErrorInfo *ei);
 AFUN_CORE_EXPORT char *getError(af_ErrorInfo *ei);
+
+/* ImportInfo 属性访问 */
+AFUN_CORE_EXPORT char *getImportMark(af_ImportInfo *ii);
+AFUN_CORE_EXPORT af_Object *getImportObject(af_ImportInfo *ii);
+
 #endif //AFUN_ENV

+ 9 - 1
src/core/__env.h

@@ -147,6 +147,9 @@ struct af_Activity {  // 活动记录器
 
             /* 尾调用优化 */
             bool optimization;
+
+            /* Import使用 */
+            char *import_mark;
         };
     };
 };
@@ -199,6 +202,11 @@ struct af_ErrorInfo {
     af_Object *obj;
 };
 
+struct af_ImportInfo {
+    char *mark;
+    af_Object *obj;
+};
+
 /* Core 管理函数 */
 AFUN_CORE_NO_EXPORT af_Object *getBaseObjectFromCore(char *name, af_Core *core);
 
@@ -213,7 +221,7 @@ AFUN_CORE_NO_EXPORT void pushGCActivity(gc_DestructList *dl, gc_DestructList **p
 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);
-AFUN_CORE_NO_EXPORT bool pushImportActivity(af_Code *bt, af_Environment *env);
+AFUN_CORE_NO_EXPORT bool pushImportActivity(af_Code *bt, af_Object **obj, char *mark, af_Environment *env);
 AFUN_CORE_NO_EXPORT bool setFuncActivityToArg(af_Object *func, af_Environment *env);
 AFUN_CORE_NO_EXPORT bool setFuncActivityAddVar(af_Environment *env);
 AFUN_CORE_NO_EXPORT int setFuncActivityToNormal(af_Environment *env);

+ 88 - 13
src/core/env.c

@@ -12,7 +12,7 @@ static af_Activity *makeActivity(af_Message *msg_up, af_VarSpaceListNode *vsl, a
 static af_Activity *makeFuncActivity(af_Code *bt_top, af_Code *bt_start, bool return_first, af_Message *msg_up,
                                      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);
+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 *freeActivity(af_Activity *activity);
 static void freeActivityTop(af_Activity *activity);
@@ -58,6 +58,7 @@ static void fprintfNote(FILE *file, char *note);
 /* 内置顶层消息处理器 */
 static void mp_NORMAL(af_Message *msg, bool is_gc, af_Environment *env);
 static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env);
+static void mp_IMPORT(af_Message *msg, bool is_gc, af_Environment *env);
 
 /* 变量检查函数 */
 static bool isInfixFunc(af_Code *code, af_Environment *env);
@@ -199,10 +200,11 @@ static af_Activity *makeTopActivity(af_Code *bt_top, af_Code *bt_start, af_VarSp
     return activity;
 }
 
-static af_Activity *makeTopImportActivity(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) {
     af_Activity *activity = makeTopActivity(bt_top, bt_start, protect, belong);
-
     activity->type = act_top_import;
+    if (mark != NULL)
+        activity->import_mark = strCopy(mark);
     return activity;
 }
 
@@ -242,6 +244,8 @@ static af_Activity *freeActivity(af_Activity *activity) {
         if (activity->fi != NULL)
             freeFuncInfo(activity->fi);
         freeAllLiteralData(activity->ld);
+
+        free(activity->import_mark);
     }
 
     free(activity);
@@ -490,6 +494,12 @@ af_Message *makeERRORMessageFormat(char *type, af_Environment *env, const char *
     return makeERRORMessage(type, buf, env);;
 }
 
+af_Message *makeIMPORTMessage(char *mark, af_Object *obj) {
+    af_Message *msg = makeMessage("IMPORT", sizeof(af_ImportInfo *));
+    *(af_ImportInfo **)msg->msg = makeImportInfo(mark, obj);
+    return msg;
+}
+
 static af_EnvVar *makeEnvVar(char *name, char *data) {
     af_EnvVar *var = calloc(1, sizeof(af_EnvVar));
     var->name = strCopy(name);
@@ -550,7 +560,7 @@ char *findEnvVar(char *name, af_Environment *env) {
 
 static void mp_NORMAL(af_Message *msg, bool is_gc, af_Environment *env) {
     if (msg->msg == NULL || *(af_Object **)msg->msg == NULL) {
-        fprintf(stderr, "msg: %p error\n", msg->msg);
+        fprintf(stderr, "NORMAL msg: %p error\n", msg->msg);
         return;
     }
     gc_delReference(*(af_Object **)msg->msg);
@@ -560,7 +570,7 @@ static void mp_NORMAL(af_Message *msg, bool is_gc, af_Environment *env) {
 
 static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env) {
     if (msg->msg == NULL || *(af_ErrorInfo **)msg->msg == NULL) {
-        printf("msg: %p error\n", msg->msg);
+        printf("ERROR msg: %p error\n", msg->msg);
         return;
     }
     if (!is_gc)
@@ -568,6 +578,26 @@ static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env) {
     freeErrorInfo(*(af_ErrorInfo **)msg->msg);
 }
 
+static void mp_IMPORT(af_Message *msg, bool is_gc, af_Environment *env) {
+    if (msg->msg == NULL || *(af_ImportInfo **)msg->msg == NULL) {
+        printf("IMPORT msg: %p error\n", msg->msg);
+        return;
+    }
+    af_ImportInfo *ii = *(af_ImportInfo **)msg->msg;
+    if (ii->obj == NULL) {
+        printf("IMPORT msg: %p do not get obj\n", msg->msg);
+        return;
+    }
+
+    gc_delReference(ii->obj);
+    if (ii->mark != NULL) {
+        makeVarToProtectVarSpace(ii->mark, 3, 3, 3, ii->obj, env);
+        printf("IMPORT point: [%s] %p \n", ii->mark, ii->obj);
+    } else
+        printf("IMPORT point: <no-name> %p \n", ii->obj);
+    freeImportInfo(ii);
+}
+
 af_Environment *makeEnvironment(enum GcRunTime grt) {
     af_Environment *env = calloc(1, sizeof(af_Environment));
     env->core = makeCore(grt);
@@ -592,6 +622,10 @@ af_Environment *makeEnvironment(enum GcRunTime grt) {
     addTopMsgProcess("ERROR", func2, env);
     FREE_SYMBOL(func2);
 
+    DLC_SYMBOL(TopMsgProcessFunc) func3 = MAKE_SYMBOL(mp_IMPORT, TopMsgProcessFunc);
+    addTopMsgProcess("IMPORT", func3, env);
+    FREE_SYMBOL(func3);
+
     env->core->status = core_init;
     env->activity = makeTopActivity(NULL, NULL, env->core->protect, env->core->global);
     return env;
@@ -824,12 +858,18 @@ void pushGCActivity(gc_DestructList *dl, gc_DestructList **pdl, af_Environment *
     env->activity = activity;
 }
 
-bool pushImportActivity(af_Code *bt, af_Environment *env) {
-    af_Object *obj = makeGlobalObject(env);
+bool pushImportActivity(af_Code *bt, af_Object **obj, char *mark, af_Environment *env) {
+    af_Object *tmp = NULL;
     if (obj == NULL)
+        obj = &tmp;
+
+    if (*obj == NULL)
+        *obj = makeGlobalObject(env);
+
+    if (*obj == NULL)
         return false;
 
-    af_Activity *activity = makeTopImportActivity(bt, bt, env->core->protect, obj);
+    af_Activity *activity = makeTopImportActivity(bt, bt, env->core->protect, *obj, mark);
     activity->prev = env->activity;
     env->activity = activity;
     return true;
@@ -1006,7 +1046,7 @@ int setFuncActivityToNormal(af_Environment *env){  // 获取函数的函数体
             break;
         }
         case func_body_import:
-            if (!pushImportActivity(body->code, env)) {
+            if (!pushImportActivity(body->code, NULL, NULL, env)) {
                 pushMessageDown(makeERRORMessage(IMPORT_ERROR, IMPORT_OBJ_ERROR, env), env);
                 activity->process_msg_first++;
                 re = 2;
@@ -1086,10 +1126,8 @@ void popActivity(bool is_normal, af_Message *msg, af_Environment *env) {
     if (env->activity->type == act_top_import && /* import模式, 并且msg_down中有normal, 则把normal替换为belong */
         env->activity->msg_down != NULL && EQ_STR(env->activity->msg_down->type, "NORMAL")) {
         af_Message *tmp = getFirstMessage(env);
-        gc_delReference(*(af_Object **) (tmp->msg));
-        freeMessage(tmp);
-
-        pushMessageDown(makeNORMALMessage(env->activity->belong), env);  // 压入belong作为msg
+        pushMessageDown(makeIMPORTMessage(env->activity->import_mark, env->activity->belong), env);  // 压入belong作为msg
+        pushMessageDown(tmp, env);
     }
 
     if (!is_normal)
@@ -1302,6 +1340,22 @@ static char *getActivityInfoToBacktracking(af_Activity *activity, bool print_bt_
     return info;
 }
 
+af_ImportInfo *makeImportInfo(char *mark, af_Object *obj) {
+    af_ImportInfo *ii = calloc(1, sizeof(af_ImportInfo));
+    if (mark != NULL)
+        ii->mark = strCopy(mark);
+    ii->obj = obj;
+    gc_addReference(obj);
+    return ii;
+}
+
+void freeImportInfo(af_ImportInfo *ii) {
+    free(ii->mark);
+    if (ii->obj != NULL)
+        gc_delReference(ii->obj);
+    free(ii);
+}
+
 void setGcMax(size_t max, af_Environment *env) {
     env->core->gc_count_max = max;
 }
@@ -1358,6 +1412,7 @@ af_Object *getMsgNormalData(af_Message *msg) {
         return NULL;
     af_Object *obj = *(af_Object **)msg->msg;
     gc_delReference(obj);
+    *(af_Object **)msg->msg = NULL;
     return obj;
 }
 
@@ -1368,10 +1423,30 @@ af_ErrorInfo *getMsgErrorInfo(af_Message *msg) {
     return ei;
 }
 
+af_ImportInfo *getMsgImportInfo(af_Message *msg) {
+    if (!EQ_STR("IMPORT", msg->type))
+        return NULL;
+    af_ImportInfo *ii = *(af_ImportInfo **)msg->msg;
+    return ii;
+}
+
 char *getErrorType(af_ErrorInfo *ei) {
     return ei->error_type;
 }
 
 char *getError(af_ErrorInfo *ei) {
     return ei->error;
+}
+
+char *getImportMark(af_ImportInfo *ii) {
+    return ii->mark;
+}
+
+af_Object *getImportObject(af_ImportInfo *ii) {
+    af_Object *obj = ii->obj;
+    if (obj == NULL)
+        return NULL;
+    ii->obj = NULL;
+    gc_delReference(obj);
+    return obj;
 }