1
0

vm.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  1. #include "pocketpy/interpreter/vm.hpp"
  2. #include <iostream>
  3. #include <cmath>
  4. #include <stdexcept>
  5. #if PK_DEBUG_CEVAL_STEP
  6. #include <map>
  7. #endif
  8. const static char* OP_NAMES[] = {
  9. #define OPCODE(name) #name,
  10. #include "pocketpy/opcodes.h"
  11. #undef OPCODE
  12. };
  13. namespace pkpy {
  14. struct JsonSerializer {
  15. VM* vm;
  16. PyVar root;
  17. SStream ss;
  18. JsonSerializer(VM* vm, PyVar root) : vm(vm), root(root) {}
  19. template <typename T>
  20. void write_array(T& arr) {
  21. ss << '[';
  22. for(int i = 0; i < arr.size(); i++) {
  23. if(i != 0) ss << ", ";
  24. write_object(arr[i]);
  25. }
  26. ss << ']';
  27. }
  28. void write_dict(Dict& dict) {
  29. ss << '{';
  30. bool first = true;
  31. dict.apply([&](PyVar k, PyVar v) {
  32. if(!first) ss << ", ";
  33. first = false;
  34. if(!is_type(k, VM::tp_str)) {
  35. vm->TypeError(_S("json keys must be string, got ", _type_name(vm, vm->_tp(k))));
  36. }
  37. ss << _CAST(Str&, k).escape('"') << ": ";
  38. write_object(v);
  39. });
  40. ss << '}';
  41. }
  42. void write_object(PyVar obj) {
  43. Type obj_t = vm->_tp(obj);
  44. if(is_none(obj)) {
  45. ss << "null";
  46. } else if(obj_t == vm->tp_int) {
  47. ss << _CAST(i64, obj);
  48. } else if(obj_t == vm->tp_float) {
  49. f64 val = _CAST(f64, obj);
  50. if(std::isinf(val) || std::isnan(val)) vm->ValueError("cannot jsonify 'nan' or 'inf'");
  51. ss << val;
  52. } else if(obj_t == vm->tp_bool) {
  53. ss << (obj == vm->True ? "true" : "false");
  54. } else if(obj_t == vm->tp_str) {
  55. ss << _CAST(Str&, obj).escape('"');
  56. } else if(obj_t == vm->tp_list) {
  57. write_array<List>(_CAST(List&, obj));
  58. } else if(obj_t == vm->tp_tuple) {
  59. write_array<Tuple>(_CAST(Tuple&, obj));
  60. } else if(obj_t == vm->tp_dict) {
  61. write_dict(_CAST(Dict&, obj));
  62. } else {
  63. vm->TypeError(_S("unrecognized type ", _type_name(vm, obj_t).escape()));
  64. }
  65. }
  66. Str serialize() {
  67. auto _lock = vm->heap.gc_scope_lock();
  68. write_object(root);
  69. return ss.str();
  70. }
  71. };
  72. VM::VM(bool enable_os) : heap(this), enable_os(enable_os) {
  73. this->vm = this;
  74. this->__c.error = nullptr;
  75. _ceval_on_step = nullptr;
  76. _stdout = [](const char* buf, int size) {
  77. std::cout.write(buf, size);
  78. };
  79. _stderr = [](const char* buf, int size) {
  80. std::cerr.write(buf, size);
  81. };
  82. builtins = nullptr;
  83. _main = nullptr;
  84. __last_exception = nullptr;
  85. _import_handler = [](const char* name, int* out_size) -> unsigned char* {
  86. return nullptr;
  87. };
  88. __init_builtin_types();
  89. }
  90. Str VM::py_str(PyVar obj) {
  91. const PyTypeInfo* ti = _tp_info(obj);
  92. if(ti->m__str__) return ti->m__str__(this, obj);
  93. PyVar self;
  94. PyVar f = get_unbound_method(obj, __str__, &self, false);
  95. if(self != PY_NULL) {
  96. PyVar retval = call_method(self, f);
  97. if(!is_type(retval, tp_str)) { throw std::runtime_error("object.__str__ must return str"); }
  98. return PK_OBJ_GET(Str, retval);
  99. }
  100. return py_repr(obj);
  101. }
  102. Str VM::py_repr(PyVar obj) {
  103. const PyTypeInfo* ti = _tp_info(obj);
  104. if(ti->m__repr__) return ti->m__repr__(this, obj);
  105. PyVar retval = call_method(obj, __repr__);
  106. if(!is_type(retval, tp_str)) { throw std::runtime_error("object.__repr__ must return str"); }
  107. return PK_OBJ_GET(Str, retval);
  108. }
  109. Str VM::py_json(PyVar obj) {
  110. JsonSerializer j(this, obj);
  111. return j.serialize();
  112. }
  113. PyVar VM::py_iter(PyVar obj) {
  114. const PyTypeInfo* ti = _tp_info(obj);
  115. if(ti->m__iter__) return ti->m__iter__(this, obj);
  116. PyVar self;
  117. PyVar iter_f = get_unbound_method(obj, __iter__, &self, false);
  118. if(self != PY_NULL) return call_method(self, iter_f);
  119. TypeError(_type_name(vm, _tp(obj)).escape() + " object is not iterable");
  120. return nullptr;
  121. }
  122. ArgsView VM::cast_array_view(PyVar obj) {
  123. if(is_type(obj, VM::tp_list)) {
  124. List& list = PK_OBJ_GET(List, obj);
  125. return ArgsView(list.begin(), list.end());
  126. } else if(is_type(obj, VM::tp_tuple)) {
  127. Tuple& tuple = PK_OBJ_GET(Tuple, obj);
  128. return ArgsView(tuple.begin(), tuple.end());
  129. }
  130. TypeError(_S("expected list or tuple, got ", _type_name(this, _tp(obj)).escape()));
  131. }
  132. void VM::set_main_argv(int argc, char** argv) {
  133. PyVar mod = vm->_modules["sys"];
  134. List argv_(argc);
  135. for(int i = 0; i < argc; i++)
  136. argv_[i] = VAR(std::string_view(argv[i]));
  137. mod->attr().set("argv", VAR(std::move(argv_)));
  138. }
  139. PyVar* VM::find_name_in_mro(Type cls, StrName name) {
  140. PyVar* val;
  141. do {
  142. val = _t(cls)->attr().try_get_2(name);
  143. if(val != nullptr) return val;
  144. cls = _all_types[cls].base;
  145. if(!cls) break;
  146. } while(true);
  147. return nullptr;
  148. }
  149. bool VM::isinstance(PyVar obj, Type base) { return issubclass(_tp(obj), base); }
  150. bool VM::issubclass(Type cls, Type base) {
  151. do {
  152. if(cls == base) return true;
  153. Type next = _all_types[cls].base;
  154. if(!next) break;
  155. cls = next;
  156. } while(true);
  157. return false;
  158. }
  159. PyVar VM::exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module) {
  160. if(_module == nullptr) _module = _main;
  161. try {
  162. #if PK_DEBUG_PRECOMPILED_EXEC == 1
  163. Str precompiled = vm->precompile(source, filename, mode);
  164. source = precompiled.sv();
  165. #endif
  166. CodeObject_ code = compile(source, filename, mode);
  167. return _exec(code, _module);
  168. } catch(TopLevelException e) {
  169. stderr_write(e.summary() + "\n");
  170. } catch(const std::exception& e) {
  171. Str msg = "An std::exception occurred! It could be a bug.\n";
  172. msg = msg + e.what() + "\n";
  173. stderr_write(msg);
  174. } catch(NeedMoreLines) { throw; } catch(...) {
  175. Str msg = "An unknown exception occurred! It could be a bug. Please report it to @blueloveTH on GitHub.\n";
  176. stderr_write(msg);
  177. }
  178. callstack.clear();
  179. s_data.clear();
  180. return nullptr;
  181. }
  182. PyVar VM::exec(std::string_view source) { return exec(source, "main.py", EXEC_MODE); }
  183. PyVar VM::eval(std::string_view source) { return exec(source, "<eval>", EVAL_MODE); }
  184. PyObject* VM::new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled, PyTypeInfo::Vt vt) {
  185. PyObject* obj = heap._new<Type>(tp_type, Type(_all_types.size()));
  186. const PyTypeInfo& base_info = _all_types[base];
  187. if(!base_info.subclass_enabled) {
  188. Str error = _S("type ", base_info.name.escape(), " is not `subclass_enabled`");
  189. throw std::runtime_error(error.c_str());
  190. }
  191. if(base_info.vt) {
  192. if(vt) {
  193. Str error = _S("type ", base_info.name.escape(), " has a custom vtable, cannot override");
  194. throw std::runtime_error(error.c_str());
  195. } else {
  196. // promote base vt to its subclass
  197. vt = base_info.vt;
  198. }
  199. }
  200. _all_types.emplace_back(obj, base, mod, name, subclass_enabled, vt);
  201. return obj;
  202. }
  203. bool VM::py_eq(PyVar lhs, PyVar rhs) {
  204. if(is_int(lhs) && is_int(rhs)) return lhs.as<i64>() == rhs.as<i64>();
  205. const PyTypeInfo* ti = _tp_info(lhs);
  206. PyVar res;
  207. if(ti->m__eq__) {
  208. res = ti->m__eq__(this, lhs, rhs);
  209. if(!is_not_implemented(res)) return res == vm->True;
  210. }
  211. res = call_method(lhs, __eq__, rhs);
  212. if(!is_not_implemented(res)) return res == vm->True;
  213. ti = _tp_info(rhs);
  214. if(ti->m__eq__) {
  215. res = ti->m__eq__(this, rhs, lhs);
  216. if(!is_not_implemented(res)) return res == vm->True;
  217. }
  218. res = call_method(rhs, __eq__, lhs);
  219. if(!is_not_implemented(res)) return res == vm->True;
  220. return false;
  221. }
  222. PyVar VM::py_op(std::string_view name) {
  223. PyVar func;
  224. auto it = __cached_op_funcs.try_get(name);
  225. if(it == nullptr) {
  226. func = py_import("operator")->attr(StrName::get(name));
  227. __cached_op_funcs.insert(name, func);
  228. } else {
  229. func = *it;
  230. }
  231. return func;
  232. }
  233. i64 VM::normalized_index(i64 index, int size) {
  234. if(index < 0) index += size;
  235. if(index < 0 || index >= size) { IndexError(std::to_string(index) + " not in [0, " + std::to_string(size) + ")"); }
  236. return index;
  237. }
  238. PyVar VM::_py_next(const PyTypeInfo* ti, PyVar obj) {
  239. if(ti->op__next__) {
  240. unsigned n = ti->op__next__(this, obj);
  241. return __pack_next_retval(n);
  242. }
  243. return call_method(obj, __next__);
  244. }
  245. PyVar VM::py_next(PyVar obj) {
  246. const PyTypeInfo* ti = _tp_info(obj);
  247. return _py_next(ti, obj);
  248. }
  249. bool VM::py_callable(PyVar obj) {
  250. Type cls = vm->_tp(obj);
  251. switch(cls.index) {
  252. case VM::tp_function.index: return true;
  253. case VM::tp_native_func.index: return true;
  254. case VM::tp_bound_method.index: return true;
  255. case VM::tp_type.index: return true;
  256. }
  257. return vm->find_name_in_mro(cls, __call__) != nullptr;
  258. }
  259. PyVar VM::__minmax_reduce(bool (VM::*op)(PyVar, PyVar), PyVar args, PyVar key) {
  260. auto _lock = heap.gc_scope_lock();
  261. const Tuple& args_tuple = PK_OBJ_GET(Tuple, args); // from *args, it must be a tuple
  262. if(is_none(key) && args_tuple.size() == 2) {
  263. // fast path
  264. PyVar a = args_tuple[0];
  265. PyVar b = args_tuple[1];
  266. return (this->*op)(a, b) ? a : b;
  267. }
  268. if(args_tuple.size() == 0) TypeError("expected at least 1 argument, got 0");
  269. ArgsView view(nullptr, nullptr);
  270. if(args_tuple.size() == 1) {
  271. view = cast_array_view(args_tuple[0]);
  272. } else {
  273. view = ArgsView(args_tuple);
  274. }
  275. if(view.empty()) ValueError("arg is an empty sequence");
  276. PyVar res = view[0];
  277. if(is_none(key)) {
  278. for(int i = 1; i < view.size(); i++) {
  279. if((this->*op)(view[i], res)) res = view[i];
  280. }
  281. } else {
  282. auto _lock = heap.gc_scope_lock();
  283. for(int i = 1; i < view.size(); i++) {
  284. PyVar a = call(key, view[i]);
  285. PyVar b = call(key, res);
  286. if((this->*op)(a, b)) res = view[i];
  287. }
  288. }
  289. return res;
  290. }
  291. PyObject* VM::py_import(Str path, bool throw_err) {
  292. if(path.empty()) vm->ValueError("empty module name");
  293. static auto f_join = [](const vector<std::string_view>& cpnts) {
  294. SStream ss;
  295. for(int i = 0; i < cpnts.size(); i++) {
  296. if(i != 0) ss << ".";
  297. ss << cpnts[i];
  298. }
  299. return ss.str();
  300. };
  301. if(path[0] == '.') {
  302. if(__import_context.pending.empty()) { ImportError("relative import outside of package"); }
  303. Str curr_path = __import_context.pending.back();
  304. bool curr_is_init = __import_context.pending_is_init.back();
  305. // convert relative path to absolute path
  306. vector<std::string_view> cpnts = curr_path.split('.');
  307. int prefix = 0; // how many dots in the prefix
  308. for(int i = 0; i < path.length(); i++) {
  309. if(path[i] == '.')
  310. prefix++;
  311. else
  312. break;
  313. }
  314. if(prefix > cpnts.size()) ImportError("attempted relative import beyond top-level package");
  315. path = path.substr(prefix); // remove prefix
  316. for(int i = (int)curr_is_init; i < prefix; i++)
  317. cpnts.pop_back();
  318. if(!path.empty()) cpnts.push_back(path.sv());
  319. path = f_join(cpnts);
  320. }
  321. assert(path.begin()[0] != '.' && path.end()[-1] != '.');
  322. // check existing module
  323. StrName name(path);
  324. PyVar ext_mod = _modules.try_get(name);
  325. if(ext_mod != nullptr) return ext_mod.get();
  326. vector<std::string_view> path_cpnts = path.split('.');
  327. // check circular import
  328. if(__import_context.pending.size() > 128) { ImportError("maximum recursion depth exceeded while importing"); }
  329. // try import
  330. Str filename = path.replace('.', PK_PLATFORM_SEP) + ".py";
  331. Str source;
  332. bool is_init = false;
  333. auto it = _lazy_modules.try_get(name);
  334. if(it == nullptr) {
  335. int out_size;
  336. unsigned char* out = _import_handler(filename.c_str(), &out_size);
  337. if(out == nullptr) {
  338. filename = path.replace('.', PK_PLATFORM_SEP).str() + PK_PLATFORM_SEP + "__init__.py";
  339. is_init = true;
  340. out = _import_handler(filename.c_str(), &out_size);
  341. }
  342. if(out == nullptr) {
  343. if(throw_err)
  344. ImportError(_S("module ", path.escape(), " not found"));
  345. else
  346. return nullptr;
  347. }
  348. assert(out_size >= 0);
  349. source = Str(std::string_view((char*)out, out_size));
  350. std::free(out);
  351. } else {
  352. source = *it;
  353. // _lazy_modules.erase(it); // no need to erase
  354. }
  355. auto _ = __import_context.scope(path, is_init);
  356. CodeObject_ code = compile(source, filename, EXEC_MODE);
  357. Str name_cpnt = path_cpnts.back();
  358. path_cpnts.pop_back();
  359. PyObject* new_mod = new_module(name_cpnt, f_join(path_cpnts));
  360. _exec(code, new_mod);
  361. return new_mod;
  362. }
  363. VM::~VM() {
  364. // clear managed heap
  365. for(PyObject* obj: heap.gen)
  366. heap._delete(obj);
  367. for(PyObject* obj: heap._no_gc)
  368. heap._delete(obj);
  369. // clear everything
  370. callstack.clear();
  371. s_data.clear();
  372. _all_types.clear();
  373. _modules.clear();
  374. _lazy_modules.clear();
  375. }
  376. PyVar VM::py_negate(PyVar obj) {
  377. const PyTypeInfo* ti = _tp_info(obj);
  378. if(ti->m__neg__) return ti->m__neg__(this, obj);
  379. return call_method(obj, __neg__);
  380. }
  381. bool VM::__py_bool_non_trivial(PyVar obj) {
  382. if(is_none(obj)) return false;
  383. if(is_int(obj)) return _CAST(i64, obj) != 0;
  384. if(is_float(obj)) return _CAST(f64, obj) != 0.0;
  385. PyVar self;
  386. PyVar len_f = get_unbound_method(obj, __len__, &self, false);
  387. if(self != PY_NULL) {
  388. PyVar ret = call_method(self, len_f);
  389. return CAST(i64, ret) != 0;
  390. }
  391. return true;
  392. }
  393. void VM::__obj_gc_mark(PyObject* obj) {
  394. if(obj->gc_marked) return;
  395. obj->gc_marked = true;
  396. const PyTypeInfo* ti = _tp_info(obj->type);
  397. if(ti->vt._gc_mark) ti->vt._gc_mark(obj->_value_ptr(), this);
  398. if(obj->is_attr_valid()) {
  399. obj->attr().apply([](StrName _, PyVar obj, void* userdata) {
  400. VM* vm = (VM*)userdata;
  401. if(obj.is_ptr) vm->__obj_gc_mark((obj).get());
  402. }, vm);
  403. }
  404. }
  405. void VM::__stack_gc_mark(PyVar* begin, PyVar* end) {
  406. for(PyVar* it = begin; it != end; it++) {
  407. if(it->is_ptr) {
  408. __obj_gc_mark(it->get());
  409. }
  410. }
  411. }
  412. List VM::py_list(PyVar it) {
  413. auto _lock = heap.gc_scope_lock();
  414. it = py_iter(it);
  415. List list;
  416. const PyTypeInfo* info = _tp_info(it);
  417. PyVar obj = _py_next(info, it);
  418. while(obj != StopIteration) {
  419. list.push_back(obj);
  420. obj = _py_next(info, it);
  421. }
  422. return list;
  423. }
  424. void VM::parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step) {
  425. auto clip = [](int value, int min, int max) {
  426. if(value < min) return min;
  427. if(value > max) return max;
  428. return value;
  429. };
  430. if(is_none(s.step))
  431. step = 1;
  432. else
  433. step = CAST(int, s.step);
  434. if(step == 0) ValueError("slice step cannot be zero");
  435. if(step > 0) {
  436. if(is_none(s.start)) {
  437. start = 0;
  438. } else {
  439. start = CAST(int, s.start);
  440. if(start < 0) start += length;
  441. start = clip(start, 0, length);
  442. }
  443. if(is_none(s.stop)) {
  444. stop = length;
  445. } else {
  446. stop = CAST(int, s.stop);
  447. if(stop < 0) stop += length;
  448. stop = clip(stop, 0, length);
  449. }
  450. } else {
  451. if(is_none(s.start)) {
  452. start = length - 1;
  453. } else {
  454. start = CAST(int, s.start);
  455. if(start < 0) start += length;
  456. start = clip(start, -1, length - 1);
  457. }
  458. if(is_none(s.stop)) {
  459. stop = -1;
  460. } else {
  461. stop = CAST(int, s.stop);
  462. if(stop < 0) stop += length;
  463. stop = clip(stop, -1, length - 1);
  464. }
  465. }
  466. }
  467. i64 VM::py_hash(PyVar obj) {
  468. // https://docs.python.org/3.10/reference/datamodel.html#object.__hash__
  469. const PyTypeInfo* ti = _tp_info(obj);
  470. if(ti->m__hash__) return ti->m__hash__(this, obj);
  471. PyVar self;
  472. PyVar f = get_unbound_method(obj, __hash__, &self, false);
  473. if(f != nullptr) {
  474. PyVar ret = call_method(self, f);
  475. return CAST(i64, ret);
  476. }
  477. // if it is trivial `object`, return PK_BITS
  478. if(ti == &_all_types[tp_object]) return obj.hash();
  479. // otherwise, we check if it has a custom __eq__ other than object.__eq__
  480. bool has_custom_eq = false;
  481. if(ti->m__eq__)
  482. has_custom_eq = true;
  483. else {
  484. f = get_unbound_method(obj, __eq__, &self, false);
  485. has_custom_eq = f != _t(tp_object)->attr(__eq__);
  486. }
  487. if(has_custom_eq) {
  488. TypeError(_S("unhashable type: ", ti->name.escape()));
  489. } else {
  490. return obj.hash();
  491. }
  492. }
  493. PyVar VM::__py_exec_internal(const CodeObject_& code, PyVar globals, PyVar locals) {
  494. Frame* frame = nullptr;
  495. if(!callstack.empty()) frame = &callstack.top();
  496. // fast path
  497. if(frame && is_none(globals) && is_none(locals)) {
  498. return vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals);
  499. }
  500. auto _lock = heap.gc_scope_lock(); // for safety
  501. PyObject* globals_obj = nullptr;
  502. Dict* globals_dict = nullptr;
  503. NameDict_ locals_closure = nullptr;
  504. Dict* locals_dict = nullptr;
  505. if(is_none(globals)){
  506. globals_obj = frame ? frame->_module: _main;
  507. } else {
  508. if(is_type(globals, VM::tp_mappingproxy)) {
  509. globals_obj = PK_OBJ_GET(MappingProxy, globals).obj;
  510. } else {
  511. check_compatible_type(globals, VM::tp_dict);
  512. // make a temporary object and copy globals into it
  513. globals_obj = new_object<DummyInstance>(VM::tp_object).get();
  514. globals_obj->_attr = new NameDict();
  515. globals_dict = &PK_OBJ_GET(Dict, globals);
  516. globals_dict->apply([&](PyVar k, PyVar v) {
  517. globals_obj->attr().set(CAST(Str&, k), v);
  518. });
  519. }
  520. }
  521. PyVar retval = nullptr;
  522. if(is_none(locals)) {
  523. retval = vm->_exec(code, globals_obj); // only globals
  524. } else {
  525. check_compatible_type(locals, VM::tp_dict);
  526. locals_dict = &PK_OBJ_GET(Dict, locals);
  527. locals_closure = std::make_shared<NameDict>();
  528. locals_dict->apply([&](PyVar k, PyVar v) {
  529. locals_closure->set(CAST(Str&, k), v);
  530. });
  531. PyObject* _callable =
  532. heap.gcnew<Function>(tp_function, __dynamic_func_decl, globals_obj, nullptr, locals_closure);
  533. retval = vm->_exec(code.get(), globals_obj, _callable, vm->s_data._sp);
  534. }
  535. if(globals_dict) {
  536. globals_dict->clear();
  537. for(auto [k, v]: globals_obj->attr().items()){
  538. globals_dict->set(vm, VAR(k.sv()), v);
  539. }
  540. }
  541. if(locals_dict) {
  542. locals_dict->clear();
  543. for(auto [k, v]: locals_closure->items()){
  544. locals_dict->set(vm, VAR(k.sv()), v);
  545. }
  546. }
  547. return retval;
  548. }
  549. void VM::py_exec(std::string_view source, PyVar globals, PyVar locals) {
  550. CodeObject_ code = vm->compile(source, "<exec>", EXEC_MODE, true);
  551. __py_exec_internal(code, globals, locals);
  552. }
  553. PyVar VM::py_eval(std::string_view source, PyVar globals, PyVar locals) {
  554. CodeObject_ code = vm->compile(source, "<eval>", EVAL_MODE, true);
  555. return __py_exec_internal(code, globals, locals);
  556. }
  557. PyVar VM::__format_object(PyVar obj, Str spec) {
  558. if(spec.empty()) return VAR(py_str(obj));
  559. char type;
  560. switch(spec.end()[-1]) {
  561. case 'f':
  562. case 'd':
  563. case 's':
  564. type = spec.end()[-1];
  565. spec = spec.slice(0, spec.length() - 1);
  566. break;
  567. default: type = ' '; break;
  568. }
  569. char pad_c = ' ';
  570. for(char c: std::string_view("0-=*#@!~")) {
  571. if(spec[0] == c) {
  572. pad_c = c;
  573. spec = spec.substr(1);
  574. break;
  575. }
  576. }
  577. char align;
  578. if(spec[0] == '^') {
  579. align = '^';
  580. spec = spec.substr(1);
  581. } else if(spec[0] == '>') {
  582. align = '>';
  583. spec = spec.substr(1);
  584. } else if(spec[0] == '<') {
  585. align = '<';
  586. spec = spec.substr(1);
  587. } else {
  588. if(is_int(obj) || is_float(obj))
  589. align = '>';
  590. else
  591. align = '<';
  592. }
  593. int dot = spec.index(".");
  594. int width, precision;
  595. try {
  596. if(dot >= 0) {
  597. if(dot == 0) {
  598. width = -1;
  599. } else {
  600. width = std::stoi(spec.slice(0, dot).str());
  601. }
  602. precision = std::stoi(spec.slice(dot + 1).str());
  603. } else {
  604. width = std::stoi(spec.str());
  605. precision = -1;
  606. }
  607. } catch(...) { ValueError("invalid format specifer"); }
  608. if(type != 'f' && dot >= 0) ValueError("precision not allowed in the format specifier");
  609. Str ret;
  610. if(type == 'f') {
  611. f64 val = CAST(f64, obj);
  612. if(precision < 0) precision = 6;
  613. SStream ss;
  614. ss.setprecision(precision);
  615. ss << val;
  616. ret = ss.str();
  617. } else if(type == 'd') {
  618. ret = std::to_string(CAST(i64, obj));
  619. } else if(type == 's') {
  620. ret = CAST(Str&, obj);
  621. } else {
  622. ret = py_str(obj);
  623. }
  624. if(width != -1 && width > ret.length()) {
  625. int pad = width - ret.length();
  626. if(align == '>' || align == '<') {
  627. std::string padding(pad, pad_c);
  628. if(align == '>')
  629. ret = padding.c_str() + ret;
  630. else
  631. ret = ret + padding.c_str();
  632. } else { // ^
  633. int pad_left = pad / 2;
  634. int pad_right = pad - pad_left;
  635. std::string padding_left(pad_left, pad_c);
  636. std::string padding_right(pad_right, pad_c);
  637. ret = padding_left.c_str() + ret + padding_right.c_str();
  638. }
  639. }
  640. return VAR(ret);
  641. }
  642. PyObject* VM::new_module(Str name, Str package) {
  643. PyObject* obj = heap._new<DummyModule>(tp_module);
  644. obj->attr().set(__name__, VAR(name));
  645. obj->attr().set(__package__, VAR(package));
  646. // convert to fullname
  647. if(!package.empty()) name = package + "." + name;
  648. obj->attr().set(__path__, VAR(name));
  649. // we do not allow override in order to avoid memory leak
  650. // it is because Module objects are not garbage collected
  651. if(_modules.contains(name)) { throw std::runtime_error(_S("module ", name.escape(), " already exists").str()); }
  652. // set it into _modules
  653. _modules.set(name, obj);
  654. return obj;
  655. }
  656. static std::string _opcode_argstr(VM* vm, int i, Bytecode byte, const CodeObject* co) {
  657. SStream ss;
  658. if(byte.is_forward_jump()) {
  659. std::string argStr = std::to_string((int16_t)byte.arg);
  660. ss << (i64)(int16_t)byte.arg;
  661. ss << " (to " << (i64)((int16_t)byte.arg + i) << ")";
  662. return ss.str().str();
  663. }
  664. ss << (i64)byte.arg;
  665. switch(byte.op) {
  666. case OP_LOAD_CONST:
  667. case OP_FORMAT_STRING:
  668. case OP_IMPORT_PATH:
  669. if(vm != nullptr) ss << " (" << vm->py_repr(co->consts[byte.arg]) << ")";
  670. break;
  671. case OP_LOAD_NAME:
  672. case OP_LOAD_GLOBAL:
  673. case OP_LOAD_NONLOCAL:
  674. case OP_STORE_GLOBAL:
  675. case OP_LOAD_ATTR:
  676. case OP_LOAD_METHOD:
  677. case OP_STORE_ATTR:
  678. case OP_DELETE_ATTR:
  679. case OP_BEGIN_CLASS:
  680. case OP_GOTO:
  681. case OP_DELETE_GLOBAL:
  682. case OP_STORE_CLASS_ATTR:
  683. case OP_FOR_ITER_STORE_GLOBAL: ss << " (" << StrName(byte.arg).sv() << ")"; break;
  684. case OP_LOAD_FAST:
  685. case OP_STORE_FAST:
  686. case OP_DELETE_FAST:
  687. case OP_FOR_ITER_STORE_FAST:
  688. case OP_LOAD_SUBSCR_FAST:
  689. case OP_STORE_SUBSCR_FAST: ss << " (" << co->varnames[byte.arg].sv() << ")"; break;
  690. case OP_LOAD_FUNCTION: ss << " (" << co->func_decls[byte.arg]->code->name << ")"; break;
  691. }
  692. return ss.str().str();
  693. }
  694. Str VM::disassemble(CodeObject_ co) {
  695. auto pad = [](const Str& s, const int n) {
  696. if(s.length() >= n) return s.slice(0, n);
  697. return s + std::string(n - s.length(), ' ');
  698. };
  699. vector<int> jumpTargets;
  700. for(int i = 0; i < co->codes.size(); i++) {
  701. Bytecode byte = co->codes[i];
  702. if(byte.is_forward_jump()) { jumpTargets.push_back((int16_t)byte.arg + i); }
  703. }
  704. SStream ss;
  705. int prev_line = -1;
  706. for(int i = 0; i < co->codes.size(); i++) {
  707. const Bytecode& byte = co->codes[i];
  708. Str line = std::to_string(co->lines[i].lineno);
  709. if(co->lines[i].lineno == prev_line)
  710. line = "";
  711. else {
  712. if(prev_line != -1) ss << "\n";
  713. prev_line = co->lines[i].lineno;
  714. }
  715. std::string pointer;
  716. if(jumpTargets.contains(i)) {
  717. pointer = "-> ";
  718. } else {
  719. pointer = " ";
  720. }
  721. ss << pad(line, 8) << pointer << pad(std::to_string(i), 3);
  722. std::string bc_name(OP_NAMES[byte.op]);
  723. if(co->lines[i].is_virtual) bc_name += '*';
  724. ss << " " << pad(bc_name, 25) << " ";
  725. std::string argStr = _opcode_argstr(this, i, byte, co.get());
  726. ss << argStr;
  727. if(i != co->codes.size() - 1) ss << '\n';
  728. }
  729. for(auto& decl: co->func_decls) {
  730. ss << "\n\n"
  731. << "Disassembly of " << decl->code->name << ":\n";
  732. ss << disassemble(decl->code);
  733. }
  734. ss << "\n";
  735. return Str(ss.str());
  736. }
  737. #if PK_DEBUG_CEVAL_STEP
  738. void VM::__log_s_data(const char* title) {
  739. // if(_main == nullptr) return;
  740. if(callstack.empty()) return;
  741. SStream ss;
  742. if(title) ss << title << " | ";
  743. std::map<PyVar*, int> sp_bases;
  744. callstack.apply([&](Frame& f) {
  745. assert(f._sp_base != nullptr);
  746. sp_bases[f._sp_base] += 1;
  747. });
  748. Frame* frame = &callstack.top();
  749. int line = frame->curr_lineno();
  750. ss << frame->co->name << ":" << line << " [";
  751. for(PyVar* p = s_data.begin(); p != s_data.end(); p++) {
  752. ss << std::string(sp_bases[p], '|');
  753. if(sp_bases[p] > 0) ss << " ";
  754. if(*p == PY_NULL)
  755. ss << "NULL";
  756. else {
  757. switch(p->type) {
  758. case tp_none_type: ss << "None"; break;
  759. case tp_int: ss << _CAST(i64, *p); break;
  760. case tp_float: ss << _CAST(f64, *p); break;
  761. case tp_bool: ss << ((*p == True) ? "True" : "False"); break;
  762. case tp_str: ss << _CAST(Str, *p).escape(); break;
  763. case tp_function: ss << p->obj_get<Function>().decl->code->name << "()"; break;
  764. case tp_type: ss << "<class " + _type_name(this, p->obj_get<Type>()).escape() + ">"; break;
  765. case tp_list: ss << "list(size=" << p->obj_get<List>().size() << ")"; break;
  766. case tp_tuple: ss << "tuple(size=" << p->obj_get<Tuple>().size() << ")"; break;
  767. default: ss << "(" << _type_name(this, p->type) << ")"; break;
  768. }
  769. }
  770. ss << ", ";
  771. }
  772. std::string output = ss.str().str();
  773. if(!s_data.empty()) {
  774. output.pop_back();
  775. output.pop_back();
  776. }
  777. output.push_back(']');
  778. Bytecode byte = *frame->_ip;
  779. std::cout << output << " " << OP_NAMES[byte.op] << " " << _opcode_argstr(nullptr, frame->ip(), byte, frame->co)
  780. << std::endl;
  781. }
  782. #endif
  783. void VM::__init_builtin_types() {
  784. _all_types.emplace_back(nullptr, Type(), nullptr, "", false); // 0 is not used
  785. _all_types.emplace_back(heap._new<Type>(tp_type, tp_object), Type(), nullptr, "object", true);
  786. _all_types.emplace_back(heap._new<Type>(tp_type, tp_type), tp_object, nullptr, "type", false);
  787. auto validate = [](Type type, PyObject* ret) {
  788. Type ret_t = ret->as<Type>();
  789. if(ret_t != type) exit(-3);
  790. };
  791. validate(tp_int, new_type_object(nullptr, "int", tp_object, false));
  792. validate(tp_float, new_type_object(nullptr, "float", tp_object, false));
  793. validate(tp_bool, new_type_object(nullptr, "bool", tp_object, false));
  794. validate(tp_str, new_type_object<Str>(nullptr, "str", tp_object, false));
  795. validate(tp_list, new_type_object<List>(nullptr, "list", tp_object, false));
  796. validate(tp_tuple, new_type_object<Tuple>(nullptr, "tuple", tp_object, false));
  797. validate(tp_slice, new_type_object<Slice>(nullptr, "slice", tp_object, false));
  798. validate(tp_range, new_type_object<Range>(nullptr, "range", tp_object, false));
  799. validate(tp_module, new_type_object<DummyModule>(nullptr, "module", tp_object, false));
  800. validate(tp_function, new_type_object<Function>(nullptr, "function", tp_object, false));
  801. validate(tp_native_func, new_type_object<NativeFunc>(nullptr, "native_func", tp_object, false));
  802. validate(tp_bound_method, new_type_object<BoundMethod>(nullptr, "bound_method", tp_object, false));
  803. validate(tp_super, new_type_object<Super>(nullptr, "super", tp_object, false));
  804. validate(tp_exception, new_type_object<Exception>(nullptr, "Exception", tp_object, true));
  805. validate(tp_bytes, new_type_object<Bytes>(nullptr, "bytes", tp_object, false));
  806. validate(tp_mappingproxy, new_type_object<MappingProxy>(nullptr, "mappingproxy", tp_object, false));
  807. validate(tp_dict, new_type_object<Dict>(nullptr, "dict", tp_object, true));
  808. validate(tp_property, new_type_object<Property>(nullptr, "property", tp_object, false));
  809. validate(tp_star_wrapper, new_type_object<StarWrapper>(nullptr, "_star_wrapper", tp_object, false));
  810. validate(tp_staticmethod, new_type_object<StaticMethod>(nullptr, "staticmethod", tp_object, false));
  811. validate(tp_classmethod, new_type_object<ClassMethod>(nullptr, "classmethod", tp_object, false));
  812. validate(tp_none_type, new_type_object(nullptr, "NoneType", tp_object, false));
  813. validate(tp_not_implemented_type, new_type_object(nullptr, "NotImplementedType", tp_object, false));
  814. validate(tp_ellipsis, new_type_object(nullptr, "ellipsis", tp_object, false));
  815. // SyntaxError and IndentationError must be created here
  816. PyObject* SyntaxError = new_type_object(nullptr, "SyntaxError", tp_exception, true);
  817. PyObject* IndentationError = new_type_object(nullptr, "IndentationError", SyntaxError->as<Type>(), true);
  818. this->StopIteration = new_type_object(nullptr, "StopIteration", tp_exception, true);
  819. this->builtins = new_module("builtins");
  820. // setup public types
  821. builtins->attr().set("type", _t(tp_type));
  822. builtins->attr().set("object", _t(tp_object));
  823. builtins->attr().set("bool", _t(tp_bool));
  824. builtins->attr().set("int", _t(tp_int));
  825. builtins->attr().set("float", _t(tp_float));
  826. builtins->attr().set("str", _t(tp_str));
  827. builtins->attr().set("list", _t(tp_list));
  828. builtins->attr().set("tuple", _t(tp_tuple));
  829. builtins->attr().set("range", _t(tp_range));
  830. builtins->attr().set("bytes", _t(tp_bytes));
  831. builtins->attr().set("dict", _t(tp_dict));
  832. builtins->attr().set("property", _t(tp_property));
  833. builtins->attr().set("StopIteration", StopIteration);
  834. builtins->attr().set("NotImplemented", NotImplemented);
  835. builtins->attr().set("slice", _t(tp_slice));
  836. builtins->attr().set("Exception", _t(tp_exception));
  837. builtins->attr().set("SyntaxError", SyntaxError);
  838. builtins->attr().set("IndentationError", IndentationError);
  839. __post_init_builtin_types();
  840. this->_main = new_module("__main__");
  841. }
  842. void VM::__unpack_as_list(ArgsView args, List& list) {
  843. auto _lock = heap.gc_scope_lock();
  844. for(PyVar obj: args) {
  845. if(is_type(obj, tp_star_wrapper)) {
  846. const StarWrapper& w = _CAST(StarWrapper&, obj);
  847. // maybe this check should be done in the compile time
  848. if(w.level != 1) TypeError("expected level 1 star wrapper");
  849. PyVar _0 = py_iter(w.obj);
  850. const PyTypeInfo* info = _tp_info(_0);
  851. PyVar _1 = _py_next(info, _0);
  852. while(_1 != StopIteration) {
  853. list.push_back(_1);
  854. _1 = _py_next(info, _0);
  855. }
  856. } else {
  857. list.push_back(obj);
  858. }
  859. }
  860. }
  861. void VM::__unpack_as_dict(ArgsView args, Dict& dict) {
  862. auto _lock = heap.gc_scope_lock();
  863. for(PyVar obj: args) {
  864. if(is_type(obj, tp_star_wrapper)) {
  865. const StarWrapper& w = _CAST(StarWrapper&, obj);
  866. // maybe this check should be done in the compile time
  867. if(w.level != 2) TypeError("expected level 2 star wrapper");
  868. const Dict& other = CAST(Dict&, w.obj);
  869. dict.update(this, other);
  870. } else {
  871. const Tuple& t = CAST(Tuple&, obj);
  872. if(t.size() != 2) TypeError("expected tuple of length 2");
  873. dict.set(this, t[0], t[1]);
  874. }
  875. }
  876. }
  877. void VM::__prepare_py_call(PyVar* buffer, ArgsView args, ArgsView kwargs, const FuncDecl_& decl) {
  878. const CodeObject* co = decl->code.get();
  879. int decl_argc = decl->args.size();
  880. if(args.size() < decl_argc) {
  881. vm->TypeError(_S(co->name, "() takes ", decl_argc, " positional arguments but ", args.size(), " were given"));
  882. }
  883. int i = 0;
  884. // prepare args
  885. std::memset(buffer, 0, co->nlocals * sizeof(PyVar));
  886. for(int index: decl->args)
  887. buffer[index] = args[i++];
  888. // prepare kwdefaults
  889. for(auto& kv: decl->kwargs)
  890. buffer[kv.index] = kv.value;
  891. // handle *args
  892. if(decl->starred_arg != -1) {
  893. ArgsView vargs(args.begin() + i, args.end());
  894. buffer[decl->starred_arg] = VAR(vargs.to_tuple());
  895. i += vargs.size();
  896. } else {
  897. // kwdefaults override
  898. for(auto& kv: decl->kwargs) {
  899. if(i >= args.size()) break;
  900. buffer[kv.index] = args[i++];
  901. }
  902. if(i < args.size()) TypeError(_S("too many arguments", " (", decl->code->name, ')'));
  903. }
  904. PyVar vkwargs;
  905. if(decl->starred_kwarg != -1) {
  906. vkwargs = VAR(Dict());
  907. buffer[decl->starred_kwarg] = vkwargs;
  908. } else {
  909. vkwargs = nullptr;
  910. }
  911. for(int j = 0; j < kwargs.size(); j += 2) {
  912. StrName key(_CAST(uint16_t, kwargs[j]));
  913. int index = decl->kw_to_index.get(key, -1);
  914. // if key is an explicit key, set as local variable
  915. if(index >= 0) {
  916. buffer[index] = kwargs[j + 1];
  917. } else {
  918. // otherwise, set as **kwargs if possible
  919. if(vkwargs == nullptr) {
  920. TypeError(_S(key.escape(), " is an invalid keyword argument for ", co->name, "()"));
  921. } else {
  922. Dict& dict = _CAST(Dict&, vkwargs);
  923. dict.set(this, VAR(key.sv()), kwargs[j + 1]);
  924. }
  925. }
  926. }
  927. }
  928. PyVar VM::vectorcall(int ARGC, int KWARGC, bool op_call) {
  929. PyVar* p1 = s_data._sp - KWARGC * 2;
  930. PyVar* p0 = p1 - ARGC - 2;
  931. // [callable, <self>, args..., kwargs...]
  932. // ^p0 ^p1 ^_sp
  933. PyVar callable = p1[-ARGC - 2];
  934. Type callable_t = _tp(callable);
  935. // handle boundmethod, do a patch
  936. if(callable_t == tp_bound_method) {
  937. assert(p0[1] == PY_NULL);
  938. BoundMethod& bm = PK_OBJ_GET(BoundMethod, callable);
  939. callable = bm.func; // get unbound method
  940. callable_t = _tp(callable);
  941. p1[-(ARGC + 2)] = bm.func;
  942. p1[-(ARGC + 1)] = bm.self;
  943. // [unbound, self, args..., kwargs...]
  944. }
  945. ArgsView args(p0[1] == PY_NULL ? (p0 + 2) : (p0 + 1), p1);
  946. ArgsView kwargs(p1, s_data._sp);
  947. PyVar* _base = args.begin();
  948. if(callable_t == tp_function) {
  949. /*****************_py_call*****************/
  950. // check stack overflow
  951. if(s_data.is_overflow()) StackOverflowError();
  952. const Function& fn = PK_OBJ_GET(Function, callable);
  953. const CodeObject* co = fn.decl->code.get();
  954. switch(fn.decl->type) {
  955. case FuncType::NORMAL:
  956. __prepare_py_call(__vectorcall_buffer, args, kwargs, fn.decl);
  957. // copy buffer back to stack
  958. s_data.reset(_base + co->nlocals);
  959. for(int j = 0; j < co->nlocals; j++)
  960. _base[j] = __vectorcall_buffer[j];
  961. break;
  962. case FuncType::SIMPLE:
  963. if(args.size() != fn.decl->args.size())
  964. TypeError(_S(co->name,
  965. "() takes ",
  966. fn.decl->args.size(),
  967. " positional arguments but ",
  968. args.size(),
  969. " were given"));
  970. if(!kwargs.empty()) TypeError(_S(co->name, "() takes no keyword arguments"));
  971. // [callable, <self>, args..., local_vars...]
  972. // ^p0 ^p1 ^_sp
  973. s_data.reset(_base + co->nlocals);
  974. // initialize local variables to PY_NULL
  975. std::memset(p1, 0, (char*)s_data._sp - (char*)p1);
  976. break;
  977. case FuncType::EMPTY:
  978. if(args.size() != fn.decl->args.size())
  979. TypeError(_S(co->name,
  980. "() takes ",
  981. fn.decl->args.size(),
  982. " positional arguments but ",
  983. args.size(),
  984. " were given"));
  985. if(!kwargs.empty()) TypeError(_S(co->name, "() takes no keyword arguments"));
  986. s_data.reset(p0);
  987. return None;
  988. case FuncType::GENERATOR:
  989. __prepare_py_call(__vectorcall_buffer, args, kwargs, fn.decl);
  990. s_data.reset(p0);
  991. callstack.emplace(nullptr, co, fn._module, callable.get(), nullptr);
  992. return __py_generator(callstack.popx(),
  993. ArgsView(__vectorcall_buffer, __vectorcall_buffer + co->nlocals));
  994. default: PK_UNREACHABLE()
  995. };
  996. // simple or normal
  997. callstack.emplace(p0, co, fn._module, callable.get(), args.begin());
  998. if(op_call) return PY_OP_CALL;
  999. return __run_top_frame();
  1000. /*****************_py_call*****************/
  1001. }
  1002. if(callable_t == tp_native_func) {
  1003. const auto& f = PK_OBJ_GET(NativeFunc, callable);
  1004. PyVar ret;
  1005. if(f.decl != nullptr) {
  1006. int co_nlocals = f.decl->code->nlocals;
  1007. __prepare_py_call(__vectorcall_buffer, args, kwargs, f.decl);
  1008. // copy buffer back to stack
  1009. s_data.reset(_base + co_nlocals);
  1010. for(int j = 0; j < co_nlocals; j++)
  1011. _base[j] = __vectorcall_buffer[j];
  1012. ret = f.call(vm, ArgsView(s_data._sp - co_nlocals, s_data._sp));
  1013. } else {
  1014. if(f.argc != -1) {
  1015. if(KWARGC != 0)
  1016. TypeError(
  1017. "old-style native_func does not accept keyword arguments. If you want to skip this check, specify `argc` to -1");
  1018. if(args.size() != f.argc) { vm->TypeError(_S("expected ", f.argc, " arguments, got ", args.size())); }
  1019. }
  1020. ret = f.call(this, args);
  1021. }
  1022. s_data.reset(p0);
  1023. return ret;
  1024. }
  1025. if(callable_t == tp_type) {
  1026. // [type, NULL, args..., kwargs...]
  1027. PyVar new_f = *find_name_in_mro(PK_OBJ_GET(Type, callable), __new__);
  1028. PyVar obj;
  1029. assert(new_f != nullptr && p0[1] == PY_NULL);
  1030. if(new_f == __cached_object_new) {
  1031. // fast path for object.__new__
  1032. obj = vm->new_object<DummyInstance>(PK_OBJ_GET(Type, callable));
  1033. } else {
  1034. PUSH(new_f);
  1035. PUSH(PY_NULL);
  1036. PUSH(callable); // cls
  1037. for(PyVar o: args)
  1038. PUSH(o);
  1039. for(PyVar o: kwargs)
  1040. PUSH(o);
  1041. // if obj is not an instance of `cls`, the behavior is undefined
  1042. obj = vectorcall(ARGC + 1, KWARGC);
  1043. }
  1044. // __init__
  1045. PyVar self;
  1046. callable = get_unbound_method(obj, __init__, &self, false);
  1047. if(callable != nullptr) {
  1048. callable_t = _tp(callable);
  1049. // replace `NULL` with `self`
  1050. p1[-(ARGC + 2)] = callable;
  1051. p1[-(ARGC + 1)] = self;
  1052. // [init_f, self, args..., kwargs...]
  1053. vectorcall(ARGC, KWARGC);
  1054. // We just discard the return value of `__init__`
  1055. // in cpython it raises a TypeError if the return value is not None
  1056. } else {
  1057. // manually reset the stack
  1058. s_data.reset(p0);
  1059. }
  1060. return obj;
  1061. }
  1062. // handle `__call__` overload
  1063. PyVar self;
  1064. PyVar call_f = get_unbound_method(callable, __call__, &self, false);
  1065. if(self != PY_NULL) {
  1066. p1[-(ARGC + 2)] = call_f;
  1067. p1[-(ARGC + 1)] = self;
  1068. // [call_f, self, args..., kwargs...]
  1069. return vectorcall(ARGC, KWARGC, op_call);
  1070. }
  1071. TypeError(_type_name(vm, callable_t).escape() + " object is not callable");
  1072. }
  1073. void VM::delattr(PyVar _0, StrName _name) {
  1074. const PyTypeInfo* ti = _tp_info(_0);
  1075. if(ti->m__delattr__ && ti->m__delattr__(this, _0, _name)) return;
  1076. if(is_tagged(_0) || !_0->is_attr_valid()) TypeError("cannot delete attribute");
  1077. if(!_0->attr().del(_name)) AttributeError(_0, _name);
  1078. }
  1079. // https://docs.python.org/3/howto/descriptor.html#invocation-from-an-instance
  1080. PyVar VM::getattr(PyVar obj, StrName name, bool throw_err) {
  1081. Type objtype(0);
  1082. // handle super() proxy
  1083. if(is_type(obj, tp_super)) {
  1084. const Super& super = PK_OBJ_GET(Super, obj);
  1085. obj = super.first;
  1086. objtype = super.second;
  1087. } else {
  1088. objtype = _tp(obj);
  1089. }
  1090. PyVar* cls_var = find_name_in_mro(objtype, name);
  1091. if(cls_var != nullptr) {
  1092. // handle descriptor
  1093. if(is_type(*cls_var, tp_property)) {
  1094. const Property& prop = PK_OBJ_GET(Property, *cls_var);
  1095. return call(prop.getter, obj);
  1096. }
  1097. }
  1098. // handle instance __dict__
  1099. if(!is_tagged(obj) && obj->is_attr_valid()) {
  1100. PyVar* val;
  1101. if(obj.type == tp_type) {
  1102. val = find_name_in_mro(PK_OBJ_GET(Type, obj), name);
  1103. if(val != nullptr) {
  1104. if(is_tagged(*val)) return *val;
  1105. if(val->type == tp_staticmethod) return PK_OBJ_GET(StaticMethod, *val).func;
  1106. if(val->type == tp_classmethod) return VAR(BoundMethod(obj, PK_OBJ_GET(ClassMethod, *val).func));
  1107. return *val;
  1108. }
  1109. } else {
  1110. val = obj->attr().try_get_2_likely_found(name);
  1111. if(val != nullptr) return *val;
  1112. }
  1113. }
  1114. if(cls_var != nullptr) {
  1115. // bound method is non-data descriptor
  1116. if(!is_tagged(*cls_var)) {
  1117. switch(cls_var->type.index) {
  1118. case tp_function.index: return VAR(BoundMethod(obj, *cls_var));
  1119. case tp_native_func.index: return VAR(BoundMethod(obj, *cls_var));
  1120. case tp_staticmethod.index: return PK_OBJ_GET(StaticMethod, *cls_var).func;
  1121. case tp_classmethod.index: return VAR(BoundMethod(_t(objtype), PK_OBJ_GET(ClassMethod, *cls_var).func));
  1122. }
  1123. }
  1124. return *cls_var;
  1125. }
  1126. const PyTypeInfo* ti = &_all_types[objtype];
  1127. if(ti->m__getattr__) {
  1128. PyVar ret = ti->m__getattr__(this, obj, name);
  1129. if(ret) return ret;
  1130. }
  1131. if(throw_err) AttributeError(obj, name);
  1132. return nullptr;
  1133. }
  1134. // used by OP_LOAD_METHOD
  1135. // try to load a unbound method (fallback to `getattr` if not found)
  1136. PyVar VM::get_unbound_method(PyVar obj, StrName name, PyVar* self, bool throw_err, bool fallback) {
  1137. self->set_null();
  1138. Type objtype(0);
  1139. // handle super() proxy
  1140. if(is_type(obj, tp_super)) {
  1141. const Super& super = PK_OBJ_GET(Super, obj);
  1142. obj = super.first;
  1143. objtype = super.second;
  1144. } else {
  1145. objtype = _tp(obj);
  1146. }
  1147. PyVar* cls_var = find_name_in_mro(objtype, name);
  1148. if(fallback) {
  1149. if(cls_var != nullptr) {
  1150. // handle descriptor
  1151. if(is_type(*cls_var, tp_property)) {
  1152. const Property& prop = PK_OBJ_GET(Property, *cls_var);
  1153. return call(prop.getter, obj);
  1154. }
  1155. }
  1156. // handle instance __dict__
  1157. if(!is_tagged(obj) && obj->is_attr_valid()) {
  1158. PyVar* val;
  1159. if(obj.type == tp_type) {
  1160. val = find_name_in_mro(PK_OBJ_GET(Type, obj), name);
  1161. if(val != nullptr) {
  1162. if(is_tagged(*val)) return *val;
  1163. if(val->type == tp_staticmethod) return PK_OBJ_GET(StaticMethod, *val).func;
  1164. if(val->type == tp_classmethod) return VAR(BoundMethod(obj, PK_OBJ_GET(ClassMethod, *val).func));
  1165. return *val;
  1166. }
  1167. } else {
  1168. val = obj->attr().try_get_2_likely_found(name);
  1169. if(val != nullptr) return *val;
  1170. }
  1171. }
  1172. }
  1173. if(cls_var != nullptr) {
  1174. if(!is_tagged(*cls_var)) {
  1175. switch(cls_var->type.index) {
  1176. case tp_function.index: *self = obj; break;
  1177. case tp_native_func.index: *self = obj; break;
  1178. case tp_staticmethod.index: self->set_null(); return PK_OBJ_GET(StaticMethod, *cls_var).func;
  1179. case tp_classmethod.index: *self = _t(objtype); return PK_OBJ_GET(ClassMethod, *cls_var).func;
  1180. }
  1181. }
  1182. return *cls_var;
  1183. }
  1184. const PyTypeInfo* ti = &_all_types[objtype];
  1185. if(fallback && ti->m__getattr__) {
  1186. PyVar ret = ti->m__getattr__(this, obj, name);
  1187. if(ret) return ret;
  1188. }
  1189. if(throw_err) AttributeError(obj, name);
  1190. return nullptr;
  1191. }
  1192. void VM::setattr(PyVar obj, StrName name, PyVar value) {
  1193. Type objtype(0);
  1194. // handle super() proxy
  1195. if(is_type(obj, tp_super)) {
  1196. Super& super = PK_OBJ_GET(Super, obj);
  1197. obj = super.first;
  1198. objtype = super.second;
  1199. } else {
  1200. objtype = _tp(obj);
  1201. }
  1202. PyVar* cls_var = find_name_in_mro(objtype, name);
  1203. if(cls_var != nullptr) {
  1204. // handle descriptor
  1205. if(is_type(*cls_var, tp_property)) {
  1206. const Property& prop = _CAST(Property&, *cls_var);
  1207. if(prop.setter != vm->None) {
  1208. call(prop.setter, obj, value);
  1209. } else {
  1210. TypeError(_S("readonly attribute: ", name.escape()));
  1211. }
  1212. return;
  1213. }
  1214. }
  1215. const PyTypeInfo* ti = &_all_types[objtype];
  1216. if(ti->m__setattr__) {
  1217. ti->m__setattr__(this, obj, name, value);
  1218. return;
  1219. }
  1220. // handle instance __dict__
  1221. if(is_tagged(obj) || !obj->is_attr_valid()) TypeError("cannot set attribute");
  1222. obj->attr().set(name, value);
  1223. }
  1224. PyObject* VM::bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, any userdata, BindType bt) {
  1225. PyObject* nf = heap.gcnew<NativeFunc>(tp_native_func, fn, argc, std::move(userdata));
  1226. switch(bt) {
  1227. case BindType::DEFAULT: break;
  1228. case BindType::STATICMETHOD: nf = heap.gcnew<StaticMethod>(tp_staticmethod, nf); break;
  1229. case BindType::CLASSMETHOD: nf = heap.gcnew<ClassMethod>(tp_classmethod, nf); break;
  1230. }
  1231. if(obj != nullptr) obj->attr().set(name, nf);
  1232. return nf;
  1233. }
  1234. PyObject* VM::bind(PyObject* obj, const char* sig, NativeFuncC fn, any userdata, BindType bt) {
  1235. return bind(obj, sig, nullptr, fn, std::move(userdata), bt);
  1236. }
  1237. PyObject* VM::bind(PyObject* obj, const char* sig, const char* docstring, NativeFuncC fn, any userdata, BindType bt) {
  1238. char buffer[256];
  1239. int length = snprintf(buffer, sizeof(buffer), "def %s : pass", sig);
  1240. std::string_view source(buffer, length);
  1241. // fn(a, b, *c, d=1) -> None
  1242. CodeObject_ co = compile(source, "<bind>", EXEC_MODE);
  1243. assert(co->func_decls.size() == 1);
  1244. FuncDecl_ decl = co->func_decls[0];
  1245. decl->docstring = docstring;
  1246. PyObject* f_obj = heap.gcnew<NativeFunc>(tp_native_func, fn, decl, std::move(userdata));
  1247. switch(bt) {
  1248. case BindType::STATICMETHOD: f_obj = heap.gcnew<StaticMethod>(tp_staticmethod, f_obj); break;
  1249. case BindType::CLASSMETHOD: f_obj = heap.gcnew<ClassMethod>(tp_classmethod, f_obj); break;
  1250. case BindType::DEFAULT: break;
  1251. }
  1252. if(obj != nullptr) obj->attr().set(decl->code->name, f_obj);
  1253. return f_obj;
  1254. }
  1255. PyObject* VM::bind_property(PyObject* obj, const char* name, NativeFuncC fget, NativeFuncC fset) {
  1256. assert(is_type(obj, tp_type));
  1257. std::string_view name_sv(name);
  1258. int pos = name_sv.find(':');
  1259. if(pos > 0) name_sv = name_sv.substr(0, pos);
  1260. PyVar _0 = new_object<NativeFunc>(tp_native_func, fget, 1);
  1261. PyVar _1 = vm->None;
  1262. if(fset != nullptr) _1 = new_object<NativeFunc>(tp_native_func, fset, 2);
  1263. PyObject* prop = heap.gcnew<Property>(tp_property, _0, _1);
  1264. obj->attr().set(StrName(name_sv), prop);
  1265. return prop;
  1266. }
  1267. void VM::__builtin_error(StrName type) { _error(call(builtins->attr(type))); }
  1268. void VM::__builtin_error(StrName type, PyVar arg) { _error(call(builtins->attr(type), arg)); }
  1269. void VM::__builtin_error(StrName type, const Str& msg) { __builtin_error(type, VAR(msg)); }
  1270. void VM::BinaryOptError(const char* op, PyVar _0, PyVar _1) {
  1271. StrName name_0 = _type_name(vm, _tp(_0));
  1272. StrName name_1 = _type_name(vm, _tp(_1));
  1273. TypeError(_S("unsupported operand type(s) for ", op, ": ", name_0.escape(), " and ", name_1.escape()));
  1274. }
  1275. void VM::AttributeError(PyVar obj, StrName name) {
  1276. if(isinstance(obj, vm->tp_type)) {
  1277. __builtin_error(
  1278. "AttributeError",
  1279. _S("type object ", _type_name(vm, PK_OBJ_GET(Type, obj)).escape(), " has no attribute ", name.escape()));
  1280. } else {
  1281. __builtin_error("AttributeError",
  1282. _S(_type_name(vm, _tp(obj)).escape(), " object has no attribute ", name.escape()));
  1283. }
  1284. }
  1285. void VM::_error(PyVar e_obj) {
  1286. assert(isinstance(e_obj, tp_exception));
  1287. Exception& e = PK_OBJ_GET(Exception, e_obj);
  1288. if(callstack.empty()) {
  1289. e.is_re = false;
  1290. __last_exception = e_obj.get();
  1291. throw TopLevelException(this, &e);
  1292. }
  1293. PUSH(e_obj);
  1294. __raise_exc();
  1295. }
  1296. void VM::__raise_exc(bool re_raise) {
  1297. Frame* frame = &callstack.top();
  1298. Exception& e = PK_OBJ_GET(Exception, s_data.top());
  1299. if(!re_raise) {
  1300. e._ip_on_error = frame->ip();
  1301. e._code_on_error = (void*)frame->co;
  1302. }
  1303. int next_ip = frame->prepare_jump_exception_handler(&s_data);
  1304. int actual_ip = frame->ip();
  1305. if(e._ip_on_error >= 0 && e._code_on_error == (void*)frame->co) actual_ip = e._ip_on_error;
  1306. int current_line = frame->co->lines[actual_ip].lineno; // current line
  1307. auto current_f_name = frame->co->name.sv(); // current function name
  1308. if(frame->_callable == nullptr) current_f_name = ""; // not in a function
  1309. e.st_push(frame->co->src, current_line, nullptr, current_f_name);
  1310. if(next_ip >= 0) {
  1311. throw InternalException(InternalExceptionType::Handled, next_ip);
  1312. } else {
  1313. throw InternalException(InternalExceptionType::Unhandled);
  1314. }
  1315. }
  1316. StrName _type_name(VM* vm, Type type) { return vm->_all_types[type].name; }
  1317. void VM::bind__getitem__(Type type, PyVar (*f)(VM*, PyVar, PyVar)) {
  1318. _all_types[type].m__getitem__ = f;
  1319. bind_func(
  1320. type,
  1321. __getitem__,
  1322. 2,
  1323. [](VM* vm, ArgsView args) {
  1324. return lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0], args[1]);
  1325. },
  1326. f);
  1327. }
  1328. void VM::bind__setitem__(Type type, void (*f)(VM*, PyVar, PyVar, PyVar)) {
  1329. _all_types[type].m__setitem__ = f;
  1330. bind_func(
  1331. type,
  1332. __setitem__,
  1333. 3,
  1334. [](VM* vm, ArgsView args) {
  1335. lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0], args[1], args[2]);
  1336. return vm->None;
  1337. },
  1338. f);
  1339. }
  1340. void VM::bind__delitem__(Type type, void (*f)(VM*, PyVar, PyVar)) {
  1341. _all_types[type].m__delitem__ = f;
  1342. bind_func(
  1343. type,
  1344. __delitem__,
  1345. 2,
  1346. [](VM* vm, ArgsView args) {
  1347. lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0], args[1]);
  1348. return vm->None;
  1349. },
  1350. f);
  1351. }
  1352. PyVar VM::__pack_next_retval(unsigned n) {
  1353. if(n == 0) return StopIteration;
  1354. if(n == 1) return s_data.popx();
  1355. PyVar retval = VAR(s_data.view(n).to_tuple());
  1356. s_data._sp -= n;
  1357. return retval;
  1358. }
  1359. void VM::bind__next__(Type type, unsigned (*f)(VM*, PyVar)) {
  1360. _all_types[type].op__next__ = f;
  1361. bind_func(
  1362. type,
  1363. __next__,
  1364. 1,
  1365. [](VM* vm, ArgsView args) {
  1366. int n = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1367. return vm->__pack_next_retval(n);
  1368. },
  1369. f);
  1370. }
  1371. void VM::bind__next__(Type type, PyVar (*f)(VM*, PyVar)) {
  1372. bind_func(
  1373. type,
  1374. __next__,
  1375. 1,
  1376. [](VM* vm, ArgsView args) {
  1377. auto f = lambda_get_userdata<PyVar (*)(VM*, PyVar)>(args.begin());
  1378. return f(vm, args[0]);
  1379. },
  1380. f);
  1381. }
  1382. #define BIND_UNARY_SPECIAL(name) \
  1383. void VM::bind##name(Type type, PyVar (*f)(VM*, PyVar)) { \
  1384. _all_types[type].m##name = f; \
  1385. bind_func( \
  1386. type, \
  1387. name, \
  1388. 1, \
  1389. [](VM* vm, ArgsView args) { \
  1390. return lambda_get_userdata<PyVar (*)(VM*, PyVar)>(args.begin())(vm, args[0]); \
  1391. }, \
  1392. f); \
  1393. }
  1394. BIND_UNARY_SPECIAL(__iter__)
  1395. BIND_UNARY_SPECIAL(__neg__)
  1396. BIND_UNARY_SPECIAL(__invert__)
  1397. #undef BIND_UNARY_SPECIAL
  1398. void VM::bind__str__(Type type, Str (*f)(VM*, PyVar)) {
  1399. _all_types[type].m__str__ = f;
  1400. bind_func(
  1401. type,
  1402. __str__,
  1403. 1,
  1404. [](VM* vm, ArgsView args) {
  1405. Str s = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1406. return VAR(s);
  1407. },
  1408. f);
  1409. }
  1410. void VM::bind__repr__(Type type, Str (*f)(VM*, PyVar)) {
  1411. _all_types[type].m__repr__ = f;
  1412. bind_func(
  1413. type,
  1414. __repr__,
  1415. 1,
  1416. [](VM* vm, ArgsView args) {
  1417. Str s = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1418. return VAR(s);
  1419. },
  1420. f);
  1421. }
  1422. void VM::bind__hash__(Type type, i64 (*f)(VM*, PyVar)) {
  1423. _all_types[type].m__hash__ = f;
  1424. bind_func(
  1425. type,
  1426. __hash__,
  1427. 1,
  1428. [](VM* vm, ArgsView args) {
  1429. i64 ret = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1430. return VAR(ret);
  1431. },
  1432. f);
  1433. }
  1434. void VM::bind__len__(Type type, i64 (*f)(VM*, PyVar)) {
  1435. _all_types[type].m__len__ = f;
  1436. bind_func(
  1437. type,
  1438. __len__,
  1439. 1,
  1440. [](VM* vm, ArgsView args) {
  1441. i64 ret = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1442. return VAR(ret);
  1443. },
  1444. f);
  1445. }
  1446. #define BIND_BINARY_SPECIAL(name) \
  1447. void VM::bind##name(Type type, BinaryFuncC f) { \
  1448. _all_types[type].m##name = f; \
  1449. bind_func( \
  1450. type, \
  1451. name, \
  1452. 2, \
  1453. [](VM* vm, ArgsView args) { \
  1454. return lambda_get_userdata<BinaryFuncC>(args.begin())(vm, args[0], args[1]); \
  1455. }, \
  1456. f); \
  1457. }
  1458. BIND_BINARY_SPECIAL(__eq__)
  1459. BIND_BINARY_SPECIAL(__lt__)
  1460. BIND_BINARY_SPECIAL(__le__)
  1461. BIND_BINARY_SPECIAL(__gt__)
  1462. BIND_BINARY_SPECIAL(__ge__)
  1463. BIND_BINARY_SPECIAL(__contains__)
  1464. BIND_BINARY_SPECIAL(__add__)
  1465. BIND_BINARY_SPECIAL(__sub__)
  1466. BIND_BINARY_SPECIAL(__mul__)
  1467. BIND_BINARY_SPECIAL(__truediv__)
  1468. BIND_BINARY_SPECIAL(__floordiv__)
  1469. BIND_BINARY_SPECIAL(__mod__)
  1470. BIND_BINARY_SPECIAL(__pow__)
  1471. BIND_BINARY_SPECIAL(__matmul__)
  1472. BIND_BINARY_SPECIAL(__lshift__)
  1473. BIND_BINARY_SPECIAL(__rshift__)
  1474. BIND_BINARY_SPECIAL(__and__)
  1475. BIND_BINARY_SPECIAL(__or__)
  1476. BIND_BINARY_SPECIAL(__xor__)
  1477. #undef BIND_BINARY_SPECIAL
  1478. #if PK_ENABLE_PROFILER
  1479. void NextBreakpoint::_step(VM* vm) {
  1480. int curr_callstack_size = vm->callstack.size();
  1481. int curr_lineno = vm->callstack.top().curr_lineno();
  1482. if(should_step_into) {
  1483. if(curr_callstack_size != callstack_size || curr_lineno != lineno) { vm->__breakpoint(); }
  1484. } else {
  1485. if(curr_callstack_size == callstack_size) {
  1486. if(curr_lineno != lineno) vm->__breakpoint();
  1487. } else if(curr_callstack_size < callstack_size) {
  1488. // returning
  1489. vm->__breakpoint();
  1490. }
  1491. }
  1492. }
  1493. #endif
  1494. void VM::__pop_frame() {
  1495. s_data.reset(callstack.top()._sp_base);
  1496. callstack.pop();
  1497. #if PK_ENABLE_PROFILER
  1498. if(!_next_breakpoint.empty() && callstack.size() < _next_breakpoint.callstack_size) {
  1499. _next_breakpoint = NextBreakpoint();
  1500. }
  1501. #endif
  1502. }
  1503. void VM::__breakpoint() {
  1504. #if PK_ENABLE_PROFILER
  1505. _next_breakpoint = NextBreakpoint();
  1506. bool show_where = false;
  1507. bool show_headers = true;
  1508. while(true) {
  1509. vector<LinkedFrame*> frames;
  1510. LinkedFrame* lf = callstack._tail;
  1511. while(lf != nullptr) {
  1512. frames.push_back(lf);
  1513. lf = lf->f_back;
  1514. if(frames.size() >= 4) break;
  1515. }
  1516. if(show_headers) {
  1517. for(int i = frames.size() - 1; i >= 0; i--) {
  1518. if(!show_where && i != 0) continue;
  1519. SStream ss;
  1520. Frame* frame = &frames[i]->frame;
  1521. int lineno = frame->curr_lineno();
  1522. ss << "File \"" << frame->co->src.filename() << "\", line " << lineno;
  1523. if(frame->_callable) {
  1524. ss << ", in ";
  1525. ss << frame->_callable->as<Function>().decl->code->name;
  1526. }
  1527. ss << '\n';
  1528. ss << "-> " << frame->co->src->get_line(lineno) << '\n';
  1529. stdout_write(ss.str());
  1530. }
  1531. show_headers = false;
  1532. }
  1533. vm->stdout_write("(Pdb) ");
  1534. Frame* frame_0 = &frames[0]->frame;
  1535. std::string line;
  1536. if(!std::getline(std::cin, line)) {
  1537. stdout_write("--KeyboardInterrupt--\n");
  1538. continue;
  1539. }
  1540. if(line == "h" || line == "help") {
  1541. stdout_write("h, help: show this help message\n");
  1542. stdout_write("q, quit: exit the debugger\n");
  1543. stdout_write("n, next: execute next line\n");
  1544. stdout_write("s, step: step into\n");
  1545. stdout_write("w, where: show current stack frame\n");
  1546. stdout_write("c, continue: continue execution\n");
  1547. stdout_write("a, args: show local variables\n");
  1548. stdout_write("p, print <expr>: evaluate expression\n");
  1549. stdout_write("l, list: show lines around current line\n");
  1550. stderr_write("ll, longlist: show all lines\n");
  1551. stdout_write("!: execute statement\n");
  1552. continue;
  1553. }
  1554. if(line == "q" || line == "quit") { vm->RuntimeError("pdb quit"); }
  1555. if(line == "n" || line == "next") {
  1556. vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), frame_0->curr_lineno(), false);
  1557. break;
  1558. }
  1559. if(line == "s" || line == "step") {
  1560. vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), frame_0->curr_lineno(), true);
  1561. break;
  1562. }
  1563. if(line == "w" || line == "where") {
  1564. show_where = !show_where;
  1565. show_headers = true;
  1566. continue;
  1567. }
  1568. if(line == "c" || line == "continue") break;
  1569. if(line == "a" || line == "args") {
  1570. int i = 0;
  1571. for(PyVar obj: frame_0->_locals) {
  1572. if(obj == PY_NULL) continue;
  1573. StrName name = frame_0->co->varnames[i++];
  1574. stdout_write(_S(name.sv(), " = ", vm->py_repr(obj), '\n'));
  1575. }
  1576. continue;
  1577. }
  1578. bool is_list = line == "l" || line == "list";
  1579. bool is_longlist = line == "ll" || line == "longlist";
  1580. if(is_list || is_longlist) {
  1581. if(frame_0->co->src->is_precompiled) continue;
  1582. int lineno = frame_0->curr_lineno();
  1583. int start, end;
  1584. if(is_list) {
  1585. int max_line = frame_0->co->src->line_starts.size() + 1;
  1586. start = (std::max)(1, lineno - 5);
  1587. end = (std::min)(max_line, lineno + 5);
  1588. } else {
  1589. start = frame_0->co->start_line;
  1590. end = frame_0->co->end_line;
  1591. if(start == -1 || end == -1) continue;
  1592. }
  1593. SStream ss;
  1594. int max_width = std::to_string(end).size();
  1595. for(int i = start; i <= end; i++) {
  1596. int spaces = max_width - std::to_string(i).size();
  1597. ss << std::string(spaces, ' ') << std::to_string(i);
  1598. if(i == lineno)
  1599. ss << " -> ";
  1600. else
  1601. ss << " ";
  1602. ss << frame_0->co->src->get_line(i) << '\n';
  1603. }
  1604. stdout_write(ss.str());
  1605. continue;
  1606. }
  1607. int space = line.find_first_of(' ');
  1608. if(space != -1) {
  1609. std::string cmd = line.substr(0, space);
  1610. std::string arg = line.substr(space + 1);
  1611. if(arg.empty()) continue; // ignore empty command
  1612. if(cmd == "p" || cmd == "print") {
  1613. CodeObject_ code = compile(arg, "<stdin>", EVAL_MODE, true);
  1614. PyVar retval = vm->_exec(code.get(), frame_0->_module, frame_0->_callable, frame_0->_locals);
  1615. stdout_write(vm->py_repr(retval));
  1616. stdout_write("\n");
  1617. } else if(cmd == "!") {
  1618. CodeObject_ code = compile(arg, "<stdin>", EXEC_MODE, true);
  1619. vm->_exec(code.get(), frame_0->_module, frame_0->_callable, frame_0->_locals);
  1620. }
  1621. continue;
  1622. }
  1623. }
  1624. #endif
  1625. }
  1626. /**************************************************************************/
  1627. void Function::_gc_mark(VM* vm) const {
  1628. decl->_gc_mark(vm);
  1629. if(_closure) {
  1630. _closure->apply([](StrName _, PyVar obj, void* userdata) {
  1631. VM* vm = (VM*)userdata;
  1632. vm->obj_gc_mark(obj);
  1633. }, vm);
  1634. }
  1635. }
  1636. void NativeFunc::_gc_mark(VM* vm) const {
  1637. if(decl) decl->_gc_mark(vm);
  1638. }
  1639. void FuncDecl::_gc_mark(VM* vm) const {
  1640. code->_gc_mark(vm);
  1641. for(int i = 0; i < kwargs.size(); i++)
  1642. vm->obj_gc_mark(kwargs[i].value);
  1643. }
  1644. void List::_gc_mark(VM* vm) const {
  1645. for(PyVar obj: *this)
  1646. vm->obj_gc_mark(obj);
  1647. }
  1648. void Tuple::_gc_mark(VM* vm) const {
  1649. for(PyVar obj: *this)
  1650. vm->obj_gc_mark(obj);
  1651. }
  1652. void MappingProxy::_gc_mark(VM* vm) const { vm->__obj_gc_mark(obj); }
  1653. void BoundMethod::_gc_mark(VM* vm) const {
  1654. vm->obj_gc_mark(func);
  1655. vm->obj_gc_mark(self);
  1656. }
  1657. void StarWrapper::_gc_mark(VM* vm) const { vm->obj_gc_mark(obj); }
  1658. void StaticMethod::_gc_mark(VM* vm) const { vm->obj_gc_mark(func); }
  1659. void ClassMethod::_gc_mark(VM* vm) const { vm->obj_gc_mark(func); }
  1660. void Property::_gc_mark(VM* vm) const {
  1661. vm->obj_gc_mark(getter);
  1662. vm->obj_gc_mark(setter);
  1663. }
  1664. void Slice::_gc_mark(VM* vm) const {
  1665. vm->obj_gc_mark(start);
  1666. vm->obj_gc_mark(stop);
  1667. vm->obj_gc_mark(step);
  1668. }
  1669. void Super::_gc_mark(VM* vm) const { vm->obj_gc_mark(first); }
  1670. void Frame::_gc_mark(VM* vm) const {
  1671. vm->obj_gc_mark(_module);
  1672. co->_gc_mark(vm);
  1673. // Frame could be stored in a generator, so mark _callable for safety
  1674. vm->obj_gc_mark(_callable);
  1675. }
  1676. void ManagedHeap::mark() {
  1677. for(PyObject* obj: _no_gc)
  1678. vm->__obj_gc_mark(obj);
  1679. vm->callstack.apply([this](Frame& frame) {
  1680. frame._gc_mark(vm);
  1681. });
  1682. for(auto [_, co]: vm->__cached_codes)
  1683. co->_gc_mark(vm);
  1684. vm->obj_gc_mark(vm->__last_exception);
  1685. vm->obj_gc_mark(vm->__curr_class);
  1686. vm->obj_gc_mark(vm->__c.error);
  1687. vm->__stack_gc_mark(vm->s_data.begin(), vm->s_data.end());
  1688. if(_gc_marker_ex) _gc_marker_ex(vm);
  1689. }
  1690. void ManagedHeap::_delete(PyObject* obj) {
  1691. const PyTypeInfo* ti = vm->_tp_info(obj->type);
  1692. if(ti->vt._dtor) ti->vt._dtor(obj->_value_ptr());
  1693. delete obj->_attr; // delete __dict__ if exists
  1694. if(obj->gc_is_large){
  1695. std::free(obj);
  1696. }else{
  1697. PoolObject_dealloc(obj);
  1698. }
  1699. }
  1700. void Dict::_gc_mark(VM* vm) const {
  1701. apply([vm](PyVar k, PyVar v) {
  1702. vm->obj_gc_mark(k);
  1703. vm->obj_gc_mark(v);
  1704. });
  1705. }
  1706. void CodeObject::_gc_mark(VM* vm) const {
  1707. for(PyVar v: consts)
  1708. vm->obj_gc_mark(v);
  1709. for(auto& decl: func_decls)
  1710. decl->_gc_mark(vm);
  1711. }
  1712. } // namespace pkpy