From e2bf81d7f2689b6604de250271397a5d68999dac Mon Sep 17 00:00:00 2001 From: Kolten Pearson Date: Mon, 8 May 2023 20:41:44 -0600 Subject: [PATCH 1/4] added a pkpy_vm_push method --- c_bindings/pocketpy_c.cpp | 8 ++++++++ c_bindings/pocketpy_c.h | 4 ++++ c_bindings/test.c | 2 ++ 3 files changed, 14 insertions(+) diff --git a/c_bindings/pocketpy_c.cpp b/c_bindings/pocketpy_c.cpp index ece6996d..cec58df0 100644 --- a/c_bindings/pocketpy_c.cpp +++ b/c_bindings/pocketpy_c.cpp @@ -547,3 +547,11 @@ bool pkpy_pop(pkpy_vm* vm_handle, int n) { vm->c_data->shrink(n); return true; } + + +bool pkpy_push(pkpy_vm* vm_handle, int index) { + CVM* vm = (CVM*) vm_handle; + index = lua_to_cstack_index(index, vm->c_data->size()); + vm->c_data->push(vm->c_data->begin()[index]); + return true; +} diff --git a/c_bindings/pocketpy_c.h b/c_bindings/pocketpy_c.h index 7924d75c..143f5019 100644 --- a/c_bindings/pocketpy_c.h +++ b/c_bindings/pocketpy_c.h @@ -30,6 +30,10 @@ typedef int (*pkpy_function)(pkpy_vm*); bool pkpy_pop(pkpy_vm*, int n); +//push the item at index onto the top of the stack (as well as leaving it where +//it is on the stack) +bool pkpy_push(pkpy_vm*, int index); + bool pkpy_push_function(pkpy_vm*, pkpy_function); bool pkpy_push_int(pkpy_vm*, int); bool pkpy_push_float(pkpy_vm*, double); diff --git a/c_bindings/test.c b/c_bindings/test.c index 12336094..d95a1d44 100644 --- a/c_bindings/test.c +++ b/c_bindings/test.c @@ -209,6 +209,8 @@ int main(int argc, char** argv) { check(pkpy_is_string(vm, -3)); check(pkpy_is_none(vm, -2)); check(pkpy_is_voidp(vm, -1)); + check(pkpy_push(vm, -3)); + check(pkpy_is_string(vm, -1)); printf("\ntesting error catching\n"); error(pkpy_vm_run(vm, "let's make sure syntax errors get caught")); From ae34a6304a29831bb263b91ec5abb32656687421 Mon Sep 17 00:00:00 2001 From: Kolten Pearson Date: Mon, 8 May 2023 20:45:59 -0600 Subject: [PATCH 2/4] gcc didn't like us catching the runtime_error --- c_bindings/pocketpy_c.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/c_bindings/pocketpy_c.cpp b/c_bindings/pocketpy_c.cpp index cec58df0..7bbad43d 100644 --- a/c_bindings/pocketpy_c.cpp +++ b/c_bindings/pocketpy_c.cpp @@ -13,11 +13,6 @@ using namespace pkpy; << "this probably means pocketpy itself has a bug!\n" \ << e.what() << "\n"; \ exit(2); \ - } catch(std::runtime_error& e) { \ - std::cerr << "ERROR: a std::runtime_error " \ - << "this probably means pocketpy itself has a bug!\n" \ - << e.what() << "\n"; \ - exit(2); \ } catch(...) { \ std::cerr << "ERROR: a unknown exception was thrown from " << __func__ \ << "\nthis probably means pocketpy itself has a bug!\n"; \ From ceab1e060914ffdd10e8f02a03f658f7934bd2ee Mon Sep 17 00:00:00 2001 From: Kolten Pearson Date: Tue, 9 May 2023 18:05:43 -0600 Subject: [PATCH 3/4] added a pkpy_error method --- c_bindings/pocketpy_c.cpp | 9 +++++++++ c_bindings/pocketpy_c.h | 7 +++++++ c_bindings/test.c | 2 ++ c_bindings/test_answers.txt | 3 +++ run_c_binding_test.sh | 4 ++-- 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/c_bindings/pocketpy_c.cpp b/c_bindings/pocketpy_c.cpp index 7bbad43d..91daea48 100644 --- a/c_bindings/pocketpy_c.cpp +++ b/c_bindings/pocketpy_c.cpp @@ -550,3 +550,12 @@ bool pkpy_push(pkpy_vm* vm_handle, int index) { vm->c_data->push(vm->c_data->begin()[index]); return true; } + + +bool pkpy_error(pkpy_vm* vm_handle, const char* message) { + CVM* vm = (CVM*) vm_handle; + ERRHANDLER_OPEN + throw Exception("CBindingError", message); + ERRHANDLER_CLOSE +} + diff --git a/c_bindings/pocketpy_c.h b/c_bindings/pocketpy_c.h index 143f5019..96b969c6 100644 --- a/c_bindings/pocketpy_c.h +++ b/c_bindings/pocketpy_c.h @@ -22,6 +22,13 @@ typedef struct pkpy_vm_handle pkpy_vm; bool pkpy_clear_error(pkpy_vm*, char** message); //NOTE you are responsible for freeing message +//this will cause the vm to enter an error state and report the given message +//when queried +//note that at the moment this is more like a panic than throwing an error +//the user will not be able to catch it with python code +bool pkpy_error(pkpy_vm*, const char* message); + + pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os); bool pkpy_vm_run(pkpy_vm*, const char* source); void pkpy_vm_destroy(pkpy_vm*); diff --git a/c_bindings/test.c b/c_bindings/test.c index d95a1d44..2fdac240 100644 --- a/c_bindings/test.c +++ b/c_bindings/test.c @@ -317,6 +317,8 @@ int main(int argc, char** argv) { //maybe worth fixing someday, but for now it is functionating as implemented error(pkpy_vm_run(vm, "try : test_error_propagate(); except NameError : pass")); + error(pkpy_error(vm, "test direct error mechanism")); + //more complicated error handling // diff --git a/c_bindings/test_answers.txt b/c_bindings/test_answers.txt index 253c664e..94b5762e 100644 --- a/c_bindings/test_answers.txt +++ b/c_bindings/test_answers.txt @@ -61,3 +61,6 @@ NameError: testing error throwing from python successfully errored with this message: Traceback (most recent call last): NameError: could not find requested global +successfully errored with this message: +Traceback (most recent call last): +CBindingError: test direct error mechanism diff --git a/run_c_binding_test.sh b/run_c_binding_test.sh index 172e1660..116d4c65 100644 --- a/run_c_binding_test.sh +++ b/run_c_binding_test.sh @@ -19,8 +19,8 @@ echo "checking results (they should be identical)" diff -q -s binding_test_scratch c_bindings/test_answers.txt echo "cleaning up" -rm pocketpy_c.o +#rm pocketpy_c.o rm test.o rm binding_test_scratch -rm c_binding_test +#rm c_binding_test From d6bd147f6f49d1ce82cf7a69c21877f9df090683 Mon Sep 17 00:00:00 2001 From: Kolten Pearson Date: Tue, 9 May 2023 18:22:20 -0600 Subject: [PATCH 4/4] restored c binding test cleanup --- run_c_binding_test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/run_c_binding_test.sh b/run_c_binding_test.sh index 116d4c65..172e1660 100644 --- a/run_c_binding_test.sh +++ b/run_c_binding_test.sh @@ -19,8 +19,8 @@ echo "checking results (they should be identical)" diff -q -s binding_test_scratch c_bindings/test_answers.txt echo "cleaning up" -#rm pocketpy_c.o +rm pocketpy_c.o rm test.o rm binding_test_scratch -#rm c_binding_test +rm c_binding_test