blueloveTH 3 سال پیش
والد
کامیت
075dd44d98
2فایلهای تغییر یافته به همراه14 افزوده شده و 6 حذف شده
  1. 1 1
      src/obj.h
  2. 13 5
      src/vm.h

+ 1 - 1
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;

+ 13 - 5
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<ThreadState> _state = THREAD_READY;
     std::optional<_Str> _sharedStr = {};
-    std::atomic<bool> _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));
         }
     }