mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
update PrintFunc
This commit is contained in:
parent
101cf24a36
commit
da12d4b8ff
@ -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:
|
These two fields are C function pointers `PrintFunc` with the following signature:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
typedef void(*PrintFunc)(VM*, const char*, int)
|
typedef void(*PrintFunc)(const char*, int)
|
||||||
```
|
```
|
@ -12,7 +12,7 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct pkpy_vm_handle pkpy_vm;
|
typedef struct pkpy_vm_handle pkpy_vm;
|
||||||
typedef int (*pkpy_CFunction)(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 unsigned char* (*pkpy_CImportHandler)(const char*, int, int*);
|
||||||
typedef int pkpy_CName;
|
typedef int pkpy_CName;
|
||||||
typedef int pkpy_CType;
|
typedef int pkpy_CType;
|
||||||
|
@ -104,7 +104,7 @@ struct FrameId{
|
|||||||
Frame* get() const { return &data->operator[](index); }
|
Frame* get() const { return &data->operator[](index); }
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef void(*PrintFunc)(VM*, const char*, int);
|
typedef void(*PrintFunc)(const char*, int);
|
||||||
|
|
||||||
class VM {
|
class VM {
|
||||||
PK_ALWAYS_PASS_BY_POINTER(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 _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2, PyObject* _3){ PUSH(_0); PUSH(_1); PUSH(_2); PUSH(_3); }
|
||||||
|
|
||||||
void stdout_write(const Str& s){
|
void stdout_write(const Str& s){
|
||||||
_stdout(this, s.data, s.size);
|
_stdout(s.data, s.size);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
|
@ -1421,13 +1421,13 @@ void add_module_sys(VM* vm){
|
|||||||
|
|
||||||
vm->bind_func<1>(stdout_, "write", [](VM* vm, ArgsView args) {
|
vm->bind_func<1>(stdout_, "write", [](VM* vm, ArgsView args) {
|
||||||
Str& s = CAST(Str&, args[0]);
|
Str& s = CAST(Str&, args[0]);
|
||||||
vm->_stdout(vm, s.data, s.size);
|
vm->stdout_write(s);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_func<1>(stderr_, "write", [](VM* vm, ArgsView args) {
|
vm->bind_func<1>(stderr_, "write", [](VM* vm, ArgsView args) {
|
||||||
Str& s = CAST(Str&, args[0]);
|
Str& s = CAST(Str&, args[0]);
|
||||||
vm->_stderr(vm, s.data, s.size);
|
vm->_stderr(s.data, s.size);
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -1532,8 +1532,7 @@ void add_module_traceback(VM* vm){
|
|||||||
vm->bind_func<0>(mod, "print_exc", [](VM* vm, ArgsView args) {
|
vm->bind_func<0>(mod, "print_exc", [](VM* vm, ArgsView args) {
|
||||||
if(vm->_last_exception==nullptr) vm->ValueError("no exception");
|
if(vm->_last_exception==nullptr) vm->ValueError("no exception");
|
||||||
Exception& e = CAST(Exception&, vm->_last_exception);
|
Exception& e = CAST(Exception&, vm->_last_exception);
|
||||||
Str sum = e.summary();
|
vm->stdout_write(e.summary());
|
||||||
vm->_stdout(vm, sum.data, sum.size);
|
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1559,8 +1558,7 @@ void add_module_dis(VM* vm){
|
|||||||
|
|
||||||
vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) {
|
vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) {
|
||||||
CodeObject_ code = get_code(vm, args[0]);
|
CodeObject_ code = get_code(vm, args[0]);
|
||||||
Str msg = vm->disassemble(code);
|
vm->stdout_write(vm->disassemble(code));
|
||||||
vm->_stdout(vm, msg.data, msg.size);
|
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -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){
|
void pkpy_set_output_handler(pkpy_vm* vm_handle, pkpy_COutputHandler handler){
|
||||||
VM* vm = (VM*) vm_handle;
|
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){
|
void pkpy_set_import_handler(pkpy_vm* vm_handle, pkpy_CImportHandler handler){
|
||||||
|
10
src/vm.cpp
10
src/vm.cpp
@ -71,12 +71,10 @@ namespace pkpy{
|
|||||||
VM::VM(bool enable_os) : heap(this), enable_os(enable_os) {
|
VM::VM(bool enable_os) : heap(this), enable_os(enable_os) {
|
||||||
this->vm = this;
|
this->vm = this;
|
||||||
this->_c.error = nullptr;
|
this->_c.error = nullptr;
|
||||||
_stdout = [](VM* vm, const char* buf, int size) {
|
_stdout = [](const char* buf, int size) {
|
||||||
PK_UNUSED(vm);
|
|
||||||
std::cout.write(buf, size);
|
std::cout.write(buf, size);
|
||||||
};
|
};
|
||||||
_stderr = [](VM* vm, const char* buf, int size) {
|
_stderr = [](const char* buf, int size) {
|
||||||
PK_UNUSED(vm);
|
|
||||||
std::cerr.write(buf, size);
|
std::cerr.write(buf, size);
|
||||||
};
|
};
|
||||||
callstack.reserve(8);
|
callstack.reserve(8);
|
||||||
@ -171,13 +169,13 @@ namespace pkpy{
|
|||||||
return _exec(code, _module);
|
return _exec(code, _module);
|
||||||
}catch (const Exception& e){
|
}catch (const Exception& e){
|
||||||
Str sum = e.summary() + "\n";
|
Str sum = e.summary() + "\n";
|
||||||
_stderr(this, sum.data, sum.size);
|
_stderr(sum.data, sum.size);
|
||||||
}
|
}
|
||||||
#if !PK_DEBUG_FULL_EXCEPTION
|
#if !PK_DEBUG_FULL_EXCEPTION
|
||||||
catch (const std::exception& e) {
|
catch (const std::exception& e) {
|
||||||
Str msg = "An std::exception occurred! It could be a bug.\n";
|
Str msg = "An std::exception occurred! It could be a bug.\n";
|
||||||
msg = msg + e.what() + "\n";
|
msg = msg + e.what() + "\n";
|
||||||
_stderr(this, msg.data, msg.size);
|
_stderr(msg.data, msg.size);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
callstack.clear();
|
callstack.clear();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user