This commit is contained in:
blueloveTH 2023-05-24 18:20:17 +08:00
parent ef21ff266b
commit f91c83e4a1
2 changed files with 16 additions and 5 deletions

View File

@ -100,10 +100,12 @@ void gc_marker_ex(CVM* vm) {
for(PyObject* obj: *vm->c_data) if(obj!=nullptr) OBJ_MARK(obj); for(PyObject* obj: *vm->c_data) if(obj!=nullptr) OBJ_MARK(obj);
} }
static OutputHandler stdout_handler = nullptr;
static OutputHandler stderr_handler = nullptr;
static void noop_output_handler(VM* vm, const Str& str) { void pkpy_set_output_handlers(pkpy_vm*, OutputHandler stdout_handler, OutputHandler stderr_handler){
(void) vm; ::stdout_handler = stdout_handler;
(void) str; ::stderr_handler = stderr_handler;
} }
pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) { pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) {
@ -113,8 +115,14 @@ pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) {
vm->_gc_marker_ex = (void (*)(VM*)) gc_marker_ex; vm->_gc_marker_ex = (void (*)(VM*)) gc_marker_ex;
if (!use_stdio) { if (!use_stdio) {
vm->_stdout = noop_output_handler; vm->_stdout = [](VM* vm, const Str& s){
vm->_stderr = noop_output_handler; std::string str = s.str();
if (stdout_handler != nullptr) stdout_handler((pkpy_vm*)vm, str.c_str());
};
vm->_stderr = [](VM* vm, const Str& s){
std::string str = s.str();
if (stderr_handler != nullptr) stderr_handler((pkpy_vm*)vm, str.c_str());
};
} }
return (pkpy_vm*) vm; return (pkpy_vm*) vm;

View File

@ -107,6 +107,9 @@ bool pkpy_check_stack(pkpy_vm*, int free);
//returns the number of elements on the stack //returns the number of elements on the stack
int pkpy_stack_size(pkpy_vm*); int pkpy_stack_size(pkpy_vm*);
typedef void (*OutputHandler)(pkpy_vm*, const char*);
void pkpy_set_output_handlers(pkpy_vm*, OutputHandler stdout_handler, OutputHandler stderr_handler);
#ifdef __cplusplus #ifdef __cplusplus
} }