diff --git a/amalgamate.py b/amalgamate.py index 17041a0a..04371316 100644 --- a/amalgamate.py +++ b/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!") diff --git a/src/public/internal.c b/src/public/internal.c index 82cfa681..051f446b 100644 --- a/src/public/internal.c +++ b/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() { diff --git a/src2/multi_vm.c b/src2/multi_vm.c new file mode 100644 index 00000000..f9e784e6 --- /dev/null +++ b/src2/multi_vm.c @@ -0,0 +1,32 @@ +#include "pocketpy.h" + +int main() +{ + py_initialize(); + + bool ok = py_exec("print('Hello world from VM0!')", "", 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!')", "", EXEC_MODE, NULL); + if(!ok){ + py_printexc(); + return 1; + } + + py_switchvm(0); + ok = py_exec("print('Hello world from VM0 again!')", "", EXEC_MODE, NULL); + if(!ok){ + py_printexc(); + return 1; + } + + py_finalize(); + + return 0; +} \ No newline at end of file