pocketpy_c.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef POCKETPY_C_H
  2. #define POCKETPY_C_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. typedef struct pkpy_vm_handle pkpy_vm;
  9. //we we take a lot of inspiration from the lua api for these bindings
  10. //the key difference being most methods return a bool,
  11. //true if it succeeded false if it did not
  12. //if a method returns false call the pkpy_clear_error method to check the error and clear it
  13. //if pkpy_clear_error returns false it means that no error was set, and it takes no action
  14. //if pkpy_clear_error returns true it means there was an error and it was cleared,
  15. //it will provide a string summary of the error in the message parameter (if it is not NULL)
  16. //if null is passed in as message, and it will just print the message to stderr
  17. bool pkpy_clear_error(pkpy_vm*, char** message);
  18. //NOTE you are responsible for freeing message
  19. pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os);
  20. bool pkpy_vm_run(pkpy_vm*, const char* source);
  21. void pkpy_vm_destroy(pkpy_vm*);
  22. typedef int (*pkpy_function)(pkpy_vm*);
  23. bool pkpy_pop(pkpy_vm*, int n);
  24. bool pkpy_push_function(pkpy_vm*, pkpy_function);
  25. bool pkpy_push_int(pkpy_vm*, int);
  26. bool pkpy_push_float(pkpy_vm*, double);
  27. bool pkpy_push_bool(pkpy_vm*, bool);
  28. bool pkpy_push_string(pkpy_vm*, const char*);
  29. bool pkpy_push_stringn(pkpy_vm*, const char*, int length);
  30. bool pkpy_push_voidp(pkpy_vm*, void*);
  31. bool pkpy_push_none(pkpy_vm*);
  32. bool pkpy_set_global(pkpy_vm*, const char* name);
  33. bool pkpy_get_global(pkpy_vm*, const char* name);
  34. //first push callable you want to call
  35. //then push the arguments to send
  36. //argc is the number of arguments that was pushed (not counting the callable)
  37. bool pkpy_call(pkpy_vm*, int argc);
  38. //first push the object the method belongs to (self)
  39. //then push the the argments
  40. //argc is the number of arguments that was pushed (not counting the callable or self)
  41. //name is the name of the method to call on the object
  42. bool pkpy_call_method(pkpy_vm*, const char* name, int argc);
  43. //we will break with the lua api here
  44. //lua uses 1 as the index to the first pushed element for all of these functions
  45. //but we will start counting at zero to match python
  46. //we will allow negative numbers to count backwards from the top
  47. bool pkpy_to_int(pkpy_vm*, int index, int* ret);
  48. bool pkpy_to_float(pkpy_vm*, int index, double* ret);
  49. bool pkpy_to_bool(pkpy_vm*, int index, bool* ret);
  50. bool pkpy_to_voidp(pkpy_vm*, int index, void** ret);
  51. //this method provides a strong reference, you are responsible for freeing the
  52. //string when you are done with it
  53. bool pkpy_to_string(pkpy_vm*, int index, char** ret);
  54. //this method provides a weak reference, it is only valid until the
  55. //next api call
  56. //it is not null terminated
  57. bool pkpy_to_stringn(pkpy_vm*, int index, const char** ret, int* size);
  58. //these do not follow the same error semantics as above, their return values
  59. //just say whether the check succeeded or not, or else return the value asked for
  60. bool pkpy_is_int(pkpy_vm*, int index);
  61. bool pkpy_is_float(pkpy_vm*, int index);
  62. bool pkpy_is_bool(pkpy_vm*, int index);
  63. bool pkpy_is_string(pkpy_vm*, int index);
  64. bool pkpy_is_voidp(pkpy_vm*, int index);
  65. bool pkpy_is_none(pkpy_vm*, int index);
  66. //will return true if global exists
  67. bool pkpy_check_global(pkpy_vm*, const char* name);
  68. //will return true if the vm is currently in an error state
  69. bool pkpy_check_error(pkpy_vm*);
  70. //will return true if at least free empty slots remain on the stack
  71. bool pkpy_check_stack(pkpy_vm*, int free);
  72. //returns the number of elements on the stack
  73. int pkpy_stack_size(pkpy_vm*);
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. #endif