diff --git a/docs/quick-start/installation.md b/docs/quick-start/installation.md index 9a0f4f38..b7254dd2 100644 --- a/docs/quick-start/installation.md +++ b/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) ``` \ No newline at end of file diff --git a/include/pocketpy/pocketpy_c.h b/include/pocketpy/pocketpy_c.h index ce61dc65..dbefbcd5 100644 --- a/include/pocketpy/pocketpy_c.h +++ b/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; diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index f453b441..d0050fb1 100644 --- a/include/pocketpy/vm.h +++ b/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 diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index e17fdda0..8eddd471 100644 --- a/src/pocketpy.cpp +++ b/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; }); diff --git a/src/pocketpy_c.cpp b/src/pocketpy_c.cpp index aaa57a8a..870d7288 100644 --- a/src/pocketpy_c.cpp +++ b/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(handler); + vm->_stdout = handler; } void pkpy_set_import_handler(pkpy_vm* vm_handle, pkpy_CImportHandler handler){ diff --git a/src/vm.cpp b/src/vm.cpp index 6ac9b874..74c81bcc 100644 --- a/src/vm.cpp +++ b/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();