obj.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #pragma once
  2. #include "common.h"
  3. #include "namedict.h"
  4. #include "tuplelist.h"
  5. namespace pkpy {
  6. struct Frame;
  7. class VM;
  8. #if PK_ENABLE_STD_FUNCTION
  9. using NativeFuncC = std::function<PyObject*(VM*, ArgsView)>;
  10. #else
  11. typedef PyObject* (*NativeFuncC)(VM*, ArgsView);
  12. #endif
  13. enum class BindType{
  14. DEFAULT,
  15. STATICMETHOD,
  16. CLASSMETHOD,
  17. };
  18. struct BoundMethod {
  19. PyObject* self;
  20. PyObject* func;
  21. BoundMethod(PyObject* self, PyObject* func) : self(self), func(func) {}
  22. };
  23. struct StaticMethod{
  24. PyObject* func;
  25. StaticMethod(PyObject* func) : func(func) {}
  26. };
  27. struct ClassMethod{
  28. PyObject* func;
  29. ClassMethod(PyObject* func) : func(func) {}
  30. };
  31. struct Property{
  32. PyObject* getter;
  33. PyObject* setter;
  34. Str signature;
  35. Property(PyObject* getter, PyObject* setter, Str signature) : getter(getter), setter(setter), signature(signature) {}
  36. };
  37. struct Range {
  38. i64 start = 0;
  39. i64 stop = -1;
  40. i64 step = 1;
  41. };
  42. struct StarWrapper{
  43. int level; // either 1 or 2
  44. PyObject* obj;
  45. StarWrapper(int level, PyObject* obj) : level(level), obj(obj) {}
  46. };
  47. struct Bytes{
  48. unsigned char* _data;
  49. int _size;
  50. int size() const noexcept { return _size; }
  51. int operator[](int i) const noexcept { return (int)_data[i]; }
  52. const unsigned char* data() const noexcept { return _data; }
  53. bool operator==(const Bytes& rhs) const;
  54. bool operator!=(const Bytes& rhs) const;
  55. Str str() const noexcept { return Str((char*)_data, _size); }
  56. std::string_view sv() const noexcept { return std::string_view((char*)_data, _size); }
  57. Bytes() : _data(nullptr), _size(0) {}
  58. Bytes(unsigned char* p, int size): _data(p), _size(size) {}
  59. Bytes(const Str& str): Bytes(str.sv()) {}
  60. operator bool() const noexcept { return _data != nullptr; }
  61. Bytes(std::string_view sv);
  62. Bytes(const Bytes& rhs);
  63. Bytes(Bytes&& rhs) noexcept;
  64. Bytes& operator=(Bytes&& rhs) noexcept;
  65. Bytes& operator=(const Bytes& rhs) = delete;
  66. std::pair<unsigned char*, int> detach() noexcept;
  67. ~Bytes(){ delete[] _data;}
  68. };
  69. using Super = std::pair<PyObject*, Type>;
  70. struct Slice {
  71. PyObject* start;
  72. PyObject* stop;
  73. PyObject* step;
  74. Slice(PyObject* start, PyObject* stop, PyObject* step) : start(start), stop(stop), step(step) {}
  75. };
  76. struct PyObject{
  77. bool gc_enabled; // whether this object is managed by GC
  78. bool gc_marked; // whether this object is marked
  79. Type type;
  80. NameDict* _attr;
  81. bool is_attr_valid() const noexcept { return _attr != nullptr; }
  82. NameDict& attr() noexcept { return *_attr; }
  83. PyObject* attr(StrName name) const noexcept { return (*_attr)[name]; }
  84. // PyObject* operator[](StrName name) const noexcept { return (*_attr)[name]; }
  85. virtual void _obj_gc_mark() = 0;
  86. virtual void* _value_ptr() = 0;
  87. PyObject(Type type) : gc_enabled(true), gc_marked(false), type(type), _attr(nullptr) {}
  88. virtual ~PyObject();
  89. void _enable_instance_dict() {
  90. _attr = new(pool128_alloc<NameDict>()) NameDict();
  91. }
  92. void _enable_instance_dict(float lf){
  93. _attr = new(pool128_alloc<NameDict>()) NameDict(lf);
  94. }
  95. };
  96. struct PySignalObject: PyObject {
  97. PySignalObject() : PyObject(0) {
  98. gc_enabled = false;
  99. }
  100. void _obj_gc_mark() override {}
  101. void* _value_ptr() override { return nullptr; }
  102. };
  103. inline PyObject* const PY_NULL = new PySignalObject();
  104. inline PyObject* const PY_OP_CALL = new PySignalObject();
  105. inline PyObject* const PY_OP_YIELD = new PySignalObject();
  106. const int kTpIntIndex = 2;
  107. const int kTpFloatIndex = 3;
  108. inline bool is_tagged(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) != 0b00; }
  109. inline bool is_small_int(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b10; }
  110. inline bool is_heap_int(PyObject* p) noexcept { return !is_tagged(p) && p->type.index == kTpIntIndex; }
  111. inline bool is_float(PyObject* p) noexcept { return (PK_BITS(p) & 1) == 1; } // 01 or 11
  112. inline bool is_int(PyObject* p) noexcept { return is_small_int(p) || is_heap_int(p); }
  113. inline bool is_type(PyObject* obj, Type type) {
  114. #if PK_DEBUG_EXTRA_CHECK
  115. if(obj == nullptr) throw std::runtime_error("is_type() called with nullptr");
  116. #endif
  117. switch(type.index){
  118. case kTpIntIndex: return is_int(obj);
  119. case kTpFloatIndex: return is_float(obj);
  120. default: return !is_tagged(obj) && obj->type == type;
  121. }
  122. }
  123. inline bool is_non_tagged_type(PyObject* obj, Type type) {
  124. #if PK_DEBUG_EXTRA_CHECK
  125. if(obj == nullptr) throw std::runtime_error("is_non_tagged_type() called with nullptr");
  126. #endif
  127. return !is_tagged(obj) && obj->type == type;
  128. }
  129. template <typename, typename=void> struct has_gc_marker : std::false_type {};
  130. template <typename T> struct has_gc_marker<T, std::void_t<decltype(&T::_gc_mark)>> : std::true_type {};
  131. template <typename T>
  132. struct Py_ final: PyObject {
  133. static_assert(!std::is_reference_v<T>, "T must not be a reference type. Are you using `PK_OBJ_GET(T&, ...)`?");
  134. T _value;
  135. void _obj_gc_mark() override {
  136. if constexpr (has_gc_marker<T>::value) {
  137. _value._gc_mark();
  138. }
  139. }
  140. void* _value_ptr() override { return &_value; }
  141. template <typename... Args>
  142. Py_(Type type, Args&&... args) : PyObject(type), _value(std::forward<Args>(args)...) { }
  143. };
  144. struct MappingProxy{
  145. PyObject* obj;
  146. MappingProxy(PyObject* obj) : obj(obj) {}
  147. NameDict& attr() noexcept { return obj->attr(); }
  148. };
  149. #define PK_OBJ_GET(T, obj) (((Py_<T>*)(obj))->_value)
  150. #define PK_OBJ_MARK(obj) \
  151. if(!is_tagged(obj) && !(obj)->gc_marked) { \
  152. (obj)->gc_marked = true; \
  153. (obj)->_obj_gc_mark(); \
  154. if((obj)->is_attr_valid()) gc_mark_namedict((obj)->attr()); \
  155. }
  156. inline void gc_mark_namedict(NameDict& t){
  157. if(t.size() == 0) return;
  158. t.apply([](StrName name, PyObject* obj){
  159. PK_OBJ_MARK(obj);
  160. });
  161. }
  162. StrName _type_name(VM* vm, Type type);
  163. template<typename T> T to_void_p(VM*, PyObject*);
  164. PyObject* from_void_p(VM*, void*);
  165. #define VAR(x) py_var(vm, x)
  166. #define CAST(T, x) py_cast<T>(vm, x)
  167. #define _CAST(T, x) _py_cast<T>(vm, x)
  168. #define CAST_F(x) py_cast<f64>(vm, x)
  169. #define CAST_DEFAULT(T, x, default_value) (x != vm->None) ? py_cast<T>(vm, x) : (default_value)
  170. /*****************************************************************/
  171. template<>
  172. struct Py_<i64> final: PyObject {
  173. i64 _value;
  174. Py_(Type type, i64 val): PyObject(type), _value(val) {}
  175. void _obj_gc_mark() override {}
  176. void* _value_ptr() override { return &_value; }
  177. };
  178. inline bool try_cast_int(PyObject* obj, i64* val) noexcept {
  179. if(is_small_int(obj)){
  180. *val = PK_BITS(obj) >> 2;
  181. return true;
  182. }else if(is_heap_int(obj)){
  183. *val = PK_OBJ_GET(i64, obj);
  184. return true;
  185. }else{
  186. return false;
  187. }
  188. }
  189. template<>
  190. struct Py_<List> final: PyObject {
  191. List _value;
  192. Py_(Type type, List&& val): PyObject(type), _value(std::move(val)) {}
  193. Py_(Type type, const List& val): PyObject(type), _value(val) {}
  194. void _obj_gc_mark() override {
  195. for(PyObject* obj: _value) PK_OBJ_MARK(obj);
  196. }
  197. void* _value_ptr() override { return &_value; }
  198. };
  199. template<>
  200. struct Py_<Tuple> final: PyObject {
  201. Tuple _value;
  202. Py_(Type type, Tuple&& val): PyObject(type), _value(std::move(val)) {}
  203. Py_(Type type, const Tuple& val): PyObject(type), _value(val) {}
  204. void _obj_gc_mark() override {
  205. for(PyObject* obj: _value) PK_OBJ_MARK(obj);
  206. }
  207. void* _value_ptr() override { return &_value; }
  208. };
  209. template<>
  210. struct Py_<MappingProxy> final: PyObject {
  211. MappingProxy _value;
  212. Py_(Type type, MappingProxy val): PyObject(type), _value(val) {}
  213. void _obj_gc_mark() override {
  214. PK_OBJ_MARK(_value.obj);
  215. }
  216. void* _value_ptr() override { return &_value; }
  217. };
  218. template<>
  219. struct Py_<BoundMethod> final: PyObject {
  220. BoundMethod _value;
  221. Py_(Type type, BoundMethod val): PyObject(type), _value(val) {}
  222. void _obj_gc_mark() override {
  223. PK_OBJ_MARK(_value.self);
  224. PK_OBJ_MARK(_value.func);
  225. }
  226. void* _value_ptr() override { return &_value; }
  227. };
  228. template<>
  229. struct Py_<StarWrapper> final: PyObject {
  230. StarWrapper _value;
  231. Py_(Type type, StarWrapper val): PyObject(type), _value(val) {}
  232. void _obj_gc_mark() override {
  233. PK_OBJ_MARK(_value.obj);
  234. }
  235. void* _value_ptr() override { return &_value; }
  236. };
  237. template<>
  238. struct Py_<StaticMethod> final: PyObject {
  239. StaticMethod _value;
  240. Py_(Type type, StaticMethod val): PyObject(type), _value(val) {}
  241. void _obj_gc_mark() override {
  242. PK_OBJ_MARK(_value.func);
  243. }
  244. void* _value_ptr() override { return &_value; }
  245. };
  246. template<>
  247. struct Py_<ClassMethod> final: PyObject {
  248. ClassMethod _value;
  249. Py_(Type type, ClassMethod val): PyObject(type), _value(val) {}
  250. void _obj_gc_mark() override {
  251. PK_OBJ_MARK(_value.func);
  252. }
  253. void* _value_ptr() override { return &_value; }
  254. };
  255. template<>
  256. struct Py_<Property> final: PyObject {
  257. Property _value;
  258. Py_(Type type, Property val): PyObject(type), _value(val) {}
  259. void _obj_gc_mark() override {
  260. PK_OBJ_MARK(_value.getter);
  261. PK_OBJ_MARK(_value.setter);
  262. }
  263. void* _value_ptr() override { return &_value; }
  264. };
  265. template<>
  266. struct Py_<Slice> final: PyObject {
  267. Slice _value;
  268. Py_(Type type, Slice val): PyObject(type), _value(val) {}
  269. void _obj_gc_mark() override {
  270. PK_OBJ_MARK(_value.start);
  271. PK_OBJ_MARK(_value.stop);
  272. PK_OBJ_MARK(_value.step);
  273. }
  274. void* _value_ptr() override { return &_value; }
  275. };
  276. template<>
  277. struct Py_<Super> final: PyObject {
  278. Super _value;
  279. template<typename... Args>
  280. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {}
  281. void _obj_gc_mark() override {
  282. PK_OBJ_MARK(_value.first);
  283. }
  284. void* _value_ptr() override { return &_value; }
  285. };
  286. template<>
  287. struct Py_<DummyInstance> final: PyObject {
  288. Py_(Type type): PyObject(type) {
  289. _enable_instance_dict();
  290. }
  291. void _obj_gc_mark() override {}
  292. void* _value_ptr() override { return nullptr; }
  293. };
  294. template<>
  295. struct Py_<Type> final: PyObject {
  296. Type _value;
  297. Py_(Type type, Type val): PyObject(type), _value(val) {
  298. _enable_instance_dict(PK_TYPE_ATTR_LOAD_FACTOR);
  299. }
  300. void _obj_gc_mark() override {}
  301. void* _value_ptr() override { return &_value; }
  302. };
  303. template<>
  304. struct Py_<DummyModule> final: PyObject {
  305. Py_(Type type): PyObject(type) {
  306. _enable_instance_dict(PK_TYPE_ATTR_LOAD_FACTOR);
  307. }
  308. void _obj_gc_mark() override {}
  309. void* _value_ptr() override { return nullptr; }
  310. };
  311. } // namespace pkpy