Преглед изворни кода

Merge pull request #74 from koltenpearson/c_binding_api

some more functionality for the c binding api
BLUELOVETH пре 2 година
родитељ
комит
5237bc1f25
4 измењених фајлова са 35 додато и 5 уклоњено
  1. 17 5
      c_bindings/pocketpy_c.cpp
  2. 11 0
      c_bindings/pocketpy_c.h
  3. 4 0
      c_bindings/test.c
  4. 3 0
      c_bindings/test_answers.txt

+ 17 - 5
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"; \
@@ -547,3 +542,20 @@ 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;
+}
+
+
+bool pkpy_error(pkpy_vm* vm_handle, const char* message) {
+    CVM* vm = (CVM*) vm_handle;
+    ERRHANDLER_OPEN
+    throw Exception("CBindingError", message);
+    ERRHANDLER_CLOSE
+}
+

+ 11 - 0
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*);
@@ -30,6 +37,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);

+ 4 - 0
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"));
@@ -315,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
     //

+ 3 - 0
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