obj.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. if(_attr == nullptr) return;
  90. _attr->~NameDict();
  91. pool128_dealloc(_attr);
  92. }
  93. void _enable_instance_dict() {
  94. _attr = new(pool128_alloc<NameDict>()) NameDict();
  95. }
  96. void _enable_instance_dict(float lf){
  97. _attr = new(pool128_alloc<NameDict>()) NameDict(lf);
  98. }
  99. };
  100. struct PySignalObject: PyObject {
  101. PySignalObject() : PyObject(0) {
  102. gc_enabled = false;
  103. }
  104. void _obj_gc_mark() override {}
  105. void* _value_ptr() override { return nullptr; }
  106. };
  107. inline PyObject* const PY_NULL = new PySignalObject();
  108. inline PyObject* const PY_OP_CALL = new PySignalObject();
  109. inline PyObject* const PY_OP_YIELD = new PySignalObject();
  110. const int kTpIntIndex = 2;
  111. const int kTpFloatIndex = 3;
  112. inline bool is_tagged(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) != 0b00; }
  113. inline bool is_small_int(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b10; }
  114. inline bool is_heap_int(PyObject* p) noexcept { return !is_tagged(p) && p->type.index == kTpIntIndex; }
  115. inline bool is_float(PyObject* p) noexcept { return (PK_BITS(p) & 1) == 1; } // 01 or 11
  116. inline bool is_int(PyObject* p) noexcept { return is_small_int(p) || is_heap_int(p); }
  117. inline bool is_type(PyObject* obj, Type type) {
  118. #if PK_DEBUG_EXTRA_CHECK
  119. if(obj == nullptr) throw std::runtime_error("is_type() called with nullptr");
  120. #endif
  121. switch(type.index){
  122. case kTpIntIndex: return is_int(obj);
  123. case kTpFloatIndex: return is_float(obj);
  124. default: return !is_tagged(obj) && obj->type == type;
  125. }
  126. }
  127. inline bool is_non_tagged_type(PyObject* obj, Type type) {
  128. #if PK_DEBUG_EXTRA_CHECK
  129. if(obj == nullptr) throw std::runtime_error("is_non_tagged_type() called with nullptr");
  130. #endif
  131. return !is_tagged(obj) && obj->type == type;
  132. }
  133. template <typename, typename=void> struct has_gc_marker : std::false_type {};
  134. template <typename T> struct has_gc_marker<T, std::void_t<decltype(&T::_gc_mark)>> : std::true_type {};
  135. template <typename T>
  136. struct Py_ final: PyObject {
  137. static_assert(!std::is_reference_v<T>, "T must not be a reference type. Are you using `PK_OBJ_GET(T&, ...)`?");
  138. T _value;
  139. void _obj_gc_mark() override {
  140. if constexpr (has_gc_marker<T>::value) {
  141. _value._gc_mark();
  142. }
  143. }
  144. void* _value_ptr() override { return &_value; }
  145. template <typename... Args>
  146. Py_(Type type, Args&&... args) : PyObject(type), _value(std::forward<Args>(args)...) { }
  147. };
  148. struct MappingProxy{
  149. PyObject* obj;
  150. MappingProxy(PyObject* obj) : obj(obj) {}
  151. NameDict& attr() noexcept { return obj->attr(); }
  152. };
  153. #define PK_OBJ_GET(T, obj) (((Py_<T>*)(obj))->_value)
  154. #define PK_OBJ_MARK(obj) \
  155. if(!is_tagged(obj) && !(obj)->gc_marked) { \
  156. (obj)->gc_marked = true; \
  157. (obj)->_obj_gc_mark(); \
  158. if((obj)->is_attr_valid()) gc_mark_namedict((obj)->attr()); \
  159. }
  160. inline void gc_mark_namedict(NameDict& t){
  161. if(t.size() == 0) return;
  162. t.apply([](StrName name, PyObject* obj){
  163. PK_OBJ_MARK(obj);
  164. });
  165. }
  166. StrName _type_name(VM* vm, Type type);
  167. template<typename T> T to_void_p(VM*, PyObject*);
  168. PyObject* from_void_p(VM*, void*);
  169. #define VAR(x) py_var(vm, x)
  170. #define CAST(T, x) py_cast<T>(vm, x)
  171. #define _CAST(T, x) _py_cast<T>(vm, x)
  172. #define CAST_F(x) py_cast<f64>(vm, x)
  173. #define CAST_DEFAULT(T, x, default_value) (x != vm->None) ? py_cast<T>(vm, x) : (default_value)
  174. /*****************************************************************/
  175. template<>
  176. struct Py_<i64> final: PyObject {
  177. i64 _value;
  178. Py_(Type type, i64 val): PyObject(type), _value(val) {}
  179. void _obj_gc_mark() override {}
  180. void* _value_ptr() override { return &_value; }
  181. };
  182. inline bool try_cast_int(PyObject* obj, i64* val) noexcept {
  183. if(is_small_int(obj)){
  184. *val = PK_BITS(obj) >> 2;
  185. return true;
  186. }else if(is_heap_int(obj)){
  187. *val = PK_OBJ_GET(i64, obj);
  188. return true;
  189. }else{
  190. return false;
  191. }
  192. }
  193. template<>
  194. struct Py_<List> final: PyObject {
  195. List _value;
  196. Py_(Type type, List&& val): PyObject(type), _value(std::move(val)) {}
  197. Py_(Type type, const List& val): PyObject(type), _value(val) {}
  198. void _obj_gc_mark() override {
  199. for(PyObject* obj: _value) PK_OBJ_MARK(obj);
  200. }
  201. void* _value_ptr() override { return &_value; }
  202. };
  203. template<>
  204. struct Py_<Tuple> final: PyObject {
  205. Tuple _value;
  206. Py_(Type type, Tuple&& val): PyObject(type), _value(std::move(val)) {}
  207. Py_(Type type, const Tuple& val): PyObject(type), _value(val) {}
  208. void _obj_gc_mark() override {
  209. for(PyObject* obj: _value) PK_OBJ_MARK(obj);
  210. }
  211. void* _value_ptr() override { return &_value; }
  212. };
  213. template<>
  214. struct Py_<MappingProxy> final: PyObject {
  215. MappingProxy _value;
  216. Py_(Type type, MappingProxy val): PyObject(type), _value(val) {}
  217. void _obj_gc_mark() override {
  218. PK_OBJ_MARK(_value.obj);
  219. }
  220. void* _value_ptr() override { return &_value; }
  221. };
  222. template<>
  223. struct Py_<BoundMethod> final: PyObject {
  224. BoundMethod _value;
  225. Py_(Type type, BoundMethod val): PyObject(type), _value(val) {}
  226. void _obj_gc_mark() override {
  227. PK_OBJ_MARK(_value.self);
  228. PK_OBJ_MARK(_value.func);
  229. }
  230. void* _value_ptr() override { return &_value; }
  231. };
  232. template<>
  233. struct Py_<StarWrapper> final: PyObject {
  234. StarWrapper _value;
  235. Py_(Type type, StarWrapper val): PyObject(type), _value(val) {}
  236. void _obj_gc_mark() override {
  237. PK_OBJ_MARK(_value.obj);
  238. }
  239. void* _value_ptr() override { return &_value; }
  240. };
  241. template<>
  242. struct Py_<StaticMethod> final: PyObject {
  243. StaticMethod _value;
  244. Py_(Type type, StaticMethod val): PyObject(type), _value(val) {}
  245. void _obj_gc_mark() override {
  246. PK_OBJ_MARK(_value.func);
  247. }
  248. void* _value_ptr() override { return &_value; }
  249. };
  250. template<>
  251. struct Py_<ClassMethod> final: PyObject {
  252. ClassMethod _value;
  253. Py_(Type type, ClassMethod val): PyObject(type), _value(val) {}
  254. void _obj_gc_mark() override {
  255. PK_OBJ_MARK(_value.func);
  256. }
  257. void* _value_ptr() override { return &_value; }
  258. };
  259. template<>
  260. struct Py_<Property> final: PyObject {
  261. Property _value;
  262. Py_(Type type, Property val): PyObject(type), _value(val) {}
  263. void _obj_gc_mark() override {
  264. PK_OBJ_MARK(_value.getter);
  265. PK_OBJ_MARK(_value.setter);
  266. }
  267. void* _value_ptr() override { return &_value; }
  268. };
  269. template<>
  270. struct Py_<Slice> final: PyObject {
  271. Slice _value;
  272. Py_(Type type, Slice val): PyObject(type), _value(val) {}
  273. void _obj_gc_mark() override {
  274. PK_OBJ_MARK(_value.start);
  275. PK_OBJ_MARK(_value.stop);
  276. PK_OBJ_MARK(_value.step);
  277. }
  278. void* _value_ptr() override { return &_value; }
  279. };
  280. template<>
  281. struct Py_<Super> final: PyObject {
  282. Super _value;
  283. template<typename... Args>
  284. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {}
  285. void _obj_gc_mark() override {
  286. PK_OBJ_MARK(_value.first);
  287. }
  288. void* _value_ptr() override { return &_value; }
  289. };
  290. template<>
  291. struct Py_<DummyInstance> final: PyObject {
  292. Py_(Type type): PyObject(type) {
  293. _enable_instance_dict();
  294. }
  295. void _obj_gc_mark() override {}
  296. void* _value_ptr() override { return nullptr; }
  297. };
  298. template<>
  299. struct Py_<Type> final: PyObject {
  300. Type _value;
  301. Py_(Type type, Type val): PyObject(type), _value(val) {
  302. _enable_instance_dict(PK_TYPE_ATTR_LOAD_FACTOR);
  303. }
  304. void _obj_gc_mark() override {}
  305. void* _value_ptr() override { return &_value; }
  306. };
  307. template<>
  308. struct Py_<DummyModule> final: PyObject {
  309. Py_(Type type): PyObject(type) {
  310. _enable_instance_dict(PK_TYPE_ATTR_LOAD_FACTOR);
  311. }
  312. void _obj_gc_mark() override {}
  313. void* _value_ptr() override { return nullptr; }
  314. };
  315. } // namespace pkpy