diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index 04495343..3258eb2f 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -42,13 +42,12 @@ inline const int BC_KEEPLINE = -1; struct CodeBlock { CodeBlockType type; int parent; // parent index in blocks - int base_stack_size; // this is used for exception handling int start; // start index of this block in codes, inclusive int end; // end index of this block in codes, exclusive int end2; // ... - CodeBlock(CodeBlockType type, int parent, int base_stack_size, int start): - type(type), parent(parent), base_stack_size(base_stack_size), start(start), end(-1), end2(-1) {} + CodeBlock(CodeBlockType type, int parent, int start): + type(type), parent(parent), start(start), end(-1), end2(-1) {} int get_break_end() const{ if(end2 != -1) return end2; diff --git a/include/pocketpy/expr.h b/include/pocketpy/expr.h index 5c80d18a..b8e064ea 100644 --- a/include/pocketpy/expr.h +++ b/include/pocketpy/expr.h @@ -104,7 +104,6 @@ struct CodeEmitContext{ int curr_iblock = 0; bool is_compiling_class = false; - int base_stack_size = 0; std::map _co_consts_nonstring_dedup_map; std::map> _co_consts_string_dedup_map; diff --git a/include/pocketpy/frame.h b/include/pocketpy/frame.h index 1f76e5e4..44acd2e6 100644 --- a/include/pocketpy/frame.h +++ b/include/pocketpy/frame.h @@ -102,8 +102,6 @@ struct Frame { : _ip(co->codes.data()-1), _sp_base(p0), co(co.get()), _module(_module), _callable(nullptr), _locals(co.get(), p0) {} PyVar* actual_sp_base() const { return _locals.a; } - - int stack_size(ValueStack* _s) const { return _s->_sp - actual_sp_base(); } ArgsView stack_view(ValueStack* _s) const { return ArgsView(actual_sp_base(), _s->_sp); } [[nodiscard]] int prepare_jump_exception_handler(ValueStack*); diff --git a/src/codeobject.cpp b/src/codeobject.cpp index 76841756..091aca8c 100644 --- a/src/codeobject.cpp +++ b/src/codeobject.cpp @@ -4,7 +4,7 @@ namespace pkpy{ CodeObject::CodeObject(std::shared_ptr src, const Str& name): src(src), name(name), nlocals(0), start_line(-1), end_line(-1) { - blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0, 0)); + blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0)); } PyVar const PY_OP_CALL(Type(), new PyObject(Type())); diff --git a/src/expr.cpp b/src/expr.cpp index 6ea1753c..25d8c546 100644 --- a/src/expr.cpp +++ b/src/expr.cpp @@ -24,17 +24,13 @@ namespace pkpy{ } CodeBlock* CodeEmitContext::enter_block(CodeBlockType type){ - if(type==CodeBlockType::FOR_LOOP || type==CodeBlockType::CONTEXT_MANAGER) base_stack_size++; - co->blocks.push_back(CodeBlock( - type, curr_iblock, base_stack_size, (int)co->codes.size() - )); + co->blocks.push_back(CodeBlock(type, curr_iblock, (int)co->codes.size())); curr_iblock = co->blocks.size()-1; return &co->blocks[curr_iblock]; } void CodeEmitContext::exit_block(){ auto curr_type = co->blocks[curr_iblock].type; - if(curr_type == CodeBlockType::FOR_LOOP || curr_type==CodeBlockType::CONTEXT_MANAGER) base_stack_size--; co->blocks[curr_iblock].end = co->codes.size(); curr_iblock = co->blocks[curr_iblock].parent; if(curr_iblock < 0) PK_FATAL_ERROR(); diff --git a/src/frame.cpp b/src/frame.cpp index 09a8cde2..70f2e894 100644 --- a/src/frame.cpp +++ b/src/frame.cpp @@ -24,20 +24,16 @@ namespace pkpy{ } int Frame::prepare_jump_exception_handler(ValueStack* _s){ + PyVar obj = _s->popx(); // try to find a parent try block - int block = co->lines[ip()].iblock; - while(block >= 0){ - if(co->blocks[block].type == CodeBlockType::TRY_EXCEPT) break; - block = co->blocks[block].parent; + int i = co->lines[ip()].iblock; + while(i >= 0){ + if(co->blocks[i].type == CodeBlockType::TRY_EXCEPT) break; + i = _exit_block(_s, i); } - if(block < 0) return -1; - PyVar obj = _s->popx(); // pop exception object - // get the stack size of the try block - int _stack_size = co->blocks[block].base_stack_size; - if(stack_size(_s) < _stack_size) throw std::runtime_error(_S("invalid state: ", stack_size(_s), '<', _stack_size).str()); - _s->reset(actual_sp_base() + _locals.size() + _stack_size); // rollback the stack - _s->push(obj); // push exception object - return co->blocks[block].end; + _s->push(obj); + if(i < 0) return -1; + return co->blocks[i].end; } int Frame::_exit_block(ValueStack* _s, int i){ diff --git a/src/vm.cpp b/src/vm.cpp index d4e7c79e..47086328 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -909,7 +909,7 @@ void VM::__init_builtin_types(){ validate(tp_none_type, new_type_object(nullptr, "NoneType", tp_object, false)); validate(tp_not_implemented, new_type_object(nullptr, "NotImplementedType", tp_object, false)); validate(tp_ellipsis, new_type_object(nullptr, "ellipsis", tp_object, false)); - validate(tp_stack_memory, new_type_object(nullptr, "stack_memory", tp_object, false)); + validate(tp_stack_memory, new_type_object(nullptr, "_stack_memory", tp_object, false)); // SyntaxError and IndentationError must be created here PyVar SyntaxError = new_type_object(nullptr, "SyntaxError", tp_exception, true);