diff --git a/src/obj.h b/src/obj.h index be95c0e3..da3f4e69 100644 --- a/src/obj.h +++ b/src/obj.h @@ -10,7 +10,7 @@ const _Int _Int_MAX_NEG = -9223372036854775807LL; const _Float _FLOAT_INF_POS = INFINITY; const _Float _FLOAT_INF_NEG = -INFINITY; -#define PK_VERSION "0.3.6" +#define PK_VERSION "0.3.7" class CodeObject; class BasePointer; diff --git a/src/vm.h b/src/vm.h index 16a85392..33cd9cbc 100644 --- a/src/vm.h +++ b/src/vm.h @@ -49,9 +49,19 @@ protected: PyVarDict _modules; // loaded modules std::map<_Str, _Code> _lazyModules; // lazy loaded modules PyVar __py2py_call_signal; + bool _stopFlag = false; + + void emitKeyboardInterrupt(){ + _stopFlag = true; + } PyVar runFrame(Frame* frame){ while(!frame->isCodeEnd()){ + if(_stopFlag){ + _stopFlag = false; + _error("KeyboardInterrupt", ""); + } + const ByteCode& byte = frame->readCode(); //printf("%s (%d) stack_size: %d\n", OP_NAMES[byte.op], byte.arg, frame->stackSize()); @@ -1083,8 +1093,7 @@ class ThreadedVM : public VM { std::thread* _thread = nullptr; std::atomic _state = THREAD_READY; std::optional<_Str> _sharedStr = {}; - std::atomic _stopFlag = false; - + PyVar jsonRpc(const _Str& _json){ _sharedStr = _json; suspend(); @@ -1095,9 +1104,8 @@ class ThreadedVM : public VM { void __deleteThread(){ if(_thread != nullptr){ - _stopFlag = true; + emitKeyboardInterrupt(); _thread->join(); - _stopFlag = false; delete _thread; _thread = nullptr; } @@ -1126,7 +1134,7 @@ public: _state = THREAD_SUSPENDED; // 50 fps is enough while(_state == THREAD_SUSPENDED){ - if(_stopFlag) std::terminate(); + if(_stopFlag) break; std::this_thread::sleep_for(std::chrono::milliseconds(20)); } }