vm.cpp 47 KB

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