Przeglądaj źródła

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

SongZihuan 3 lat temu
rodzic
commit
d7737506e8

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

@@ -82,15 +82,16 @@ namespace aFuncore {
     class AFUN_CORE_EXPORT FuncActivation : public Activation {
     public:
         explicit inline FuncActivation(const Code::ByteCode *code, Inter &inter_);
+        explicit FuncActivation(Function *func, Inter &inter_);
         ~FuncActivation() override;
         ActivationStatus getCode(const Code::ByteCode *&code) override;
         void endRun() override;
 
     private:
         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;
 
         bool on_tail = false;

+ 1 - 0
include/core/inter.h

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

+ 8 - 0
src/core/activation.cpp

@@ -147,6 +147,14 @@ namespace aFuncore {
         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) {
         if (on_tail)
             return as_end;

+ 21 - 0
src/core/inter.cpp

@@ -100,6 +100,27 @@ namespace aFuncore {
         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 字面量

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

@@ -6,14 +6,16 @@ using namespace aFuntool;
 class Func1 : public Function {
     class CallFunc1 : public CallFunction {
         Code &func_code;
-        const Code::ByteCode *code;
+        const Code::ByteCode *call_code;
         Inter &inter;
         std::list<ArgCodeList> *acl;
     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>;
-            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 {
@@ -21,7 +23,10 @@ class Func1 : public Function {
         }
 
         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);
         }
 
@@ -201,9 +206,16 @@ int main() {
         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};
         std::thread thread{thread_test, std::ref(son)};