vm.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. #include "pocketpy/vm.h"
  2. namespace pkpy{
  3. struct JsonSerializer{
  4. VM* vm;
  5. PyObject* root;
  6. SStream ss;
  7. JsonSerializer(VM* vm, PyObject* root) : vm(vm), root(root) {}
  8. template<typename T>
  9. void write_array(T& arr){
  10. ss << '[';
  11. for(int i=0; i<arr.size(); i++){
  12. if(i != 0) ss << ", ";
  13. write_object(arr[i]);
  14. }
  15. ss << ']';
  16. }
  17. void write_dict(Dict& dict){
  18. ss << '{';
  19. bool first = true;
  20. dict.apply([&](PyObject* k, PyObject* v){
  21. if(!first) ss << ", ";
  22. first = false;
  23. if(!is_non_tagged_type(k, vm->tp_str)){
  24. vm->TypeError(_S("json keys must be string, got ", _type_name(vm, vm->_tp(k))));
  25. }
  26. ss << _CAST(Str&, k).escape(false) << ": ";
  27. write_object(v);
  28. });
  29. ss << '}';
  30. }
  31. void write_object(PyObject* obj){
  32. Type obj_t = vm->_tp(obj);
  33. if(obj == vm->None){
  34. ss << "null";
  35. }else if(obj_t == vm->tp_int){
  36. ss << _CAST(i64, obj);
  37. }else if(obj_t == vm->tp_float){
  38. f64 val = _CAST(f64, obj);
  39. if(std::isinf(val) || std::isnan(val)) vm->ValueError("cannot jsonify 'nan' or 'inf'");
  40. ss << val;
  41. }else if(obj_t == vm->tp_bool){
  42. ss << (obj == vm->True ? "true" : "false");
  43. }else if(obj_t == vm->tp_str){
  44. _CAST(Str&, obj).escape_(ss, false);
  45. }else if(obj_t == vm->tp_list){
  46. write_array<List>(_CAST(List&, obj));
  47. }else if(obj_t == vm->tp_tuple){
  48. write_array<Tuple>(_CAST(Tuple&, obj));
  49. }else if(obj_t == vm->tp_dict){
  50. write_dict(_CAST(Dict&, obj));
  51. }else{
  52. vm->TypeError(_S("unrecognized type ", _type_name(vm, obj_t).escape()));
  53. }
  54. }
  55. Str serialize(){
  56. auto _lock = vm->heap.gc_scope_lock();
  57. write_object(root);
  58. return ss.str();
  59. }
  60. };
  61. VM::VM(bool enable_os) : heap(this), enable_os(enable_os) {
  62. this->vm = this;
  63. this->_c.error = nullptr;
  64. _stdout = [](const char* buf, int size) { std::cout.write(buf, size); };
  65. _stderr = [](const char* buf, int size) { std::cerr.write(buf, size); };
  66. _main = nullptr;
  67. _last_exception = nullptr;
  68. _import_handler = [](const char* name_p, int name_size, int* out_size) -> unsigned char*{ return nullptr; };
  69. init_builtin_types();
  70. }
  71. PyObject* VM::py_str(PyObject* obj){
  72. const PyTypeInfo* ti = _inst_type_info(obj);
  73. if(ti->m__str__) return ti->m__str__(this, obj);
  74. PyObject* self;
  75. PyObject* f = get_unbound_method(obj, __str__, &self, false);
  76. if(self != PY_NULL) return call_method(self, f);
  77. return py_repr(obj);
  78. }
  79. PyObject* VM::py_repr(PyObject* obj){
  80. const PyTypeInfo* ti = _inst_type_info(obj);
  81. if(ti->m__repr__) return ti->m__repr__(this, obj);
  82. return call_method(obj, __repr__);
  83. }
  84. PyObject* VM::py_json(PyObject* obj){
  85. auto j = JsonSerializer(this, obj);
  86. return VAR(j.serialize());
  87. }
  88. PyObject* VM::py_iter(PyObject* obj){
  89. const PyTypeInfo* ti = _inst_type_info(obj);
  90. if(ti->m__iter__) return ti->m__iter__(this, obj);
  91. PyObject* self;
  92. PyObject* iter_f = get_unbound_method(obj, __iter__, &self, false);
  93. if(self != PY_NULL) return call_method(self, iter_f);
  94. TypeError(_type_name(vm, _tp(obj)).escape() + " object is not iterable");
  95. return nullptr;
  96. }
  97. std::pair<PyObject**, int> VM::_cast_array(PyObject* obj){
  98. if(is_non_tagged_type(obj, VM::tp_list)){
  99. List& list = PK_OBJ_GET(List, obj);
  100. return {list.data(), list.size()};
  101. }else if(is_non_tagged_type(obj, VM::tp_tuple)){
  102. Tuple& tuple = PK_OBJ_GET(Tuple, obj);
  103. return {tuple.data(), tuple.size()};
  104. }
  105. TypeError(_S("expected list or tuple, got ", _type_name(this, _tp(obj)).escape()));
  106. PK_UNREACHABLE();
  107. }
  108. Frame* VM::top_frame(){
  109. #if PK_DEBUG_EXTRA_CHECK
  110. if(callstack.empty()) PK_FATAL_ERROR();
  111. #endif
  112. return &callstack.top();
  113. }
  114. void VM::_pop_frame(){
  115. s_data.reset(callstack.top()._sp_base);
  116. callstack.pop();
  117. }
  118. PyObject* VM::find_name_in_mro(Type cls, StrName name){
  119. PyObject* val;
  120. do{
  121. val = _t(cls)->attr().try_get(name);
  122. if(val != nullptr) return val;
  123. cls = _all_types[cls].base;
  124. if(cls.index == -1) break;
  125. }while(true);
  126. return nullptr;
  127. }
  128. bool VM::isinstance(PyObject* obj, Type base){
  129. return issubclass(_tp(obj), base);
  130. }
  131. bool VM::issubclass(Type cls, Type base){
  132. do{
  133. if(cls == base) return true;
  134. Type next = _all_types[cls].base;
  135. if(next.index == -1) break;
  136. cls = next;
  137. }while(true);
  138. return false;
  139. }
  140. PyObject* VM::exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module){
  141. if(_module == nullptr) _module = _main;
  142. try {
  143. CodeObject_ code = compile(source, filename, mode);
  144. #if PK_DEBUG_DIS_EXEC
  145. if(_module == _main) std::cout << disassemble(code) << '\n';
  146. #endif
  147. return _exec(code, _module);
  148. }catch (const Exception& e){
  149. stderr_write(e.summary() + "\n");
  150. }
  151. #if !PK_DEBUG_FULL_EXCEPTION
  152. catch(const std::exception& e) {
  153. Str msg = "An std::exception occurred! It could be a bug.\n";
  154. msg = msg + e.what() + "\n";
  155. stderr_write(msg);
  156. }
  157. catch(NeedMoreLines){
  158. throw;
  159. }
  160. catch(...) {
  161. Str msg = "An unknown exception occurred! It could be a bug. Please report it to @blueloveTH on GitHub.\n";
  162. stderr_write(msg);
  163. }
  164. #endif
  165. callstack.clear();
  166. s_data.clear();
  167. return nullptr;
  168. }
  169. PyObject* VM::exec(std::string_view source){
  170. return exec(source, "main.py", EXEC_MODE);
  171. }
  172. PyObject* VM::eval(std::string_view source){
  173. return exec(source, "<eval>", EVAL_MODE);
  174. }
  175. PyObject* VM::new_type_object(PyObject* mod, StrName name, Type base, bool subclass_enabled){
  176. PyObject* obj = heap._new<Type>(tp_type, _all_types.size());
  177. const PyTypeInfo& base_info = _all_types[base];
  178. if(!base_info.subclass_enabled){
  179. TypeError(_S("type ", base_info.name.escape(), " is not `subclass_enabled`"));
  180. }
  181. PyTypeInfo info{
  182. obj,
  183. base,
  184. mod,
  185. name,
  186. subclass_enabled,
  187. };
  188. _all_types.push_back(info);
  189. return obj;
  190. }
  191. Type VM::_new_type_object(StrName name, Type base, bool subclass_enabled) {
  192. PyObject* obj = new_type_object(nullptr, name, base, subclass_enabled);
  193. return PK_OBJ_GET(Type, obj);
  194. }
  195. const PyTypeInfo* VM::_inst_type_info(PyObject* obj){
  196. if(is_int(obj)) return &_all_types[tp_int];
  197. if(is_float(obj)) return &_all_types[tp_float];
  198. return &_all_types[obj->type];
  199. }
  200. bool VM::py_eq(PyObject* lhs, PyObject* rhs){
  201. if(lhs == rhs) return true;
  202. const PyTypeInfo* ti = _inst_type_info(lhs);
  203. PyObject* res;
  204. if(ti->m__eq__){
  205. res = ti->m__eq__(this, lhs, rhs);
  206. if(res != vm->NotImplemented) return res == vm->True;
  207. }
  208. res = call_method(lhs, __eq__, rhs);
  209. if(res != vm->NotImplemented) return res == vm->True;
  210. ti = _inst_type_info(rhs);
  211. if(ti->m__eq__){
  212. res = ti->m__eq__(this, rhs, lhs);
  213. if(res != vm->NotImplemented) return res == vm->True;
  214. }
  215. res = call_method(rhs, __eq__, lhs);
  216. if(res != vm->NotImplemented) return res == vm->True;
  217. return false;
  218. }
  219. i64 VM::normalized_index(i64 index, int size){
  220. if(index < 0) index += size;
  221. if(index < 0 || index >= size){
  222. IndexError(std::to_string(index) + " not in [0, " + std::to_string(size) + ")");
  223. }
  224. return index;
  225. }
  226. PyObject* VM::py_next(PyObject* obj){
  227. const PyTypeInfo* ti = _inst_type_info(obj);
  228. if(ti->m__next__) return ti->m__next__(this, obj);
  229. return call_method(obj, __next__);
  230. }
  231. bool VM::py_callable(PyObject* obj){
  232. Type cls = vm->_tp(obj);
  233. switch(cls.index){
  234. case VM::tp_function.index: return vm->True;
  235. case VM::tp_native_func.index: return vm->True;
  236. case VM::tp_bound_method.index: return vm->True;
  237. case VM::tp_type.index: return vm->True;
  238. }
  239. return vm->find_name_in_mro(cls, __call__) != nullptr;
  240. }
  241. PyObject* VM::py_import(Str path, bool throw_err){
  242. if(path.empty()) vm->ValueError("empty module name");
  243. static auto f_join = [](const pod_vector<std::string_view>& cpnts){
  244. SStream ss;
  245. for(int i=0; i<cpnts.size(); i++){
  246. if(i != 0) ss << ".";
  247. ss << cpnts[i];
  248. }
  249. return ss.str();
  250. };
  251. if(path[0] == '.'){
  252. if(_import_context.pending.empty()){
  253. ImportError("relative import outside of package");
  254. }
  255. Str curr_path = _import_context.pending.back();
  256. bool curr_is_init = _import_context.pending_is_init.back();
  257. // convert relative path to absolute path
  258. pod_vector<std::string_view> cpnts = curr_path.split('.');
  259. int prefix = 0; // how many dots in the prefix
  260. for(int i=0; i<path.length(); i++){
  261. if(path[i] == '.') prefix++;
  262. else break;
  263. }
  264. if(prefix > cpnts.size()) ImportError("attempted relative import beyond top-level package");
  265. path = path.substr(prefix); // remove prefix
  266. for(int i=(int)curr_is_init; i<prefix; i++) cpnts.pop_back();
  267. if(!path.empty()) cpnts.push_back(path.sv());
  268. path = f_join(cpnts);
  269. }
  270. PK_ASSERT(path.begin()[0] != '.' && path.end()[-1] != '.');
  271. // check existing module
  272. StrName name(path);
  273. PyObject* ext_mod = _modules.try_get(name);
  274. if(ext_mod != nullptr) return ext_mod;
  275. pod_vector<std::string_view> path_cpnts = path.split('.');
  276. // check circular import
  277. if(_import_context.pending.size() > 128){
  278. ImportError("maximum recursion depth exceeded while importing");
  279. }
  280. // try import
  281. Str filename = path.replace('.', PK_PLATFORM_SEP) + ".py";
  282. Str source;
  283. bool is_init = false;
  284. auto it = _lazy_modules.find(name);
  285. if(it == _lazy_modules.end()){
  286. int out_size;
  287. unsigned char* out = _import_handler(filename.data, filename.size, &out_size);
  288. if(out == nullptr){
  289. filename = path.replace('.', PK_PLATFORM_SEP).str() + PK_PLATFORM_SEP + "__init__.py";
  290. is_init = true;
  291. out = _import_handler(filename.data, filename.size, &out_size);
  292. }
  293. if(out == nullptr){
  294. if(throw_err) ImportError(_S("module ", path.escape(), " not found"));
  295. else return nullptr;
  296. }
  297. PK_ASSERT(out_size >= 0)
  298. source = Str(std::string_view((char*)out, out_size));
  299. free(out);
  300. }else{
  301. source = it->second;
  302. _lazy_modules.erase(it);
  303. }
  304. auto _ = _import_context.scope(path, is_init);
  305. CodeObject_ code = compile(source, filename, EXEC_MODE);
  306. Str name_cpnt = path_cpnts.back();
  307. path_cpnts.pop_back();
  308. PyObject* new_mod = new_module(name_cpnt, f_join(path_cpnts));
  309. _exec(code, new_mod);
  310. return new_mod;
  311. }
  312. VM::~VM() {
  313. callstack.clear();
  314. s_data.clear();
  315. _all_types.clear();
  316. _modules.clear();
  317. _lazy_modules.clear();
  318. }
  319. PyObject* VM::py_negate(PyObject* obj){
  320. const PyTypeInfo* ti = _inst_type_info(obj);
  321. if(ti->m__neg__) return ti->m__neg__(this, obj);
  322. return call_method(obj, __neg__);
  323. }
  324. bool VM::py_bool(PyObject* obj){
  325. if(obj == vm->True) return true;
  326. if(obj == vm->False) return false;
  327. if(obj == None) return false;
  328. if(is_int(obj)) return _CAST(i64, obj) != 0;
  329. if(is_float(obj)) return _CAST(f64, obj) != 0.0;
  330. PyObject* self;
  331. PyObject* len_f = get_unbound_method(obj, __len__, &self, false);
  332. if(self != PY_NULL){
  333. PyObject* ret = call_method(self, len_f);
  334. return CAST(i64, ret) > 0;
  335. }
  336. return true;
  337. }
  338. PyObject* VM::py_list(PyObject* it){
  339. auto _lock = heap.gc_scope_lock();
  340. it = py_iter(it);
  341. List list;
  342. PyObject* obj = py_next(it);
  343. while(obj != StopIteration){
  344. list.push_back(obj);
  345. obj = py_next(it);
  346. }
  347. return VAR(std::move(list));
  348. }
  349. void VM::parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step){
  350. auto clip = [](int value, int min, int max){
  351. if(value < min) return min;
  352. if(value > max) return max;
  353. return value;
  354. };
  355. if(s.step == None) step = 1;
  356. else step = CAST(int, s.step);
  357. if(step == 0) ValueError("slice step cannot be zero");
  358. if(step > 0){
  359. if(s.start == None){
  360. start = 0;
  361. }else{
  362. start = CAST(int, s.start);
  363. if(start < 0) start += length;
  364. start = clip(start, 0, length);
  365. }
  366. if(s.stop == None){
  367. stop = length;
  368. }else{
  369. stop = CAST(int, s.stop);
  370. if(stop < 0) stop += length;
  371. stop = clip(stop, 0, length);
  372. }
  373. }else{
  374. if(s.start == None){
  375. start = length - 1;
  376. }else{
  377. start = CAST(int, s.start);
  378. if(start < 0) start += length;
  379. start = clip(start, -1, length - 1);
  380. }
  381. if(s.stop == None){
  382. stop = -1;
  383. }else{
  384. stop = CAST(int, s.stop);
  385. if(stop < 0) stop += length;
  386. stop = clip(stop, -1, length - 1);
  387. }
  388. }
  389. }
  390. i64 VM::py_hash(PyObject* obj){
  391. // https://docs.python.org/3.10/reference/datamodel.html#object.__hash__
  392. const PyTypeInfo* ti = _inst_type_info(obj);
  393. if(ti->m__hash__) return ti->m__hash__(this, obj);
  394. PyObject* self;
  395. PyObject* f = get_unbound_method(obj, __hash__, &self, false);
  396. if(f != nullptr){
  397. PyObject* ret = call_method(self, f);
  398. return CAST(i64, ret);
  399. }
  400. // if it is trivial `object`, return PK_BITS
  401. if(ti == &_all_types[tp_object]) return PK_BITS(obj);
  402. // otherwise, we check if it has a custom __eq__ other than object.__eq__
  403. bool has_custom_eq = false;
  404. if(ti->m__eq__) has_custom_eq = true;
  405. else{
  406. f = get_unbound_method(obj, __eq__, &self, false);
  407. has_custom_eq = f != _t(tp_object)->attr(__eq__);
  408. }
  409. if(has_custom_eq){
  410. TypeError(_S("unhashable type: ", ti->name.escape()));
  411. PK_UNREACHABLE()
  412. }else{
  413. return PK_BITS(obj);
  414. }
  415. }
  416. PyObject* VM::_format_string(Str spec, PyObject* obj){
  417. if(spec.empty()) return py_str(obj);
  418. char type;
  419. switch(spec.end()[-1]){
  420. case 'f': case 'd': case 's':
  421. type = spec.end()[-1];
  422. spec = spec.substr(0, spec.length() - 1);
  423. break;
  424. default: type = ' '; break;
  425. }
  426. char pad_c = ' ';
  427. for(char c: std::string_view("0-=*#@!~")){
  428. if(spec[0] == c){
  429. pad_c = c;
  430. spec = spec.substr(1);
  431. break;
  432. }
  433. }
  434. char align;
  435. if(spec[0] == '^'){
  436. align = '^';
  437. spec = spec.substr(1);
  438. }else if(spec[0] == '>'){
  439. align = '>';
  440. spec = spec.substr(1);
  441. }else if(spec[0] == '<'){
  442. align = '<';
  443. spec = spec.substr(1);
  444. }else{
  445. if(is_int(obj) || is_float(obj)) align = '>';
  446. else align = '<';
  447. }
  448. int dot = spec.index(".");
  449. int width, precision;
  450. try{
  451. if(dot >= 0){
  452. if(dot == 0){
  453. width = -1;
  454. }else{
  455. width = std::stoi(spec.substr(0, dot).str());
  456. }
  457. precision = std::stoi(spec.substr(dot+1).str());
  458. }else{
  459. width = std::stoi(spec.str());
  460. precision = -1;
  461. }
  462. }catch(...){
  463. ValueError("invalid format specifer");
  464. }
  465. if(type != 'f' && dot >= 0) ValueError("precision not allowed in the format specifier");
  466. Str ret;
  467. if(type == 'f'){
  468. f64 val = CAST(f64, obj);
  469. if(precision < 0) precision = 6;
  470. SStream ss;
  471. ss.setprecision(precision);
  472. ss << val;
  473. ret = ss.str();
  474. }else if(type == 'd'){
  475. ret = std::to_string(CAST(i64, obj));
  476. }else if(type == 's'){
  477. ret = CAST(Str&, obj);
  478. }else{
  479. ret = CAST(Str&, py_str(obj));
  480. }
  481. if(width != -1 && width > ret.length()){
  482. int pad = width - ret.length();
  483. if(align == '>' || align == '<'){
  484. std::string padding(pad, pad_c);
  485. if(align == '>') ret = padding.c_str() + ret;
  486. else ret = ret + padding.c_str();
  487. }else{ // ^
  488. int pad_left = pad / 2;
  489. int pad_right = pad - pad_left;
  490. std::string padding_left(pad_left, pad_c);
  491. std::string padding_right(pad_right, pad_c);
  492. ret = padding_left.c_str() + ret + padding_right.c_str();
  493. }
  494. }
  495. return VAR(ret);
  496. }
  497. PyObject* VM::new_module(Str name, Str package) {
  498. PyObject* obj = heap._new<DummyModule>(tp_module);
  499. obj->attr().set(__name__, VAR(name));
  500. obj->attr().set(__package__, VAR(package));
  501. // convert to fullname
  502. if(!package.empty()) name = package + "." + name;
  503. obj->attr().set(__path__, VAR(name));
  504. // we do not allow override in order to avoid memory leak
  505. // it is because Module objects are not garbage collected
  506. if(_modules.contains(name)){
  507. throw std::runtime_error(_S("module ", name.escape(), " already exists").str());
  508. }
  509. // set it into _modules
  510. _modules.set(name, obj);
  511. return obj;
  512. }
  513. static std::string _opcode_argstr(VM* vm, Bytecode byte, const CodeObject* co){
  514. std::string argStr = std::to_string(byte.arg);
  515. switch(byte.op){
  516. case OP_LOAD_CONST: case OP_FORMAT_STRING: case OP_IMPORT_PATH:
  517. if(vm != nullptr){
  518. argStr += _S(" (", CAST(Str, vm->py_repr(co->consts[byte.arg])), ")").sv();
  519. }
  520. break;
  521. case OP_LOAD_NAME: case OP_LOAD_GLOBAL: case OP_LOAD_NONLOCAL: case OP_STORE_GLOBAL:
  522. case OP_LOAD_ATTR: case OP_LOAD_METHOD: case OP_STORE_ATTR: case OP_DELETE_ATTR:
  523. case OP_BEGIN_CLASS: case OP_GOTO:
  524. case OP_DELETE_GLOBAL: case OP_INC_GLOBAL: case OP_DEC_GLOBAL: case OP_STORE_CLASS_ATTR:
  525. argStr += _S(" (", StrName(byte.arg).sv(), ")").sv();
  526. break;
  527. case OP_LOAD_FAST: case OP_STORE_FAST: case OP_DELETE_FAST: case OP_INC_FAST: case OP_DEC_FAST:
  528. argStr += _S(" (", co->varnames[byte.arg].sv(), ")").sv();
  529. break;
  530. case OP_LOAD_FUNCTION:
  531. argStr += _S(" (", co->func_decls[byte.arg]->code->name, ")").sv();
  532. break;
  533. }
  534. return argStr;
  535. }
  536. Str VM::disassemble(CodeObject_ co){
  537. auto pad = [](const Str& s, const int n){
  538. if(s.length() >= n) return s.substr(0, n);
  539. return s + std::string(n - s.length(), ' ');
  540. };
  541. pod_vector<int> jumpTargets;
  542. for(auto byte : co->codes){
  543. if(byte.op == OP_JUMP_ABSOLUTE || byte.op == OP_POP_JUMP_IF_FALSE || byte.op == OP_SHORTCUT_IF_FALSE_OR_POP || byte.op == OP_FOR_ITER){
  544. jumpTargets.push_back(byte.arg);
  545. }
  546. if(byte.op == OP_GOTO){
  547. // TODO: pre-compute jump targets for OP_GOTO
  548. int* target = co->labels.try_get_2_likely_found(StrName(byte.arg));
  549. if(target != nullptr) jumpTargets.push_back(*target);
  550. }
  551. }
  552. SStream ss;
  553. int prev_line = -1;
  554. for(int i=0; i<co->codes.size(); i++){
  555. const Bytecode& byte = co->codes[i];
  556. Str line = std::to_string(co->lines[i].lineno);
  557. if(co->lines[i].lineno == prev_line) line = "";
  558. else{
  559. if(prev_line != -1) ss << "\n";
  560. prev_line = co->lines[i].lineno;
  561. }
  562. std::string pointer;
  563. if(std::find(jumpTargets.begin(), jumpTargets.end(), i) != jumpTargets.end()){
  564. pointer = "-> ";
  565. }else{
  566. pointer = " ";
  567. }
  568. ss << pad(line, 8) << pointer << pad(std::to_string(i), 3);
  569. std::string bc_name(OP_NAMES[byte.op]);
  570. if(co->lines[i].is_virtual) bc_name += '*';
  571. ss << " " << pad(bc_name, 25) << " ";
  572. // ss << pad(byte.arg == -1 ? "" : std::to_string(byte.arg), 5);
  573. std::string argStr = _opcode_argstr(this, byte, co.get());
  574. ss << argStr;
  575. // ss << pad(argStr, 40); // may overflow
  576. // ss << co->blocks[byte.block].type;
  577. if(i != co->codes.size() - 1) ss << '\n';
  578. }
  579. for(auto& decl: co->func_decls){
  580. ss << "\n\n" << "Disassembly of " << decl->code->name << ":\n";
  581. ss << disassemble(decl->code);
  582. }
  583. ss << "\n";
  584. return Str(ss.str());
  585. }
  586. #if PK_DEBUG_CEVAL_STEP
  587. void VM::_log_s_data(const char* title) {
  588. if(_main == nullptr) return;
  589. if(callstack.empty()) return;
  590. SStream ss;
  591. if(title) ss << title << " | ";
  592. std::map<PyObject**, int> sp_bases;
  593. for(Frame& f: callstack.data()){
  594. if(f._sp_base == nullptr) PK_FATAL_ERROR();
  595. sp_bases[f._sp_base] += 1;
  596. }
  597. Frame* frame = top_frame();
  598. int line = frame->co->lines[frame->_ip];
  599. ss << frame->co->name << ":" << line << " [";
  600. for(PyObject** p=s_data.begin(); p!=s_data.end(); p++){
  601. ss << std::string(sp_bases[p], '|');
  602. if(sp_bases[p] > 0) ss << " ";
  603. PyObject* obj = *p;
  604. if(obj == nullptr) ss << "(nil)";
  605. else if(obj == PY_NULL) ss << "NULL";
  606. else if(is_int(obj)) ss << CAST(i64, obj);
  607. else if(is_float(obj)) ss << CAST(f64, obj);
  608. else if(is_type(obj, tp_str)) ss << CAST(Str, obj).escape();
  609. else if(obj == None) ss << "None";
  610. else if(obj == True) ss << "True";
  611. else if(obj == False) ss << "False";
  612. else if(is_type(obj, tp_function)){
  613. auto& f = CAST(Function&, obj);
  614. ss << f.decl->code->name << "(...)";
  615. } else if(is_type(obj, tp_type)){
  616. Type t = PK_OBJ_GET(Type, obj);
  617. ss << "<class " + _all_types[t].name.escape() + ">";
  618. } else if(is_type(obj, tp_list)){
  619. auto& t = CAST(List&, obj);
  620. ss << "list(size=" << t.size() << ")";
  621. } else if(is_type(obj, tp_tuple)){
  622. auto& t = CAST(Tuple&, obj);
  623. ss << "tuple(size=" << t.size() << ")";
  624. } else ss << "(" << _type_name(this, obj->type) << ")";
  625. ss << ", ";
  626. }
  627. std::string output = ss.str();
  628. if(!s_data.empty()) {
  629. output.pop_back(); output.pop_back();
  630. }
  631. output.push_back(']');
  632. Bytecode byte = frame->co->codes[frame->_ip];
  633. std::cout << output << " " << OP_NAMES[byte.op] << " " << _opcode_argstr(nullptr, byte, frame->co) << std::endl;
  634. }
  635. #endif
  636. void VM::init_builtin_types(){
  637. _all_types.push_back({heap._new<Type>(Type(1), Type(0)), -1, nullptr, "object", true});
  638. _all_types.push_back({heap._new<Type>(Type(1), Type(1)), 0, nullptr, "type", false});
  639. if(tp_int != _new_type_object("int")) exit(-3);
  640. if((tp_float != _new_type_object("float"))) exit(-3);
  641. if(tp_bool != _new_type_object("bool")) exit(-3);
  642. if(tp_str != _new_type_object("str")) exit(-3);
  643. if(tp_list != _new_type_object("list")) exit(-3);
  644. if(tp_tuple != _new_type_object("tuple")) exit(-3);
  645. if(tp_slice != _new_type_object("slice")) exit(-3);
  646. if(tp_range != _new_type_object("range")) exit(-3);
  647. if(tp_module != _new_type_object("module")) exit(-3);
  648. if(tp_function != _new_type_object("function")) exit(-3);
  649. if(tp_native_func != _new_type_object("native_func")) exit(-3);
  650. if(tp_bound_method != _new_type_object("bound_method")) exit(-3);
  651. if(tp_super != _new_type_object("super")) exit(-3);
  652. if(tp_exception != _new_type_object("Exception", 0, true)) exit(-3);
  653. if(tp_bytes != _new_type_object("bytes")) exit(-3);
  654. if(tp_mappingproxy != _new_type_object("mappingproxy")) exit(-3);
  655. if(tp_dict != _new_type_object("dict")) exit(-3);
  656. if(tp_property != _new_type_object("property")) exit(-3);
  657. if(tp_star_wrapper != _new_type_object("_star_wrapper")) exit(-3);
  658. if(tp_staticmethod != _new_type_object("staticmethod")) exit(-3);
  659. if(tp_classmethod != _new_type_object("classmethod")) exit(-3);
  660. // SyntaxError and IndentationError must be created here
  661. Type tp_syntax_error = _new_type_object("SyntaxError", tp_exception, true);
  662. Type tp_indentation_error = _new_type_object("IndentationError", tp_syntax_error, true);
  663. this->None = heap._new<Dummy>(_new_type_object("NoneType"));
  664. this->NotImplemented = heap._new<Dummy>(_new_type_object("NotImplementedType"));
  665. this->Ellipsis = heap._new<Dummy>(_new_type_object("ellipsis"));
  666. this->True = heap._new<Dummy>(tp_bool);
  667. this->False = heap._new<Dummy>(tp_bool);
  668. this->StopIteration = heap._new<Dummy>(_new_type_object("StopIterationType"));
  669. this->builtins = new_module("builtins");
  670. // setup public types
  671. builtins->attr().set("type", _t(tp_type));
  672. builtins->attr().set("object", _t(tp_object));
  673. builtins->attr().set("bool", _t(tp_bool));
  674. builtins->attr().set("int", _t(tp_int));
  675. builtins->attr().set("float", _t(tp_float));
  676. builtins->attr().set("str", _t(tp_str));
  677. builtins->attr().set("list", _t(tp_list));
  678. builtins->attr().set("tuple", _t(tp_tuple));
  679. builtins->attr().set("range", _t(tp_range));
  680. builtins->attr().set("bytes", _t(tp_bytes));
  681. builtins->attr().set("dict", _t(tp_dict));
  682. builtins->attr().set("property", _t(tp_property));
  683. builtins->attr().set("StopIteration", StopIteration);
  684. builtins->attr().set("NotImplemented", NotImplemented);
  685. builtins->attr().set("slice", _t(tp_slice));
  686. builtins->attr().set("Exception", _t(tp_exception));
  687. builtins->attr().set("SyntaxError", _t(tp_syntax_error));
  688. builtins->attr().set("IndentationError", _t(tp_indentation_error));
  689. post_init();
  690. this->_main = new_module("__main__");
  691. }
  692. // `heap.gc_scope_lock();` needed before calling this function
  693. void VM::_unpack_as_list(ArgsView args, List& list){
  694. for(PyObject* obj: args){
  695. if(is_non_tagged_type(obj, tp_star_wrapper)){
  696. const StarWrapper& w = _CAST(StarWrapper&, obj);
  697. // maybe this check should be done in the compile time
  698. if(w.level != 1) TypeError("expected level 1 star wrapper");
  699. PyObject* _0 = py_iter(w.obj);
  700. PyObject* _1 = py_next(_0);
  701. while(_1 != StopIteration){
  702. list.push_back(_1);
  703. _1 = py_next(_0);
  704. }
  705. }else{
  706. list.push_back(obj);
  707. }
  708. }
  709. }
  710. // `heap.gc_scope_lock();` needed before calling this function
  711. void VM::_unpack_as_dict(ArgsView args, Dict& dict){
  712. for(PyObject* obj: args){
  713. if(is_non_tagged_type(obj, tp_star_wrapper)){
  714. const StarWrapper& w = _CAST(StarWrapper&, obj);
  715. // maybe this check should be done in the compile time
  716. if(w.level != 2) TypeError("expected level 2 star wrapper");
  717. const Dict& other = CAST(Dict&, w.obj);
  718. dict.update(other);
  719. }else{
  720. const Tuple& t = CAST(Tuple&, obj);
  721. if(t.size() != 2) TypeError("expected tuple of length 2");
  722. dict.set(t[0], t[1]);
  723. }
  724. }
  725. }
  726. void VM::_prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, const FuncDecl_& decl){
  727. const CodeObject* co = decl->code.get();
  728. int co_nlocals = co->varnames.size();
  729. int decl_argc = decl->args.size();
  730. if(args.size() < decl_argc){
  731. vm->TypeError(_S(
  732. co->name, "() takes ", decl_argc, " positional arguments but ", args.size(), " were given"
  733. ));
  734. }
  735. int i = 0;
  736. // prepare args
  737. for(int index: decl->args) buffer[index] = args[i++];
  738. // set extra varnames to PY_NULL
  739. for(int j=i; j<co_nlocals; j++) buffer[j] = PY_NULL;
  740. // prepare kwdefaults
  741. for(auto& kv: decl->kwargs) buffer[kv.index] = kv.value;
  742. // handle *args
  743. if(decl->starred_arg != -1){
  744. ArgsView vargs(args.begin() + i, args.end());
  745. buffer[decl->starred_arg] = VAR(vargs.to_tuple());
  746. i += vargs.size();
  747. }else{
  748. // kwdefaults override
  749. for(auto& kv: decl->kwargs){
  750. if(i >= args.size()) break;
  751. buffer[kv.index] = args[i++];
  752. }
  753. if(i < args.size()) TypeError(_S("too many arguments", " (", decl->code->name, ')'));
  754. }
  755. PyObject* vkwargs;
  756. if(decl->starred_kwarg != -1){
  757. vkwargs = VAR(Dict(this));
  758. buffer[decl->starred_kwarg] = vkwargs;
  759. }else{
  760. vkwargs = nullptr;
  761. }
  762. for(int j=0; j<kwargs.size(); j+=2){
  763. StrName key(_CAST(uint16_t, kwargs[j]));
  764. int index = decl->kw_to_index.try_get_likely_found(key);
  765. // if key is an explicit key, set as local variable
  766. if(index >= 0){
  767. buffer[index] = kwargs[j+1];
  768. }else{
  769. // otherwise, set as **kwargs if possible
  770. if(vkwargs == nullptr){
  771. TypeError(_S(key.escape(), " is an invalid keyword argument for ", co->name, "()"));
  772. }else{
  773. Dict& dict = _CAST(Dict&, vkwargs);
  774. dict.set(VAR(key.sv()), kwargs[j+1]);
  775. }
  776. }
  777. }
  778. }
  779. PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
  780. PyObject** p1 = s_data._sp - KWARGC*2;
  781. PyObject** p0 = p1 - ARGC - 2;
  782. // [callable, <self>, args..., kwargs...]
  783. // ^p0 ^p1 ^_sp
  784. PyObject* callable = p1[-(ARGC + 2)];
  785. Type callable_t = _tp(callable);
  786. bool method_call = p0[1] != PY_NULL;
  787. // handle boundmethod, do a patch
  788. if(callable_t == tp_bound_method){
  789. if(method_call) PK_FATAL_ERROR();
  790. BoundMethod& bm = PK_OBJ_GET(BoundMethod, callable);
  791. callable = bm.func; // get unbound method
  792. callable_t = _tp(callable);
  793. p1[-(ARGC + 2)] = bm.func;
  794. p1[-(ARGC + 1)] = bm.self;
  795. method_call = true;
  796. // [unbound, self, args..., kwargs...]
  797. }
  798. ArgsView args(p1 - ARGC - int(method_call), p1);
  799. ArgsView kwargs(p1, s_data._sp);
  800. PyObject** _base = args.begin();
  801. PyObject* buffer[PK_MAX_CO_VARNAMES];
  802. if(callable_t == tp_function){
  803. /*****************_py_call*****************/
  804. if(s_data.is_overflow()) StackOverflowError();
  805. const Function& fn = PK_OBJ_GET(Function, callable);
  806. const CodeObject* co = fn.decl->code.get();
  807. int co_nlocals = co->varnames.size();
  808. if(fn.decl->is_simple){
  809. if(args.size() != fn.decl->args.size()){
  810. TypeError(_S(
  811. co->name, "() takes ", fn.decl->args.size(), " positional arguments but ", args.size(), " were given"
  812. ));
  813. }
  814. if(!kwargs.empty()) TypeError(_S(co->name, "() takes no keyword arguments"));
  815. // [callable, <self>, args..., local_vars...]
  816. // ^p0 ^p1 ^_sp
  817. s_data.reset(_base + co_nlocals);
  818. // initialize local variables to PY_NULL
  819. for(PyObject** p=p1; p!=s_data._sp; p++) *p = PY_NULL;
  820. goto __FAST_CALL;
  821. }
  822. _prepare_py_call(buffer, args, kwargs, fn.decl);
  823. if(co->is_generator){
  824. s_data.reset(p0);
  825. return _py_generator(
  826. Frame(nullptr, co, fn._module, callable, nullptr),
  827. ArgsView(buffer, buffer + co_nlocals)
  828. );
  829. }
  830. // copy buffer back to stack
  831. s_data.reset(_base + co_nlocals);
  832. for(int j=0; j<co_nlocals; j++) _base[j] = buffer[j];
  833. __FAST_CALL:
  834. callstack.emplace(p0, co, fn._module, callable, args.begin());
  835. if(op_call) return PY_OP_CALL;
  836. return _run_top_frame();
  837. /*****************_py_call*****************/
  838. }
  839. if(callable_t == tp_native_func){
  840. const auto& f = PK_OBJ_GET(NativeFunc, callable);
  841. PyObject* ret;
  842. if(f.decl != nullptr){
  843. int co_nlocals = f.decl->code->varnames.size();
  844. _prepare_py_call(buffer, args, kwargs, f.decl);
  845. // copy buffer back to stack
  846. s_data.reset(_base + co_nlocals);
  847. for(int j=0; j<co_nlocals; j++) _base[j] = buffer[j];
  848. ret = f.call(vm, ArgsView(s_data._sp - co_nlocals, s_data._sp));
  849. }else{
  850. if(KWARGC != 0) TypeError("old-style native_func does not accept keyword arguments");
  851. f.check_size(this, args);
  852. ret = f.call(this, args);
  853. }
  854. s_data.reset(p0);
  855. return ret;
  856. }
  857. if(callable_t == tp_type){
  858. // [type, NULL, args..., kwargs...]
  859. PyObject* new_f = find_name_in_mro(PK_OBJ_GET(Type, callable), __new__);
  860. PyObject* obj;
  861. #if PK_DEBUG_EXTRA_CHECK
  862. PK_ASSERT(new_f != nullptr && !method_call);
  863. #endif
  864. if(new_f == cached_object__new__) {
  865. // fast path for object.__new__
  866. obj = vm->heap.gcnew<DummyInstance>(PK_OBJ_GET(Type, callable));
  867. }else{
  868. PUSH(new_f);
  869. PUSH(PY_NULL);
  870. PUSH(callable); // cls
  871. for(PyObject* o: args) PUSH(o);
  872. for(PyObject* o: kwargs) PUSH(o);
  873. // if obj is not an instance of `cls`, the behavior is undefined
  874. obj = vectorcall(ARGC+1, KWARGC);
  875. }
  876. // __init__
  877. PyObject* self;
  878. callable = get_unbound_method(obj, __init__, &self, false);
  879. if (callable != nullptr) {
  880. callable_t = _tp(callable);
  881. // replace `NULL` with `self`
  882. p1[-(ARGC + 2)] = callable;
  883. p1[-(ARGC + 1)] = self;
  884. // [init_f, self, args..., kwargs...]
  885. vectorcall(ARGC, KWARGC);
  886. // We just discard the return value of `__init__`
  887. // in cpython it raises a TypeError if the return value is not None
  888. }else{
  889. // manually reset the stack
  890. s_data.reset(p0);
  891. }
  892. return obj;
  893. }
  894. // handle `__call__` overload
  895. PyObject* self;
  896. PyObject* call_f = get_unbound_method(callable, __call__, &self, false);
  897. if(self != PY_NULL){
  898. p1[-(ARGC + 2)] = call_f;
  899. p1[-(ARGC + 1)] = self;
  900. // [call_f, self, args..., kwargs...]
  901. return vectorcall(ARGC, KWARGC, false);
  902. }
  903. TypeError(_type_name(vm, callable_t).escape() + " object is not callable");
  904. PK_UNREACHABLE()
  905. }
  906. void VM::delattr(PyObject *_0, StrName _name){
  907. const PyTypeInfo* ti = _inst_type_info(_0);
  908. if(ti->m__delattr__ && ti->m__delattr__(this, _0, _name)) return;
  909. if(is_tagged(_0) || !_0->is_attr_valid()) TypeError("cannot delete attribute");
  910. if(!_0->attr().del(_name)) AttributeError(_0, _name);
  911. }
  912. // https://docs.python.org/3/howto/descriptor.html#invocation-from-an-instance
  913. PyObject* VM::getattr(PyObject* obj, StrName name, bool throw_err){
  914. Type objtype(0);
  915. // handle super() proxy
  916. if(is_non_tagged_type(obj, tp_super)){
  917. const Super& super = PK_OBJ_GET(Super, obj);
  918. obj = super.first;
  919. objtype = super.second;
  920. }else{
  921. objtype = _tp(obj);
  922. }
  923. PyObject* cls_var = find_name_in_mro(objtype, name);
  924. if(cls_var != nullptr){
  925. // handle descriptor
  926. if(is_non_tagged_type(cls_var, tp_property)){
  927. const Property& prop = PK_OBJ_GET(Property, cls_var);
  928. return call(prop.getter, obj);
  929. }
  930. }
  931. // handle instance __dict__
  932. if(!is_tagged(obj) && obj->is_attr_valid()){
  933. PyObject* val;
  934. if(obj->type == tp_type){
  935. val = find_name_in_mro(PK_OBJ_GET(Type, obj), name);
  936. if(val != nullptr){
  937. if(is_tagged(val)) return val;
  938. if(val->type == tp_staticmethod) return PK_OBJ_GET(StaticMethod, val).func;
  939. if(val->type == tp_classmethod) return VAR(BoundMethod(obj, PK_OBJ_GET(ClassMethod, val).func));
  940. return val;
  941. }
  942. }else{
  943. val = obj->attr().try_get_likely_found(name);
  944. if(val != nullptr) return val;
  945. }
  946. }
  947. if(cls_var != nullptr){
  948. // bound method is non-data descriptor
  949. if(!is_tagged(cls_var)){
  950. switch(cls_var->type){
  951. case tp_function.index:
  952. return VAR(BoundMethod(obj, cls_var));
  953. case tp_native_func.index:
  954. return VAR(BoundMethod(obj, cls_var));
  955. case tp_staticmethod.index:
  956. return PK_OBJ_GET(StaticMethod, cls_var).func;
  957. case tp_classmethod.index:
  958. return VAR(BoundMethod(_t(objtype), PK_OBJ_GET(ClassMethod, cls_var).func));
  959. }
  960. }
  961. return cls_var;
  962. }
  963. const PyTypeInfo* ti = &_all_types[objtype];
  964. if(ti->m__getattr__){
  965. PyObject* ret = ti->m__getattr__(this, obj, name);
  966. if(ret) return ret;
  967. }
  968. if(throw_err) AttributeError(obj, name);
  969. return nullptr;
  970. }
  971. // used by OP_LOAD_METHOD
  972. // try to load a unbound method (fallback to `getattr` if not found)
  973. PyObject* VM::get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err, bool fallback){
  974. *self = PY_NULL;
  975. Type objtype(0);
  976. // handle super() proxy
  977. if(is_non_tagged_type(obj, tp_super)){
  978. const Super& super = PK_OBJ_GET(Super, obj);
  979. obj = super.first;
  980. objtype = super.second;
  981. }else{
  982. objtype = _tp(obj);
  983. }
  984. PyObject* cls_var = find_name_in_mro(objtype, name);
  985. if(fallback){
  986. if(cls_var != nullptr){
  987. // handle descriptor
  988. if(is_non_tagged_type(cls_var, tp_property)){
  989. const Property& prop = PK_OBJ_GET(Property, cls_var);
  990. return call(prop.getter, obj);
  991. }
  992. }
  993. // handle instance __dict__
  994. if(!is_tagged(obj) && obj->is_attr_valid()){
  995. PyObject* val;
  996. if(obj->type == tp_type){
  997. val = find_name_in_mro(PK_OBJ_GET(Type, obj), name);
  998. if(val != nullptr){
  999. if(is_tagged(val)) return val;
  1000. if(val->type == tp_staticmethod) return PK_OBJ_GET(StaticMethod, val).func;
  1001. if(val->type == tp_classmethod) return VAR(BoundMethod(obj, PK_OBJ_GET(ClassMethod, val).func));
  1002. return val;
  1003. }
  1004. }else{
  1005. val = obj->attr().try_get_likely_found(name);
  1006. if(val != nullptr) return val;
  1007. }
  1008. }
  1009. }
  1010. if(cls_var != nullptr){
  1011. if(!is_tagged(cls_var)){
  1012. switch(cls_var->type){
  1013. case tp_function.index:
  1014. *self = obj;
  1015. break;
  1016. case tp_native_func.index:
  1017. *self = obj;
  1018. break;
  1019. case tp_staticmethod.index:
  1020. *self = PY_NULL;
  1021. return PK_OBJ_GET(StaticMethod, cls_var).func;
  1022. case tp_classmethod.index:
  1023. *self = _t(objtype);
  1024. return PK_OBJ_GET(ClassMethod, cls_var).func;
  1025. }
  1026. }
  1027. return cls_var;
  1028. }
  1029. const PyTypeInfo* ti = &_all_types[objtype];
  1030. if(fallback && ti->m__getattr__){
  1031. PyObject* ret = ti->m__getattr__(this, obj, name);
  1032. if(ret) return ret;
  1033. }
  1034. if(throw_err) AttributeError(obj, name);
  1035. return nullptr;
  1036. }
  1037. void VM::setattr(PyObject* obj, StrName name, PyObject* value){
  1038. Type objtype(0);
  1039. // handle super() proxy
  1040. if(is_non_tagged_type(obj, tp_super)){
  1041. Super& super = PK_OBJ_GET(Super, obj);
  1042. obj = super.first;
  1043. objtype = super.second;
  1044. }else{
  1045. objtype = _tp(obj);
  1046. }
  1047. PyObject* cls_var = find_name_in_mro(objtype, name);
  1048. if(cls_var != nullptr){
  1049. // handle descriptor
  1050. if(is_non_tagged_type(cls_var, tp_property)){
  1051. const Property& prop = _CAST(Property&, cls_var);
  1052. if(prop.setter != vm->None){
  1053. call(prop.setter, obj, value);
  1054. }else{
  1055. TypeError(_S("readonly attribute: ", name.escape()));
  1056. }
  1057. return;
  1058. }
  1059. }
  1060. const PyTypeInfo* ti = &_all_types[objtype];
  1061. if(ti->m__setattr__){
  1062. ti->m__setattr__(this, obj, name, value);
  1063. return;
  1064. }
  1065. // handle instance __dict__
  1066. if(is_tagged(obj) || !obj->is_attr_valid()) TypeError("cannot set attribute");
  1067. obj->attr().set(name, value);
  1068. }
  1069. PyObject* VM::bind(PyObject* obj, const char* sig, NativeFuncC fn, UserData userdata, BindType bt){
  1070. return bind(obj, sig, nullptr, fn, userdata, bt);
  1071. }
  1072. PyObject* VM::bind(PyObject* obj, const char* sig, const char* docstring, NativeFuncC fn, UserData userdata, BindType bt){
  1073. CodeObject_ co;
  1074. try{
  1075. // fn(a, b, *c, d=1) -> None
  1076. co = compile(_S("def ", sig, " : pass"), "<bind>", EXEC_MODE);
  1077. }catch(const Exception&){
  1078. throw std::runtime_error("invalid signature: " + std::string(sig));
  1079. }
  1080. if(co->func_decls.size() != 1){
  1081. throw std::runtime_error("expected 1 function declaration");
  1082. }
  1083. FuncDecl_ decl = co->func_decls[0];
  1084. decl->signature = Str(sig);
  1085. if(docstring != nullptr){
  1086. decl->docstring = Str(docstring).strip();
  1087. }
  1088. PyObject* f_obj = VAR(NativeFunc(fn, decl));
  1089. PK_OBJ_GET(NativeFunc, f_obj).set_userdata(userdata);
  1090. switch(bt){
  1091. case BindType::STATICMETHOD:
  1092. f_obj = VAR(StaticMethod(f_obj));
  1093. break;
  1094. case BindType::CLASSMETHOD:
  1095. f_obj = VAR(ClassMethod(f_obj));
  1096. break;
  1097. case BindType::DEFAULT:
  1098. break;
  1099. }
  1100. if(obj != nullptr) obj->attr().set(decl->code->name, f_obj);
  1101. return f_obj;
  1102. }
  1103. PyObject* VM::bind_property(PyObject* obj, Str name, NativeFuncC fget, NativeFuncC fset){
  1104. PyObject* _0 = heap.gcnew<NativeFunc>(tp_native_func, fget, 1, false);
  1105. PyObject* _1 = vm->None;
  1106. if(fset != nullptr) _1 = heap.gcnew<NativeFunc>(tp_native_func, fset, 2, false);
  1107. Str signature = name;
  1108. int pos = name.index(":");
  1109. if(pos > 0) name = name.substr(0, pos).strip();
  1110. PyObject* prop = VAR(Property(_0, _1, signature));
  1111. obj->attr().set(name, prop);
  1112. return prop;
  1113. }
  1114. void VM::_builtin_error(StrName type){ _error(call(builtins->attr(type))); }
  1115. void VM::_builtin_error(StrName type, PyObject* arg){ _error(call(builtins->attr(type), arg)); }
  1116. void VM::_builtin_error(StrName type, const Str& msg){ _builtin_error(type, VAR(msg)); }
  1117. void VM::_error(PyObject* e_obj){
  1118. PK_ASSERT(isinstance(e_obj, tp_exception))
  1119. Exception& e = PK_OBJ_GET(Exception, e_obj);
  1120. if(callstack.empty()){
  1121. e.is_re = false;
  1122. throw e;
  1123. }
  1124. PUSH(e_obj);
  1125. _raise();
  1126. }
  1127. void VM::_raise(bool re_raise){
  1128. Frame* frame = top_frame();
  1129. Exception& e = PK_OBJ_GET(Exception, s_data.top());
  1130. if(!re_raise){
  1131. e._ip_on_error = frame->_ip;
  1132. e._code_on_error = (void*)frame->co;
  1133. }
  1134. bool ok = frame->jump_to_exception_handler(&s_data);
  1135. int actual_ip = frame->_ip;
  1136. if(e._ip_on_error >= 0 && e._code_on_error == (void*)frame->co) actual_ip = e._ip_on_error;
  1137. int current_line = frame->co->lines[actual_ip].lineno; // current line
  1138. auto current_f_name = frame->co->name.sv(); // current function name
  1139. if(frame->_callable == nullptr) current_f_name = ""; // not in a function
  1140. e.st_push(frame->co->src, current_line, nullptr, current_f_name);
  1141. if(ok) throw HandledException();
  1142. else throw UnhandledException();
  1143. }
  1144. void ManagedHeap::mark() {
  1145. for(PyObject* obj: _no_gc) PK_OBJ_MARK(obj);
  1146. vm->callstack.apply([](Frame& frame){ frame._gc_mark(); });
  1147. for(PyObject* obj: vm->s_data) PK_OBJ_MARK(obj);
  1148. for(auto [_, co]: vm->_cached_codes) co->_gc_mark();
  1149. if(vm->_last_exception) PK_OBJ_MARK(vm->_last_exception);
  1150. if(vm->_curr_class) PK_OBJ_MARK(vm->_curr_class);
  1151. if(vm->_c.error != nullptr) PK_OBJ_MARK(vm->_c.error);
  1152. if(_gc_marker_ex) _gc_marker_ex(vm);
  1153. }
  1154. StrName _type_name(VM *vm, Type type){
  1155. return vm->_all_types[type].name;
  1156. }
  1157. void VM::bind__getitem__(Type type, PyObject* (*f)(VM*, PyObject*, PyObject*)){
  1158. _all_types[type].m__getitem__ = f;
  1159. PyObject* nf = bind_method<1>(type, "__getitem__", [](VM* vm, ArgsView args){
  1160. return lambda_get_userdata<PyObject*(*)(VM*, PyObject*, PyObject*)>(args.begin())(vm, args[0], args[1]);
  1161. });
  1162. PK_OBJ_GET(NativeFunc, nf).set_userdata(f);
  1163. }
  1164. void VM::bind__setitem__(Type type, void (*f)(VM*, PyObject*, PyObject*, PyObject*)){
  1165. _all_types[type].m__setitem__ = f;
  1166. PyObject* nf = bind_method<2>(type, "__setitem__", [](VM* vm, ArgsView args){
  1167. lambda_get_userdata<void(*)(VM* vm, PyObject*, PyObject*, PyObject*)>(args.begin())(vm, args[0], args[1], args[2]);
  1168. return vm->None;
  1169. });
  1170. PK_OBJ_GET(NativeFunc, nf).set_userdata(f);
  1171. }
  1172. void VM::bind__delitem__(Type type, void (*f)(VM*, PyObject*, PyObject*)){
  1173. _all_types[type].m__delitem__ = f;
  1174. PyObject* nf = bind_method<1>(type, "__delitem__", [](VM* vm, ArgsView args){
  1175. lambda_get_userdata<void(*)(VM*, PyObject*, PyObject*)>(args.begin())(vm, args[0], args[1]);
  1176. return vm->None;
  1177. });
  1178. PK_OBJ_GET(NativeFunc, nf).set_userdata(f);
  1179. }
  1180. void VM::bind__hash__(Type type, i64 (*f)(VM*, PyObject*)){
  1181. PyObject* obj = _t(type);
  1182. _all_types[type].m__hash__ = f;
  1183. PyObject* nf = bind_method<0>(obj, "__hash__", [](VM* vm, ArgsView args){
  1184. i64 ret = lambda_get_userdata<i64(*)(VM*, PyObject*)>(args.begin())(vm, args[0]);
  1185. return VAR(ret);
  1186. });
  1187. PK_OBJ_GET(NativeFunc, nf).set_userdata(f);
  1188. }
  1189. void VM::bind__len__(Type type, i64 (*f)(VM*, PyObject*)){
  1190. PyObject* obj = _t(type);
  1191. _all_types[type].m__len__ = f;
  1192. PyObject* nf = bind_method<0>(obj, "__len__", [](VM* vm, ArgsView args){
  1193. i64 ret = lambda_get_userdata<i64(*)(VM*, PyObject*)>(args.begin())(vm, args[0]);
  1194. return VAR(ret);
  1195. });
  1196. PK_OBJ_GET(NativeFunc, nf).set_userdata(f);
  1197. }
  1198. void Dict::_probe_0(PyObject *key, bool &ok, int &i) const{
  1199. ok = false;
  1200. i64 hash = vm->py_hash(key);
  1201. i = hash & _mask;
  1202. // std::cout << CAST(Str, vm->py_repr(key)) << " " << hash << " " << i << std::endl;
  1203. for(int j=0; j<_capacity; j++) {
  1204. if(_items[i].first != nullptr){
  1205. if(vm->py_eq(_items[i].first, key)) { ok = true; break; }
  1206. }else{
  1207. if(_items[i].second == nullptr) break;
  1208. }
  1209. // https://github.com/python/cpython/blob/3.8/Objects/dictobject.c#L166
  1210. i = ((5*i) + 1) & _mask;
  1211. // std::cout << CAST(Str, vm->py_repr(key)) << " next: " << i << std::endl;
  1212. }
  1213. }
  1214. void Dict::_probe_1(PyObject *key, bool &ok, int &i) const{
  1215. ok = false;
  1216. i = vm->py_hash(key) & _mask;
  1217. while(_items[i].first != nullptr) {
  1218. if(vm->py_eq(_items[i].first, key)) { ok = true; break; }
  1219. // https://github.com/python/cpython/blob/3.8/Objects/dictobject.c#L166
  1220. i = ((5*i) + 1) & _mask;
  1221. }
  1222. }
  1223. void NativeFunc::check_size(VM* vm, ArgsView args) const{
  1224. if(args.size() != argc && argc != -1) {
  1225. vm->TypeError(_S("expected ", argc, " arguments, got ", args.size()));
  1226. }
  1227. }
  1228. PyObject* NativeFunc::call(VM *vm, ArgsView args) const {
  1229. return f(vm, args);
  1230. }
  1231. } // namespace pkpy