pocketpy.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  1. #include "pocketpy/pocketpy.h"
  2. #ifdef PK_USE_BOX2D
  3. #include "box2dw.hpp"
  4. #endif
  5. #ifdef PK_USE_CJSON
  6. #include "cJSONw.hpp"
  7. #endif
  8. #if defined (_WIN32) && PK_SUPPORT_DYLIB == 1
  9. #define WIN32_LEAN_AND_MEAN
  10. #include <windows.h>
  11. #endif
  12. namespace pkpy{
  13. using dylib_entry_t = const char* (*)(void*, const char*);
  14. #if PK_ENABLE_OS
  15. #if PK_SUPPORT_DYLIB == 1
  16. // win32
  17. static dylib_entry_t load_dylib(const char* path){
  18. std::error_code ec;
  19. auto p = std::filesystem::absolute(path, ec);
  20. if(ec) return nullptr;
  21. HMODULE handle = LoadLibraryA(p.string().c_str());
  22. if(!handle){
  23. DWORD errorCode = GetLastError();
  24. // Convert the error code to text
  25. LPSTR errorMessage = nullptr;
  26. FormatMessageA(
  27. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  28. nullptr,
  29. errorCode,
  30. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  31. (LPSTR)&errorMessage,
  32. 0,
  33. nullptr
  34. );
  35. printf("%lu: %s\n", errorCode, errorMessage);
  36. LocalFree(errorMessage);
  37. return nullptr;
  38. }
  39. return (dylib_entry_t)GetProcAddress(handle, "pkpy_module__init__");
  40. }
  41. #elif PK_SUPPORT_DYLIB == 2
  42. // linux/darwin
  43. static dylib_entry_t load_dylib(const char* path){
  44. std::error_code ec;
  45. auto p = std::filesystem::absolute(path, ec);
  46. if(ec) return nullptr;
  47. void* handle = dlopen(p.c_str(), RTLD_LAZY);
  48. if(!handle) return nullptr;
  49. return (dylib_entry_t)dlsym(handle, "pkpy_module__init__");
  50. }
  51. #elif PK_SUPPORT_DYLIB == 3
  52. // android
  53. static dylib_entry_t load_dylib(const char* path){
  54. void* handle = dlopen(path, RTLD_LAZY);
  55. if(!handle) return nullptr;
  56. return (dylib_entry_t)dlsym(handle, "pkpy_module__init__");
  57. }
  58. #else
  59. static dylib_entry_t load_dylib(const char* path){
  60. return nullptr;
  61. }
  62. #endif
  63. #else
  64. static dylib_entry_t load_dylib(const char* path){
  65. return nullptr;
  66. }
  67. #endif
  68. void init_builtins(VM* _vm) {
  69. #define BIND_NUM_ARITH_OPT(name, op) \
  70. _vm->bind##name(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  71. if(is_int(rhs)) return VAR(_CAST(i64, lhs) op _CAST(i64, rhs)); \
  72. if(is_float(rhs)) return VAR(_CAST(i64, lhs) op _CAST(f64, rhs)); \
  73. return vm->NotImplemented; \
  74. }); \
  75. _vm->bind##name(_vm->tp_float, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  76. if(is_float(rhs)) return VAR(_CAST(f64, lhs) op _CAST(f64, rhs)); \
  77. if(is_int(rhs)) return VAR(_CAST(f64, lhs) op _CAST(i64, rhs)); \
  78. return vm->NotImplemented; \
  79. });
  80. BIND_NUM_ARITH_OPT(__add__, +)
  81. BIND_NUM_ARITH_OPT(__sub__, -)
  82. BIND_NUM_ARITH_OPT(__mul__, *)
  83. #undef BIND_NUM_ARITH_OPT
  84. #define BIND_NUM_LOGICAL_OPT(name, op) \
  85. _vm->bind##name(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  86. i64 val; \
  87. if(try_cast_int(rhs, &val)) return VAR(_CAST(i64, lhs) op val); \
  88. if(is_float(rhs)) return VAR(_CAST(i64, lhs) op _CAST(f64, rhs)); \
  89. return vm->NotImplemented; \
  90. }); \
  91. _vm->bind##name(_vm->tp_float, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  92. i64 val; \
  93. if(try_cast_int(rhs, &val)) return VAR(_CAST(f64, lhs) op val); \
  94. if(is_float(rhs)) return VAR(_CAST(f64, lhs) op _CAST(f64, rhs)); \
  95. return vm->NotImplemented; \
  96. });
  97. BIND_NUM_LOGICAL_OPT(__eq__, ==)
  98. BIND_NUM_LOGICAL_OPT(__lt__, <)
  99. BIND_NUM_LOGICAL_OPT(__le__, <=)
  100. BIND_NUM_LOGICAL_OPT(__gt__, >)
  101. BIND_NUM_LOGICAL_OPT(__ge__, >=)
  102. #undef BIND_NUM_ARITH_OPT
  103. #undef BIND_NUM_LOGICAL_OPT
  104. _vm->bind_builtin_func<-1>("super", [](VM* vm, ArgsView args) {
  105. PyObject* class_arg = nullptr;
  106. PyObject* self_arg = nullptr;
  107. if(args.size() == 2){
  108. class_arg = args[0];
  109. self_arg = args[1];
  110. }else if(args.size() == 0){
  111. FrameId frame = vm->top_frame();
  112. if(frame->_callable != nullptr){
  113. class_arg = PK_OBJ_GET(Function, frame->_callable)._class;
  114. if(frame->_locals.size() > 0) self_arg = frame->_locals[0];
  115. }
  116. if(class_arg == nullptr || self_arg == nullptr){
  117. vm->TypeError("super(): unable to determine the class context, use super(class, self) instead");
  118. }
  119. }else{
  120. vm->TypeError("super() takes 0 or 2 arguments");
  121. }
  122. vm->check_non_tagged_type(class_arg, vm->tp_type);
  123. Type type = PK_OBJ_GET(Type, class_arg);
  124. if(!vm->isinstance(self_arg, type)){
  125. Str _0 = obj_type_name(vm, vm->_tp(self_arg));
  126. Str _1 = obj_type_name(vm, type);
  127. vm->TypeError("super(): " + _0.escape() + " is not an instance of " + _1.escape());
  128. }
  129. return vm->heap.gcnew<Super>(vm->tp_super, self_arg, vm->_all_types[type].base);
  130. });
  131. _vm->bind_builtin_func<2>("isinstance", [](VM* vm, ArgsView args) {
  132. if(is_non_tagged_type(args[1], vm->tp_tuple)){
  133. Tuple& types = _CAST(Tuple&, args[1]);
  134. for(PyObject* type : types){
  135. vm->check_non_tagged_type(type, vm->tp_type);
  136. if(vm->isinstance(args[0], PK_OBJ_GET(Type, type))) return vm->True;
  137. }
  138. return vm->False;
  139. }
  140. vm->check_non_tagged_type(args[1], vm->tp_type);
  141. Type type = PK_OBJ_GET(Type, args[1]);
  142. return VAR(vm->isinstance(args[0], type));
  143. });
  144. _vm->bind_builtin_func<0>("globals", [](VM* vm, ArgsView args) {
  145. PyObject* mod = vm->top_frame()->_module;
  146. return VAR(MappingProxy(mod));
  147. });
  148. _vm->bind(_vm->builtins, "round(x, ndigits=0)", [](VM* vm, ArgsView args) {
  149. f64 x = CAST(f64, args[0]);
  150. int ndigits = CAST(int, args[1]);
  151. if(ndigits == 0){
  152. return x >= 0 ? VAR((i64)(x + 0.5)) : VAR((i64)(x - 0.5));
  153. }
  154. if(ndigits < 0) vm->ValueError("ndigits should be non-negative");
  155. if(x >= 0){
  156. return VAR((i64)(x * std::pow(10, ndigits) + 0.5) / std::pow(10, ndigits));
  157. }else{
  158. return VAR((i64)(x * std::pow(10, ndigits) - 0.5) / std::pow(10, ndigits));
  159. }
  160. });
  161. _vm->bind_builtin_func<1>("abs", [](VM* vm, ArgsView args) {
  162. if(is_int(args[0])) return VAR(std::abs(_CAST(i64, args[0])));
  163. if(is_float(args[0])) return VAR(std::abs(_CAST(f64, args[0])));
  164. vm->TypeError("bad operand type for abs()");
  165. return vm->None;
  166. });
  167. _vm->bind_builtin_func<1>("id", [](VM* vm, ArgsView args) {
  168. PyObject* obj = args[0];
  169. if(is_tagged(obj)) return vm->None;
  170. return VAR(PK_BITS(obj));
  171. });
  172. _vm->bind_builtin_func<1>("callable", [](VM* vm, ArgsView args) {
  173. PyObject* cls = vm->_t(args[0]);
  174. Type t = PK_OBJ_GET(Type, cls);
  175. if(t == vm->tp_function) return vm->True;
  176. if(t == vm->tp_native_func) return vm->True;
  177. if(t == vm->tp_bound_method) return vm->True;
  178. if(t == vm->tp_type) return vm->True;
  179. bool ok = vm->find_name_in_mro(cls, __call__) != nullptr;
  180. return VAR(ok);
  181. });
  182. _vm->bind_builtin_func<1>("__import__", [](VM* vm, ArgsView args) {
  183. const Str& name = CAST(Str&, args[0]);
  184. auto dot = name.sv().find_last_of(".");
  185. if(dot != std::string_view::npos){
  186. auto ext = name.sv().substr(dot);
  187. if(ext == ".so" || ext == ".dll" || ext == ".dylib"){
  188. dylib_entry_t entry = load_dylib(name.c_str());
  189. if(!entry){
  190. vm->ImportError("cannot load dynamic library: " + name.escape());
  191. }
  192. vm->_c.s_view.push(ArgsView(vm->s_data.end(), vm->s_data.end()));
  193. const char* name = entry(vm, PK_VERSION);
  194. vm->_c.s_view.pop();
  195. if(name == nullptr){
  196. vm->ImportError("module initialization failed: " + Str(name).escape());
  197. }
  198. return vm->_modules[name];
  199. }
  200. }
  201. return vm->py_import(name);
  202. });
  203. _vm->bind_builtin_func<2>("divmod", [](VM* vm, ArgsView args) {
  204. if(is_int(args[0])){
  205. i64 lhs = _CAST(i64, args[0]);
  206. i64 rhs = CAST(i64, args[1]);
  207. if(rhs == 0) vm->ZeroDivisionError();
  208. auto res = std::div(lhs, rhs);
  209. return VAR(Tuple({VAR(res.quot), VAR(res.rem)}));
  210. }else{
  211. return vm->call_method(args[0], __divmod__, args[1]);
  212. }
  213. });
  214. _vm->bind(_vm->builtins, "eval(__source, __globals=None)", [](VM* vm, ArgsView args) {
  215. CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<eval>", EVAL_MODE, true);
  216. PyObject* globals = args[1];
  217. if(globals == vm->None){
  218. FrameId frame = vm->top_frame();
  219. return vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals);
  220. }
  221. vm->check_non_tagged_type(globals, vm->tp_mappingproxy);
  222. PyObject* obj = PK_OBJ_GET(MappingProxy, globals).obj;
  223. return vm->_exec(code, obj);
  224. });
  225. _vm->bind(_vm->builtins, "exec(__source, __globals=None)", [](VM* vm, ArgsView args) {
  226. CodeObject_ code = vm->compile(CAST(Str&, args[0]), "<exec>", EXEC_MODE, true);
  227. PyObject* globals = args[1];
  228. if(globals == vm->None){
  229. FrameId frame = vm->top_frame();
  230. vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals);
  231. return vm->None;
  232. }
  233. vm->check_non_tagged_type(globals, vm->tp_mappingproxy);
  234. PyObject* obj = PK_OBJ_GET(MappingProxy, globals).obj;
  235. vm->_exec(code, obj);
  236. return vm->None;
  237. });
  238. _vm->bind_builtin_func<-1>("exit", [](VM* vm, ArgsView args) {
  239. if(args.size() == 0) std::exit(0);
  240. else if(args.size() == 1) std::exit(CAST(int, args[0]));
  241. else vm->TypeError("exit() takes at most 1 argument");
  242. return vm->None;
  243. });
  244. _vm->bind_builtin_func<1>("repr", PK_LAMBDA(vm->py_repr(args[0])));
  245. _vm->bind_builtin_func<1>("len", [](VM* vm, ArgsView args){
  246. const PyTypeInfo* ti = vm->_inst_type_info(args[0]);
  247. if(ti->m__len__) return VAR(ti->m__len__(vm, args[0]));
  248. return vm->call_method(args[0], __len__);
  249. });
  250. _vm->bind_builtin_func<1>("hash", [](VM* vm, ArgsView args){
  251. i64 value = vm->py_hash(args[0]);
  252. return VAR(value);
  253. });
  254. _vm->bind_builtin_func<1>("chr", [](VM* vm, ArgsView args) {
  255. i64 i = CAST(i64, args[0]);
  256. if (i < 0 || i > 128) vm->ValueError("chr() arg not in range(128)");
  257. return VAR(std::string(1, (char)i));
  258. });
  259. _vm->bind_builtin_func<1>("ord", [](VM* vm, ArgsView args) {
  260. const Str& s = CAST(Str&, args[0]);
  261. if (s.length()!=1) vm->TypeError("ord() expected an ASCII character");
  262. return VAR((i64)(s[0]));
  263. });
  264. _vm->bind_builtin_func<2>("hasattr", [](VM* vm, ArgsView args) {
  265. return VAR(vm->getattr(args[0], CAST(Str&, args[1]), false) != nullptr);
  266. });
  267. _vm->bind_builtin_func<3>("setattr", [](VM* vm, ArgsView args) {
  268. vm->setattr(args[0], CAST(Str&, args[1]), args[2]);
  269. return vm->None;
  270. });
  271. _vm->bind_builtin_func<2>("getattr", [](VM* vm, ArgsView args) {
  272. const Str& name = CAST(Str&, args[1]);
  273. return vm->getattr(args[0], name);
  274. });
  275. _vm->bind_builtin_func<2>("delattr", [](VM* vm, ArgsView args) {
  276. vm->delattr(args[0], CAST(Str&, args[1]));
  277. return vm->None;
  278. });
  279. _vm->bind_builtin_func<1>("hex", [](VM* vm, ArgsView args) {
  280. std::stringstream ss; // hex
  281. ss << std::hex << CAST(i64, args[0]);
  282. return VAR("0x" + ss.str());
  283. });
  284. _vm->bind_builtin_func<1>("iter", [](VM* vm, ArgsView args) {
  285. return vm->py_iter(args[0]);
  286. });
  287. _vm->bind_builtin_func<1>("next", [](VM* vm, ArgsView args) {
  288. return vm->py_next(args[0]);
  289. });
  290. _vm->bind_builtin_func<1>("bin", [](VM* vm, ArgsView args) {
  291. SStream ss;
  292. i64 x = CAST(i64, args[0]);
  293. if(x < 0){ ss << "-"; x = -x; }
  294. ss << "0b";
  295. std::string bits;
  296. while(x){
  297. bits += (x & 1) ? '1' : '0';
  298. x >>= 1;
  299. }
  300. std::reverse(bits.begin(), bits.end());
  301. if(bits.empty()) bits = "0";
  302. ss << bits;
  303. return VAR(ss.str());
  304. });
  305. _vm->bind_builtin_func<1>("dir", [](VM* vm, ArgsView args) {
  306. std::set<StrName> names;
  307. if(!is_tagged(args[0]) && args[0]->is_attr_valid()){
  308. std::vector<StrName> keys = args[0]->attr().keys();
  309. names.insert(keys.begin(), keys.end());
  310. }
  311. const NameDict& t_attr = vm->_t(args[0])->attr();
  312. std::vector<StrName> keys = t_attr.keys();
  313. names.insert(keys.begin(), keys.end());
  314. List ret;
  315. for (StrName name : names) ret.push_back(VAR(name.sv()));
  316. return VAR(std::move(ret));
  317. });
  318. _vm->bind__repr__(_vm->tp_object, [](VM* vm, PyObject* obj) {
  319. if(is_tagged(obj)) FATAL_ERROR();
  320. std::stringstream ss; // hex
  321. ss << "<" << OBJ_NAME(vm->_t(obj)) << " object at 0x";
  322. ss << std::hex << reinterpret_cast<intptr_t>(obj) << ">";
  323. return VAR(ss.str());
  324. });
  325. _vm->bind__eq__(_vm->tp_object, [](VM* vm, PyObject* lhs, PyObject* rhs) { return VAR(lhs == rhs); });
  326. _vm->cached_object__new__ = _vm->bind_constructor<1>("object", [](VM* vm, ArgsView args) {
  327. vm->check_non_tagged_type(args[0], vm->tp_type);
  328. Type t = PK_OBJ_GET(Type, args[0]);
  329. return vm->heap.gcnew<DummyInstance>(t);
  330. });
  331. _vm->bind_method<0>("object", "_enable_instance_dict", [](VM* vm, ArgsView args){
  332. PyObject* self = args[0];
  333. if(is_tagged(self)){
  334. vm->TypeError("object: tagged object cannot enable instance dict");
  335. }
  336. if(self->is_attr_valid()){
  337. vm->TypeError("object: instance dict is already enabled");
  338. }
  339. self->_enable_instance_dict();
  340. return vm->None;
  341. });
  342. _vm->bind_constructor<2>("type", PK_LAMBDA(vm->_t(args[1])));
  343. _vm->bind_constructor<-1>("range", [](VM* vm, ArgsView args) {
  344. args._begin += 1; // skip cls
  345. Range r;
  346. switch (args.size()) {
  347. case 1: r.stop = CAST(i64, args[0]); break;
  348. case 2: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); break;
  349. case 3: r.start = CAST(i64, args[0]); r.stop = CAST(i64, args[1]); r.step = CAST(i64, args[2]); break;
  350. default: vm->TypeError("expected 1-3 arguments, got " + std::to_string(args.size()));
  351. }
  352. return VAR(r);
  353. });
  354. _vm->bind__iter__(_vm->tp_range, [](VM* vm, PyObject* obj) { return VAR_T(RangeIter, PK_OBJ_GET(Range, obj)); });
  355. _vm->bind__repr__(_vm->_type("NoneType"), [](VM* vm, PyObject* obj) { return VAR("None"); });
  356. _vm->bind__truediv__(_vm->tp_float, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  357. f64 value = CAST_F(rhs);
  358. return VAR(_CAST(f64, lhs) / value);
  359. });
  360. _vm->bind__truediv__(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  361. f64 value = CAST_F(rhs);
  362. return VAR(_CAST(i64, lhs) / value);
  363. });
  364. auto py_number_pow = [](VM* vm, PyObject* lhs_, PyObject* rhs_) {
  365. i64 lhs, rhs;
  366. if(try_cast_int(lhs_, &lhs) && try_cast_int(rhs_, &rhs)){
  367. if(rhs < 0) {
  368. if(lhs == 0) vm->ZeroDivisionError("0.0 cannot be raised to a negative power");
  369. return VAR((f64)std::pow(lhs, rhs));
  370. }
  371. i64 ret = 1;
  372. while(rhs){
  373. if(rhs & 1) ret *= lhs;
  374. lhs *= lhs;
  375. rhs >>= 1;
  376. }
  377. return VAR(ret);
  378. }else{
  379. return VAR((f64)std::pow(CAST_F(lhs_), CAST_F(rhs_)));
  380. }
  381. };
  382. _vm->bind__pow__(_vm->tp_int, py_number_pow);
  383. _vm->bind__pow__(_vm->tp_float, py_number_pow);
  384. /************ int ************/
  385. _vm->bind_constructor<-1>("int", [](VM* vm, ArgsView args) {
  386. if(args.size() == 1+0) return VAR(0);
  387. // 1 arg
  388. if(args.size() == 1+1){
  389. if (is_type(args[1], vm->tp_float)) return VAR((i64)CAST(f64, args[1]));
  390. if (is_type(args[1], vm->tp_int)) return args[1];
  391. if (is_type(args[1], vm->tp_bool)) return VAR(_CAST(bool, args[1]) ? 1 : 0);
  392. }
  393. if(args.size() > 1+2) vm->TypeError("int() takes at most 2 arguments");
  394. // 2 args
  395. if (is_type(args[1], vm->tp_str)) {
  396. int base = 10;
  397. if(args.size() == 1+2) base = CAST(i64, args[2]);
  398. const Str& s = CAST(Str&, args[1]);
  399. i64 val;
  400. if(!parse_int(s.sv(), &val, base)){
  401. vm->ValueError("invalid literal for int(): " + s.escape());
  402. }
  403. return VAR(val);
  404. }
  405. vm->TypeError("invalid arguments for int()");
  406. return vm->None;
  407. });
  408. _vm->bind_method<0>("int", "bit_length", [](VM* vm, ArgsView args) {
  409. i64 x = _CAST(i64, args[0]);
  410. if(x < 0) x = -x;
  411. int bits = 0;
  412. while(x){ x >>= 1; bits++; }
  413. return VAR(bits);
  414. });
  415. _vm->bind__floordiv__(_vm->tp_int, [](VM* vm, PyObject* lhs_, PyObject* rhs_) {
  416. i64 rhs = CAST(i64, rhs_);
  417. if(rhs == 0) vm->ZeroDivisionError();
  418. return VAR(_CAST(i64, lhs_) / rhs);
  419. });
  420. _vm->bind__mod__(_vm->tp_int, [](VM* vm, PyObject* lhs_, PyObject* rhs_) {
  421. i64 rhs = CAST(i64, rhs_);
  422. if(rhs == 0) vm->ZeroDivisionError();
  423. return VAR(_CAST(i64, lhs_) % rhs);
  424. });
  425. _vm->bind__repr__(_vm->tp_int, [](VM* vm, PyObject* obj) { return VAR(std::to_string(_CAST(i64, obj))); });
  426. _vm->bind__neg__(_vm->tp_int, [](VM* vm, PyObject* obj) { return VAR(-_CAST(i64, obj)); });
  427. _vm->bind__hash__(_vm->tp_int, [](VM* vm, PyObject* obj) { return _CAST(i64, obj); });
  428. _vm->bind__invert__(_vm->tp_int, [](VM* vm, PyObject* obj) { return VAR(~_CAST(i64, obj)); });
  429. #define INT_BITWISE_OP(name, op) \
  430. _vm->bind##name(_vm->tp_int, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  431. return VAR(_CAST(i64, lhs) op CAST(i64, rhs)); \
  432. });
  433. INT_BITWISE_OP(__lshift__, <<)
  434. INT_BITWISE_OP(__rshift__, >>)
  435. INT_BITWISE_OP(__and__, &)
  436. INT_BITWISE_OP(__or__, |)
  437. INT_BITWISE_OP(__xor__, ^)
  438. #undef INT_BITWISE_OP
  439. /************ float ************/
  440. _vm->bind_constructor<-1>("float", [](VM* vm, ArgsView args) {
  441. if(args.size() == 1+0) return VAR(0.0);
  442. if(args.size() > 1+1) vm->TypeError("float() takes at most 1 argument");
  443. // 1 arg
  444. if (is_type(args[1], vm->tp_int)) return VAR((f64)CAST(i64, args[1]));
  445. if (is_type(args[1], vm->tp_float)) return args[1];
  446. if (is_type(args[1], vm->tp_bool)) return VAR(_CAST(bool, args[1]) ? 1.0 : 0.0);
  447. if (is_type(args[1], vm->tp_str)) {
  448. const Str& s = CAST(Str&, args[1]);
  449. if(s == "inf") return VAR(INFINITY);
  450. if(s == "-inf") return VAR(-INFINITY);
  451. double float_out;
  452. char* p_end;
  453. try{
  454. float_out = std::strtod(s.data, &p_end);
  455. PK_ASSERT(p_end == s.end());
  456. }catch(...){
  457. vm->ValueError("invalid literal for float(): " + s.escape());
  458. }
  459. return VAR(float_out);
  460. }
  461. vm->TypeError("invalid arguments for float()");
  462. return vm->None;
  463. });
  464. _vm->bind__hash__(_vm->tp_float, [](VM* vm, PyObject* obj) {
  465. f64 val = _CAST(f64, obj);
  466. return (i64)std::hash<f64>()(val);
  467. });
  468. _vm->bind__neg__(_vm->tp_float, [](VM* vm, PyObject* obj) { return VAR(-_CAST(f64, obj)); });
  469. _vm->bind__repr__(_vm->tp_float, [](VM* vm, PyObject* obj) {
  470. f64 val = _CAST(f64, obj);
  471. SStream ss;
  472. ss << val;
  473. return VAR(ss.str());
  474. });
  475. /************ str ************/
  476. _vm->bind_constructor<2>("str", PK_LAMBDA(vm->py_str(args[1])));
  477. _vm->bind__hash__(_vm->tp_str, [](VM* vm, PyObject* obj) {
  478. return (i64)_CAST(Str&, obj).hash();
  479. });
  480. _vm->bind__add__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  481. return VAR(_CAST(Str&, lhs) + CAST(Str&, rhs));
  482. });
  483. _vm->bind__len__(_vm->tp_str, [](VM* vm, PyObject* obj) {
  484. return (i64)_CAST(Str&, obj).u8_length();
  485. });
  486. _vm->bind__mul__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  487. const Str& self = _CAST(Str&, lhs);
  488. i64 n = CAST(i64, rhs);
  489. SStream ss;
  490. for(i64 i = 0; i < n; i++) ss << self.sv();
  491. return VAR(ss.str());
  492. });
  493. _vm->bind_method<1>("str", "__rmul__", [](VM* vm, ArgsView args) {
  494. const Str& self = _CAST(Str&, args[0]);
  495. i64 n = CAST(i64, args[1]);
  496. SStream ss;
  497. for(i64 i = 0; i < n; i++) ss << self.sv();
  498. return VAR(ss.str());
  499. });
  500. _vm->bind__contains__(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  501. const Str& self = _CAST(Str&, lhs);
  502. return VAR(self.index(CAST(Str&, rhs)) != -1);
  503. });
  504. _vm->bind__str__(_vm->tp_str, [](VM* vm, PyObject* obj) { return obj; });
  505. _vm->bind__iter__(_vm->tp_str, [](VM* vm, PyObject* obj) { return VAR_T(StringIter, obj); });
  506. _vm->bind__repr__(_vm->tp_str, [](VM* vm, PyObject* obj) {
  507. const Str& self = _CAST(Str&, obj);
  508. return VAR(self.escape(true));
  509. });
  510. #define BIND_CMP_STR(name, op) \
  511. _vm->bind##name(_vm->tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) { \
  512. if(!is_non_tagged_type(rhs, vm->tp_str)) return vm->NotImplemented; \
  513. return VAR(_CAST(Str&, lhs) op _CAST(Str&, rhs)); \
  514. });
  515. BIND_CMP_STR(__eq__, ==)
  516. BIND_CMP_STR(__lt__, <)
  517. BIND_CMP_STR(__le__, <=)
  518. BIND_CMP_STR(__gt__, >)
  519. BIND_CMP_STR(__ge__, >=)
  520. #undef BIND_CMP_STR
  521. _vm->bind__getitem__(_vm->tp_str, [](VM* vm, PyObject* obj, PyObject* index) {
  522. const Str& self = _CAST(Str&, obj);
  523. if(is_non_tagged_type(index, vm->tp_slice)){
  524. const Slice& s = _CAST(Slice&, index);
  525. int start, stop, step;
  526. vm->parse_int_slice(s, self.u8_length(), start, stop, step);
  527. return VAR(self.u8_slice(start, stop, step));
  528. }
  529. int i = CAST(int, index);
  530. i = vm->normalized_index(i, self.u8_length());
  531. return VAR(self.u8_getitem(i));
  532. });
  533. _vm->bind(_vm->_t(_vm->tp_str), "replace(self, old, new, count=-1)", [](VM* vm, ArgsView args) {
  534. const Str& self = _CAST(Str&, args[0]);
  535. const Str& old = CAST(Str&, args[1]);
  536. if(old.empty()) vm->ValueError("empty substring");
  537. const Str& new_ = CAST(Str&, args[2]);
  538. int count = CAST(int, args[3]);
  539. return VAR(self.replace(old, new_, count));
  540. });
  541. _vm->bind(_vm->_t(_vm->tp_str), "split(self, sep=' ')", [](VM* vm, ArgsView args) {
  542. const Str& self = _CAST(Str&, args[0]);
  543. const Str& sep = CAST(Str&, args[1]);
  544. if(sep.empty()) vm->ValueError("empty separator");
  545. std::vector<std::string_view> parts;
  546. if(sep.size == 1){
  547. parts = self.split(sep[0]);
  548. }else{
  549. parts = self.split(sep);
  550. }
  551. List ret(parts.size());
  552. for(int i=0; i<parts.size(); i++) ret[i] = VAR(Str(parts[i]));
  553. return VAR(std::move(ret));
  554. });
  555. _vm->bind(_vm->_t(_vm->tp_str), "count(self, s: str)", [](VM* vm, ArgsView args) {
  556. const Str& self = _CAST(Str&, args[0]);
  557. const Str& s = CAST(Str&, args[1]);
  558. return VAR(self.count(s));
  559. });
  560. _vm->bind_method<1>("str", "index", [](VM* vm, ArgsView args) {
  561. const Str& self = _CAST(Str&, args[0]);
  562. const Str& sub = CAST(Str&, args[1]);
  563. int index = self.index(sub);
  564. if(index == -1) vm->ValueError("substring not found");
  565. return VAR(index);
  566. });
  567. _vm->bind_method<1>("str", "find", [](VM* vm, ArgsView args) {
  568. const Str& self = _CAST(Str&, args[0]);
  569. const Str& sub = CAST(Str&, args[1]);
  570. return VAR(self.index(sub));
  571. });
  572. _vm->bind_method<1>("str", "startswith", [](VM* vm, ArgsView args) {
  573. const Str& self = _CAST(Str&, args[0]);
  574. const Str& prefix = CAST(Str&, args[1]);
  575. return VAR(self.index(prefix) == 0);
  576. });
  577. _vm->bind_method<1>("str", "endswith", [](VM* vm, ArgsView args) {
  578. const Str& self = _CAST(Str&, args[0]);
  579. const Str& suffix = CAST(Str&, args[1]);
  580. int offset = self.length() - suffix.length();
  581. if(offset < 0) return vm->False;
  582. bool ok = memcmp(self.data+offset, suffix.data, suffix.length()) == 0;
  583. return VAR(ok);
  584. });
  585. _vm->bind_method<0>("str", "encode", [](VM* vm, ArgsView args) {
  586. const Str& self = _CAST(Str&, args[0]);
  587. unsigned char* buffer = new unsigned char[self.length()];
  588. memcpy(buffer, self.data, self.length());
  589. return VAR(Bytes(buffer, self.length()));
  590. });
  591. _vm->bind_method<1>("str", "join", [](VM* vm, ArgsView args) {
  592. auto _lock = vm->heap.gc_scope_lock();
  593. const Str& self = _CAST(Str&, args[0]);
  594. SStream ss;
  595. PyObject* it = vm->py_iter(args[1]); // strong ref
  596. PyObject* obj = vm->py_next(it);
  597. while(obj != vm->StopIteration){
  598. if(!ss.empty()) ss << self;
  599. ss << CAST(Str&, obj);
  600. obj = vm->py_next(it);
  601. }
  602. return VAR(ss.str());
  603. });
  604. _vm->bind_method<0>("str", "lower", [](VM* vm, ArgsView args) {
  605. const Str& self = _CAST(Str&, args[0]);
  606. return VAR(self.lower());
  607. });
  608. _vm->bind_method<0>("str", "upper", [](VM* vm, ArgsView args) {
  609. const Str& self = _CAST(Str&, args[0]);
  610. return VAR(self.upper());
  611. });
  612. /************ list ************/
  613. _vm->bind(_vm->_t(_vm->tp_list), "sort(self, key=None, reverse=False)", [](VM* vm, ArgsView args) {
  614. List& self = _CAST(List&, args[0]);
  615. PyObject* key = args[1];
  616. if(key == vm->None){
  617. std::stable_sort(self.begin(), self.end(), [vm](PyObject* a, PyObject* b){
  618. return vm->py_lt(a, b);
  619. });
  620. }else{
  621. std::stable_sort(self.begin(), self.end(), [vm, key](PyObject* a, PyObject* b){
  622. return vm->py_lt(vm->call(key, a), vm->call(key, b));
  623. });
  624. }
  625. bool reverse = CAST(bool, args[2]);
  626. if(reverse) self.reverse();
  627. return vm->None;
  628. });
  629. _vm->bind__repr__(_vm->tp_list, [](VM* vm, PyObject* _0){
  630. List& iterable = _CAST(List&, _0);
  631. SStream ss;
  632. ss << '[';
  633. for(int i=0; i<iterable.size(); i++){
  634. ss << CAST(Str&, vm->py_repr(iterable[i]));
  635. if(i != iterable.size()-1) ss << ", ";
  636. }
  637. ss << ']';
  638. return VAR(ss.str());
  639. });
  640. _vm->bind__repr__(_vm->tp_tuple, [](VM* vm, PyObject* _0){
  641. Tuple& iterable = _CAST(Tuple&, _0);
  642. SStream ss;
  643. ss << '(';
  644. if(iterable.size() == 1){
  645. ss << CAST(Str&, vm->py_repr(iterable[0]));
  646. ss << ',';
  647. }else{
  648. for(int i=0; i<iterable.size(); i++){
  649. ss << CAST(Str&, vm->py_repr(iterable[i]));
  650. if(i != iterable.size()-1) ss << ", ";
  651. }
  652. }
  653. ss << ')';
  654. return VAR(ss.str());
  655. });
  656. _vm->bind_constructor<-1>("list", [](VM* vm, ArgsView args) {
  657. if(args.size() == 1+0) return VAR(List());
  658. if(args.size() == 1+1){
  659. return vm->py_list(args[1]);
  660. }
  661. vm->TypeError("list() takes 0 or 1 arguments");
  662. return vm->None;
  663. });
  664. _vm->bind__contains__(_vm->tp_list, [](VM* vm, PyObject* obj, PyObject* item) {
  665. List& self = _CAST(List&, obj);
  666. for(PyObject* i: self) if(vm->py_eq(i, item)) return vm->True;
  667. return vm->False;
  668. });
  669. _vm->bind_method<1>("list", "count", [](VM* vm, ArgsView args) {
  670. List& self = _CAST(List&, args[0]);
  671. int count = 0;
  672. for(PyObject* i: self) if(vm->py_eq(i, args[1])) count++;
  673. return VAR(count);
  674. });
  675. _vm->bind__eq__(_vm->tp_list, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  676. List& a = _CAST(List&, lhs);
  677. if(!is_non_tagged_type(rhs, vm->tp_list)) return vm->NotImplemented;
  678. List& b = _CAST(List&, rhs);
  679. if(a.size() != b.size()) return vm->False;
  680. for(int i=0; i<a.size(); i++){
  681. if(!vm->py_eq(a[i], b[i])) return vm->False;
  682. }
  683. return vm->True;
  684. });
  685. _vm->bind_method<1>("list", "index", [](VM* vm, ArgsView args) {
  686. List& self = _CAST(List&, args[0]);
  687. PyObject* obj = args[1];
  688. for(int i=0; i<self.size(); i++){
  689. if(vm->py_eq(self[i], obj)) return VAR(i);
  690. }
  691. vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
  692. return vm->None;
  693. });
  694. _vm->bind_method<1>("list", "remove", [](VM* vm, ArgsView args) {
  695. List& self = _CAST(List&, args[0]);
  696. PyObject* obj = args[1];
  697. for(int i=0; i<self.size(); i++){
  698. if(vm->py_eq(self[i], obj)){
  699. self.erase(i);
  700. return vm->None;
  701. }
  702. }
  703. vm->ValueError(_CAST(Str&, vm->py_repr(obj)) + " is not in list");
  704. return vm->None;
  705. });
  706. _vm->bind_method<-1>("list", "pop", [](VM* vm, ArgsView args) {
  707. List& self = _CAST(List&, args[0]);
  708. if(args.size() == 1+0){
  709. if(self.empty()) vm->IndexError("pop from empty list");
  710. return self.popx_back();
  711. }
  712. if(args.size() == 1+1){
  713. int index = CAST(int, args[1]);
  714. index = vm->normalized_index(index, self.size());
  715. PyObject* ret = self[index];
  716. self.erase(index);
  717. return ret;
  718. }
  719. vm->TypeError("pop() takes at most 1 argument");
  720. return vm->None;
  721. });
  722. _vm->bind_method<1>("list", "append", [](VM* vm, ArgsView args) {
  723. List& self = _CAST(List&, args[0]);
  724. self.push_back(args[1]);
  725. return vm->None;
  726. });
  727. _vm->bind_method<1>("list", "extend", [](VM* vm, ArgsView args) {
  728. auto _lock = vm->heap.gc_scope_lock();
  729. List& self = _CAST(List&, args[0]);
  730. PyObject* it = vm->py_iter(args[1]); // strong ref
  731. PyObject* obj = vm->py_next(it);
  732. while(obj != vm->StopIteration){
  733. self.push_back(obj);
  734. obj = vm->py_next(it);
  735. }
  736. return vm->None;
  737. });
  738. _vm->bind_method<0>("list", "reverse", [](VM* vm, ArgsView args) {
  739. List& self = _CAST(List&, args[0]);
  740. std::reverse(self.begin(), self.end());
  741. return vm->None;
  742. });
  743. _vm->bind__mul__(_vm->tp_list, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  744. const List& self = _CAST(List&, lhs);
  745. if(!is_int(rhs)) return vm->NotImplemented;
  746. int n = _CAST(int, rhs);
  747. List result;
  748. result.reserve(self.size() * n);
  749. for(int i = 0; i < n; i++) result.extend(self);
  750. return VAR(std::move(result));
  751. });
  752. _vm->bind_method<1>("list", "__rmul__", [](VM* vm, ArgsView args) {
  753. const List& self = _CAST(List&, args[0]);
  754. if(!is_int(args[1])) return vm->NotImplemented;
  755. int n = _CAST(int, args[1]);
  756. List result;
  757. result.reserve(self.size() * n);
  758. for(int i = 0; i < n; i++) result.extend(self);
  759. return VAR(std::move(result));
  760. });
  761. _vm->bind_method<2>("list", "insert", [](VM* vm, ArgsView args) {
  762. List& self = _CAST(List&, args[0]);
  763. int index = CAST(int, args[1]);
  764. if(index < 0) index += self.size();
  765. if(index < 0) index = 0;
  766. if(index > self.size()) index = self.size();
  767. self.insert(index, args[2]);
  768. return vm->None;
  769. });
  770. _vm->bind_method<0>("list", "clear", [](VM* vm, ArgsView args) {
  771. _CAST(List&, args[0]).clear();
  772. return vm->None;
  773. });
  774. _vm->bind_method<0>("list", "copy", PK_LAMBDA(VAR(_CAST(List, args[0]))));
  775. #define BIND_RICH_CMP(name, op, _t, _T) \
  776. _vm->bind__##name##__(_vm->_t, [](VM* vm, PyObject* lhs, PyObject* rhs){ \
  777. if(!is_non_tagged_type(rhs, vm->_t)) return vm->NotImplemented; \
  778. auto& a = _CAST(_T&, lhs); \
  779. auto& b = _CAST(_T&, rhs); \
  780. for(int i=0; i<a.size() && i<b.size(); i++){ \
  781. if(vm->py_eq(a[i], b[i])) continue; \
  782. return VAR(vm->py_##name(a[i], b[i])); \
  783. } \
  784. return VAR(a.size() op b.size()); \
  785. });
  786. BIND_RICH_CMP(lt, <, tp_list, List)
  787. BIND_RICH_CMP(le, <=, tp_list, List)
  788. BIND_RICH_CMP(gt, >, tp_list, List)
  789. BIND_RICH_CMP(ge, >=, tp_list, List)
  790. BIND_RICH_CMP(lt, <, tp_tuple, Tuple)
  791. BIND_RICH_CMP(le, <=, tp_tuple, Tuple)
  792. BIND_RICH_CMP(gt, >, tp_tuple, Tuple)
  793. BIND_RICH_CMP(ge, >=, tp_tuple, Tuple)
  794. #undef BIND_RICH_CMP
  795. _vm->bind__add__(_vm->tp_list, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  796. const List& self = _CAST(List&, lhs);
  797. const List& other = CAST(List&, rhs);
  798. List new_list(self); // copy construct
  799. new_list.extend(other);
  800. return VAR(std::move(new_list));
  801. });
  802. _vm->bind__len__(_vm->tp_list, [](VM* vm, PyObject* obj) {
  803. return (i64)_CAST(List&, obj).size();
  804. });
  805. _vm->bind__iter__(_vm->tp_list, [](VM* vm, PyObject* obj) {
  806. List& self = _CAST(List&, obj);
  807. return VAR_T(ArrayIter, obj, self.begin(), self.end());
  808. });
  809. _vm->bind__getitem__(_vm->tp_list, PyArrayGetItem<List>);
  810. _vm->bind__setitem__(_vm->tp_list, [](VM* vm, PyObject* obj, PyObject* index, PyObject* value){
  811. List& self = _CAST(List&, obj);
  812. int i = CAST(int, index);
  813. i = vm->normalized_index(i, self.size());
  814. self[i] = value;
  815. });
  816. _vm->bind__delitem__(_vm->tp_list, [](VM* vm, PyObject* obj, PyObject* index){
  817. List& self = _CAST(List&, obj);
  818. int i = CAST(int, index);
  819. i = vm->normalized_index(i, self.size());
  820. self.erase(i);
  821. });
  822. /************ tuple ************/
  823. _vm->bind_constructor<-1>("tuple", [](VM* vm, ArgsView args) {
  824. if(args.size() == 1+0) return VAR(Tuple(0));
  825. if(args.size() == 1+1){
  826. List list = CAST(List, vm->py_list(args[1]));
  827. return VAR(Tuple(std::move(list)));
  828. }
  829. vm->TypeError("tuple() takes at most 1 argument");
  830. return vm->None;
  831. });
  832. _vm->bind__contains__(_vm->tp_tuple, [](VM* vm, PyObject* obj, PyObject* item) {
  833. Tuple& self = _CAST(Tuple&, obj);
  834. for(PyObject* i: self) if(vm->py_eq(i, item)) return vm->True;
  835. return vm->False;
  836. });
  837. _vm->bind_method<1>("tuple", "count", [](VM* vm, ArgsView args) {
  838. Tuple& self = _CAST(Tuple&, args[0]);
  839. int count = 0;
  840. for(PyObject* i: self) if(vm->py_eq(i, args[1])) count++;
  841. return VAR(count);
  842. });
  843. _vm->bind__eq__(_vm->tp_tuple, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  844. const Tuple& self = _CAST(Tuple&, lhs);
  845. if(!is_non_tagged_type(rhs, vm->tp_tuple)) return vm->NotImplemented;
  846. const Tuple& other = _CAST(Tuple&, rhs);
  847. if(self.size() != other.size()) return vm->False;
  848. for(int i = 0; i < self.size(); i++) {
  849. if(!vm->py_eq(self[i], other[i])) return vm->False;
  850. }
  851. return vm->True;
  852. });
  853. _vm->bind__hash__(_vm->tp_tuple, [](VM* vm, PyObject* obj) {
  854. i64 x = 1000003;
  855. const Tuple& items = CAST(Tuple&, obj);
  856. for (int i=0; i<items.size(); i++) {
  857. i64 y = vm->py_hash(items[i]);
  858. // recommended by Github Copilot
  859. x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 2));
  860. }
  861. return x;
  862. });
  863. _vm->bind__iter__(_vm->tp_tuple, [](VM* vm, PyObject* obj) {
  864. Tuple& self = _CAST(Tuple&, obj);
  865. return VAR_T(ArrayIter, obj, self.begin(), self.end());
  866. });
  867. _vm->bind__getitem__(_vm->tp_tuple, PyArrayGetItem<Tuple>);
  868. _vm->bind__len__(_vm->tp_tuple, [](VM* vm, PyObject* obj) {
  869. return (i64)_CAST(Tuple&, obj).size();
  870. });
  871. /************ bool ************/
  872. _vm->bind_constructor<2>("bool", PK_LAMBDA(VAR(vm->py_bool(args[1]))));
  873. _vm->bind__hash__(_vm->tp_bool, [](VM* vm, PyObject* obj) {
  874. return (i64)_CAST(bool, obj);
  875. });
  876. _vm->bind__repr__(_vm->tp_bool, [](VM* vm, PyObject* self) {
  877. bool val = _CAST(bool, self);
  878. return VAR(val ? "True" : "False");
  879. });
  880. _vm->bind__and__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  881. return VAR(_CAST(bool, lhs) && CAST(bool, rhs));
  882. });
  883. _vm->bind__or__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  884. return VAR(_CAST(bool, lhs) || CAST(bool, rhs));
  885. });
  886. _vm->bind__xor__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  887. return VAR(_CAST(bool, lhs) != CAST(bool, rhs));
  888. });
  889. _vm->bind__eq__(_vm->tp_bool, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  890. if(is_non_tagged_type(rhs, vm->tp_bool)) return VAR(lhs == rhs);
  891. if(is_int(rhs)) return VAR(_CAST(bool, lhs) == (bool)CAST(i64, rhs));
  892. return vm->NotImplemented;
  893. });
  894. _vm->bind__repr__(_vm->_type("ellipsis"), [](VM* vm, PyObject* self) {
  895. return VAR("...");
  896. });
  897. _vm->bind__repr__(_vm->_type("NotImplementedType"), [](VM* vm, PyObject* self) {
  898. return VAR("NotImplemented");
  899. });
  900. /************ bytes ************/
  901. _vm->bind_constructor<2>("bytes", [](VM* vm, ArgsView args){
  902. List& list = CAST(List&, args[1]);
  903. std::vector<unsigned char> buffer(list.size());
  904. for(int i=0; i<list.size(); i++){
  905. i64 b = CAST(i64, list[i]);
  906. if(b<0 || b>255) vm->ValueError("byte must be in range[0, 256)");
  907. buffer[i] = (char)b;
  908. }
  909. return VAR(Bytes(buffer));
  910. });
  911. _vm->bind__getitem__(_vm->tp_bytes, [](VM* vm, PyObject* obj, PyObject* index) {
  912. const Bytes& self = _CAST(Bytes&, obj);
  913. int i = CAST(int, index);
  914. i = vm->normalized_index(i, self.size());
  915. return VAR(self[i]);
  916. });
  917. _vm->bind__hash__(_vm->tp_bytes, [](VM* vm, PyObject* obj) {
  918. const Bytes& self = _CAST(Bytes&, obj);
  919. std::string_view view((char*)self.data(), self.size());
  920. return (i64)std::hash<std::string_view>()(view);
  921. });
  922. _vm->bind__repr__(_vm->tp_bytes, [](VM* vm, PyObject* obj) {
  923. const Bytes& self = _CAST(Bytes&, obj);
  924. SStream ss;
  925. ss << "b'";
  926. for(int i=0; i<self.size(); i++){
  927. ss << "\\x"; // << std::hex << std::setw(2) << std::setfill('0') << self[i];
  928. ss << "0123456789ABCDEF"[self[i] >> 4];
  929. ss << "0123456789ABCDEF"[self[i] & 0xf];
  930. }
  931. ss << "'";
  932. return VAR(ss.str());
  933. });
  934. _vm->bind__len__(_vm->tp_bytes, [](VM* vm, PyObject* obj) {
  935. return (i64)_CAST(Bytes&, obj).size();
  936. });
  937. _vm->bind_method<0>("bytes", "decode", [](VM* vm, ArgsView args) {
  938. const Bytes& self = _CAST(Bytes&, args[0]);
  939. // TODO: check encoding is utf-8
  940. return VAR(Str(self.str()));
  941. });
  942. _vm->bind__eq__(_vm->tp_bytes, [](VM* vm, PyObject* lhs, PyObject* rhs) {
  943. if(!is_non_tagged_type(rhs, vm->tp_bytes)) return vm->NotImplemented;
  944. return VAR(_CAST(Bytes&, lhs) == _CAST(Bytes&, rhs));
  945. });
  946. /************ slice ************/
  947. _vm->bind_constructor<4>("slice", [](VM* vm, ArgsView args) {
  948. return VAR(Slice(args[1], args[2], args[3]));
  949. });
  950. _vm->bind__repr__(_vm->tp_slice, [](VM* vm, PyObject* obj) {
  951. const Slice& self = _CAST(Slice&, obj);
  952. SStream ss;
  953. ss << "slice(";
  954. ss << CAST(Str, vm->py_repr(self.start)) << ", ";
  955. ss << CAST(Str, vm->py_repr(self.stop)) << ", ";
  956. ss << CAST(Str, vm->py_repr(self.step)) << ")";
  957. return VAR(ss.str());
  958. });
  959. /************ mappingproxy ************/
  960. _vm->bind_method<0>("mappingproxy", "keys", [](VM* vm, ArgsView args) {
  961. MappingProxy& self = _CAST(MappingProxy&, args[0]);
  962. List keys;
  963. for(StrName name : self.attr().keys()) keys.push_back(VAR(name.sv()));
  964. return VAR(std::move(keys));
  965. });
  966. _vm->bind_method<0>("mappingproxy", "values", [](VM* vm, ArgsView args) {
  967. MappingProxy& self = _CAST(MappingProxy&, args[0]);
  968. List values;
  969. for(auto& item : self.attr().items()) values.push_back(item.second);
  970. return VAR(std::move(values));
  971. });
  972. _vm->bind_method<0>("mappingproxy", "items", [](VM* vm, ArgsView args) {
  973. MappingProxy& self = _CAST(MappingProxy&, args[0]);
  974. List items;
  975. for(auto& item : self.attr().items()){
  976. PyObject* t = VAR(Tuple({VAR(item.first.sv()), item.second}));
  977. items.push_back(std::move(t));
  978. }
  979. return VAR(std::move(items));
  980. });
  981. _vm->bind__len__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) {
  982. return (i64)_CAST(MappingProxy&, obj).attr().size();
  983. });
  984. _vm->bind__eq__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj, PyObject* other){
  985. MappingProxy& a = _CAST(MappingProxy&, obj);
  986. if(!is_non_tagged_type(other, vm->tp_mappingproxy)){
  987. return vm->NotImplemented;
  988. }
  989. MappingProxy& b = _CAST(MappingProxy&, other);
  990. return VAR(a.obj == b.obj);
  991. });
  992. _vm->bind__getitem__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj, PyObject* index) {
  993. MappingProxy& self = _CAST(MappingProxy&, obj);
  994. StrName key = CAST(Str&, index);
  995. PyObject* ret = self.attr().try_get(key);
  996. if(ret == nullptr) vm->AttributeError(key.sv());
  997. return ret;
  998. });
  999. _vm->bind__repr__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj) {
  1000. MappingProxy& self = _CAST(MappingProxy&, obj);
  1001. SStream ss;
  1002. ss << "mappingproxy({";
  1003. bool first = true;
  1004. for(auto& item : self.attr().items()){
  1005. if(!first) ss << ", ";
  1006. first = false;
  1007. ss << item.first.escape() << ": " << CAST(Str, vm->py_repr(item.second));
  1008. }
  1009. ss << "})";
  1010. return VAR(ss.str());
  1011. });
  1012. _vm->bind__contains__(_vm->tp_mappingproxy, [](VM* vm, PyObject* obj, PyObject* key) {
  1013. MappingProxy& self = _CAST(MappingProxy&, obj);
  1014. return VAR(self.attr().contains(CAST(Str&, key)));
  1015. });
  1016. /************ dict ************/
  1017. _vm->bind_constructor<-1>("dict", [](VM* vm, ArgsView args){
  1018. return VAR(Dict(vm));
  1019. });
  1020. _vm->bind_method<-1>("dict", "__init__", [](VM* vm, ArgsView args){
  1021. if(args.size() == 1+0) return vm->None;
  1022. if(args.size() == 1+1){
  1023. auto _lock = vm->heap.gc_scope_lock();
  1024. Dict& self = _CAST(Dict&, args[0]);
  1025. List& list = CAST(List&, args[1]);
  1026. for(PyObject* item : list){
  1027. Tuple& t = CAST(Tuple&, item);
  1028. if(t.size() != 2){
  1029. vm->ValueError("dict() takes an iterable of tuples (key, value)");
  1030. return vm->None;
  1031. }
  1032. self.set(t[0], t[1]);
  1033. }
  1034. return vm->None;
  1035. }
  1036. vm->TypeError("dict() takes at most 1 argument");
  1037. return vm->None;
  1038. });
  1039. _vm->bind__len__(_vm->tp_dict, [](VM* vm, PyObject* obj) {
  1040. return (i64)_CAST(Dict&, obj).size();
  1041. });
  1042. _vm->bind__getitem__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* index) {
  1043. Dict& self = _CAST(Dict&, obj);
  1044. PyObject* ret = self.try_get(index);
  1045. if(ret == nullptr) vm->KeyError(index);
  1046. return ret;
  1047. });
  1048. _vm->bind__setitem__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* key, PyObject* value) {
  1049. Dict& self = _CAST(Dict&, obj);
  1050. self.set(key, value);
  1051. });
  1052. _vm->bind__delitem__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* key) {
  1053. Dict& self = _CAST(Dict&, obj);
  1054. bool ok = self.erase(key);
  1055. if(!ok) vm->KeyError(key);
  1056. });
  1057. _vm->bind_method<-1>("dict", "pop", [](VM* vm, ArgsView args) {
  1058. if(args.size() != 2 && args.size() != 3){
  1059. vm->TypeError("pop() expected 1 or 2 arguments");
  1060. return vm->None;
  1061. }
  1062. Dict& self = _CAST(Dict&, args[0]);
  1063. PyObject* value = self.try_get(args[1]);
  1064. if(value == nullptr){
  1065. if(args.size() == 2) vm->KeyError(args[1]);
  1066. if(args.size() == 3){
  1067. return args[2];
  1068. }
  1069. }
  1070. self.erase(args[1]);
  1071. return value;
  1072. });
  1073. // _vm->bind_method<0>("dict", "_data", [](VM* vm, ArgsView args) {
  1074. // Dict& self = _CAST(Dict&, args[0]);
  1075. // SStream ss;
  1076. // ss << "[\n";
  1077. // for(int i=0; i<self._capacity; i++){
  1078. // auto item = self._items[i];
  1079. // Str key("None");
  1080. // Str value("None");
  1081. // if(item.first != nullptr){
  1082. // key = CAST(Str&, vm->py_repr(item.first));
  1083. // }
  1084. // if(item.second != nullptr){
  1085. // value = CAST(Str&, vm->py_repr(item.second));
  1086. // }
  1087. // int prev = self._nodes[i].prev;
  1088. // int next = self._nodes[i].next;
  1089. // ss << " [" << key << ", " << value << ", " << prev << ", " << next << "],\n";
  1090. // }
  1091. // ss << "]\n";
  1092. // vm->stdout_write(ss.str());
  1093. // return vm->None;
  1094. // });
  1095. _vm->bind__contains__(_vm->tp_dict, [](VM* vm, PyObject* obj, PyObject* key) {
  1096. Dict& self = _CAST(Dict&, obj);
  1097. return VAR(self.contains(key));
  1098. });
  1099. _vm->bind__iter__(_vm->tp_dict, [](VM* vm, PyObject* obj) {
  1100. const Dict& self = _CAST(Dict&, obj);
  1101. return vm->py_iter(VAR(self.keys()));
  1102. });
  1103. _vm->bind_method<-1>("dict", "get", [](VM* vm, ArgsView args) {
  1104. Dict& self = _CAST(Dict&, args[0]);
  1105. if(args.size() == 1+1){
  1106. PyObject* ret = self.try_get(args[1]);
  1107. if(ret != nullptr) return ret;
  1108. return vm->None;
  1109. }else if(args.size() == 1+2){
  1110. PyObject* ret = self.try_get(args[1]);
  1111. if(ret != nullptr) return ret;
  1112. return args[2];
  1113. }
  1114. vm->TypeError("get() takes at most 2 arguments");
  1115. return vm->None;
  1116. });
  1117. _vm->bind_method<0>("dict", "keys", [](VM* vm, ArgsView args) {
  1118. const Dict& self = _CAST(Dict&, args[0]);
  1119. return VAR(self.keys());
  1120. });
  1121. _vm->bind_method<0>("dict", "values", [](VM* vm, ArgsView args) {
  1122. const Dict& self = _CAST(Dict&, args[0]);
  1123. return VAR(self.values());
  1124. });
  1125. _vm->bind_method<0>("dict", "items", [](VM* vm, ArgsView args) {
  1126. const Dict& self = _CAST(Dict&, args[0]);
  1127. Tuple items(self.size());
  1128. int j = 0;
  1129. self.apply([&](PyObject* k, PyObject* v){
  1130. items[j++] = VAR(Tuple({k, v}));
  1131. });
  1132. return VAR(std::move(items));
  1133. });
  1134. _vm->bind_method<1>("dict", "update", [](VM* vm, ArgsView args) {
  1135. Dict& self = _CAST(Dict&, args[0]);
  1136. const Dict& other = CAST(Dict&, args[1]);
  1137. self.update(other);
  1138. return vm->None;
  1139. });
  1140. _vm->bind_method<0>("dict", "copy", [](VM* vm, ArgsView args) {
  1141. const Dict& self = _CAST(Dict&, args[0]);
  1142. return VAR(self);
  1143. });
  1144. _vm->bind_method<0>("dict", "clear", [](VM* vm, ArgsView args) {
  1145. Dict& self = _CAST(Dict&, args[0]);
  1146. self.clear();
  1147. return vm->None;
  1148. });
  1149. _vm->bind__repr__(_vm->tp_dict, [](VM* vm, PyObject* obj) {
  1150. Dict& self = _CAST(Dict&, obj);
  1151. SStream ss;
  1152. ss << "{";
  1153. bool first = true;
  1154. self.apply([&](PyObject* k, PyObject* v){
  1155. if(!first) ss << ", ";
  1156. first = false;
  1157. Str key = CAST(Str&, vm->py_repr(k));
  1158. Str value = CAST(Str&, vm->py_repr(v));
  1159. ss << key << ": " << value;
  1160. });
  1161. ss << "}";
  1162. return VAR(ss.str());
  1163. });
  1164. _vm->bind__eq__(_vm->tp_dict, [](VM* vm, PyObject* a, PyObject* b) {
  1165. Dict& self = _CAST(Dict&, a);
  1166. if(!is_non_tagged_type(b, vm->tp_dict)) return vm->NotImplemented;
  1167. Dict& other = _CAST(Dict&, b);
  1168. if(self.size() != other.size()) return vm->False;
  1169. for(int i=0; i<self._capacity; i++){
  1170. auto item = self._items[i];
  1171. if(item.first == nullptr) continue;
  1172. PyObject* value = other.try_get(item.first);
  1173. if(value == nullptr) return vm->False;
  1174. if(!vm->py_eq(item.second, value)) return vm->False;
  1175. }
  1176. return vm->True;
  1177. });
  1178. _vm->bind__repr__(_vm->tp_module, [](VM* vm, PyObject* obj) {
  1179. const Str& path = CAST(Str&, obj->attr(__path__));
  1180. return VAR(fmt("<module ", path.escape(), ">"));
  1181. });
  1182. /************ property ************/
  1183. _vm->bind_constructor<-1>("property", [](VM* vm, ArgsView args) {
  1184. if(args.size() == 1+1){
  1185. return VAR(Property(args[1], vm->None, ""));
  1186. }else if(args.size() == 1+2){
  1187. return VAR(Property(args[1], args[2], ""));
  1188. }else if(args.size() == 1+3){
  1189. return VAR(Property(args[1], args[2], CAST(Str, args[3])));
  1190. }
  1191. vm->TypeError("property() takes at most 3 arguments");
  1192. return vm->None;
  1193. });
  1194. _vm->bind_property(_vm->_t(_vm->tp_property), "__signature__", [](VM* vm, ArgsView args){
  1195. Property& self = _CAST(Property&, args[0]);
  1196. return VAR(self.signature);
  1197. });
  1198. _vm->bind_property(_vm->_t(_vm->tp_function), "__doc__", [](VM* vm, ArgsView args) {
  1199. Function& func = _CAST(Function&, args[0]);
  1200. return VAR(func.decl->docstring);
  1201. });
  1202. _vm->bind_property(_vm->_t(_vm->tp_native_func), "__doc__", [](VM* vm, ArgsView args) {
  1203. NativeFunc& func = _CAST(NativeFunc&, args[0]);
  1204. if(func.decl != nullptr) return VAR(func.decl->docstring);
  1205. return VAR("");
  1206. });
  1207. _vm->bind_property(_vm->_t(_vm->tp_function), "__signature__", [](VM* vm, ArgsView args) {
  1208. Function& func = _CAST(Function&, args[0]);
  1209. return VAR(func.decl->signature);
  1210. });
  1211. // _vm->bind_property(_vm->_t(_vm->tp_function), "__call__", [](VM* vm, ArgsView args) {
  1212. // return args[0];
  1213. // });
  1214. _vm->bind_property(_vm->_t(_vm->tp_native_func), "__signature__", [](VM* vm, ArgsView args) {
  1215. NativeFunc& func = _CAST(NativeFunc&, args[0]);
  1216. if(func.decl != nullptr) return VAR(func.decl->signature);
  1217. return VAR("");
  1218. });
  1219. // _vm->bind_property(_vm->_t(_vm->tp_native_func), "__call__", [](VM* vm, ArgsView args) {
  1220. // return args[0];
  1221. // });
  1222. RangeIter::register_class(_vm, _vm->builtins);
  1223. ArrayIter::register_class(_vm, _vm->builtins);
  1224. StringIter::register_class(_vm, _vm->builtins);
  1225. Generator::register_class(_vm, _vm->builtins);
  1226. }
  1227. void add_module_timeit(VM* vm){
  1228. PyObject* mod = vm->new_module("timeit");
  1229. vm->bind_func<2>(mod, "timeit", [](VM* vm, ArgsView args) {
  1230. PyObject* f = args[0];
  1231. i64 iters = CAST(i64, args[1]);
  1232. auto now = std::chrono::system_clock::now();
  1233. for(i64 i=0; i<iters; i++) vm->call(f);
  1234. auto end = std::chrono::system_clock::now();
  1235. f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - now).count() / 1000.0;
  1236. return VAR(elapsed);
  1237. });
  1238. }
  1239. void add_module_operator(VM* vm){
  1240. PyObject* mod = vm->new_module("operator");
  1241. vm->bind_func<2>(mod, "lt", [](VM* vm, ArgsView args) { return VAR(vm->py_lt(args[0], args[1]));});
  1242. vm->bind_func<2>(mod, "le", [](VM* vm, ArgsView args) { return VAR(vm->py_le(args[0], args[1]));});
  1243. vm->bind_func<2>(mod, "eq", [](VM* vm, ArgsView args) { return VAR(vm->py_eq(args[0], args[1]));});
  1244. vm->bind_func<2>(mod, "ne", [](VM* vm, ArgsView args) { return VAR(vm->py_ne(args[0], args[1]));});
  1245. vm->bind_func<2>(mod, "ge", [](VM* vm, ArgsView args) { return VAR(vm->py_ge(args[0], args[1]));});
  1246. vm->bind_func<2>(mod, "gt", [](VM* vm, ArgsView args) { return VAR(vm->py_gt(args[0], args[1]));});
  1247. }
  1248. struct PyStructTime{
  1249. PY_CLASS(PyStructTime, time, struct_time)
  1250. int tm_year;
  1251. int tm_mon;
  1252. int tm_mday;
  1253. int tm_hour;
  1254. int tm_min;
  1255. int tm_sec;
  1256. int tm_wday;
  1257. int tm_yday;
  1258. int tm_isdst;
  1259. PyStructTime(std::time_t t){
  1260. std::tm* tm = std::localtime(&t);
  1261. tm_year = tm->tm_year + 1900;
  1262. tm_mon = tm->tm_mon + 1;
  1263. tm_mday = tm->tm_mday;
  1264. tm_hour = tm->tm_hour;
  1265. tm_min = tm->tm_min;
  1266. tm_sec = tm->tm_sec + 1;
  1267. tm_wday = (tm->tm_wday + 6) % 7;
  1268. tm_yday = tm->tm_yday + 1;
  1269. tm_isdst = tm->tm_isdst;
  1270. }
  1271. PyStructTime* _() { return this; }
  1272. static void _register(VM* vm, PyObject* mod, PyObject* type){
  1273. vm->bind_notimplemented_constructor<PyStructTime>(type);
  1274. PY_READONLY_FIELD(PyStructTime, "tm_year", _, tm_year);
  1275. PY_READONLY_FIELD(PyStructTime, "tm_mon", _, tm_mon);
  1276. PY_READONLY_FIELD(PyStructTime, "tm_mday", _, tm_mday);
  1277. PY_READONLY_FIELD(PyStructTime, "tm_hour", _, tm_hour);
  1278. PY_READONLY_FIELD(PyStructTime, "tm_min", _, tm_min);
  1279. PY_READONLY_FIELD(PyStructTime, "tm_sec", _, tm_sec);
  1280. PY_READONLY_FIELD(PyStructTime, "tm_wday", _, tm_wday);
  1281. PY_READONLY_FIELD(PyStructTime, "tm_yday", _, tm_yday);
  1282. PY_READONLY_FIELD(PyStructTime, "tm_isdst", _, tm_isdst);
  1283. }
  1284. };
  1285. void add_module_time(VM* vm){
  1286. PyObject* mod = vm->new_module("time");
  1287. PyStructTime::register_class(vm, mod);
  1288. vm->bind_func<0>(mod, "time", [](VM* vm, ArgsView args) {
  1289. auto now = std::chrono::system_clock::now();
  1290. return VAR(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count() / 1000.0);
  1291. });
  1292. vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) {
  1293. f64 seconds = CAST_F(args[0]);
  1294. auto begin = std::chrono::system_clock::now();
  1295. while(true){
  1296. auto now = std::chrono::system_clock::now();
  1297. f64 elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() / 1000.0;
  1298. if(elapsed >= seconds) break;
  1299. }
  1300. return vm->None;
  1301. });
  1302. vm->bind_func<0>(mod, "localtime", [](VM* vm, ArgsView args) {
  1303. auto now = std::chrono::system_clock::now();
  1304. std::time_t t = std::chrono::system_clock::to_time_t(now);
  1305. return VAR_T(PyStructTime, t);
  1306. });
  1307. }
  1308. void add_module_sys(VM* vm){
  1309. PyObject* mod = vm->new_module("sys");
  1310. vm->setattr(mod, "version", VAR(PK_VERSION));
  1311. vm->setattr(mod, "platform", VAR(PK_SYS_PLATFORM));
  1312. PyObject* stdout_ = vm->heap.gcnew<DummyInstance>(vm->tp_object);
  1313. PyObject* stderr_ = vm->heap.gcnew<DummyInstance>(vm->tp_object);
  1314. vm->setattr(mod, "stdout", stdout_);
  1315. vm->setattr(mod, "stderr", stderr_);
  1316. vm->bind_func<1>(stdout_, "write", [](VM* vm, ArgsView args) {
  1317. Str& s = CAST(Str&, args[0]);
  1318. vm->_stdout(vm, s.data, s.size);
  1319. return vm->None;
  1320. });
  1321. vm->bind_func<1>(stderr_, "write", [](VM* vm, ArgsView args) {
  1322. Str& s = CAST(Str&, args[0]);
  1323. vm->_stderr(vm, s.data, s.size);
  1324. return vm->None;
  1325. });
  1326. }
  1327. void add_module_json(VM* vm){
  1328. PyObject* mod = vm->new_module("json");
  1329. vm->bind_func<1>(mod, "loads", [](VM* vm, ArgsView args) {
  1330. const Str& expr = CAST(Str&, args[0]);
  1331. CodeObject_ code = vm->compile(expr, "<json>", JSON_MODE);
  1332. return vm->_exec(code, vm->top_frame()->_module);
  1333. });
  1334. vm->bind_func<1>(mod, "dumps", [](VM* vm, ArgsView args) {
  1335. return vm->py_json(args[0]);
  1336. });
  1337. }
  1338. // https://docs.python.org/3.5/library/math.html
  1339. void add_module_math(VM* vm){
  1340. PyObject* mod = vm->new_module("math");
  1341. mod->attr().set("pi", VAR(3.1415926535897932384));
  1342. mod->attr().set("e" , VAR(2.7182818284590452354));
  1343. mod->attr().set("inf", VAR(std::numeric_limits<double>::infinity()));
  1344. mod->attr().set("nan", VAR(std::numeric_limits<double>::quiet_NaN()));
  1345. vm->bind_func<1>(mod, "ceil", PK_LAMBDA(VAR((i64)std::ceil(CAST_F(args[0])))));
  1346. vm->bind_func<1>(mod, "fabs", PK_LAMBDA(VAR(std::fabs(CAST_F(args[0])))));
  1347. vm->bind_func<1>(mod, "floor", PK_LAMBDA(VAR((i64)std::floor(CAST_F(args[0])))));
  1348. vm->bind_func<1>(mod, "fsum", [](VM* vm, ArgsView args) {
  1349. List& list = CAST(List&, args[0]);
  1350. double sum = 0;
  1351. double c = 0;
  1352. for(PyObject* arg : list){
  1353. double x = CAST_F(arg);
  1354. double y = x - c;
  1355. double t = sum + y;
  1356. c = (t - sum) - y;
  1357. sum = t;
  1358. }
  1359. return VAR(sum);
  1360. });
  1361. vm->bind_func<2>(mod, "gcd", [](VM* vm, ArgsView args) {
  1362. i64 a = CAST(i64, args[0]);
  1363. i64 b = CAST(i64, args[1]);
  1364. if(a < 0) a = -a;
  1365. if(b < 0) b = -b;
  1366. while(b != 0){
  1367. i64 t = b;
  1368. b = a % b;
  1369. a = t;
  1370. }
  1371. return VAR(a);
  1372. });
  1373. vm->bind_func<1>(mod, "isfinite", PK_LAMBDA(VAR(std::isfinite(CAST_F(args[0])))));
  1374. vm->bind_func<1>(mod, "isinf", PK_LAMBDA(VAR(std::isinf(CAST_F(args[0])))));
  1375. vm->bind_func<1>(mod, "isnan", PK_LAMBDA(VAR(std::isnan(CAST_F(args[0])))));
  1376. vm->bind_func<1>(mod, "exp", PK_LAMBDA(VAR(std::exp(CAST_F(args[0])))));
  1377. vm->bind_func<1>(mod, "log", PK_LAMBDA(VAR(std::log(CAST_F(args[0])))));
  1378. vm->bind_func<1>(mod, "log2", PK_LAMBDA(VAR(std::log2(CAST_F(args[0])))));
  1379. vm->bind_func<1>(mod, "log10", PK_LAMBDA(VAR(std::log10(CAST_F(args[0])))));
  1380. vm->bind_func<2>(mod, "pow", PK_LAMBDA(VAR(std::pow(CAST_F(args[0]), CAST_F(args[1])))));
  1381. vm->bind_func<1>(mod, "sqrt", PK_LAMBDA(VAR(std::sqrt(CAST_F(args[0])))));
  1382. vm->bind_func<1>(mod, "acos", PK_LAMBDA(VAR(std::acos(CAST_F(args[0])))));
  1383. vm->bind_func<1>(mod, "asin", PK_LAMBDA(VAR(std::asin(CAST_F(args[0])))));
  1384. vm->bind_func<1>(mod, "atan", PK_LAMBDA(VAR(std::atan(CAST_F(args[0])))));
  1385. vm->bind_func<2>(mod, "atan2", PK_LAMBDA(VAR(std::atan2(CAST_F(args[0]), CAST_F(args[1])))));
  1386. vm->bind_func<1>(mod, "cos", PK_LAMBDA(VAR(std::cos(CAST_F(args[0])))));
  1387. vm->bind_func<1>(mod, "sin", PK_LAMBDA(VAR(std::sin(CAST_F(args[0])))));
  1388. vm->bind_func<1>(mod, "tan", PK_LAMBDA(VAR(std::tan(CAST_F(args[0])))));
  1389. vm->bind_func<1>(mod, "degrees", PK_LAMBDA(VAR(CAST_F(args[0]) * 180 / 3.1415926535897932384)));
  1390. vm->bind_func<1>(mod, "radians", PK_LAMBDA(VAR(CAST_F(args[0]) * 3.1415926535897932384 / 180)));
  1391. vm->bind_func<1>(mod, "modf", [](VM* vm, ArgsView args) {
  1392. f64 i;
  1393. f64 f = std::modf(CAST_F(args[0]), &i);
  1394. return VAR(Tuple({VAR(f), VAR(i)}));
  1395. });
  1396. vm->bind_func<1>(mod, "factorial", [](VM* vm, ArgsView args) {
  1397. i64 n = CAST(i64, args[0]);
  1398. if(n < 0) vm->ValueError("factorial() not defined for negative values");
  1399. i64 r = 1;
  1400. for(i64 i=2; i<=n; i++) r *= i;
  1401. return VAR(r);
  1402. });
  1403. }
  1404. void add_module_traceback(VM* vm){
  1405. PyObject* mod = vm->new_module("traceback");
  1406. vm->bind_func<0>(mod, "print_exc", [](VM* vm, ArgsView args) {
  1407. if(vm->_last_exception==nullptr) vm->ValueError("no exception");
  1408. Exception& e = CAST(Exception&, vm->_last_exception);
  1409. Str sum = e.summary();
  1410. vm->_stdout(vm, sum.data, sum.size);
  1411. return vm->None;
  1412. });
  1413. vm->bind_func<0>(mod, "format_exc", [](VM* vm, ArgsView args) {
  1414. if(vm->_last_exception==nullptr) vm->ValueError("no exception");
  1415. Exception& e = CAST(Exception&, vm->_last_exception);
  1416. return VAR(e.summary());
  1417. });
  1418. }
  1419. void add_module_dis(VM* vm){
  1420. PyObject* mod = vm->new_module("dis");
  1421. static const auto get_code = [](VM* vm, PyObject* obj)->CodeObject_{
  1422. if(is_type(obj, vm->tp_str)){
  1423. const Str& source = CAST(Str, obj);
  1424. return vm->compile(source, "<dis>", EXEC_MODE);
  1425. }
  1426. PyObject* f = obj;
  1427. if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, obj).func;
  1428. return CAST(Function&, f).decl->code;
  1429. };
  1430. vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) {
  1431. CodeObject_ code = get_code(vm, args[0]);
  1432. Str msg = vm->disassemble(code);
  1433. vm->_stdout(vm, msg.data, msg.size);
  1434. return vm->None;
  1435. });
  1436. // vm->bind_func<1>(mod, "_s", [](VM* vm, ArgsView args) {
  1437. // CodeObject_ code = get_code(vm, args[0]);
  1438. // return VAR(code->serialize(vm));
  1439. // });
  1440. }
  1441. void add_module_gc(VM* vm){
  1442. PyObject* mod = vm->new_module("gc");
  1443. vm->bind_func<0>(mod, "collect", PK_LAMBDA(VAR(vm->heap.collect())));
  1444. }
  1445. void VM::post_init(){
  1446. init_builtins(this);
  1447. // type
  1448. bind__getitem__(tp_type, [](VM* vm, PyObject* self, PyObject* _){
  1449. PK_UNUSED(_);
  1450. return self; // for generics
  1451. });
  1452. bind__repr__(tp_type, [](VM* vm, PyObject* self){
  1453. SStream ss;
  1454. const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, self)];
  1455. ss << "<class '" << info.name << "'>";
  1456. return VAR(ss.str());
  1457. });
  1458. bind_property(_t(tp_object), "__class__", PK_LAMBDA(vm->_t(args[0])));
  1459. bind_property(_t(tp_type), "__base__", [](VM* vm, ArgsView args){
  1460. const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])];
  1461. return info.base.index == -1 ? vm->None : vm->_all_types[info.base].obj;
  1462. });
  1463. bind_property(_t(tp_type), "__name__", [](VM* vm, ArgsView args){
  1464. const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])];
  1465. return VAR(info.name);
  1466. });
  1467. bind_property(_t(tp_type), "__module__", [](VM* vm, ArgsView args){
  1468. const PyTypeInfo& info = vm->_all_types[PK_OBJ_GET(Type, args[0])];
  1469. if(info.mod == nullptr) return vm->None;
  1470. return info.mod;
  1471. });
  1472. bind_property(_t(tp_bound_method), "__self__", [](VM* vm, ArgsView args){
  1473. return CAST(BoundMethod&, args[0]).self;
  1474. });
  1475. bind_property(_t(tp_bound_method), "__func__", [](VM* vm, ArgsView args){
  1476. return CAST(BoundMethod&, args[0]).func;
  1477. });
  1478. bind__eq__(tp_bound_method, [](VM* vm, PyObject* lhs, PyObject* rhs){
  1479. if(!is_non_tagged_type(rhs, vm->tp_bound_method)) return vm->NotImplemented;
  1480. return VAR(_CAST(BoundMethod&, lhs) == _CAST(BoundMethod&, rhs));
  1481. });
  1482. bind_property(_t(tp_slice), "start", [](VM* vm, ArgsView args){
  1483. return CAST(Slice&, args[0]).start;
  1484. });
  1485. bind_property(_t(tp_slice), "stop", [](VM* vm, ArgsView args){
  1486. return CAST(Slice&, args[0]).stop;
  1487. });
  1488. bind_property(_t(tp_slice), "step", [](VM* vm, ArgsView args){
  1489. return CAST(Slice&, args[0]).step;
  1490. });
  1491. bind_property(_t(tp_object), "__dict__", [](VM* vm, ArgsView args){
  1492. if(is_tagged(args[0]) || !args[0]->is_attr_valid()) return vm->None;
  1493. return VAR(MappingProxy(args[0]));
  1494. });
  1495. #if !PK_DEBUG_NO_BUILTINS
  1496. add_module_sys(this);
  1497. add_module_traceback(this);
  1498. add_module_time(this);
  1499. add_module_json(this);
  1500. add_module_math(this);
  1501. add_module_re(this);
  1502. add_module_dis(this);
  1503. add_module_c(this);
  1504. add_module_gc(this);
  1505. add_module_random(this);
  1506. add_module_base64(this);
  1507. add_module_timeit(this);
  1508. add_module_operator(this);
  1509. for(const char* name: {"this", "functools", "heapq", "bisect", "pickle", "_long", "colorsys", "typing", "datetime"}){
  1510. _lazy_modules[name] = kPythonLibs[name];
  1511. }
  1512. try{
  1513. CodeObject_ code = compile(kPythonLibs["builtins"], "<builtins>", EXEC_MODE);
  1514. this->_exec(code, this->builtins);
  1515. code = compile(kPythonLibs["_set"], "<set>", EXEC_MODE);
  1516. this->_exec(code, this->builtins);
  1517. }catch(Exception& e){
  1518. std::cerr << e.summary() << std::endl;
  1519. std::cerr << "failed to load builtins module!!" << std::endl;
  1520. exit(1);
  1521. }
  1522. if(enable_os){
  1523. add_module_io(this);
  1524. add_module_os(this);
  1525. _import_handler = _default_import_handler;
  1526. }
  1527. add_module_linalg(this);
  1528. add_module_easing(this);
  1529. add_module_collections(this);
  1530. #ifdef PK_USE_BOX2D
  1531. add_module_box2d(this);
  1532. #endif
  1533. #ifdef PK_USE_CJSON
  1534. add_module_cjson(this);
  1535. #endif
  1536. #endif
  1537. }
  1538. CodeObject_ VM::compile(Str source, Str filename, CompileMode mode, bool unknown_global_scope) {
  1539. Compiler compiler(this, source, filename, mode, unknown_global_scope);
  1540. try{
  1541. return compiler.compile();
  1542. }catch(Exception& e){
  1543. #if PK_DEBUG_FULL_EXCEPTION
  1544. std::cerr << e.summary() << std::endl;
  1545. #endif
  1546. _error(e);
  1547. return nullptr;
  1548. }
  1549. }
  1550. } // namespace pkpy