mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
adopt yyw02's idea
This commit is contained in:
parent
181e7eb298
commit
e4f3c8f040
@ -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;
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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;
|
||||||
|
2
src/vm.h
2
src/vm.h
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user