obj.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include "safestl.h"
  3. struct CodeObject;
  4. struct Frame;
  5. struct BaseRef;
  6. class VM;
  7. //typedef PyVar (*_CppFuncRaw)(VM*, const pkpy::Args&);
  8. typedef std::function<PyVar(VM*, const pkpy::Args&)> _CppFuncRaw;
  9. typedef pkpy::shared_ptr<CodeObject> _Code;
  10. struct _CppFunc {
  11. _CppFuncRaw f;
  12. int argc; // DONOT include self
  13. bool method;
  14. _CppFunc(_CppFuncRaw f, int argc, bool method) : f(f), argc(argc), method(method) {}
  15. inline PyVar operator()(VM* vm, const pkpy::Args& args) const;
  16. };
  17. struct Function {
  18. _Str name;
  19. _Code code;
  20. std::vector<_Str> args;
  21. _Str starredArg; // empty if no *arg
  22. PyVarDict kwArgs; // empty if no k=v
  23. std::vector<_Str> kwArgsOrder;
  24. bool hasName(const _Str& val) const {
  25. bool _0 = std::find(args.begin(), args.end(), val) != args.end();
  26. bool _1 = starredArg == val;
  27. bool _2 = kwArgs.find(val) != kwArgs.end();
  28. return _0 || _1 || _2;
  29. }
  30. };
  31. struct _BoundMethod {
  32. PyVar obj;
  33. PyVar method;
  34. };
  35. struct _Range {
  36. i64 start = 0;
  37. i64 stop = -1;
  38. i64 step = 1;
  39. };
  40. struct _Slice {
  41. int start = 0;
  42. int stop = 0x7fffffff;
  43. void normalize(int len){
  44. if(start < 0) start += len;
  45. if(stop < 0) stop += len;
  46. if(start < 0) start = 0;
  47. if(stop > len) stop = len;
  48. }
  49. };
  50. class BaseIterator {
  51. protected:
  52. VM* vm;
  53. PyVar _ref; // keep a reference to the object so it will not be deleted while iterating
  54. public:
  55. virtual PyVar next() = 0;
  56. virtual bool hasNext() = 0;
  57. PyVarRef var;
  58. BaseIterator(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {}
  59. virtual ~BaseIterator() = default;
  60. };
  61. typedef pkpy::shared_ptr<Function> _Func;
  62. typedef pkpy::shared_ptr<BaseIterator> _Iterator;
  63. struct PyObject {
  64. PyVar type;
  65. PyVarDict attribs;
  66. inline bool is_type(const PyVar& type) const noexcept{ return this->type == type; }
  67. inline virtual void* value() = 0;
  68. PyObject(const PyVar& type) : type(type) {}
  69. virtual ~PyObject() = default;
  70. };
  71. template <typename T>
  72. struct Py_ : PyObject {
  73. T _value;
  74. Py_(const PyVar& type, T val) : PyObject(type), _value(val) {}
  75. virtual void* value() override { return &_value; }
  76. };
  77. // Unsafe cast from PyObject to C++ type
  78. #define OBJ_GET(T, obj) (*static_cast<T*>((obj)->value()))
  79. #define OBJ_NAME(obj) OBJ_GET(_Str, (obj)->attribs[__name__])
  80. #define OBJ_TP_NAME(obj) OBJ_GET(_Str, (obj)->type->attribs[__name__])
  81. #define PY_CLASS(mod, name) \
  82. inline static PyVar _type(VM* vm) { return vm->_modules[#mod]->attribs[#name]; } \
  83. inline static const char* _name() { return #name; }
  84. #define PY_BUILTIN_CLASS(name) inline static PyVar _type(VM* vm) { return vm->_tp_##name; }