adopt yyw02's idea

This commit is contained in:
blueloveTH 2022-11-12 22:23:00 +08:00
parent 181e7eb298
commit e4f3c8f040
4 changed files with 7 additions and 20 deletions

View File

@ -18,6 +18,8 @@ class CodeObject;
class BasePointer; class BasePointer;
class VM; class VM;
class PkExportedResource {};
typedef std::shared_ptr<PyObject> PyVar; typedef std::shared_ptr<PyObject> PyVar;
typedef PyVar PyVarOrNull; typedef PyVar PyVarOrNull;

View File

@ -547,17 +547,10 @@ void __addModuleSys(VM* vm){
vm->setAttr(mod, "version", vm->PyStr(PK_VERSION)); vm->setAttr(mod, "version", vm->PyStr(PK_VERSION));
} }
enum ExportType {
PKPY_VM,
PKPY_REPL
};
static std::unordered_map<void*, ExportType> __pkpy_allocations;
extern "C" { extern "C" {
__EXPORT __EXPORT
VM* pkpy_new_vm(PrintFn _stdout, PrintFn _stderr){ VM* pkpy_new_vm(PrintFn _stdout, PrintFn _stderr){
VM* vm = new VM(); VM* vm = new VM();
__pkpy_allocations[vm] = PKPY_VM;
__initializeBuiltinFunctions(vm); __initializeBuiltinFunctions(vm);
vm->_stdout = _stdout; vm->_stdout = _stdout;
vm->_stderr = _stderr; vm->_stderr = _stderr;
@ -572,14 +565,8 @@ extern "C" {
} }
__EXPORT __EXPORT
bool pkpy_delete(void* p){ void pkpy_delete(PkExportedResource* p){
auto it = __pkpy_allocations.find(p); delete 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;
} }
__EXPORT __EXPORT
@ -590,9 +577,7 @@ extern "C" {
__EXPORT __EXPORT
REPL* pkpy_new_repl(VM* vm, bool use_prompt){ REPL* pkpy_new_repl(VM* vm, bool use_prompt){
REPL* repl = new REPL(vm, use_prompt); return new REPL(vm, use_prompt);
__pkpy_allocations[repl] = PKPY_REPL;
return repl;
} }
__EXPORT __EXPORT

View File

@ -3,7 +3,7 @@
#include "compiler.h" #include "compiler.h"
#include "vm.h" #include "vm.h"
class REPL { class REPL: public PkExportedResource {
int need_more_lines = 0; int need_more_lines = 0;
std::string buffer; std::string buffer;
CompileMode mode; CompileMode mode;

View File

@ -44,7 +44,7 @@
typedef void(*PrintFn)(const VM*, const char*); typedef void(*PrintFn)(const VM*, const char*);
class VM{ class VM: public PkExportedResource{
private: private:
std::stack< std::unique_ptr<Frame> > callstack; std::stack< std::unique_ptr<Frame> > callstack;
PyVarDict _modules; // 3rd modules PyVarDict _modules; // 3rd modules