builtins.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include "pocketpy/common/vector.hpp"
  3. #include "pocketpy/objects/object.hpp"
  4. namespace pkpy{
  5. struct BoundMethod {
  6. PyVar self;
  7. PyVar func;
  8. BoundMethod(PyVar self, PyVar func) : self(self), func(func) {}
  9. void _gc_mark(VM*) const;
  10. };
  11. struct StaticMethod{
  12. PyVar func;
  13. StaticMethod(PyVar func) : func(func) {}
  14. void _gc_mark(VM*) const;
  15. };
  16. struct ClassMethod{
  17. PyVar func;
  18. ClassMethod(PyVar func) : func(func) {}
  19. void _gc_mark(VM*) const;
  20. };
  21. struct Property{
  22. PyVar getter;
  23. PyVar setter;
  24. Property(PyVar getter, PyVar setter) : getter(getter), setter(setter) {}
  25. void _gc_mark(VM*) const;
  26. };
  27. struct Range {
  28. i64 start = 0;
  29. i64 stop = -1;
  30. i64 step = 1;
  31. };
  32. struct StarWrapper{
  33. int level; // either 1 or 2
  34. PyVar obj;
  35. StarWrapper(int level, PyVar obj) : level(level), obj(obj) {}
  36. void _gc_mark(VM*) const;
  37. };
  38. using Bytes = array<unsigned char>;
  39. struct Super{
  40. PyVar first;
  41. Type second;
  42. Super(PyVar first, Type second) : first(first), second(second) {}
  43. void _gc_mark(VM*) const;
  44. };
  45. struct Slice {
  46. PyVar start;
  47. PyVar stop;
  48. PyVar step;
  49. Slice(PyVar start, PyVar stop, PyVar step) : start(start), stop(stop), step(step) {}
  50. void _gc_mark(VM*) const;
  51. };
  52. inline const int kTpIntIndex = 3;
  53. inline const int kTpFloatIndex = 4;
  54. inline bool is_tagged(PyVar p) noexcept { return !p.is_ptr; }
  55. inline bool is_float(PyVar p) noexcept { return p.type.index == kTpFloatIndex; }
  56. inline bool is_int(PyVar p) noexcept { return p.type.index == kTpIntIndex; }
  57. inline bool is_type(PyVar obj, Type type) {
  58. assert(obj != nullptr);
  59. return obj.type == type;
  60. }
  61. struct MappingProxy{
  62. PyObject* obj;
  63. MappingProxy(PyObject* obj) : obj(obj) {}
  64. NameDict& attr() { return obj->attr(); }
  65. void _gc_mark(VM*) const;
  66. };
  67. StrName _type_name(VM* vm, Type type);
  68. template<typename T> T to_void_p(VM*, PyVar);
  69. PyVar from_void_p(VM*, void*);
  70. template<typename T>
  71. obj_get_t<T> PyVar::obj_get(){
  72. if constexpr(is_sso_v<T>){
  73. return as<T>();
  74. }else{
  75. assert(is_ptr);
  76. void* v = ((PyObject*)_1)->_value_ptr();
  77. return *reinterpret_cast<T*>(v);
  78. }
  79. }
  80. #define PK_OBJ_GET(T, obj) ((obj).obj_get<T>())
  81. // deprecated
  82. #define PK_OBJ_MARK(obj) vm->obj_gc_mark(obj)
  83. #define VAR(x) py_var(vm, x)
  84. #define CAST(T, x) py_cast<T>(vm, x)
  85. #define _CAST(T, x) _py_cast<T>(vm, x)
  86. #define CAST_F(x) py_cast<f64>(vm, x)
  87. #define CAST_DEFAULT(T, x, default_value) (x != vm->None) ? py_cast<T>(vm, x) : (default_value)
  88. /*****************************************************************/
  89. #define PY_NULL nullptr
  90. extern PyVar const PY_OP_CALL;
  91. extern PyVar const PY_OP_YIELD;
  92. } // namespace pkpy