vm.cpp 61 KB

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