vm.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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 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. 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. 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. 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. void VM::__unpack_as_list(ArgsView args, List& list){
  856. auto _lock = heap.gc_scope_lock();
  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. void VM::__unpack_as_dict(ArgsView args, Dict& dict){
  875. auto _lock = heap.gc_scope_lock();
  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(TopLevelException){
  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. __last_exception = e_obj.get();
  1315. throw TopLevelException(&e);
  1316. }
  1317. PUSH(e_obj);
  1318. __raise_exc();
  1319. }
  1320. void VM::__raise_exc(bool re_raise){
  1321. Frame* frame = &callstack.top();
  1322. Exception& e = PK_OBJ_GET(Exception, s_data.top());
  1323. if(!re_raise){
  1324. e._ip_on_error = frame->ip();
  1325. e._code_on_error = (void*)frame->co;
  1326. }
  1327. int next_ip = frame->prepare_jump_exception_handler(&s_data);
  1328. int actual_ip = frame->ip();
  1329. if(e._ip_on_error >= 0 && e._code_on_error == (void*)frame->co) actual_ip = e._ip_on_error;
  1330. int current_line = frame->co->lines[actual_ip].lineno; // current line
  1331. auto current_f_name = frame->co->name.sv(); // current function name
  1332. if(frame->_callable == nullptr) current_f_name = ""; // not in a function
  1333. e.st_push(frame->co->src, current_line, nullptr, current_f_name);
  1334. if(next_ip >= 0){
  1335. throw InternalException(InternalExceptionType::Handled, next_ip);
  1336. }else{
  1337. throw InternalException(InternalExceptionType::Unhandled);
  1338. }
  1339. }
  1340. StrName _type_name(VM *vm, Type type){
  1341. return vm->_all_types[type].name;
  1342. }
  1343. void VM::bind__getitem__(Type type, PyVar (*f)(VM*, PyVar, PyVar)){
  1344. _all_types[type].m__getitem__ = f;
  1345. bind_func(type, __getitem__, 2, [](VM* vm, ArgsView args){
  1346. return lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0], args[1]);
  1347. }, f);
  1348. }
  1349. void VM::bind__setitem__(Type type, void (*f)(VM*, PyVar, PyVar, PyVar)){
  1350. _all_types[type].m__setitem__ = f;
  1351. bind_func(type, __setitem__, 3, [](VM* vm, ArgsView args){
  1352. lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0], args[1], args[2]);
  1353. return vm->None;
  1354. }, f);
  1355. }
  1356. void VM::bind__delitem__(Type type, void (*f)(VM*, PyVar, PyVar)){
  1357. _all_types[type].m__delitem__ = f;
  1358. bind_func(type, __delitem__, 2, [](VM* vm, ArgsView args){
  1359. lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0], args[1]);
  1360. return vm->None;
  1361. }, f);
  1362. }
  1363. PyVar VM::__pack_next_retval(unsigned n){
  1364. if(n == 0) return StopIteration;
  1365. if(n == 1) return s_data.popx();
  1366. PyVar retval = VAR(s_data.view(n).to_tuple());
  1367. s_data._sp -= n;
  1368. return retval;
  1369. }
  1370. void VM::bind__next__(Type type, unsigned (*f)(VM*, PyVar)){
  1371. _all_types[type].op__next__ = f;
  1372. bind_func(type, __next__, 1, [](VM* vm, ArgsView args){
  1373. int n = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1374. return vm->__pack_next_retval(n);
  1375. }, f);
  1376. }
  1377. void VM::bind__next__(Type type, PyVar (*f)(VM*, PyVar)){
  1378. bind_func(type, __next__, 1, [](VM* vm, ArgsView args){
  1379. auto f = lambda_get_userdata<PyVar(*)(VM*, PyVar)>(args.begin());
  1380. return f(vm, args[0]);
  1381. }, f);
  1382. }
  1383. #define BIND_UNARY_SPECIAL(name) \
  1384. void VM::bind##name(Type type, PyVar (*f)(VM*, PyVar)){ \
  1385. _all_types[type].m##name = f; \
  1386. bind_func(type, name, 1, [](VM* vm, ArgsView args){ \
  1387. return lambda_get_userdata<PyVar(*)(VM*, PyVar)>(args.begin())(vm, args[0]);\
  1388. }, f); \
  1389. }
  1390. BIND_UNARY_SPECIAL(__iter__)
  1391. BIND_UNARY_SPECIAL(__neg__)
  1392. BIND_UNARY_SPECIAL(__invert__)
  1393. #undef BIND_UNARY_SPECIAL
  1394. void VM::bind__str__(Type type, Str (*f)(VM*, PyVar)){
  1395. _all_types[type].m__str__ = f;
  1396. bind_func(type, __str__, 1, [](VM* vm, ArgsView args){
  1397. Str s = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1398. return VAR(s);
  1399. }, f);
  1400. }
  1401. void VM::bind__repr__(Type type, Str (*f)(VM*, PyVar)){
  1402. _all_types[type].m__repr__ = f;
  1403. bind_func(type, __repr__, 1, [](VM* vm, ArgsView args){
  1404. Str s = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1405. return VAR(s);
  1406. }, f);
  1407. }
  1408. void VM::bind__hash__(Type type, i64 (*f)(VM*, PyVar)){
  1409. _all_types[type].m__hash__ = f;
  1410. bind_func(type, __hash__, 1, [](VM* vm, ArgsView args){
  1411. i64 ret = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1412. return VAR(ret);
  1413. }, f);
  1414. }
  1415. void VM::bind__len__(Type type, i64 (*f)(VM*, PyVar)){
  1416. _all_types[type].m__len__ = f;
  1417. bind_func(type, __len__, 1, [](VM* vm, ArgsView args){
  1418. i64 ret = lambda_get_userdata<decltype(f)>(args.begin())(vm, args[0]);
  1419. return VAR(ret);
  1420. }, f);
  1421. }
  1422. #define BIND_BINARY_SPECIAL(name) \
  1423. void VM::bind##name(Type type, BinaryFuncC f){ \
  1424. _all_types[type].m##name = f; \
  1425. bind_func(type, name, 2, [](VM* vm, ArgsView args){ \
  1426. return lambda_get_userdata<BinaryFuncC>(args.begin())(vm, args[0], args[1]);\
  1427. }, f); \
  1428. }
  1429. BIND_BINARY_SPECIAL(__eq__)
  1430. BIND_BINARY_SPECIAL(__lt__)
  1431. BIND_BINARY_SPECIAL(__le__)
  1432. BIND_BINARY_SPECIAL(__gt__)
  1433. BIND_BINARY_SPECIAL(__ge__)
  1434. BIND_BINARY_SPECIAL(__contains__)
  1435. BIND_BINARY_SPECIAL(__add__)
  1436. BIND_BINARY_SPECIAL(__sub__)
  1437. BIND_BINARY_SPECIAL(__mul__)
  1438. BIND_BINARY_SPECIAL(__truediv__)
  1439. BIND_BINARY_SPECIAL(__floordiv__)
  1440. BIND_BINARY_SPECIAL(__mod__)
  1441. BIND_BINARY_SPECIAL(__pow__)
  1442. BIND_BINARY_SPECIAL(__matmul__)
  1443. BIND_BINARY_SPECIAL(__lshift__)
  1444. BIND_BINARY_SPECIAL(__rshift__)
  1445. BIND_BINARY_SPECIAL(__and__)
  1446. BIND_BINARY_SPECIAL(__or__)
  1447. BIND_BINARY_SPECIAL(__xor__)
  1448. #undef BIND_BINARY_SPECIAL
  1449. void Dict::_probe_0(VM* vm, PyVar key, bool &ok, int &i) const{
  1450. ok = false;
  1451. i64 hash = vm->py_hash(key);
  1452. i = hash & _mask;
  1453. for(int j=0; j<_capacity; j++) {
  1454. if(_items[i].first != nullptr){
  1455. if(vm->py_eq(_items[i].first, key)) { ok = true; break; }
  1456. }else{
  1457. if(_items[i].second == nullptr) break;
  1458. }
  1459. // https://github.com/python/cpython/blob/3.8/Objects/dictobject.c#L166
  1460. i = ((5*i) + 1) & _mask;
  1461. }
  1462. }
  1463. void Dict::_probe_1(VM* vm, PyVar key, bool &ok, int &i) const{
  1464. ok = false;
  1465. i = vm->py_hash(key) & _mask;
  1466. while(_items[i].first != nullptr) {
  1467. if(vm->py_eq(_items[i].first, key)) { ok = true; break; }
  1468. // https://github.com/python/cpython/blob/3.8/Objects/dictobject.c#L166
  1469. i = ((5*i) + 1) & _mask;
  1470. }
  1471. }
  1472. #if PK_ENABLE_PROFILER
  1473. void NextBreakpoint::_step(VM* vm){
  1474. int curr_callstack_size = vm->callstack.size();
  1475. int curr_lineno = vm->callstack.top().curr_lineno();
  1476. if(should_step_into){
  1477. if(curr_callstack_size != callstack_size || curr_lineno != lineno){
  1478. vm->__breakpoint();
  1479. }
  1480. }else{
  1481. if(curr_callstack_size == callstack_size) {
  1482. if(curr_lineno != lineno) vm->__breakpoint();
  1483. }else if(curr_callstack_size < callstack_size){
  1484. // returning
  1485. vm->__breakpoint();
  1486. }
  1487. }
  1488. }
  1489. #endif
  1490. void VM::__pop_frame(){
  1491. s_data.reset(callstack.top()._sp_base);
  1492. callstack.pop();
  1493. #if PK_ENABLE_PROFILER
  1494. if(!_next_breakpoint.empty() && callstack.size()<_next_breakpoint.callstack_size){
  1495. _next_breakpoint = NextBreakpoint();
  1496. }
  1497. #endif
  1498. }
  1499. void VM::__breakpoint(){
  1500. #if PK_ENABLE_PROFILER
  1501. _next_breakpoint = NextBreakpoint();
  1502. bool show_where = false;
  1503. bool show_headers = true;
  1504. while(true){
  1505. vector<LinkedFrame*> frames;
  1506. LinkedFrame* lf = callstack._tail;
  1507. while(lf != nullptr){
  1508. frames.push_back(lf);
  1509. lf = lf->f_back;
  1510. if(frames.size() >= 4) break;
  1511. }
  1512. if(show_headers){
  1513. for(int i=frames.size()-1; i>=0; i--){
  1514. if(!show_where && i!=0) continue;
  1515. SStream ss;
  1516. Frame* frame = &frames[i]->frame;
  1517. int lineno = frame->curr_lineno();
  1518. ss << "File \"" << frame->co->src->filename << "\", line " << lineno;
  1519. if(frame->_callable){
  1520. ss << ", in ";
  1521. ss << frame->_callable->as<Function>().decl->code->name;
  1522. }
  1523. ss << '\n';
  1524. ss << "-> " << frame->co->src->get_line(lineno) << '\n';
  1525. stdout_write(ss.str());
  1526. }
  1527. show_headers = false;
  1528. }
  1529. vm->stdout_write("(Pdb) ");
  1530. Frame* frame_0 = &frames[0]->frame;
  1531. std::string line;
  1532. if(!std::getline(std::cin, line)){
  1533. stdout_write("--KeyboardInterrupt--\n");
  1534. continue;
  1535. }
  1536. if(line == "h" || line == "help"){
  1537. stdout_write("h, help: show this help message\n");
  1538. stdout_write("q, quit: exit the debugger\n");
  1539. stdout_write("n, next: execute next line\n");
  1540. stdout_write("s, step: step into\n");
  1541. stdout_write("w, where: show current stack frame\n");
  1542. stdout_write("c, continue: continue execution\n");
  1543. stdout_write("a, args: show local variables\n");
  1544. stdout_write("p, print <expr>: evaluate expression\n");
  1545. stdout_write("l, list: show lines around current line\n");
  1546. stderr_write("ll, longlist: show all lines\n");
  1547. stdout_write("!: execute statement\n");
  1548. continue;
  1549. }
  1550. if(line == "q" || line == "quit") {
  1551. vm->RuntimeError("pdb quit");
  1552. }
  1553. if(line == "n" || line == "next"){
  1554. vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), frame_0->curr_lineno(), false);
  1555. break;
  1556. }
  1557. if(line == "s" || line == "step"){
  1558. vm->_next_breakpoint = NextBreakpoint(vm->callstack.size(), frame_0->curr_lineno(), true);
  1559. break;
  1560. }
  1561. if(line == "w" || line == "where"){
  1562. show_where = !show_where;
  1563. show_headers = true;
  1564. continue;
  1565. }
  1566. if(line == "c" || line == "continue") break;
  1567. if(line == "a" || line == "args"){
  1568. int i = 0;
  1569. for(PyVar obj: frame_0->_locals){
  1570. if(obj == PY_NULL) continue;
  1571. StrName name = frame_0->co->varnames[i++];
  1572. stdout_write(_S(name.sv(), " = ", vm->py_repr(obj), '\n'));
  1573. }
  1574. continue;
  1575. }
  1576. bool is_list = line == "l" || line == "list";
  1577. bool is_longlist = line == "ll" || line == "longlist";
  1578. if(is_list || is_longlist){
  1579. if(frame_0->co->src->is_precompiled) continue;
  1580. int lineno = frame_0->curr_lineno();
  1581. int start, end;
  1582. if(is_list){
  1583. int max_line = frame_0->co->src->line_starts.size() + 1;
  1584. start = std::max(1, lineno-5);
  1585. end = std::min(max_line, lineno+5);
  1586. }else{
  1587. start = frame_0->co->start_line;
  1588. end = frame_0->co->end_line;
  1589. if(start == -1 || end == -1) continue;
  1590. }
  1591. SStream ss;
  1592. int max_width = std::to_string(end).size();
  1593. for(int i=start; i<=end; i++){
  1594. int spaces = max_width - std::to_string(i).size();
  1595. ss << std::string(spaces, ' ') << std::to_string(i);
  1596. if(i == lineno) ss << " -> ";
  1597. else ss << " ";
  1598. ss << frame_0->co->src->get_line(i) << '\n';
  1599. }
  1600. stdout_write(ss.str());
  1601. continue;
  1602. }
  1603. int space = line.find_first_of(' ');
  1604. if(space != -1){
  1605. std::string cmd = line.substr(0, space);
  1606. std::string arg = line.substr(space+1);
  1607. if(arg.empty()) continue; // ignore empty command
  1608. if(cmd == "p" || cmd == "print"){
  1609. CodeObject_ code = compile(arg, "<stdin>", EVAL_MODE, true);
  1610. PyVar retval = vm->_exec(code.get(), frame_0->_module, frame_0->_callable, frame_0->_locals);
  1611. stdout_write(vm->py_repr(retval));
  1612. stdout_write("\n");
  1613. }else if(cmd == "!"){
  1614. CodeObject_ code = compile(arg, "<stdin>", EXEC_MODE, true);
  1615. vm->_exec(code.get(), frame_0->_module, frame_0->_callable, frame_0->_locals);
  1616. }
  1617. continue;
  1618. }
  1619. }
  1620. #endif
  1621. }
  1622. /**************************************************************************/
  1623. void Function::_gc_mark(VM* vm) const{
  1624. decl->_gc_mark(vm);
  1625. if(_closure){
  1626. _closure->apply([=](StrName _, PyVar obj){
  1627. vm->obj_gc_mark(obj);
  1628. });
  1629. }
  1630. }
  1631. void NativeFunc::_gc_mark(VM* vm) const{
  1632. if(decl) decl->_gc_mark(vm);
  1633. }
  1634. void FuncDecl::_gc_mark(VM* vm) const{
  1635. code->_gc_mark(vm);
  1636. for(int i=0; i<kwargs.size(); i++) vm->obj_gc_mark(kwargs[i].value);
  1637. }
  1638. void List::_gc_mark(VM* vm) const{
  1639. for(PyVar obj: *this) vm->obj_gc_mark(obj);
  1640. }
  1641. void Tuple::_gc_mark(VM* vm) const{
  1642. for(PyVar obj: *this) vm->obj_gc_mark(obj);
  1643. }
  1644. void MappingProxy::_gc_mark(VM* vm) const{
  1645. vm->__obj_gc_mark(obj);
  1646. }
  1647. void BoundMethod::_gc_mark(VM* vm) const{
  1648. vm->obj_gc_mark(func);
  1649. vm->obj_gc_mark(self);
  1650. }
  1651. void StarWrapper::_gc_mark(VM* vm) const{
  1652. vm->obj_gc_mark(obj);
  1653. }
  1654. void StaticMethod::_gc_mark(VM* vm) const{
  1655. vm->obj_gc_mark(func);
  1656. }
  1657. void ClassMethod::_gc_mark(VM* vm) const{
  1658. vm->obj_gc_mark(func);
  1659. }
  1660. void Property::_gc_mark(VM* vm) const{
  1661. vm->obj_gc_mark(getter);
  1662. vm->obj_gc_mark(setter);
  1663. }
  1664. void Slice::_gc_mark(VM* vm) const{
  1665. vm->obj_gc_mark(start);
  1666. vm->obj_gc_mark(stop);
  1667. vm->obj_gc_mark(step);
  1668. }
  1669. void Super::_gc_mark(VM* vm) const{
  1670. vm->obj_gc_mark(first);
  1671. }
  1672. void Frame::_gc_mark(VM* vm) const {
  1673. vm->obj_gc_mark(_module);
  1674. co->_gc_mark(vm);
  1675. // Frame could be stored in a generator, so mark _callable for safety
  1676. vm->obj_gc_mark(_callable);
  1677. }
  1678. void ManagedHeap::mark() {
  1679. for(PyObject* obj: _no_gc) vm->__obj_gc_mark(obj);
  1680. vm->callstack.apply([this](Frame& frame){ frame._gc_mark(vm); });
  1681. for(auto [_, co]: vm->__cached_codes) co->_gc_mark(vm);
  1682. vm->obj_gc_mark(vm->__last_exception);
  1683. vm->obj_gc_mark(vm->__curr_class);
  1684. vm->obj_gc_mark(vm->__c.error);
  1685. vm->__stack_gc_mark(vm->s_data.begin(), vm->s_data.end());
  1686. if(_gc_marker_ex) _gc_marker_ex(vm);
  1687. }
  1688. void ManagedHeap::_delete(PyObject* obj){
  1689. const PyTypeInfo* ti = vm->_tp_info(obj->type);
  1690. if(ti->vt._dtor) ti->vt._dtor(obj->_value_ptr());
  1691. if(obj->_attr){
  1692. obj->_attr->~NameDict();
  1693. pool128_dealloc(obj->_attr);
  1694. }
  1695. pool128_dealloc(obj);
  1696. }
  1697. void Dict::_gc_mark(VM* vm) const{
  1698. apply([vm](PyVar k, PyVar v){
  1699. vm->obj_gc_mark(k);
  1700. vm->obj_gc_mark(v);
  1701. });
  1702. }
  1703. void CodeObject::_gc_mark(VM* vm) const {
  1704. for(PyVar v : consts) vm->obj_gc_mark(v);
  1705. for(auto& decl: func_decls) decl->_gc_mark(vm);
  1706. }
  1707. } // namespace pkpy