vm.h 34 KB

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