diff --git a/src/obj.h b/src/obj.h index a70b23ee..3bf8c2b4 100644 --- a/src/obj.h +++ b/src/obj.h @@ -18,6 +18,8 @@ class CodeObject; class BasePointer; class VM; +class PkExportedResource {}; + typedef std::shared_ptr PyVar; typedef PyVar PyVarOrNull; diff --git a/src/pocketpy.h b/src/pocketpy.h index 2b6d82e0..58e2e9f9 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -547,17 +547,10 @@ void __addModuleSys(VM* vm){ vm->setAttr(mod, "version", vm->PyStr(PK_VERSION)); } -enum ExportType { - PKPY_VM, - PKPY_REPL -}; -static std::unordered_map __pkpy_allocations; - extern "C" { __EXPORT VM* pkpy_new_vm(PrintFn _stdout, PrintFn _stderr){ VM* vm = new VM(); - __pkpy_allocations[vm] = PKPY_VM; __initializeBuiltinFunctions(vm); vm->_stdout = _stdout; vm->_stderr = _stderr; @@ -572,14 +565,8 @@ extern "C" { } __EXPORT - bool pkpy_delete(void* p){ - auto it = __pkpy_allocations.find(p); - if(it == __pkpy_allocations.end()) return false; - switch(it->second){ - case PKPY_VM: delete (VM*)p; __pkpy_allocations.erase(it); return true; - case PKPY_REPL: delete (REPL*)p; __pkpy_allocations.erase(it); return true; - } - return false; + void pkpy_delete(PkExportedResource* p){ + delete p; } __EXPORT @@ -590,9 +577,7 @@ extern "C" { __EXPORT REPL* pkpy_new_repl(VM* vm, bool use_prompt){ - REPL* repl = new REPL(vm, use_prompt); - __pkpy_allocations[repl] = PKPY_REPL; - return repl; + return new REPL(vm, use_prompt); } __EXPORT diff --git a/src/repl.h b/src/repl.h index 0ff92824..87b1dfd1 100644 --- a/src/repl.h +++ b/src/repl.h @@ -3,7 +3,7 @@ #include "compiler.h" #include "vm.h" -class REPL { +class REPL: public PkExportedResource { int need_more_lines = 0; std::string buffer; CompileMode mode; diff --git a/src/vm.h b/src/vm.h index 27628739..92fe78f6 100644 --- a/src/vm.h +++ b/src/vm.h @@ -44,7 +44,7 @@ typedef void(*PrintFn)(const VM*, const char*); -class VM{ +class VM: public PkExportedResource{ private: std::stack< std::unique_ptr > callstack; PyVarDict _modules; // 3rd modules