Sfoglia il codice sorgente

refactor & feat: 提取Environment模块

SongZihuan 3 anni fa
parent
commit
40d31fa687

+ 2 - 1
include/core/aFuncore.h

@@ -9,6 +9,7 @@
 #include "env-var.h"
 #include "value.h"
 #include "var.h"
-#include "activation.h"
+#include "core-activation.h"
+#include "core-exception.h"
 
 #endif //AFUN_AFUNCORE_H

+ 7 - 6
include/core/activation.h → include/core/core-activation.h

@@ -1,10 +1,11 @@
-#ifndef AFUN_ACTIVATION_H
-#define AFUN_ACTIVATION_H
+#ifndef AFUN_CORE_ACTIVATION_H
+#define AFUN_CORE_ACTIVATION_H
 #include "aFuntool.h"
 #include "aFunCoreExport.h"
+#include "msg.h"
+#include "code.h"
 #include "value.h"
 #include "var.h"
-#include "msg.h"
 
 namespace aFuncore {
     class Inter;
@@ -99,7 +100,7 @@ namespace aFuncore {
     };
 }
 
-#include "activation.inline.h"
-#include "activation.template.h"
+#include "core-activation.inline.h"
+#include "core-activation.template.h"
 
-#endif //AFUN_ACTIVATION_H
+#endif //AFUN_CORE_ACTIVATION_H

+ 4 - 4
include/core/activation.inline.h → include/core/core-activation.inline.h

@@ -1,7 +1,7 @@
-#ifndef AFUN_ACTIVATION_INLINE_H
-#define AFUN_ACTIVATION_INLINE_H
+#ifndef AFUN_CORE_ACTIVATION_INLINE_H
+#define AFUN_CORE_ACTIVATION_INLINE_H
 
-#include "activation.h"
+#include "core-activation.h"
 
 namespace aFuncore {
     inline void Activation::endRun() {
@@ -45,4 +45,4 @@ namespace aFuncore {
     }
 }
 
-#endif //AFUN_ACTIVATION_INLINE_H
+#endif //AFUN_CORE_ACTIVATION_INLINE_H

+ 4 - 4
include/core/activation.template.h → include/core/core-activation.template.h

@@ -1,6 +1,6 @@
-#ifndef AFUN_ACTIVATION_TEMPLATE_H
-#define AFUN_ACTIVATION_TEMPLATE_H
-#include "activation.h"
+#ifndef AFUN_CORE_ACTIVATION_TEMPLATE_H
+#define AFUN_CORE_ACTIVATION_TEMPLATE_H
+#include "core-activation.h"
 
 namespace aFuncore {
     template <typename Callable, typename...T>
@@ -10,4 +10,4 @@ namespace aFuncore {
     }
 }
 
-#endif //AFUN_ACTIVATION_TEMPLATE_H
+#endif //AFUN_CORE_ACTIVATION_TEMPLATE_H

+ 16 - 0
include/core/core-exception.h

@@ -0,0 +1,16 @@
+#ifndef AFUN_CORE_EXCEPTION_H
+#define AFUN_CORE_EXCEPTION_H
+#include "iostream"
+
+namespace aFuncore {
+    class EnvironmentDestructException : public std::exception {
+        constexpr static const char *message = "Environment Destruct Error";
+    public:
+        inline virtual const char *what();
+    };
+
+}
+
+#include "core-exception.inline.h"
+
+#endif //AFUN_CORE_EXCEPTION_H

+ 11 - 0
include/core/core-exception.inline.h

@@ -0,0 +1,11 @@
+#ifndef AFUN_CORE_EXCEPTION_INLINE_H
+#define AFUN_CORE_EXCEPTION_INLINE_H
+#include "core-exception.h"
+
+namespace aFuncore {
+    inline const char *EnvironmentDestructException::what() {
+        return message;
+    }
+}
+
+#endif //AFUN_CORE_EXCEPTION_INLINE_H

+ 47 - 39
include/core/inter.h

@@ -6,14 +6,50 @@
 
 #include "code.h"
 #include "env-var.h"
+#include "msg.h"
 
-#include "value.h"
-#include "var.h"
-#include "activation.h"
+namespace aFuncore {
+    class Activation;
+    class Var;
+    class ProtectVarSpace;
+    class VarSpace;
+    class VarList;
+    class Object;
+
+    class AFUN_CORE_EXPORT Environment {
+        friend class Object;
+        friend class Var;
+        friend class VarSpace;
+        friend class Inter;
 
+    public:
+        Environment();
+        ~Environment() noexcept(false);
+        Environment(Environment &) = delete;
+        Environment &operator=(Environment &) = delete;
+
+        inline size_t operator++();
+        inline size_t operator--();
+        inline size_t operator++(int);
+        inline size_t operator--(int);
+
+    private:
+        Object *obj;
+        Var *var;
+        VarSpace *varspace;
+
+        ProtectVarSpace *protect;  // 保护变量空间
+        VarSpace *global;  // 全局变量空间
+        VarList *global_varlist;  // global + protect
+        EnvVarSpace envvar;
+
+        size_t reference;  // 引用计数
+    };
 
-namespace aFuncore {
     class AFUN_CORE_EXPORT Inter {
+        friend class Activation;
+
+        struct LiteralRegex;
     public:
         typedef enum InterStatus {
             inter_creat = 0,
@@ -43,16 +79,16 @@ namespace aFuncore {
         constexpr static const char *E_PREFIX = "$`'";  /* NOLINT element前缀 */
         constexpr static const char *B_PREFIX = "$`'%^&<?>";  /* NOLINT block前缀 */
 
-        explicit Inter(int argc=0, char **argv=nullptr, ExitMode em=em_activity);
-        Inter(const Inter &base_inter, ExitMode em=em_activity);
+        explicit Inter(Environment &env_, int argc = 0, char **argv = nullptr, ExitMode em = em_activity);
+        Inter(const Inter &base_inter, ExitMode em = em_activity);
         ~Inter();
-        Inter &operator=(const Inter &)=delete;
+        Inter &operator=(const Inter &) = delete;
 
         void enable();
 
         [[nodiscard]] inline InterStatus getStatus() const;
         [[nodiscard]] inline bool isExit() const;
-
+        [[nodiscard]] inline Environment &getEnvironment();
         [[nodiscard]] inline ProtectVarSpace *getProtectVarSpace() const;
         [[nodiscard]] inline VarSpace *getGlobalVarSpace() const;
         [[nodiscard]] inline VarList *getGlobalVarlist() const;
@@ -67,51 +103,23 @@ namespace aFuncore {
         bool runCode(Code *code);
 
     private:
-        /* 解释器原信息记录 */
         InterStatus status;
 
-        /* GC 记录器 */
-        struct GcRecord;
-        struct GcRecord *gc;
-        [[nodiscard]] inline struct GcRecord *getGcRecord() const;
-        friend Object::Object(const std::string &type_, Inter &inter_);
-        friend Var::Var(Object *data_, Inter &inter_);
-        friend VarSpace::VarSpace(Inter &inter_);
+        Environment &env;
 
-        /* 运行相关 */
-        ProtectVarSpace *protect;  // 保护变量空间
-        VarSpace *global;  // 全局变量空间
-        VarList *global_varlist;  // global + protect
         Activation *activation;  // 活动记录
+
         InterMessage out;
         InterMessage in;
 
-        inline void pushActivation(Activation *new_activation);
-        friend Activation::Activation(Inter &inter_);
-
-        struct LiteralRegex;
         std::list<LiteralRegex> literal;
 
-        /* 配置信息记录器 */
-        EnvVarSpace &envvar;
-
-        /* 线程信息 */
-    public:
-        const bool is_derive;  // 是否派生
-        Inter &base;  // 主线程
-
-    private:
         Object *result;  // 线程执行的结果
-        std::list<Inter *> *son_inter;  // 派生线程链表, 由主线程负责管理
 
         ExitFlat exit_flat;  // 外部设置退出
         ExitMode exit_mode;  // 退出模式
-    };
 
-    struct Inter::GcRecord {
-        Object *obj;
-        Var *var;
-        VarSpace *varspace;
+        inline void pushActivation(Activation *new_activation);
     };
 
     struct Inter::LiteralRegex {

+ 22 - 6
include/core/inter.inline.h

@@ -3,8 +3,8 @@
 #include "inter.h"
 
 namespace aFuncore {
-    inline Inter::GcRecord *Inter::getGcRecord() const {
-        return gc;
+    inline Environment &Inter::getEnvironment() {
+        return env;
     }
 
     inline void Inter::pushActivation(Activation *new_activation) {
@@ -20,15 +20,15 @@ namespace aFuncore {
     }
 
     inline ProtectVarSpace *Inter::getProtectVarSpace() const {
-        return protect;
+        return env.protect;
     }
 
     inline VarSpace *Inter::getGlobalVarSpace() const {
-        return global;
+        return env.global;
     }
 
     inline VarList *Inter::getGlobalVarlist() const {
-        return global_varlist;
+        return env.global_varlist;
     }
 
     inline Activation *Inter::getActivation() const {
@@ -36,7 +36,23 @@ namespace aFuncore {
     }
 
     inline EnvVarSpace &Inter::getEnvVarSpace() {
-        return envvar;
+        return env.envvar;
+    }
+
+    inline size_t Environment::operator++(){
+        return ++reference;
+    }
+
+    inline size_t Environment::operator--(){
+        return --reference;
+    }
+
+    inline size_t Environment::operator++(int){
+        return reference++;
+    }
+
+    inline size_t Environment::operator--(int){
+        return reference--;
     }
 }
 

+ 6 - 6
include/core/value.h

@@ -5,16 +5,16 @@
 #include "list"
 #include "gc.h"
 #include "code.h"
+#include "inter.h"
 
 namespace aFuncore {
-    class Inter;
-
     class AFUN_CORE_EXPORT Object : public GcObject<class Object> {
     public:
-        Inter &inter;
+        Environment &env;
         const std::string type;  // 标识 Object 的字符串
 
-        explicit Object(const std::string &type_, Inter &inter_);
+        Object(std::string type_, Inter &inter);
+        Object(std::string type_, Environment &env_);
         ~Object() override = default;
     };
 
@@ -48,14 +48,14 @@ namespace aFuncore {
     class AFUN_CORE_EXPORT Literaler : public Object {
     public:
         inline Literaler(const std::string &type_, Inter &inter_);
-        virtual void getObject(const std::string &literal, char prefix) = 0;
+        virtual void getObject(const std::string &literal, char prefix, Inter &inter) = 0;
     };
 
     class AFUN_CORE_EXPORT CallBackVar : public Object {
     public:
         inline CallBackVar(const std::string &type_, Inter &inter_);
         virtual inline bool isCallBack();
-        virtual void callBack() = 0;
+        virtual void callBack(Inter &inter) = 0;
     };
 };
 

+ 10 - 7
include/core/var.h

@@ -1,19 +1,20 @@
 #ifndef AFUN_VAR_H
 #define AFUN_VAR_H
+#include <list>
 #include "aFuntool.h"
 #include "aFunCoreExport.h"
 #include "gc.h"
-#include <list>
+#include "inter.h"
 
 namespace aFuncore {
-    class Inter;
     class Object;
 
     class AFUN_CORE_EXPORT Var : public GcObject<class Var> {
     public:
-        Inter &inter;
+        Environment &env;
 
-        Var(Object *data_, Inter &inter_);
+        Var(Object *data_, Inter &inter);
+        Var(Object *data_, Environment &env_);
         ~Var() override = default;
 
         [[nodiscard]] inline virtual Object *getData();
@@ -32,9 +33,10 @@ namespace aFuncore {
             vof_fail = 3,  // 存在其他错误
         } VarOperationFlat;
 
-        Inter &inter;
+        Environment &env;
 
-        explicit VarSpace(Inter &inter_);
+        explicit VarSpace(Inter &inter);
+        explicit VarSpace(Environment &env_);
         ~VarSpace() override;
 
         template <typename Callable,typename...T>
@@ -63,7 +65,8 @@ namespace aFuncore {
 
     class AFUN_CORE_EXPORT ProtectVarSpace : public VarSpace {
     public:
-        explicit inline ProtectVarSpace(Inter &inter_);
+        explicit inline ProtectVarSpace(Inter &inter);
+        explicit inline ProtectVarSpace(Environment &env_);
 
         [[nodiscard]] inline bool getProtect() const;
         inline bool setProtect(bool protect);

+ 5 - 1
include/core/var.inline.h

@@ -20,7 +20,11 @@ namespace aFuncore {
         return ret ? ret->getData() : nullptr;
     }
 
-    inline ProtectVarSpace::ProtectVarSpace(Inter &inter_) : VarSpace(inter_), is_protect{false} {
+    inline ProtectVarSpace::ProtectVarSpace(Inter &inter) : VarSpace(inter), is_protect{false} {
+
+    }
+
+    inline ProtectVarSpace::ProtectVarSpace(Environment &env_) : VarSpace(env_), is_protect{false} {
 
     }
 

+ 1 - 1
include/tool/aFuntool.h

@@ -10,7 +10,7 @@
 #include "tool.h"
 #include "aFunToolExport.h"
 
-#include "exception.h"
+#include "tool-exception.h"
 #include "stdio_.h"
 #include "exit_.h"
 #include "byte.h"

+ 4 - 4
include/tool/exception.h → include/tool/tool-exception.h

@@ -1,5 +1,5 @@
-#ifndef AFUN_EXCEPTION_H
-#define AFUN_EXCEPTION_H
+#ifndef AFUN_TOOL_EXCEPTION_H
+#define AFUN_TOOL_EXCEPTION_H
 #include "tool.h"
 
 namespace aFuntool {
@@ -26,6 +26,6 @@ namespace aFuntool {
     };
 }
 
-#include "exception.inline.h"
+#include "tool-exception.inline.h"
 
-#endif //AFUN_EXCEPTION_H
+#endif //AFUN_TOOL_EXCEPTION_H

+ 4 - 4
include/tool/exception.inline.h → include/tool/tool-exception.inline.h

@@ -1,7 +1,7 @@
-#ifndef AFUN_EXCEPTION_INLINE_H
-#define AFUN_EXCEPTION_INLINE_H
+#ifndef AFUN_TOOL_EXCEPTION_INLINE_H
+#define AFUN_TOOL_EXCEPTION_INLINE_H
 
-#include "exception.h"
+#include "tool-exception.h"
 
 namespace aFuntool {
     inline FileOpenException::FileOpenException(ConstFilePath file) {
@@ -29,4 +29,4 @@ namespace aFuntool {
     }
 }
 
-#endif //AFUN_EXCEPTION_INLINE_H
+#endif //AFUN_TOOL_EXCEPTION_INLINE_H

+ 3 - 4
src/core/activation.cpp

@@ -1,8 +1,7 @@
-#include "value.h"
+#include "core-activation.h"
 #include "inter.h"
 #include "init.h"
 #include "msg.h"
-#include "var.h"
 #include "code.h"
 
 using namespace aFuncore;
@@ -79,7 +78,7 @@ void Activation::runCodeElement(Code *code) {
             obj = varlist->findObject(literaler_name);
         auto literaler = dynamic_cast<Literaler *>(obj);
         if (literaler != nullptr)
-            literaler->getObject(code->getElement(), code->getPrefix());
+            literaler->getObject(code->getElement(), code->getPrefix(), inter);
         else
             down.pushMessage(new ErrorMessage("TypeError", "Error type of literal.", this));
     } else {
@@ -88,7 +87,7 @@ void Activation::runCodeElement(Code *code) {
         if (obj != nullptr) {
             auto cbv = dynamic_cast<CallBackVar *>(obj);
             if (cbv != nullptr && cbv->isCallBack())
-                cbv->callBack();
+                cbv->callBack(inter);
             else
                 down.pushMessage(new NormalMessage(obj));
         } else

+ 51 - 46
src/core/inter.cpp

@@ -1,87 +1,58 @@
 #include "inter.h"
-#include "activation.h"
+#include "core-activation.h"
 #include "init.h"
-#include "env-var.h"
-#include "var.h"
 #include "msg.h"
+#include "core-exception.h"
 
 using namespace aFuncore;
 using namespace aFuntool;
 
-Inter::Inter(int argc, char **argv, ExitMode em)
-    : base{*this}, is_derive{false}, out{}, in{}, envvar{*(new EnvVarSpace())} {
+Inter::Inter(Environment &env_, int argc, char **argv, ExitMode em)
+    : out{}, in{}, env{env_} {
     status = inter_creat;
 
-    gc = new GcRecord;
-    gc->obj = nullptr;
-    gc->var = nullptr;
-    gc->varspace = nullptr;
-
     activation = nullptr;
 
-    envvar.setNumber("sys:gc-runtime", 2);
-    envvar.setString("sys:prefix", "''");  // 引用,顺序执行
-    envvar.setNumber("sys:exit-code", 0);
-    envvar.setNumber("sys:argc", argc);
-    envvar.setNumber("sys:error_std", 0);
+    env.envvar.setNumber("sys:gc-runtime", 2);
+    env.envvar.setString("sys:prefix", "''");  // 引用,顺序执行
+    env.envvar.setNumber("sys:exit-code", 0);
+    env.envvar.setNumber("sys:argc", argc);
+    env.envvar.setNumber("sys:error_std", 0);
 
     for (int i = 0; i < argc; i++) {
         char buf[20];
         snprintf(buf, 10, "sys:arg%d", i);
-        envvar.setString(buf, argv[i]);
+        env.envvar.setString(buf, argv[i]);
     }
 
     result = nullptr;
-    son_inter = new std::list<Inter *>;
 
     exit_flat = ef_none;
     exit_mode = em;
 
-    protect = new ProtectVarSpace(*this);  // 放到最后
-    global = new VarSpace(*this);  // 放到最后
-    global_varlist = new VarList(protect);
-    global_varlist->push(global);
-
     status = inter_init;
+    env++;
 }
 
 Inter::Inter(const Inter &base_inter, ExitMode em)
-        : base{base_inter.base}, is_derive{true}, out{}, in{}, envvar{base_inter.base.envvar} {
+        : out{}, in{}, env{base_inter.env} {
     status = inter_creat;
 
-    gc = base.gc;
-
     activation = nullptr;
 
-    for(auto &i : base.literal)
+    for(auto &i : base_inter.literal)
         literal.push_back(i);
 
     result = nullptr;
-    son_inter = nullptr;
-    base.son_inter->push_back(this);
-
     exit_flat = ef_none;
     exit_mode = em;
 
-    protect = base.protect;  // 放到最后
-    global = base.global;  // 放到最后
-    global_varlist = base.global_varlist;
-
-    status = inter_init;
+    status = inter_normal;
+    env++;
 }
 
 Inter::~Inter(){
-    if (!is_derive) {
-        delete global_varlist;
-
-        Object::destruct(gc->obj);
-        Var::destruct(gc->var);
-        VarSpace::destruct(gc->varspace);
-
-        delete gc;
-        delete son_inter;
-        delete &envvar;
-    }
+    env--;
 }
 
 /**
@@ -89,7 +60,7 @@ Inter::~Inter(){
  */
 void Inter::enable(){
     if (status == inter_init) {
-        protect->setProtect(true);
+        env.protect->setProtect(true);
         status = inter_normal;
     }
 }
@@ -203,3 +174,37 @@ bool Inter::pushLiteral(const std::string &pattern, const std::string &literaler
     return true;
 }
 
+Environment::Environment() : envvar{} {
+    obj = nullptr;
+    var = nullptr;
+    varspace = nullptr;
+
+    protect = new ProtectVarSpace(*this);  // 放到最后
+    global = new VarSpace(*this);  // 放到最后
+    global_varlist = new VarList(protect);
+    global_varlist->push(global);
+
+    reference = 0;
+}
+
+Environment::~Environment() noexcept(false) {
+    if (reference != 0)
+        throw EnvironmentDestructException();
+
+    if (global_varlist == nullptr)
+        return;
+
+    delete global_varlist;
+
+    Object::destruct(obj);
+    Var::destruct(var);
+    VarSpace::destruct(varspace);
+
+    obj = nullptr;
+    var = nullptr;
+    varspace = nullptr;
+
+    protect = nullptr;  // 放到最后
+    global = nullptr;  // 放到最后
+    global_varlist = nullptr;
+}

+ 1 - 1
src/core/msg.cpp

@@ -1,5 +1,5 @@
 #include "msg.h"
-#include "activation.h"
+#include "core-activation.h"
 #include "inter.h"
 #include "env-var.h"
 

+ 8 - 3
src/core/value.cpp

@@ -4,7 +4,12 @@
 using namespace aFuncore;
 using namespace aFuntool;
 
-aFuncore::Object::Object(const std::string &type_, Inter &inter_)
-        : type{type_}, inter{inter_.base} {
-    this->addObject(inter.getGcRecord()->obj);
+aFuncore::Object::Object(std::string type_, Inter &inter)
+        : type{std::move(type_)}, env{inter.getEnvironment()} {
+    this->addObject(env.obj);
 }
+
+aFuncore::Object::Object(std::string type_, Environment &env_)
+        : type{std::move(type_)}, env{env_} {
+    this->addObject(env.obj);
+}

+ 13 - 5
src/core/var.cpp

@@ -5,12 +5,20 @@ using namespace aFuncore;
 using namespace aFuntool;
 
 
-aFuncore::Var::Var(Object *data_, Inter &inter_) : data{data_}, inter{inter_.base} {
-    addObject(inter.getGcRecord()->var);
+aFuncore::Var::Var(Object *data_, Inter &inter) : data{data_}, env{inter.getEnvironment()} {
+    addObject(env.var);
 }
 
-aFuncore::VarSpace::VarSpace(Inter &inter_) : count{0}, var{}, inter{inter_.base} {
-    addObject(inter.getGcRecord()->varspace);
+aFuncore::Var::Var(Object *data_, Environment &env_) : data{data_}, env{env_} {
+    addObject(env.var);
+}
+
+aFuncore::VarSpace::VarSpace(Inter &inter) : count{0}, var{}, env{inter.getEnvironment()} {
+    addObject(env.varspace);
+}
+
+aFuncore::VarSpace::VarSpace(Environment &env_) : count{0}, var{}, env{env_} {
+    addObject(env.varspace);
 }
 
 /**
@@ -42,7 +50,7 @@ VarSpace::VarOperationFlat aFuncore::VarSpace::defineVar(const std::string &name
     }
     (*tmp) = new VarCup;
     (*tmp)->name = name;
-    (*tmp)->var = new Var(data, inter);
+    (*tmp)->var = new Var(data, env);
     count++;
     return vof_success;
 }

+ 1 - 1
src/tool/log.cpp

@@ -17,7 +17,7 @@
 #include <cstring>
 #include "tool.h"
 #include "log.h"
-#include "exception.h"
+#include "tool-exception.h"
 #include "log-macro.h"
 #include "time_.h"
 #include "file.h"

+ 1 - 1
src/tool/md5.cpp

@@ -9,7 +9,7 @@
 #include "tool.h"
 #include "md5.h"
 #include "file.h"
-#include "exception.h"
+#include "tool-exception.h"
 #include "__md5.h"
 
 using namespace aFuntool;

+ 1 - 1
src/tool/regex.cpp

@@ -2,7 +2,7 @@
 #include <cstring>
 #include <utility>
 #include "tool.h"
-#include "exception.h"
+#include "tool-exception.h"
 #include "file.h"
 
 #define PCRE2_CODE_UNIT_WIDTH 8

+ 4 - 3
test/src/run-code.cpp

@@ -60,7 +60,7 @@ public:
         Code::destruct(func_code);
     }
 
-    void getObject(const std::string &literal, char prefix) override {
+    void getObject(const std::string &literal, char prefix, Inter &inter) override {
         printf_stdout(0, "Literaler1: %s %c\n", literal.c_str(), prefix == NUL ? '-' : prefix);
         new ExeActivation(func_code, inter);
     }
@@ -78,14 +78,15 @@ public:
         Code::destruct(func_code);
     }
 
-    void callBack() override {
+    void callBack(Inter &inter) override {
         printf_stdout(0, "CallBackVar callback\n");
         new ExeActivation(func_code, inter);
     }
 };
 
 int main() {
-    Inter inter {};
+    Environment env {};
+    Inter inter {env};
 
     auto obj = new Object("Object", inter);
     inter.getGlobalVarlist()->defineVar("test-var", obj);