Kaynağa Gözat

refactor & feat: varlist改为activation的成员

SongZihuan 3 yıl önce
ebeveyn
işleme
e592aa3c31

+ 0 - 1
include/core/aFuncore.h

@@ -8,7 +8,6 @@
 #include "inter.h"
 #include "object.h"
 #include "object-value.h"
-#include "varlist.h"
 #include "core-activation.h"
 #include "core-exception.h"
 #include "reader.h"

+ 31 - 3
include/core/core-activation.h

@@ -5,7 +5,6 @@
 #include "msg.h"
 #include "code.h"
 #include "object-value.h"
-#include "varlist.h"
 
 namespace aFuncore {
     class Inter;
@@ -18,6 +17,34 @@ namespace aFuncore {
             as_end_run = 2,
         } ActivationStatus;
 
+        class AFUN_CORE_EXPORT VarList {
+        public:
+            inline explicit VarList();
+            virtual ~VarList();
+            VarList(VarList &&new_varlist);
+            VarList &operator=(VarList &&new_varlist);
+            VarList(const VarList &) = delete;
+            VarList &operator=(const VarList &) = delete;
+
+            void clear();
+            void connect(VarList &new_varlist);
+            inline void push(VarSpace *varspace_);
+            inline size_t count();
+
+            template <typename Callable,typename...T>
+            void forEach(Callable func, T...arg);
+
+            [[nodiscard]] virtual Var *findVar(const std::string &name);
+            virtual bool defineVar(const std::string &name, Object *data);
+            virtual bool defineVar(const std::string &name, Var *data);
+            virtual bool setVar(const std::string &name, Object *data);
+            virtual bool delVar(const std::string &name);
+            [[nodiscard]] inline Object *findObject(const std::string &name);
+
+        private:
+            std::list<VarSpace *> varspace;
+        };
+
         Inter &inter;
 
         explicit Activation(Inter &inter_);
@@ -28,7 +55,7 @@ namespace aFuncore {
         virtual void runCode(const Code::ByteCode *code);
         virtual void endRun();
 
-        [[nodiscard]] inline VarList *getVarlist() const;
+        [[nodiscard]] inline VarList &getVarlist();
         [[nodiscard]] inline UpMessage &getUpStream();
         [[nodiscard]] inline DownMessage &getDownStream();
 
@@ -36,7 +63,7 @@ namespace aFuncore {
         [[nodiscard]] inline  const aFuntool::FilePath &getFilePath() const;
 
     protected:
-        VarList *varlist;
+        VarList varlist;
 
         UpMessage up;
         DownMessage down;
@@ -101,5 +128,6 @@ namespace aFuncore {
 }
 
 #include "core-activation.inline.h"
+#include "core-activation.template.h"
 
 #endif //AFUN_CORE_ACTIVATION_H

+ 29 - 1
include/core/core-activation.inline.h

@@ -4,7 +4,7 @@
 #include "core-activation.h"
 
 namespace aFuncore {
-    inline VarList *Activation::getVarlist() const{
+    inline Activation::VarList &Activation::getVarlist(){
         return varlist;
     }
 
@@ -24,6 +24,34 @@ namespace aFuncore {
         return path;
     }
 
+    inline Activation::VarList::VarList() : varspace{} {
+
+    }
+
+    inline Activation::VarList::VarList(VarList &&new_varlist) : varspace{std::move(new_varlist.varspace)} {
+
+    }
+
+    inline Activation::VarList &Activation::VarList::operator=(VarList &&new_varlist) {
+        clear();
+        varspace = std::move(new_varlist.varspace);
+        return *this;
+    }
+
+    inline void Activation::VarList::push(VarSpace *varspace_) {
+        varspace_->addReference();
+        varspace.push_front(varspace_);
+    }
+
+    inline size_t Activation::VarList::count() {
+        return varspace.size();
+    }
+
+    inline Object *Activation::VarList::findObject(const std::string &name) {
+        Var *var = findVar(name);
+        return var ? var->getData() : nullptr;
+    }
+
     inline ExeActivation::ExeActivation(const Code &code, Inter &inter_) : Activation(inter_), start{code.getByteCode()}, next{code.getByteCode()} {
 
     }

+ 14 - 0
include/core/core-activation.template.h

@@ -0,0 +1,14 @@
+#ifndef AFUN_CORE_ACTIVATION_TEMPLATE_H
+#define AFUN_CORE_ACTIVATION_TEMPLATE_H
+
+#include "core-activation.h"
+
+namespace aFuncore {
+    template <typename Callable, typename...T>
+    void Activation::VarList::forEach(Callable func, T...arg) {
+        for (auto &vs : varspace)
+            func(vs, arg...);
+    }
+}
+
+#endif //AFUN_CORE_ACTIVATION_TEMPLATE_H

+ 0 - 5
include/core/inter.h

@@ -15,7 +15,6 @@ namespace aFuncore {
     class Var;
     class ProtectVarSpace;
     class VarSpace;
-    class VarList;
     class Object;
     class Inter;
 
@@ -45,8 +44,6 @@ namespace aFuncore {
 
     protected:  // 位于 mutex 之下
         ProtectVarSpace *const protect;  // 保护变量空间
-        VarSpace *const global;  // 全局变量空间
-        VarList *const global_varlist;  // global + protect
         EnvVarSpace envvar;
     };
 
@@ -85,8 +82,6 @@ namespace aFuncore {
         [[nodiscard]] inline bool isInterExit() const;
         [[nodiscard]] inline Environment &getEnvironment();
         [[nodiscard]] inline ProtectVarSpace *getProtectVarSpace() const;
-        [[nodiscard]] inline VarSpace *getGlobalVarSpace() const;
-        [[nodiscard]] inline VarList *getGlobalVarlist() const;
         [[nodiscard]] inline const std::list<Activation *> &getStack() const;
         [[nodiscard]] inline Activation *getActivation() const;
         [[nodiscard]] bool checkLiteral(const std::string &element) const;

+ 0 - 8
include/core/inter.inline.h

@@ -41,14 +41,6 @@ namespace aFuncore {
         return env.protect;
     }
 
-    inline VarSpace *Inter::getGlobalVarSpace() const {
-        return env.global;
-    }
-
-    inline VarList *Inter::getGlobalVarlist() const {
-        return env.global_varlist;
-    }
-
     inline const std::list<Activation *> &Inter::getStack() const {
         return stack;
     }

+ 0 - 50
include/core/varlist.h

@@ -1,50 +0,0 @@
-#ifndef AFUN_VARLIST_H
-#define AFUN_VARLIST_H
-#include <list>
-#include <unordered_map>
-#include <mutex>
-#include "aFuntool.h"
-#include "aFunCoreExport.h"
-#include "object-value.h"
-#include "inter.h"
-
-namespace aFuncore {
-    class AFUN_CORE_EXPORT VarList {
-    public:
-        explicit inline VarList() = default;
-        explicit VarList(VarList *varlist);
-        explicit VarList(VarSpace *varspace);
-        ~VarList() = default;
-        VarList(const VarList &) = delete;
-        VarList &operator=(const VarList &) = delete;
-
-        void connect(VarList *varlist);
-        inline void push(VarSpace *varspace_);
-
-        template <typename Callable,typename...T>
-        void forEach(Callable func, T...arg);
-
-        template <typename Callable,typename...T>
-        void forEachLock(Callable func, T...arg);
-
-        [[nodiscard]] virtual Var *findVar(const std::string &name);
-        virtual bool defineVar(const std::string &name, Object *data);
-        virtual bool defineVar(const std::string &name, Var *data);
-        virtual bool setVar(const std::string &name, Object *data);
-        virtual bool delVar(const std::string &name);
-        [[nodiscard]] inline Object *findObject(const std::string &name);
-
-        inline void GcLinkObject(std::queue<Object *> &queue);  /* 虽然不是GcObject, 但是也设定改函数便于将其包含的varspace快速压入queue中 */
-
-    protected:
-        std::mutex lock;
-
-    private:
-        std::list<VarSpace *> varspace;
-    };
-}
-
-#include "varlist.inline.h"
-#include "varlist.template.h"
-
-#endif //AFUN_VARLIST_H

+ 0 - 26
include/core/varlist.inline.h

@@ -1,26 +0,0 @@
-#ifndef AFUN_VARLIST_INLINE_H
-#define AFUN_VARLIST_INLINE_H
-#include "varlist.h"
-
-namespace aFuncore {
-    inline VarList::VarList(VarSpace *varspace) {
-        this->varspace.push_front(varspace);
-    }
-
-    inline void VarList::push(VarSpace *varspace_) {
-        std::unique_lock<std::mutex> mutex{lock};
-        varspace.push_front(varspace_);
-    }
-
-    inline Object *VarList::findObject(const std::string &name) {
-        Var *var = findVar(name);
-        return var ? var->getData() : nullptr;
-    }
-
-    inline void VarList::GcLinkObject(std::queue<Object *> &queue) {
-        for (auto var : varspace)
-            queue.push(var);
-    }
-}
-
-#endif //AFUN_VARLIST_INLINE_H

+ 0 - 25
include/core/varlist.template.h

@@ -1,25 +0,0 @@
-#ifndef AFUN_VARLIST_TEMPLATE_H
-#define AFUN_VARLIST_TEMPLATE_H
-
-#include "varlist.h"
-
-namespace aFuncore {
-    template <typename Callable, typename...T>
-    void VarList::forEach(Callable func, T...arg) {
-        std::unique_lock<std::mutex> mutex{lock};
-        for (auto &vs : varspace) {
-            mutex.unlock();
-            func(vs, arg...);
-            mutex.lock();
-        }
-    }
-
-    template <typename Callable, typename...T>
-    void VarList::forEachLock(Callable func, T...arg) {
-        std::unique_lock<std::mutex> mutex{lock};
-        for (auto &vs : varspace)
-            func(vs, arg...);
-    }
-}
-
-#endif //AFUN_VARLIST_TEMPLATE_H

+ 99 - 9
src/core/activation.cpp

@@ -16,14 +16,17 @@ namespace aFuncore {
             : inter{inter_}, line{0},
               up{inter_.activation == nullptr ? nullptr : &inter_.activation->up}, down{}{
         if (inter_.activation != nullptr) {
-            varlist = new VarList(inter_.activation->varlist);
+            varlist.connect(inter_.activation->varlist);
             line = inter_.activation->line;
             path = inter_.activation->path;
         } else {
-            varlist = new VarList();
-            varlist->connect(inter_.getGlobalVarlist());
+            auto global = new VarSpace(inter);
+            varlist.push(inter_.getProtectVarSpace());
+            varlist.push(global);
+            global->delReference();
             path = "";
         }
+
         inter.pushActivation(this);
     }
 
@@ -43,7 +46,6 @@ namespace aFuncore {
             down.joinMsg(inter.activation->down);
         else
             down.forEach(ActivationTopProgress, std::ref(inter), std::ref(*this));
-        delete varlist;
     }
 
     void Activation::endRun() {
@@ -89,15 +91,14 @@ namespace aFuncore {
             if (in_protect)
                 obj = inter.getProtectVarSpace()->findObject(literaler_name);
             else
-                obj = varlist->findObject(literaler_name);
+                obj = varlist.findObject(literaler_name);
             auto literaler = dynamic_cast<Literaler *>(obj);
             if (literaler != nullptr)
                 literaler->getObject(code->getElement(), code->getPrefix(), inter, *this);
             else
                 down.pushMessage("ERROR", new ErrorMessage("TypeError", "Error type of literal.", this));
         } else {
-            if (varlist != nullptr)
-                obj = varlist->findObject(code->getElement());
+            obj = varlist.findObject(code->getElement());
             if (obj != nullptr) {
                 auto cbv = dynamic_cast<CallBackVar *>(obj);
                 if (cbv != nullptr && cbv->isCallBack(inter, *this))
@@ -144,11 +145,100 @@ namespace aFuncore {
         return as_run;
     }
 
+    Activation::VarList::~VarList() {
+        for (auto &t : varspace)
+            t->delReference();
+    }
+
+    /**
+     * 访问变量
+     * @param name 变量名
+     * @return
+     */
+    Var *Activation::VarList::findVar(const std::string &name){
+        Var *ret = nullptr;
+        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == nullptr; tmp++)
+            ret = (*tmp)->findVar(name);
+        return ret;
+    }
+
+    /**
+     * 定义变量
+     * 若定义出现redefine则退出报错
+     * 若出现fail则跳到下一个变量空间尝试定义
+     * @param name 变量名
+     * @param data 变量(Object)
+     * @return
+     */
+    bool Activation::VarList::defineVar(const std::string &name, Object *data){
+        VarSpace::VarOperationFlat ret = VarSpace::vof_fail;
+        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_fail; tmp++)
+            ret = (*tmp)->defineVar(name, data);
+        return ret == VarSpace::vof_success;
+    }
+
+    /**
+     * 定义变量
+     * 若定义出现redefine则退出报错
+     * 若出现fail则跳到下一个变量空间尝试定义
+     * @param name 变量名
+     * @param data 变量(Var)
+     * @return
+     */
+    bool Activation::VarList::defineVar(const std::string &name, Var *data){
+        VarSpace::VarOperationFlat ret = VarSpace::vof_fail;
+        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_fail; tmp++)
+            ret = (*tmp)->defineVar(name, data);
+        return ret == VarSpace::vof_success;
+    }
+
+    /**
+     * 设置变量的值
+     * 若not_var则跳到下一个变量空间
+     * 若fail则结束
+     * @param name 变量名
+     * @param data 数据
+     * @return
+     */
+    bool Activation::VarList::setVar(const std::string &name, Object *data){
+        VarSpace::VarOperationFlat ret = VarSpace::vof_not_var;
+        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_not_var; tmp++)
+            ret = (*tmp)->setVar(name, data);
+        return ret == VarSpace::vof_success;
+    }
+
+    /**
+     * 删除变量
+     * 若not_var则跳到下一个变量空间
+     * 若fail则结束
+     * @param name
+     * @return
+     */
+    bool Activation::VarList::delVar(const std::string &name){
+        VarSpace::VarOperationFlat ret = VarSpace::vof_not_var;
+        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_not_var; tmp++)
+            ret = (*tmp)->delVar(name);
+        return ret == VarSpace::vof_success;
+    }
+
+    void Activation::VarList::clear(){
+        for (auto &t: varspace)
+            t->delReference();
+        varspace.clear();
+    }
+
+    void Activation::VarList::connect(VarList &new_varlist){
+        for (auto &t: new_varlist.varspace) {
+            t->addReference();
+            this->varspace.push_back(t);
+        }
+    }
+
     TopActivation::TopActivation(const Code &code, Inter &inter_) : ExeActivation(code, inter_), base{code} {
 
     }
 
-    FuncActivation::FuncActivation(Function *func_, Inter &inter_) : Activation(inter_), call{nullptr} {
+    FuncActivation::FuncActivation(Function *func_, Inter &inter_) : Activation(inter_), call{nullptr}, acl_begin{}, acl_end{} {
         on_tail = false;  // 跳过所有阶段
         status = func_get_func;
         func = func_;
@@ -189,7 +279,7 @@ namespace aFuncore {
                         if (var->getType() != Code::ByteCode::code_element || var->getPrefix() == quote ||
                             inter.checkLiteral(var->getElement()))
                             continue;
-                        Object *obj = varlist->findObject(var->getElement());
+                        Object *obj = varlist.findObject(var->getElement());
                         if (obj == nullptr || !dynamic_cast<Function *>(obj) ||
                             !dynamic_cast<Function *>(obj)->isInfix())
                             continue;

+ 1 - 5
src/core/inter.cpp

@@ -192,9 +192,7 @@ namespace aFuncore {
 
     Environment::Environment(int argc, char **argv)
         : reference{0}, gc_inter{*(new Inter(*this))},
-          protect{new ProtectVarSpace(*this)}, global{new VarSpace(*this)},
-          global_varlist{new VarList(protect)}, destruct{false} {
-        global_varlist->push(global);
+          protect{new ProtectVarSpace(*this)}, destruct{false} {
         /* 生成 gc_inter 后, reference == 1 */
 
         envvar.setNumber("sys:gc-runtime", 2);
@@ -233,9 +231,7 @@ namespace aFuncore {
         gc_thread.join();
         delete &gc_inter;
 
-        delete global_varlist;
         protect->delReference();
-        global->delReference();
 
         Object::deleteAll(gc); /* 不需要mutex锁 */
 

+ 0 - 113
src/core/varlist.cpp

@@ -1,113 +0,0 @@
-#include "varlist.h"
-#include "object-value.h"
-#include "inter.h"
-#include "core-init.h"
-
-namespace aFuncore {
-    VarList::VarList(VarList *varlist){
-        std::unique_lock<std::mutex> mutex{lock};
-        for (auto &t: varlist->varspace)
-            this->varspace.push_back(t);
-    }
-
-    /**
-     * 访问变量
-     * @param name 变量名
-     * @return
-     */
-    Var *VarList::findVar(const std::string &name){
-        std::unique_lock<std::mutex> mutex{lock};
-        Var *ret = nullptr;
-        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == nullptr; tmp++) {
-            mutex.unlock();
-            ret = (*tmp)->findVar(name);
-            mutex.lock();
-        }
-        return ret;
-    }
-
-    /**
-     * 定义变量
-     * 若定义出现redefine则退出报错
-     * 若出现fail则跳到下一个变量空间尝试定义
-     * @param name 变量名
-     * @param data 变量(Object)
-     * @return
-     */
-    bool VarList::defineVar(const std::string &name, Object *data){
-        std::unique_lock<std::mutex> mutex{lock};
-        VarSpace::VarOperationFlat ret = VarSpace::vof_fail;
-        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_fail; tmp++) {
-            mutex.unlock();
-            ret = (*tmp)->defineVar(name, data);
-            mutex.lock();
-        }
-        return ret == VarSpace::vof_success;
-    }
-    
-    /**
-     * 定义变量
-     * 若定义出现redefine则退出报错
-     * 若出现fail则跳到下一个变量空间尝试定义
-     * @param name 变量名
-     * @param data 变量(Var)
-     * @return
-     */
-    bool VarList::defineVar(const std::string &name, Var *data){
-        std::unique_lock<std::mutex> mutex{lock};
-        VarSpace::VarOperationFlat ret = VarSpace::vof_fail;
-        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_fail; tmp++) {
-            mutex.unlock();
-            ret = (*tmp)->defineVar(name, data);
-            mutex.lock();
-        }
-        return ret == VarSpace::vof_success;
-    }
-    
-    /**
-     * 设置变量的值
-     * 若not_var则跳到下一个变量空间
-     * 若fail则结束
-     * @param name 变量名
-     * @param data 数据
-     * @return
-     */
-    bool VarList::setVar(const std::string &name, Object *data){
-        std::unique_lock<std::mutex> mutex{lock};
-        VarSpace::VarOperationFlat ret = VarSpace::vof_not_var;
-        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_not_var; tmp++) {
-            mutex.unlock();
-            ret = (*tmp)->setVar(name, data);
-            mutex.lock();
-        }
-        return ret == VarSpace::vof_success;
-    }
-    
-    /**
-     * 删除变量
-     * 若not_var则跳到下一个变量空间
-     * 若fail则结束
-     * @param name
-     * @return
-     */
-    bool VarList::delVar(const std::string &name){
-        std::unique_lock<std::mutex> mutex{lock};
-        VarSpace::VarOperationFlat ret = VarSpace::vof_not_var;
-        for (auto tmp = varspace.begin(), end = varspace.end(); tmp != end && ret == VarSpace::vof_not_var; tmp++) {
-            mutex.unlock();
-            ret = (*tmp)->delVar(name);
-            mutex.lock();
-        }
-        return ret == VarSpace::vof_success;
-    }
-    
-    void VarList::connect(VarList *varlist){
-        std::unique_lock<std::mutex> mutex{lock};
-        for (auto &t: varlist->varspace) {
-            mutex.unlock();
-            this->varspace.push_back(t);
-            mutex.lock();
-        }
-    }
-
-}

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

@@ -146,22 +146,22 @@ int Main() {
     Inter inter {env};
 
     auto obj = new Object("Object", inter);
-    inter.getGlobalVarlist()->defineVar("test-var", obj);
+    inter.getProtectVarSpace()->defineVar("test-var", obj);
     aFuntool::cout << "obj: " << obj << "\n";
     obj->delReference();
 
     auto func = new Func1(inter);
-    inter.getGlobalVarlist()->defineVar("test-func", func);
+    inter.getProtectVarSpace()->defineVar("test-func", func);
     aFuntool::cout << "func: " << func << "\n";
     func->delReference();
 
     auto literaler = new Literaler1(inter);
-    inter.getGlobalVarlist()->defineVar("test-literaler", literaler);
+    inter.getProtectVarSpace()->defineVar("test-literaler", literaler);
     aFuntool::cout << "literaler: " << literaler << "\n";
     literaler->delReference();
 
     auto cbv = new CBV1(inter);
-    inter.getGlobalVarlist()->defineVar("test-cbv", cbv);
+    inter.getProtectVarSpace()->defineVar("test-cbv", cbv);
     aFuntool::cout << "cbv: " << cbv << "\n";
     inter.getEnvVarSpace().setNumber("sys:error_std", 1);
     cbv->delReference();