Selaa lähdekoodia

refactor & feat: 添加多线程测试代码

SongZihuan 3 vuotta sitten
vanhempi
sitoutus
37a7b5fd49
3 muutettua tiedostoa jossa 50 lisäystä ja 28 poistoa
  1. 2 2
      include/core/inter.h
  2. 14 14
      src/core/inter.cpp
  3. 34 12
      test/src/run-code.cpp

+ 2 - 2
include/core/inter.h

@@ -24,7 +24,7 @@ namespace aFuncore {
         friend class Inter;
 
     public:
-        Environment();
+        Environment(int argc = 0, char **argv = nullptr);
         ~Environment() noexcept(false);
         Environment(Environment &) = delete;
         Environment &operator=(Environment &) = delete;
@@ -71,7 +71,7 @@ namespace aFuncore {
         constexpr static const char *B_PREFIX = "$`'%^&<?>";  /* NOLINT block前缀 */
         constexpr static const char *ALL_PREFIX = "$`'%^&<?>";  /* NOLINT block前缀 */
 
-        explicit Inter(Environment &env_, int argc = 0, char **argv = nullptr);
+        explicit Inter(Environment &env_);
         Inter(const Inter &base_inter);
         ~Inter();
         Inter &operator=(const Inter &) = delete;

+ 14 - 14
src/core/inter.cpp

@@ -5,24 +5,12 @@
 #include "core-exception.h"
 
 namespace aFuncore {
-    Inter::Inter(Environment &env_, int argc, char **argv)
+    Inter::Inter(Environment &env_)
             : out{}, in{}, env{env_}{
         status = inter_creat;
 
         activation = nullptr;
 
-        env.envvar.setNumber("sys:gc-runtime", 2);
-        env.envvar.setString("sys:prefix", "''");  // 引用,顺序执行
-        env.envvar.setNumber("sys:exit-code", 0);
-        env.envvar.setNumber("sys:argc", argc);
-        env.envvar.setNumber("sys:error_std", 0);
-
-        for (int i = 0; i < argc; i++) {
-            char buf[20];
-            snprintf(buf, 10, "sys:arg%d", i);
-            env.envvar.setString(buf, argv[i]);
-        }
-
         status = inter_init;
         env++;
     }
@@ -167,12 +155,24 @@ namespace aFuncore {
         return true;
     }
 
-    Environment::Environment()
+    Environment::Environment(int argc, char **argv)
         : obj{nullptr}, var{nullptr}, varspace{nullptr},
           protect{new ProtectVarSpace(*this)}, global{new VarSpace(*this)},
           global_varlist{new VarList(protect)}, destruct{false} {
         global_varlist->push(global);
         reference = 0;
+
+        envvar.setNumber("sys:gc-runtime", 2);
+        envvar.setString("sys:prefix", "''");  // 引用,顺序执行
+        envvar.setNumber("sys:exit-code", 0);
+        envvar.setNumber("sys:argc", argc);
+        envvar.setNumber("sys:error_std", 0);
+
+        for (int i = 0; i < argc; i++) {
+            char buf[20];
+            snprintf(buf, 10, "sys:arg%d", i);
+            envvar.setString(buf, argv[i]);
+        }
     }
 
     Environment::~Environment() noexcept(false) {

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

@@ -59,7 +59,7 @@ public:
     ~Literaler1() override = default;
 
     void getObject(const std::string &literal, char prefix, Inter &inter, Activation &activation) override {
-        aFuntool::cout << "Literaler1: " << literal << (prefix == NUL ? '-' : prefix) << "\n";
+        aFuntool::cout << "Literaler1: " << literal  << "prefix: " << (prefix == NUL ? '-' : prefix) << "\n";
         new ExeActivation(func_code, inter);
     }
 };
@@ -115,6 +115,15 @@ void printInterEvent(Inter &inter) {
     }
 }
 
+void thread_test(Inter &son) {
+    auto code = Code("run-code.aun");
+    code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
+                                                   new Code::ByteCode(code, "test-var", 1), 0));
+    son.runCode(code);
+    printInterEvent(son);
+    fputs_stdout("\n");
+}
+
 int main() {
     Environment env {};
     Inter inter {env};
@@ -137,6 +146,7 @@ int main() {
     inter.getEnvVarSpace().setNumber("sys:error_std", 1);
 
     {
+        fputs_stdout("Test-1: block-p & get test-var\n");
         auto code = Code("run-code.aun");
         code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
                                                        new Code::ByteCode(code, "test-var", 1), 0));
@@ -146,6 +156,7 @@ int main() {
     }
 
     {
+        fputs_stdout("Test-2: block-c & run {test-func test-var}\n");
         auto code = Code("run-code.aun");
 
         auto arg = new Code::ByteCode(code, "test-func", 1);
@@ -159,6 +170,7 @@ int main() {
     }
 
     {
+        fputs_stdout("Test-3: block-b & run [test-var test-func]\n");
         auto code = Code("run-code.aun");
 
         auto arg = new Code::ByteCode(code, "test-var", 1);
@@ -171,15 +183,17 @@ int main() {
     }
 
     {
+        fputs_stdout("Test-4: test-literaler\n");
         inter.pushLiteral("data[0-9]", "test-literaler", false);
         auto code = Code("run-code.aun");
-        code.getByteCode()->connect(new Code::ByteCode(code, "data3", 1));
+        code.getByteCode()->connect(new Code::ByteCode(code, "data4", 1));
         inter.runCode(code);
         printInterEvent(inter);
         fputs_stdout("\n");
     }
 
     {
+        fputs_stdout("Test-5: test-cbv\n");
         auto code = Code("run-code.aun");
         code.getByteCode()->connect(new Code::ByteCode(code, "test-cbv", 1));
         inter.runCode(code);
@@ -187,22 +201,30 @@ int main() {
         fputs_stdout("\n");
     }
 
-    /* 执行错误的代码 */
     {
-        auto code = Code("run-code.aun");
-        code.getByteCode()->connect(new Code::ByteCode(code, "test-not-var", 1));
-        inter.runCode(code);
-        printInterEvent(inter);
-        fputs_stdout("\n");
+        /* 多线程 */
+        fputs_stdout("Test-6: thread\n");
+        Inter son{inter};
+        std::thread thread{thread_test, std::ref(son)};
+
+        {
+            auto code = Code("run-code.aun");
+            code.getByteCode()->connect(new Code::ByteCode(code, Code::ByteCode::block_p,
+                                                           new Code::ByteCode(code, "test-var", 1), 0));
+            inter.runCode(code);
+            printInterEvent(inter);
+            fputs_stdout("\n");
+        }
+
+        thread.join();
     }
 
-    /* 多线程 */
+    /* 执行错误的代码 */
     {
-        Inter son {inter};
         auto code = Code("run-code.aun");
         code.getByteCode()->connect(new Code::ByteCode(code, "test-not-var", 1));
-        son.runCode(code);
-        printInterEvent(son);
+        inter.runCode(code);
+        printInterEvent(inter);
         fputs_stdout("\n");
     }