vm.cpp 47 KB

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