Ver código fonte

fix https://github.com/pocketpy/pocketpy/issues/315

blueloveTH 1 ano atrás
pai
commit
0c1abd3b5c
3 arquivos alterados com 41 adições e 3 exclusões
  1. 1 1
      amalgamate.py
  2. 8 2
      src/public/internal.c
  3. 32 0
      src2/multi_vm.c

+ 1 - 1
amalgamate.py

@@ -149,7 +149,7 @@ write_file('amalgamated/pocketpy.h', merge_h_files())
 shutil.copy("src2/main.c", "amalgamated/main.c")
 
 if sys.platform in ['linux', 'darwin']:
-	ok = os.system("clang -o main amalgamated/pocketpy.c amalgamated/main.c -O1 --std=c11 -lm")
+	ok = os.system("clang -o main amalgamated/pocketpy.c amalgamated/main.c -O1 --std=c11 -lm -ldl")
 	if ok == 0:
 		print("Test build success!")
 

+ 8 - 2
src/public/internal.c

@@ -39,10 +39,14 @@ void py_finalize() {
     for(int i = 1; i < 16; i++) {
         VM* vm = pk_all_vm[i];
         if(vm) {
+            // temp fix https://github.com/pocketpy/pocketpy/issues/315
+            // TODO: refactor VM__ctor and VM__dtor
+            pk_current_vm = vm;
             VM__dtor(vm);
             free(vm);
         }
     }
+    pk_current_vm = &pk_default_vm;
     VM__dtor(&pk_default_vm);
     pk_current_vm = NULL;
     py_Name__finalize();
@@ -52,10 +56,12 @@ void py_finalize() {
 void py_switchvm(int index) {
     if(index < 0 || index >= 16) c11__abort("invalid vm index");
     if(!pk_all_vm[index]) {
-        pk_all_vm[index] = malloc(sizeof(VM));
+        pk_current_vm = pk_all_vm[index] = malloc(sizeof(VM));
+        memset(pk_current_vm, 0, sizeof(VM));
         VM__ctor(pk_all_vm[index]);
+    }else{
+        pk_current_vm = pk_all_vm[index];
     }
-    pk_current_vm = pk_all_vm[index];
 }
 
 void py_resetvm() {

+ 32 - 0
src2/multi_vm.c

@@ -0,0 +1,32 @@
+#include "pocketpy.h"
+
+int main()
+{
+    py_initialize();
+
+    bool ok = py_exec("print('Hello world from VM0!')", "<string1>", EXEC_MODE, NULL);
+    if(!ok){
+        py_printexc();
+        return 1;
+    }
+
+	//This line will cause assert error in Debug build and crash (exception) in Release build
+    py_switchvm(1); 
+
+    ok = py_exec("print('Hello world from VM1!')", "<string2>", EXEC_MODE, NULL);
+    if(!ok){
+        py_printexc();
+        return 1;
+    }
+
+    py_switchvm(0);
+    ok = py_exec("print('Hello world from VM0 again!')", "<string3>", EXEC_MODE, NULL);
+    if(!ok){
+        py_printexc();
+        return 1;
+    }
+
+    py_finalize();
+	
+	return 0;
+}