This commit is contained in:
blueloveTH 2023-05-27 16:41:47 +08:00
parent 959fa9387a
commit 6db30e61ce
2 changed files with 22 additions and 36 deletions

View File

@ -34,19 +34,17 @@ struct LuaStack: public ValueStackImpl<32>{
}; };
#define ERRHANDLER_OPEN \ #define ERRHANDLER_OPEN \
if (vm->c_data->size() > 0 && vm->c_data->top() == nullptr) \ if (vm->error != nullptr) \
return false; \ return false; \
try { try {
#define ERRHANDLER_CLOSE \ #define ERRHANDLER_CLOSE \
} catch(Exception& e ) { \ } catch(Exception& e ) { \
vm->c_data->push(py_var(vm, e)); \ vm->error = py_var(vm, e); \
vm->c_data->push(NULL); \
return false; \ return false; \
} catch(const std::exception& re){ \ } catch(const std::exception& re){ \
auto e = Exception("std::exception", re.what()); \ auto e = Exception("std::exception", re.what()); \
vm->c_data->push(py_var(vm, e)); \ vm->error = py_var(vm, e); \
vm->c_data->push(NULL); \
return false; \ return false; \
} }
@ -55,8 +53,11 @@ class CVM : public VM {
public : public :
LuaStack* c_data; LuaStack* c_data;
PyObject* error;
CVM(bool enable_os=true) : VM(enable_os) { CVM(bool enable_os=true) : VM(enable_os) {
c_data = new LuaStack(); c_data = new LuaStack();
error = nullptr;
} }
~CVM() { ~CVM() {
@ -109,16 +110,14 @@ static void unpack_return(CVM* vm, PyObject* ret) {
bool pkpy_clear_error(pkpy_vm* vm_handle, char** message) { bool pkpy_clear_error(pkpy_vm* vm_handle, char** message) {
CVM* vm = (CVM*) vm_handle; CVM* vm = (CVM*) vm_handle;
if (vm->c_data->size() == 0 || vm->c_data->top() != nullptr) // no error
return false; if (vm->error == nullptr) return false;
Exception& e = _py_cast<Exception&>(vm, vm->error);
vm->c_data->pop();
Exception& e = py_cast<Exception&>(vm, vm->c_data->top());
if (message != nullptr) if (message != nullptr)
*message = e.summary().c_str_dup(); *message = e.summary().c_str_dup();
else else
std::cerr << "ERROR: " << e.summary() << "\n"; std::cerr << "ERROR: " << e.summary() << "\n";
vm->error = nullptr;
vm->c_data->clear(); vm->c_data->clear();
vm->callstack.clear(); vm->callstack.clear();
vm->s_data.clear(); vm->s_data.clear();
@ -127,6 +126,7 @@ bool pkpy_clear_error(pkpy_vm* vm_handle, char** message) {
void gc_marker_ex(CVM* vm) { 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);
if(vm->error != nullptr) OBJ_MARK(vm->error);
} }
static OutputHandler stdout_handler = nullptr; static OutputHandler stdout_handler = nullptr;
@ -138,7 +138,6 @@ void pkpy_set_output_handlers(pkpy_vm*, OutputHandler stdout_handler, OutputHand
} }
pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) { pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) {
CVM* vm = new CVM(enable_os); CVM* vm = new CVM(enable_os);
vm->c_data = new LuaStack(); vm->c_data = new LuaStack();
vm->heap._gc_marker_ex = (void (*)(VM*)) gc_marker_ex; vm->heap._gc_marker_ex = (void (*)(VM*)) gc_marker_ex;
@ -197,11 +196,10 @@ PyObject* c_function_wrapper(VM* vm, ArgsView args) {
int retc = f(cvm); int retc = f(cvm);
// propagate_if_errored // propagate_if_errored
if (!cvm->c_data->empty() && cvm->c_data->top() == nullptr){ if (cvm->error != nullptr){
cvm->c_data->pop(); // pop nullptr Exception e = _py_cast<Exception&>(vm, cvm->error);
Exception& e = _py_cast<Exception&>(vm, cvm->c_data->popx()); cvm->error = nullptr;
tmp.restore(); tmp.restore();
// throw e;
vm->_error(e); vm->_error(e);
} }
tmp.restore(); tmp.restore();
@ -320,11 +318,8 @@ bool pkpy_get_global(pkpy_vm* vm_handle, const char* name) {
bool pkpy_call(pkpy_vm* vm_handle, int argc) { bool pkpy_call(pkpy_vm* vm_handle, int argc) {
CVM* vm = (CVM*) vm_handle; CVM* vm = (CVM*) vm_handle;
ERRHANDLER_OPEN ERRHANDLER_OPEN
int callable_index = vm->c_data->size() - argc - 1; int callable_index = vm->c_data->size() - argc - 1;
PyObject* callable = vm->c_data->at(callable_index);
PyObject* callable = vm->c_data->begin()[callable_index];
vm->s_data.push(callable); vm->s_data.push(callable);
vm->s_data.push(PY_NULL); vm->s_data.push(PY_NULL);
@ -346,7 +341,7 @@ bool pkpy_call_method(pkpy_vm* vm_handle, const char* name, int argc) {
ERRHANDLER_OPEN ERRHANDLER_OPEN
int self_index = vm->c_data->size() - argc - 1; int self_index = vm->c_data->size() - argc - 1;
PyObject* self = vm->c_data->begin()[self_index]; PyObject* self = vm->c_data->at(self_index);
PyObject* callable = vm->get_unbound_method(self, name, &self); PyObject* callable = vm->get_unbound_method(self, name, &self);
@ -357,17 +352,12 @@ bool pkpy_call_method(pkpy_vm* vm_handle, const char* name, int argc) {
vm->s_data.push(vm->c_data->at(self_index + i + 1)); vm->s_data.push(vm->c_data->at(self_index + i + 1));
PyObject* o = vm->vectorcall(argc); PyObject* o = vm->vectorcall(argc);
vm->c_data->shrink(argc + 1); vm->c_data->shrink(argc + 1);
unpack_return(vm, o); unpack_return(vm, o);
return true; return true;
ERRHANDLER_CLOSE ERRHANDLER_CLOSE
} }
static int lua_to_cstack_index(int index, int size) { static int lua_to_cstack_index(int index, int size) {
if (index < 0) index = size + index; if (index < 0) index = size + index;
return index; return index;
@ -494,9 +484,7 @@ bool pkpy_check_global(pkpy_vm* vm_handle, const char* name) {
bool pkpy_check_error(pkpy_vm* vm_handle) { bool pkpy_check_error(pkpy_vm* vm_handle) {
CVM* vm = (CVM*) vm_handle; CVM* vm = (CVM*) vm_handle;
if (vm->c_data->size() > 0 && vm->c_data->top() == nullptr) return vm->error != nullptr;
return true;
return false;
} }
@ -528,11 +516,8 @@ bool pkpy_push(pkpy_vm* vm_handle, int index) {
bool pkpy_error(pkpy_vm* vm_handle, const char* name, const char* message) { bool pkpy_error(pkpy_vm* vm_handle, const char* name, const char* message) {
CVM* vm = (CVM*) vm_handle; CVM* vm = (CVM*) vm_handle;
// already in error state // already in error state
if (vm->c_data->size() > 0 && vm->c_data->top() == nullptr) { if (vm->error != nullptr) return false;
return false; vm->error = py_var(vm, Exception(name, message));
}
vm->c_data->safe_push(py_var(vm, Exception(name, message)));
vm->c_data->safe_push(NULL);
return false; return false;
} }

View File

@ -39,9 +39,10 @@ void error_impl(pkpy_vm* vm, bool result, int lineno) {
exit(1); exit(1);
} else { } else {
char* message; char* message;
if (!pkpy_clear_error(vm, &message)) if (!pkpy_clear_error(vm, &message)){
printf("clear error reported everything was fine\n"); printf("clear error reported everything was fine\n");
else { exit(1);
} else {
printf("successfully errored with this message: \n"); printf("successfully errored with this message: \n");
printf("%s\n", message); printf("%s\n", message);
free(message); free(message);