Browse Source

refactor & feat: import移除出core

SongZihuan 3 năm trước cách đây
mục cha
commit
f87644a85c

+ 0 - 24
include/core/object-value.h

@@ -121,30 +121,6 @@ namespace aFuncore {
         virtual bool isCallBack(Inter &inter, Activation &activation);
         virtual void callBack(Inter &inter, Activation &activation) = 0;
     };
-
-    class ImportFunction : public Function {
-        class CallFunc : public CallFunction {
-            const Code::ByteCode *call_code;
-            Inter &inter;
-            std::list<ArgCodeList> *acl;
-            std::string import;
-        public:
-            CallFunc(const Code::ByteCode *code_, Inter &inter_);
-            std::list<ArgCodeList> *getArgCodeList(Inter &inter_,
-                                                   Activation &activation,
-                                                   const Code::ByteCode *call) override;
-
-            void runFunction() override;
-            ~CallFunc() override;
-        };
-
-    public:
-        AFUN_INLINE explicit ImportFunction(Inter &inter_);
-        AFUN_INLINE explicit ImportFunction(Environment &env_);
-        ~ImportFunction() override = default;
-
-        CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) override;
-    };
 };
 
 #include "object-value.inline.h"

+ 0 - 8
include/core/object-value.inline.h

@@ -49,14 +49,6 @@ namespace aFuncore {
     Object *Function::CallFunction::ArgCodeList::getObject() {
         return ret;
     }
-
-    ImportFunction::ImportFunction(Inter &inter_) : Object("Function", inter_) {
-
-    }
-
-    ImportFunction::ImportFunction(Environment &env_) : Object("Function", env_) {
-
-    }
 };
 
 #endif //AFUN_OBJECT_VALUE_INLINE_H

+ 0 - 6
src/core/inter.cpp

@@ -196,12 +196,6 @@ namespace aFuncore {
         }
 
         gc_thread = std::thread([this](){this->gcThread();});
-
-        {  // 导入函数
-            auto import = new ImportFunction(*this);
-            protect->defineVar("import", import);
-            import->delReference();
-        }
     }
 
     Environment::~Environment() noexcept(false) {

+ 0 - 32
src/core/object-value.cpp

@@ -172,36 +172,4 @@ namespace aFuncore {
     bool CallBackVar::isCallBack(Inter &, Activation &) {
         return true;
     }
-
-    Function::CallFunction *ImportFunction::getCallFunction(const Code::ByteCode *code, Inter &inter) {
-        return dynamic_cast<CallFunction *>(new CallFunc(code, inter));
-    }
-
-    ImportFunction::CallFunc::CallFunc(const Code::ByteCode *code_, Inter &inter_) : call_code{code_}, inter{inter_} {
-        if (code_ == nullptr ||
-            code_->getSon() == nullptr ||
-            code_->getSon()->toNext() == nullptr ||
-            code_->getSon()->toNext()->getType() != Code::ByteCode::code_element)
-            throw ArgumentError();
-        acl = new std::list<ArgCodeList>;
-        import = code_->getSon()->toNext()->getElement();
-    }
-
-    std::list<Function::CallFunction::ArgCodeList> *ImportFunction::CallFunc::getArgCodeList(Inter &,
-                                                                                             Activation &,
-                                                                                             const Code::ByteCode *) {
-        return acl;
-    }
-
-    void ImportFunction::CallFunc::runFunction() {
-        auto &stream = inter.getActivation()->getDownStream();
-        auto none = new Object("None", inter);
-        stream.pushMessage("NORMAL", new NormalMessage(none));
-        none->delReference();
-        aFuntool::cout << "Import " << import << " : " << call_code << "\n";
-    }
-
-    ImportFunction::CallFunc::~CallFunc() {
-        delete acl;
-    }
 }

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

@@ -48,8 +48,8 @@ public:
 
     ~Func1() override = default;
 
-    CallFunction *getCallFunction(const Code::ByteCode *code, Inter &inter) override {
-        return dynamic_cast<CallFunction *>(new CallFunc1(func_code, code, inter));
+    CallFunc1 *getCallFunction(const Code::ByteCode *code, Inter &inter) override {
+        return new CallFunc1(func_code, code, inter);
     }
 
     bool isInfix() override {return true;}
@@ -98,6 +98,58 @@ public:
     }
 };
 
+class ImportFunction : public Function {
+    class CallFunc : public CallFunction {
+        const Code::ByteCode *call_code;
+        Inter &inter;
+        std::list<ArgCodeList> *acl;
+        std::string import;
+    public:
+        CallFunc(const Code::ByteCode *code_, Inter &inter_) : call_code{code_}, inter{inter_} {
+            if (code_ == nullptr ||
+                code_->getSon() == nullptr ||
+                code_->getSon()->toNext() == nullptr ||
+                code_->getSon()->toNext()->getType() != Code::ByteCode::code_element)
+                throw ArgumentError();
+            acl = new std::list<ArgCodeList>;
+            import = code_->getSon()->toNext()->getElement();
+        }
+
+        std::list<ArgCodeList> *getArgCodeList(Inter &,
+                                               Activation &,
+                                               const Code::ByteCode *) override {
+            return acl;
+        }
+
+        void runFunction() override {
+            auto &stream = inter.getActivation()->getDownStream();
+            auto none = new Object("None", inter);
+            stream.pushMessage("NORMAL", new NormalMessage(none));
+            none->delReference();
+            aFuntool::cout << "Import " << import << " : " << call_code << "\n";
+        }
+
+        ~CallFunc() override {
+            delete acl;
+        }
+    };
+
+public:
+    AFUN_INLINE explicit ImportFunction(Inter &inter_) : Object("Function", inter_) {
+
+    }
+
+    AFUN_INLINE explicit ImportFunction(Environment &env_) : Object("Function", env_) {
+
+    }
+
+    ~ImportFunction() override = default;
+
+    CallFunc *getCallFunction(const Code::ByteCode *code, Inter &inter) override {
+        return new CallFunc(code, inter);
+    }
+};
+
 void printMessage(const std::string &type, Message *msg, Inter &inter) {
     if (type == "NORMAL") {
         auto *msg_ = dynamic_cast<NormalMessage *>(msg);
@@ -163,9 +215,14 @@ int Main() {
     auto cbv = new CBV1(inter);
     inter.getProtectVarSpace()->defineVar("test-cbv", cbv);
     aFuntool::cout << "cbv: " << cbv << "\n";
-    inter.getEnvVarSpace().setNumber("sys:error_std", 1);
     cbv->delReference();
 
+    auto import = new ImportFunction(inter);
+    inter.getProtectVarSpace()->defineVar("import", import);
+    aFuntool::cout << "import: " << import << "\n";
+    import->delReference();
+
+    inter.getEnvVarSpace().setNumber("sys:error_std", 1);
     auto tmp = new Func1(inter);
     aFuntool::cout << "tmp: " << tmp << "\n\n";
     tmp->delReference();