obj.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. struct BoundMethod {
  14. PyObject* self;
  15. PyObject* func;
  16. BoundMethod(PyObject* self, PyObject* func) : self(self), func(func) {}
  17. bool operator==(const BoundMethod& rhs) const noexcept {
  18. return self == rhs.self && func == rhs.func;
  19. }
  20. bool operator!=(const BoundMethod& rhs) const noexcept {
  21. return self != rhs.self || func != rhs.func;
  22. }
  23. };
  24. struct Property{
  25. PyObject* getter;
  26. PyObject* setter;
  27. Str signature;
  28. Property(PyObject* getter, PyObject* setter, Str signature) : getter(getter), setter(setter), signature(signature) {}
  29. };
  30. struct Range {
  31. i64 start = 0;
  32. i64 stop = -1;
  33. i64 step = 1;
  34. };
  35. struct StarWrapper{
  36. int level; // either 1 or 2
  37. PyObject* obj;
  38. StarWrapper(int level, PyObject* obj) : level(level), obj(obj) {}
  39. };
  40. struct Bytes{
  41. std::vector<char> _v;
  42. bool valid;
  43. int size() const noexcept { return (int)_v.size(); }
  44. int operator[](int i) const noexcept { return (int)(uint8_t)_v[i]; }
  45. const char* data() const noexcept { return _v.data(); }
  46. bool operator==(const Bytes& rhs) const{ return _v == rhs._v && valid == rhs.valid; }
  47. bool operator!=(const Bytes& rhs) const{ return _v != rhs._v || valid != rhs.valid; }
  48. Str str() const noexcept { return Str(_v.data(), _v.size()); }
  49. std::string_view sv() const noexcept { return std::string_view(_v.data(), _v.size()); }
  50. Bytes() : valid(false) {}
  51. Bytes(std::vector<char>&& v): _v(std::move(v)), valid(true) {}
  52. Bytes(std::string_view sv): valid(true) {
  53. _v.resize(sv.size());
  54. for(int i=0; i<sv.size(); i++) _v[i] = sv[i];
  55. }
  56. Bytes(const Str& str): Bytes(str.sv()) {}
  57. operator bool() const noexcept { return valid; }
  58. // copy constructor
  59. Bytes(const Bytes& rhs) : _v(rhs._v), valid(rhs.valid) {}
  60. // move constructor
  61. Bytes(Bytes&& rhs) noexcept : _v(std::move(rhs._v)), valid(rhs.valid) {
  62. rhs.valid = false;
  63. }
  64. Bytes& operator=(Bytes&& rhs) noexcept {
  65. _v = std::move(rhs._v);
  66. valid = rhs.valid;
  67. rhs.valid = false;
  68. return *this;
  69. }
  70. // delete copy assignment
  71. Bytes& operator=(const Bytes& rhs) = delete;
  72. };
  73. using Super = std::pair<PyObject*, Type>;
  74. struct Slice {
  75. PyObject* start;
  76. PyObject* stop;
  77. PyObject* step;
  78. Slice(PyObject* start, PyObject* stop, PyObject* step) : start(start), stop(stop), step(step) {}
  79. };
  80. struct GCHeader {
  81. bool enabled; // whether this object is managed by GC
  82. bool marked; // whether this object is marked
  83. GCHeader() : enabled(true), marked(false) {}
  84. };
  85. struct PyObject{
  86. GCHeader gc;
  87. Type type;
  88. NameDict* _attr;
  89. bool is_attr_valid() const noexcept { return _attr != nullptr; }
  90. NameDict& attr() noexcept { return *_attr; }
  91. PyObject* attr(StrName name) const noexcept { return (*_attr)[name]; }
  92. virtual void _obj_gc_mark() = 0;
  93. PyObject(Type type) : type(type), _attr(nullptr) {}
  94. virtual ~PyObject();
  95. void enable_instance_dict(float lf=kInstAttrLoadFactor) {
  96. _attr = new(pool64_alloc<NameDict>()) NameDict(lf);
  97. }
  98. };
  99. struct PySignalObject: PyObject {
  100. PySignalObject() : PyObject(0) {
  101. gc.enabled = false;
  102. }
  103. void _obj_gc_mark() override {}
  104. };
  105. inline PyObject* const PY_NULL = new PySignalObject();
  106. inline PyObject* const PY_OP_CALL = new PySignalObject();
  107. inline PyObject* const PY_OP_YIELD = new PySignalObject();
  108. const int kTpIntIndex = 2;
  109. const int kTpFloatIndex = 3;
  110. inline bool is_tagged(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) != 0b00; }
  111. inline bool is_small_int(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b10; }
  112. inline bool is_heap_int(PyObject* p) noexcept { return !is_tagged(p) && p->type.index == kTpIntIndex; }
  113. inline bool is_float(PyObject* p) noexcept { return (PK_BITS(p) & 1) == 1; } // 01 or 11
  114. inline bool is_int(PyObject* p) noexcept { return is_small_int(p) || is_heap_int(p); }
  115. inline bool is_type(PyObject* obj, Type type) {
  116. #if PK_DEBUG_EXTRA_CHECK
  117. if(obj == nullptr) throw std::runtime_error("is_type() called with nullptr");
  118. #endif
  119. switch(type.index){
  120. case kTpIntIndex: return is_int(obj);
  121. case kTpFloatIndex: return is_float(obj);
  122. default: return !is_tagged(obj) && obj->type == type;
  123. }
  124. }
  125. inline bool is_non_tagged_type(PyObject* obj, Type type) {
  126. #if PK_DEBUG_EXTRA_CHECK
  127. if(obj == nullptr) throw std::runtime_error("is_non_tagged_type() called with nullptr");
  128. #endif
  129. return !is_tagged(obj) && obj->type == type;
  130. }
  131. template <typename, typename=void> struct has_gc_marker : std::false_type {};
  132. template <typename T> struct has_gc_marker<T, std::void_t<decltype(&T::_gc_mark)>> : std::true_type {};
  133. template <typename T>
  134. struct Py_ final: PyObject {
  135. T _value;
  136. void _obj_gc_mark() override {
  137. if constexpr (has_gc_marker<T>::value) {
  138. _value._gc_mark();
  139. }
  140. }
  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. for(uint16_t i=0; i<t._capacity; i++){
  159. if(t._items[i].first.empty()) continue;
  160. PK_OBJ_MARK(t._items[i].second);
  161. }
  162. }
  163. Str obj_type_name(VM* vm, Type type);
  164. #if PK_DEBUG_NO_BUILTINS
  165. #define OBJ_NAME(obj) Str("<?>")
  166. #else
  167. #define OBJ_NAME(obj) PK_OBJ_GET(Str, vm->getattr(obj, __name__))
  168. #endif
  169. template <typename, typename=void> struct is_py_class : std::false_type {};
  170. template <typename T> struct is_py_class<T, std::void_t<decltype(T::_type)>> : std::true_type {};
  171. template<typename T> T to_void_p(VM*, PyObject*);
  172. template<typename __T>
  173. __T py_cast(VM* vm, PyObject* obj) {
  174. using T = std::decay_t<__T>;
  175. if constexpr(std::is_enum_v<T>){
  176. return (__T)py_cast<i64>(vm, obj);
  177. }else if constexpr(std::is_pointer_v<T>){
  178. return to_void_p<T>(vm, obj);
  179. }else if constexpr(is_py_class<T>::value){
  180. T::_check_type(vm, obj);
  181. return PK_OBJ_GET(T, obj);
  182. }else {
  183. return Discarded();
  184. }
  185. }
  186. template<typename __T>
  187. __T _py_cast(VM* vm, PyObject* obj) {
  188. using T = std::decay_t<__T>;
  189. if constexpr(std::is_enum_v<T>){
  190. return (__T)_py_cast<i64>(vm, obj);
  191. }else if constexpr(std::is_pointer_v<__T>){
  192. return to_void_p<__T>(vm, obj);
  193. }else if constexpr(is_py_class<T>::value){
  194. return PK_OBJ_GET(T, obj);
  195. }else {
  196. return Discarded();
  197. }
  198. }
  199. #define VAR(x) py_var(vm, x)
  200. #define CAST(T, x) py_cast<T>(vm, x)
  201. #define _CAST(T, x) _py_cast<T>(vm, x)
  202. #define CAST_F(x) py_cast<f64>(vm, x)
  203. #define CAST_DEFAULT(T, x, default_value) (x != vm->None) ? py_cast<T>(vm, x) : (default_value)
  204. /*****************************************************************/
  205. template<>
  206. struct Py_<i64> final: PyObject {
  207. i64 _value;
  208. Py_(Type type, i64 val): PyObject(type), _value(val) {}
  209. void _obj_gc_mark() override {}
  210. };
  211. inline bool try_cast_int(PyObject* obj, i64* val) noexcept {
  212. if(is_small_int(obj)){
  213. *val = PK_BITS(obj) >> 2;
  214. return true;
  215. }else if(is_heap_int(obj)){
  216. *val = PK_OBJ_GET(i64, obj);
  217. return true;
  218. }else{
  219. return false;
  220. }
  221. }
  222. template<>
  223. struct Py_<List> final: PyObject {
  224. List _value;
  225. Py_(Type type, List&& val): PyObject(type), _value(std::move(val)) {}
  226. Py_(Type type, const List& val): PyObject(type), _value(val) {}
  227. void _obj_gc_mark() override {
  228. for(PyObject* obj: _value) PK_OBJ_MARK(obj);
  229. }
  230. };
  231. template<>
  232. struct Py_<Tuple> final: PyObject {
  233. Tuple _value;
  234. Py_(Type type, Tuple&& val): PyObject(type), _value(std::move(val)) {}
  235. Py_(Type type, const Tuple& val): PyObject(type), _value(val) {}
  236. void _obj_gc_mark() override {
  237. for(PyObject* obj: _value) PK_OBJ_MARK(obj);
  238. }
  239. };
  240. template<>
  241. struct Py_<MappingProxy> final: PyObject {
  242. MappingProxy _value;
  243. Py_(Type type, MappingProxy val): PyObject(type), _value(val) {}
  244. void _obj_gc_mark() override {
  245. PK_OBJ_MARK(_value.obj);
  246. }
  247. };
  248. template<>
  249. struct Py_<BoundMethod> final: PyObject {
  250. BoundMethod _value;
  251. Py_(Type type, BoundMethod val): PyObject(type), _value(val) {}
  252. void _obj_gc_mark() override {
  253. PK_OBJ_MARK(_value.self);
  254. PK_OBJ_MARK(_value.func);
  255. }
  256. };
  257. template<>
  258. struct Py_<StarWrapper> final: PyObject {
  259. StarWrapper _value;
  260. Py_(Type type, StarWrapper val): PyObject(type), _value(val) {}
  261. void _obj_gc_mark() override {
  262. PK_OBJ_MARK(_value.obj);
  263. }
  264. };
  265. template<>
  266. struct Py_<Property> final: PyObject {
  267. Property _value;
  268. Py_(Type type, Property val): PyObject(type), _value(val) {}
  269. void _obj_gc_mark() override {
  270. PK_OBJ_MARK(_value.getter);
  271. PK_OBJ_MARK(_value.setter);
  272. }
  273. };
  274. template<>
  275. struct Py_<Slice> final: PyObject {
  276. Slice _value;
  277. Py_(Type type, Slice val): PyObject(type), _value(val) {}
  278. void _obj_gc_mark() override {
  279. PK_OBJ_MARK(_value.start);
  280. PK_OBJ_MARK(_value.stop);
  281. PK_OBJ_MARK(_value.step);
  282. }
  283. };
  284. template<>
  285. struct Py_<Super> final: PyObject {
  286. Super _value;
  287. template<typename... Args>
  288. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {}
  289. void _obj_gc_mark() override {
  290. PK_OBJ_MARK(_value.first);
  291. }
  292. };
  293. template<>
  294. struct Py_<DummyInstance> final: PyObject {
  295. Py_(Type type): PyObject(type) {
  296. enable_instance_dict();
  297. }
  298. void _obj_gc_mark() override {}
  299. };
  300. template<>
  301. struct Py_<Type> final: PyObject {
  302. Type _value;
  303. Py_(Type type, Type val): PyObject(type), _value(val) {
  304. enable_instance_dict(kTypeAttrLoadFactor);
  305. }
  306. void _obj_gc_mark() override {}
  307. };
  308. template<>
  309. struct Py_<DummyModule> final: PyObject {
  310. Py_(Type type): PyObject(type) {
  311. enable_instance_dict(kTypeAttrLoadFactor);
  312. }
  313. void _obj_gc_mark() override {}
  314. };
  315. } // namespace pkpy