From 6ec6c5290b877d79c72e68f5e28e197240001f64 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 14 Apr 2024 21:26:03 +0800 Subject: [PATCH] some fix --- include/pocketpy/codeobject.h | 2 +- include/pocketpy/vm.h | 15 +++++++++------ src/ceval.cpp | 2 +- src/pocketpy.cpp | 2 +- src/vm.cpp | 27 +++++++++++---------------- 5 files changed, 23 insertions(+), 25 deletions(-) diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index f11ce4de..ac26e973 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -173,7 +173,7 @@ struct NativeFunc { NativeFunc(NativeFuncC f, FuncDecl_ decl); void check_size(VM* vm, ArgsView args) const; - PyObject* call(VM* vm, ArgsView args) const; + PyObject* call(VM* vm, ArgsView args) const { return f(vm, args); } }; struct Function{ diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 04d51df1..72e6ba9d 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -30,14 +30,13 @@ namespace pkpy{ typedef PyObject* (*BinaryFuncC)(VM*, PyObject*, PyObject*); struct NextBreakpoint{ - LinkedFrame* linked_frame; + int callstack_size; int lineno; bool should_step_into; - NextBreakpoint(): linked_frame(nullptr) {} - NextBreakpoint(LinkedFrame* lf, bool should_step_into): - linked_frame(lf), lineno(lf->frame.curr_lineno()), should_step_into(should_step_into) {} - void _step(VM* vm, LinkedFrame* lf); - bool empty() const { return linked_frame == nullptr; } + NextBreakpoint(): callstack_size(0) {} + NextBreakpoint(int callstack_size, int lineno, bool should_step_into): callstack_size(callstack_size), lineno(lineno), should_step_into(should_step_into) {} + void _step(VM* vm); + bool empty() const { return callstack_size == 0; } }; struct PyTypeInfo{ @@ -175,6 +174,10 @@ public: void _pop_frame(){ s_data.reset(callstack.top()._sp_base); callstack.pop(); + + if(!_next_breakpoint.empty() && callstack.size()<_next_breakpoint.callstack_size){ + _next_breakpoint = NextBreakpoint(); + } } PyObject* py_str(PyObject* obj); diff --git a/src/ceval.cpp b/src/ceval.cpp index adb96b08..93376389 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -75,7 +75,7 @@ PyObject* VM::_run_top_frame(){ #define CEVAL_STEP_CALLBACK() \ if(_ceval_on_step) _ceval_on_step(this, frame, byte); \ if(_profiler) _profiler->_step(callstack._tail); \ - if(!_next_breakpoint.empty()) { _next_breakpoint._step(this, callstack._tail); } + if(!_next_breakpoint.empty()) { _next_breakpoint._step(this); } #define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; } __NEXT_FRAME: diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 94550cbb..a5c367f2 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -73,7 +73,7 @@ void init_builtins(VM* _vm) { // builtin functions _vm->bind_func<0>(_vm->builtins, "breakpoint", [](VM* vm, ArgsView args) { - vm->_next_breakpoint = NextBreakpoint(vm->callstack._tail, false); + vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), vm->top_frame()->curr_lineno(), false); return vm->None; }); diff --git a/src/vm.cpp b/src/vm.cpp index 587592bd..e2700f7d 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -1349,24 +1349,19 @@ void NativeFunc::check_size(VM* vm, ArgsView args) const{ } } -PyObject* NativeFunc::call(VM *vm, ArgsView args) const { - return f(vm, args); -} - -void NextBreakpoint::_step(VM* vm, LinkedFrame* linked_frame){ - int lineno = linked_frame->frame.curr_lineno(); +void NextBreakpoint::_step(VM* vm){ + int curr_callstack_size = vm->callstack.size(); + int curr_lineno = vm->top_frame()->curr_lineno(); if(should_step_into){ - if(linked_frame != this->linked_frame || lineno != this->lineno){ + if(curr_callstack_size != callstack_size || curr_lineno != lineno){ vm->_breakpoint(); } }else{ - if(linked_frame == this->linked_frame){ - if(lineno != this->lineno) vm->_breakpoint(); - }else{ - if(this->linked_frame->f_back == linked_frame){ - // returning - vm->_breakpoint(); - } + if(curr_callstack_size == callstack_size) { + if(curr_lineno != lineno) vm->_breakpoint(); + }else if(curr_callstack_size < callstack_size){ + // returning + vm->_breakpoint(); } } } @@ -1436,11 +1431,11 @@ void VM::_breakpoint(){ PK_UNREACHABLE() } if(line == "n" || line == "next"){ - vm->_next_breakpoint = NextBreakpoint(frames[0], false); + vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), frame_0->curr_lineno(), false); break; } if(line == "s" || line == "step"){ - vm->_next_breakpoint = NextBreakpoint(frames[0], true); + vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), frame_0->curr_lineno(), true); break; } if(line == "w" || line == "where"){