pocketpy_c.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. //push the item at index onto the top of the stack (as well as leaving it where
  25. //it is on the stack)
  26. bool pkpy_push(pkpy_vm*, int index);
  27. bool pkpy_push_function(pkpy_vm*, pkpy_function);
  28. bool pkpy_push_int(pkpy_vm*, int);
  29. bool pkpy_push_float(pkpy_vm*, double);
  30. bool pkpy_push_bool(pkpy_vm*, bool);
  31. bool pkpy_push_string(pkpy_vm*, const char*);
  32. bool pkpy_push_stringn(pkpy_vm*, const char*, int length);
  33. bool pkpy_push_voidp(pkpy_vm*, void*);
  34. bool pkpy_push_none(pkpy_vm*);
  35. bool pkpy_set_global(pkpy_vm*, const char* name);
  36. bool pkpy_get_global(pkpy_vm*, const char* name);
  37. //first push callable you want to call
  38. //then push the arguments to send
  39. //argc is the number of arguments that was pushed (not counting the callable)
  40. bool pkpy_call(pkpy_vm*, int argc);
  41. //first push the object the method belongs to (self)
  42. //then push the the argments
  43. //argc is the number of arguments that was pushed (not counting the callable or self)
  44. //name is the name of the method to call on the object
  45. bool pkpy_call_method(pkpy_vm*, const char* name, int argc);
  46. //we will break with the lua api here
  47. //lua uses 1 as the index to the first pushed element for all of these functions
  48. //but we will start counting at zero to match python
  49. //we will allow negative numbers to count backwards from the top
  50. bool pkpy_to_int(pkpy_vm*, int index, int* ret);
  51. bool pkpy_to_float(pkpy_vm*, int index, double* ret);
  52. bool pkpy_to_bool(pkpy_vm*, int index, bool* ret);
  53. bool pkpy_to_voidp(pkpy_vm*, int index, void** ret);
  54. //this method provides a strong reference, you are responsible for freeing the
  55. //string when you are done with it
  56. bool pkpy_to_string(pkpy_vm*, int index, char** ret);
  57. //this method provides a weak reference, it is only valid until the
  58. //next api call
  59. //it is not null terminated
  60. bool pkpy_to_stringn(pkpy_vm*, int index, const char** ret, int* size);
  61. //these do not follow the same error semantics as above, their return values
  62. //just say whether the check succeeded or not, or else return the value asked for
  63. bool pkpy_is_int(pkpy_vm*, int index);
  64. bool pkpy_is_float(pkpy_vm*, int index);
  65. bool pkpy_is_bool(pkpy_vm*, int index);
  66. bool pkpy_is_string(pkpy_vm*, int index);
  67. bool pkpy_is_voidp(pkpy_vm*, int index);
  68. bool pkpy_is_none(pkpy_vm*, int index);
  69. //will return true if global exists
  70. bool pkpy_check_global(pkpy_vm*, const char* name);
  71. //will return true if the vm is currently in an error state
  72. bool pkpy_check_error(pkpy_vm*);
  73. //will return true if at least free empty slots remain on the stack
  74. bool pkpy_check_stack(pkpy_vm*, int free);
  75. //returns the number of elements on the stack
  76. int pkpy_stack_size(pkpy_vm*);
  77. #ifdef __cplusplus
  78. }
  79. #endif
  80. #endif