vm.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "common.h"
  4. #include "frame.h"
  5. #include "error.h"
  6. #include "gc.h"
  7. #include "memory.h"
  8. #include "obj.h"
  9. #include "str.h"
  10. #include "tuplelist.h"
  11. #include "dict.h"
  12. #include "profiler.h"
  13. namespace pkpy{
  14. /* Stack manipulation macros */
  15. // https://github.com/python/cpython/blob/3.9/Python/ceval.c#L1123
  16. #define TOP() (s_data.top())
  17. #define SECOND() (s_data.second())
  18. #define THIRD() (s_data.third())
  19. #define STACK_SHRINK(n) (s_data.shrink(n))
  20. #define PUSH(v) (s_data.push(v))
  21. #define POP() (s_data.pop())
  22. #define POPX() (s_data.popx())
  23. #define STACK_VIEW(n) (s_data.view(n))
  24. typedef PyObject* (*BinaryFuncC)(VM*, PyObject*, PyObject*);
  25. typedef void (*RegisterFunc)(VM*, PyObject*, PyObject*);
  26. #if PK_ENABLE_PROFILER
  27. struct NextBreakpoint{
  28. int callstack_size;
  29. int lineno;
  30. bool should_step_into;
  31. NextBreakpoint(): callstack_size(0) {}
  32. NextBreakpoint(int callstack_size, int lineno, bool should_step_into): callstack_size(callstack_size), lineno(lineno), should_step_into(should_step_into) {}
  33. void _step(VM* vm);
  34. bool empty() const { return callstack_size == 0; }
  35. };
  36. #endif
  37. struct PyTypeInfo{
  38. PyObject* obj; // never be garbage collected
  39. Type base;
  40. PyObject* mod; // never be garbage collected
  41. StrName name;
  42. bool subclass_enabled;
  43. std::vector<StrName> annotated_fields = {};
  44. // unary operators
  45. Str (*m__repr__)(VM* vm, PyObject*) = nullptr;
  46. Str (*m__str__)(VM* vm, PyObject*) = nullptr;
  47. i64 (*m__hash__)(VM* vm, PyObject*) = nullptr;
  48. i64 (*m__len__)(VM* vm, PyObject*) = nullptr;
  49. PyObject* (*m__iter__)(VM* vm, PyObject*) = nullptr;
  50. unsigned (*m__next__)(VM* vm, PyObject*) = nullptr;
  51. PyObject* (*m__neg__)(VM* vm, PyObject*) = nullptr;
  52. PyObject* (*m__invert__)(VM* vm, PyObject*) = nullptr;
  53. BinaryFuncC m__eq__ = nullptr;
  54. BinaryFuncC m__lt__ = nullptr;
  55. BinaryFuncC m__le__ = nullptr;
  56. BinaryFuncC m__gt__ = nullptr;
  57. BinaryFuncC m__ge__ = nullptr;
  58. BinaryFuncC m__contains__ = nullptr;
  59. // binary operators
  60. BinaryFuncC m__add__ = nullptr;
  61. BinaryFuncC m__sub__ = nullptr;
  62. BinaryFuncC m__mul__ = nullptr;
  63. BinaryFuncC m__truediv__ = nullptr;
  64. BinaryFuncC m__floordiv__ = nullptr;
  65. BinaryFuncC m__mod__ = nullptr;
  66. BinaryFuncC m__pow__ = nullptr;
  67. BinaryFuncC m__matmul__ = nullptr;
  68. BinaryFuncC m__lshift__ = nullptr;
  69. BinaryFuncC m__rshift__ = nullptr;
  70. BinaryFuncC m__and__ = nullptr;
  71. BinaryFuncC m__or__ = nullptr;
  72. BinaryFuncC m__xor__ = nullptr;
  73. // indexer
  74. PyObject* (*m__getitem__)(VM* vm, PyObject*, PyObject*) = nullptr;
  75. void (*m__setitem__)(VM* vm, PyObject*, PyObject*, PyObject*) = nullptr;
  76. void (*m__delitem__)(VM* vm, PyObject*, PyObject*) = nullptr;
  77. // attributes
  78. void (*m__setattr__)(VM* vm, PyObject*, StrName, PyObject*) = nullptr;
  79. PyObject* (*m__getattr__)(VM* vm, PyObject*, StrName) = nullptr;
  80. bool (*m__delattr__)(VM* vm, PyObject*, StrName) = nullptr;
  81. // backdoors
  82. void (*on_end_subclass)(VM* vm, PyTypeInfo*) = nullptr;
  83. };
  84. struct ImportContext{
  85. PK_ALWAYS_PASS_BY_POINTER(ImportContext)
  86. std::vector<Str> pending;
  87. std::vector<bool> pending_is_init; // a.k.a __init__.py
  88. ImportContext() {}
  89. struct Temp{
  90. PK_ALWAYS_PASS_BY_POINTER(Temp)
  91. ImportContext* ctx;
  92. Temp(ImportContext* ctx, Str name, bool is_init) : ctx(ctx){
  93. ctx->pending.push_back(name);
  94. ctx->pending_is_init.push_back(is_init);
  95. }
  96. ~Temp(){
  97. ctx->pending.pop_back();
  98. ctx->pending_is_init.pop_back();
  99. }
  100. };
  101. Temp scope(Str name, bool is_init){
  102. return {this, name, is_init};
  103. }
  104. };
  105. class VM {
  106. PK_ALWAYS_PASS_BY_POINTER(VM)
  107. VM* vm; // self reference to simplify code
  108. public:
  109. ManagedHeap heap;
  110. ValueStack s_data;
  111. CallStack callstack;
  112. std::vector<PyTypeInfo> _all_types;
  113. NameDict _modules; // loaded modules
  114. std::map<StrName, Str> _lazy_modules; // lazy loaded modules
  115. struct{
  116. PyObject* error;
  117. stack_no_copy<ArgsView> s_view;
  118. } __c;
  119. PyObject *None, *True, *False, *NotImplemented;
  120. PyObject *StopIteration, *Ellipsis;
  121. PyObject *builtins, *_main;
  122. // typeid -> Type
  123. std::map<const std::type_index, Type> _cxx_typeid_map;
  124. // this is for repr() recursion detection (no need to mark)
  125. std::set<PyObject*> _repr_recursion_set;
  126. ImportContext __import_context;
  127. PyObject* __last_exception;
  128. PyObject* __curr_class;
  129. PyObject* __cached_object_new;
  130. std::map<std::string_view, CodeObject_> __cached_codes;
  131. #if PK_ENABLE_PROFILER
  132. LineProfiler* _profiler = nullptr;
  133. NextBreakpoint _next_breakpoint;
  134. #endif
  135. void (*_ceval_on_step)(VM*, Frame*, Bytecode bc);
  136. void(*_stdout)(const char*, int);
  137. void(*_stderr)(const char*, int);
  138. unsigned char* (*_import_handler)(const char*, int*);
  139. // function<void(const char*, int)> _stdout;
  140. // function<void(const char*, int)> _stderr;
  141. // function<unsigned char*(const char*, int*)> _import_handler;
  142. // for quick access
  143. static constexpr Type tp_object=Type(0), tp_type=Type(1);
  144. static constexpr Type tp_int=Type(kTpIntIndex), tp_float=Type(kTpFloatIndex), tp_bool=Type(4), tp_str=Type(5);
  145. static constexpr Type tp_list=Type(6), tp_tuple=Type(7);
  146. static constexpr Type tp_slice=Type(8), tp_range=Type(9), tp_module=Type(10);
  147. static constexpr Type tp_function=Type(11), tp_native_func=Type(12), tp_bound_method=Type(13);
  148. static constexpr Type tp_super=Type(14), tp_exception=Type(15), tp_bytes=Type(16), tp_mappingproxy=Type(17);
  149. static constexpr Type tp_dict=Type(18), tp_property=Type(19), tp_star_wrapper=Type(20);
  150. static constexpr Type tp_staticmethod=Type(21), tp_classmethod=Type(22);
  151. const bool enable_os;
  152. VM(bool enable_os=true);
  153. #if PK_REGION("Python Equivalents")
  154. Str py_str(PyObject* obj); // x -> str(x)
  155. Str py_repr(PyObject* obj); // x -> repr(x)
  156. Str py_json(PyObject* obj); // x -> json.dumps(x)
  157. PyObject* py_iter(PyObject* obj); // x -> iter(x)
  158. PyObject* py_next(PyObject*); // x -> next(x)
  159. PyObject* _py_next(const PyTypeInfo*, PyObject*); // x -> next(x) with type info cache
  160. PyObject* py_import(Str path, bool throw_err=true); // x -> __import__(x)
  161. PyObject* py_negate(PyObject* obj); // x -> -x
  162. List py_list(PyObject*); // x -> list(x)
  163. bool py_callable(PyObject* obj); // x -> callable(x)
  164. bool py_bool(PyObject* obj); // x -> bool(x)
  165. i64 py_hash(PyObject* obj); // x -> hash(x)
  166. bool py_eq(PyObject* lhs, PyObject* rhs); // (lhs, rhs) -> lhs == rhs
  167. bool py_lt(PyObject* lhs, PyObject* rhs); // (lhs, rhs) -> lhs < rhs
  168. bool py_le(PyObject* lhs, PyObject* rhs); // (lhs, rhs) -> lhs <= rhs
  169. bool py_gt(PyObject* lhs, PyObject* rhs); // (lhs, rhs) -> lhs > rhs
  170. bool py_ge(PyObject* lhs, PyObject* rhs); // (lhs, rhs) -> lhs >= rhs
  171. bool py_ne(PyObject* lhs, PyObject* rhs) { // (lhs, rhs) -> lhs != rhs
  172. return !py_eq(lhs, rhs);
  173. }
  174. void py_exec(std::string_view, PyObject*, PyObject*); // exec(source, globals, locals)
  175. PyObject* py_eval(std::string_view, PyObject*, PyObject*); // eval(source, globals, locals)
  176. #endif
  177. #if PK_REGION("Utility Methods")
  178. ArgsView cast_array_view(PyObject* obj);
  179. void set_main_argv(int argc, char** argv);
  180. i64 normalized_index(i64 index, int size);
  181. Str disassemble(CodeObject_ co);
  182. void parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step);
  183. #endif
  184. #if PK_REGION("Name Lookup Methods")
  185. PyObject* find_name_in_mro(Type cls, StrName name);
  186. PyObject* get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err=true, bool fallback=false);
  187. PyObject* getattr(PyObject* obj, StrName name, bool throw_err=true);
  188. void delattr(PyObject* obj, StrName name);
  189. void setattr(PyObject* obj, StrName name, PyObject* value);
  190. #endif
  191. #if PK_REGION("Source Execution Methods")
  192. CodeObject_ compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
  193. Str precompile(std::string_view source, const Str& filename, CompileMode mode);
  194. PyObject* exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module=nullptr);
  195. PyObject* exec(std::string_view source);
  196. PyObject* eval(std::string_view source);
  197. template<typename ...Args>
  198. PyObject* _exec(Args&&... args){
  199. callstack.emplace(s_data._sp, std::forward<Args>(args)...);
  200. return __run_top_frame();
  201. }
  202. #endif
  203. #if PK_REGION("Invocation Methods")
  204. PyObject* vectorcall(int ARGC, int KWARGC=0, bool op_call=false);
  205. template<typename... Args>
  206. PyObject* call(PyObject* callable, Args&&... args){
  207. PUSH(callable); PUSH(PY_NULL);
  208. __push_varargs(args...);
  209. return vectorcall(sizeof...(args));
  210. }
  211. template<typename... Args>
  212. PyObject* call_method(PyObject* self, PyObject* callable, Args&&... args){
  213. PUSH(callable); PUSH(self);
  214. __push_varargs(args...);
  215. return vectorcall(sizeof...(args));
  216. }
  217. template<typename... Args>
  218. PyObject* call_method(PyObject* self, StrName name, Args&&... args){
  219. PyObject* callable = get_unbound_method(self, name, &self);
  220. return call_method(self, callable, args...);
  221. }
  222. #endif
  223. #if PK_REGION("Logging Methods")
  224. virtual void stdout_write(const Str& s){ _stdout(s.data, s.size); }
  225. virtual void stderr_write(const Str& s){ _stderr(s.data, s.size); }
  226. #endif
  227. #if PK_REGION("Magic Bindings")
  228. void bind__repr__(Type type, Str (*f)(VM*, PyObject*));
  229. void bind__str__(Type type, Str (*f)(VM*, PyObject*));
  230. void bind__iter__(Type type, PyObject* (*f)(VM*, PyObject*));
  231. void bind__next__(Type type, unsigned (*f)(VM*, PyObject*));
  232. [[deprecated]] void bind__next__(Type type, PyObject* (*f)(VM*, PyObject*));
  233. void bind__neg__(Type type, PyObject* (*f)(VM*, PyObject*));
  234. void bind__invert__(Type type, PyObject* (*f)(VM*, PyObject*));
  235. void bind__hash__(Type type, i64 (*f)(VM* vm, PyObject*));
  236. void bind__len__(Type type, i64 (*f)(VM* vm, PyObject*));
  237. void bind__eq__(Type type, BinaryFuncC f);
  238. void bind__lt__(Type type, BinaryFuncC f);
  239. void bind__le__(Type type, BinaryFuncC f);
  240. void bind__gt__(Type type, BinaryFuncC f);
  241. void bind__ge__(Type type, BinaryFuncC f);
  242. void bind__contains__(Type type, BinaryFuncC f);
  243. void bind__add__(Type type, BinaryFuncC f);
  244. void bind__sub__(Type type, BinaryFuncC f);
  245. void bind__mul__(Type type, BinaryFuncC f);
  246. void bind__truediv__(Type type, BinaryFuncC f);
  247. void bind__floordiv__(Type type, BinaryFuncC f);
  248. void bind__mod__(Type type, BinaryFuncC f);
  249. void bind__pow__(Type type, BinaryFuncC f);
  250. void bind__matmul__(Type type, BinaryFuncC f);
  251. void bind__lshift__(Type type, BinaryFuncC f);
  252. void bind__rshift__(Type type, BinaryFuncC f);
  253. void bind__and__(Type type, BinaryFuncC f);
  254. void bind__or__(Type type, BinaryFuncC f);
  255. void bind__xor__(Type type, BinaryFuncC f);
  256. void bind__getitem__(Type type, PyObject* (*f)(VM*, PyObject*, PyObject*));
  257. void bind__setitem__(Type type, void (*f)(VM*, PyObject*, PyObject*, PyObject*));
  258. void bind__delitem__(Type type, void (*f)(VM*, PyObject*, PyObject*));
  259. #endif
  260. #if PK_REGION("General Bindings")
  261. PyObject* bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, any userdata={}, BindType bt=BindType::DEFAULT);
  262. PyObject* bind_func(Type type, StrName name, int argc, NativeFuncC fn, any userdata={}, BindType bt=BindType::DEFAULT){
  263. return bind_func(_t(type), name, argc, fn, std::move(userdata), bt);
  264. }
  265. PyObject* bind_property(PyObject*, const char*, NativeFuncC fget, NativeFuncC fset=nullptr);
  266. template<typename T, typename F, bool ReadOnly=false>
  267. PyObject* bind_field(PyObject*, const char*, F T::*);
  268. PyObject* bind(PyObject*, const char*, NativeFuncC, any userdata={}, BindType bt=BindType::DEFAULT);
  269. template<typename Ret, typename... Params>
  270. PyObject* bind(PyObject*, const char*, Ret(*)(Params...), BindType bt=BindType::DEFAULT);
  271. template<typename Ret, typename T, typename... Params>
  272. PyObject* bind(PyObject*, const char*, Ret(T::*)(Params...), BindType bt=BindType::DEFAULT);
  273. PyObject* bind(PyObject*, const char*, const char*, NativeFuncC, any userdata={}, BindType bt=BindType::DEFAULT);
  274. template<typename Ret, typename... Params>
  275. PyObject* bind(PyObject*, const char*, const char*, Ret(*)(Params...), BindType bt=BindType::DEFAULT);
  276. template<typename Ret, typename T, typename... Params>
  277. PyObject* bind(PyObject*, const char*, const char*, Ret(T::*)(Params...), BindType bt=BindType::DEFAULT);
  278. #endif
  279. #if PK_REGION("Error Reporting Methods")
  280. void _error(PyObject*);
  281. void StackOverflowError() { __builtin_error("StackOverflowError"); }
  282. void IOError(const Str& msg) { __builtin_error("IOError", msg); }
  283. void NotImplementedError(){ __builtin_error("NotImplementedError"); }
  284. void TypeError(const Str& msg){ __builtin_error("TypeError", msg); }
  285. void TypeError(Type expected, Type actual) { TypeError("expected " + _type_name(vm, expected).escape() + ", got " + _type_name(vm, actual).escape()); }
  286. void IndexError(const Str& msg){ __builtin_error("IndexError", msg); }
  287. void ValueError(const Str& msg){ __builtin_error("ValueError", msg); }
  288. void RuntimeError(const Str& msg){ __builtin_error("RuntimeError", msg); }
  289. void ZeroDivisionError(const Str& msg){ __builtin_error("ZeroDivisionError", msg); }
  290. void ZeroDivisionError(){ __builtin_error("ZeroDivisionError", "division by zero"); }
  291. void NameError(StrName name){ __builtin_error("NameError", _S("name ", name.escape() + " is not defined")); }
  292. void UnboundLocalError(StrName name){ __builtin_error("UnboundLocalError", _S("local variable ", name.escape() + " referenced before assignment")); }
  293. void KeyError(PyObject* obj){ __builtin_error("KeyError", obj); }
  294. void ImportError(const Str& msg){ __builtin_error("ImportError", msg); }
  295. void AssertionError(const Str& msg){ __builtin_error("AssertionError", msg); }
  296. void AssertionError(){ __builtin_error("AssertionError"); }
  297. void BinaryOptError(const char* op, PyObject* _0, PyObject* _1);
  298. void AttributeError(PyObject* obj, StrName name);
  299. void AttributeError(const Str& msg){ __builtin_error("AttributeError", msg); }
  300. #endif
  301. #if PK_REGION("Type Checking Methods")
  302. bool isinstance(PyObject* obj, Type base);
  303. bool issubclass(Type cls, Type base);
  304. void check_type(PyObject* obj, Type type){ if(!is_type(obj, type)) TypeError(type, _tp(obj)); }
  305. void check_compatible_type(PyObject* obj, Type type){ if(!isinstance(obj, type)) TypeError(type, _tp(obj)); }
  306. Type _tp(PyObject* obj){ return is_small_int(obj) ? tp_int : obj->type; }
  307. const PyTypeInfo* _tp_info(PyObject* obj) { return &_all_types[_tp(obj)]; }
  308. const PyTypeInfo* _tp_info(Type type) { return &_all_types[type]; }
  309. PyObject* _t(PyObject* obj){ return _all_types[_tp(obj)].obj; }
  310. PyObject* _t(Type type){ return _all_types[type].obj; }
  311. #endif
  312. #if PK_REGION("User Type Registration")
  313. PyObject* new_module(Str name, Str package="");
  314. PyObject* new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled=true);
  315. template<typename T>
  316. Type _tp_user(){ return _find_type_in_cxx_typeid_map<T>(); }
  317. template<typename T>
  318. bool is_user_type(PyObject* obj){ return _tp(obj) == _tp_user<T>(); }
  319. template<typename T>
  320. PyObject* register_user_class(PyObject*, StrName, RegisterFunc, Type base=tp_object, bool subclass_enabled=false);
  321. template<typename T>
  322. PyObject* register_user_class(PyObject*, StrName, Type base=tp_object, bool subclass_enabled=false);
  323. template<typename T, typename ...Args>
  324. PyObject* new_user_object(Args&&... args){
  325. return heap.gcnew<T>(_tp_user<T>(), std::forward<Args>(args)...);
  326. }
  327. #endif
  328. template<typename T>
  329. Type _find_type_in_cxx_typeid_map(){
  330. auto it = _cxx_typeid_map.find(typeid(T));
  331. if(it == _cxx_typeid_map.end()){
  332. #if __GNUC__ || __clang__
  333. throw std::runtime_error(__PRETTY_FUNCTION__ + std::string(" failed: T not found"));
  334. #elif _MSC_VER
  335. throw std::runtime_error(__FUNCSIG__ + std::string(" failed: T not found"));
  336. #else
  337. throw std::runtime_error("_find_type_in_cxx_typeid_map() failed: T not found");
  338. #endif
  339. }
  340. return it->second;
  341. }
  342. /********** private **********/
  343. virtual ~VM();
  344. #if PK_DEBUG_CEVAL_STEP
  345. void __log_s_data(const char* title = nullptr);
  346. #endif
  347. void __breakpoint();
  348. PyObject* __format_object(PyObject*, Str);
  349. PyObject* __run_top_frame(lightfunction<void(Frame*)> on_will_pop_base_frame = {});
  350. void __pop_frame();
  351. PyObject* __py_generator(Frame&& frame, ArgsView buffer);
  352. void __op_unpack_sequence(uint16_t arg);
  353. void __prepare_py_call(PyObject**, ArgsView, ArgsView, const FuncDecl_&);
  354. void __unpack_as_list(ArgsView args, List& list);
  355. void __unpack_as_dict(ArgsView args, Dict& dict);
  356. void __raise_exc(bool re_raise=false);
  357. void __init_builtin_types();
  358. void __post_init_builtin_types();
  359. void __builtin_error(StrName type);
  360. void __builtin_error(StrName type, PyObject* arg);
  361. void __builtin_error(StrName type, const Str& msg);
  362. void __push_varargs(){}
  363. void __push_varargs(PyObject* _0){ PUSH(_0); }
  364. void __push_varargs(PyObject* _0, PyObject* _1){ PUSH(_0); PUSH(_1); }
  365. void __push_varargs(PyObject* _0, PyObject* _1, PyObject* _2){ PUSH(_0); PUSH(_1); PUSH(_2); }
  366. void __push_varargs(PyObject* _0, PyObject* _1, PyObject* _2, PyObject* _3){ PUSH(_0); PUSH(_1); PUSH(_2); PUSH(_3); }
  367. PyObject* __pack_next_retval(unsigned);
  368. PyObject* __minmax_reduce(bool (VM::*op)(PyObject*, PyObject*), PyObject* args, PyObject* key);
  369. };
  370. template<typename T>
  371. inline constexpr bool is_immutable_v = is_integral_v<T> || is_floating_point_v<T>
  372. || std::is_same_v<T, Str> || std::is_same_v<T, Tuple> || std::is_same_v<T, Bytes> || std::is_same_v<T, bool>
  373. || std::is_same_v<T, Range> || std::is_same_v<T, Slice>
  374. || std::is_pointer_v<T> || std::is_enum_v<T>;
  375. template<typename T> constexpr Type _find_type_in_const_cxx_typeid_map(){ return Type(-1); }
  376. template<> constexpr Type _find_type_in_const_cxx_typeid_map<Str>(){ return VM::tp_str; }
  377. template<> constexpr Type _find_type_in_const_cxx_typeid_map<List>(){ return VM::tp_list; }
  378. template<> constexpr Type _find_type_in_const_cxx_typeid_map<Tuple>(){ return VM::tp_tuple; }
  379. template<> constexpr Type _find_type_in_const_cxx_typeid_map<Function>(){ return VM::tp_function; }
  380. template<> constexpr Type _find_type_in_const_cxx_typeid_map<NativeFunc>(){ return VM::tp_native_func; }
  381. template<> constexpr Type _find_type_in_const_cxx_typeid_map<BoundMethod>(){ return VM::tp_bound_method; }
  382. template<> constexpr Type _find_type_in_const_cxx_typeid_map<Range>(){ return VM::tp_range; }
  383. template<> constexpr Type _find_type_in_const_cxx_typeid_map<Slice>(){ return VM::tp_slice; }
  384. template<> constexpr Type _find_type_in_const_cxx_typeid_map<Exception>(){ return VM::tp_exception; }
  385. template<> constexpr Type _find_type_in_const_cxx_typeid_map<Bytes>(){ return VM::tp_bytes; }
  386. template<> constexpr Type _find_type_in_const_cxx_typeid_map<MappingProxy>(){ return VM::tp_mappingproxy; }
  387. template<> constexpr Type _find_type_in_const_cxx_typeid_map<Dict>(){ return VM::tp_dict; }
  388. template<> constexpr Type _find_type_in_const_cxx_typeid_map<Property>(){ return VM::tp_property; }
  389. template<> constexpr Type _find_type_in_const_cxx_typeid_map<StarWrapper>(){ return VM::tp_star_wrapper; }
  390. template<> constexpr Type _find_type_in_const_cxx_typeid_map<StaticMethod>(){ return VM::tp_staticmethod; }
  391. template<> constexpr Type _find_type_in_const_cxx_typeid_map<ClassMethod>(){ return VM::tp_classmethod; }
  392. template<typename __T>
  393. PyObject* py_var(VM* vm, __T&& value){
  394. using T = std::decay_t<__T>;
  395. static_assert(!std::is_same_v<T, PyObject*>, "py_var(VM*, PyObject*) is not allowed");
  396. if constexpr(std::is_same_v<T, const char*> || std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>){
  397. // str (shortcuts)
  398. return VAR(Str(std::forward<__T>(value)));
  399. }else if constexpr(std::is_same_v<T, NoReturn>){
  400. // NoneType
  401. return vm->None;
  402. }else if constexpr(std::is_same_v<T, bool>){
  403. // bool
  404. return value ? vm->True : vm->False;
  405. }else if constexpr(is_integral_v<T>){
  406. // int
  407. i64 val = static_cast<i64>(std::forward<__T>(value));
  408. if(val >= Number::kMinSmallInt && val <= Number::kMaxSmallInt){
  409. val = (val << 2) | 0b10;
  410. return reinterpret_cast<PyObject*>(val);
  411. }else{
  412. return vm->heap.gcnew<i64>(vm->tp_int, val);
  413. }
  414. }else if constexpr(is_floating_point_v<T>){
  415. // float
  416. f64 val = static_cast<f64>(std::forward<__T>(value));
  417. return vm->heap.gcnew<f64>(vm->tp_float, val);
  418. }else if constexpr(std::is_pointer_v<T>){
  419. return from_void_p(vm, (void*)value);
  420. }else{
  421. constexpr Type const_type = _find_type_in_const_cxx_typeid_map<T>();
  422. if constexpr(const_type.index >= 0){
  423. return vm->heap.gcnew<T>(const_type, std::forward<__T>(value));
  424. }
  425. }
  426. Type type = vm->_find_type_in_cxx_typeid_map<T>();
  427. return vm->heap.gcnew<T>(type, std::forward<__T>(value));
  428. }
  429. template<typename __T, bool with_check>
  430. __T _py_cast__internal(VM* vm, PyObject* obj) {
  431. static_assert(!std::is_rvalue_reference_v<__T>, "rvalue reference is not allowed");
  432. using T = std::decay_t<__T>;
  433. if constexpr(std::is_same_v<T, const char*> || std::is_same_v<T, CString>){
  434. static_assert(!std::is_reference_v<__T>);
  435. // str (shortcuts)
  436. if(obj == vm->None) return nullptr;
  437. if constexpr(with_check) vm->check_type(obj, vm->tp_str);
  438. return PK_OBJ_GET(Str, obj).c_str();
  439. }else if constexpr(std::is_same_v<T, bool>){
  440. static_assert(!std::is_reference_v<__T>);
  441. // bool
  442. if constexpr(with_check){
  443. if(obj == vm->True) return true;
  444. if(obj == vm->False) return false;
  445. vm->TypeError("expected 'bool', got " + _type_name(vm, vm->_tp(obj)).escape());
  446. }else{
  447. return obj == vm->True;
  448. }
  449. }else if constexpr(is_integral_v<T>){
  450. static_assert(!std::is_reference_v<__T>);
  451. // int
  452. if constexpr(with_check){
  453. if(is_small_int(obj)) return (T)(PK_BITS(obj) >> 2);
  454. if(is_heap_int(obj)) return (T)PK_OBJ_GET(i64, obj);
  455. vm->TypeError("expected 'int', got " + _type_name(vm, vm->_tp(obj)).escape());
  456. }else{
  457. if(is_small_int(obj)) return (T)(PK_BITS(obj) >> 2);
  458. return (T)PK_OBJ_GET(i64, obj);
  459. }
  460. }else if constexpr(is_floating_point_v<T>){
  461. static_assert(!std::is_reference_v<__T>);
  462. // float
  463. if(is_float(obj)) return PK_OBJ_GET(f64, obj);
  464. i64 bits;
  465. if(try_cast_int(obj, &bits)) return (float)bits;
  466. vm->TypeError("expected 'int' or 'float', got " + _type_name(vm, vm->_tp(obj)).escape());
  467. }else if constexpr(std::is_enum_v<T>){
  468. static_assert(!std::is_reference_v<__T>);
  469. return (__T)_py_cast__internal<i64, with_check>(vm, obj);
  470. }else if constexpr(std::is_pointer_v<T>){
  471. static_assert(!std::is_reference_v<__T>);
  472. return to_void_p<T>(vm, obj);
  473. }else{
  474. constexpr Type const_type = _find_type_in_const_cxx_typeid_map<T>();
  475. if constexpr(const_type.index >= 0){
  476. if constexpr(with_check){
  477. if constexpr(std::is_same_v<T, Exception>){
  478. // Exception is `subclass_enabled`
  479. vm->check_compatible_type(obj, const_type);
  480. }else{
  481. vm->check_type(obj, const_type);
  482. }
  483. }
  484. return PK_OBJ_GET(T, obj);
  485. }
  486. }
  487. Type type = vm->_find_type_in_cxx_typeid_map<T>();
  488. if constexpr(with_check) vm->check_compatible_type(obj, type);
  489. return PK_OBJ_GET(T, obj);
  490. }
  491. template<typename __T>
  492. __T py_cast(VM* vm, PyObject* obj) { return _py_cast__internal<__T, true>(vm, obj); }
  493. template<typename __T>
  494. __T _py_cast(VM* vm, PyObject* obj) { return _py_cast__internal<__T, false>(vm, obj); }
  495. template<typename T>
  496. PyObject* VM::register_user_class(PyObject* mod, StrName name, RegisterFunc _register, Type base, bool subclass_enabled){
  497. PyObject* type = new_type_object(mod, name, base, subclass_enabled);
  498. mod->attr().set(name, type);
  499. _cxx_typeid_map[typeid(T)] = PK_OBJ_GET(Type, type);
  500. _register(this, mod, type);
  501. if(!type->attr().contains(__new__)){
  502. if constexpr(std::is_default_constructible_v<T>) {
  503. bind_func(type, __new__, -1, [](VM* vm, ArgsView args){
  504. Type cls_t = PK_OBJ_GET(Type, args[0]);
  505. return vm->heap.gcnew<T>(cls_t);
  506. });
  507. }else{
  508. bind_func(type, __new__, -1, PK_ACTION(vm->NotImplementedError()));
  509. }
  510. }
  511. return type;
  512. }
  513. template<typename T>
  514. PyObject* VM::register_user_class(PyObject* mod, StrName name, Type base, bool subclass_enabled){
  515. return register_user_class<T>(mod, name, &T::_register, base, subclass_enabled);
  516. }
  517. } // namespace pkpy