vm.h 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. #pragma once
  2. #include "codeobject.h"
  3. #include "error.h"
  4. #define DEF_NATIVE(type, ctype, ptype) \
  5. inline ctype& Py##type##_AS_C(const PyVar& obj) { \
  6. check_type(obj, ptype); \
  7. return OBJ_GET(ctype, obj); \
  8. } \
  9. inline PyVar Py##type(ctype value) { \
  10. return new_object(ptype, value); \
  11. }
  12. // static std::map<Str, int> _stats;
  13. class VM {
  14. std::stack< std::unique_ptr<Frame> > callstack;
  15. PyVar _py_op_call;
  16. // PyVar _ascii_str_pool[128];
  17. PyVar run_frame(Frame* frame){
  18. while(frame->has_next_bytecode()){
  19. const Bytecode& byte = frame->next_bytecode();
  20. // if(true || frame->_module != builtins){
  21. // printf("%d: %s (%d) %s\n", frame->_ip, OP_NAMES[byte.op], byte.arg, frame->stack_info().c_str());
  22. // }
  23. switch (byte.op)
  24. {
  25. case OP_NO_OP: break; // do nothing
  26. case OP_LOAD_CONST: frame->push(frame->co->consts[byte.arg]); break;
  27. case OP_LOAD_LAMBDA: {
  28. PyVar obj = frame->co->consts[byte.arg];
  29. setattr(obj, __module__, frame->_module);
  30. frame->push(obj);
  31. } break;
  32. case OP_LOAD_NAME_REF: {
  33. frame->push(PyRef(NameRef(frame->co->names[byte.arg])));
  34. } break;
  35. case OP_LOAD_NAME: {
  36. frame->push(NameRef(frame->co->names[byte.arg]).get(this, frame));
  37. } break;
  38. case OP_STORE_NAME: {
  39. auto& p = frame->co->names[byte.arg];
  40. NameRef(p).set(this, frame, frame->pop_value(this));
  41. } break;
  42. case OP_BUILD_ATTR: {
  43. int name = byte.arg >> 1;
  44. bool _rvalue = byte.arg % 2 == 1;
  45. auto& attr = frame->co->names[name];
  46. PyVar obj = frame->pop_value(this);
  47. AttrRef ref = AttrRef(obj, NameRef(attr));
  48. if(_rvalue) frame->push(ref.get(this, frame));
  49. else frame->push(PyRef(ref));
  50. } break;
  51. case OP_BUILD_INDEX: {
  52. PyVar index = frame->pop_value(this);
  53. auto ref = IndexRef(frame->pop_value(this), index);
  54. if(byte.arg == 0) frame->push(PyRef(ref));
  55. else frame->push(ref.get(this, frame));
  56. } break;
  57. case OP_STORE_REF: {
  58. PyVar obj = frame->pop_value(this);
  59. PyVarRef r = frame->pop();
  60. PyRef_AS_C(r)->set(this, frame, std::move(obj));
  61. } break;
  62. case OP_DELETE_REF: {
  63. PyVarRef r = frame->pop();
  64. PyRef_AS_C(r)->del(this, frame);
  65. } break;
  66. case OP_BUILD_SMART_TUPLE:
  67. {
  68. pkpy::Args items = frame->pop_n_reversed(byte.arg);
  69. bool done = false;
  70. for(int i=0; i<items.size(); i++){
  71. if(!items[i]->is_type(tp_ref)) {
  72. done = true;
  73. for(int j=i; j<items.size(); j++) frame->try_deref(this, items[j]);
  74. frame->push(PyTuple(std::move(items)));
  75. break;
  76. }
  77. }
  78. if(done) break;
  79. frame->push(PyRef(TupleRef(std::move(items))));
  80. } break;
  81. case OP_BUILD_STRING:
  82. {
  83. pkpy::Args items = frame->pop_n_values_reversed(this, byte.arg);
  84. StrStream ss;
  85. for(int i=0; i<items.size(); i++) ss << PyStr_AS_C(asStr(items[i]));
  86. frame->push(PyStr(ss.str()));
  87. } break;
  88. case OP_LOAD_EVAL_FN: {
  89. frame->push(builtins->attr(m_eval));
  90. } break;
  91. case OP_LIST_APPEND: {
  92. pkpy::Args args(2);
  93. args[1] = frame->pop_value(this); // obj
  94. args[0] = frame->top_value_offset(this, -2); // list
  95. fast_call(m_append, std::move(args));
  96. } break;
  97. case OP_STORE_FUNCTION:
  98. {
  99. PyVar obj = frame->pop_value(this);
  100. const pkpy::Function_& fn = PyFunction_AS_C(obj);
  101. setattr(obj, __module__, frame->_module);
  102. frame->f_globals()[fn->name] = obj;
  103. } break;
  104. case OP_BUILD_CLASS:
  105. {
  106. const Str& clsName = frame->co->names[byte.arg].first;
  107. PyVar clsBase = frame->pop_value(this);
  108. if(clsBase == None) clsBase = _t(tp_object);
  109. check_type(clsBase, tp_type);
  110. PyVar cls = new_type_object(frame->_module, clsName, clsBase);
  111. while(true){
  112. PyVar fn = frame->pop_value(this);
  113. if(fn == None) break;
  114. const pkpy::Function_& f = PyFunction_AS_C(fn);
  115. setattr(fn, __module__, frame->_module);
  116. setattr(cls, f->name, fn);
  117. }
  118. } break;
  119. case OP_RETURN_VALUE: return frame->pop_value(this);
  120. case OP_PRINT_EXPR:
  121. {
  122. const PyVar expr = frame->top_value(this);
  123. if(expr == None) break;
  124. *_stdout << PyStr_AS_C(asRepr(expr)) << '\n';
  125. } break;
  126. case OP_POP_TOP: frame->_pop(); break;
  127. case OP_BINARY_OP:
  128. {
  129. pkpy::Args args(2);
  130. args[1] = frame->pop_value(this);
  131. args[0] = frame->top_value(this);
  132. frame->top() = fast_call(BINARY_SPECIAL_METHODS[byte.arg], std::move(args));
  133. } break;
  134. case OP_BITWISE_OP:
  135. {
  136. frame->push(
  137. fast_call(BITWISE_SPECIAL_METHODS[byte.arg],
  138. frame->pop_n_values_reversed(this, 2))
  139. );
  140. } break;
  141. case OP_COMPARE_OP:
  142. {
  143. pkpy::Args args(2);
  144. args[1] = frame->pop_value(this);
  145. args[0] = frame->top_value(this);
  146. frame->top() = fast_call(CMP_SPECIAL_METHODS[byte.arg], std::move(args));
  147. } break;
  148. case OP_IS_OP:
  149. {
  150. PyVar rhs = frame->pop_value(this);
  151. bool ret_c = rhs == frame->top_value(this);
  152. if(byte.arg == 1) ret_c = !ret_c;
  153. frame->top() = PyBool(ret_c);
  154. } break;
  155. case OP_CONTAINS_OP:
  156. {
  157. PyVar rhs = frame->pop_value(this);
  158. bool ret_c = PyBool_AS_C(call(rhs, __contains__, pkpy::one_arg(frame->pop_value(this))));
  159. if(byte.arg == 1) ret_c = !ret_c;
  160. frame->push(PyBool(ret_c));
  161. } break;
  162. case OP_UNARY_NEGATIVE:
  163. frame->top() = num_negated(frame->top_value(this));
  164. break;
  165. case OP_UNARY_NOT:
  166. {
  167. PyVar obj = frame->pop_value(this);
  168. const PyVar& obj_bool = asBool(obj);
  169. frame->push(PyBool(!PyBool_AS_C(obj_bool)));
  170. } break;
  171. case OP_POP_JUMP_IF_FALSE:
  172. if(!PyBool_AS_C(asBool(frame->pop_value(this)))) frame->jump_abs(byte.arg);
  173. break;
  174. case OP_LOAD_NONE: frame->push(None); break;
  175. case OP_LOAD_TRUE: frame->push(True); break;
  176. case OP_LOAD_FALSE: frame->push(False); break;
  177. case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); break;
  178. case OP_ASSERT:
  179. {
  180. PyVar _msg = frame->pop_value(this);
  181. Str msg = PyStr_AS_C(asStr(_msg));
  182. PyVar expr = frame->pop_value(this);
  183. if(asBool(expr) != True) _error("AssertionError", msg);
  184. } break;
  185. case OP_EXCEPTION_MATCH:
  186. {
  187. const auto& _e = PyException_AS_C(frame->top());
  188. Str name = frame->co->names[byte.arg].first;
  189. frame->push(PyBool(_e.match_type(name)));
  190. } break;
  191. case OP_RAISE:
  192. {
  193. PyVar obj = frame->pop_value(this);
  194. Str msg = obj == None ? "" : PyStr_AS_C(asStr(obj));
  195. Str type = frame->co->names[byte.arg].first;
  196. _error(type, msg);
  197. } break;
  198. case OP_RE_RAISE: _raise(); break;
  199. case OP_BUILD_LIST:
  200. frame->push(PyList(
  201. frame->pop_n_values_reversed(this, byte.arg).to_list()));
  202. break;
  203. case OP_BUILD_MAP:
  204. {
  205. pkpy::Args items = frame->pop_n_values_reversed(this, byte.arg*2);
  206. PyVar obj = call(builtins->attr("dict"));
  207. for(int i=0; i<items.size(); i+=2){
  208. call(obj, __setitem__, pkpy::two_args(items[i], items[i+1]));
  209. }
  210. frame->push(obj);
  211. } break;
  212. case OP_BUILD_SET:
  213. {
  214. PyVar list = PyList(
  215. frame->pop_n_values_reversed(this, byte.arg).to_list()
  216. );
  217. PyVar obj = call(builtins->attr("set"), pkpy::one_arg(list));
  218. frame->push(obj);
  219. } break;
  220. case OP_DUP_TOP: frame->push(frame->top_value(this)); break;
  221. case OP_CALL:
  222. {
  223. int ARGC = byte.arg & 0xFFFF;
  224. int KWARGC = (byte.arg >> 16) & 0xFFFF;
  225. pkpy::Args kwargs(0);
  226. if(KWARGC > 0) kwargs = frame->pop_n_values_reversed(this, KWARGC*2);
  227. pkpy::Args args = frame->pop_n_values_reversed(this, ARGC);
  228. PyVar callable = frame->pop_value(this);
  229. PyVar ret = call(callable, std::move(args), kwargs, true);
  230. if(ret == _py_op_call) return ret;
  231. frame->push(std::move(ret));
  232. } break;
  233. case OP_JUMP_ABSOLUTE: frame->jump_abs(byte.arg); break;
  234. case OP_SAFE_JUMP_ABSOLUTE: frame->jump_abs_safe(byte.arg); break;
  235. case OP_GOTO: {
  236. const Str& label = frame->co->names[byte.arg].first;
  237. int* target = frame->co->labels.try_get(label);
  238. if(target == nullptr) _error("KeyError", "label '" + label + "' not found");
  239. frame->jump_abs_safe(*target);
  240. } break;
  241. case OP_GET_ITER:
  242. {
  243. PyVar obj = frame->pop_value(this);
  244. PyVarOrNull iter_fn = getattr(obj, __iter__, false);
  245. if(iter_fn != nullptr){
  246. PyVar tmp = call(iter_fn);
  247. PyVarRef var = frame->pop();
  248. check_type(var, tp_ref);
  249. PyIter_AS_C(tmp)->var = var;
  250. frame->push(std::move(tmp));
  251. }else{
  252. TypeError(OBJ_NAME(_t(obj)).escape(true) + " object is not iterable");
  253. }
  254. } break;
  255. case OP_FOR_ITER:
  256. {
  257. // top() must be PyIter, so no need to try_deref()
  258. auto& it = PyIter_AS_C(frame->top());
  259. if(it->has_next()){
  260. PyRef_AS_C(it->var)->set(this, frame, it->next());
  261. }else{
  262. int blockEnd = frame->co->blocks[byte.block].end;
  263. frame->jump_abs_safe(blockEnd);
  264. }
  265. } break;
  266. case OP_LOOP_CONTINUE:
  267. {
  268. int blockStart = frame->co->blocks[byte.block].start;
  269. frame->jump_abs(blockStart);
  270. } break;
  271. case OP_LOOP_BREAK:
  272. {
  273. int blockEnd = frame->co->blocks[byte.block].end;
  274. frame->jump_abs_safe(blockEnd);
  275. } break;
  276. case OP_JUMP_IF_FALSE_OR_POP:
  277. {
  278. const PyVar expr = frame->top_value(this);
  279. if(asBool(expr)==False) frame->jump_abs(byte.arg);
  280. else frame->pop_value(this);
  281. } break;
  282. case OP_JUMP_IF_TRUE_OR_POP:
  283. {
  284. const PyVar expr = frame->top_value(this);
  285. if(asBool(expr)==True) frame->jump_abs(byte.arg);
  286. else frame->pop_value(this);
  287. } break;
  288. case OP_BUILD_SLICE:
  289. {
  290. PyVar stop = frame->pop_value(this);
  291. PyVar start = frame->pop_value(this);
  292. pkpy::Slice s;
  293. if(start != None) {check_type(start, tp_int); s.start = (int)PyInt_AS_C(start);}
  294. if(stop != None) {check_type(stop, tp_int); s.stop = (int)PyInt_AS_C(stop);}
  295. frame->push(PySlice(s));
  296. } break;
  297. case OP_IMPORT_NAME:
  298. {
  299. const Str& name = frame->co->names[byte.arg].first;
  300. auto it = _modules.find(name);
  301. if(it == _modules.end()){
  302. auto it2 = _lazy_modules.find(name);
  303. if(it2 == _lazy_modules.end()){
  304. _error("ImportError", "module '" + name + "' not found");
  305. }else{
  306. const Str& source = it2->second;
  307. CodeObject_ code = compile(source, name, EXEC_MODE);
  308. PyVar _m = new_module(name);
  309. _exec(code, _m, pkpy::make_shared<pkpy::NameDict>());
  310. frame->push(_m);
  311. _lazy_modules.erase(it2);
  312. }
  313. }else{
  314. frame->push(it->second);
  315. }
  316. } break;
  317. // TODO: using "goto" inside with block may cause __exit__ not called
  318. case OP_WITH_ENTER: call(frame->pop_value(this), __enter__); break;
  319. case OP_WITH_EXIT: call(frame->pop_value(this), __exit__); break;
  320. case OP_TRY_BLOCK_ENTER: frame->on_try_block_enter(); break;
  321. case OP_TRY_BLOCK_EXIT: frame->on_try_block_exit(); break;
  322. default:
  323. throw std::runtime_error(Str("opcode ") + OP_NAMES[byte.op] + " is not implemented");
  324. break;
  325. }
  326. }
  327. if(frame->co->src->mode == EVAL_MODE || frame->co->src->mode == JSON_MODE){
  328. if(frame->_data.size() != 1) throw std::runtime_error("_data.size() != 1 in EVAL/JSON_MODE");
  329. return frame->pop_value(this);
  330. }
  331. if(!frame->_data.empty()) throw std::runtime_error("_data.size() != 0 in EXEC_MODE");
  332. return None;
  333. }
  334. public:
  335. pkpy::NameDict _types;
  336. pkpy::NameDict _modules; // loaded modules
  337. emhash8::HashMap<Str, Str> _lazy_modules; // lazy loaded modules
  338. PyVar None, True, False, Ellipsis;
  339. bool use_stdio;
  340. std::ostream* _stdout;
  341. std::ostream* _stderr;
  342. PyVar builtins; // builtins module
  343. PyVar _main; // __main__ module
  344. int maxRecursionDepth = 1000;
  345. VM(bool use_stdio){
  346. this->use_stdio = use_stdio;
  347. if(use_stdio){
  348. this->_stdout = &std::cout;
  349. this->_stderr = &std::cerr;
  350. }else{
  351. this->_stdout = new StrStream();
  352. this->_stderr = new StrStream();
  353. }
  354. init_builtin_types();
  355. // for(int i=0; i<128; i++) _ascii_str_pool[i] = new_object(tp_str, std::string(1, (char)i));
  356. }
  357. PyVar asStr(const PyVar& obj){
  358. PyVarOrNull f = getattr(obj, __str__, false);
  359. if(f != nullptr) return call(f);
  360. return asRepr(obj);
  361. }
  362. inline Frame* top_frame() const {
  363. if(callstack.empty()) UNREACHABLE();
  364. return callstack.top().get();
  365. }
  366. PyVar asRepr(const PyVar& obj){
  367. if(obj->is_type(tp_type)) return PyStr("<class '" + OBJ_GET(Str, obj->attr(__name__)) + "'>");
  368. return call(obj, __repr__);
  369. }
  370. const PyVar& asBool(const PyVar& obj){
  371. if(obj->is_type(tp_bool)) return obj;
  372. if(obj == None) return False;
  373. if(obj->is_type(tp_int)) return PyBool(PyInt_AS_C(obj) != 0);
  374. if(obj->is_type(tp_float)) return PyBool(PyFloat_AS_C(obj) != 0.0);
  375. PyVarOrNull len_fn = getattr(obj, __len__, false);
  376. if(len_fn != nullptr){
  377. PyVar ret = call(len_fn);
  378. return PyBool(PyInt_AS_C(ret) > 0);
  379. }
  380. return True;
  381. }
  382. PyVar fast_call(const Str& name, pkpy::Args&& args){
  383. PyObject* cls = _t(args[0]).get();
  384. while(cls != None.get()) {
  385. PyVar* val = cls->attr().try_get(name);
  386. if(val != nullptr) return call(*val, std::move(args));
  387. cls = cls->attr(__base__).get();
  388. }
  389. AttributeError(args[0], name);
  390. return nullptr;
  391. }
  392. inline PyVar call(const PyVar& _callable){
  393. return call(_callable, pkpy::no_arg(), pkpy::no_arg(), false);
  394. }
  395. template<typename ArgT>
  396. inline std::enable_if_t<std::is_same_v<std::remove_const_t<std::remove_reference_t<ArgT>>, pkpy::Args>, PyVar>
  397. call(const PyVar& _callable, ArgT&& args){
  398. return call(_callable, std::forward<ArgT>(args), pkpy::no_arg(), false);
  399. }
  400. template<typename ArgT>
  401. inline std::enable_if_t<std::is_same_v<std::remove_const_t<std::remove_reference_t<ArgT>>, pkpy::Args>, PyVar>
  402. call(const PyVar& obj, const Str& func, ArgT&& args){
  403. return call(getattr(obj, func), std::forward<ArgT>(args), pkpy::no_arg(), false);
  404. }
  405. inline PyVar call(const PyVar& obj, const Str& func){
  406. return call(getattr(obj, func), pkpy::no_arg(), pkpy::no_arg(), false);
  407. }
  408. PyVar call(const PyVar& _callable, pkpy::Args args, const pkpy::Args& kwargs, bool opCall){
  409. if(_callable->is_type(tp_type)){
  410. PyVar* new_f = _callable->attr().try_get(__new__);
  411. PyVar obj;
  412. if(new_f != nullptr){
  413. obj = call(*new_f, args, kwargs, false);
  414. }else{
  415. obj = new_object(_callable, DUMMY_VAL);
  416. PyVarOrNull init_f = getattr(obj, __init__, false);
  417. if (init_f != nullptr) call(init_f, args, kwargs, false);
  418. }
  419. return obj;
  420. }
  421. const PyVar* callable = &_callable;
  422. if((*callable)->is_type(tp_bound_method)){
  423. auto& bm = PyBoundMethod_AS_C((*callable));
  424. callable = &bm.method; // get unbound method
  425. args.extend_self(bm.obj);
  426. }
  427. if((*callable)->is_type(tp_native_function)){
  428. const auto& f = OBJ_GET(pkpy::NativeFunc, *callable);
  429. if(kwargs.size() != 0) TypeError("native_function does not accept keyword arguments");
  430. return f(this, args);
  431. } else if((*callable)->is_type(tp_function)){
  432. const pkpy::Function_& fn = PyFunction_AS_C((*callable));
  433. // pkpy::_stats[fn->name] += 1;
  434. pkpy::shared_ptr<pkpy::NameDict> _locals = pkpy::make_shared<pkpy::NameDict>();
  435. pkpy::NameDict& locals = *_locals;
  436. int i = 0;
  437. for(const auto& name : fn->args){
  438. if(i < args.size()){
  439. locals.emplace(name, args[i++]);
  440. continue;
  441. }
  442. TypeError("missing positional argument '" + name + "'");
  443. }
  444. locals.insert(fn->kwArgs.begin(), fn->kwArgs.end());
  445. std::vector<Str> positional_overrided_keys;
  446. if(!fn->starredArg.empty()){
  447. pkpy::List vargs; // handle *args
  448. while(i < args.size()) vargs.push_back(args[i++]);
  449. locals.emplace(fn->starredArg, PyTuple(std::move(vargs)));
  450. }else{
  451. for(const auto& key : fn->kwArgsOrder){
  452. if(i < args.size()){
  453. locals[key] = args[i++];
  454. positional_overrided_keys.push_back(key);
  455. }else{
  456. break;
  457. }
  458. }
  459. if(i < args.size()) TypeError("too many arguments");
  460. }
  461. for(int i=0; i<kwargs.size(); i+=2){
  462. const Str& key = PyStr_AS_C(kwargs[i]);
  463. if(!fn->kwArgs.contains(key)){
  464. TypeError(key.escape(true) + " is an invalid keyword argument for " + fn->name + "()");
  465. }
  466. const PyVar& val = kwargs[i+1];
  467. if(!positional_overrided_keys.empty()){
  468. auto it = std::find(positional_overrided_keys.begin(), positional_overrided_keys.end(), key);
  469. if(it != positional_overrided_keys.end()){
  470. TypeError("multiple values for argument '" + key + "'");
  471. }
  472. }
  473. locals[key] = val;
  474. }
  475. PyVar* it_m = (*callable)->attr().try_get(__module__);
  476. PyVar _module = it_m != nullptr ? *it_m : top_frame()->_module;
  477. if(opCall){
  478. _new_frame(fn->code, _module, _locals);
  479. return _py_op_call;
  480. }
  481. return _exec(fn->code, _module, _locals);
  482. }
  483. TypeError("'" + OBJ_NAME(_t(*callable)) + "' object is not callable");
  484. return None;
  485. }
  486. // repl mode is only for setting `frame->id` to 0
  487. PyVarOrNull exec(Str source, Str filename, CompileMode mode, PyVar _module=nullptr){
  488. if(_module == nullptr) _module = _main;
  489. try {
  490. CodeObject_ code = compile(source, filename, mode);
  491. return _exec(code, _module, pkpy::make_shared<pkpy::NameDict>());
  492. }catch (const pkpy::Exception& e){
  493. *_stderr << e.summary() << '\n';
  494. }
  495. catch (const std::exception& e) {
  496. *_stderr << "A std::exception occurred! It may be a bug, please report it!!\n";
  497. *_stderr << e.what() << '\n';
  498. }
  499. callstack = {};
  500. return nullptr;
  501. }
  502. template<typename ...Args>
  503. Frame* _new_frame(Args&&... args){
  504. if(callstack.size() > maxRecursionDepth){
  505. _error("RecursionError", "maximum recursion depth exceeded");
  506. }
  507. callstack.emplace(std::make_unique<Frame>(std::forward<Args>(args)...));
  508. return callstack.top().get();
  509. }
  510. template<typename ...Args>
  511. PyVar _exec(Args&&... args){
  512. Frame* frame = _new_frame(std::forward<Args>(args)...);
  513. i64 base_id = frame->id;
  514. PyVar ret = nullptr;
  515. bool need_raise = false;
  516. while(true){
  517. if(frame->id < base_id) UNREACHABLE();
  518. try{
  519. if(need_raise){ need_raise = false; _raise(); }
  520. ret = run_frame(frame);
  521. if(ret != _py_op_call){
  522. if(frame->id == base_id){ // [ frameBase<- ]
  523. callstack.pop();
  524. return ret;
  525. }else{
  526. callstack.pop();
  527. frame = callstack.top().get();
  528. frame->push(ret);
  529. }
  530. }else{
  531. frame = callstack.top().get(); // [ frameBase, newFrame<- ]
  532. }
  533. }catch(HandledException& e){
  534. continue;
  535. }catch(UnhandledException& e){
  536. PyVar obj = frame->pop();
  537. pkpy::Exception& _e = PyException_AS_C(obj);
  538. _e.st_push(frame->snapshot());
  539. callstack.pop();
  540. if(callstack.empty()) throw _e;
  541. frame = callstack.top().get();
  542. frame->push(obj);
  543. if(frame->id < base_id) throw ToBeRaisedException();
  544. need_raise = true;
  545. }catch(ToBeRaisedException& e){
  546. need_raise = true;
  547. }
  548. }
  549. }
  550. std::vector<PyVar> _all_types;
  551. PyVar new_type_object(PyVar mod, Str name, PyVar base){
  552. if(!base->is_type(tp_type)) UNREACHABLE();
  553. PyVar obj = pkpy::make_shared<PyObject, Py_<Type>>(tp_type, _all_types.size());
  554. setattr(obj, __base__, base);
  555. Str fullName = name;
  556. if(mod != builtins) fullName = OBJ_NAME(mod) + "." + name;
  557. setattr(obj, __name__, PyStr(fullName));
  558. setattr(mod, name, obj);
  559. _all_types.push_back(obj);
  560. return obj;
  561. }
  562. Type _new_type_object(Str name, Type base=0) {
  563. PyVar obj = pkpy::make_shared<PyObject, Py_<Type>>(tp_type, _all_types.size());
  564. setattr(obj, __base__, _t(base));
  565. _types[name] = obj;
  566. _all_types.push_back(obj);
  567. return OBJ_GET(Type, obj);
  568. }
  569. template<typename T>
  570. inline PyVar new_object(PyVar type, T _value) {
  571. if(!type->is_type(tp_type)) UNREACHABLE();
  572. return pkpy::make_shared<PyObject, Py_<T>>(
  573. OBJ_GET(Type, type), _value);
  574. }
  575. template<typename T>
  576. inline PyVar new_object(Type type, T _value) {
  577. return pkpy::make_shared<PyObject, Py_<T>>(type, _value);
  578. }
  579. template<typename T, typename... Args>
  580. inline PyVar new_object(Args&&... args) {
  581. return new_object(T::_type(this), T(std::forward<Args>(args)...));
  582. }
  583. PyVar new_module(const Str& name) {
  584. PyVar obj = new_object(tp_module, DUMMY_VAL);
  585. setattr(obj, __name__, PyStr(name));
  586. _modules[name] = obj;
  587. return obj;
  588. }
  589. PyVarOrNull getattr(const PyVar& obj, const Str& name, bool throw_err=true) {
  590. pkpy::NameDict::iterator it;
  591. PyObject* cls;
  592. if(obj->is_type(tp_super)){
  593. const PyVar* root = &obj;
  594. int depth = 1;
  595. while(true){
  596. root = &OBJ_GET(PyVar, *root);
  597. if(!(*root)->is_type(tp_super)) break;
  598. depth++;
  599. }
  600. cls = _t(*root).get();
  601. for(int i=0; i<depth; i++) cls = cls->attr(__base__).get();
  602. it = (*root)->attr().find(name);
  603. if(it != (*root)->attr().end()) return it->second;
  604. }else{
  605. if(obj->is_attr_valid()){
  606. it = obj->attr().find(name);
  607. if(it != obj->attr().end()) return it->second;
  608. }
  609. cls = _t(obj).get();
  610. }
  611. while(cls != None.get()) {
  612. it = cls->attr().find(name);
  613. if(it != cls->attr().end()){
  614. PyVar valueFromCls = it->second;
  615. if(valueFromCls->is_type(tp_function) || valueFromCls->is_type(tp_native_function)){
  616. return PyBoundMethod({obj, std::move(valueFromCls)});
  617. }else{
  618. return valueFromCls;
  619. }
  620. }
  621. cls = cls->attr()[__base__].get();
  622. }
  623. if(throw_err) AttributeError(obj, name);
  624. return nullptr;
  625. }
  626. template<typename T>
  627. inline void setattr(PyVar& obj, const Str& name, T&& value) {
  628. PyObject* p = obj.get();
  629. while(p->is_type(tp_super)) p = static_cast<PyVar*>(p->value())->get();
  630. if(!p->is_attr_valid()) TypeError("cannot set attribute");
  631. p->attr()[name] = std::forward<T>(value);
  632. }
  633. template<int ARGC>
  634. void bind_method(PyVar obj, Str funcName, NativeFuncRaw fn) {
  635. check_type(obj, tp_type);
  636. setattr(obj, funcName, PyNativeFunc(pkpy::NativeFunc(fn, ARGC, true)));
  637. }
  638. template<int ARGC>
  639. void bind_func(PyVar obj, Str funcName, NativeFuncRaw fn) {
  640. setattr(obj, funcName, PyNativeFunc(pkpy::NativeFunc(fn, ARGC, false)));
  641. }
  642. template<int ARGC>
  643. void bind_method(Str typeName, Str funcName, NativeFuncRaw fn) {
  644. bind_method<ARGC>(_types[typeName], funcName, fn);
  645. }
  646. template<int ARGC>
  647. void bind_static_method(Str typeName, Str funcName, NativeFuncRaw fn) {
  648. bind_func<ARGC>(_types[typeName], funcName, fn);
  649. }
  650. template<int ARGC>
  651. void _bind_methods(std::vector<Str> typeNames, Str funcName, NativeFuncRaw fn) {
  652. for(auto& typeName : typeNames) bind_method<ARGC>(typeName, funcName, fn);
  653. }
  654. template<int ARGC>
  655. void bind_builtin_func(Str funcName, NativeFuncRaw fn) {
  656. bind_func<ARGC>(builtins, funcName, fn);
  657. }
  658. inline f64 num_to_float(const PyVar& obj){
  659. if (obj->is_type(tp_int)){
  660. return (f64)PyInt_AS_C(obj);
  661. }else if(obj->is_type(tp_float)){
  662. return PyFloat_AS_C(obj);
  663. }
  664. TypeError("expected 'int' or 'float', got " + OBJ_NAME(_t(obj)).escape(true));
  665. return 0;
  666. }
  667. PyVar num_negated(const PyVar& obj){
  668. if (obj->is_type(tp_int)){
  669. return PyInt(-PyInt_AS_C(obj));
  670. }else if(obj->is_type(tp_float)){
  671. return PyFloat(-PyFloat_AS_C(obj));
  672. }
  673. TypeError("unsupported operand type(s) for -");
  674. return nullptr;
  675. }
  676. int normalized_index(int index, int size){
  677. if(index < 0) index += size;
  678. if(index < 0 || index >= size){
  679. IndexError(std::to_string(index) + " not in [0, " + std::to_string(size) + ")");
  680. }
  681. return index;
  682. }
  683. Str disassemble(CodeObject_ co){
  684. std::vector<int> jumpTargets;
  685. for(auto byte : co->codes){
  686. if(byte.op == OP_JUMP_ABSOLUTE || byte.op == OP_SAFE_JUMP_ABSOLUTE || byte.op == OP_POP_JUMP_IF_FALSE){
  687. jumpTargets.push_back(byte.arg);
  688. }
  689. }
  690. StrStream ss;
  691. ss << std::string(54, '-') << '\n';
  692. ss << co->name << ":\n";
  693. int prev_line = -1;
  694. for(int i=0; i<co->codes.size(); i++){
  695. const Bytecode& byte = co->codes[i];
  696. Str line = std::to_string(byte.line);
  697. if(byte.line == prev_line) line = "";
  698. else{
  699. if(prev_line != -1) ss << "\n";
  700. prev_line = byte.line;
  701. }
  702. std::string pointer;
  703. if(std::find(jumpTargets.begin(), jumpTargets.end(), i) != jumpTargets.end()){
  704. pointer = "-> ";
  705. }else{
  706. pointer = " ";
  707. }
  708. ss << pad(line, 8) << pointer << pad(std::to_string(i), 3);
  709. ss << " " << pad(OP_NAMES[byte.op], 20) << " ";
  710. // ss << pad(byte.arg == -1 ? "" : std::to_string(byte.arg), 5);
  711. std::string argStr = byte.arg == -1 ? "" : std::to_string(byte.arg);
  712. if(byte.op == OP_LOAD_CONST){
  713. argStr += " (" + PyStr_AS_C(asRepr(co->consts[byte.arg])) + ")";
  714. }
  715. if(byte.op == OP_LOAD_NAME_REF || byte.op == OP_LOAD_NAME || byte.op == OP_RAISE){
  716. argStr += " (" + co->names[byte.arg].first.escape(true) + ")";
  717. }
  718. ss << pad(argStr, 20); // may overflow
  719. ss << co->blocks[byte.block].to_string();
  720. if(i != co->codes.size() - 1) ss << '\n';
  721. }
  722. StrStream consts;
  723. consts << "co_consts: ";
  724. consts << PyStr_AS_C(asRepr(PyList(co->consts)));
  725. StrStream names;
  726. names << "co_names: ";
  727. pkpy::List list;
  728. for(int i=0; i<co->names.size(); i++){
  729. list.push_back(PyStr(co->names[i].first));
  730. }
  731. names << PyStr_AS_C(asRepr(PyList(list)));
  732. ss << '\n' << consts.str() << '\n' << names.str() << '\n';
  733. for(int i=0; i<co->consts.size(); i++){
  734. PyVar obj = co->consts[i];
  735. if(obj->is_type(tp_function)){
  736. const auto& f = PyFunction_AS_C(obj);
  737. ss << disassemble(f->code);
  738. }
  739. }
  740. return Str(ss.str());
  741. }
  742. // for quick access
  743. Type tp_object, tp_type, tp_int, tp_float, tp_bool, tp_str;
  744. Type tp_list, tp_tuple;
  745. Type tp_function, tp_native_function, tp_native_iterator, tp_bound_method;
  746. Type tp_slice, tp_range, tp_module, tp_ref;
  747. Type tp_super, tp_exception;
  748. template<typename P>
  749. inline PyVarRef PyRef(P&& value) {
  750. static_assert(std::is_base_of<BaseRef, std::remove_reference_t<P>>::value, "P should derive from BaseRef");
  751. return new_object(tp_ref, std::forward<P>(value));
  752. }
  753. inline const BaseRef* PyRef_AS_C(const PyVar& obj)
  754. {
  755. if(!obj->is_type(tp_ref)) TypeError("expected an l-value");
  756. return (const BaseRef*)(obj->value());
  757. }
  758. inline const Str& PyStr_AS_C(const PyVar& obj) {
  759. check_type(obj, tp_str);
  760. return OBJ_GET(Str, obj);
  761. }
  762. inline PyVar PyStr(const Str& value) {
  763. // some BUGs here
  764. // if(value.size() == 1){
  765. // char c = value.c_str()[0];
  766. // if(c >= 0) return _ascii_str_pool[(int)c];
  767. // }
  768. return new_object(tp_str, value);
  769. }
  770. DEF_NATIVE(Int, i64, tp_int)
  771. DEF_NATIVE(Float, f64, tp_float)
  772. DEF_NATIVE(List, pkpy::List, tp_list)
  773. DEF_NATIVE(Tuple, pkpy::Tuple, tp_tuple)
  774. DEF_NATIVE(Function, pkpy::Function_, tp_function)
  775. DEF_NATIVE(NativeFunc, pkpy::NativeFunc, tp_native_function)
  776. DEF_NATIVE(Iter, pkpy::shared_ptr<BaseIter>, tp_native_iterator)
  777. DEF_NATIVE(BoundMethod, pkpy::BoundMethod, tp_bound_method)
  778. DEF_NATIVE(Range, pkpy::Range, tp_range)
  779. DEF_NATIVE(Slice, pkpy::Slice, tp_slice)
  780. DEF_NATIVE(Exception, pkpy::Exception, tp_exception)
  781. // there is only one True/False, so no need to copy them!
  782. inline bool PyBool_AS_C(const PyVar& obj){return obj == True;}
  783. inline const PyVar& PyBool(bool value){return value ? True : False;}
  784. void init_builtin_types(){
  785. PyVar _tp_object = pkpy::make_shared<PyObject, Py_<Type>>(1, 0);
  786. PyVar _tp_type = pkpy::make_shared<PyObject, Py_<Type>>(1, 1);
  787. _all_types.push_back(_tp_object);
  788. _all_types.push_back(_tp_type);
  789. tp_object = 0; tp_type = 1;
  790. _types["object"] = _tp_object;
  791. _types["type"] = _tp_type;
  792. tp_bool = _new_type_object("bool");
  793. tp_int = _new_type_object("int");
  794. tp_float = _new_type_object("float");
  795. tp_str = _new_type_object("str");
  796. tp_list = _new_type_object("list");
  797. tp_tuple = _new_type_object("tuple");
  798. tp_slice = _new_type_object("slice");
  799. tp_range = _new_type_object("range");
  800. tp_module = _new_type_object("module");
  801. tp_ref = _new_type_object("_ref");
  802. tp_function = _new_type_object("function");
  803. tp_native_function = _new_type_object("native_function");
  804. tp_native_iterator = _new_type_object("native_iterator");
  805. tp_bound_method = _new_type_object("bound_method");
  806. tp_super = _new_type_object("super");
  807. tp_exception = _new_type_object("Exception");
  808. this->None = new_object(_new_type_object("NoneType"), DUMMY_VAL);
  809. this->Ellipsis = new_object(_new_type_object("ellipsis"), DUMMY_VAL);
  810. this->True = new_object(tp_bool, true);
  811. this->False = new_object(tp_bool, false);
  812. this->builtins = new_module("builtins");
  813. this->_main = new_module("__main__");
  814. this->_py_op_call = new_object(_new_type_object("_internal"), DUMMY_VAL);
  815. setattr(_t(tp_type), __base__, _t(tp_object));
  816. setattr(_t(tp_object), __base__, None);
  817. for (auto& [name, type] : _types) {
  818. setattr(type, __name__, PyStr(name));
  819. }
  820. std::vector<Str> publicTypes = {"type", "object", "bool", "int", "float", "str", "list", "tuple", "range"};
  821. for (auto& name : publicTypes) {
  822. setattr(builtins, name, _types[name]);
  823. }
  824. }
  825. i64 hash(const PyVar& obj){
  826. if (obj->is_type(tp_int)) return PyInt_AS_C(obj);
  827. if (obj->is_type(tp_bool)) return PyBool_AS_C(obj) ? 1 : 0;
  828. if (obj->is_type(tp_float)){
  829. f64 val = PyFloat_AS_C(obj);
  830. return (i64)std::hash<f64>()(val);
  831. }
  832. if (obj->is_type(tp_str)) return PyStr_AS_C(obj).hash();
  833. if (obj->is_type(tp_type)) return (i64)obj.get();
  834. if (obj->is_type(tp_tuple)) {
  835. i64 x = 1000003;
  836. const pkpy::Tuple& items = PyTuple_AS_C(obj);
  837. for (int i=0; i<items.size(); i++) {
  838. i64 y = hash(items[i]);
  839. x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 2)); // recommended by Github Copilot
  840. }
  841. return x;
  842. }
  843. TypeError("unhashable type: " + OBJ_NAME(_t(obj)).escape(true));
  844. return 0;
  845. }
  846. /***** Error Reporter *****/
  847. private:
  848. void _error(const Str& name, const Str& msg){
  849. _error(pkpy::Exception(name, msg));
  850. }
  851. void _error(pkpy::Exception e){
  852. if(callstack.empty()){
  853. e.is_re = false;
  854. throw e;
  855. }
  856. top_frame()->push(PyException(e));
  857. _raise();
  858. }
  859. void _raise(){
  860. bool ok = top_frame()->jump_to_exception_handler();
  861. if(ok) throw HandledException();
  862. else throw UnhandledException();
  863. }
  864. public:
  865. void NotImplementedError(){ _error("NotImplementedError", ""); }
  866. void TypeError(const Str& msg){ _error("TypeError", msg); }
  867. void ZeroDivisionError(){ _error("ZeroDivisionError", "division by zero"); }
  868. void IndexError(const Str& msg){ _error("IndexError", msg); }
  869. void ValueError(const Str& msg){ _error("ValueError", msg); }
  870. void NameError(const Str& name){ _error("NameError", "name " + name.escape(true) + " is not defined"); }
  871. void AttributeError(PyVar obj, const Str& name){
  872. _error("AttributeError", "type " + OBJ_NAME(_t(obj)).escape(true) + " has no attribute " + name.escape(true));
  873. }
  874. inline void check_type(const PyVar& obj, Type type){
  875. if(obj->is_type(type)) return;
  876. TypeError("expected " + OBJ_NAME(_t(type)).escape(true) + ", but got " + OBJ_NAME(_t(obj)).escape(true));
  877. }
  878. inline PyVar& _t(Type t){
  879. return _all_types[t.index];
  880. }
  881. inline PyVar& _t(const PyVar& obj){
  882. return _all_types[OBJ_GET(Type, _t(obj->type)).index];
  883. }
  884. template<typename T>
  885. PyVar register_class(PyVar mod){
  886. PyVar type = new_type_object(mod, T::_name(), _t(tp_object));
  887. if(OBJ_NAME(mod) != T::_mod()) UNREACHABLE();
  888. T::_register(this, mod, type);
  889. return type;
  890. }
  891. template<typename T>
  892. inline T& py_cast(const PyVar& obj){
  893. check_type(obj, T::_type(this));
  894. return OBJ_GET(T, obj);
  895. }
  896. ~VM() {
  897. if(!use_stdio){
  898. delete _stdout;
  899. delete _stderr;
  900. }
  901. }
  902. CodeObject_ compile(Str source, Str filename, CompileMode mode);
  903. };
  904. /***** Pointers' Impl *****/
  905. PyVar NameRef::get(VM* vm, Frame* frame) const{
  906. PyVar* val;
  907. val = frame->f_locals().try_get(name());
  908. if(val) return *val;
  909. val = frame->f_globals().try_get(name());
  910. if(val) return *val;
  911. val = vm->builtins->attr().try_get(name());
  912. if(val) return *val;
  913. vm->NameError(name());
  914. return nullptr;
  915. }
  916. void NameRef::set(VM* vm, Frame* frame, PyVar val) const{
  917. switch(scope()) {
  918. case NAME_LOCAL: frame->f_locals()[name()] = std::move(val); break;
  919. case NAME_GLOBAL:
  920. {
  921. PyVar* existing = frame->f_locals().try_get(name());
  922. if(existing != nullptr){
  923. *existing = std::move(val);
  924. }else{
  925. frame->f_globals()[name()] = std::move(val);
  926. }
  927. } break;
  928. default: UNREACHABLE();
  929. }
  930. }
  931. void NameRef::del(VM* vm, Frame* frame) const{
  932. switch(scope()) {
  933. case NAME_LOCAL: {
  934. if(frame->f_locals().contains(name())){
  935. frame->f_locals().erase(name());
  936. }else{
  937. vm->NameError(name());
  938. }
  939. } break;
  940. case NAME_GLOBAL:
  941. {
  942. if(frame->f_locals().contains(name())){
  943. frame->f_locals().erase(name());
  944. }else{
  945. if(frame->f_globals().contains(name())){
  946. frame->f_globals().erase(name());
  947. }else{
  948. vm->NameError(name());
  949. }
  950. }
  951. } break;
  952. default: UNREACHABLE();
  953. }
  954. }
  955. PyVar AttrRef::get(VM* vm, Frame* frame) const{
  956. return vm->getattr(obj, attr.name());
  957. }
  958. void AttrRef::set(VM* vm, Frame* frame, PyVar val) const{
  959. vm->setattr(obj, attr.name(), val);
  960. }
  961. void AttrRef::del(VM* vm, Frame* frame) const{
  962. if(!obj->is_attr_valid()) vm->TypeError("cannot delete attribute");
  963. if(!obj->attr().contains(attr.name())) vm->AttributeError(obj, attr.name());
  964. obj->attr().erase(attr.name());
  965. }
  966. PyVar IndexRef::get(VM* vm, Frame* frame) const{
  967. return vm->call(obj, __getitem__, pkpy::one_arg(index));
  968. }
  969. void IndexRef::set(VM* vm, Frame* frame, PyVar val) const{
  970. vm->call(obj, __setitem__, pkpy::two_args(index, val));
  971. }
  972. void IndexRef::del(VM* vm, Frame* frame) const{
  973. vm->call(obj, __delitem__, pkpy::one_arg(index));
  974. }
  975. PyVar TupleRef::get(VM* vm, Frame* frame) const{
  976. pkpy::Tuple args(objs.size());
  977. for (int i = 0; i < objs.size(); i++) {
  978. args[i] = vm->PyRef_AS_C(objs[i])->get(vm, frame);
  979. }
  980. return vm->PyTuple(std::move(args));
  981. }
  982. void TupleRef::set(VM* vm, Frame* frame, PyVar val) const{
  983. #define TUPLE_REF_SET() \
  984. if(args.size() > objs.size()) vm->ValueError("too many values to unpack"); \
  985. if(args.size() < objs.size()) vm->ValueError("not enough values to unpack"); \
  986. for (int i = 0; i < objs.size(); i++) vm->PyRef_AS_C(objs[i])->set(vm, frame, args[i]);
  987. if(val->is_type(vm->tp_tuple)){
  988. const pkpy::Tuple& args = OBJ_GET(pkpy::Tuple, val);
  989. TUPLE_REF_SET()
  990. }else if(val->is_type(vm->tp_list)){
  991. const pkpy::List& args = OBJ_GET(pkpy::List, val);
  992. TUPLE_REF_SET()
  993. }else{
  994. vm->TypeError("only tuple or list can be unpacked");
  995. }
  996. #undef TUPLE_REF_SET
  997. }
  998. void TupleRef::del(VM* vm, Frame* frame) const{
  999. for(int i=0; i<objs.size(); i++) vm->PyRef_AS_C(objs[i])->del(vm, frame);
  1000. }
  1001. /***** Frame's Impl *****/
  1002. inline void Frame::try_deref(VM* vm, PyVar& v){
  1003. if(v->is_type(vm->tp_ref)) v = vm->PyRef_AS_C(v)->get(vm, this);
  1004. }
  1005. PyVar pkpy::NativeFunc::operator()(VM* vm, const pkpy::Args& args) const{
  1006. int args_size = args.size() - (int)method; // remove self
  1007. if(argc != -1 && args_size != argc) {
  1008. vm->TypeError("expected " + std::to_string(argc) + " arguments, but got " + std::to_string(args_size));
  1009. }
  1010. return f(vm, args);
  1011. }