瀏覽代碼

refactor & feat: 添加隐式调用函数模式

SongZihuan 3 年之前
父節點
當前提交
d7737506e8
共有 5 個文件被更改,包括 52 次插入9 次删除
  1. 4 3
      include/core/core-activation.h
  2. 1 0
      include/core/inter.h
  3. 8 0
      src/core/activation.cpp
  4. 21 0
      src/core/inter.cpp
  5. 18 6
      test/src/run-code.cpp

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

@@ -82,15 +82,16 @@ namespace aFuncore {
     class AFUN_CORE_EXPORT FuncActivation : public Activation {
     class AFUN_CORE_EXPORT FuncActivation : public Activation {
     public:
     public:
         explicit inline FuncActivation(const Code::ByteCode *code, Inter &inter_);
         explicit inline FuncActivation(const Code::ByteCode *code, Inter &inter_);
+        explicit FuncActivation(Function *func, Inter &inter_);
         ~FuncActivation() override;
         ~FuncActivation() override;
         ActivationStatus getCode(const Code::ByteCode *&code) override;
         ActivationStatus getCode(const Code::ByteCode *&code) override;
         void endRun() override;
         void endRun() override;
 
 
     private:
     private:
         enum {
         enum {
-            func_first = 0,
-            func_get_func = 1,
-            func_get_arg = 2,
+            func_first = 0,  // 获取函数体前准备
+            func_get_func = 1,  // 获取函数体后,开始获取参数前
+            func_get_arg = 2,  // 获取参数过程
         } status = func_first;
         } status = func_first;
 
 
         bool on_tail = false;
         bool on_tail = false;

+ 1 - 0
include/core/inter.h

@@ -96,6 +96,7 @@ namespace aFuncore {
 
 
         bool runCode();
         bool runCode();
         bool runCode(const Code &code);
         bool runCode(const Code &code);
+        bool runCode(Object *obj);
 
 
         inline InterStatus setInterStop();
         inline InterStatus setInterStop();
         inline InterStatus setInterExit();
         inline InterStatus setInterExit();

+ 8 - 0
src/core/activation.cpp

@@ -147,6 +147,14 @@ namespace aFuncore {
         delete call_func;
         delete call_func;
     }
     }
 
 
+    FuncActivation::FuncActivation(Function *func_, Inter &inter_) : Activation(inter_), call{nullptr} {
+        on_tail = false;  // 跳过所有阶段
+        status = func_get_func;
+        func = func_;
+        if (prev == nullptr)
+            varlist->connect(inter_.getGlobalVarlist());
+    }
+
     Activation::ActivationStatus FuncActivation::getCode(const Code::ByteCode *&code) {
     Activation::ActivationStatus FuncActivation::getCode(const Code::ByteCode *&code) {
         if (on_tail)
         if (on_tail)
             return as_end;
             return as_end;

+ 21 - 0
src/core/inter.cpp

@@ -100,6 +100,27 @@ namespace aFuncore {
         return runCode();
         return runCode();
     }
     }
 
 
+    /**
+     * 函数调用
+     * @param code 代码
+     * @return
+     */
+    bool Inter::runCode(Object *func){
+        if (activation != nullptr) {
+            errorLog(aFunCoreLogger, "Run function with activation");
+            return false;
+        }
+
+        Function *func_obj = dynamic_cast<Function *>(func);
+        if (func_obj == nullptr) {
+            errorLog(aFunCoreLogger, "Run without function");
+            return false;
+        }
+
+        new FuncActivation(func_obj, *this);
+        return runCode();
+    }
+
     /**
     /**
      * 检查字面量是否匹配
      * 检查字面量是否匹配
      * @param element 字面量
      * @param element 字面量

+ 18 - 6
test/src/run-code.cpp

@@ -6,14 +6,16 @@ using namespace aFuntool;
 class Func1 : public Function {
 class Func1 : public Function {
     class CallFunc1 : public CallFunction {
     class CallFunc1 : public CallFunction {
         Code &func_code;
         Code &func_code;
-        const Code::ByteCode *code;
+        const Code::ByteCode *call_code;
         Inter &inter;
         Inter &inter;
         std::list<ArgCodeList> *acl;
         std::list<ArgCodeList> *acl;
     public:
     public:
-        CallFunc1(Code &func_code_, const Code::ByteCode *code_, Inter &inter_) : func_code{func_code_}, code{code_}, inter{inter_} {
+        CallFunc1(Code &func_code_, const Code::ByteCode *code_, Inter &inter_) : func_code{func_code_}, call_code{code_}, inter{inter_} {
             acl = new std::list<ArgCodeList>;
             acl = new std::list<ArgCodeList>;
-            ArgCodeList agr1 = {code_->getSon()->toNext()};
-            acl->push_front(agr1);
+            if (code_ != nullptr) {
+                ArgCodeList agr1 = {code_->getSon()->toNext()};
+                acl->push_front(agr1);
+            }
         }
         }
 
 
         std::list<ArgCodeList> *getArgCodeList(Inter &inter_, Activation &activation, const Code::ByteCode *call) override {
         std::list<ArgCodeList> *getArgCodeList(Inter &inter_, Activation &activation, const Code::ByteCode *call) override {
@@ -21,7 +23,10 @@ class Func1 : public Function {
         }
         }
 
 
         void runFunction() override {
         void runFunction() override {
-            printf_stdout(0, "runFunction : %p\n", acl->begin()->ret);
+            if (acl->empty())
+                printf_stdout(0, "runFunction No AegCodeList\n");
+            else
+                printf_stdout(0, "runFunction : %p\n", acl->begin()->ret);
             new ExeActivation(func_code, inter);
             new ExeActivation(func_code, inter);
         }
         }
 
 
@@ -201,9 +206,16 @@ int main() {
         fputs_stdout("\n");
         fputs_stdout("\n");
     }
     }
 
 
+    {
+        fputs_stdout("Test-6: run-function\n");
+        inter.runCode(func);
+        printInterEvent(inter);
+        fputs_stdout("\n");
+    }
+
     {
     {
         /* 多线程 */
         /* 多线程 */
-        fputs_stdout("Test-6: thread\n");
+        fputs_stdout("Test-7: thread\n");
         Inter son{inter};
         Inter son{inter};
         std::thread thread{thread_test, std::ref(son)};
         std::thread thread{thread_test, std::ref(son)};