update PrintFunc

This commit is contained in:
blueloveTH 2023-11-29 12:20:38 +08:00
parent 101cf24a36
commit da12d4b8ff
6 changed files with 13 additions and 17 deletions

View File

@ -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)
```

View File

@ -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;

View File

@ -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>

View File

@ -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;
});

View File

@ -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){

View File

@ -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();