obj.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #pragma once
  2. #include "common.h"
  3. #include "namedict.h"
  4. #include "tuplelist.h"
  5. namespace pkpy {
  6. struct CodeObject;
  7. struct Frame;
  8. struct Function;
  9. class VM;
  10. #if PK_ENABLE_STD_FUNCTION
  11. using NativeFuncC = std::function<PyObject*(VM*, ArgsView)>;
  12. #else
  13. typedef PyObject* (*NativeFuncC)(VM*, ArgsView);
  14. #endif
  15. typedef int (*LuaStyleFuncC)(VM*);
  16. struct NativeFunc {
  17. NativeFuncC f;
  18. int argc; // DONOT include self
  19. bool method;
  20. // this is designed for lua style C bindings
  21. // access it via `_CAST(NativeFunc&, args[-2])._lua_f`
  22. // (-2) or (-1) depends on the calling convention
  23. LuaStyleFuncC _lua_f;
  24. using UserData = char[32];
  25. UserData _userdata;
  26. bool _has_userdata;
  27. template <typename T>
  28. void set_userdata(T data) {
  29. static_assert(std::is_trivially_copyable_v<T>);
  30. static_assert(sizeof(T) <= sizeof(UserData));
  31. if(_has_userdata) throw std::runtime_error("userdata already set");
  32. _has_userdata = true;
  33. memcpy(_userdata, &data, sizeof(T));
  34. }
  35. template <typename T>
  36. T get_userdata() const {
  37. static_assert(std::is_trivially_copyable_v<T>);
  38. static_assert(sizeof(T) <= sizeof(UserData));
  39. #if DEBUG_EXTRA_CHECK
  40. if(!_has_userdata) throw std::runtime_error("userdata not set");
  41. #endif
  42. return reinterpret_cast<const T&>(_userdata);
  43. }
  44. NativeFunc(NativeFuncC f, int argc, bool method) : f(f), argc(argc), method(method), _has_userdata(false) {}
  45. PyObject* operator()(VM* vm, ArgsView args) const;
  46. };
  47. typedef shared_ptr<CodeObject> CodeObject_;
  48. struct FuncDecl {
  49. struct KwArg {
  50. int key; // index in co->varnames
  51. PyObject* value; // default value
  52. };
  53. CodeObject_ code; // code object of this function
  54. pod_vector<int> args; // indices in co->varnames
  55. pod_vector<KwArg> kwargs; // indices in co->varnames
  56. int starred_arg = -1; // index in co->varnames, -1 if no *arg
  57. int starred_kwarg = -1; // index in co->varnames, -1 if no **kwarg
  58. bool nested = false; // whether this function is nested
  59. void _gc_mark() const;
  60. };
  61. using FuncDecl_ = shared_ptr<FuncDecl>;
  62. struct Function{
  63. FuncDecl_ decl;
  64. bool is_simple;
  65. int argc; // cached argc
  66. PyObject* _module;
  67. NameDict_ _closure;
  68. };
  69. struct BoundMethod {
  70. PyObject* self;
  71. PyObject* func;
  72. BoundMethod(PyObject* self, PyObject* func) : self(self), func(func) {}
  73. bool operator==(const BoundMethod& rhs) const noexcept {
  74. return self == rhs.self && func == rhs.func;
  75. }
  76. bool operator!=(const BoundMethod& rhs) const noexcept {
  77. return self != rhs.self || func != rhs.func;
  78. }
  79. };
  80. struct Property{
  81. PyObject* getter;
  82. PyObject* setter;
  83. Property(PyObject* getter, PyObject* setter) : getter(getter), setter(setter) {}
  84. };
  85. struct Range {
  86. i64 start = 0;
  87. i64 stop = -1;
  88. i64 step = 1;
  89. };
  90. struct StarWrapper{
  91. int level; // either 1 or 2
  92. PyObject* obj;
  93. StarWrapper(int level, PyObject* obj) : level(level), obj(obj) {}
  94. };
  95. struct Bytes{
  96. std::vector<char> _data;
  97. bool _ok;
  98. int size() const noexcept { return _data.size(); }
  99. int operator[](int i) const noexcept { return (int)(uint8_t)_data[i]; }
  100. const char* data() const noexcept { return _data.data(); }
  101. bool operator==(const Bytes& rhs) const noexcept {
  102. return _data == rhs._data;
  103. }
  104. bool operator!=(const Bytes& rhs) const noexcept {
  105. return _data != rhs._data;
  106. }
  107. std::string str() const noexcept { return std::string(_data.begin(), _data.end()); }
  108. Bytes() : _data(), _ok(false) {}
  109. Bytes(std::vector<char>&& data) : _data(std::move(data)), _ok(true) {}
  110. Bytes(const std::string& data) : _data(data.begin(), data.end()), _ok(true) {}
  111. operator bool() const noexcept { return _ok; }
  112. };
  113. using Super = std::pair<PyObject*, Type>;
  114. struct Slice {
  115. PyObject* start;
  116. PyObject* stop;
  117. PyObject* step;
  118. Slice(PyObject* start, PyObject* stop, PyObject* step) : start(start), stop(stop), step(step) {}
  119. };
  120. struct GCHeader {
  121. bool enabled; // whether this object is managed by GC
  122. bool marked; // whether this object is marked
  123. GCHeader() : enabled(true), marked(false) {}
  124. };
  125. struct PyObject{
  126. GCHeader gc;
  127. Type type;
  128. NameDict* _attr;
  129. bool is_attr_valid() const noexcept { return _attr != nullptr; }
  130. NameDict& attr() noexcept { return *_attr; }
  131. PyObject* attr(StrName name) const noexcept { return (*_attr)[name]; }
  132. virtual void _obj_gc_mark() = 0;
  133. PyObject(Type type) : type(type), _attr(nullptr) {}
  134. virtual ~PyObject() {
  135. if(_attr == nullptr) return;
  136. _attr->~NameDict();
  137. pool64.dealloc(_attr);
  138. }
  139. void enable_instance_dict(float lf=kInstAttrLoadFactor) noexcept {
  140. _attr = new(pool64.alloc<NameDict>()) NameDict(lf);
  141. }
  142. };
  143. template <typename, typename=void> struct has_gc_marker : std::false_type {};
  144. template <typename T> struct has_gc_marker<T, std::void_t<decltype(&T::_gc_mark)>> : std::true_type {};
  145. template <typename T>
  146. struct Py_ final: PyObject {
  147. T _value;
  148. void _obj_gc_mark() override {
  149. if constexpr (has_gc_marker<T>::value) {
  150. _value._gc_mark();
  151. }
  152. }
  153. Py_(Type type, const T& value) : PyObject(type), _value(value) {}
  154. Py_(Type type, T&& value) : PyObject(type), _value(std::move(value)) {}
  155. };
  156. struct MappingProxy{
  157. PyObject* obj;
  158. MappingProxy(PyObject* obj) : obj(obj) {}
  159. NameDict& attr() noexcept { return obj->attr(); }
  160. };
  161. #define OBJ_GET(T, obj) (((Py_<T>*)(obj))->_value)
  162. #define OBJ_MARK(obj) \
  163. if(!is_tagged(obj) && !(obj)->gc.marked) { \
  164. (obj)->gc.marked = true; \
  165. (obj)->_obj_gc_mark(); \
  166. if((obj)->is_attr_valid()) gc_mark_namedict((obj)->attr()); \
  167. }
  168. inline void gc_mark_namedict(NameDict& t){
  169. if(t.size() == 0) return;
  170. for(uint16_t i=0; i<t._capacity; i++){
  171. if(t._items[i].first.empty()) continue;
  172. OBJ_MARK(t._items[i].second);
  173. }
  174. }
  175. Str obj_type_name(VM* vm, Type type);
  176. #if DEBUG_NO_BUILTIN_MODULES
  177. #define OBJ_NAME(obj) Str("<?>")
  178. #else
  179. DEF_SNAME(__name__);
  180. #define OBJ_NAME(obj) OBJ_GET(Str, vm->getattr(obj, __name__))
  181. #endif
  182. const int kTpIntIndex = 2;
  183. const int kTpFloatIndex = 3;
  184. inline bool is_type(PyObject* obj, Type type) {
  185. #if DEBUG_EXTRA_CHECK
  186. if(obj == nullptr) throw std::runtime_error("is_type() called with nullptr");
  187. if(is_special(obj)) throw std::runtime_error("is_type() called with special object");
  188. #endif
  189. switch(type.index){
  190. case kTpIntIndex: return is_int(obj);
  191. case kTpFloatIndex: return is_float(obj);
  192. default: return !is_tagged(obj) && obj->type == type;
  193. }
  194. }
  195. inline bool is_non_tagged_type(PyObject* obj, Type type) {
  196. #if DEBUG_EXTRA_CHECK
  197. if(obj == nullptr) throw std::runtime_error("is_non_tagged_type() called with nullptr");
  198. if(is_special(obj)) throw std::runtime_error("is_non_tagged_type() called with special object");
  199. #endif
  200. return !is_tagged(obj) && obj->type == type;
  201. }
  202. union BitsCvt {
  203. i64 _int;
  204. f64 _float;
  205. BitsCvt(i64 val) : _int(val) {}
  206. BitsCvt(f64 val) : _float(val) {}
  207. };
  208. template <typename, typename=void> struct is_py_class : std::false_type {};
  209. template <typename T> struct is_py_class<T, std::void_t<decltype(T::_type)>> : std::true_type {};
  210. template<typename T> T to_void_p(VM*, PyObject*);
  211. template<typename T> T to_c99_struct(VM*, PyObject*);
  212. template<typename __T>
  213. __T py_cast(VM* vm, PyObject* obj) {
  214. using T = std::decay_t<__T>;
  215. if constexpr(std::is_enum_v<T>){
  216. return (__T)py_cast<i64>(vm, obj);
  217. }else if constexpr(std::is_pointer_v<T>){
  218. return to_void_p<T>(vm, obj);
  219. }else if constexpr(is_py_class<T>::value){
  220. T::_check_type(vm, obj);
  221. return OBJ_GET(T, obj);
  222. }else if constexpr(std::is_pod_v<T>){
  223. return to_c99_struct<T>(vm, obj);
  224. }else {
  225. return Discarded();
  226. }
  227. }
  228. template<typename __T>
  229. __T _py_cast(VM* vm, PyObject* obj) {
  230. using T = std::decay_t<__T>;
  231. if constexpr(std::is_enum_v<T>){
  232. return (__T)_py_cast<i64>(vm, obj);
  233. }else if constexpr(std::is_pointer_v<__T>){
  234. return to_void_p<__T>(vm, obj);
  235. }else if constexpr(is_py_class<T>::value){
  236. return OBJ_GET(T, obj);
  237. }else if constexpr(std::is_pod_v<T>){
  238. return to_c99_struct<T>(vm, obj);
  239. }else {
  240. return Discarded();
  241. }
  242. }
  243. #define VAR(x) py_var(vm, x)
  244. #define CAST(T, x) py_cast<T>(vm, x)
  245. #define _CAST(T, x) _py_cast<T>(vm, x)
  246. #define CAST_F(x) vm->num_to_float(x)
  247. /*****************************************************************/
  248. template<>
  249. struct Py_<List> final: PyObject {
  250. List _value;
  251. Py_(Type type, List&& val): PyObject(type), _value(std::move(val)) {}
  252. Py_(Type type, const List& val): PyObject(type), _value(val) {}
  253. void _obj_gc_mark() override {
  254. for(PyObject* obj: _value) OBJ_MARK(obj);
  255. }
  256. };
  257. template<>
  258. struct Py_<Tuple> final: PyObject {
  259. Tuple _value;
  260. Py_(Type type, Tuple&& val): PyObject(type), _value(std::move(val)) {}
  261. Py_(Type type, const Tuple& val): PyObject(type), _value(val) {}
  262. void _obj_gc_mark() override {
  263. for(PyObject* obj: _value) OBJ_MARK(obj);
  264. }
  265. };
  266. template<>
  267. struct Py_<MappingProxy> final: PyObject {
  268. MappingProxy _value;
  269. Py_(Type type, MappingProxy val): PyObject(type), _value(val) {}
  270. void _obj_gc_mark() override {
  271. OBJ_MARK(_value.obj);
  272. }
  273. };
  274. template<>
  275. struct Py_<BoundMethod> final: PyObject {
  276. BoundMethod _value;
  277. Py_(Type type, BoundMethod val): PyObject(type), _value(val) {}
  278. void _obj_gc_mark() override {
  279. OBJ_MARK(_value.self);
  280. OBJ_MARK(_value.func);
  281. }
  282. };
  283. template<>
  284. struct Py_<StarWrapper> final: PyObject {
  285. StarWrapper _value;
  286. Py_(Type type, StarWrapper val): PyObject(type), _value(val) {}
  287. void _obj_gc_mark() override {
  288. OBJ_MARK(_value.obj);
  289. }
  290. };
  291. template<>
  292. struct Py_<Property> final: PyObject {
  293. Property _value;
  294. Py_(Type type, Property val): PyObject(type), _value(val) {}
  295. void _obj_gc_mark() override {
  296. OBJ_MARK(_value.getter);
  297. OBJ_MARK(_value.setter);
  298. }
  299. };
  300. template<>
  301. struct Py_<Slice> final: PyObject {
  302. Slice _value;
  303. Py_(Type type, Slice val): PyObject(type), _value(val) {}
  304. void _obj_gc_mark() override {
  305. OBJ_MARK(_value.start);
  306. OBJ_MARK(_value.stop);
  307. OBJ_MARK(_value.step);
  308. }
  309. };
  310. template<>
  311. struct Py_<Function> final: PyObject {
  312. Function _value;
  313. Py_(Type type, Function val): PyObject(type), _value(val) {
  314. enable_instance_dict();
  315. }
  316. void _obj_gc_mark() override {
  317. _value.decl->_gc_mark();
  318. if(_value._module != nullptr) OBJ_MARK(_value._module);
  319. if(_value._closure != nullptr) gc_mark_namedict(*_value._closure);
  320. }
  321. };
  322. template<>
  323. struct Py_<NativeFunc> final: PyObject {
  324. NativeFunc _value;
  325. Py_(Type type, NativeFunc val): PyObject(type), _value(val) {
  326. enable_instance_dict();
  327. }
  328. void _obj_gc_mark() override {}
  329. };
  330. template<>
  331. struct Py_<Super> final: PyObject {
  332. Super _value;
  333. Py_(Type type, Super val): PyObject(type), _value(val) {}
  334. void _obj_gc_mark() override {
  335. OBJ_MARK(_value.first);
  336. }
  337. };
  338. template<>
  339. struct Py_<DummyInstance> final: PyObject {
  340. Py_(Type type, DummyInstance val): PyObject(type) {
  341. enable_instance_dict();
  342. }
  343. void _obj_gc_mark() override {}
  344. };
  345. template<>
  346. struct Py_<Type> final: PyObject {
  347. Type _value;
  348. Py_(Type type, Type val): PyObject(type), _value(val) {
  349. enable_instance_dict(kTypeAttrLoadFactor);
  350. }
  351. void _obj_gc_mark() override {}
  352. };
  353. template<>
  354. struct Py_<DummyModule> final: PyObject {
  355. Py_(Type type, DummyModule val): PyObject(type) {
  356. enable_instance_dict(kTypeAttrLoadFactor);
  357. }
  358. void _obj_gc_mark() override {}
  359. };
  360. template<typename T>
  361. inline T lambda_get_userdata(PyObject** p){
  362. if(p[-1] != PY_NULL) return OBJ_GET(NativeFunc, p[-1]).get_userdata<T>();
  363. else return OBJ_GET(NativeFunc, p[-2]).get_userdata<T>();
  364. }
  365. } // namespace pkpy