瀏覽代碼

feat: 可以根据EnvVar设定error的输出位置

SongZihuan 3 年之前
父節點
當前提交
f539519620
共有 5 個文件被更改,包括 28 次插入16 次删除
  1. 2 1
      include/core/env.h
  2. 1 0
      include/core/info/magic_func.h
  3. 1 1
      include/core/info/obj_api.h
  4. 3 2
      src/core/__env.h
  5. 21 12
      src/core/env.c

+ 2 - 1
include/core/env.h

@@ -10,7 +10,7 @@ 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);
+typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);
 DEFINE_DLC_SYMBOL(TopMsgProcessFunc);
 
 enum GcRunTime {
@@ -96,6 +96,7 @@ AFUN_CORE_EXPORT FilePath getActivityFile(af_Environment *env);
 AFUN_CORE_EXPORT FileLine getActivityLine(af_Environment *env);
 AFUN_CORE_EXPORT af_VarSpaceListNode *getRunVarSpaceList(af_Environment *env);
 AFUN_CORE_EXPORT int isCoreExit(af_Environment *env);
+AFUN_CORE_EXPORT bool getErrorStd(af_Environment *env);
 
 /* 消息 属性访问 */
 AFUN_CORE_EXPORT af_Object *getMsgNormalData(af_Message *msg);

+ 1 - 0
include/core/info/magic_func.h

@@ -22,5 +22,6 @@
 #define ev_exit_code SYS_NAME(exit-code)
 #define ev_argc SYS_NAME(argc)
 #define ev_argvx_prefix SYS_NAME(argv)
+#define ev_error_std SYS_NAME(error-std)
 
 #endif //AFUN_MAGIC_FUNC_H

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

@@ -20,7 +20,7 @@
 typedef void objectAPIFunc();  // 位于object.h (所有Object API函数指针都转换为该类型存储, 注: 具体调用类型参见下文)
 
 /* 顶层信号处理器函数 */
-typedef void TopMsgProcessFunc(af_Message *msg, bool is_gc, af_Environment *env);  // 位于env.h
+typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);  // 位于env.h
 
 /* 回调C函数 */
 typedef struct CallFuncInfo CallFuncInfo;

+ 3 - 2
src/core/__env.h

@@ -56,6 +56,7 @@ struct af_Core {  // 解释器核心
     af_EnvVar *prefix;
     af_EnvVar *exit_code_;  // 退出代码
     af_EnvVar *argc;  // 参数个数
+    af_EnvVar *error_std;  // Error输出的位置 0-stdout 其他-stderr
 };
 
 struct af_Message {
@@ -178,7 +179,7 @@ struct af_ActivityTrackBack {
     struct af_ActivityTrackBack *next;
 };
 
-typedef void TopMsgProcessFunc(af_Message *msg, bool is_gc, af_Environment *env);
+typedef void TopMsgProcessFunc(af_Message *msg, bool is_top, af_Environment *env);
 NEW_DLC_SYMBOL(TopMsgProcessFunc, TopMsgProcessFunc);
 
 struct af_TopMsgProcess {  // 顶层msg处理器
@@ -266,7 +267,7 @@ AFUN_CORE_NO_EXPORT void pushLiteralData(char *data, af_Environment *env);
 AFUN_CORE_NO_EXPORT bool checkLiteralCode(char *literal, char **func, bool *in_protect, af_Environment *env);
 
 /* 顶层消息处理器 处理函数 */
-AFUN_CORE_NO_EXPORT void runTopMessageProcess(bool is_gc, af_Environment *env);
+AFUN_CORE_NO_EXPORT void runTopMessageProcess(bool is_top, af_Environment *env);
 
 /* 消息 创建与释放 */
 AFUN_CORE_NO_EXPORT void freeAllMessage(af_Message *msg);

+ 21 - 12
src/core/env.c

@@ -64,9 +64,9 @@ static void fprintfNoteStderr(char *note);
 static void fprintfNoteStdout(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 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);
+static void mp_IMPORT(af_Message *msg, bool is_top, af_Environment *env);
 
 /* 变量检查函数 */
 static bool isInfixFunc(af_Code *code, af_Environment *env);
@@ -85,6 +85,7 @@ static af_Core *makeCore(enum GcRunTime grt, af_Environment *env) {
     core->gc_count = setEnvVarNumber_(ev_gccount, 0, env);
     core->exit_code_ = setEnvVarNumber_(ev_exit_code, 0, env);
     core->argc = setEnvVarNumber_(ev_argc, 0, env);
+    core->error_std = setEnvVarNumber_(ev_error_std, 0, env);
 
     core->status = core_creat;
     core->protect = makeVarSpaceByCore(NULL, 3, 3, 3, core);
@@ -683,27 +684,31 @@ int32_t *findEnvVarNumber(char *name, af_Environment *env) {
     return NULL;
 }
 
-static void mp_NORMAL(af_Message *msg, bool is_gc, af_Environment *env) {
+static void mp_NORMAL(af_Message *msg, bool is_top, af_Environment *env) {
     if (msg->msg == NULL || *(af_Object **)msg->msg == NULL) {
         writeErrorLog(aFunCoreLogger, "NORMAL msg: %p error", msg->msg);
         return;
     }
     gc_delReference(*(af_Object **)msg->msg);
-    if (!is_gc)
+    if (is_top)
         writeDebugLog(aFunCoreLogger, "NORMAL Point: %p", *(af_Object **)msg->msg);
 }
 
-static void mp_ERROR(af_Message *msg, bool is_gc, af_Environment *env) {
+static void mp_ERROR(af_Message *msg, bool is_top, af_Environment *env) {
     if (msg->msg == NULL || *(af_ErrorInfo **)msg->msg == NULL) {
         writeErrorLog(aFunCoreLogger, "ERROR msg: %p error", msg->msg);
         return;
     }
-    if (!is_gc)
-        fprintfErrorInfoStdout(*(af_ErrorInfo **)msg->msg);  // TODO-szh 获取EnvVar, 设定输出的位置 (stdout/stderr)
+    if (is_top) {
+        if (env->core->error_std->num == 0)
+            fprintfErrorInfoStdout(*(af_ErrorInfo **) msg->msg);
+        else
+            fprintfErrorInfoStderr(*(af_ErrorInfo **) msg->msg);
+    }
     freeErrorInfo(*(af_ErrorInfo **)msg->msg);
 }
 
-static void mp_IMPORT(af_Message *msg, bool is_gc, af_Environment *env) {
+static void mp_IMPORT(af_Message *msg, bool is_top, af_Environment *env) {
     if (msg->msg == NULL || *(af_ImportInfo **)msg->msg == NULL) {
         writeErrorLog(aFunCoreLogger, "IMPORT msg: %p error", msg->msg);
         return;
@@ -1223,12 +1228,12 @@ int setFuncActivityToNormal(af_Environment *env){  // 获取函数的函数体
  * 函数名: runTopMessageProcess
  * 目标: 运行顶层信息处理器
  */
-void runTopMessageProcess(bool is_gc, af_Environment *env) {
+void runTopMessageProcess(bool is_top, af_Environment *env) {
     af_Message **pmsg = &env->activity->msg_down;
     while (*pmsg != NULL) {
         af_TopMsgProcess *mp = findTopMsgProcessFunc((*pmsg)->type, env);
         if (mp != NULL) {
-            GET_SYMBOL(mp->func)(*pmsg, is_gc, env);
+            GET_SYMBOL(mp->func)(*pmsg, is_top, env);
             *pmsg = freeMessage(*pmsg);
         } else
             pmsg = &((*pmsg)->next);
@@ -1288,7 +1293,7 @@ void popActivity(bool is_normal, af_Message *msg, af_Environment *env) {
         freeMark(env->activity);  // 遇到非正常退出时, 释放`mark`
 
     if (env->activity->type == act_top || env->activity->type == act_gc) // 顶层或gc层
-        runTopMessageProcess((env->activity->type == act_gc), env);
+        runTopMessageProcess((env->activity->type == act_top), env);
     else {
         connectMessage(&(env->activity->msg_down), env->activity->prev->msg_down);
         env->activity->prev->msg_down = env->activity->msg_down;
@@ -1701,3 +1706,7 @@ int isCoreExit(af_Environment *env) {
         return -1;
     return 0;
 }
+
+bool getErrorStd(af_Environment *env) {
+    return env->core->error_std->num;  // true-stderr, false-stdout
+}