blueloveTH 2 лет назад
Родитель
Сommit
da12d4b8ff
6 измененных файлов с 13 добавлено и 17 удалено
  1. 1 1
      docs/quick-start/installation.md
  2. 1 1
      include/pocketpy/pocketpy_c.h
  3. 2 2
      include/pocketpy/vm.h
  4. 4 6
      src/pocketpy.cpp
  5. 1 1
      src/pocketpy_c.cpp
  6. 4 6
      src/vm.cpp

+ 1 - 1
docs/quick-start/installation.md

@@ -125,5 +125,5 @@ You can redirect them to your own buffer by setting `vm->_stdout` and `vm->_stde
 These two fields are C function pointers `PrintFunc` with the following signature:
 
 ```cpp
-typedef void(*PrintFunc)(VM*, const char*, int)
+typedef void(*PrintFunc)(const char*, int)
 ```

+ 1 - 1
include/pocketpy/pocketpy_c.h

@@ -12,7 +12,7 @@ extern "C" {
 
 typedef struct pkpy_vm_handle pkpy_vm;
 typedef int (*pkpy_CFunction)(pkpy_vm*);
-typedef void (*pkpy_COutputHandler)(pkpy_vm*, const char*, int);
+typedef void (*pkpy_COutputHandler)(const char*, int);
 typedef unsigned char* (*pkpy_CImportHandler)(const char*, int, int*);
 typedef int pkpy_CName;
 typedef int pkpy_CType;

+ 2 - 2
include/pocketpy/vm.h

@@ -104,7 +104,7 @@ struct FrameId{
     Frame* get() const { return &data->operator[](index); }
 };
 
-typedef void(*PrintFunc)(VM*, const char*, int);
+typedef void(*PrintFunc)(const char*, int);
 
 class VM {
     PK_ALWAYS_PASS_BY_POINTER(VM)
@@ -186,7 +186,7 @@ public:
     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){
-        _stdout(this, s.data, s.size);
+        _stdout(s.data, s.size);
     }
 
     template<typename... Args>

+ 4 - 6
src/pocketpy.cpp

@@ -1421,13 +1421,13 @@ void add_module_sys(VM* vm){
 
     vm->bind_func<1>(stdout_, "write", [](VM* vm, ArgsView args) {
         Str& s = CAST(Str&, args[0]);
-        vm->_stdout(vm, s.data, s.size);
+        vm->stdout_write(s);
         return vm->None;
     });
 
     vm->bind_func<1>(stderr_, "write", [](VM* vm, ArgsView args) {
         Str& s = CAST(Str&, args[0]);
-        vm->_stderr(vm, s.data, s.size);
+        vm->_stderr(s.data, s.size);
         return vm->None;
     });
 }
@@ -1532,8 +1532,7 @@ void add_module_traceback(VM* vm){
     vm->bind_func<0>(mod, "print_exc", [](VM* vm, ArgsView args) {
         if(vm->_last_exception==nullptr) vm->ValueError("no exception");
         Exception& e = CAST(Exception&, vm->_last_exception);
-        Str sum = e.summary();
-        vm->_stdout(vm, sum.data, sum.size);
+        vm->stdout_write(e.summary());
         return vm->None;
     });
 
@@ -1559,8 +1558,7 @@ void add_module_dis(VM* vm){
 
     vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) {
         CodeObject_ code = get_code(vm, args[0]);
-        Str msg = vm->disassemble(code);
-        vm->_stdout(vm, msg.data, msg.size);
+        vm->stdout_write(vm->disassemble(code));
         return vm->None;
     });
 

+ 1 - 1
src/pocketpy_c.cpp

@@ -581,7 +581,7 @@ void pkpy_compile_to_string(pkpy_vm* vm_handle, const char* source, const char*
 
 void pkpy_set_output_handler(pkpy_vm* vm_handle, pkpy_COutputHandler handler){
     VM* vm = (VM*) vm_handle;
-    vm->_stdout = reinterpret_cast<PrintFunc>(handler);
+    vm->_stdout = handler;
 }
 
 void pkpy_set_import_handler(pkpy_vm* vm_handle, pkpy_CImportHandler handler){

+ 4 - 6
src/vm.cpp

@@ -71,12 +71,10 @@ namespace pkpy{
     VM::VM(bool enable_os) : heap(this), enable_os(enable_os) {
         this->vm = this;
         this->_c.error = nullptr;
-        _stdout = [](VM* vm, const char* buf, int size) {
-            PK_UNUSED(vm);
+        _stdout = [](const char* buf, int size) {
             std::cout.write(buf, size);
         };
-        _stderr = [](VM* vm, const char* buf, int size) {
-            PK_UNUSED(vm);
+        _stderr = [](const char* buf, int size) {
             std::cerr.write(buf, size);
         };
         callstack.reserve(8);
@@ -171,13 +169,13 @@ namespace pkpy{
             return _exec(code, _module);
         }catch (const Exception& e){
             Str sum = e.summary() + "\n";
-            _stderr(this, sum.data, sum.size);
+            _stderr(sum.data, sum.size);
         }
 #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(this, msg.data, msg.size);
+            _stderr(msg.data, msg.size);
         }
 #endif
         callstack.clear();