Răsfoiți Sursa

mark `stdout_write` and `stderr_write` as virtual

blueloveTH 2 ani în urmă
părinte
comite
d166766ebf
3 a modificat fișierele cu 19 adăugiri și 5 ștergeri
  1. 11 0
      docs/quick-start/installation.md
  2. 5 1
      include/pocketpy/vm.h
  3. 3 4
      src/vm.cpp

+ 11 - 0
docs/quick-start/installation.md

@@ -160,4 +160,15 @@ These two fields are C function pointers `PrintFunc` with the following signatur
 
 ```cpp
 typedef void(*PrintFunc)(const char*, int)
+```
+
+Or you can override these two virtual functions:
+```cpp
+    virtual void stdout_write(const Str& s){
+        _stdout(s.data, s.size);
+    }
+
+    virtual void stderr_write(const Str& s){
+        _stderr(s.data, s.size);
+    }
 ```

+ 5 - 1
include/pocketpy/vm.h

@@ -205,10 +205,14 @@ public:
     void _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2){ PUSH(_0); PUSH(_1); PUSH(_2); }
     void _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2, PyObject* _3){ PUSH(_0); PUSH(_1); PUSH(_2); PUSH(_3); }
 
-    void stdout_write(const Str& s){
+    virtual void stdout_write(const Str& s){
         _stdout(s.data, s.size);
     }
 
+    virtual void stderr_write(const Str& s){
+        _stderr(s.data, s.size);
+    }
+
     template<typename... Args>
     PyObject* call(PyObject* callable, Args&&... args){
         PUSH(callable);

+ 3 - 4
src/vm.cpp

@@ -164,21 +164,20 @@ namespace pkpy{
 #endif
             return _exec(code, _module);
         }catch (const Exception& e){
-            Str sum = e.summary() + "\n";
-            _stderr(sum.data, sum.size);
+            stderr_write(e.summary() + "\n");
         }
 #if !PK_DEBUG_FULL_EXCEPTION
         catch(const std::exception& e) {
             Str msg = "An std::exception occurred! It could be a bug.\n";
             msg = msg + e.what() + "\n";
-            _stderr(msg.data, msg.size);
+            stderr_write(msg);
         }
         catch(NeedMoreLines){
             throw;
         }
         catch(...) {
             Str msg = "An unknown exception occurred! It could be a bug. Please report it to @blueloveTH on GitHub.\n";
-            _stderr(msg.data, msg.size);
+            stderr_write(msg);
         }
 #endif
         callstack.clear();