فهرست منبع

add `py_interrupt`

blueloveTH 6 روز پیش
والد
کامیت
e3255207c3
4فایلهای تغییر یافته به همراه11 افزوده شده و 0 حذف شده
  1. 3 0
      include/pocketpy/interpreter/vm.h
  2. 5 0
      src/interpreter/ceval.c
  3. 1 0
      src/interpreter/vm.c
  4. 2 0
      src/public/GlobalSetup.c

+ 3 - 0
include/pocketpy/interpreter/vm.h

@@ -12,6 +12,8 @@
 #include "pocketpy/interpreter/line_profiler.h"
 #include "pocketpy/interpreter/line_profiler.h"
 #include <time.h>
 #include <time.h>
 
 
+#include <stdatomic.h>
+
 // TODO:
 // TODO:
 // 1. __eq__ and __ne__ fallbacks
 // 1. __eq__ and __ne__ fallbacks
 // 2. un-cleared exception detection
 // 2. un-cleared exception detection
@@ -51,6 +53,7 @@ typedef struct VM {
 
 
     py_Callbacks callbacks;
     py_Callbacks callbacks;
     py_Capabilities capabilities;
     py_Capabilities capabilities;
+    atomic_bool is_interrupted;
 
 
     py_TValue last_retval;
     py_TValue last_retval;
     py_TValue unhandled_exc;
     py_TValue unhandled_exc;

+ 5 - 0
src/interpreter/ceval.c

@@ -124,6 +124,11 @@ __NEXT_STEP:
     }
     }
 #endif
 #endif
 
 
+    if(atomic_exchange(&self->is_interrupted, false)) {
+        py_exception(tp_KeyboardInterrupt, "");
+        goto __ERROR;
+    }
+
 #ifndef NDEBUG
 #ifndef NDEBUG
     pk_print_stack(self, frame, byte);
     pk_print_stack(self, frame, byte);
 #endif
 #endif

+ 1 - 0
src/interpreter/vm.c

@@ -94,6 +94,7 @@ void VM__ctor(VM* self) {
     self->callbacks.getchr = pk_default_getchr;
     self->callbacks.getchr = pk_default_getchr;
 
 
     memset(&self->capabilities, 0, sizeof(py_Capabilities));
     memset(&self->capabilities, 0, sizeof(py_Capabilities));
+    atomic_store(&self->is_interrupted, false);
 
 
     self->last_retval = *py_NIL();
     self->last_retval = *py_NIL();
     self->unhandled_exc = *py_NIL();
     self->unhandled_exc = *py_NIL();

+ 2 - 0
src/public/GlobalSetup.c

@@ -112,6 +112,8 @@ py_Callbacks* py_callbacks() { return &pk_current_vm->callbacks; }
 
 
 py_Capabilities* py_capabilities() { return &pk_current_vm->capabilities; }
 py_Capabilities* py_capabilities() { return &pk_current_vm->capabilities; }
 
 
+void py_interrupt() { atomic_store(&pk_current_vm->is_interrupted, true); }
+
 py_AppCallbacks* py_appcallbacks() {
 py_AppCallbacks* py_appcallbacks() {
     static py_AppCallbacks _callbacks = {0};
     static py_AppCallbacks _callbacks = {0};
     return &_callbacks;
     return &_callbacks;