pocketpy_c.h 4.5 KB

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