Merge pull request #74 from koltenpearson/c_binding_api

some more functionality for the c binding api
This commit is contained in:
BLUELOVETH 2023-05-10 10:07:37 +08:00 committed by GitHub
commit 5237bc1f25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 5 deletions

View File

@ -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
}

View File

@ -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);

View File

@ -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
//

View File

@ -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