obj.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. virtual void* _value_ptr() = 0;
  94. PyObject(Type type) : type(type), _attr(nullptr) {}
  95. virtual ~PyObject();
  96. void enable_instance_dict(float lf=kInstAttrLoadFactor) {
  97. _attr = new(pool64_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. T _value;
  138. void _obj_gc_mark() override {
  139. if constexpr (has_gc_marker<T>::value) {
  140. _value._gc_mark();
  141. }
  142. }
  143. void* _value_ptr() override { return &_value; }
  144. template <typename... Args>
  145. Py_(Type type, Args&&... args) : PyObject(type), _value(std::forward<Args>(args)...) { }
  146. };
  147. struct MappingProxy{
  148. PyObject* obj;
  149. MappingProxy(PyObject* obj) : obj(obj) {}
  150. NameDict& attr() noexcept { return obj->attr(); }
  151. };
  152. #define PK_OBJ_GET(T, obj) (((Py_<T>*)(obj))->_value)
  153. #define PK_OBJ_MARK(obj) \
  154. if(!is_tagged(obj) && !(obj)->gc.marked) { \
  155. (obj)->gc.marked = true; \
  156. (obj)->_obj_gc_mark(); \
  157. if((obj)->is_attr_valid()) gc_mark_namedict((obj)->attr()); \
  158. }
  159. inline void gc_mark_namedict(NameDict& t){
  160. if(t.size() == 0) return;
  161. for(uint16_t i=0; i<t._capacity; i++){
  162. if(t._items[i].first.empty()) continue;
  163. PK_OBJ_MARK(t._items[i].second);
  164. }
  165. }
  166. Str obj_type_name(VM* vm, Type type);
  167. #if PK_DEBUG_NO_BUILTINS
  168. #define OBJ_NAME(obj) Str("<?>")
  169. #else
  170. #define OBJ_NAME(obj) PK_OBJ_GET(Str, vm->getattr(obj, __name__))
  171. #endif
  172. template <typename, typename=void> struct is_py_class : std::false_type {};
  173. template <typename T> struct is_py_class<T, std::void_t<decltype(T::_type)>> : std::true_type {};
  174. template<typename T> T to_void_p(VM*, PyObject*);
  175. template<typename __T>
  176. __T py_cast(VM* vm, PyObject* obj) {
  177. using T = std::decay_t<__T>;
  178. if constexpr(std::is_enum_v<T>){
  179. return (__T)py_cast<i64>(vm, obj);
  180. }else if constexpr(std::is_pointer_v<T>){
  181. return to_void_p<T>(vm, obj);
  182. }else if constexpr(is_py_class<T>::value){
  183. T::_check_type(vm, obj);
  184. return PK_OBJ_GET(T, obj);
  185. }else {
  186. return Discarded();
  187. }
  188. }
  189. template<typename __T>
  190. __T _py_cast(VM* vm, PyObject* obj) {
  191. using T = std::decay_t<__T>;
  192. if constexpr(std::is_enum_v<T>){
  193. return (__T)_py_cast<i64>(vm, obj);
  194. }else if constexpr(std::is_pointer_v<__T>){
  195. return to_void_p<__T>(vm, obj);
  196. }else if constexpr(is_py_class<T>::value){
  197. return PK_OBJ_GET(T, obj);
  198. }else {
  199. return Discarded();
  200. }
  201. }
  202. #define VAR(x) py_var(vm, x)
  203. #define CAST(T, x) py_cast<T>(vm, x)
  204. #define _CAST(T, x) _py_cast<T>(vm, x)
  205. #define CAST_F(x) py_cast<f64>(vm, x)
  206. #define CAST_DEFAULT(T, x, default_value) (x != vm->None) ? py_cast<T>(vm, x) : (default_value)
  207. /*****************************************************************/
  208. template<>
  209. struct Py_<i64> final: PyObject {
  210. i64 _value;
  211. Py_(Type type, i64 val): PyObject(type), _value(val) {}
  212. void _obj_gc_mark() override {}
  213. void* _value_ptr() override { return &_value; }
  214. };
  215. inline bool try_cast_int(PyObject* obj, i64* val) noexcept {
  216. if(is_small_int(obj)){
  217. *val = PK_BITS(obj) >> 2;
  218. return true;
  219. }else if(is_heap_int(obj)){
  220. *val = PK_OBJ_GET(i64, obj);
  221. return true;
  222. }else{
  223. return false;
  224. }
  225. }
  226. template<>
  227. struct Py_<List> final: PyObject {
  228. List _value;
  229. Py_(Type type, List&& val): PyObject(type), _value(std::move(val)) {}
  230. Py_(Type type, const List& val): PyObject(type), _value(val) {}
  231. void _obj_gc_mark() override {
  232. for(PyObject* obj: _value) PK_OBJ_MARK(obj);
  233. }
  234. void* _value_ptr() override { return &_value; }
  235. };
  236. template<>
  237. struct Py_<Tuple> final: PyObject {
  238. Tuple _value;
  239. Py_(Type type, Tuple&& val): PyObject(type), _value(std::move(val)) {}
  240. Py_(Type type, const Tuple& val): PyObject(type), _value(val) {}
  241. void _obj_gc_mark() override {
  242. for(PyObject* obj: _value) PK_OBJ_MARK(obj);
  243. }
  244. void* _value_ptr() override { return &_value; }
  245. };
  246. template<>
  247. struct Py_<MappingProxy> final: PyObject {
  248. MappingProxy _value;
  249. Py_(Type type, MappingProxy val): PyObject(type), _value(val) {}
  250. void _obj_gc_mark() override {
  251. PK_OBJ_MARK(_value.obj);
  252. }
  253. void* _value_ptr() override { return &_value; }
  254. };
  255. template<>
  256. struct Py_<BoundMethod> final: PyObject {
  257. BoundMethod _value;
  258. Py_(Type type, BoundMethod val): PyObject(type), _value(val) {}
  259. void _obj_gc_mark() override {
  260. PK_OBJ_MARK(_value.self);
  261. PK_OBJ_MARK(_value.func);
  262. }
  263. void* _value_ptr() override { return &_value; }
  264. };
  265. template<>
  266. struct Py_<StarWrapper> final: PyObject {
  267. StarWrapper _value;
  268. Py_(Type type, StarWrapper val): PyObject(type), _value(val) {}
  269. void _obj_gc_mark() override {
  270. PK_OBJ_MARK(_value.obj);
  271. }
  272. void* _value_ptr() override { return &_value; }
  273. };
  274. template<>
  275. struct Py_<Property> final: PyObject {
  276. Property _value;
  277. Py_(Type type, Property val): PyObject(type), _value(val) {}
  278. void _obj_gc_mark() override {
  279. PK_OBJ_MARK(_value.getter);
  280. PK_OBJ_MARK(_value.setter);
  281. }
  282. void* _value_ptr() override { return &_value; }
  283. };
  284. template<>
  285. struct Py_<Slice> final: PyObject {
  286. Slice _value;
  287. Py_(Type type, Slice val): PyObject(type), _value(val) {}
  288. void _obj_gc_mark() override {
  289. PK_OBJ_MARK(_value.start);
  290. PK_OBJ_MARK(_value.stop);
  291. PK_OBJ_MARK(_value.step);
  292. }
  293. void* _value_ptr() override { return &_value; }
  294. };
  295. template<>
  296. struct Py_<Super> final: PyObject {
  297. Super _value;
  298. template<typename... Args>
  299. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {}
  300. void _obj_gc_mark() override {
  301. PK_OBJ_MARK(_value.first);
  302. }
  303. void* _value_ptr() override { return &_value; }
  304. };
  305. template<>
  306. struct Py_<DummyInstance> final: PyObject {
  307. Py_(Type type): PyObject(type) {
  308. enable_instance_dict();
  309. }
  310. void _obj_gc_mark() override {}
  311. void* _value_ptr() override { return nullptr; }
  312. };
  313. template<>
  314. struct Py_<Type> final: PyObject {
  315. Type _value;
  316. Py_(Type type, Type val): PyObject(type), _value(val) {
  317. enable_instance_dict(kTypeAttrLoadFactor);
  318. }
  319. void _obj_gc_mark() override {}
  320. void* _value_ptr() override { return &_value; }
  321. };
  322. template<>
  323. struct Py_<DummyModule> final: PyObject {
  324. Py_(Type type): PyObject(type) {
  325. enable_instance_dict(kTypeAttrLoadFactor);
  326. }
  327. void _obj_gc_mark() override {}
  328. void* _value_ptr() override { return nullptr; }
  329. };
  330. } // namespace pkpy