Răsfoiți Sursa

fix run native constructor

lyon 3 ani în urmă
părinte
comite
0d71c8eed3
4 a modificat fișierele cu 41 adăugiri și 3 ștergeri
  1. 1 1
      port/linux/.vscode/launch.json
  2. 2 2
      src/PikaVM.c
  3. 21 0
      test/VM-test.cpp
  4. 17 0
      test/pikaMain-test.cpp

+ 1 - 1
port/linux/.vscode/launch.json

@@ -11,7 +11,7 @@
             "program": "${workspaceFolder}/build/test/pikascript_test",
             // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
             "args": [
-                // "--gtest_filter=pikaui.*"
+                "--gtest_filter=pikaMain.mem_now"
             ],
             "stopAtEntry": false,
             "cwd": "${workspaceFolder}",

+ 2 - 2
src/PikaVM.c

@@ -962,8 +962,6 @@ Arg* _obj_runMethodArgWithState(PikaObj* self,
     if (ARG_TYPE_NONE == method_type) {
         return NULL;
     }
-    ByteCodeFrame* method_bytecodeFrame =
-        methodArg_getBytecodeFrame(method_arg);
 
     /* redirect to def context */
     if (!argType_isNative(method_type)) {
@@ -986,6 +984,8 @@ Arg* _obj_runMethodArgWithState(PikaObj* self,
     } else {
         /* static method and object method */
         /* byteCode */
+        ByteCodeFrame* method_bytecodeFrame =
+            methodArg_getBytecodeFrame(method_arg);
         uintptr_t insturctArray_start = (uintptr_t)instructArray_getByOffset(
             &(method_bytecodeFrame->instruct_array), 0);
         uint16_t pc = (uintptr_t)method_ptr - insturctArray_start;

+ 21 - 0
test/VM-test.cpp

@@ -2656,6 +2656,27 @@ TEST(vm, bool_){
     EXPECT_EQ(pikaMemNow(), 0);
 }
 
+TEST(vm, method_cb){
+    PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
+    obj_run(pikaMain, 
+    "class Test:\n"
+    "    _cb = None\n"
+    "    def callback(self):\n"
+    "        print('test')\n"
+    "    def addcb(self, cb):\n"
+    "        self._cb = cb\n"
+    "    def runcb(self):\n"
+    "        self._cb()\n"
+    "    def __init__(self):\n"
+    "        self.addcb(self.callback)\n"
+    "t = Test()\n"
+    "t.runcb()"
+    );
+    obj_deinit(pikaMain);
+    EXPECT_STREQ(log_buff[0], "test\r\n");
+    EXPECT_EQ(pikaMemNow(), 0);
+}
+
 #endif
 
 TEST_END

+ 17 - 0
test/pikaMain-test.cpp

@@ -2906,6 +2906,23 @@ TEST(pikaMain, SHELL_filter_bye_pika_nomache) {
     obj_deinit(pikaMain);
     EXPECT_EQ(pikaMemNow(), 0);
 }
+
+TEST(pikaMain, mem_now) {
+    /* init */
+    pikaMemInfo.heapUsedMax = 0;
+    /* run */
+    PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
+    __platform_printf("BEGIN\r\n");
+    obj_run(pikaMain,
+            "from PikaStdLib import MemChecker as mem\n"
+            "mem.now()\n");
+    /* collect */
+    /* assert */
+    /* deinit */
+    obj_deinit(pikaMain);
+    EXPECT_EQ(pikaMemNow(), 0);
+}
+
 #endif
 
 TEST(pikaMain, obj_setNone) {