vm.cpp 66 KB

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