blueloveTH 2024-11-03 14:57:17 +08:00
parent 56996cc2e7
commit 0c1abd3b5c
3 changed files with 41 additions and 3 deletions

View File

@ -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!")

View File

@ -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];
}
}
void py_resetvm() {

32
src2/multi_vm.c Normal file
View File

@ -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;
}