cffi.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include "common.h"
  3. #include "vm.h"
  4. #include "_generated.h"
  5. namespace pkpy {
  6. #define PY_CLASS(T, mod, name) \
  7. static Type _type(VM* vm) { \
  8. PK_LOCAL_STATIC const std::pair<StrName, StrName> _path(#mod, #name); \
  9. return PK_OBJ_GET(Type, vm->_modules[_path.first]->attr()[_path.second]); \
  10. } \
  11. static void _check_type(VM* vm, PyObject* val){ \
  12. if(!vm->isinstance(val, T::_type(vm))){ \
  13. vm->TypeError("expected '" #mod "." #name "', got " + OBJ_NAME(vm->_t(val)).escape()); \
  14. } \
  15. } \
  16. static PyObject* register_class(VM* vm, PyObject* mod, Type base=0) { \
  17. if(OBJ_NAME(mod) != #mod) { \
  18. Str msg = fmt("register_class() failed: ", OBJ_NAME(mod), " != ", #mod); \
  19. throw std::runtime_error(msg.str()); \
  20. } \
  21. PyObject* type = vm->new_type_object(mod, #name, base); \
  22. T::_register(vm, mod, type); \
  23. return type; \
  24. }
  25. #define VAR_T(T, ...) vm->heap.gcnew<T>(T::_type(vm), __VA_ARGS__)
  26. struct VoidP{
  27. PY_CLASS(VoidP, c, void_p)
  28. void* ptr;
  29. VoidP(const void* ptr): ptr(const_cast<void*>(ptr)){}
  30. bool operator==(const VoidP& other) const {
  31. return ptr == other.ptr;
  32. }
  33. bool operator!=(const VoidP& other) const {
  34. return ptr != other.ptr;
  35. }
  36. bool operator<(const VoidP& other) const { return ptr < other.ptr; }
  37. bool operator<=(const VoidP& other) const { return ptr <= other.ptr; }
  38. bool operator>(const VoidP& other) const { return ptr > other.ptr; }
  39. bool operator>=(const VoidP& other) const { return ptr >= other.ptr; }
  40. Str hex() const{
  41. std::stringstream ss; // hex
  42. ss << std::hex << reinterpret_cast<intptr_t>(ptr);
  43. return "0x" + ss.str();
  44. }
  45. static void _register(VM* vm, PyObject* mod, PyObject* type);
  46. };
  47. inline PyObject* py_var(VM* vm, const void* p){
  48. return VAR_T(VoidP, p);
  49. }
  50. #define POINTER_VAR(Tp, NAME) \
  51. inline PyObject* py_var(VM* vm, Tp val){ \
  52. const static std::pair<StrName, StrName> P("c", NAME); \
  53. PyObject* type = vm->_modules[P.first]->attr(P.second); \
  54. return vm->heap.gcnew<VoidP>(PK_OBJ_GET(Type, type), val); \
  55. }
  56. POINTER_VAR(char*, "char_p")
  57. // const char* is special, need to rethink about it
  58. POINTER_VAR(const unsigned char*, "uchar_p")
  59. POINTER_VAR(const short*, "short_p")
  60. POINTER_VAR(const unsigned short*, "ushort_p")
  61. POINTER_VAR(const int*, "int_p")
  62. POINTER_VAR(const unsigned int*, "uint_p")
  63. POINTER_VAR(const long*, "long_p")
  64. POINTER_VAR(const unsigned long*, "ulong_p")
  65. POINTER_VAR(const long long*, "longlong_p")
  66. POINTER_VAR(const unsigned long long*, "ulonglong_p")
  67. POINTER_VAR(const float*, "float_p")
  68. POINTER_VAR(const double*, "double_p")
  69. POINTER_VAR(const bool*, "bool_p")
  70. #undef POINTER_VAR
  71. struct C99Struct{
  72. PY_CLASS(C99Struct, c, struct)
  73. static constexpr int INLINE_SIZE = 24;
  74. char _inlined[INLINE_SIZE];
  75. char* p;
  76. int size;
  77. C99Struct(int new_size, bool zero_init=true){
  78. this->size = new_size;
  79. if(size <= INLINE_SIZE){
  80. p = _inlined;
  81. }else{
  82. p = (char*)malloc(size);
  83. }
  84. if(zero_init) memset(p, 0, size);
  85. }
  86. C99Struct(void* p, int size): C99Struct(size, false){
  87. if(p != nullptr) memcpy(this->p, p, size);
  88. }
  89. C99Struct(const C99Struct& other): C99Struct(other.p, other.size){}
  90. ~C99Struct(){ if(p!=_inlined) free(p); }
  91. static void _register(VM* vm, PyObject* mod, PyObject* type);
  92. };
  93. static_assert(sizeof(Py_<C99Struct>) <= 64);
  94. static_assert(sizeof(Py_<Tuple>) <= 64);
  95. /***********************************************/
  96. template<typename Tp>
  97. Tp to_void_p(VM* vm, PyObject* var){
  98. static_assert(std::is_pointer_v<Tp>);
  99. if(var == vm->None) return nullptr; // None can be casted to any pointer implicitly
  100. VoidP& p = CAST(VoidP&, var);
  101. return reinterpret_cast<Tp>(p.ptr);
  102. }
  103. /*****************************************************************/
  104. void add_module_c(VM* vm);
  105. } // namespace pkpy