obj.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. const int kTpIntIndex = 2;
  100. const int kTpFloatIndex = 3;
  101. inline bool is_tagged(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) != 0b00; }
  102. inline bool is_small_int(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b01; }
  103. inline bool is_heap_int(PyObject* p) noexcept { return !is_tagged(p) && p->type.index == kTpIntIndex; }
  104. inline bool is_float(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b10; }
  105. inline bool is_special(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b11; }
  106. inline bool is_int(PyObject* p) noexcept { return is_small_int(p) || is_heap_int(p); }
  107. inline bool is_type(PyObject* obj, Type type) {
  108. #if PK_DEBUG_EXTRA_CHECK
  109. if(obj == nullptr) throw std::runtime_error("is_type() called with nullptr");
  110. if(is_special(obj)) throw std::runtime_error("is_type() called with special object");
  111. #endif
  112. switch(type.index){
  113. case kTpIntIndex: return is_int(obj);
  114. case kTpFloatIndex: return is_float(obj);
  115. default: return !is_tagged(obj) && obj->type == type;
  116. }
  117. }
  118. inline bool is_non_tagged_type(PyObject* obj, Type type) {
  119. #if PK_DEBUG_EXTRA_CHECK
  120. if(obj == nullptr) throw std::runtime_error("is_non_tagged_type() called with nullptr");
  121. if(is_special(obj)) throw std::runtime_error("is_non_tagged_type() called with special object");
  122. #endif
  123. return !is_tagged(obj) && obj->type == type;
  124. }
  125. template <typename, typename=void> struct has_gc_marker : std::false_type {};
  126. template <typename T> struct has_gc_marker<T, std::void_t<decltype(&T::_gc_mark)>> : std::true_type {};
  127. template <typename T>
  128. struct Py_ final: PyObject {
  129. T _value;
  130. void _obj_gc_mark() override {
  131. if constexpr (has_gc_marker<T>::value) {
  132. _value._gc_mark();
  133. }
  134. }
  135. template <typename... Args>
  136. Py_(Type type, Args&&... args) : PyObject(type), _value(std::forward<Args>(args)...) { }
  137. };
  138. struct MappingProxy{
  139. PyObject* obj;
  140. MappingProxy(PyObject* obj) : obj(obj) {}
  141. NameDict& attr() noexcept { return obj->attr(); }
  142. };
  143. #define PK_OBJ_GET(T, obj) (((Py_<T>*)(obj))->_value)
  144. #define PK_OBJ_MARK(obj) \
  145. if(!is_tagged(obj) && !(obj)->gc.marked) { \
  146. (obj)->gc.marked = true; \
  147. (obj)->_obj_gc_mark(); \
  148. if((obj)->is_attr_valid()) gc_mark_namedict((obj)->attr()); \
  149. }
  150. inline void gc_mark_namedict(NameDict& t){
  151. if(t.size() == 0) return;
  152. for(uint16_t i=0; i<t._capacity; i++){
  153. if(t._items[i].first.empty()) continue;
  154. PK_OBJ_MARK(t._items[i].second);
  155. }
  156. }
  157. Str obj_type_name(VM* vm, Type type);
  158. #if PK_DEBUG_NO_BUILTINS
  159. #define OBJ_NAME(obj) Str("<?>")
  160. #else
  161. #define OBJ_NAME(obj) PK_OBJ_GET(Str, vm->getattr(obj, __name__))
  162. #endif
  163. union BitsCvt {
  164. i64 _int;
  165. f64 _float;
  166. BitsCvt(i64 val) : _int(val) {}
  167. BitsCvt(f64 val) : _float(val) {}
  168. };
  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> T to_c99_struct(VM*, PyObject*);
  173. template<typename __T>
  174. __T py_cast(VM* vm, PyObject* obj) {
  175. using T = std::decay_t<__T>;
  176. if constexpr(std::is_enum_v<T>){
  177. return (__T)py_cast<i64>(vm, obj);
  178. }else if constexpr(std::is_pointer_v<T>){
  179. return to_void_p<T>(vm, obj);
  180. }else if constexpr(is_py_class<T>::value){
  181. T::_check_type(vm, obj);
  182. return PK_OBJ_GET(T, obj);
  183. }else if constexpr(is_pod<T>::value){
  184. return to_c99_struct<T>(vm, 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 if constexpr(is_pod<T>::value){
  199. return to_c99_struct<T>(vm, obj);
  200. }else {
  201. return Discarded();
  202. }
  203. }
  204. #define VAR(x) py_var(vm, x)
  205. #define CAST(T, x) py_cast<T>(vm, x)
  206. #define _CAST(T, x) _py_cast<T>(vm, x)
  207. #define CAST_F(x) py_cast<f64>(vm, x)
  208. #define CAST_DEFAULT(T, x, default_value) (x != vm->None) ? py_cast<T>(vm, x) : (default_value)
  209. /*****************************************************************/
  210. template<>
  211. struct Py_<i64> final: PyObject {
  212. i64 _value;
  213. Py_(Type type, i64 val): PyObject(type), _value(val) {}
  214. void _obj_gc_mark() override {}
  215. };
  216. inline bool try_cast_int(PyObject* obj, i64* val) noexcept {
  217. if(is_small_int(obj)){
  218. *val = PK_BITS(obj) >> 2;
  219. return true;
  220. }else if(is_heap_int(obj)){
  221. *val = PK_OBJ_GET(i64, obj);
  222. return true;
  223. }else{
  224. return false;
  225. }
  226. }
  227. template<>
  228. struct Py_<List> final: PyObject {
  229. List _value;
  230. Py_(Type type, List&& val): PyObject(type), _value(std::move(val)) {}
  231. Py_(Type type, const List& val): PyObject(type), _value(val) {}
  232. void _obj_gc_mark() override {
  233. for(PyObject* obj: _value) PK_OBJ_MARK(obj);
  234. }
  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. };
  245. template<>
  246. struct Py_<MappingProxy> final: PyObject {
  247. MappingProxy _value;
  248. Py_(Type type, MappingProxy val): PyObject(type), _value(val) {}
  249. void _obj_gc_mark() override {
  250. PK_OBJ_MARK(_value.obj);
  251. }
  252. };
  253. template<>
  254. struct Py_<BoundMethod> final: PyObject {
  255. BoundMethod _value;
  256. Py_(Type type, BoundMethod val): PyObject(type), _value(val) {}
  257. void _obj_gc_mark() override {
  258. PK_OBJ_MARK(_value.self);
  259. PK_OBJ_MARK(_value.func);
  260. }
  261. };
  262. template<>
  263. struct Py_<StarWrapper> final: PyObject {
  264. StarWrapper _value;
  265. Py_(Type type, StarWrapper val): PyObject(type), _value(val) {}
  266. void _obj_gc_mark() override {
  267. PK_OBJ_MARK(_value.obj);
  268. }
  269. };
  270. template<>
  271. struct Py_<Property> final: PyObject {
  272. Property _value;
  273. Py_(Type type, Property val): PyObject(type), _value(val) {}
  274. void _obj_gc_mark() override {
  275. PK_OBJ_MARK(_value.getter);
  276. PK_OBJ_MARK(_value.setter);
  277. }
  278. };
  279. template<>
  280. struct Py_<Slice> final: PyObject {
  281. Slice _value;
  282. Py_(Type type, Slice val): PyObject(type), _value(val) {}
  283. void _obj_gc_mark() override {
  284. PK_OBJ_MARK(_value.start);
  285. PK_OBJ_MARK(_value.stop);
  286. PK_OBJ_MARK(_value.step);
  287. }
  288. };
  289. template<>
  290. struct Py_<Super> final: PyObject {
  291. Super _value;
  292. template<typename... Args>
  293. Py_(Type type, Args&&... args): PyObject(type), _value(std::forward<Args>(args)...) {}
  294. void _obj_gc_mark() override {
  295. PK_OBJ_MARK(_value.first);
  296. }
  297. };
  298. template<>
  299. struct Py_<DummyInstance> final: PyObject {
  300. Py_(Type type): PyObject(type) {
  301. enable_instance_dict();
  302. }
  303. void _obj_gc_mark() override {}
  304. };
  305. template<>
  306. struct Py_<Type> final: PyObject {
  307. Type _value;
  308. Py_(Type type, Type val): PyObject(type), _value(val) {
  309. enable_instance_dict(kTypeAttrLoadFactor);
  310. }
  311. void _obj_gc_mark() override {}
  312. };
  313. template<>
  314. struct Py_<DummyModule> final: PyObject {
  315. Py_(Type type): PyObject(type) {
  316. enable_instance_dict(kTypeAttrLoadFactor);
  317. }
  318. void _obj_gc_mark() override {}
  319. };
  320. } // namespace pkpy