vm.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "iter.h"
  4. #include "error.h"
  5. #define __DEF_PY_AS_C(type, ctype, ptype) \
  6. inline ctype& Py##type##_AS_C(const PyVar& obj) { \
  7. __checkType(obj, ptype); \
  8. return std::get<ctype>(obj->_native); \
  9. }
  10. #define __DEF_PY(type, ctype, ptype) \
  11. inline PyVar Py##type(ctype value) { \
  12. return newObject(ptype, value); \
  13. }
  14. #define DEF_NATIVE(type, ctype, ptype) \
  15. __DEF_PY(type, ctype, ptype) \
  16. __DEF_PY_AS_C(type, ctype, ptype)
  17. #define __DEF_PY_POOL(name, ctype, ptype, max_size) \
  18. std::vector<PyObject*> _pool##name; \
  19. PyVar Py##name(ctype _native) { \
  20. PyObject* _raw = nullptr; \
  21. if(_pool##name.size() > 0) { \
  22. _raw = _pool##name.back(); \
  23. _raw->_native = std::move(_native); \
  24. _pool##name.pop_back(); \
  25. }else{ \
  26. __checkType(ptype, _tp_type); \
  27. _raw = new PyObject(std::move(_native));\
  28. _raw->setType(ptype); \
  29. } \
  30. return PyVar(_raw, [this](PyObject* p){ \
  31. if(_pool##name.size() < max_size){ \
  32. _pool##name.push_back(p); \
  33. }else{ \
  34. delete p; \
  35. } \
  36. }); \
  37. }
  38. typedef void(*PrintFn)(const VM*, const char*);
  39. class VM: public PkExportedResource{
  40. private:
  41. std::stack< std::unique_ptr<Frame> > callstack;
  42. PyVarDict _modules; // 3rd modules
  43. PyVar __py2py_call_signal;
  44. PyVar runFrame(Frame* frame){
  45. while(!frame->isCodeEnd()){
  46. const ByteCode& byte = frame->readCode();
  47. //printf("%s (%d) stack_size: %d\n", OP_NAMES[byte.op], byte.arg, frame->stackSize());
  48. switch (byte.op)
  49. {
  50. case OP_NO_OP: break; // do nothing
  51. case OP_LOAD_CONST: frame->push(frame->code->co_consts[byte.arg]); break;
  52. case OP_LOAD_LAMBDA: {
  53. PyVar obj = frame->code->co_consts[byte.arg];
  54. setAttr(obj, __module__, frame->_module);
  55. frame->push(obj);
  56. } break;
  57. case OP_LOAD_NAME_PTR: {
  58. frame->push(PyPointer(frame->code->co_names[byte.arg]));
  59. } break;
  60. case OP_STORE_NAME_PTR: {
  61. const auto& p = frame->code->co_names[byte.arg];
  62. p->set(this, frame, frame->popValue(this));
  63. } break;
  64. case OP_BUILD_ATTR_PTR: {
  65. const auto& attr = frame->code->co_names[byte.arg];
  66. PyVar obj = frame->popValue(this);
  67. frame->push(PyPointer(std::make_shared<AttrPointer>(obj, attr.get())));
  68. } break;
  69. case OP_BUILD_INDEX_PTR: {
  70. PyVar index = frame->popValue(this);
  71. PyVar obj = frame->popValue(this);
  72. frame->push(PyPointer(std::make_shared<IndexPointer>(obj, index)));
  73. } break;
  74. case OP_STORE_PTR: {
  75. PyVar obj = frame->popValue(this);
  76. const _Pointer& p = PyPointer_AS_C(frame->__pop());
  77. p->set(this, frame, std::move(obj));
  78. } break;
  79. case OP_DELETE_PTR: {
  80. const _Pointer& p = PyPointer_AS_C(frame->__pop());
  81. p->del(this, frame);
  82. } break;
  83. case OP_BUILD_SMART_TUPLE:
  84. {
  85. PyVarList items = frame->__popNReversed(byte.arg);
  86. bool done = false;
  87. for(auto& item : items){
  88. if(!item->isType(_tp_pointer)) {
  89. done = true;
  90. PyVarList values(items.size());
  91. for(int i=0; i<items.size(); i++){
  92. values[i] = frame->__deref_pointer(this, items[i]);
  93. }
  94. frame->push(PyTuple(values));
  95. break;
  96. }
  97. }
  98. if(done) break;
  99. std::vector<_Pointer> pointers(items.size());
  100. for(int i=0; i<items.size(); i++)
  101. pointers[i] = PyPointer_AS_C(items[i]);
  102. frame->push(PyPointer(std::make_shared<CompoundPointer>(pointers)));
  103. } break;
  104. case OP_BUILD_STRING:
  105. {
  106. PyVarList items = frame->popNValuesReversed(this, byte.arg);
  107. _StrStream ss;
  108. for(const auto& i : items) ss << PyStr_AS_C(asStr(i));
  109. frame->push(PyStr(ss.str()));
  110. } break;
  111. case OP_LOAD_EVAL_FN: {
  112. frame->push(builtins->attribs["eval"]);
  113. } break;
  114. case OP_LIST_APPEND: {
  115. PyVar obj = frame->popValue(this);
  116. PyVar list = frame->__topValueN(this, -2);
  117. fastCall(list, "append", {list, obj});
  118. } break;
  119. case OP_STORE_FUNCTION:
  120. {
  121. PyVar obj = frame->popValue(this);
  122. const _Func& fn = PyFunction_AS_C(obj);
  123. setAttr(obj, __module__, frame->_module);
  124. frame->f_globals()[fn->name] = obj;
  125. } break;
  126. case OP_BUILD_CLASS:
  127. {
  128. const _Str& clsName = frame->code->co_names[byte.arg]->name;
  129. PyVar clsBase = frame->popValue(this);
  130. if(clsBase == None) clsBase = _tp_object;
  131. __checkType(clsBase, _tp_type);
  132. PyVar cls = newUserClassType(clsName, clsBase);
  133. while(true){
  134. PyVar fn = frame->popValue(this);
  135. if(fn == None) break;
  136. const _Func& f = PyFunction_AS_C(fn);
  137. setAttr(fn, __module__, frame->_module);
  138. setAttr(cls, f->name, fn);
  139. }
  140. frame->f_globals()[clsName] = cls;
  141. } break;
  142. case OP_RETURN_VALUE: return frame->popValue(this);
  143. case OP_PRINT_EXPR:
  144. {
  145. const PyVar& expr = frame->topValue(this);
  146. if(expr == None) break;
  147. *_stdout << PyStr_AS_C(asRepr(expr)) << '\n';
  148. } break;
  149. case OP_POP_TOP: frame->popValue(this); break;
  150. case OP_BINARY_OP:
  151. {
  152. pkpy::ArgList args(2);
  153. args[1] = frame->popValue(this);
  154. args[0] = frame->popValue(this);
  155. frame->push(fastCall(args[0], BINARY_SPECIAL_METHODS[byte.arg], std::move(args)));
  156. } break;
  157. case OP_BITWISE_OP:
  158. {
  159. pkpy::ArgList args(2);
  160. args[1] = frame->popValue(this);
  161. args[0] = frame->popValue(this);
  162. frame->push(fastCall(args[0], BITWISE_SPECIAL_METHODS[byte.arg], std::move(args)));
  163. } break;
  164. case OP_COMPARE_OP:
  165. {
  166. pkpy::ArgList args(2);
  167. args[1] = frame->popValue(this);
  168. args[0] = frame->popValue(this);
  169. // for __ne__ we use the negation of __eq__
  170. int op = byte.arg == 3 ? 2 : byte.arg;
  171. PyVar res = fastCall(args[0], CMP_SPECIAL_METHODS[op], std::move(args));
  172. if(op != byte.arg) res = PyBool(!PyBool_AS_C(res));
  173. frame->push(std::move(res));
  174. } break;
  175. case OP_IS_OP:
  176. {
  177. bool ret_c = frame->popValue(this) == frame->popValue(this);
  178. if(byte.arg == 1) ret_c = !ret_c;
  179. frame->push(PyBool(ret_c));
  180. } break;
  181. case OP_CONTAINS_OP:
  182. {
  183. PyVar rhs = frame->popValue(this);
  184. PyVar lhs = frame->popValue(this);
  185. bool ret_c = PyBool_AS_C(call(std::move(rhs), __contains__, {std::move(lhs)}));
  186. if(byte.arg == 1) ret_c = !ret_c;
  187. frame->push(PyBool(ret_c));
  188. } break;
  189. case OP_UNARY_NEGATIVE:
  190. {
  191. PyVar obj = frame->popValue(this);
  192. frame->push(numNegated(obj));
  193. } break;
  194. case OP_UNARY_NOT:
  195. {
  196. PyVar obj = frame->popValue(this);
  197. const PyVar& obj_bool = asBool(obj);
  198. frame->push(PyBool(!PyBool_AS_C(obj_bool)));
  199. } break;
  200. case OP_POP_JUMP_IF_FALSE:
  201. if(!PyBool_AS_C(asBool(frame->popValue(this)))) frame->jump(byte.arg);
  202. break;
  203. case OP_LOAD_NONE: frame->push(None); break;
  204. case OP_LOAD_TRUE: frame->push(True); break;
  205. case OP_LOAD_FALSE: frame->push(False); break;
  206. case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); break;
  207. case OP_ASSERT:
  208. {
  209. PyVar expr = frame->popValue(this);
  210. _assert(PyBool_AS_C(expr), "assertion failed");
  211. } break;
  212. case OP_RAISE_ERROR:
  213. {
  214. _Str msg = PyStr_AS_C(asRepr(frame->popValue(this)));
  215. _Str type = PyStr_AS_C(frame->popValue(this));
  216. _error(type, msg);
  217. } break;
  218. case OP_BUILD_LIST:
  219. {
  220. PyVarList items = frame->popNValuesReversed(this, byte.arg);
  221. frame->push(PyList(items));
  222. } break;
  223. case OP_BUILD_MAP:
  224. {
  225. PyVarList items = frame->popNValuesReversed(this, byte.arg*2);
  226. PyVar obj = call(builtins->attribs["dict"], {});
  227. for(int i=0; i<items.size(); i+=2){
  228. call(obj, __setitem__, {items[i], items[i+1]});
  229. }
  230. frame->push(obj);
  231. } break;
  232. case OP_DUP_TOP: frame->push(frame->topValue(this)); break;
  233. case OP_CALL:
  234. {
  235. PyVarList args = frame->popNValuesReversed(this, byte.arg);
  236. PyVar callable = frame->popValue(this);
  237. PyVar ret = call(std::move(callable), std::move(args), true);
  238. if(ret == __py2py_call_signal) return ret;
  239. frame->push(std::move(ret));
  240. } break;
  241. case OP_JUMP_ABSOLUTE: frame->jump(byte.arg); break;
  242. case OP_SAFE_JUMP_ABSOLUTE: frame->safeJump(byte.arg); break;
  243. case OP_GOTO: {
  244. PyVar obj = frame->popValue(this);
  245. const _Str& label = PyStr_AS_C(obj);
  246. auto it = frame->code->co_labels.find(label);
  247. if(it == frame->code->co_labels.end()){
  248. _error("KeyError", "label '" + label + "' not found");
  249. }
  250. frame->safeJump(it->second);
  251. } break;
  252. case OP_GET_ITER:
  253. {
  254. PyVar obj = frame->popValue(this);
  255. PyVarOrNull iter_fn = getAttr(obj, __iter__, false);
  256. if(iter_fn != nullptr){
  257. PyVar tmp = call(iter_fn, {obj});
  258. PyIter_AS_C(tmp)->var = std::move(PyPointer_AS_C(frame->__pop()));
  259. frame->push(std::move(tmp));
  260. }else{
  261. typeError("'" + obj->getTypeName() + "' object is not iterable");
  262. }
  263. } break;
  264. case OP_FOR_ITER:
  265. {
  266. frame->__reportForIter();
  267. // __top() must be PyIter, so no need to __deref()
  268. auto& it = PyIter_AS_C(frame->__top());
  269. if(it->hasNext()){
  270. it->var->set(this, frame, it->next());
  271. }
  272. else{
  273. frame->safeJump(byte.arg);
  274. }
  275. } break;
  276. case OP_JUMP_IF_FALSE_OR_POP:
  277. {
  278. const PyVar& expr = frame->topValue(this);
  279. if(asBool(expr)==False) frame->jump(byte.arg);
  280. else frame->popValue(this);
  281. } break;
  282. case OP_JUMP_IF_TRUE_OR_POP:
  283. {
  284. const PyVar& expr = frame->topValue(this);
  285. if(asBool(expr)==True) frame->jump(byte.arg);
  286. else frame->popValue(this);
  287. } break;
  288. case OP_BUILD_SLICE:
  289. {
  290. PyVar stop = frame->popValue(this);
  291. PyVar start = frame->popValue(this);
  292. _Slice s;
  293. if(start != None) {__checkType(start, _tp_int); s.start = PyInt_AS_C(start);}
  294. if(stop != None) {__checkType(stop, _tp_int); s.stop = PyInt_AS_C(stop);}
  295. frame->push(PySlice(s));
  296. } break;
  297. case OP_IMPORT_NAME:
  298. {
  299. const _Str& name = frame->code->co_names[byte.arg]->name;
  300. auto it = _modules.find(name);
  301. if(it == _modules.end()) _error("ImportError", "module '" + name + "' not found");
  302. else frame->push(it->second);
  303. } break;
  304. default:
  305. systemError(_Str("opcode ") + OP_NAMES[byte.op] + " is not implemented");
  306. break;
  307. }
  308. }
  309. if(frame->code->src->mode == EVAL_MODE) {
  310. if(frame->stackSize() != 1) systemError("stack size is not 1 in EVAL_MODE");
  311. return frame->popValue(this);
  312. }
  313. if(frame->stackSize() != 0) systemError("stack not empty in EXEC_MODE");
  314. return None;
  315. }
  316. public:
  317. PyVarDict _types;
  318. PyVar None, True, False, Ellipsis;
  319. bool use_stdio;
  320. std::ostream* _stdout;
  321. std::ostream* _stderr;
  322. PyVar builtins; // builtins module
  323. PyVar _main; // __main__ module
  324. int maxRecursionDepth = 1000;
  325. VM(bool use_stdio){
  326. this->use_stdio = use_stdio;
  327. if(use_stdio){
  328. std::cout.setf(std::ios::unitbuf);
  329. std::cerr.setf(std::ios::unitbuf);
  330. this->_stdout = &std::cout;
  331. this->_stderr = &std::cerr;
  332. }else{
  333. this->_stdout = new _StrStream();
  334. this->_stderr = new _StrStream();
  335. }
  336. initializeBuiltinClasses();
  337. }
  338. PyVar asStr(const PyVar& obj){
  339. PyVarOrNull str_fn = getAttr(obj, __str__, false);
  340. if(str_fn != nullptr) return call(str_fn, {});
  341. return asRepr(obj);
  342. }
  343. Frame* topFrame(){
  344. if(callstack.size() == 0) UNREACHABLE();
  345. return callstack.top().get();
  346. }
  347. PyVar asRepr(const PyVar& obj){
  348. if(obj->isType(_tp_type)) return PyStr("<class '" + obj->getName() + "'>");
  349. return call(obj, __repr__, {});
  350. }
  351. PyVar asJson(const PyVar& obj){
  352. return call(obj, __json__, {});
  353. }
  354. const PyVar& asBool(const PyVar& obj){
  355. if(obj == None) return False;
  356. if(obj->_type == _tp_bool) return obj;
  357. if(obj->_type == _tp_int) return PyBool(PyInt_AS_C(obj) != 0);
  358. if(obj->_type == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0);
  359. PyVarOrNull len_fn = getAttr(obj, __len__, false);
  360. if(len_fn != nullptr){
  361. PyVar ret = call(std::move(len_fn), {});
  362. return PyBool(PyInt_AS_C(ret) > 0);
  363. }
  364. return True;
  365. }
  366. PyVar fastCall(const PyVar& obj, const _Str& name, pkpy::ArgList&& args){
  367. PyObject* cls = obj->_type.get();
  368. while(cls != None.get()) {
  369. auto it = cls->attribs.find(name);
  370. if(it != cls->attribs.end()){
  371. return call(it->second, args);
  372. }
  373. cls = cls->attribs[__base__].get();
  374. }
  375. attributeError(obj, name);
  376. return nullptr;
  377. }
  378. PyVar call(const PyVar& _callable, pkpy::ArgList args, bool opCall=false){
  379. if(_callable->isType(_tp_type)){
  380. auto it = _callable->attribs.find(__new__);
  381. PyVar obj;
  382. if(it != _callable->attribs.end()){
  383. obj = call(it->second, args);
  384. }else{
  385. obj = newObject(_callable, (_Int)-1);
  386. }
  387. if(obj->isType(_callable)){
  388. PyVarOrNull init_fn = getAttr(obj, __init__, false);
  389. if (init_fn != nullptr) call(init_fn, args);
  390. }
  391. return obj;
  392. }
  393. const PyVar* callable = &_callable;
  394. if((*callable)->isType(_tp_bounded_method)){
  395. auto& bm = PyBoundedMethod_AS_C((*callable));
  396. // TODO: avoid insertion here, bad performance
  397. pkpy::ArgList new_args(args.size()+1);
  398. new_args[0] = bm.obj;
  399. for(int i=0; i<args.size(); i++) new_args[i+1] = args[i];
  400. callable = &bm.method;
  401. args = std::move(new_args);
  402. }
  403. if((*callable)->isType(_tp_native_function)){
  404. const auto& f = std::get<_CppFunc>((*callable)->_native);
  405. return f(this, args);
  406. } else if((*callable)->isType(_tp_function)){
  407. const _Func& fn = PyFunction_AS_C((*callable));
  408. PyVarDict locals;
  409. int i = 0;
  410. for(const auto& name : fn->args){
  411. if(i < args.size()) {
  412. locals[name] = args[i++];
  413. }else{
  414. typeError("missing positional argument '" + name + "'");
  415. }
  416. }
  417. // handle *args
  418. if(!fn->starredArg.empty()){
  419. PyVarList vargs;
  420. while(i < args.size()) vargs.push_back(args[i++]);
  421. locals[fn->starredArg] = PyTuple(vargs);
  422. }
  423. // handle keyword arguments
  424. for(const _Str& name : fn->kwArgsOrder){
  425. if(i < args.size()) {
  426. locals[name] = args[i++];
  427. }else{
  428. locals[name] = fn->kwArgs[name];
  429. }
  430. }
  431. if(i < args.size()) typeError("too many arguments");
  432. auto it_m = (*callable)->attribs.find(__module__);
  433. PyVar _module = it_m != (*callable)->attribs.end() ? it_m->second : topFrame()->_module;
  434. if(opCall){
  435. __pushNewFrame(fn->code, _module, locals);
  436. return __py2py_call_signal;
  437. }
  438. return _exec(fn->code, _module, locals);
  439. }
  440. typeError("'" + (*callable)->getTypeName() + "' object is not callable");
  441. return None;
  442. }
  443. inline PyVar call(const PyVar& obj, const _Str& func, const pkpy::ArgList& args){
  444. return call(getAttr(obj, func), args);
  445. }
  446. inline PyVar call(const PyVar& obj, const _Str& func, pkpy::ArgList&& args){
  447. return call(getAttr(obj, func), args);
  448. }
  449. PyVarOrNull exec(const _Code& code, PyVar _module=nullptr){
  450. if(_module == nullptr) _module = _main;
  451. try {
  452. return _exec(code, _module);
  453. } catch (const std::exception& e) {
  454. if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
  455. *_stderr << e.what() << '\n';
  456. }else{
  457. auto re = RuntimeError("UnexpectedError", e.what(), _cleanErrorAndGetSnapshots());
  458. *_stderr << re.what() << '\n';
  459. }
  460. return nullptr;
  461. }
  462. }
  463. Frame* __pushNewFrame(const _Code& code, PyVar _module, const PyVarDict& locals){
  464. if(code == nullptr) UNREACHABLE();
  465. if(callstack.size() > maxRecursionDepth){
  466. throw RuntimeError("RecursionError", "maximum recursion depth exceeded", _cleanErrorAndGetSnapshots());
  467. }
  468. Frame* frame = new Frame(code.get(), _module, locals);
  469. callstack.push(std::unique_ptr<Frame>(frame));
  470. return frame;
  471. }
  472. PyVar _exec(const _Code& code, PyVar _module, const PyVarDict& locals={}){
  473. Frame* frame = __pushNewFrame(code, _module, locals);
  474. Frame* frameBase = frame;
  475. PyVar ret = nullptr;
  476. while(true){
  477. ret = runFrame(frame);
  478. if(ret != __py2py_call_signal){
  479. if(frame == frameBase){ // [ frameBase<- ]
  480. break;
  481. }else{
  482. callstack.pop();
  483. frame = callstack.top().get();
  484. frame->push(ret);
  485. }
  486. }else{
  487. frame = callstack.top().get(); // [ frameBase, newFrame<- ]
  488. }
  489. }
  490. callstack.pop();
  491. return ret;
  492. }
  493. PyVar newUserClassType(_Str name, PyVar base){
  494. PyVar obj = newClassType(name, base);
  495. setAttr(obj, __name__, PyStr(name));
  496. _types.erase(name);
  497. return obj;
  498. }
  499. PyVar newClassType(_Str name, PyVar base=nullptr) {
  500. if(base == nullptr) base = _tp_object;
  501. PyVar obj = std::make_shared<PyObject>((_Int)0);
  502. obj->setType(_tp_type);
  503. setAttr(obj, __base__, base);
  504. _types[name] = obj;
  505. return obj;
  506. }
  507. PyVar newObject(PyVar type, _Value _native) {
  508. __checkType(type, _tp_type);
  509. PyVar obj = std::make_shared<PyObject>(_native);
  510. obj->setType(type);
  511. return obj;
  512. }
  513. PyVar newModule(_Str name, bool saveToPath=true) {
  514. PyVar obj = newObject(_tp_module, (_Int)-2);
  515. setAttr(obj, __name__, PyStr(name));
  516. if(saveToPath) _modules[name] = obj;
  517. return obj;
  518. }
  519. PyVarOrNull getAttr(const PyVar& obj, const _Str& name, bool throw_err=true) {
  520. auto it = obj->attribs.find(name);
  521. if(it != obj->attribs.end()) return it->second;
  522. PyObject* cls = obj->_type.get();
  523. while(cls != None.get()) {
  524. it = cls->attribs.find(name);
  525. if(it != cls->attribs.end()){
  526. PyVar valueFromCls = it->second;
  527. if(valueFromCls->isType(_tp_function) || valueFromCls->isType(_tp_native_function)){
  528. return PyBoundedMethod({obj, valueFromCls});
  529. }else{
  530. return valueFromCls;
  531. }
  532. }
  533. cls = cls->attribs[__base__].get();
  534. }
  535. if(throw_err) attributeError(obj, name);
  536. return nullptr;
  537. }
  538. inline void setAttr(PyVar& obj, const _Str& name, const PyVar& value) {
  539. obj->attribs[name] = value;
  540. }
  541. inline void setAttr(PyVar& obj, const _Str& name, PyVar&& value) {
  542. obj->attribs[name] = std::move(value);
  543. }
  544. void bindMethod(_Str typeName, _Str funcName, _CppFunc fn) {
  545. funcName.intern();
  546. PyVar type = _types[typeName];
  547. PyVar func = PyNativeFunction(fn);
  548. setAttr(type, funcName, func);
  549. }
  550. void bindMethodMulti(std::vector<_Str> typeNames, _Str funcName, _CppFunc fn) {
  551. for(auto& typeName : typeNames){
  552. bindMethod(typeName, funcName, fn);
  553. }
  554. }
  555. void bindBuiltinFunc(_Str funcName, _CppFunc fn) {
  556. bindFunc(builtins, funcName, fn);
  557. }
  558. void bindFunc(PyVar module, _Str funcName, _CppFunc fn) {
  559. funcName.intern();
  560. __checkType(module, _tp_module);
  561. PyVar func = PyNativeFunction(fn);
  562. setAttr(module, funcName, func);
  563. }
  564. bool isInstance(PyVar obj, PyVar type){
  565. __checkType(type, _tp_type);
  566. PyVar t = obj->_type;
  567. while (t != None){
  568. if (t == type) return true;
  569. t = t->attribs[__base__];
  570. }
  571. return false;
  572. }
  573. inline bool isIntOrFloat(const PyVar& obj){
  574. return obj->isType(_tp_int) || obj->isType(_tp_float);
  575. }
  576. inline bool isIntOrFloat(const PyVar& obj1, const PyVar& obj2){
  577. return isIntOrFloat(obj1) && isIntOrFloat(obj2);
  578. }
  579. _Float numToFloat(const PyVar& obj){
  580. if (obj->isType(_tp_int)){
  581. return (_Float)PyInt_AS_C(obj);
  582. }else if(obj->isType(_tp_float)){
  583. return PyFloat_AS_C(obj);
  584. }
  585. UNREACHABLE();
  586. }
  587. PyVar numNegated(const PyVar& obj){
  588. if (obj->isType(_tp_int)){
  589. return PyInt(-PyInt_AS_C(obj));
  590. }else if(obj->isType(_tp_float)){
  591. return PyFloat(-PyFloat_AS_C(obj));
  592. }
  593. typeError("unsupported operand type(s) for -");
  594. return nullptr;
  595. }
  596. int normalizedIndex(int index, int size){
  597. if(index < 0) index += size;
  598. if(index < 0 || index >= size){
  599. indexError("index out of range, " + std::to_string(index) + " not in [0, " + std::to_string(size) + ")");
  600. }
  601. return index;
  602. }
  603. // for quick access
  604. PyVar _tp_object, _tp_type, _tp_int, _tp_float, _tp_bool, _tp_str;
  605. PyVar _tp_list, _tp_tuple;
  606. PyVar _tp_function, _tp_native_function, _tp_native_iterator, _tp_bounded_method;
  607. PyVar _tp_slice, _tp_range, _tp_module, _tp_pointer;
  608. __DEF_PY_POOL(Int, _Int, _tp_int, 256);
  609. __DEF_PY_AS_C(Int, _Int, _tp_int)
  610. __DEF_PY_POOL(Float, _Float, _tp_float, 256);
  611. __DEF_PY_AS_C(Float, _Float, _tp_float)
  612. __DEF_PY_POOL(Pointer, _Pointer, _tp_pointer, 512)
  613. __DEF_PY_AS_C(Pointer, _Pointer, _tp_pointer)
  614. DEF_NATIVE(Str, _Str, _tp_str)
  615. DEF_NATIVE(List, PyVarList, _tp_list)
  616. DEF_NATIVE(Tuple, PyVarList, _tp_tuple)
  617. DEF_NATIVE(Function, _Func, _tp_function)
  618. DEF_NATIVE(NativeFunction, _CppFunc, _tp_native_function)
  619. DEF_NATIVE(Iter, std::shared_ptr<_Iterator>, _tp_native_iterator)
  620. DEF_NATIVE(BoundedMethod, _BoundedMethod, _tp_bounded_method)
  621. DEF_NATIVE(Range, _Range, _tp_range)
  622. DEF_NATIVE(Slice, _Slice, _tp_slice)
  623. // there is only one True/False, so no need to copy them!
  624. inline bool PyBool_AS_C(const PyVar& obj){return obj == True;}
  625. inline const PyVar& PyBool(bool value){return value ? True : False;}
  626. void initializeBuiltinClasses(){
  627. _tp_object = std::make_shared<PyObject>((_Int)0);
  628. _tp_type = std::make_shared<PyObject>((_Int)0);
  629. _types["object"] = _tp_object;
  630. _types["type"] = _tp_type;
  631. _tp_bool = newClassType("bool");
  632. _tp_int = newClassType("int");
  633. _tp_float = newClassType("float");
  634. _tp_str = newClassType("str");
  635. _tp_list = newClassType("list");
  636. _tp_tuple = newClassType("tuple");
  637. _tp_slice = newClassType("slice");
  638. _tp_range = newClassType("range");
  639. _tp_module = newClassType("module");
  640. _tp_pointer = newClassType("_pointer");
  641. newClassType("NoneType");
  642. newClassType("ellipsis");
  643. _tp_function = newClassType("function");
  644. _tp_native_function = newClassType("_native_function");
  645. _tp_native_iterator = newClassType("_native_iterator");
  646. _tp_bounded_method = newClassType("_bounded_method");
  647. this->None = newObject(_types["NoneType"], (_Int)0);
  648. this->Ellipsis = newObject(_types["ellipsis"], (_Int)0);
  649. this->True = newObject(_tp_bool, true);
  650. this->False = newObject(_tp_bool, false);
  651. this->builtins = newModule("builtins");
  652. this->_main = newModule("__main__"_c, false);
  653. setAttr(_tp_type, __base__, _tp_object);
  654. _tp_type->setType(_tp_type);
  655. setAttr(_tp_object, __base__, None);
  656. _tp_object->setType(_tp_type);
  657. for (auto& [name, type] : _types) {
  658. setAttr(type, __name__, PyStr(name));
  659. }
  660. this->__py2py_call_signal = newObject(_tp_object, (_Int)7);
  661. std::vector<_Str> publicTypes = {"type", "object", "bool", "int", "float", "str", "list", "tuple", "range"};
  662. for (auto& name : publicTypes) {
  663. setAttr(builtins, name, _types[name]);
  664. }
  665. }
  666. _Int hash(const PyVar& obj){
  667. if (obj->isType(_tp_int)) return PyInt_AS_C(obj);
  668. if (obj->isType(_tp_bool)) return PyBool_AS_C(obj) ? 1 : 0;
  669. if (obj->isType(_tp_float)){
  670. _Float val = PyFloat_AS_C(obj);
  671. return (_Int)std::hash<_Float>()(val);
  672. }
  673. if (obj->isType(_tp_str)) return PyStr_AS_C(obj).hash();
  674. if (obj->isType(_tp_type)) return (_Int)obj.get();
  675. if (obj->isType(_tp_tuple)) {
  676. _Int x = 1000003;
  677. for (const auto& item : PyTuple_AS_C(obj)) {
  678. _Int y = hash(item);
  679. // this is recommended by Github Copilot
  680. // i am not sure whether it is a good idea
  681. x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 2));
  682. }
  683. return x;
  684. }
  685. typeError("unhashable type: " + obj->getTypeName());
  686. return 0;
  687. }
  688. /***** Error Reporter *****/
  689. private:
  690. void _error(const _Str& name, const _Str& msg){
  691. throw RuntimeError(name, msg, _cleanErrorAndGetSnapshots());
  692. }
  693. std::stack<_Str> _cleanErrorAndGetSnapshots(){
  694. std::stack<_Str> snapshots;
  695. while (!callstack.empty()){
  696. if(snapshots.size() < 8){
  697. snapshots.push(callstack.top()->errorSnapshot());
  698. }
  699. callstack.pop();
  700. }
  701. return snapshots;
  702. }
  703. public:
  704. void typeError(const _Str& msg){
  705. _error("TypeError", msg);
  706. }
  707. void systemError(const _Str& msg){
  708. _error("SystemError", msg);
  709. }
  710. void zeroDivisionError(){
  711. _error("ZeroDivisionError", "division by zero");
  712. }
  713. void indexError(const _Str& msg){
  714. _error("IndexError", msg);
  715. }
  716. void valueError(const _Str& msg){
  717. _error("ValueError", msg);
  718. }
  719. void nameError(const _Str& name){
  720. _error("NameError", "name '" + name + "' is not defined");
  721. }
  722. void attributeError(PyVar obj, const _Str& name){
  723. _error("AttributeError", "type '" + obj->getTypeName() + "' has no attribute '" + name + "'");
  724. }
  725. inline void __checkType(const PyVar& obj, const PyVar& type){
  726. if(!obj->isType(type)) typeError("expected '" + type->getName() + "', but got '" + obj->getTypeName() + "'");
  727. }
  728. inline void __checkArgSize(const pkpy::ArgList& args, int size, bool method=false){
  729. if(args.size() == size) return;
  730. if(method) typeError(args.size()>size ? "too many arguments" : "too few arguments");
  731. else typeError("expected " + std::to_string(size) + " arguments, but got " + std::to_string(args.size()));
  732. }
  733. void _assert(bool val, const _Str& msg){
  734. if (!val) _error("AssertionError", msg);
  735. }
  736. virtual ~VM() {
  737. if(!use_stdio){
  738. delete _stdout;
  739. delete _stderr;
  740. }
  741. }
  742. };
  743. /***** Pointers' Impl *****/
  744. PyVar NamePointer::get(VM* vm, Frame* frame) const{
  745. auto it = frame->f_locals.find(name);
  746. if(it != frame->f_locals.end()) return it->second;
  747. it = frame->f_globals().find(name);
  748. if(it != frame->f_globals().end()) return it->second;
  749. it = vm->builtins->attribs.find(name);
  750. if(it != vm->builtins->attribs.end()) return it->second;
  751. vm->nameError(name);
  752. return nullptr;
  753. }
  754. void NamePointer::set(VM* vm, Frame* frame, PyVar val) const{
  755. switch(scope) {
  756. case NAME_LOCAL: frame->f_locals[name] = std::move(val); break;
  757. case NAME_GLOBAL:
  758. {
  759. if(frame->f_locals.count(name) > 0){
  760. frame->f_locals[name] = std::move(val);
  761. }else{
  762. frame->f_globals()[name] = std::move(val);
  763. }
  764. } break;
  765. default: UNREACHABLE();
  766. }
  767. }
  768. void NamePointer::del(VM* vm, Frame* frame) const{
  769. switch(scope) {
  770. case NAME_LOCAL: {
  771. if(frame->f_locals.count(name) > 0){
  772. frame->f_locals.erase(name);
  773. }else{
  774. vm->nameError(name);
  775. }
  776. } break;
  777. case NAME_GLOBAL:
  778. {
  779. if(frame->f_locals.count(name) > 0){
  780. frame->f_locals.erase(name);
  781. }else{
  782. if(frame->f_globals().count(name) > 0){
  783. frame->f_globals().erase(name);
  784. }else{
  785. vm->nameError(name);
  786. }
  787. }
  788. } break;
  789. default: UNREACHABLE();
  790. }
  791. }
  792. PyVar AttrPointer::get(VM* vm, Frame* frame) const{
  793. return vm->getAttr(obj, attr->name);
  794. }
  795. void AttrPointer::set(VM* vm, Frame* frame, PyVar val) const{
  796. vm->setAttr(obj, attr->name, val);
  797. }
  798. void AttrPointer::del(VM* vm, Frame* frame) const{
  799. vm->typeError("cannot delete attribute");
  800. }
  801. PyVar IndexPointer::get(VM* vm, Frame* frame) const{
  802. return vm->call(obj, __getitem__, {index});
  803. }
  804. void IndexPointer::set(VM* vm, Frame* frame, PyVar val) const{
  805. vm->call(obj, __setitem__, {index, val});
  806. }
  807. void IndexPointer::del(VM* vm, Frame* frame) const{
  808. vm->call(obj, __delitem__, {index});
  809. }
  810. PyVar CompoundPointer::get(VM* vm, Frame* frame) const{
  811. PyVarList args(pointers.size());
  812. for (int i = 0; i < pointers.size(); i++) {
  813. args[i] = pointers[i]->get(vm, frame);
  814. }
  815. return vm->PyTuple(args);
  816. }
  817. void CompoundPointer::set(VM* vm, Frame* frame, PyVar val) const{
  818. if(!val->isType(vm->_tp_tuple) && !val->isType(vm->_tp_list)){
  819. vm->typeError("only tuple or list can be unpacked");
  820. }
  821. const PyVarList& args = std::get<PyVarList>(val->_native);
  822. if(args.size() > pointers.size()) vm->valueError("too many values to unpack");
  823. if(args.size() < pointers.size()) vm->valueError("not enough values to unpack");
  824. for (int i = 0; i < pointers.size(); i++) {
  825. pointers[i]->set(vm, frame, args[i]);
  826. }
  827. }
  828. void CompoundPointer::del(VM* vm, Frame* frame) const{
  829. for (auto& ptr : pointers) ptr->del(vm, frame);
  830. }
  831. /***** Frame's Impl *****/
  832. inline PyVar Frame::__deref_pointer(VM* vm, PyVar v){
  833. if(v->isType(vm->_tp_pointer)) return vm->PyPointer_AS_C(v)->get(vm, this);
  834. return v;
  835. }
  836. /***** Iterators' Impl *****/
  837. PyVar RangeIterator::next(){
  838. PyVar val = vm->PyInt(current);
  839. current += r.step;
  840. return val;
  841. }
  842. PyVar StringIterator::next(){
  843. return vm->PyStr(str.u8_getitem(index++));
  844. }
  845. enum ThreadState {
  846. THREAD_READY,
  847. THREAD_RUNNING,
  848. THREAD_SUSPENDED,
  849. THREAD_FINISHED
  850. };
  851. class ThreadedVM : public VM {
  852. std::thread* _thread = nullptr;
  853. std::atomic<ThreadState> state = THREAD_READY;
  854. public:
  855. ThreadedVM(bool use_stdio) : VM(use_stdio) {}
  856. _Str _stdin;
  857. void suspend(){
  858. if(_thread == nullptr) UNREACHABLE();
  859. if(state != THREAD_RUNNING) UNREACHABLE();
  860. state = THREAD_SUSPENDED;
  861. // 50 fps is enough
  862. while(state == THREAD_SUSPENDED) std::this_thread::sleep_for(std::chrono::milliseconds(20));
  863. }
  864. _Str readStdin(){
  865. if(_thread == nullptr) UNREACHABLE();
  866. _Str copy = _stdin;
  867. _stdin = "";
  868. return copy;
  869. }
  870. /***** For outer use *****/
  871. ThreadState getState(){
  872. return state.load();
  873. }
  874. void resume(){
  875. if(_thread == nullptr) UNREACHABLE();
  876. if(state != THREAD_SUSPENDED) UNREACHABLE();
  877. state = THREAD_RUNNING;
  878. }
  879. void startExec(const _Code& code){
  880. if(_thread != nullptr) UNREACHABLE();
  881. if(state != THREAD_READY) UNREACHABLE();
  882. _thread = new std::thread([this, code](){
  883. this->state = THREAD_RUNNING;
  884. this->exec(code);
  885. this->state = THREAD_FINISHED;
  886. });
  887. }
  888. ~ThreadedVM(){
  889. if(_thread != nullptr){
  890. _thread->join();
  891. delete _thread;
  892. }
  893. }
  894. };