From 1003f3a6df11e2990ad6553e5e5efc277eb11866 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 8 May 2024 16:36:05 +0800 Subject: [PATCH] fix c binding bug --- c_bindings/test.c | 7 +------ include/pocketpy/codeobject.h | 7 ++++++- src/pocketpy_c.cpp | 6 ++---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/c_bindings/test.c b/c_bindings/test.c index 1800d871..10b1e91e 100644 --- a/c_bindings/test.c +++ b/c_bindings/test.c @@ -7,14 +7,9 @@ void check_impl(pkpy_vm* vm, bool result, int lineno) { if (!result) { printf("ERROR: failed where it should have succeed at line %i\n", lineno); - char* message; - if (!pkpy_clear_error(vm, &message)) { + if (!pkpy_clear_error(vm, NULL)) { printf("clear error reported everything was fine\n"); - exit(1); } - - printf("%s\n", message); - free(message); exit(1); } } diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index 48d3184f..25ede579 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -190,7 +190,12 @@ T& lambda_get_userdata(PyObject** p){ if(p[-1] != PY_NULL) ud = &PK_OBJ_GET(NativeFunc, p[-1])._userdata; else ud = &PK_OBJ_GET(NativeFunc, p[-2])._userdata; T* out; - if(!any_cast(*ud, &out)) throw std::runtime_error("lambda_get_userdata: any_cast failed"); + if(!any_cast(*ud, &out)){ + const char* expected = typeid(T).name(); + const char* actual = ud->type_id().name(); + Str error = _S("lambda_get_userdata: any_cast failed: expected ", expected, ", got ", actual); + throw std::runtime_error(error.c_str()); + } return *out; } diff --git a/src/pocketpy_c.cpp b/src/pocketpy_c.cpp index bc8b794f..183ef986 100644 --- a/src/pocketpy_c.cpp +++ b/src/pocketpy_c.cpp @@ -5,8 +5,6 @@ using namespace pkpy; -typedef int (*LuaStyleFuncC)(VM*); - #define PK_ASSERT_N_EXTRA_ELEMENTS(n) \ int __ex_count = count_extra_elements(vm, n); \ if(__ex_count < n){ \ @@ -334,12 +332,12 @@ struct TempViewPopper{ // function static PyObject* c_function_wrapper(VM* vm, ArgsView args) { - LuaStyleFuncC f = lambda_get_userdata(args.begin()); + pkpy_CFunction f = lambda_get_userdata(args.begin()); PyObject** curr_sp = vm->s_data._sp; vm->__c.s_view.push(args); TempViewPopper _tvp(vm); - int retc = f(vm); // may raise, _tvp will handle this via RAII + int retc = f((pkpy_vm*)vm); // may raise, _tvp will handle this via RAII _tvp.restore(); // propagate_if_errored