Browse Source

refactor & feat: 隐式回调函数

SongZihuan 3 năm trước cách đây
mục cha
commit
44872447aa
4 tập tin đã thay đổi với 48 bổ sung3 xóa
  1. 7 0
      include/core/value.hpp
  2. 7 2
      src/core/activation.cpp
  3. 0 1
      src/core/inter.cpp
  4. 34 0
      test/src/run-code.cpp

+ 7 - 0
include/core/value.hpp

@@ -38,6 +38,13 @@ namespace aFuncore {
         Literaler(const std::string &type_, Inter *inter_) : Object(type_ + ":Literaler", inter_) {}
         virtual void getObject(const std::string &literal, char prefix)=0;
     };
+
+    class CallBackVar : public Object {
+    public:
+        CallBackVar(const std::string &type_, Inter *inter_) : Object(type_ + ":CallBackVar", inter_) {}
+        virtual bool isCallBack() {return true;}
+        virtual void callBack()=0;
+    };
 };
 
 #endif //AFUN_VALUE_HPP

+ 7 - 2
src/core/activation.cpp

@@ -66,8 +66,13 @@ void Activation::runCode(Code *code){
                 if (varlist != nullptr)
                     obj = varlist->findObject(code->getElement());
                 trackLog(aFunCoreLogger, "Find Var %s -> %p", code->getElement(), obj);
-                if (obj != nullptr)
-                    down->pushMessage(new NormalMessage(obj));
+                if (obj != nullptr) {
+                    auto cbv = dynamic_cast<CallBackVar *>(obj);
+                    if (cbv != nullptr && cbv->isCallBack())
+                        cbv->callBack();
+                    else
+                        down->pushMessage(new NormalMessage(obj));
+                }
             }
         } else switch (code->getBlockType()) {
             case block_p:  // 顺序执行

+ 0 - 1
src/core/inter.cpp

@@ -63,7 +63,6 @@ Inter::~Inter(){
 
         for (auto &it : *literal)
             delete it.rg;
-
         delete literal;
         delete gc;
         delete son_inter;

+ 34 - 0
test/src/run-code.cpp

@@ -71,6 +71,24 @@ public:
     }
 };
 
+class CBV1 : public CallBackVar {
+    Code *func_code;
+public:
+    explicit CBV1(Inter *inter_) : CallBackVar("CBV1", inter_) {
+        func_code = (new Code(0, "run-code.aun"));
+        func_code->connect(new Code(block_p, new Code("test-var", 1), 0));
+    }
+
+    ~CBV1() override {
+        func_code->destructAll();
+    }
+
+    void callBack() override {
+        printf("CallBackVar callback\n");
+        new ExeActivation(func_code, inter);
+    }
+};
+
 int main() {
     auto *inter = new Inter();
 
@@ -86,11 +104,16 @@ int main() {
     inter->getGlobalVarlist()->defineVar("test-literaler", literaler);
     printf_stdout(0, "literaler: %p\n", literaler);
 
+    auto cbv = new CBV1(inter);
+    inter->getGlobalVarlist()->defineVar("test-cbv", cbv);
+    printf_stdout(0, "cbv: %p\n", cbv);
+
     {
         auto code = (new Code(0, "run-code.aun"));
         code->connect(new Code(block_p, new Code("test-var", 1), 0));
         inter->runCode(code);
         code->destructAll();
+        printf("\n");
     }
 
     {
@@ -101,6 +124,7 @@ int main() {
         code->connect(new Code(block_c, arg, 0));
         inter->runCode(code);
         code->destructAll();
+        printf("\n");
     }
 
     {
@@ -111,6 +135,7 @@ int main() {
         code->connect(new Code(block_b, arg, 0));
         inter->runCode(code);
         code->destructAll();
+        printf("\n");
     }
 
     {
@@ -119,6 +144,15 @@ int main() {
         code->connect(new Code("data3", 1));
         inter->runCode(code);
         code->destructAll();
+        printf("\n");
+    }
+
+    {
+        auto code = (new Code(0, "run-code.aun"));
+        code->connect(new Code("test-cbv", 1));
+        inter->runCode(code);
+        code->destructAll();
+        printf("\n");
     }
 
     delete inter;