This commit is contained in:
blueloveTH 2022-11-21 00:30:40 +08:00
parent 69f820acc6
commit 2e943f0e34
5 changed files with 25 additions and 18 deletions

View File

@ -125,6 +125,8 @@ public:
PyVar _module; PyVar _module;
PyVarDict f_locals; PyVarDict f_locals;
uint64_t id;
inline PyVarDict& f_globals(){ inline PyVarDict& f_globals(){
return _module->attribs; return _module->attribs;
} }
@ -132,7 +134,11 @@ public:
const CodeObject* code; const CodeObject* code;
Frame(const CodeObject* code, PyVar _module, const PyVarDict& locals) Frame(const CodeObject* code, PyVar _module, const PyVarDict& locals)
: code(code), _module(_module), f_locals(locals) {} : code(code), _module(_module), f_locals(locals) {
static uint64_t frame_id = 1;
id = frame_id++;
}
inline const ByteCode& readCode() { inline const ByteCode& readCode() {
return code->co_code[ip++]; return code->co_code[ip++];

View File

@ -704,7 +704,7 @@ extern "C" {
__initializeBuiltinFunctions(vm); __initializeBuiltinFunctions(vm);
_Code code = compile(vm, __BUILTINS_CODE, "<builtins>"); _Code code = compile(vm, __BUILTINS_CODE, "<builtins>");
if(code == nullptr) exit(1); if(code == nullptr) exit(1);
vm->_exec(code, vm->builtins); vm->_exec(code, vm->builtins, {});
__addModuleSys(vm); __addModuleSys(vm);
__addModuleTime(vm); __addModuleTime(vm);

View File

@ -62,8 +62,8 @@ struct CompoundPointer : BasePointer {
struct UserPointer : BasePointer { struct UserPointer : BasePointer {
const _Pointer p; const _Pointer p;
Frame* frame; uint64_t f_id;
UserPointer(_Pointer p, Frame* frame) : p(p), frame(frame) {} UserPointer(_Pointer p, uint64_t f_id) : p(p), f_id(f_id) {}
PyVar get(VM* vm, Frame* frame) const; PyVar get(VM* vm, Frame* frame) const;
void set(VM* vm, Frame* frame, PyVar val) const; void set(VM* vm, Frame* frame, PyVar val) const;

View File

@ -62,7 +62,7 @@ __NOT_ENOUGH_LINES:
try{ try{
_Code code = compile(vm, line.c_str(), "<stdin>", mode); _Code code = compile(vm, line.c_str(), "<stdin>", mode);
if(code != nullptr) vm->exec(code); if(code != nullptr) vm->exec(code, nullptr, true);
}catch(NeedMoreLines& ne){ }catch(NeedMoreLines& ne){
buffer += line; buffer += line;
buffer += '\n'; buffer += '\n';

View File

@ -211,7 +211,7 @@ private:
{ {
// _pointer to pointer // _pointer to pointer
const _Pointer& p = PyPointer_AS_C(frame->__pop()); const _Pointer& p = PyPointer_AS_C(frame->__pop());
_Pointer up = std::make_shared<UserPointer>(p, frame); _Pointer up = std::make_shared<UserPointer>(p, frame->id);
frame->push(newObject(_tp_user_pointer, std::move(up))); frame->push(newObject(_tp_user_pointer, std::move(up)));
} break; } break;
case OP_UNARY_DEREF: case OP_UNARY_DEREF:
@ -373,11 +373,13 @@ public:
return asRepr(obj); return asRepr(obj);
} }
bool __isFrameValid(Frame* frame){ Frame* __findFrame(uint64_t up_f_id){
for(auto it=callstack.crbegin(); it!=callstack.crend(); ++it){ for(auto it=callstack.crbegin(); it!=callstack.crend(); ++it){
if(it->get() == frame) return true; uint64_t f_id = it->get()->id;
if(f_id == up_f_id) return it->get();
if(f_id < up_f_id) return nullptr;
} }
return false; return nullptr;
} }
Frame* topFrame(){ Frame* topFrame(){
@ -498,10 +500,10 @@ public:
return call(getAttr(obj, func), args); return call(getAttr(obj, func), args);
} }
PyVarOrNull exec(const _Code& code, PyVar _module=nullptr){ PyVarOrNull exec(const _Code& code, PyVar _module=nullptr, bool repl_mode=false){
if(_module == nullptr) _module = _main; if(_module == nullptr) _module = _main;
try { try {
return _exec(code, _module); return _exec(code, _module, {}, repl_mode);
} catch (const std::exception& e) { } catch (const std::exception& e) {
if(const _Error* _ = dynamic_cast<const _Error*>(&e)){ if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
*_stderr << e.what() << '\n'; *_stderr << e.what() << '\n';
@ -523,8 +525,9 @@ public:
return frame; return frame;
} }
PyVar _exec(const _Code& code, PyVar _module, const PyVarDict& locals={}){ PyVar _exec(const _Code& code, PyVar _module, const PyVarDict& locals, bool repl_mode=false){
Frame* frame = __pushNewFrame(code, _module, locals); Frame* frame = __pushNewFrame(code, _module, locals);
if(repl_mode) frame->id = 0;
Frame* frameBase = frame; Frame* frameBase = frame;
PyVar ret = nullptr; PyVar ret = nullptr;
@ -956,16 +959,14 @@ void CompoundPointer::del(VM* vm, Frame* frame) const{
} }
PyVar UserPointer::get(VM* vm, Frame* frame) const{ PyVar UserPointer::get(VM* vm, Frame* frame) const{
frame = this->frame; frame = vm->__findFrame(f_id);
// this check is unsafe, but it's the best we can do if(frame == nullptr) vm->nullPointerError();
if(!vm->__isFrameValid(frame)) vm->nullPointerError();
return p->get(vm, frame); return p->get(vm, frame);
} }
void UserPointer::set(VM* vm, Frame* frame, PyVar val) const{ void UserPointer::set(VM* vm, Frame* frame, PyVar val) const{
frame = this->frame; frame = vm->__findFrame(f_id);
// this check is unsafe, but it's the best we can do if(frame == nullptr) vm->nullPointerError();
if(!vm->__isFrameValid(frame)) vm->nullPointerError();
p->set(vm, frame, val); p->set(vm, frame, val);
} }