pocketpy.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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 "base64.h"
  8. #include "cffi.h"
  9. #include "linalg.h"
  10. #include "easing.h"
  11. #include "requests.h"
  12. #include "io.h"
  13. #include "_generated.h"
  14. #include "export.h"
  15. #include "vm.h"
  16. #include "re.h"
  17. #include "random.h"
  18. namespace pkpy {
  19. inline CodeObject_ VM::compile(Str source, Str filename, CompileMode mode, bool unknown_global_scope) {
  20. Compiler compiler(this, source, filename, mode, unknown_global_scope);
  21. try{
  22. return compiler.compile();
  23. }catch(Exception& e){
  24. #if DEBUG_FULL_EXCEPTION
  25. std::cerr << e.summary() << std::endl;
  26. #endif
  27. _error(e);
  28. return nullptr;
  29. }
  30. }
  31. inline void init_builtins(VM* _vm) {
  32. #define BIND_NUM_ARITH_OPT(name, op) \
  33. _vm->bind##name(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  34. if(is_int(rhs)) return VAR(_CAST(i64, lhs) op _CAST(i64, rhs)); \
  35. if(is_float(rhs)) return VAR(_CAST(i64, lhs) op _CAST(f64, rhs)); \
  36. return vm->NotImplemented; \
  37. }); \
  38. _vm->bind##name(_vm->tp_float, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  39. if(is_float(rhs)) return VAR(_CAST(f64, lhs) op _CAST(f64, rhs)); \
  40. if(is_int(rhs)) return VAR(_CAST(f64, lhs) op _CAST(i64, rhs)); \
  41. return vm->NotImplemented; \
  42. });
  43. BIND_NUM_ARITH_OPT(__add__, +)
  44. BIND_NUM_ARITH_OPT(__sub__, -)
  45. BIND_NUM_ARITH_OPT(__mul__, *)
  46. #undef BIND_NUM_ARITH_OPT
  47. #define BIND_NUM_LOGICAL_OPT(name, op, is_eq) \
  48. _vm->bind##name(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  49. if(is_int(rhs)) return _CAST(i64, lhs) op _CAST(i64, rhs); \
  50. if(is_float(rhs)) return _CAST(i64, lhs) op _CAST(f64, rhs); \
  51. if constexpr(is_eq) return lhs op rhs; \
  52. vm->TypeError("unsupported operand type(s) for " #op ); \
  53. return false; \
  54. }); \
  55. _vm->bind##name(_vm->tp_float, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  56. if(is_int(rhs)) return _CAST(f64, lhs) op _CAST(i64, rhs); \
  57. if(is_float(rhs)) return _CAST(f64, lhs) op _CAST(f64, rhs); \
  58. if constexpr(is_eq) return lhs op rhs; \
  59. vm->TypeError("unsupported operand type(s) for " #op ); \
  60. return false; \
  61. });
  62. BIND_NUM_LOGICAL_OPT(__lt__, <, false)
  63. BIND_NUM_LOGICAL_OPT(__le__, <=, false)
  64. BIND_NUM_LOGICAL_OPT(__gt__, >, false)
  65. BIND_NUM_LOGICAL_OPT(__ge__, >=, false)
  66. BIND_NUM_LOGICAL_OPT(__eq__, ==, true)
  67. #undef BIND_NUM_ARITH_OPT
  68. #undef BIND_NUM_LOGICAL_OPT
  69. _vm->bind_builtin_func<2>("super", [](VM* vm, ArgsView args) {
  70. vm->check_non_tagged_type(args[0], vm->tp_type);
  71. Type type = OBJ_GET(Type, args[0]);
  72. if(!vm->isinstance(args[1], type)){
  73. Str _0 = obj_type_name(vm, OBJ_GET(Type, vm->_t(args[1])));
  74. Str _1 = obj_type_name(vm, type);
  75. vm->TypeError("super(): " + _0.escape() + " is not an instance of " + _1.escape());
  76. }
  77. Type base = vm->_all_types[type].base;
  78. return vm->heap.gcnew(vm->tp_super, Super(args[1], base));
  79. });
  80. _vm->bind_builtin_func<2>("isinstance", [](VM* vm, ArgsView args) {
  81. vm->check_non_tagged_type(args[1], vm->tp_type);
  82. Type type = OBJ_GET(Type, args[1]);
  83. return VAR(vm->isinstance(args[0], type));
  84. });
  85. _vm->bind_builtin_func<0>("globals", [](VM* vm, ArgsView args) {
  86. PyObject* mod = vm->top_frame()->_module;
  87. return VAR(MappingProxy(mod));
  88. });
  89. _vm->bind_builtin_func<3>("pow", [](VM* vm, ArgsView args) {
  90. i64 lhs = CAST(i64, args[0]); // assume lhs>=0
  91. i64 rhs = CAST(i64, args[1]); // assume rhs>=0
  92. i64 mod = CAST(i64, args[2]); // assume mod>0
  93. i64 res = 1;
  94. while(rhs){
  95. if(rhs & 1) res = res*lhs % mod;
  96. lhs = lhs*lhs % mod;
  97. rhs >>= 1;
  98. }
  99. return VAR(res);
  100. });
  101. _vm->bind_builtin_func<1>("id", [](VM* vm, ArgsView args) {
  102. PyObject* obj = args[0];
  103. if(is_tagged(obj)) return vm->None;
  104. return VAR_T(VoidP, obj);
  105. });
  106. _vm->bind_builtin_func<1>("staticmethod", [](VM* vm, ArgsView args) {
  107. return args[0];
  108. });
  109. _vm->bind_builtin_func<1>("__import__", [](VM* vm, ArgsView args) {
  110. return vm->py_import(CAST(Str&, args[0]));
  111. });
  112. _vm->bind_builtin_func<2>("divmod", [](VM* vm, ArgsView args) {
  113. if(is_int(args[0])){
  114. i64 lhs = _CAST(i64, args[0]);
  115. i64 rhs = CAST(i64, args[1]);
  116. auto res = std::div(lhs, rhs);
  117. return VAR(Tuple({VAR(res.quot), VAR(res.rem)}));
  118. }else{
  119. DEF_SNAME(__divmod__);
  120. return vm->call_method(args[0], __divmod__, args[1]);
  121. }
  122. });
  123. _vm->bind_builtin_func<1>("eval", [](VM* vm, ArgsView args) {
  124. CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<eval>", EVAL_MODE, true);
  125. FrameId frame = vm->top_frame();
  126. return vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals);
  127. });
  128. _vm->bind_builtin_func<1>("exec", [](VM* vm, ArgsView args) {
  129. CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<exec>", EXEC_MODE, true);
  130. FrameId frame = vm->top_frame();
  131. vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals);
  132. return vm->None;
  133. });
  134. _vm->bind_builtin_func<-1>("exit", [](VM* vm, ArgsView args) {
  135. if(args.size() == 0) std::exit(0);
  136. else if(args.size() == 1) std::exit(CAST(int, args[0]));
  137. else vm->TypeError("exit() takes at most 1 argument");
  138. return vm->None;
  139. });
  140. _vm->bind_builtin_func<1>("repr", CPP_LAMBDA(vm->py_repr(args[0])));
  141. _vm->bind_builtin_func<1>("len", [](VM* vm, ArgsView args){
  142. const PyTypeInfo* ti = vm->_inst_type_info(args[0]);
  143. if(ti->m__len__) return VAR(ti->m__len__(vm, args[0]));
  144. return vm->call_method(args[0], __len__);
  145. });
  146. _vm->bind_builtin_func<1>("hash", [](VM* vm, ArgsView args){
  147. i64 value = vm->py_hash(args[0]);
  148. if(((value << 2) >> 2) != value) value >>= 2;
  149. return VAR(value);
  150. });
  151. _vm->bind_builtin_func<1>("chr", [](VM* vm, ArgsView args) {
  152. i64 i = CAST(i64, args[0]);
  153. if (i < 0 || i > 128) vm->ValueError("chr() arg not in range(128)");
  154. return VAR(std::string(1, (char)i));
  155. });
  156. _vm->bind_builtin_func<1>("ord", [](VM* vm, ArgsView args) {
  157. const Str& s = CAST(Str&, args[0]);
  158. if (s.length()!=1) vm->TypeError("ord() expected an ASCII character");
  159. return VAR((i64)(s[0]));
  160. });
  161. _vm->bind_builtin_func<2>("hasattr", [](VM* vm, ArgsView args) {
  162. return VAR(vm->getattr(args[0], CAST(Str&, args[1]), false) != nullptr);
  163. });
  164. _vm->bind_builtin_func<3>("setattr", [](VM* vm, ArgsView args) {
  165. vm->setattr(args[0], CAST(Str&, args[1]), args[2]);
  166. return vm->None;
  167. });
  168. _vm->bind_builtin_func<2>("getattr", [](VM* vm, ArgsView args) {
  169. const Str& name = CAST(Str&, args[1]);
  170. return vm->getattr(args[0], name);
  171. });
  172. _vm->bind_builtin_func<1>("hex", [](VM* vm, ArgsView args) {
  173. std::stringstream ss;
  174. ss << std::hex << CAST(i64, args[0]);
  175. return VAR("0x" + ss.str());
  176. });
  177. _vm->bind_builtin_func<1>("iter", [](VM* vm, ArgsView args) {
  178. return vm->py_iter(args[0]);
  179. });
  180. _vm->bind_builtin_func<1>("next", [](VM* vm, ArgsView args) {
  181. return vm->py_next(args[0]);
  182. });
  183. _vm->bind_builtin_func<1>("dir", [](VM* vm, ArgsView args) {
  184. std::set<StrName> names;
  185. if(!is_tagged(args[0]) && args[0]->is_attr_valid()){
  186. std::vector<StrName> keys = args[0]->attr().keys();
  187. names.insert(keys.begin(), keys.end());
  188. }
  189. const NameDict& t_attr = vm->_t(args[0])->attr();
  190. std::vector<StrName> keys = t_attr.keys();
  191. names.insert(keys.begin(), keys.end());
  192. List ret;
  193. for (StrName name : names) ret.push_back(VAR(name.sv()));
  194. return VAR(std::move(ret));
  195. });
  196. _vm->bind__repr__(_vm->tp_object, [](VM* vm, PyObject* obj) {
  197. if(is_tagged(obj)) FATAL_ERROR();
  198. std::stringstream ss;
  199. ss << "<" << OBJ_NAME(vm->_t(obj)) << " object at 0x";
  200. ss << std::hex << reinterpret_cast<intptr_t>(obj) << ">";
  201. return VAR(ss.str());
  202. });
  203. _vm->bind__eq__(_vm->tp_object, [](VM* vm, PyObject* lhs, PyObject* rhs) { return lhs == rhs; });
  204. _vm->bind__hash__(_vm->tp_object, [](VM* vm, PyObject* obj) { return BITS(obj); });
  205. _vm->cached_object__new__ = _vm->bind_constructor<1>("object", [](VM* vm, ArgsView args) {
  206. vm->check_non_tagged_type(args[0], vm->tp_type);
  207. Type t = OBJ_GET(Type, args[0]);
  208. return vm->heap.gcnew<DummyInstance>(t, {});
  209. });
  210. _vm->bind_constructor<2>("type", CPP_LAMBDA(vm->_t(args[1])));
  211. _vm->bind_constructor<-1>("range", [](VM* vm, ArgsView args) {
  212. args._begin += 1; // skip cls
  213. Range r;
  214. switch (args.size()) {
  215. case 1: r.stop = CAST(i64, args[0]); break;
  216. case 2: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); break;
  217. case 3: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); r.step = CAST(i64, args[2]); break;
  218. default: vm->TypeError("expected 1-3 arguments, but got " + std::to_string(args.size()));
  219. }
  220. return VAR(r);
  221. });
  222. _vm->bind__iter__(_vm->tp_range, [](VM* vm, PyObject* obj) { return VAR_T(RangeIter, OBJ_GET(Range, obj)); });
  223. _vm->bind__repr__(_vm->_type("NoneType"), [](VM* vm, PyObject* obj) { return VAR("None"); });
  224. _vm->bind__json__(_vm->_type("NoneType"), [](VM* vm, PyObject* obj) { return VAR("null"); });
  225. _vm->bind__truediv__(_vm->tp_float, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  226. f64 value = CAST_F(rhs);
  227. return VAR(_CAST(f64, lhs) / value);
  228. });
  229. _vm->bind__truediv__(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  230. f64 value = CAST_F(rhs);
  231. return VAR(_CAST(i64, lhs) / value);
  232. });
  233. auto py_number_pow = [](VM* vm, PyObject* lhs_, PyObject* rhs_) {
  234. if(is_both_int(lhs_, rhs_)){
  235. i64 lhs = _CAST(i64, lhs_);
  236. i64 rhs = _CAST(i64, rhs_);
  237. bool flag = false;
  238. if(rhs < 0) {flag = true; rhs = -rhs;}
  239. i64 ret = 1;
  240. while(rhs){
  241. if(rhs & 1) ret *= lhs;
  242. lhs *= lhs;
  243. rhs >>= 1;
  244. }
  245. if(flag) return VAR((f64)(1.0 / ret));
  246. return VAR(ret);
  247. }else{
  248. return VAR((f64)std::pow(CAST_F(lhs_), CAST_F(rhs_)));
  249. }
  250. };
  251. _vm->bind__pow__(_vm->tp_int, py_number_pow);
  252. _vm->bind__pow__(_vm->tp_float, py_number_pow);
  253. /************ int ************/
  254. _vm->bind_constructor<2>("int", [](VM* vm, ArgsView args) {
  255. if (is_type(args[1], vm->tp_float)) return VAR((i64)CAST(f64, args[1]));
  256. if (is_type(args[1], vm->tp_int)) return args[1];
  257. if (is_type(args[1], vm->tp_bool)) return VAR(_CAST(bool, args[1]) ? 1 : 0);
  258. if (is_type(args[1], vm->tp_str)) {
  259. const Str& s = CAST(Str&, args[1]);
  260. try{
  261. size_t parsed = 0;
  262. i64 val = Number::stoi(s.str(), &parsed, 10);
  263. if(parsed != s.length()) throw std::invalid_argument("<?>");
  264. return VAR(val);
  265. }catch(std::invalid_argument&){
  266. vm->ValueError("invalid literal for int(): " + s.escape());
  267. }
  268. }
  269. vm->TypeError("int() argument must be a int, float, bool or str");
  270. return vm->None;
  271. });
  272. _vm->bind_method<0>("int", "bit_length", [](VM* vm, ArgsView args) {
  273. i64 x = _CAST(i64, args[0]);
  274. if(x < 0) x = -x;
  275. int bits = 0;
  276. while(x){ x >>= 1; bits++; }
  277. return VAR(bits);
  278. });
  279. _vm->bind__floordiv__(_vm->tp_int, [](VM* vm, PyObject* lhs_, PyObject* rhs_) {
  280. i64 rhs = CAST(i64, rhs_);
  281. return VAR(_CAST(i64, lhs_) / rhs);
  282. });
  283. _vm->bind__mod__(_vm->tp_int, [](VM* vm, PyObject* lhs_, PyObject* rhs_) {
  284. i64 rhs = CAST(i64, rhs_);
  285. return VAR(_CAST(i64, lhs_) % rhs);
  286. });
  287. _vm->bind__repr__(_vm->tp_int, [](VM* vm, PyObject* obj) { return VAR(std::to_string(_CAST(i64, obj))); });
  288. _vm->bind__json__(_vm->tp_int, [](VM* vm, PyObject* obj) { return VAR(std::to_string(_CAST(i64, obj))); });
  289. _vm->bind__neg__(_vm->tp_int, [](VM* vm, PyObject* obj) { return VAR(-_CAST(i64, obj)); });
  290. _vm->bind__hash__(_vm->tp_int, [](VM* vm, PyObject* obj) { return _CAST(i64, obj); });
  291. #define INT_BITWISE_OP(name, op) \
  292. _vm->bind##name(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  293. return VAR(_CAST(i64, lhs) op CAST(i64, rhs)); \
  294. });
  295. INT_BITWISE_OP(__lshift__, <<)
  296. INT_BITWISE_OP(__rshift__, >>)
  297. INT_BITWISE_OP(__and__, &)
  298. INT_BITWISE_OP(__or__, |)
  299. INT_BITWISE_OP(__xor__, ^)
  300. #undef INT_BITWISE_OP
  301. /************ float ************/
  302. _vm->bind_constructor<2>("float", [](VM* vm, ArgsView args) {
  303. if (is_type(args[1], vm->tp_int)) return VAR((f64)CAST(i64, args[1]));
  304. if (is_type(args[1], vm->tp_float)) return args[1];
  305. if (is_type(args[1], vm->tp_bool)) return VAR(_CAST(bool, args[1]) ? 1.0 : 0.0);
  306. if (is_type(args[1], vm->tp_str)) {
  307. const Str& s = CAST(Str&, args[1]);
  308. if(s == "inf") return VAR(INFINITY);
  309. if(s == "-inf") return VAR(-INFINITY);
  310. try{
  311. f64 val = Number::stof(s.str());
  312. return VAR(val);
  313. }catch(std::invalid_argument&){
  314. vm->ValueError("invalid literal for float(): " + s.escape());
  315. }
  316. }
  317. vm->TypeError("float() argument must be a int, float, bool or str");
  318. return vm->None;
  319. });
  320. _vm->bind__hash__(_vm->tp_float, [](VM* vm, PyObject* obj) {
  321. f64 val = _CAST(f64, obj);
  322. return (i64)std::hash<f64>()(val);
  323. });
  324. _vm->bind__neg__(_vm->tp_float, [](VM* vm, PyObject* obj) { return VAR(-_CAST(f64, obj)); });
  325. _vm->bind__repr__(_vm->tp_float, [](VM* vm, PyObject* obj) {
  326. f64 val = _CAST(f64, obj);
  327. if(std::isinf(val) || std::isnan(val)) return VAR(std::to_string(val));
  328. std::stringstream ss;
  329. ss << std::setprecision(std::numeric_limits<f64>::max_digits10-2) << val;
  330. std::string s = ss.str();
  331. if(std::all_of(s.begin()+1, s.end(), isdigit)) s += ".0";
  332. return VAR(s);
  333. });
  334. _vm->bind__json__(_vm->tp_float, [](VM* vm, PyObject* obj) {
  335. f64 val = _CAST(f64, obj);
  336. if(std::isinf(val) || std::isnan(val)) vm->ValueError("cannot jsonify 'nan' or 'inf'");
  337. return VAR(std::to_string(val));
  338. });
  339. /************ str ************/
  340. _vm->bind_constructor<2>("str", CPP_LAMBDA(vm->py_str(args[1])));
  341. _vm->bind__hash__(_vm->tp_str, [](VM* vm, PyObject* obj) {
  342. return (i64)_CAST(Str&, obj).hash();
  343. });
  344. _vm->bind__add__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  345. return VAR(_CAST(Str&, lhs) + CAST(Str&, rhs));
  346. });
  347. _vm->bind__len__(_vm->tp_str, [](VM* vm, PyObject* obj) {
  348. return (i64)_CAST(Str&, obj).u8_length();
  349. });
  350. _vm->bind__mul__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  351. const Str& self = _CAST(Str&, lhs);
  352. i64 n = CAST(i64, rhs);
  353. std::stringstream ss;
  354. for(i64 i = 0; i < n; i++) ss << self.sv();
  355. return VAR(ss.str());
  356. });
  357. _vm->bind__contains__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  358. const Str& self = _CAST(Str&, lhs);
  359. const Str& other = CAST(Str&, rhs);
  360. return self.index(other) != -1;
  361. });
  362. _vm->bind__str__(_vm->tp_str, [](VM* vm, PyObject* obj) { return obj; });
  363. _vm->bind__iter__(_vm->tp_str, [](VM* vm, PyObject* obj) { return VAR_T(StringIter, obj); });
  364. _vm->bind__repr__(_vm->tp_str, [](VM* vm, PyObject* obj) {
  365. const Str& self = _CAST(Str&, obj);
  366. return VAR(self.escape(true));
  367. });
  368. _vm->bind__json__(_vm->tp_str, [](VM* vm, PyObject* obj) {
  369. const Str& self = _CAST(Str&, obj);
  370. return VAR(self.escape(false));
  371. });
  372. _vm->bind__eq__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  373. if(!is_non_tagged_type(rhs, vm->tp_str)) return false;
  374. return _CAST(Str&, lhs) == _CAST(Str&, rhs);
  375. });
  376. _vm->bind__gt__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  377. return _CAST(Str&, lhs) > CAST(Str&, rhs);
  378. });
  379. _vm->bind__lt__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  380. return _CAST(Str&, lhs) < CAST(Str&, rhs);
  381. });
  382. _vm->bind__ge__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  383. return _CAST(Str&, lhs) >= CAST(Str&, rhs);
  384. });
  385. _vm->bind__le__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  386. return _CAST(Str&, lhs) <= CAST(Str&, rhs);
  387. });
  388. _vm->bind__getitem__(_vm->tp_str, [](VM* vm, PyObject* obj, PyObject* index) {
  389. const Str& self = _CAST(Str&, obj);
  390. if(is_non_tagged_type(index, vm->tp_slice)){
  391. const Slice& s = _CAST(Slice&, index);
  392. int start, stop, step;
  393. vm->parse_int_slice(s, self.u8_length(), start, stop, step);
  394. return VAR(self.u8_slice(start, stop, step));
  395. }
  396. int i = CAST(int, index);
  397. i = vm->normalized_index(i, self.u8_length());
  398. return VAR(self.u8_getitem(i));
  399. });
  400. _vm->bind_method<-1>("str", "replace", [](VM* vm, ArgsView args) {
  401. if(args.size() != 1+2 && args.size() != 1+3) vm->TypeError("replace() takes 2 or 3 arguments");
  402. const Str& self = _CAST(Str&, args[0]);
  403. const Str& old = CAST(Str&, args[1]);
  404. const Str& new_ = CAST(Str&, args[2]);
  405. int count = args.size()==1+3 ? CAST(int, args[3]) : -1;
  406. return VAR(self.replace(old, new_, count));
  407. });
  408. _vm->bind_method<1>("str", "index", [](VM* vm, ArgsView args) {
  409. const Str& self = _CAST(Str&, args[0]);
  410. const Str& sub = CAST(Str&, args[1]);
  411. int index = self.index(sub);
  412. if(index == -1) vm->ValueError("substring not found");
  413. return VAR(index);
  414. });
  415. _vm->bind_method<1>("str", "startswith", [](VM* vm, ArgsView args) {
  416. const Str& self = _CAST(Str&, args[0]);
  417. const Str& prefix = CAST(Str&, args[1]);
  418. return VAR(self.index(prefix) == 0);
  419. });
  420. _vm->bind_method<1>("str", "endswith", [](VM* vm, ArgsView args) {
  421. const Str& self = _CAST(Str&, args[0]);
  422. const Str& suffix = CAST(Str&, args[1]);
  423. int offset = self.length() - suffix.length();
  424. if(offset < 0) return vm->False;
  425. bool ok = memcmp(self.data+offset, suffix.data, suffix.length()) == 0;
  426. return VAR(ok);
  427. });
  428. _vm->bind_method<0>("str", "encode", [](VM* vm, ArgsView args) {
  429. const Str& self = _CAST(Str&, args[0]);
  430. std::vector<char> buffer(self.length());
  431. memcpy(buffer.data(), self.data, self.length());
  432. return VAR(Bytes(std::move(buffer)));
  433. });
  434. _vm->bind_method<1>("str", "join", [](VM* vm, ArgsView args) {
  435. auto _lock = vm->heap.gc_scope_lock();
  436. const Str& self = _CAST(Str&, args[0]);
  437. FastStrStream ss;
  438. PyObject* it = vm->py_iter(args[1]); // strong ref
  439. PyObject* obj = vm->py_next(it);
  440. while(obj != vm->StopIteration){
  441. if(!ss.empty()) ss << self;
  442. ss << CAST(Str&, obj);
  443. obj = vm->py_next(it);
  444. }
  445. return VAR(ss.str());
  446. });
  447. _vm->bind_method<0>("str", "to_c_str", [](VM* vm, ArgsView args){
  448. const Str& self = _CAST(Str&, args[0]);
  449. return VAR(self.c_str_dup());
  450. });
  451. _vm->bind_func<1>("str", "from_c_str", [](VM* vm, ArgsView args){
  452. char* p = CAST(char*, args[0]);
  453. return VAR(Str(p));
  454. });
  455. _vm->bind_method<0>("str", "lower", [](VM* vm, ArgsView args) {
  456. const Str& self = _CAST(Str&, args[0]);
  457. return VAR(self.lower());
  458. });
  459. _vm->bind_method<0>("str", "upper", [](VM* vm, ArgsView args) {
  460. const Str& self = _CAST(Str&, args[0]);
  461. return VAR(self.upper());
  462. });
  463. /************ list ************/
  464. _vm->bind_constructor<2>("list", [](VM* vm, ArgsView args) {
  465. return vm->py_list(args[1]);
  466. });
  467. _vm->bind__contains__(_vm->tp_list, [](VM* vm, PyObject* obj, PyObject* item) {
  468. List& self = _CAST(List&, obj);
  469. for(PyObject* i: self) if(vm->py_equals(i, item)) return true;
  470. return false;
  471. });
  472. _vm->bind_method<1>("list", "count", [](VM* vm, ArgsView args) {
  473. List& self = _CAST(List&, args[0]);
  474. int count = 0;
  475. for(PyObject* i: self) if(vm->py_equals(i, args[1])) count++;
  476. return VAR(count);
  477. });
  478. _vm->bind__eq__(_vm->tp_list, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  479. List& a = _CAST(List&, lhs);
  480. List& b = _CAST(List&, rhs);
  481. if(a.size() != b.size()) return false;
  482. for(int i=0; i<a.size(); i++){
  483. if(!vm->py_equals(a[i], b[i])) return false;
  484. }
  485. return true;
  486. });
  487. _vm->bind_method<1>("list", "index", [](VM* vm, ArgsView args) {
  488. List& self = _CAST(List&, args[0]);
  489. PyObject* obj = args[1];
  490. for(int i=0; i<self.size(); i++){
  491. if(vm->py_equals(self[i], obj)) return VAR(i);
  492. }
  493. vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
  494. return vm->None;
  495. });
  496. _vm->bind_method<1>("list", "remove", [](VM* vm, ArgsView args) {
  497. List& self = _CAST(List&, args[0]);
  498. PyObject* obj = args[1];
  499. for(int i=0; i<self.size(); i++){
  500. if(vm->py_equals(self[i], obj)){
  501. self.erase(i);
  502. return vm->None;
  503. }
  504. }
  505. vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
  506. return vm->None;
  507. });
  508. _vm->bind_method<-1>("list", "pop", [](VM* vm, ArgsView args) {
  509. List& self = _CAST(List&, args[0]);
  510. if(args.size() == 1+0){
  511. if(self.empty()) vm->IndexError("pop from empty list");
  512. return self.popx_back();
  513. }
  514. if(args.size() == 1+1){
  515. int index = CAST(int, args[1]);
  516. index = vm->normalized_index(index, self.size());
  517. PyObject* ret = self[index];
  518. self.erase(index);
  519. return ret;
  520. }
  521. vm->TypeError("pop() takes at most 1 argument");
  522. return vm->None;
  523. });
  524. _vm->bind_method<1>("list", "append", [](VM* vm, ArgsView args) {
  525. List& self = _CAST(List&, args[0]);
  526. self.push_back(args[1]);
  527. return vm->None;
  528. });
  529. _vm->bind_method<1>("list", "extend", [](VM* vm, ArgsView args) {
  530. auto _lock = vm->heap.gc_scope_lock();
  531. List& self = _CAST(List&, args[0]);
  532. PyObject* it = vm->py_iter(args[1]); // strong ref
  533. PyObject* obj = vm->py_next(it);
  534. while(obj != vm->StopIteration){
  535. self.push_back(obj);
  536. obj = vm->py_next(it);
  537. }
  538. return vm->None;
  539. });
  540. _vm->bind_method<0>("list", "reverse", [](VM* vm, ArgsView args) {
  541. List& self = _CAST(List&, args[0]);
  542. std::reverse(self.begin(), self.end());
  543. return vm->None;
  544. });
  545. _vm->bind__mul__(_vm->tp_list, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  546. const List& self = _CAST(List&, lhs);
  547. int n = CAST(int, rhs);
  548. List result;
  549. result.reserve(self.size() * n);
  550. for(int i = 0; i < n; i++) result.extend(self);
  551. return VAR(std::move(result));
  552. });
  553. _vm->bind_method<2>("list", "insert", [](VM* vm, ArgsView args) {
  554. List& self = _CAST(List&, args[0]);
  555. int index = CAST(int, args[1]);
  556. if(index < 0) index += self.size();
  557. if(index < 0) index = 0;
  558. if(index > self.size()) index = self.size();
  559. self.insert(index, args[2]);
  560. return vm->None;
  561. });
  562. _vm->bind_method<0>("list", "clear", [](VM* vm, ArgsView args) {
  563. _CAST(List&, args[0]).clear();
  564. return vm->None;
  565. });
  566. _vm->bind_method<0>("list", "copy", CPP_LAMBDA(VAR(_CAST(List, args[0]))));
  567. _vm->bind__hash__(_vm->tp_list, [](VM* vm, PyObject* obj) {
  568. vm->TypeError("unhashable type: 'list'");
  569. return (i64)0;
  570. });
  571. _vm->bind__add__(_vm->tp_list, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  572. const List& self = _CAST(List&, lhs);
  573. const List& other = CAST(List&, rhs);
  574. List new_list(self); // copy construct
  575. new_list.extend(other);
  576. return VAR(std::move(new_list));
  577. });
  578. _vm->bind__len__(_vm->tp_list, [](VM* vm, PyObject* obj) {
  579. return (i64)_CAST(List&, obj).size();
  580. });
  581. _vm->bind__iter__(_vm->tp_list, [](VM* vm, PyObject* obj) {
  582. List& self = _CAST(List&, obj);
  583. return VAR_T(ArrayIter, obj, self.begin(), self.end());
  584. });
  585. _vm->bind__getitem__(_vm->tp_list, PyArrayGetItem<List>);
  586. _vm->bind__setitem__(_vm->tp_list, [](VM* vm, PyObject* obj, PyObject* index, PyObject* value){
  587. List& self = _CAST(List&, obj);
  588. int i = CAST(int, index);
  589. i = vm->normalized_index(i, self.size());
  590. self[i] = value;
  591. });
  592. _vm->bind__delitem__(_vm->tp_list, [](VM* vm, PyObject* obj, PyObject* index){
  593. List& self = _CAST(List&, obj);
  594. int i = CAST(int, index);
  595. i = vm->normalized_index(i, self.size());
  596. self.erase(i);
  597. });
  598. /************ tuple ************/
  599. _vm->bind_constructor<2>("tuple", [](VM* vm, ArgsView args) {
  600. List list = CAST(List, vm->py_list(args[1]));
  601. return VAR(Tuple(std::move(list)));
  602. });
  603. _vm->bind__contains__(_vm->tp_tuple, [](VM* vm, PyObject* obj, PyObject* item) {
  604. Tuple& self = _CAST(Tuple&, obj);
  605. for(PyObject* i: self) if(vm->py_equals(i, item)) return true;
  606. return false;
  607. });
  608. _vm->bind_method<1>("tuple", "count", [](VM* vm, ArgsView args) {
  609. Tuple& self = _CAST(Tuple&, args[0]);
  610. int count = 0;
  611. for(PyObject* i: self) if(vm->py_equals(i, args[1])) count++;
  612. return VAR(count);
  613. });
  614. _vm->bind__eq__(_vm->tp_tuple, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  615. const Tuple& self = _CAST(Tuple&, lhs);
  616. const Tuple& other = CAST(Tuple&, rhs);
  617. if(self.size() != other.size()) return false;
  618. for(int i = 0; i < self.size(); i++) {
  619. if(!vm->py_equals(self[i], other[i])) return false;
  620. }
  621. return true;
  622. });
  623. _vm->bind__hash__(_vm->tp_tuple, [](VM* vm, PyObject* obj) {
  624. i64 x = 1000003;
  625. const Tuple& items = CAST(Tuple&, obj);
  626. for (int i=0; i<items.size(); i++) {
  627. i64 y = vm->py_hash(items[i]);
  628. // recommended by Github Copilot
  629. x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 2));
  630. }
  631. return x;
  632. });
  633. _vm->bind__iter__(_vm->tp_tuple, [](VM* vm, PyObject* obj) {
  634. Tuple& self = _CAST(Tuple&, obj);
  635. return VAR_T(ArrayIter, obj, self.begin(), self.end());
  636. });
  637. _vm->bind__getitem__(_vm->tp_tuple, PyArrayGetItem<Tuple>);
  638. _vm->bind__len__(_vm->tp_tuple, [](VM* vm, PyObject* obj) {
  639. return (i64)_CAST(Tuple&, obj).size();
  640. });
  641. /************ bool ************/
  642. _vm->bind_constructor<2>("bool", CPP_LAMBDA(VAR(vm->py_bool(args[1]))));
  643. _vm->bind__hash__(_vm->tp_bool, [](VM* vm, PyObject* obj) {
  644. return (i64)_CAST(bool, obj);
  645. });
  646. _vm->bind__repr__(_vm->tp_bool, [](VM* vm, PyObject* self) {
  647. bool val = _CAST(bool, self);
  648. return VAR(val ? "True" : "False");
  649. });
  650. _vm->bind__json__(_vm->tp_bool, [](VM* vm, PyObject* self) {
  651. bool val = _CAST(bool, self);
  652. return VAR(val ? "true" : "false");
  653. });
  654. _vm->bind__and__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  655. return VAR(_CAST(bool, lhs) && CAST(bool, rhs));
  656. });
  657. _vm->bind__or__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  658. return VAR(_CAST(bool, lhs) || CAST(bool, rhs));
  659. });
  660. _vm->bind__xor__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  661. return VAR(_CAST(bool, lhs) != CAST(bool, rhs));
  662. });
  663. _vm->bind__eq__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  664. return _CAST(bool, lhs) == CAST(bool, rhs);
  665. });
  666. _vm->bind__repr__(_vm->_type("ellipsis"), [](VM* vm, PyObject* self) {
  667. return VAR("Ellipsis");
  668. });
  669. /************ bytes ************/
  670. _vm->bind_constructor<2>("bytes", [](VM* vm, ArgsView args){
  671. List& list = CAST(List&, args[1]);
  672. std::vector<char> buffer(list.size());
  673. for(int i=0; i<list.size(); i++){
  674. i64 b = CAST(i64, list[i]);
  675. if(b<0 || b>255) vm->ValueError("byte must be in range[0, 256)");
  676. buffer[i] = (char)b;
  677. }
  678. return VAR(Bytes(std::move(buffer)));
  679. });
  680. _vm->bind__getitem__(_vm->tp_bytes, [](VM* vm, PyObject* obj, PyObject* index) {
  681. const Bytes& self = _CAST(Bytes&, obj);
  682. int i = CAST(int, index);
  683. i = vm->normalized_index(i, self.size());
  684. return VAR(self[i]);
  685. });
  686. _vm->bind__hash__(_vm->tp_bytes, [](VM* vm, PyObject* obj) {
  687. const Bytes& self = _CAST(Bytes&, obj);
  688. return (i64)std::hash<std::string>()(self.str());
  689. });
  690. _vm->bind__repr__(_vm->tp_bytes, [](VM* vm, PyObject* obj) {
  691. const Bytes& self = _CAST(Bytes&, obj);
  692. std::stringstream ss;
  693. ss << "b'";
  694. for(int i=0; i<self.size(); i++){
  695. ss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << self[i];
  696. }
  697. ss << "'";
  698. return VAR(ss.str());
  699. });
  700. _vm->bind__len__(_vm->tp_bytes, [](VM* vm, PyObject* obj) {
  701. return (i64)_CAST(Bytes&, obj).size();
  702. });
  703. _vm->bind_method<0>("bytes", "decode", [](VM* vm, ArgsView args) {
  704. const Bytes& self = _CAST(Bytes&, args[0]);
  705. // TODO: check encoding is utf-8
  706. return VAR(Str(self.str()));
  707. });
  708. _vm->bind_method<0>("bytes", "to_char_array", [](VM* vm, ArgsView args) {
  709. const Bytes& self = _CAST(Bytes&, args[0]);
  710. void* buffer = malloc(self.size());
  711. memcpy(buffer, self.data(), self.size());
  712. return VAR_T(VoidP, buffer);
  713. });
  714. _vm->bind_func<2>("bytes", "from_char_array", [](VM* vm, ArgsView args) {
  715. const VoidP& data = _CAST(VoidP&, args[0]);
  716. int size = CAST(int, args[1]);
  717. std::vector<char> buffer(size);
  718. memcpy(buffer.data(), data.ptr, size);
  719. return VAR(Bytes(std::move(buffer)));
  720. });
  721. _vm->bind__eq__(_vm->tp_bytes, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  722. return _CAST(Bytes&, lhs) == _CAST(Bytes&, rhs);
  723. });
  724. /************ slice ************/
  725. _vm->bind_constructor<4>("slice", [](VM* vm, ArgsView args) {
  726. return VAR(Slice(args[1], args[2], args[3]));
  727. });
  728. _vm->bind__repr__(_vm->tp_slice, [](VM* vm, PyObject* obj) {
  729. const Slice& self = _CAST(Slice&, obj);
  730. std::stringstream ss;
  731. ss << "slice(";
  732. ss << CAST(Str, vm->py_repr(self.start)) << ", ";
  733. ss << CAST(Str, vm->py_repr(self.stop)) << ", ";
  734. ss << CAST(Str, vm->py_repr(self.step)) << ")";
  735. return VAR(ss.str());
  736. });
  737. /************ mappingproxy ************/
  738. _vm->bind_method<0>("mappingproxy", "keys", [](VM* vm, ArgsView args) {
  739. MappingProxy& self = _CAST(MappingProxy&, args[0]);
  740. List keys;
  741. for(StrName name : self.attr().keys()) keys.push_back(VAR(name.sv()));
  742. return VAR(std::move(keys));
  743. });
  744. _vm->bind_method<0>("mappingproxy", "values", [](VM* vm, ArgsView args) {
  745. MappingProxy& self = _CAST(MappingProxy&, args[0]);
  746. List values;
  747. for(auto& item : self.attr().items()) values.push_back(item.second);
  748. return VAR(std::move(values));
  749. });
  750. _vm->bind_method<0>("mappingproxy", "items", [](VM* vm, ArgsView args) {
  751. MappingProxy& self = _CAST(MappingProxy&, args[0]);
  752. List items;
  753. for(auto& item : self.attr().items()){
  754. PyObject* t = VAR(Tuple({VAR(item.first.sv()), item.second}));
  755. items.push_back(std::move(t));
  756. }
  757. return VAR(std::move(items));
  758. });
  759. _vm->bind__len__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) {
  760. return (i64)_CAST(MappingProxy&, obj).attr().size();
  761. });
  762. _vm->bind__getitem__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj, PyObject* index) {
  763. MappingProxy& self = _CAST(MappingProxy&, obj);
  764. StrName key = CAST(Str&, index);
  765. PyObject* ret = self.attr().try_get(key);
  766. if(ret == nullptr) vm->AttributeError(key.sv());
  767. return ret;
  768. });
  769. _vm->bind__repr__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) {
  770. MappingProxy& self = _CAST(MappingProxy&, obj);
  771. std::stringstream ss;
  772. ss << "mappingproxy({";
  773. bool first = true;
  774. for(auto& item : self.attr().items()){
  775. if(!first) ss << ", ";
  776. first = false;
  777. ss << item.first.escape() << ": " << CAST(Str, vm->py_repr(item.second));
  778. }
  779. ss << "})";
  780. return VAR(ss.str());
  781. });
  782. _vm->bind__contains__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj, PyObject* key) {
  783. MappingProxy& self = _CAST(MappingProxy&, obj);
  784. return self.attr().contains(CAST(Str&, key));
  785. });
  786. /************ dict ************/
  787. _vm->bind_constructor<-1>("dict", [](VM* vm, ArgsView args){
  788. return VAR(Dict(vm));
  789. });
  790. _vm->bind_method<-1>("dict", "__init__", [](VM* vm, ArgsView args){
  791. if(args.size() == 1+0) return vm->None;
  792. if(args.size() == 1+1){
  793. auto _lock = vm->heap.gc_scope_lock();
  794. Dict& self = _CAST(Dict&, args[0]);
  795. List& list = CAST(List&, args[1]);
  796. for(PyObject* item : list){
  797. Tuple& t = CAST(Tuple&, item);
  798. if(t.size() != 2){
  799. vm->ValueError("dict() takes an iterable of tuples (key, value)");
  800. return vm->None;
  801. }
  802. self.set(t[0], t[1]);
  803. }
  804. return vm->None;
  805. }
  806. vm->TypeError("dict() takes at most 1 argument");
  807. return vm->None;
  808. });
  809. _vm->bind__len__(_vm->tp_dict, [](VM* vm, PyObject* obj) {
  810. return (i64)_CAST(Dict&, obj).size();
  811. });
  812. _vm->bind__getitem__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* index) {
  813. Dict& self = _CAST(Dict&, obj);
  814. PyObject* ret = self.try_get(index);
  815. if(ret == nullptr) vm->KeyError(index);
  816. return ret;
  817. });
  818. _vm->bind__setitem__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* key, PyObject* value) {
  819. Dict& self = _CAST(Dict&, obj);
  820. self.set(key, value);
  821. });
  822. _vm->bind__delitem__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* key) {
  823. Dict& self = _CAST(Dict&, obj);
  824. if(!self.contains(key)) vm->KeyError(key);
  825. self.erase(key);
  826. });
  827. _vm->bind_method<1>("dict", "pop", [](VM* vm, ArgsView args) {
  828. Dict& self = _CAST(Dict&, args[0]);
  829. PyObject* value = self.try_get(args[1]);
  830. if(value == nullptr) vm->KeyError(args[1]);
  831. self.erase(args[1]);
  832. return value;
  833. });
  834. _vm->bind__contains__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* key) {
  835. Dict& self = _CAST(Dict&, obj);
  836. return self.contains(key);
  837. });
  838. _vm->bind_method<-1>("dict", "get", [](VM* vm, ArgsView args) {
  839. Dict& self = _CAST(Dict&, args[0]);
  840. if(args.size() == 1+1){
  841. PyObject* ret = self.try_get(args[1]);
  842. if(ret != nullptr) return ret;
  843. return vm->None;
  844. }else if(args.size() == 1+2){
  845. PyObject* ret = self.try_get(args[1]);
  846. if(ret != nullptr) return ret;
  847. return args[2];
  848. }
  849. vm->TypeError("get() takes at most 2 arguments");
  850. return vm->None;
  851. });
  852. _vm->bind_method<0>("dict", "keys", [](VM* vm, ArgsView args) {
  853. Dict& self = _CAST(Dict&, args[0]);
  854. List keys;
  855. for(auto& item : self.items()) keys.push_back(item.first);
  856. return VAR(std::move(keys));
  857. });
  858. _vm->bind_method<0>("dict", "values", [](VM* vm, ArgsView args) {
  859. Dict& self = _CAST(Dict&, args[0]);
  860. List values;
  861. for(auto& item : self.items()) values.push_back(item.second);
  862. return VAR(std::move(values));
  863. });
  864. _vm->bind_method<0>("dict", "items", [](VM* vm, ArgsView args) {
  865. Dict& self = _CAST(Dict&, args[0]);
  866. List items;
  867. for(auto& item : self.items()){
  868. PyObject* t = VAR(Tuple({item.first, item.second}));
  869. items.push_back(std::move(t));
  870. }
  871. return VAR(std::move(items));
  872. });
  873. _vm->bind_method<1>("dict", "update", [](VM* vm, ArgsView args) {
  874. Dict& self = _CAST(Dict&, args[0]);
  875. const Dict& other = CAST(Dict&, args[1]);
  876. self.update(other);
  877. return vm->None;
  878. });
  879. _vm->bind_method<0>("dict", "copy", [](VM* vm, ArgsView args) {
  880. const Dict& self = _CAST(Dict&, args[0]);
  881. return VAR(self);
  882. });
  883. _vm->bind_method<0>("dict", "clear", [](VM* vm, ArgsView args) {
  884. Dict& self = _CAST(Dict&, args[0]);
  885. self.clear();
  886. return vm->None;
  887. });
  888. _vm->bind__repr__(_vm->tp_dict, [](VM* vm, PyObject* obj) {
  889. Dict& self = _CAST(Dict&, obj);
  890. std::stringstream ss;
  891. ss << "{";
  892. bool first = true;
  893. for(auto& item : self.items()){
  894. if(!first) ss << ", ";
  895. first = false;
  896. Str key = CAST(Str&, vm->py_repr(item.first));
  897. Str value = CAST(Str&, vm->py_repr(item.second));
  898. ss << key << ": " << value;
  899. }
  900. ss << "}";
  901. return VAR(ss.str());
  902. });
  903. _vm->bind__json__(_vm->tp_dict, [](VM* vm, PyObject* obj) {
  904. Dict& self = _CAST(Dict&, obj);
  905. std::stringstream ss;
  906. ss << "{";
  907. bool first = true;
  908. for(auto& item : self.items()){
  909. if(!first) ss << ", ";
  910. first = false;
  911. Str key = CAST(Str&, item.first).escape(false);
  912. Str value = CAST(Str&, vm->py_json(item.second));
  913. ss << key << ": " << value;
  914. }
  915. ss << "}";
  916. return VAR(ss.str());
  917. });
  918. _vm->bind__eq__(_vm->tp_dict, [](VM* vm, PyObject* a, PyObject* b) {
  919. Dict& self = _CAST(Dict&, a);
  920. if(!is_non_tagged_type(b, vm->tp_dict)) return false;
  921. Dict& other = _CAST(Dict&, b);
  922. if(self.size() != other.size()) return false;
  923. for(auto& item : self.items()){
  924. PyObject* value = other.try_get(item.first);
  925. if(value == nullptr) return false;
  926. if(!vm->py_equals(item.second, value)) return false;
  927. }
  928. return true;
  929. });
  930. /************ property ************/
  931. _vm->bind_constructor<-1>("property", [](VM* vm, ArgsView args) {
  932. if(args.size() == 1+1){
  933. return VAR(Property(args[1], vm->None));
  934. }else if(args.size() == 1+2){
  935. return VAR(Property(args[1], args[2]));
  936. }
  937. vm->TypeError("property() takes at most 2 arguments");
  938. return vm->None;
  939. });
  940. RangeIter::register_class(_vm, _vm->builtins);
  941. ArrayIter::register_class(_vm, _vm->builtins);
  942. StringIter::register_class(_vm, _vm->builtins);
  943. Generator::register_class(_vm, _vm->builtins);
  944. }
  945. inline void add_module_timeit(VM* vm){
  946. PyObject* mod = vm->new_module("timeit");
  947. vm->bind_func<2>(mod, "timeit", [](VM* vm, ArgsView args) {
  948. PyObject* f = args[0];
  949. i64 iters = CAST(i64, args[1]);
  950. auto now = std::chrono::system_clock::now();
  951. for(i64 i=0; i<iters; i++) vm->call(f);
  952. auto end = std::chrono::system_clock::now();
  953. f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - now).count() / 1000.0;
  954. return VAR(elapsed);
  955. });
  956. }
  957. inline void add_module_time(VM* vm){
  958. PyObject* mod = vm->new_module("time");
  959. vm->bind_func<0>(mod, "time", [](VM* vm, ArgsView args) {
  960. auto now = std::chrono::system_clock::now();
  961. return VAR(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() / 1000.0);
  962. });
  963. vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
  964. f64 seconds = CAST_F(args[0]);
  965. auto begin = std::chrono::system_clock::now();
  966. while(true){
  967. auto now = std::chrono::system_clock::now();
  968. f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() / 1000.0;
  969. if(elapsed >= seconds) break;
  970. }
  971. return vm->None;
  972. });
  973. vm->bind_func<0>(mod, "localtime", [](VM* vm, ArgsView args) {
  974. auto now = std::chrono::system_clock::now();
  975. std::time_t t = std::chrono::system_clock::to_time_t(now);
  976. std::tm* tm = std::localtime(&t);
  977. Dict d(vm);
  978. d.set(VAR("tm_year"), VAR(tm->tm_year + 1900));
  979. d.set(VAR("tm_mon"), VAR(tm->tm_mon + 1));
  980. d.set(VAR("tm_mday"), VAR(tm->tm_mday));
  981. d.set(VAR("tm_hour"), VAR(tm->tm_hour));
  982. d.set(VAR("tm_min"), VAR(tm->tm_min));
  983. d.set(VAR("tm_sec"), VAR(tm->tm_sec + 1));
  984. d.set(VAR("tm_wday"), VAR((tm->tm_wday + 6) % 7));
  985. d.set(VAR("tm_yday"), VAR(tm->tm_yday + 1));
  986. d.set(VAR("tm_isdst"), VAR(tm->tm_isdst));
  987. return VAR(std::move(d));
  988. });
  989. }
  990. struct PyREPL{
  991. PY_CLASS(PyREPL, sys, _repl)
  992. REPL* repl;
  993. PyREPL(VM* vm){ repl = new REPL(vm); }
  994. ~PyREPL(){ delete repl; }
  995. PyREPL(const PyREPL&) = delete;
  996. PyREPL& operator=(const PyREPL&) = delete;
  997. PyREPL(PyREPL&& other) noexcept{
  998. repl = other.repl;
  999. other.repl = nullptr;
  1000. }
  1001. struct TempOut{
  1002. PrintFunc backup;
  1003. VM* vm;
  1004. TempOut(VM* vm, PrintFunc f){
  1005. this->vm = vm;
  1006. this->backup = vm->_stdout;
  1007. vm->_stdout = f;
  1008. }
  1009. ~TempOut(){
  1010. vm->_stdout = backup;
  1011. }
  1012. TempOut(const TempOut&) = delete;
  1013. TempOut& operator=(const TempOut&) = delete;
  1014. TempOut(TempOut&&) = delete;
  1015. TempOut& operator=(TempOut&&) = delete;
  1016. };
  1017. static void _register(VM* vm, PyObject* mod, PyObject* type){
  1018. vm->bind_constructor<1>(type, [](VM* vm, ArgsView args){
  1019. return VAR_T(PyREPL, vm);
  1020. });
  1021. vm->bind_method<1>(type, "input", [](VM* vm, ArgsView args){
  1022. PyREPL& self = _CAST(PyREPL&, args[0]);
  1023. const Str& s = CAST(Str&, args[1]);
  1024. static std::stringstream ss_out;
  1025. ss_out.str("");
  1026. TempOut _(vm, [](VM* vm, const Str& s){ ss_out << s; });
  1027. bool ok = self.repl->input(s.str());
  1028. return VAR(Tuple({VAR(ok), VAR(ss_out.str())}));
  1029. });
  1030. }
  1031. };
  1032. inline void add_module_sys(VM* vm){
  1033. PyObject* mod = vm->new_module("sys");
  1034. PyREPL::register_class(vm, mod);
  1035. vm->setattr(mod, "version", VAR(PK_VERSION));
  1036. PyObject* stdout_ = vm->heap.gcnew<DummyInstance>(vm->tp_object, {});
  1037. PyObject* stderr_ = vm->heap.gcnew<DummyInstance>(vm->tp_object, {});
  1038. PyObject* stdin_ = vm->heap.gcnew<DummyInstance>(vm->tp_object, {});
  1039. vm->setattr(mod, "stdout", stdout_);
  1040. vm->setattr(mod, "stderr", stderr_);
  1041. vm->setattr(mod, "stdin", stdin_);
  1042. vm->bind_func<1>(stdout_, "write", [](VM* vm, ArgsView args) {
  1043. vm->_stdout(vm, CAST(Str&, args[0]));
  1044. return vm->None;
  1045. });
  1046. vm->bind_func<1>(stderr_, "write", [](VM* vm, ArgsView args) {
  1047. vm->_stderr(vm, CAST(Str&, args[0]));
  1048. return vm->None;
  1049. });
  1050. }
  1051. inline void add_module_json(VM* vm){
  1052. PyObject* mod = vm->new_module("json");
  1053. vm->bind_func<1>(mod, "loads", [](VM* vm, ArgsView args) {
  1054. const Str& expr = CAST(Str&, args[0]);
  1055. CodeObject_ code = vm->compile(expr, "<json>", JSON_MODE);
  1056. return vm->_exec(code, vm->top_frame()->_module);
  1057. });
  1058. vm->bind_func<1>(mod, "dumps", [](VM* vm, ArgsView args) {
  1059. return vm->py_json(args[0]);
  1060. });
  1061. }
  1062. // https://docs.python.org/3.5/library/math.html
  1063. inline void add_module_math(VM* vm){
  1064. PyObject* mod = vm->new_module("math");
  1065. mod->attr().set("pi", VAR(3.1415926535897932384));
  1066. mod->attr().set("e" , VAR(2.7182818284590452354));
  1067. mod->attr().set("inf", VAR(std::numeric_limits<double>::infinity()));
  1068. mod->attr().set("nan", VAR(std::numeric_limits<double>::quiet_NaN()));
  1069. vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(VAR((i64)std::ceil(CAST_F(args[0])))));
  1070. vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(VAR(std::fabs(CAST_F(args[0])))));
  1071. vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(CAST_F(args[0])))));
  1072. vm->bind_func<1>(mod, "fsum", [](VM* vm, ArgsView args) {
  1073. List& list = CAST(List&, args[0]);
  1074. double sum = 0;
  1075. double c = 0;
  1076. for(PyObject* arg : list){
  1077. double x = CAST_F(arg);
  1078. double y = x - c;
  1079. double t = sum + y;
  1080. c = (t - sum) - y;
  1081. sum = t;
  1082. }
  1083. return VAR(sum);
  1084. });
  1085. vm->bind_func<2>(mod, "gcd", [](VM* vm, ArgsView args) {
  1086. i64 a = CAST(i64, args[0]);
  1087. i64 b = CAST(i64, args[1]);
  1088. if(a < 0) a = -a;
  1089. if(b < 0) b = -b;
  1090. while(b != 0){
  1091. i64 t = b;
  1092. b = a % b;
  1093. a = t;
  1094. }
  1095. return VAR(a);
  1096. });
  1097. vm->bind_func<1>(mod, "isfinite", CPP_LAMBDA(VAR(std::isfinite(CAST_F(args[0])))));
  1098. vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(VAR(std::isinf(CAST_F(args[0])))));
  1099. vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(VAR(std::isnan(CAST_F(args[0])))));
  1100. vm->bind_func<1>(mod, "exp", CPP_LAMBDA(VAR(std::exp(CAST_F(args[0])))));
  1101. vm->bind_func<1>(mod, "log", CPP_LAMBDA(VAR(std::log(CAST_F(args[0])))));
  1102. vm->bind_func<1>(mod, "log2", CPP_LAMBDA(VAR(std::log2(CAST_F(args[0])))));
  1103. vm->bind_func<1>(mod, "log10", CPP_LAMBDA(VAR(std::log10(CAST_F(args[0])))));
  1104. vm->bind_func<2>(mod, "pow", CPP_LAMBDA(VAR(std::pow(CAST_F(args[0]), CAST_F(args[1])))));
  1105. vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(VAR(std::sqrt(CAST_F(args[0])))));
  1106. vm->bind_func<1>(mod, "acos", CPP_LAMBDA(VAR(std::acos(CAST_F(args[0])))));
  1107. vm->bind_func<1>(mod, "asin", CPP_LAMBDA(VAR(std::asin(CAST_F(args[0])))));
  1108. vm->bind_func<1>(mod, "atan", CPP_LAMBDA(VAR(std::atan(CAST_F(args[0])))));
  1109. vm->bind_func<2>(mod, "atan2", CPP_LAMBDA(VAR(std::atan2(CAST_F(args[0]), CAST_F(args[1])))));
  1110. vm->bind_func<1>(mod, "cos", CPP_LAMBDA(VAR(std::cos(CAST_F(args[0])))));
  1111. vm->bind_func<1>(mod, "sin", CPP_LAMBDA(VAR(std::sin(CAST_F(args[0])))));
  1112. vm->bind_func<1>(mod, "tan", CPP_LAMBDA(VAR(std::tan(CAST_F(args[0])))));
  1113. vm->bind_func<1>(mod, "degrees", CPP_LAMBDA(VAR(CAST_F(args[0]) * 180 / 3.1415926535897932384)));
  1114. vm->bind_func<1>(mod, "radians", CPP_LAMBDA(VAR(CAST_F(args[0]) * 3.1415926535897932384 / 180)));
  1115. vm->bind_func<1>(mod, "modf", [](VM* vm, ArgsView args) {
  1116. f64 i;
  1117. f64 f = std::modf(CAST_F(args[0]), &i);
  1118. return VAR(Tuple({VAR(f), VAR(i)}));
  1119. });
  1120. }
  1121. inline void add_module_traceback(VM* vm){
  1122. PyObject* mod = vm->new_module("traceback");
  1123. vm->bind_func<0>(mod, "print_exc", [](VM* vm, ArgsView args) {
  1124. if(vm->_last_exception==nullptr) vm->ValueError("no exception");
  1125. Exception& e = CAST(Exception&, vm->_last_exception);
  1126. vm->_stdout(vm, e.summary());
  1127. return vm->None;
  1128. });
  1129. vm->bind_func<0>(mod, "format_exc", [](VM* vm, ArgsView args) {
  1130. if(vm->_last_exception==nullptr) vm->ValueError("no exception");
  1131. Exception& e = CAST(Exception&, vm->_last_exception);
  1132. return VAR(e.summary());
  1133. });
  1134. }
  1135. inline void add_module_dis(VM* vm){
  1136. PyObject* mod = vm->new_module("dis");
  1137. vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) {
  1138. if(is_type(args[0], vm->tp_str)){
  1139. const Str& source = CAST(Str, args[0]);
  1140. CodeObject_ code = vm->compile(source, "<dis>", EXEC_MODE);
  1141. vm->_stdout(vm, vm->disassemble(code));
  1142. return vm->None;
  1143. }
  1144. PyObject* f = args[0];
  1145. if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, args[0]).func;
  1146. CodeObject_ code = CAST(Function&, f).decl->code;
  1147. vm->_stdout(vm, vm->disassemble(code));
  1148. return vm->None;
  1149. });
  1150. }
  1151. inline void add_module_gc(VM* vm){
  1152. PyObject* mod = vm->new_module("gc");
  1153. vm->bind_func<0>(mod, "collect", CPP_LAMBDA(VAR(vm->heap.collect())));
  1154. }
  1155. inline void VM::post_init(){
  1156. init_builtins(this);
  1157. _t(tp_object)->attr().set("__class__", property(CPP_LAMBDA(vm->_t(args[0]))));
  1158. _t(tp_type)->attr().set("__base__", property([](VM* vm, ArgsView args){
  1159. const PyTypeInfo& info = vm->_all_types[OBJ_GET(Type, args[0])];
  1160. return info.base.index == -1 ? vm->None : vm->_all_types[info.base].obj;
  1161. }));
  1162. _t(tp_type)->attr().set("__name__", property([](VM* vm, ArgsView args){
  1163. const PyTypeInfo& info = vm->_all_types[OBJ_GET(Type, args[0])];
  1164. return VAR(info.name);
  1165. }));
  1166. _t(tp_bound_method)->attr().set("__self__", property([](VM* vm, ArgsView args){
  1167. return CAST(BoundMethod&, args[0]).self;
  1168. }));
  1169. _t(tp_bound_method)->attr().set("__func__", property([](VM* vm, ArgsView args){
  1170. return CAST(BoundMethod&, args[0]).func;
  1171. }));
  1172. bind__eq__(tp_bound_method, [](VM* vm, PyObject* lhs, PyObject* rhs){
  1173. if(!is_non_tagged_type(rhs, vm->tp_bound_method)) return false;
  1174. return _CAST(BoundMethod&, lhs) == _CAST(BoundMethod&, rhs);
  1175. });
  1176. _t(tp_slice)->attr().set("start", property([](VM* vm, ArgsView args){
  1177. return CAST(Slice&, args[0]).start;
  1178. }));
  1179. _t(tp_slice)->attr().set("stop", property([](VM* vm, ArgsView args){
  1180. return CAST(Slice&, args[0]).stop;
  1181. }));
  1182. _t(tp_slice)->attr().set("step", property([](VM* vm, ArgsView args){
  1183. return CAST(Slice&, args[0]).step;
  1184. }));
  1185. _t(tp_object)->attr().set("__dict__", property([](VM* vm, ArgsView args){
  1186. if(is_tagged(args[0]) || !args[0]->is_attr_valid()) return vm->None;
  1187. return VAR(MappingProxy(args[0]));
  1188. }));
  1189. #if !DEBUG_NO_BUILTIN_MODULES
  1190. add_module_sys(this);
  1191. add_module_traceback(this);
  1192. add_module_time(this);
  1193. add_module_json(this);
  1194. add_module_math(this);
  1195. add_module_re(this);
  1196. add_module_dis(this);
  1197. add_module_c(this);
  1198. add_module_gc(this);
  1199. add_module_random(this);
  1200. add_module_base64(this);
  1201. add_module_timeit(this);
  1202. for(const char* name: {"this", "functools", "collections", "heapq", "bisect", "pickle", "_long"}){
  1203. _lazy_modules[name] = kPythonLibs[name];
  1204. }
  1205. try{
  1206. CodeObject_ code = compile(kPythonLibs["builtins"], "<builtins>", EXEC_MODE);
  1207. this->_exec(code, this->builtins);
  1208. code = compile(kPythonLibs["_set"], "<set>", EXEC_MODE);
  1209. this->_exec(code, this->builtins);
  1210. }catch(Exception& e){
  1211. std::cerr << e.summary() << std::endl;
  1212. std::cerr << "failed to load builtins module!!" << std::endl;
  1213. exit(1);
  1214. }
  1215. if(enable_os){
  1216. add_module_io(this);
  1217. add_module_os(this);
  1218. add_module_requests(this);
  1219. _import_handler = _default_import_handler;
  1220. }
  1221. add_module_linalg(this);
  1222. add_module_easing(this);
  1223. #endif
  1224. }
  1225. } // namespace pkpy
  1226. /*************************GLOBAL NAMESPACE*************************/
  1227. extern "C" {
  1228. PK_LEGACY_EXPORT
  1229. void pkpy_free(void* p){
  1230. free(p);
  1231. }
  1232. PK_LEGACY_EXPORT
  1233. void pkpy_vm_exec(pkpy::VM* vm, const char* source){
  1234. vm->exec(source, "main.py", pkpy::EXEC_MODE);
  1235. }
  1236. PK_LEGACY_EXPORT
  1237. void pkpy_vm_exec_2(pkpy::VM* vm, const char* source, const char* filename, int mode, const char* module){
  1238. pkpy::PyObject* mod;
  1239. if(module == nullptr) mod = vm->_main;
  1240. else{
  1241. mod = vm->_modules.try_get(module);
  1242. if(mod == nullptr) return;
  1243. }
  1244. vm->exec(source, filename, (pkpy::CompileMode)mode, mod);
  1245. }
  1246. PK_LEGACY_EXPORT
  1247. pkpy::REPL* pkpy_new_repl(pkpy::VM* vm){
  1248. pkpy::REPL* p = new pkpy::REPL(vm);
  1249. return p;
  1250. }
  1251. PK_LEGACY_EXPORT
  1252. bool pkpy_repl_input(pkpy::REPL* r, const char* line){
  1253. return r->input(line);
  1254. }
  1255. PK_LEGACY_EXPORT
  1256. void pkpy_vm_add_module(pkpy::VM* vm, const char* name, const char* source){
  1257. vm->_lazy_modules[name] = source;
  1258. }
  1259. PK_LEGACY_EXPORT
  1260. pkpy::VM* pkpy_new_vm(bool enable_os=true){
  1261. pkpy::VM* p = new pkpy::VM(enable_os);
  1262. return p;
  1263. }
  1264. PK_LEGACY_EXPORT
  1265. void pkpy_delete_vm(pkpy::VM* vm){
  1266. delete vm;
  1267. }
  1268. PK_LEGACY_EXPORT
  1269. void pkpy_delete_repl(pkpy::REPL* repl){
  1270. delete repl;
  1271. }
  1272. PK_LEGACY_EXPORT
  1273. void pkpy_vm_gc_on_delete(pkpy::VM* vm, void (*f)(pkpy::VM *, pkpy::PyObject *)){
  1274. vm->heap._gc_on_delete = f;
  1275. }
  1276. }