pocketpy.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. #pragma once
  2. #include "ceval.h"
  3. #include "compiler.h"
  4. #include "obj.h"
  5. #include "repl.h"
  6. #include "iter.h"
  7. #include "cffi.h"
  8. #include "io.h"
  9. #include "_generated.h"
  10. namespace pkpy {
  11. inline CodeObject_ VM::compile(Str source, Str filename, CompileMode mode) {
  12. Compiler compiler(this, source, filename, mode);
  13. try{
  14. return compiler.compile();
  15. }catch(Exception& e){
  16. #if DEBUG_FULL_EXCEPTION
  17. std::cerr << e.summary() << std::endl;
  18. #endif
  19. _error(e);
  20. return nullptr;
  21. }
  22. }
  23. #define BIND_NUM_ARITH_OPT(name, op) \
  24. _vm->_bind_methods<1>({"int","float"}, #name, [](VM* vm, Args& args){ \
  25. if(is_both_int(args[0], args[1])){ \
  26. return VAR(_CAST(i64, args[0]) op _CAST(i64, args[1])); \
  27. }else{ \
  28. return VAR(vm->num_to_float(args[0]) op vm->num_to_float(args[1])); \
  29. } \
  30. });
  31. #define BIND_NUM_LOGICAL_OPT(name, op, is_eq) \
  32. _vm->_bind_methods<1>({"int","float"}, #name, [](VM* vm, Args& args){ \
  33. if(is_both_int(args[0], args[1])) \
  34. return VAR(_CAST(i64, args[0]) op _CAST(i64, args[1])); \
  35. if(!is_both_int_or_float(args[0], args[1])){ \
  36. if constexpr(is_eq) return VAR(args[0] op args[1]); \
  37. vm->TypeError("unsupported operand type(s) for " #op ); \
  38. } \
  39. return VAR(vm->num_to_float(args[0]) op vm->num_to_float(args[1])); \
  40. });
  41. inline void init_builtins(VM* _vm) {
  42. BIND_NUM_ARITH_OPT(__add__, +)
  43. BIND_NUM_ARITH_OPT(__sub__, -)
  44. BIND_NUM_ARITH_OPT(__mul__, *)
  45. BIND_NUM_LOGICAL_OPT(__lt__, <, false)
  46. BIND_NUM_LOGICAL_OPT(__le__, <=, false)
  47. BIND_NUM_LOGICAL_OPT(__gt__, >, false)
  48. BIND_NUM_LOGICAL_OPT(__ge__, >=, false)
  49. BIND_NUM_LOGICAL_OPT(__eq__, ==, true)
  50. BIND_NUM_LOGICAL_OPT(__ne__, !=, true)
  51. #undef BIND_NUM_ARITH_OPT
  52. #undef BIND_NUM_LOGICAL_OPT
  53. _vm->bind_builtin_func<1>("__sys_stdout_write", [](VM* vm, Args& args) {
  54. (*vm->_stdout) << CAST(Str&, args[0]);
  55. return vm->None;
  56. });
  57. _vm->bind_builtin_func<2>("super", [](VM* vm, Args& args) {
  58. vm->check_type(args[0], vm->tp_type);
  59. Type type = OBJ_GET(Type, args[0]);
  60. if(!vm->isinstance(args[1], type)){
  61. Str _0 = obj_type_name(vm, OBJ_GET(Type, vm->_t(args[1])));
  62. Str _1 = obj_type_name(vm, type);
  63. vm->TypeError("super(): " + _0.escape() + " is not an instance of " + _1.escape());
  64. }
  65. Type base = vm->_all_types[type].base;
  66. return vm->heap.gcnew(vm->tp_super, Super(args[1], base));
  67. });
  68. _vm->bind_builtin_func<2>("isinstance", [](VM* vm, Args& args) {
  69. vm->check_type(args[1], vm->tp_type);
  70. Type type = OBJ_GET(Type, args[1]);
  71. return VAR(vm->isinstance(args[0], type));
  72. });
  73. _vm->bind_builtin_func<1>("id", [](VM* vm, Args& args) {
  74. PyObject* obj = args[0];
  75. if(is_tagged(obj)) return VAR((i64)0);
  76. return VAR(BITS(obj));
  77. });
  78. _vm->bind_builtin_func<2>("divmod", [](VM* vm, Args& args) {
  79. i64 lhs = CAST(i64, args[0]);
  80. i64 rhs = CAST(i64, args[1]);
  81. if(rhs == 0) vm->ZeroDivisionError();
  82. return VAR(Tuple({VAR(lhs/rhs), VAR(lhs%rhs)}));
  83. });
  84. _vm->bind_builtin_func<1>("eval", [](VM* vm, Args& args) {
  85. CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<eval>", EVAL_MODE);
  86. return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
  87. });
  88. _vm->bind_builtin_func<1>("exec", [](VM* vm, Args& args) {
  89. CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<exec>", EXEC_MODE);
  90. vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
  91. return vm->None;
  92. });
  93. _vm->bind_builtin_func<-1>("exit", [](VM* vm, Args& args) {
  94. if(args.size() == 0) std::exit(0);
  95. else if(args.size() == 1) std::exit(CAST(int, args[0]));
  96. else vm->TypeError("exit() takes at most 1 argument");
  97. return vm->None;
  98. });
  99. _vm->bind_builtin_func<1>("repr", CPP_LAMBDA(vm->asRepr(args[0])));
  100. _vm->bind_builtin_func<1>("len", CPP_LAMBDA(vm->fast_call(__len__, Args{args[0]})));
  101. _vm->bind_builtin_func<1>("hash", [](VM* vm, Args& args){
  102. i64 value = vm->hash(args[0]);
  103. if(((value << 2) >> 2) != value) value >>= 2;
  104. return VAR(value);
  105. });
  106. _vm->bind_builtin_func<1>("chr", [](VM* vm, Args& args) {
  107. i64 i = CAST(i64, args[0]);
  108. if (i < 0 || i > 128) vm->ValueError("chr() arg not in range(128)");
  109. return VAR(std::string(1, (char)i));
  110. });
  111. _vm->bind_builtin_func<1>("ord", [](VM* vm, Args& args) {
  112. const Str& s = CAST(Str&, args[0]);
  113. if (s.length()!=1) vm->TypeError("ord() expected an ASCII character");
  114. return VAR((i64)(s[0]));
  115. });
  116. _vm->bind_builtin_func<2>("hasattr", [](VM* vm, Args& args) {
  117. return VAR(vm->getattr(args[0], CAST(Str&, args[1]), false) != nullptr);
  118. });
  119. _vm->bind_builtin_func<3>("setattr", [](VM* vm, Args& args) {
  120. vm->setattr(args[0], CAST(Str&, args[1]), args[2]);
  121. return vm->None;
  122. });
  123. _vm->bind_builtin_func<2>("getattr", [](VM* vm, Args& args) {
  124. const Str& name = CAST(Str&, args[1]);
  125. return vm->getattr(args[0], name);
  126. });
  127. _vm->bind_builtin_func<1>("hex", [](VM* vm, Args& args) {
  128. std::stringstream ss;
  129. ss << std::hex << CAST(i64, args[0]);
  130. return VAR("0x" + ss.str());
  131. });
  132. _vm->bind_builtin_func<1>("iter", [](VM* vm, Args& args) {
  133. return vm->asIter(args[0]);
  134. });
  135. _vm->bind_builtin_func<1>("dir", [](VM* vm, Args& args) {
  136. std::set<StrName> names;
  137. if(args[0]->is_attr_valid()){
  138. std::vector<StrName> keys = args[0]->attr().keys();
  139. names.insert(keys.begin(), keys.end());
  140. }
  141. const NameDict& t_attr = vm->_t(args[0])->attr();
  142. std::vector<StrName> keys = t_attr.keys();
  143. names.insert(keys.begin(), keys.end());
  144. List ret;
  145. for (StrName name : names) ret.push_back(VAR(name.sv()));
  146. return VAR(std::move(ret));
  147. });
  148. _vm->bind_method<0>("object", "__repr__", [](VM* vm, Args& args) {
  149. PyObject* self = args[0];
  150. if(is_tagged(self)) self = nullptr;
  151. std::stringstream ss;
  152. ss << "<" << OBJ_NAME(vm->_t(self)) << " object at " << std::hex << self << ">";
  153. return VAR(ss.str());
  154. });
  155. _vm->bind_method<1>("object", "__eq__", CPP_LAMBDA(VAR(args[0] == args[1])));
  156. _vm->bind_method<1>("object", "__ne__", CPP_LAMBDA(VAR(args[0] != args[1])));
  157. _vm->bind_static_method<1>("type", "__new__", CPP_LAMBDA(vm->_t(args[0])));
  158. _vm->bind_static_method<-1>("range", "__new__", [](VM* vm, Args& args) {
  159. Range r;
  160. switch (args.size()) {
  161. case 1: r.stop = CAST(i64, args[0]); break;
  162. case 2: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); break;
  163. case 3: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); r.step = CAST(i64, args[2]); break;
  164. default: vm->TypeError("expected 1-3 arguments, but got " + std::to_string(args.size()));
  165. }
  166. return VAR(r);
  167. });
  168. _vm->bind_method<0>("range", "__iter__", CPP_LAMBDA(
  169. vm->PyIter(RangeIter(vm, args[0]))
  170. ));
  171. _vm->bind_method<0>("NoneType", "__repr__", CPP_LAMBDA(VAR("None")));
  172. _vm->bind_method<0>("NoneType", "__json__", CPP_LAMBDA(VAR("null")));
  173. _vm->_bind_methods<1>({"int", "float"}, "__truediv__", [](VM* vm, Args& args) {
  174. f64 rhs = vm->num_to_float(args[1]);
  175. if (rhs == 0) vm->ZeroDivisionError();
  176. return VAR(vm->num_to_float(args[0]) / rhs);
  177. });
  178. _vm->_bind_methods<1>({"int", "float"}, "__pow__", [](VM* vm, Args& args) {
  179. if(is_both_int(args[0], args[1])){
  180. i64 lhs = _CAST(i64, args[0]);
  181. i64 rhs = _CAST(i64, args[1]);
  182. bool flag = false;
  183. if(rhs < 0) {flag = true; rhs = -rhs;}
  184. i64 ret = 1;
  185. while(rhs){
  186. if(rhs & 1) ret *= lhs;
  187. lhs *= lhs;
  188. rhs >>= 1;
  189. }
  190. if(flag) return VAR((f64)(1.0 / ret));
  191. return VAR(ret);
  192. }else{
  193. return VAR((f64)std::pow(vm->num_to_float(args[0]), vm->num_to_float(args[1])));
  194. }
  195. });
  196. /************ PyInt ************/
  197. _vm->bind_static_method<1>("int", "__new__", [](VM* vm, Args& args) {
  198. if (is_type(args[0], vm->tp_int)) return args[0];
  199. if (is_type(args[0], vm->tp_float)) return VAR((i64)CAST(f64, args[0]));
  200. if (is_type(args[0], vm->tp_bool)) return VAR(_CAST(bool, args[0]) ? 1 : 0);
  201. if (is_type(args[0], vm->tp_str)) {
  202. const Str& s = CAST(Str&, args[0]);
  203. try{
  204. size_t parsed = 0;
  205. i64 val = S_TO_INT(s.str(), &parsed, 10);
  206. if(parsed != s.length()) throw std::invalid_argument("<?>");
  207. return VAR(val);
  208. }catch(std::invalid_argument&){
  209. vm->ValueError("invalid literal for int(): " + s.escape());
  210. }
  211. }
  212. vm->TypeError("int() argument must be a int, float, bool or str");
  213. return vm->None;
  214. });
  215. _vm->bind_method<1>("int", "__floordiv__", [](VM* vm, Args& args) {
  216. i64 rhs = CAST(i64, args[1]);
  217. if(rhs == 0) vm->ZeroDivisionError();
  218. return VAR(CAST(i64, args[0]) / rhs);
  219. });
  220. _vm->bind_method<1>("int", "__mod__", [](VM* vm, Args& args) {
  221. i64 rhs = CAST(i64, args[1]);
  222. if(rhs == 0) vm->ZeroDivisionError();
  223. return VAR(CAST(i64, args[0]) % rhs);
  224. });
  225. _vm->bind_method<0>("int", "__repr__", CPP_LAMBDA(VAR(std::to_string(CAST(i64, args[0])))));
  226. _vm->bind_method<0>("int", "__json__", CPP_LAMBDA(VAR(std::to_string(CAST(i64, args[0])))));
  227. #define INT_BITWISE_OP(name,op) \
  228. _vm->bind_method<1>("int", #name, CPP_LAMBDA(VAR(CAST(i64, args[0]) op CAST(i64, args[1]))));
  229. INT_BITWISE_OP(__lshift__, <<)
  230. INT_BITWISE_OP(__rshift__, >>)
  231. INT_BITWISE_OP(__and__, &)
  232. INT_BITWISE_OP(__or__, |)
  233. INT_BITWISE_OP(__xor__, ^)
  234. #undef INT_BITWISE_OP
  235. /************ PyFloat ************/
  236. _vm->bind_static_method<1>("float", "__new__", [](VM* vm, Args& args) {
  237. if (is_type(args[0], vm->tp_int)) return VAR((f64)CAST(i64, args[0]));
  238. if (is_type(args[0], vm->tp_float)) return args[0];
  239. if (is_type(args[0], vm->tp_bool)) return VAR(_CAST(bool, args[0]) ? 1.0 : 0.0);
  240. if (is_type(args[0], vm->tp_str)) {
  241. const Str& s = CAST(Str&, args[0]);
  242. if(s == "inf") return VAR(INFINITY);
  243. if(s == "-inf") return VAR(-INFINITY);
  244. try{
  245. f64 val = S_TO_FLOAT(s.str());
  246. return VAR(val);
  247. }catch(std::invalid_argument&){
  248. vm->ValueError("invalid literal for float(): '" + s + "'");
  249. }
  250. }
  251. vm->TypeError("float() argument must be a int, float, bool or str");
  252. return vm->None;
  253. });
  254. _vm->bind_method<0>("float", "__repr__", [](VM* vm, Args& args) {
  255. f64 val = CAST(f64, args[0]);
  256. if(std::isinf(val) || std::isnan(val)) return VAR(std::to_string(val));
  257. std::stringstream ss;
  258. ss << std::setprecision(std::numeric_limits<f64>::max_digits10-1-2) << val;
  259. std::string s = ss.str();
  260. if(std::all_of(s.begin()+1, s.end(), isdigit)) s += ".0";
  261. return VAR(s);
  262. });
  263. _vm->bind_method<0>("float", "__json__", [](VM* vm, Args& args) {
  264. f64 val = CAST(f64, args[0]);
  265. if(std::isinf(val) || std::isnan(val)) vm->ValueError("cannot jsonify 'nan' or 'inf'");
  266. return VAR(std::to_string(val));
  267. });
  268. /************ PyString ************/
  269. _vm->bind_static_method<1>("str", "__new__", CPP_LAMBDA(vm->asStr(args[0])));
  270. _vm->bind_method<1>("str", "__add__", [](VM* vm, Args& args) {
  271. const Str& lhs = CAST(Str&, args[0]);
  272. const Str& rhs = CAST(Str&, args[1]);
  273. return VAR(lhs + rhs);
  274. });
  275. _vm->bind_method<0>("str", "__len__", [](VM* vm, Args& args) {
  276. const Str& self = CAST(Str&, args[0]);
  277. return VAR(self.u8_length());
  278. });
  279. _vm->bind_method<1>("str", "__contains__", [](VM* vm, Args& args) {
  280. const Str& self = CAST(Str&, args[0]);
  281. const Str& other = CAST(Str&, args[1]);
  282. return VAR(self.index(other) != -1);
  283. });
  284. _vm->bind_method<0>("str", "__str__", CPP_LAMBDA(args[0]));
  285. _vm->bind_method<0>("str", "__iter__", CPP_LAMBDA(vm->PyIter(StringIter(vm, args[0]))));
  286. _vm->bind_method<0>("str", "__repr__", [](VM* vm, Args& args) {
  287. const Str& _self = CAST(Str&, args[0]);
  288. return VAR(_self.escape());
  289. });
  290. _vm->bind_method<0>("str", "__json__", [](VM* vm, Args& args) {
  291. const Str& self = CAST(Str&, args[0]);
  292. return VAR(self.escape(false));
  293. });
  294. _vm->bind_method<1>("str", "__eq__", [](VM* vm, Args& args) {
  295. if(is_type(args[0], vm->tp_str) && is_type(args[1], vm->tp_str))
  296. return VAR(CAST(Str&, args[0]) == CAST(Str&, args[1]));
  297. return VAR(args[0] == args[1]);
  298. });
  299. _vm->bind_method<1>("str", "__ne__", [](VM* vm, Args& args) {
  300. if(is_type(args[0], vm->tp_str) && is_type(args[1], vm->tp_str))
  301. return VAR(CAST(Str&, args[0]) != CAST(Str&, args[1]));
  302. return VAR(args[0] != args[1]);
  303. });
  304. _vm->bind_method<1>("str", "__getitem__", [](VM* vm, Args& args) {
  305. const Str& self (CAST(Str&, args[0]));
  306. if(is_type(args[1], vm->tp_slice)){
  307. Slice s = _CAST(Slice, args[1]);
  308. s.normalize(self.u8_length());
  309. return VAR(self.u8_slice(s.start, s.stop));
  310. }
  311. int index = CAST(int, args[1]);
  312. index = vm->normalized_index(index, self.u8_length());
  313. return VAR(self.u8_getitem(index));
  314. });
  315. _vm->bind_method<1>("str", "__gt__", [](VM* vm, Args& args) {
  316. const Str& self (CAST(Str&, args[0]));
  317. const Str& obj (CAST(Str&, args[1]));
  318. return VAR(self > obj);
  319. });
  320. _vm->bind_method<1>("str", "__lt__", [](VM* vm, Args& args) {
  321. const Str& self (CAST(Str&, args[0]));
  322. const Str& obj (CAST(Str&, args[1]));
  323. return VAR(self < obj);
  324. });
  325. _vm->bind_method<2>("str", "replace", [](VM* vm, Args& args) {
  326. const Str& self = CAST(Str&, args[0]);
  327. const Str& old = CAST(Str&, args[1]);
  328. const Str& new_ = CAST(Str&, args[2]);
  329. return VAR(self.replace(old, new_));
  330. });
  331. _vm->bind_method<1>("str", "startswith", [](VM* vm, Args& args) {
  332. const Str& self = CAST(Str&, args[0]);
  333. const Str& prefix = CAST(Str&, args[1]);
  334. return VAR(self.index(prefix) == 0);
  335. });
  336. _vm->bind_method<1>("str", "endswith", [](VM* vm, Args& args) {
  337. const Str& self = CAST(Str&, args[0]);
  338. const Str& suffix = CAST(Str&, args[1]);
  339. int offset = self.length() - suffix.length();
  340. if(offset < 0) return vm->False;
  341. bool ok = memcmp(self.data+offset, suffix.data, suffix.length()) == 0;
  342. return VAR(ok);
  343. });
  344. _vm->bind_method<1>("str", "join", [](VM* vm, Args& args) {
  345. const Str& self = CAST(Str&, args[0]);
  346. FastStrStream ss;
  347. PyObject* obj = vm->asList(args[1]);
  348. const List& list = CAST(List&, obj);
  349. for (int i = 0; i < list.size(); ++i) {
  350. if (i > 0) ss << self;
  351. ss << CAST(Str&, list[i]);
  352. }
  353. return VAR(ss.str());
  354. });
  355. /************ PyList ************/
  356. _vm->bind_method<1>("list", "append", [](VM* vm, Args& args) {
  357. List& self = CAST(List&, args[0]);
  358. self.push_back(args[1]);
  359. return vm->None;
  360. });
  361. _vm->bind_method<1>("list", "extend", [](VM* vm, Args& args) {
  362. List& self = CAST(List&, args[0]);
  363. PyObject* obj = vm->asList(args[1]);
  364. const List& list = CAST(List&, obj);
  365. self.extend(list);
  366. return vm->None;
  367. });
  368. _vm->bind_method<0>("list", "reverse", [](VM* vm, Args& args) {
  369. List& self = CAST(List&, args[0]);
  370. std::reverse(self.begin(), self.end());
  371. return vm->None;
  372. });
  373. _vm->bind_method<1>("list", "__mul__", [](VM* vm, Args& args) {
  374. const List& self = CAST(List&, args[0]);
  375. int n = CAST(int, args[1]);
  376. List result;
  377. result.reserve(self.size() * n);
  378. for(int i = 0; i < n; i++) result.extend(self);
  379. return VAR(std::move(result));
  380. });
  381. _vm->bind_method<2>("list", "insert", [](VM* vm, Args& args) {
  382. List& self = CAST(List&, args[0]);
  383. int index = CAST(int, args[1]);
  384. if(index < 0) index += self.size();
  385. if(index < 0) index = 0;
  386. if(index > self.size()) index = self.size();
  387. self.insert(index, args[2]);
  388. return vm->None;
  389. });
  390. _vm->bind_method<0>("list", "clear", [](VM* vm, Args& args) {
  391. CAST(List&, args[0]).clear();
  392. return vm->None;
  393. });
  394. _vm->bind_method<0>("list", "copy", CPP_LAMBDA(VAR(CAST(List, args[0]))));
  395. _vm->bind_method<1>("list", "__add__", [](VM* vm, Args& args) {
  396. const List& self = CAST(List&, args[0]);
  397. const List& other = CAST(List&, args[1]);
  398. List new_list(self); // copy construct
  399. new_list.extend(other);
  400. return VAR(std::move(new_list));
  401. });
  402. _vm->bind_method<0>("list", "__len__", [](VM* vm, Args& args) {
  403. const List& self = CAST(List&, args[0]);
  404. return VAR(self.size());
  405. });
  406. _vm->bind_method<0>("list", "__iter__", [](VM* vm, Args& args) {
  407. return vm->PyIter(ArrayIter<List>(vm, args[0]));
  408. });
  409. _vm->bind_method<1>("list", "__getitem__", [](VM* vm, Args& args) {
  410. const List& self = CAST(List&, args[0]);
  411. if(is_type(args[1], vm->tp_slice)){
  412. Slice s = _CAST(Slice, args[1]);
  413. s.normalize(self.size());
  414. List new_list;
  415. for(size_t i = s.start; i < s.stop; i++) new_list.push_back(self[i]);
  416. return VAR(std::move(new_list));
  417. }
  418. int index = CAST(int, args[1]);
  419. index = vm->normalized_index(index, self.size());
  420. return self[index];
  421. });
  422. _vm->bind_method<2>("list", "__setitem__", [](VM* vm, Args& args) {
  423. List& self = CAST(List&, args[0]);
  424. int index = CAST(int, args[1]);
  425. index = vm->normalized_index(index, self.size());
  426. self[index] = args[2];
  427. return vm->None;
  428. });
  429. _vm->bind_method<1>("list", "__delitem__", [](VM* vm, Args& args) {
  430. List& self = CAST(List&, args[0]);
  431. int index = CAST(int, args[1]);
  432. index = vm->normalized_index(index, self.size());
  433. self.erase(index);
  434. return vm->None;
  435. });
  436. /************ PyTuple ************/
  437. _vm->bind_static_method<1>("tuple", "__new__", [](VM* vm, Args& args) {
  438. List list = CAST(List, vm->asList(args[0]));
  439. return VAR(Tuple(std::move(list)));
  440. });
  441. _vm->bind_method<0>("tuple", "__iter__", [](VM* vm, Args& args) {
  442. return vm->PyIter(ArrayIter<Args>(vm, args[0]));
  443. });
  444. _vm->bind_method<1>("tuple", "__getitem__", [](VM* vm, Args& args) {
  445. const Tuple& self = CAST(Tuple&, args[0]);
  446. if(is_type(args[1], vm->tp_slice)){
  447. Slice s = _CAST(Slice, args[1]);
  448. s.normalize(self.size());
  449. List new_list;
  450. for(size_t i = s.start; i < s.stop; i++) new_list.push_back(self[i]);
  451. return VAR(Tuple(std::move(new_list)));
  452. }
  453. int index = CAST(int, args[1]);
  454. index = vm->normalized_index(index, self.size());
  455. return self[index];
  456. });
  457. _vm->bind_method<0>("tuple", "__len__", [](VM* vm, Args& args) {
  458. const Tuple& self = CAST(Tuple&, args[0]);
  459. return VAR(self.size());
  460. });
  461. /************ PyBool ************/
  462. _vm->bind_static_method<1>("bool", "__new__", CPP_LAMBDA(VAR(vm->asBool(args[0]))));
  463. _vm->bind_method<0>("bool", "__repr__", [](VM* vm, Args& args) {
  464. bool val = CAST(bool, args[0]);
  465. return VAR(val ? "True" : "False");
  466. });
  467. _vm->bind_method<0>("bool", "__json__", [](VM* vm, Args& args) {
  468. bool val = CAST(bool, args[0]);
  469. return VAR(val ? "true" : "false");
  470. });
  471. _vm->bind_method<1>("bool", "__xor__", [](VM* vm, Args& args) {
  472. bool self = CAST(bool, args[0]);
  473. bool other = CAST(bool, args[1]);
  474. return VAR(self ^ other);
  475. });
  476. _vm->bind_method<0>("ellipsis", "__repr__", CPP_LAMBDA(VAR("Ellipsis")));
  477. }
  478. #ifdef _WIN32
  479. #define __EXPORT __declspec(dllexport) inline
  480. #elif __APPLE__
  481. #define __EXPORT __attribute__((visibility("default"))) __attribute__((used)) inline
  482. #elif __EMSCRIPTEN__
  483. #include <emscripten.h>
  484. #define __EXPORT EMSCRIPTEN_KEEPALIVE inline
  485. #else
  486. #define __EXPORT inline
  487. #endif
  488. inline void add_module_time(VM* vm){
  489. PyObject* mod = vm->new_module("time");
  490. vm->bind_func<0>(mod, "time", [](VM* vm, Args& args) {
  491. auto now = std::chrono::high_resolution_clock::now();
  492. return VAR(std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count() / 1000000.0);
  493. });
  494. }
  495. inline void add_module_sys(VM* vm){
  496. PyObject* mod = vm->new_module("sys");
  497. vm->setattr(mod, "version", VAR(PK_VERSION));
  498. vm->bind_func<0>(mod, "getrecursionlimit", CPP_LAMBDA(VAR(vm->recursionlimit)));
  499. vm->bind_func<1>(mod, "setrecursionlimit", [](VM* vm, Args& args) {
  500. vm->recursionlimit = CAST(int, args[0]);
  501. return vm->None;
  502. });
  503. }
  504. inline void add_module_json(VM* vm){
  505. PyObject* mod = vm->new_module("json");
  506. vm->bind_func<1>(mod, "loads", [](VM* vm, Args& args) {
  507. const Str& expr = CAST(Str&, args[0]);
  508. CodeObject_ code = vm->compile(expr, "<json>", JSON_MODE);
  509. return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
  510. });
  511. vm->bind_func<1>(mod, "dumps", CPP_LAMBDA(vm->fast_call(__json__, Args{args[0]})));
  512. }
  513. inline void add_module_math(VM* vm){
  514. PyObject* mod = vm->new_module("math");
  515. vm->setattr(mod, "pi", VAR(3.1415926535897932384));
  516. vm->setattr(mod, "e" , VAR(2.7182818284590452354));
  517. vm->bind_func<1>(mod, "log", CPP_LAMBDA(VAR(std::log(vm->num_to_float(args[0])))));
  518. vm->bind_func<1>(mod, "log10", CPP_LAMBDA(VAR(std::log10(vm->num_to_float(args[0])))));
  519. vm->bind_func<1>(mod, "log2", CPP_LAMBDA(VAR(std::log2(vm->num_to_float(args[0])))));
  520. vm->bind_func<1>(mod, "sin", CPP_LAMBDA(VAR(std::sin(vm->num_to_float(args[0])))));
  521. vm->bind_func<1>(mod, "cos", CPP_LAMBDA(VAR(std::cos(vm->num_to_float(args[0])))));
  522. vm->bind_func<1>(mod, "tan", CPP_LAMBDA(VAR(std::tan(vm->num_to_float(args[0])))));
  523. vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(VAR(std::isnan(vm->num_to_float(args[0])))));
  524. vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(VAR(std::isinf(vm->num_to_float(args[0])))));
  525. vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(VAR(std::fabs(vm->num_to_float(args[0])))));
  526. vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(vm->num_to_float(args[0])))));
  527. vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(VAR((i64)std::ceil(vm->num_to_float(args[0])))));
  528. vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(VAR(std::sqrt(vm->num_to_float(args[0])))));
  529. }
  530. inline void add_module_dis(VM* vm){
  531. PyObject* mod = vm->new_module("dis");
  532. vm->bind_func<1>(mod, "dis", [](VM* vm, Args& args) {
  533. PyObject* f = args[0];
  534. if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, args[0]).method;
  535. CodeObject_ code = CAST(Function&, f).decl->code;
  536. (*vm->_stdout) << vm->disassemble(code);
  537. return vm->None;
  538. });
  539. }
  540. struct ReMatch {
  541. PY_CLASS(ReMatch, re, Match)
  542. i64 start;
  543. i64 end;
  544. std::cmatch m;
  545. ReMatch(i64 start, i64 end, std::cmatch m) : start(start), end(end), m(m) {}
  546. static void _register(VM* vm, PyObject* mod, PyObject* type){
  547. vm->bind_method<-1>(type, "__init__", CPP_NOT_IMPLEMENTED());
  548. vm->bind_method<0>(type, "start", CPP_LAMBDA(VAR(CAST(ReMatch&, args[0]).start)));
  549. vm->bind_method<0>(type, "end", CPP_LAMBDA(VAR(CAST(ReMatch&, args[0]).end)));
  550. vm->bind_method<0>(type, "span", [](VM* vm, Args& args) {
  551. auto& self = CAST(ReMatch&, args[0]);
  552. return VAR(Tuple({VAR(self.start), VAR(self.end)}));
  553. });
  554. vm->bind_method<1>(type, "group", [](VM* vm, Args& args) {
  555. auto& self = CAST(ReMatch&, args[0]);
  556. int index = CAST(int, args[1]);
  557. index = vm->normalized_index(index, self.m.size());
  558. return VAR(self.m[index].str());
  559. });
  560. }
  561. };
  562. inline PyObject* _regex_search(const Str& pattern, const Str& string, bool from_start, VM* vm){
  563. std::regex re(pattern.begin(), pattern.end());
  564. std::cmatch m;
  565. if(std::regex_search(string.begin(), string.end(), m, re)){
  566. if(from_start && m.position() != 0) return vm->None;
  567. i64 start = string._byte_index_to_unicode(m.position());
  568. i64 end = string._byte_index_to_unicode(m.position() + m.length());
  569. return VAR_T(ReMatch, start, end, m);
  570. }
  571. return vm->None;
  572. };
  573. inline void add_module_re(VM* vm){
  574. PyObject* mod = vm->new_module("re");
  575. ReMatch::register_class(vm, mod);
  576. vm->bind_func<2>(mod, "match", [](VM* vm, Args& args) {
  577. const Str& pattern = CAST(Str&, args[0]);
  578. const Str& string = CAST(Str&, args[1]);
  579. return _regex_search(pattern, string, true, vm);
  580. });
  581. vm->bind_func<2>(mod, "search", [](VM* vm, Args& args) {
  582. const Str& pattern = CAST(Str&, args[0]);
  583. const Str& string = CAST(Str&, args[1]);
  584. return _regex_search(pattern, string, false, vm);
  585. });
  586. vm->bind_func<3>(mod, "sub", [](VM* vm, Args& args) {
  587. const Str& pattern = CAST(Str&, args[0]);
  588. const Str& repl = CAST(Str&, args[1]);
  589. const Str& string = CAST(Str&, args[2]);
  590. std::regex re(pattern.begin(), pattern.end());
  591. return VAR(std::regex_replace(string.str(), re, repl.str()));
  592. });
  593. vm->bind_func<2>(mod, "split", [](VM* vm, Args& args) {
  594. const Str& pattern = CAST(Str&, args[0]);
  595. const Str& string = CAST(Str&, args[1]);
  596. std::regex re(pattern.begin(), pattern.end());
  597. std::cregex_token_iterator it(string.begin(), string.end(), re, -1);
  598. std::cregex_token_iterator end;
  599. List vec;
  600. for(; it != end; ++it){
  601. vec.push_back(VAR(it->str()));
  602. }
  603. return VAR(vec);
  604. });
  605. }
  606. struct Random{
  607. PY_CLASS(Random, random, Random)
  608. std::mt19937 gen;
  609. Random(){
  610. gen.seed(std::chrono::high_resolution_clock::now().time_since_epoch().count());
  611. }
  612. i64 randint(i64 a, i64 b) {
  613. std::uniform_int_distribution<i64> dis(a, b);
  614. return dis(gen);
  615. }
  616. f64 random() {
  617. std::uniform_real_distribution<f64> dis(0.0, 1.0);
  618. return dis(gen);
  619. }
  620. f64 uniform(f64 a, f64 b) {
  621. std::uniform_real_distribution<f64> dis(a, b);
  622. return dis(gen);
  623. }
  624. void seed(i64 seed) {
  625. gen.seed(seed);
  626. }
  627. static void _register(VM* vm, PyObject* mod, PyObject* type){
  628. vm->bind_static_method<0>(type, "__new__", CPP_LAMBDA(VAR_T(Random)));
  629. vm->bind_method<1>(type, "seed", native_proxy_callable(&Random::seed));
  630. vm->bind_method<2>(type, "randint", native_proxy_callable(&Random::randint));
  631. vm->bind_method<0>(type, "random", native_proxy_callable(&Random::random));
  632. vm->bind_method<2>(type, "uniform", native_proxy_callable(&Random::uniform));
  633. }
  634. };
  635. inline void add_module_random(VM* vm){
  636. PyObject* mod = vm->new_module("random");
  637. Random::register_class(vm, mod);
  638. CodeObject_ code = vm->compile(kPythonLibs["random"], "random.py", EXEC_MODE);
  639. vm->_exec(code, mod);
  640. }
  641. inline void add_module_gc(VM* vm){
  642. PyObject* mod = vm->new_module("gc");
  643. vm->bind_func<0>(mod, "collect", CPP_LAMBDA(VAR(vm->heap.collect())));
  644. }
  645. inline void VM::post_init(){
  646. init_builtins(this);
  647. #if !DEBUG_NO_BUILTIN_MODULES
  648. add_module_sys(this);
  649. add_module_time(this);
  650. add_module_json(this);
  651. add_module_math(this);
  652. add_module_re(this);
  653. add_module_dis(this);
  654. add_module_random(this);
  655. add_module_io(this);
  656. add_module_os(this);
  657. // add_module_c(this);
  658. add_module_gc(this);
  659. for(const char* name: {"this", "functools", "collections", "heapq", "bisect"}){
  660. _lazy_modules[name] = kPythonLibs[name];
  661. }
  662. CodeObject_ code = compile(kPythonLibs["builtins"], "<builtins>", EXEC_MODE);
  663. this->_exec(code, this->builtins);
  664. code = compile(kPythonLibs["_dict"], "<builtins>", EXEC_MODE);
  665. this->_exec(code, this->builtins);
  666. code = compile(kPythonLibs["_set"], "<builtins>", EXEC_MODE);
  667. this->_exec(code, this->builtins);
  668. // property is defined in builtins.py so we need to add it after builtins is loaded
  669. _t(tp_object)->attr().set(__class__, property(CPP_LAMBDA(vm->_t(args[0]))));
  670. _t(tp_type)->attr().set(__base__, property([](VM* vm, Args& args){
  671. const PyTypeInfo& info = vm->_all_types[OBJ_GET(Type, args[0])];
  672. return info.base.index == -1 ? vm->None : vm->_all_types[info.base].obj;
  673. }));
  674. _t(tp_type)->attr().set(__name__, property([](VM* vm, Args& args){
  675. const PyTypeInfo& info = vm->_all_types[OBJ_GET(Type, args[0])];
  676. return VAR(info.name);
  677. }));
  678. #endif
  679. }
  680. } // namespace pkpy
  681. /*************************GLOBAL NAMESPACE*************************/
  682. class PkExportedBase{
  683. public:
  684. virtual ~PkExportedBase() = default;
  685. virtual void* get() = 0;
  686. };
  687. static std::vector<PkExportedBase*> _pk_lookup_table;
  688. template<typename T>
  689. class PkExported : public PkExportedBase{
  690. T* _ptr;
  691. public:
  692. template<typename... Args>
  693. PkExported(Args&&... args) {
  694. _ptr = new T(std::forward<Args>(args)...);
  695. _pk_lookup_table.push_back(this);
  696. }
  697. ~PkExported() override { delete _ptr; }
  698. void* get() override { return _ptr; }
  699. operator T*() { return _ptr; }
  700. };
  701. #define PKPY_ALLOCATE(T, ...) *(new PkExported<T>(__VA_ARGS__))
  702. extern "C" {
  703. __EXPORT
  704. /// Delete a pointer allocated by `pkpy_xxx_xxx`.
  705. /// It can be `VM*`, `REPL*`, `char*`, etc.
  706. ///
  707. /// !!!
  708. /// If the pointer is not allocated by `pkpy_xxx_xxx`, the behavior is undefined.
  709. /// !!!
  710. void pkpy_delete(void* p){
  711. for(int i = 0; i < _pk_lookup_table.size(); i++){
  712. if(_pk_lookup_table[i]->get() == p){
  713. delete _pk_lookup_table[i];
  714. _pk_lookup_table.erase(_pk_lookup_table.begin() + i);
  715. return;
  716. }
  717. }
  718. free(p);
  719. }
  720. __EXPORT
  721. /// Run a given source on a virtual machine.
  722. void pkpy_vm_exec(pkpy::VM* vm, const char* source){
  723. vm->exec(source, "main.py", pkpy::EXEC_MODE);
  724. }
  725. __EXPORT
  726. /// Get a global variable of a virtual machine.
  727. ///
  728. /// Return `__repr__` of the result.
  729. /// If the variable is not found, return `nullptr`.
  730. char* pkpy_vm_get_global(pkpy::VM* vm, const char* name){
  731. pkpy::PyObject* val = vm->_main->attr().try_get(name);
  732. if(val == nullptr) return nullptr;
  733. try{
  734. pkpy::Str repr = pkpy::CAST(pkpy::Str&, vm->asRepr(val));
  735. return repr.c_str_dup();
  736. }catch(...){
  737. return nullptr;
  738. }
  739. }
  740. __EXPORT
  741. /// Evaluate an expression.
  742. ///
  743. /// Return `__repr__` of the result.
  744. /// If there is any error, return `nullptr`.
  745. char* pkpy_vm_eval(pkpy::VM* vm, const char* source){
  746. pkpy::PyObject* ret = vm->exec(source, "<eval>", pkpy::EVAL_MODE);
  747. if(ret == nullptr) return nullptr;
  748. try{
  749. pkpy::Str repr = pkpy::CAST(pkpy::Str&, vm->asRepr(ret));
  750. return repr.c_str_dup();
  751. }catch(...){
  752. return nullptr;
  753. }
  754. }
  755. __EXPORT
  756. /// Create a REPL, using the given virtual machine as the backend.
  757. pkpy::REPL* pkpy_new_repl(pkpy::VM* vm){
  758. return PKPY_ALLOCATE(pkpy::REPL, vm);
  759. }
  760. __EXPORT
  761. /// Input a source line to an interactive console. Return true if need more lines.
  762. bool pkpy_repl_input(pkpy::REPL* r, const char* line){
  763. return r->input(line);
  764. }
  765. __EXPORT
  766. /// Add a source module into a virtual machine.
  767. void pkpy_vm_add_module(pkpy::VM* vm, const char* name, const char* source){
  768. vm->_lazy_modules[name] = source;
  769. }
  770. __EXPORT
  771. /// Create a virtual machine.
  772. pkpy::VM* pkpy_new_vm(bool use_stdio){
  773. return PKPY_ALLOCATE(pkpy::VM, use_stdio);
  774. }
  775. __EXPORT
  776. /// Read the standard output and standard error as string of a virtual machine.
  777. /// The `vm->use_stdio` should be `false`.
  778. /// After this operation, both stream will be cleared.
  779. ///
  780. /// Return a json representing the result.
  781. char* pkpy_vm_read_output(pkpy::VM* vm){
  782. if(vm->is_stdio_used()) return nullptr;
  783. std::stringstream* s_out = (std::stringstream*)(vm->_stdout);
  784. std::stringstream* s_err = (std::stringstream*)(vm->_stderr);
  785. pkpy::Str _stdout = s_out->str();
  786. pkpy::Str _stderr = s_err->str();
  787. std::stringstream ss;
  788. ss << '{' << "\"stdout\": " << _stdout.escape(false);
  789. ss << ", " << "\"stderr\": " << _stderr.escape(false) << '}';
  790. s_out->str(""); s_err->str("");
  791. return strdup(ss.str().c_str());
  792. }
  793. typedef i64 (*f_int_t)(char*);
  794. typedef f64 (*f_float_t)(char*);
  795. typedef bool (*f_bool_t)(char*);
  796. typedef char* (*f_str_t)(char*);
  797. typedef void (*f_None_t)(char*);
  798. static f_int_t f_int = nullptr;
  799. static f_float_t f_float = nullptr;
  800. static f_bool_t f_bool = nullptr;
  801. static f_str_t f_str = nullptr;
  802. static f_None_t f_None = nullptr;
  803. __EXPORT
  804. /// Setup the callback functions.
  805. void pkpy_setup_callbacks(f_int_t _f_int, f_float_t _f_float, f_bool_t _f_bool, f_str_t _f_str, f_None_t _f_None){
  806. f_int = _f_int;
  807. f_float = _f_float;
  808. f_bool = _f_bool;
  809. f_str = _f_str;
  810. f_None = _f_None;
  811. }
  812. __EXPORT
  813. /// Bind a function to a virtual machine.
  814. char* pkpy_vm_bind(pkpy::VM* vm, const char* mod, const char* name, int ret_code){
  815. if(!f_int || !f_float || !f_bool || !f_str || !f_None) return nullptr;
  816. static int kGlobalBindId = 0;
  817. for(int i=0; mod[i]; i++) if(mod[i] == ' ') return nullptr;
  818. for(int i=0; name[i]; i++) if(name[i] == ' ') return nullptr;
  819. std::string f_header = std::string(mod) + '.' + name + '#' + std::to_string(kGlobalBindId++);
  820. pkpy::PyObject* obj = vm->_modules.contains(mod) ? vm->_modules[mod] : vm->new_module(mod);
  821. vm->bind_func<-1>(obj, name, [ret_code, f_header](pkpy::VM* vm, const pkpy::Args& args){
  822. std::stringstream ss;
  823. ss << f_header;
  824. for(int i=0; i<args.size(); i++){
  825. ss << ' ';
  826. pkpy::PyObject* x = vm->fast_call(pkpy::__json__, pkpy::Args{args[i]});
  827. ss << pkpy::CAST(pkpy::Str&, x);
  828. }
  829. char* packet = strdup(ss.str().c_str());
  830. switch(ret_code){
  831. case 'i': return VAR(f_int(packet));
  832. case 'f': return VAR(f_float(packet));
  833. case 'b': return VAR(f_bool(packet));
  834. case 's': {
  835. char* p = f_str(packet);
  836. if(p == nullptr) return vm->None;
  837. return VAR(p); // no need to free(p)
  838. }
  839. case 'N': f_None(packet); return vm->None;
  840. }
  841. free(packet);
  842. UNREACHABLE();
  843. return vm->None;
  844. });
  845. return strdup(f_header.c_str());
  846. }
  847. }