vm.h 35 KB

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