blueloveTH 1 год назад
Родитель
Сommit
1003f3a6df
3 измененных файлов с 9 добавлено и 11 удалено
  1. 1 6
      c_bindings/test.c
  2. 6 1
      include/pocketpy/codeobject.h
  3. 2 4
      src/pocketpy_c.cpp

+ 1 - 6
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);
     }
 }

+ 6 - 1
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;
 }
 

+ 2 - 4
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<LuaStyleFuncC>(args.begin());
+    pkpy_CFunction f = lambda_get_userdata<pkpy_CFunction>(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