BoxedModule* importTestExtension(const std::string& name) { std::string pathname_name = "test/test_extension/" + name + ".pyston.so"; const char* pathname = pathname_name.c_str(); void* handle = dlopen(pathname, RTLD_NOW); if (!handle) { fprintf(stderr, "%s\n", dlerror()); exit(1); } assert(handle); std::string initname = "init" + name; void (*init)() = (void (*)())dlsym(handle, initname.c_str()); char* error; if ((error = dlerror()) != NULL) { fprintf(stderr, "%s\n", error); exit(1); } assert(init); (*init)(); BoxedDict* sys_modules = getSysModulesDict(); Box* s = boxStrConstant(name.c_str()); Box* _m = sys_modules->d[s]; RELEASE_ASSERT(_m, "module failed to initialize properly?"); assert(_m->cls == module_cls); BoxedModule* m = static_cast<BoxedModule*>(_m); m->setattr("__file__", boxStrConstant(pathname), NULL); m->fn = pathname; return m; }
void setupCAPI() { capifunc_cls = new BoxedClass(type_cls, object_cls, NULL, 0, sizeof(BoxedCApiFunction), false); capifunc_cls->giveAttr("__name__", boxStrConstant("capifunc")); capifunc_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)BoxedCApiFunction::__repr__, UNKNOWN, 1))); capifunc_cls->giveAttr("__str__", capifunc_cls->getattr("__repr__")); capifunc_cls->giveAttr( "__call__", new BoxedFunction(boxRTFunction((void*)BoxedCApiFunction::__call__, UNKNOWN, 1, 0, true, true))); capifunc_cls->freeze(); method_cls = new BoxedClass(type_cls, object_cls, NULL, 0, sizeof(BoxedMethodDescriptor), false); method_cls->giveAttr("__name__", boxStrConstant("method")); method_cls->giveAttr("__get__", new BoxedFunction(boxRTFunction((void*)BoxedMethodDescriptor::__get__, UNKNOWN, 3))); method_cls->giveAttr("__call__", new BoxedFunction(boxRTFunction((void*)BoxedMethodDescriptor::__call__, UNKNOWN, 2, 0, true, true))); method_cls->freeze(); wrapperdescr_cls = new BoxedClass(type_cls, object_cls, NULL, 0, sizeof(BoxedWrapperDescriptor), false); wrapperdescr_cls->giveAttr("__name__", boxStrConstant("wrapper_descriptor")); wrapperdescr_cls->giveAttr("__get__", new BoxedFunction(boxRTFunction((void*)BoxedWrapperDescriptor::__get__, UNKNOWN, 3))); wrapperdescr_cls->freeze(); wrapperobject_cls = new BoxedClass(type_cls, object_cls, NULL, 0, sizeof(BoxedWrapperObject), false); wrapperobject_cls->giveAttr("__name__", boxStrConstant("method-wrapper")); wrapperobject_cls->giveAttr( "__call__", new BoxedFunction(boxRTFunction((void*)BoxedWrapperObject::__call__, UNKNOWN, 1, 0, true, true))); wrapperobject_cls->freeze(); }
void setupDict() { dict_iterator_cls = new BoxedClass(object_cls, 0, sizeof(BoxedDict), false); dict_cls->giveAttr("__name__", boxStrConstant("dict")); // dict_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)dictLen, NULL, 1))); // dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, NULL, 2))); // dict_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)dictNew, NULL, 1))); // dict_cls->giveAttr("__init__", new BoxedFunction(boxRTFunction((void*)dictInit, NULL, 1))); dict_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)dictRepr, STR, 1))); dict_cls->giveAttr("__str__", dict_cls->getattr("__repr__")); dict_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)dictIterKeys, typeFromClass(dict_iterator_cls), 1))); dict_cls->giveAttr("items", new BoxedFunction(boxRTFunction((void*)dictItems, LIST, 1))); dict_cls->giveAttr("iteritems", new BoxedFunction(boxRTFunction((void*)dictIterItems, typeFromClass(dict_iterator_cls), 1))); dict_cls->giveAttr("values", new BoxedFunction(boxRTFunction((void*)dictValues, LIST, 1))); dict_cls->giveAttr("itervalues", new BoxedFunction(boxRTFunction((void*)dictIterValues, typeFromClass(dict_iterator_cls), 1))); dict_cls->giveAttr("keys", new BoxedFunction(boxRTFunction((void*)dictKeys, LIST, 1))); dict_cls->giveAttr("iterkeys", dict_cls->getattr("__iter__")); dict_cls->giveAttr("pop", new BoxedFunction(boxRTFunction((void*)dictPop, UNKNOWN, 3, 1, false, false), { NULL })); dict_cls->giveAttr("get", new BoxedFunction(boxRTFunction((void*)dictGet, UNKNOWN, 3, 1, false, false), { None })); dict_cls->giveAttr("setdefault", new BoxedFunction(boxRTFunction((void*)dictSetdefault, UNKNOWN, 3, 1, false, false), { None })); dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, UNKNOWN, 2))); dict_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)dictSetitem, NONE, 3))); dict_cls->freeze(); gc::registerStaticRootObj(dict_iterator_cls); dict_iterator_cls->giveAttr("__name__", boxStrConstant("dictiterator")); CLFunction* hasnext = boxRTFunction((void*)dictIterHasnextUnboxed, BOOL, 1); addRTFunction(hasnext, (void*)dictIterHasnext, BOXED_BOOL); dict_iterator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext)); dict_iterator_cls->giveAttr( "__iter__", new BoxedFunction(boxRTFunction((void*)dictIterIter, typeFromClass(dict_iterator_cls), 1))); dict_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)dictIterNext, UNKNOWN, 1))); dict_iterator_cls->freeze(); }
void setupComplex() { complex_cls->giveAttr("__name__", boxStrConstant("complex")); complex_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)complexNew, UNKNOWN, 3, 2, false, false), { boxInt(0), boxInt(0) })); _addFunc("__add__", BOXED_COMPLEX, (void*)complexAddComplex, (void*)complexAddFloat, (void*)complexAddInt, (void*)complexAdd); _addFunc("__sub__", BOXED_COMPLEX, (void*)complexSubComplex, (void*)complexSubFloat, (void*)complexSubInt, (void*)complexSub); _addFunc("__mul__", BOXED_COMPLEX, (void*)complexMulComplex, (void*)complexMulFloat, (void*)complexMulInt, (void*)complexMul); _addFunc("__div__", BOXED_COMPLEX, (void*)complexDivComplex, (void*)complexDivFloat, (void*)complexDivInt, (void*)complexDiv); complex_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)complexStr, STR, 1))); complex_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)complexRepr, STR, 1))); complex_cls->giveAttr("real", new BoxedMemberDescriptor(BoxedMemberDescriptor::FLOAT, offsetof(BoxedComplex, real))); complex_cls->giveAttr("imag", new BoxedMemberDescriptor(BoxedMemberDescriptor::FLOAT, offsetof(BoxedComplex, imag))); complex_cls->freeze(); }
void raiseExcHelper(BoxedClass* cls, const char* msg, ...) { auto entries = getTracebackEntries(); last_tb = std::move(entries); if (msg != NULL) { va_list ap; va_start(ap, msg); // printf("Raising: "); // vprintf(msg, ap); // printf("\n"); // va_start(ap, msg); char buf[1024]; vsnprintf(buf, sizeof(buf), msg, ap); va_end(ap); BoxedString* message = boxStrConstant(buf); Box* exc_obj = exceptionNew2(cls, message); raiseExc(exc_obj); } else { Box* exc_obj = exceptionNew1(cls); raiseExc(exc_obj); } }
void setupDict() { dict_cls->giveAttr("__name__", boxStrConstant("dict")); // dict_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)dictLen, NULL, 1, false))); // dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, NULL, 2, false))); // dict_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)dictNew, NULL, 1, false))); // dict_cls->giveAttr("__init__", new BoxedFunction(boxRTFunction((void*)dictInit, NULL, 1, false))); dict_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)dictRepr, NULL, 1, false))); dict_cls->setattr("__str__", dict_cls->peekattr("__repr__"), NULL, NULL); dict_cls->giveAttr("items", new BoxedFunction(boxRTFunction((void*)dictItems, NULL, 1, false))); dict_cls->setattr("iteritems", dict_cls->peekattr("items"), NULL, NULL); dict_cls->giveAttr("values", new BoxedFunction(boxRTFunction((void*)dictValues, NULL, 1, false))); dict_cls->setattr("itervalues", dict_cls->peekattr("values"), NULL, NULL); dict_cls->giveAttr("keys", new BoxedFunction(boxRTFunction((void*)dictKeys, NULL, 1, false))); dict_cls->setattr("iterkeys", dict_cls->peekattr("keys"), NULL, NULL); CLFunction* pop = boxRTFunction((void*)dictPop2, UNKNOWN, 2, false); addRTFunction(pop, (void*)dictPop3, UNKNOWN, 3, false); dict_cls->giveAttr("pop", new BoxedFunction(pop)); CLFunction* get = boxRTFunction((void*)dictGet2, UNKNOWN, 2, false); addRTFunction(get, (void*)dictGet3, UNKNOWN, 3, false); dict_cls->giveAttr("get", new BoxedFunction(get)); dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, NULL, 2, false))); dict_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)dictSetitem, NULL, 3, false))); dict_cls->freeze(); }
Box* typeRepr(BoxedClass* self) { if (isUserDefined(self)) { std::ostringstream os; os << "<class '"; Box* m = self->getattr("__module__"); RELEASE_ASSERT(m, ""); if (m->cls == str_cls) { BoxedString* sm = static_cast<BoxedString*>(m); os << sm->s << '.'; } Box* n = self->getattr("__name__"); RELEASE_ASSERT(n, ""); RELEASE_ASSERT(n->cls == str_cls, "should have prevented you from setting __name__ to non-string"); BoxedString* sn = static_cast<BoxedString*>(n); os << sn->s; os << "'>"; return boxString(os.str()); } else { char buf[80]; snprintf(buf, 80, "<type '%s'>", getNameOfClass(self)->c_str()); return boxStrConstant(buf); } }
void setupFile() { file_cls->giveAttr("__name__", boxStrConstant("file")); CLFunction *read = boxRTFunction((void*)fileRead1, NULL, 1, false); addRTFunction(read, (void*)fileRead2, NULL, 2, false); file_cls->giveAttr("read", new BoxedFunction(read)); CLFunction *readline = boxRTFunction((void*)fileReadline1, STR, 1, false); file_cls->giveAttr("readline", new BoxedFunction(readline)); file_cls->giveAttr("write", new BoxedFunction(boxRTFunction((void*)fileWrite, NULL, 2, false))); file_cls->giveAttr("close", new BoxedFunction(boxRTFunction((void*)fileClose, NULL, 1, false))); file_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)fileRepr, NULL, 1, false))); file_cls->setattr("__str__", file_cls->peekattr("__repr__"), NULL, NULL); file_cls->giveAttr("__enter__", new BoxedFunction(boxRTFunction((void*)fileEnter, NULL, 1, false))); file_cls->giveAttr("__exit__", new BoxedFunction(boxRTFunction((void*)fileExit, NULL, 4, false))); CLFunction *__new__ = boxRTFunction((void*)fileNew2, NULL, 2, false); addRTFunction(__new__, (void*)fileNew3, NULL, 3, false); file_cls->giveAttr("__new__", new BoxedFunction(__new__)); file_cls->freeze(); }
// Have a special helper function for syntax errors, since we want to include the location // of the syntax error in the traceback, even though it is not part of the execution: void raiseSyntaxError(const char* msg, int lineno, int col_offset, const std::string& file, const std::string& func) { Box* exc = runtimeCall(SyntaxError, ArgPassSpec(1), boxStrConstant(msg), NULL, NULL, NULL, NULL); auto tb = getTraceback(); std::vector<const LineInfo*> entries = tb->lines; entries.push_back(new LineInfo(lineno, col_offset, file, func)); raiseRaw(ExcInfo(exc->cls, exc, new BoxedTraceback(std::move(entries)))); }
void setupIter() { seqiter_cls = new BoxedClass(type_cls, object_cls, NULL, 0, sizeof(BoxedSeqIter), false); seqiter_cls->giveAttr("__name__", boxStrConstant("iterator")); seqiter_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)seqiterNext, UNKNOWN, 1))); seqiter_cls->giveAttr("__hasnext__", new BoxedFunction(boxRTFunction((void*)seqiterHasnext, BOXED_BOOL, 1))); seqiter_cls->freeze(); }
// Have a special helper function for syntax errors, since we want to include the location // of the syntax error in the traceback, even though it is not part of the execution: void raiseSyntaxError(const char* msg, int lineno, int col_offset, const std::string& file, const std::string& func) { last_exc = exceptionNew2(SyntaxError, boxStrConstant(msg)); auto entries = getTracebackEntries(); last_tb = std::move(entries); // TODO: leaks this! last_tb.push_back(new LineInfo(lineno, col_offset, file, func)); raiseRaw(last_exc); }
void setupSys() { sys_modules_dict = new BoxedDict(); gc::registerPermanentRoot(sys_modules_dict); // This is ok to call here because we've already created the sys_modules_dict sys_module = createModule("sys", "__builtin__"); sys_module->giveAttr("modules", sys_modules_dict); BoxedList* sys_path = new BoxedList(); sys_module->giveAttr("path", sys_path); sys_module->giveAttr("argv", new BoxedList()); sys_module->giveAttr("stdout", new BoxedFile(stdout)); sys_module->giveAttr("stdin", new BoxedFile(stdin)); sys_module->giveAttr("stderr", new BoxedFile(stderr)); sys_module->giveAttr("warnoptions", new BoxedList()); sys_module->giveAttr("py3kwarning", False); sys_module->giveAttr("platform", boxStrConstant("unknown")); // seems like a reasonable, if poor, default sys_module->giveAttr("hexversion", boxInt(PY_VERSION_HEX)); sys_module->giveAttr("maxint", boxInt(PYSTON_INT_MAX)); sys_flags_cls = new BoxedClass(type_cls, object_cls, BoxedSysFlags::gcHandler, 0, sizeof(BoxedSysFlags), false); sys_flags_cls->giveAttr("__name__", boxStrConstant("flags")); sys_flags_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)BoxedSysFlags::__new__, UNKNOWN, 1, 0, true, true))); #define ADD(name) \ sys_flags_cls->giveAttr(STRINGIFY(name), \ new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, offsetof(BoxedSysFlags, name))) ADD(division_warning); ADD(bytes_warning); #undef ADD sys_flags_cls->freeze(); sys_module->giveAttr("flags", new BoxedSysFlags()); }
void setupCAPI() { capifunc_cls = new BoxedClass(false, NULL); capifunc_cls->giveAttr("__name__", boxStrConstant("capifunc")); capifunc_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)BoxedCApiFunction::__repr__, NULL, 1, false))); capifunc_cls->setattr("__str__", capifunc_cls->peekattr("__repr__"), NULL, NULL); capifunc_cls->giveAttr("__call__", new BoxedFunction(boxRTFunction((void*)BoxedCApiFunction::__call__, NULL, 1, true))); capifunc_cls->freeze(); }
extern "C" PyObject* _PyObject_Str(PyObject* v) noexcept { if (v == NULL) return boxStrConstant("<NULL>"); if (v->cls == str_cls) return v; try { return str(v); } catch (ExcInfo e) { setCAPIException(e); return NULL; } }
void setupFile() { file_cls->giveAttr("__name__", boxStrConstant("file")); file_cls->giveAttr("read", new BoxedFunction(boxRTFunction((void*)fileRead, STR, 2, 1, false, false), { boxInt(-1) })); CLFunction* readline = boxRTFunction((void*)fileReadline1, STR, 1); file_cls->giveAttr("readline", new BoxedFunction(readline)); file_cls->giveAttr("write", new BoxedFunction(boxRTFunction((void*)fileWrite, NONE, 2))); file_cls->giveAttr("close", new BoxedFunction(boxRTFunction((void*)fileClose, NONE, 1))); file_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)fileRepr, STR, 1))); file_cls->giveAttr("__str__", file_cls->getattr("__repr__")); file_cls->giveAttr("__enter__", new BoxedFunction(boxRTFunction((void*)fileEnter, typeFromClass(file_cls), 1))); file_cls->giveAttr("__exit__", new BoxedFunction(boxRTFunction((void*)fileExit, UNKNOWN, 4))); file_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)fileNew, UNKNOWN, 3, 1, false, false), { boxStrConstant("r") })); file_cls->freeze(); }
extern "C" BoxedString* functionRepr(BoxedFunction* v) { // TODO there has to be a better way if (v == repr_obj) return boxStrConstant("<built-in function repr>"); if (v == len_obj) return boxStrConstant("<built-in function len>"); if (v == hash_obj) return boxStrConstant("<built-in function hash>"); if (v == range_obj) return boxStrConstant("<built-in function range>"); if (v == abs_obj) return boxStrConstant("<built-in function abs>"); if (v == min_obj) return boxStrConstant("<built-in function min>"); if (v == max_obj) return boxStrConstant("<built-in function max>"); if (v == open_obj) return boxStrConstant("<built-in function open>"); if (v == chr_obj) return boxStrConstant("<built-in function chr>"); if (v == ord_obj) return boxStrConstant("<built-in function ord>"); return new BoxedString("function"); }
void setupFloat() { float_cls->giveAttr("__name__", boxStrConstant("float")); _addFunc("__add__", BOXED_FLOAT, (void*)floatAddFloat, (void*)floatAddInt, (void*)floatAdd); float_cls->giveAttr("__radd__", float_cls->getattr("__add__")); _addFunc("__div__", BOXED_FLOAT, (void*)floatDivFloat, (void*)floatDivInt, (void*)floatDiv); _addFunc("__rdiv__", BOXED_FLOAT, (void*)floatRDivFloat, (void*)floatRDivInt, (void*)floatRDiv); float_cls->giveAttr("__floordiv__", new BoxedFunction(boxRTFunction((void*)floatFloorDiv, UNKNOWN, 2))); _addFunc("__truediv__", BOXED_FLOAT, (void*)floatDivFloat, (void*)floatDivInt, (void*)floatTruediv); _addFunc("__eq__", BOXED_BOOL, (void*)floatEqFloat, (void*)floatEqInt, (void*)floatEq); _addFunc("__ge__", BOXED_BOOL, (void*)floatGeFloat, (void*)floatGeInt, (void*)floatGe); _addFunc("__gt__", BOXED_BOOL, (void*)floatGtFloat, (void*)floatGtInt, (void*)floatGt); _addFunc("__le__", BOXED_BOOL, (void*)floatLeFloat, (void*)floatLeInt, (void*)floatLe); _addFunc("__lt__", BOXED_BOOL, (void*)floatLtFloat, (void*)floatLtInt, (void*)floatLt); _addFunc("__ne__", BOXED_BOOL, (void*)floatNeFloat, (void*)floatNeInt, (void*)floatNe); _addFunc("__mod__", BOXED_FLOAT, (void*)floatModFloat, (void*)floatModInt, (void*)floatMod); _addFunc("__rmod__", BOXED_FLOAT, (void*)floatRModFloat, (void*)floatRModInt, (void*)floatRMod); _addFunc("__mul__", BOXED_FLOAT, (void*)floatMulFloat, (void*)floatMulInt, (void*)floatMul); float_cls->giveAttr("__rmul__", float_cls->getattr("__mul__")); _addFunc("__pow__", BOXED_FLOAT, (void*)floatPowFloat, (void*)floatPowInt, (void*)floatPow); _addFunc("__sub__", BOXED_FLOAT, (void*)floatSubFloat, (void*)floatSubInt, (void*)floatSub); _addFunc("__rsub__", BOXED_FLOAT, (void*)floatRSubFloat, (void*)floatRSubInt, (void*)floatRSub); float_cls->giveAttr( "__new__", new BoxedFunction(boxRTFunction((void*)floatNew, UNKNOWN, 2, 1, false, false), { boxFloat(0.0) })); float_cls->giveAttr("__neg__", new BoxedFunction(boxRTFunction((void*)floatNeg, BOXED_FLOAT, 1))); CLFunction* nonzero = boxRTFunction((void*)floatNonzeroUnboxed, BOOL, 1); addRTFunction(nonzero, (void*)floatNonzero, UNKNOWN); float_cls->giveAttr("__nonzero__", new BoxedFunction(nonzero)); // float_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)floatNonzero, NULL, 1))); float_cls->giveAttr("__str__", new BoxedFunction(boxRTFunction((void*)floatStr, STR, 1))); float_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)floatRepr, STR, 1))); float_cls->freeze(); }
void setupBool() { bool_cls->giveAttr("__name__", boxStrConstant("bool")); bool_cls->giveAttr("__invert__", new BoxedFunction(boxRTFunction((void*)boolInvert, BOXED_INT, 1))); bool_cls->giveAttr("__pos__", new BoxedFunction(boxRTFunction((void*)boolPos, BOXED_INT, 1))); bool_cls->giveAttr("__neg__", new BoxedFunction(boxRTFunction((void*)boolNeg, BOXED_INT, 1))); bool_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)boolNonzero, BOXED_BOOL, 1))); bool_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)boolRepr, STR, 1))); bool_cls->giveAttr("__str__", bool_cls->getattr("__repr__")); bool_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)boolNew, UNKNOWN, 2, 1, false, false), { None })); bool_cls->freeze(); True = new BoxedBool(true); False = new BoxedBool(false); gc::registerStaticRootObj(True); gc::registerStaticRootObj(False); }
void raiseExcHelper(BoxedClass* cls, const char* msg, ...) { if (msg != NULL) { va_list ap; va_start(ap, msg); // printf("Raising: "); // vprintf(msg, ap); // printf("\n"); // va_start(ap, msg); char buf[1024]; vsnprintf(buf, sizeof(buf), msg, ap); va_end(ap); BoxedString* message = boxStrConstant(buf); Box* exc_obj = runtimeCall(cls, ArgPassSpec(1), message, NULL, NULL, NULL, NULL); raiseExc(exc_obj); } else { Box* exc_obj = runtimeCall(cls, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL); raiseExc(exc_obj); } }
void setupDict() { dict_cls->giveAttr("__name__", boxStrConstant("dict")); // dict_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)dictLen, NULL, 1, false))); // dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, NULL, 2, false))); // dict_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)dictNew, NULL, 1, false))); // dict_cls->giveAttr("__init__", new BoxedFunction(boxRTFunction((void*)dictInit, NULL, 1, false))); dict_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)dictRepr, NULL, 1, false))); dict_cls->setattr("__str__", dict_cls->peekattr("__repr__"), NULL, NULL); dict_cls->giveAttr("items", new BoxedFunction(boxRTFunction((void*)dictItems, NULL, 1, false))); dict_cls->setattr("iteritems", dict_cls->peekattr("items"), NULL, NULL); dict_cls->giveAttr("values", new BoxedFunction(boxRTFunction((void*)dictValues, NULL, 1, false))); dict_cls->setattr("itervalues", dict_cls->peekattr("values"), NULL, NULL); dict_cls->giveAttr("keys", new BoxedFunction(boxRTFunction((void*)dictKeys, NULL, 1, false))); dict_cls->setattr("iterkeys", dict_cls->peekattr("keys"), NULL, NULL); dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, NULL, 2, false))); dict_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)dictSetitem, NULL, 3, false))); dict_cls->freeze(); }
void setupBool() { bool_cls->giveAttr("__name__", boxStrConstant("bool")); bool_cls->giveAttr("__invert__", new BoxedFunction(boxRTFunction((void*)boolInvert, NULL, 1, false))); bool_cls->giveAttr("__pos__", new BoxedFunction(boxRTFunction((void*)boolPos, NULL, 1, false))); bool_cls->giveAttr("__neg__", new BoxedFunction(boxRTFunction((void*)boolNeg, NULL, 1, false))); bool_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)boolNonzero, NULL, 1, false))); bool_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)boolRepr, NULL, 1, false))); bool_cls->setattr("__str__", bool_cls->peekattr("__repr__"), NULL, NULL); CLFunction *__new__ = boxRTFunction((void*)boolNew1, NULL, 1, false); addRTFunction(__new__, (void*)boolNew2, NULL, 2, false); bool_cls->giveAttr("__new__", new BoxedFunction(__new__)); bool_cls->freeze(); True = new BoxedBool(true); False = new BoxedBool(false); gc::registerStaticRootObj(True); gc::registerStaticRootObj(False); }
void setupRuntime() { HiddenClass::getRoot(); object_cls = new BoxedClass(NULL, 0, sizeof(Box), false); type_cls = new BoxedClass(object_cls, offsetof(BoxedClass, attrs), sizeof(BoxedClass), false); type_cls->cls = type_cls; object_cls->cls = type_cls; none_cls = new BoxedClass(object_cls, 0, sizeof(Box), false); None = new Box(&none_flavor, none_cls); str_cls = new BoxedClass(object_cls, 0, sizeof(BoxedString), false); // It wasn't safe to add __base__ attributes until object+type+str are set up, so do that now: type_cls->giveAttr("__base__", object_cls); str_cls->giveAttr("__base__", object_cls); none_cls->giveAttr("__base__", object_cls); object_cls->giveAttr("__base__", None); tuple_cls = new BoxedClass(object_cls, 0, sizeof(BoxedTuple), false); EmptyTuple = new BoxedTuple({}); gc::registerStaticRootObj(EmptyTuple); module_cls = new BoxedClass(object_cls, offsetof(BoxedModule, attrs), sizeof(BoxedModule), false); // TODO it'd be nice to be able to do these in the respective setupType methods, // but those setup methods probably want access to these objects. // We could have a multi-stage setup process, but that seems overkill for now. bool_cls = new BoxedClass(object_cls, 0, sizeof(BoxedBool), false); int_cls = new BoxedClass(object_cls, 0, sizeof(BoxedInt), false); float_cls = new BoxedClass(object_cls, 0, sizeof(BoxedFloat), false); function_cls = new BoxedClass(object_cls, offsetof(BoxedFunction, attrs), sizeof(BoxedFunction), false); instancemethod_cls = new BoxedClass(object_cls, 0, sizeof(BoxedInstanceMethod), false); list_cls = new BoxedClass(object_cls, 0, sizeof(BoxedList), false); slice_cls = new BoxedClass(object_cls, 0, sizeof(BoxedSlice), false); dict_cls = new BoxedClass(object_cls, 0, sizeof(BoxedDict), false); file_cls = new BoxedClass(object_cls, 0, sizeof(BoxedFile), false); set_cls = new BoxedClass(object_cls, 0, sizeof(BoxedSet), false); member_cls = new BoxedClass(object_cls, 0, sizeof(BoxedMemberDescriptor), false); STR = typeFromClass(str_cls); BOXED_INT = typeFromClass(int_cls); BOXED_FLOAT = typeFromClass(float_cls); BOXED_BOOL = typeFromClass(bool_cls); NONE = typeFromClass(none_cls); LIST = typeFromClass(list_cls); SLICE = typeFromClass(slice_cls); MODULE = typeFromClass(module_cls); DICT = typeFromClass(dict_cls); SET = typeFromClass(set_cls); BOXED_TUPLE = typeFromClass(tuple_cls); object_cls->giveAttr("__name__", boxStrConstant("object")); object_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)objectNew, UNKNOWN, 1, 0, true, false))); object_cls->freeze(); auto typeCallObj = boxRTFunction((void*)typeCall, UNKNOWN, 1, 0, true, false); typeCallObj->internal_callable = &typeCallInternal; type_cls->giveAttr("__call__", new BoxedFunction(typeCallObj)); type_cls->giveAttr("__name__", boxStrConstant("type")); type_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)typeNew, UNKNOWN, 2))); type_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)typeRepr, STR, 1))); type_cls->giveAttr("__str__", type_cls->getattr("__repr__")); type_cls->freeze(); none_cls->giveAttr("__name__", boxStrConstant("NoneType")); none_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)noneRepr, STR, 1))); none_cls->giveAttr("__str__", none_cls->getattr("__repr__")); none_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)noneHash, UNKNOWN, 1))); none_cls->freeze(); module_cls->giveAttr("__name__", boxStrConstant("module")); module_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)moduleRepr, STR, 1))); module_cls->giveAttr("__str__", module_cls->getattr("__repr__")); module_cls->freeze(); member_cls->giveAttr("__name__", boxStrConstant("member")); member_cls->freeze(); setupBool(); setupInt(); setupFloat(); setupStr(); setupList(); setupDict(); setupSet(); setupTuple(); setupFile(); function_cls->giveAttr("__name__", boxStrConstant("function")); function_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)functionRepr, STR, 1))); function_cls->giveAttr("__str__", function_cls->getattr("__repr__")); function_cls->freeze(); instancemethod_cls->giveAttr("__name__", boxStrConstant("instancemethod")); instancemethod_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)instancemethodRepr, STR, 1))); instancemethod_cls->freeze(); slice_cls->giveAttr("__name__", boxStrConstant("slice")); slice_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)sliceNew, UNKNOWN, 4, 2, false, false), { NULL, None })); slice_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)sliceRepr, STR, 1))); slice_cls->giveAttr("__str__", slice_cls->getattr("__repr__")); slice_cls->giveAttr("start", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, SLICE_START_OFFSET)); slice_cls->giveAttr("stop", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, SLICE_STOP_OFFSET)); slice_cls->giveAttr("step", new BoxedMemberDescriptor(BoxedMemberDescriptor::OBJECT, SLICE_STEP_OFFSET)); slice_cls->freeze(); // sys is the first module that needs to be set up, due to modules // being tracked in sys.modules: setupSys(); setupBuiltins(); setupMath(); setupTime(); setupThread(); setupCAPI(); TRACK_ALLOCATIONS = true; }
Box* instancemethodRepr(BoxedInstanceMethod* self) { return boxStrConstant("<bound instancemethod object>"); }
void addToSysArgv(const char* str) { Box* sys_argv = sys_module->getattr("argv"); assert(sys_argv); assert(sys_argv->cls == list_cls); listAppendInternal(sys_argv, boxStrConstant(str)); }
void setupList() { list_iterator_cls = new BoxedHeapClass(object_cls, &listIteratorGCHandler, 0, sizeof(BoxedList), false); list_cls->giveAttr("__name__", boxStrConstant("list")); list_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)listLen, BOXED_INT, 1))); CLFunction* getitem = createRTFunction(2, 0, 0, 0); addRTFunction(getitem, (void*)listGetitemInt, UNKNOWN, std::vector<ConcreteCompilerType*>{ LIST, BOXED_INT }); addRTFunction(getitem, (void*)listGetitemSlice, LIST, std::vector<ConcreteCompilerType*>{ LIST, SLICE }); addRTFunction(getitem, (void*)listGetitem, UNKNOWN, std::vector<ConcreteCompilerType*>{ LIST, UNKNOWN }); list_cls->giveAttr("__getitem__", new BoxedFunction(getitem)); list_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)listIter, typeFromClass(list_iterator_cls), 1))); list_cls->giveAttr("__eq__", new BoxedFunction(boxRTFunction((void*)listEq, UNKNOWN, 2))); list_cls->giveAttr("__ne__", new BoxedFunction(boxRTFunction((void*)listNe, UNKNOWN, 2))); list_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)listRepr, STR, 1))); list_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)listNonzero, BOXED_BOOL, 1))); list_cls->giveAttr("pop", new BoxedFunction(boxRTFunction((void*)listPop, UNKNOWN, 2, 1, false, false), { None })); list_cls->giveAttr("append", new BoxedFunction(boxRTFunction((void*)listAppend, NONE, 2))); list_cls->giveAttr("extend", new BoxedFunction(boxRTFunction((void*)listIAdd, NONE, 2))); CLFunction* setitem = createRTFunction(3, 0, false, false); addRTFunction(setitem, (void*)listSetitemInt, NONE, std::vector<ConcreteCompilerType*>{ LIST, BOXED_INT, UNKNOWN }); addRTFunction(setitem, (void*)listSetitemSlice, NONE, std::vector<ConcreteCompilerType*>{ LIST, SLICE, UNKNOWN }); addRTFunction(setitem, (void*)listSetitem, NONE, std::vector<ConcreteCompilerType*>{ LIST, UNKNOWN, UNKNOWN }); list_cls->giveAttr("__setitem__", new BoxedFunction(setitem)); CLFunction* delitem = createRTFunction(2, 0, false, false); addRTFunction(delitem, (void*)listDelitemInt, NONE, std::vector<ConcreteCompilerType*>{ LIST, BOXED_INT }); addRTFunction(delitem, (void*)listDelitemSlice, NONE, std::vector<ConcreteCompilerType*>{ LIST, SLICE }); addRTFunction(delitem, (void*)listDelitem, NONE, std::vector<ConcreteCompilerType*>{ LIST, UNKNOWN }); list_cls->giveAttr("__delitem__", new BoxedFunction(delitem)); list_cls->giveAttr("insert", new BoxedFunction(boxRTFunction((void*)listInsert, NONE, 3))); list_cls->giveAttr("__mul__", new BoxedFunction(boxRTFunction((void*)listMul, LIST, 2))); list_cls->giveAttr("__rmul__", new BoxedFunction(boxRTFunction((void*)listMul, LIST, 2))); list_cls->giveAttr("__iadd__", new BoxedFunction(boxRTFunction((void*)listIAdd, UNKNOWN, 2))); list_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)listAdd, UNKNOWN, 2))); list_cls->giveAttr("sort", new BoxedFunction(boxRTFunction((void*)listSort1, NONE, 1))); list_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)listContains, BOXED_BOOL, 2))); list_cls->giveAttr("__new__", new BoxedFunction(boxRTFunction((void*)listNew, UNKNOWN, 2, 1, false, false), { None })); list_cls->giveAttr("count", new BoxedFunction(boxRTFunction((void*)listCount, BOXED_INT, 2))); list_cls->giveAttr("index", new BoxedFunction(boxRTFunction((void*)listIndex, BOXED_INT, 2))); list_cls->giveAttr("remove", new BoxedFunction(boxRTFunction((void*)listRemove, NONE, 2))); list_cls->giveAttr("reverse", new BoxedFunction(boxRTFunction((void*)listReverse, NONE, 1))); list_cls->freeze(); list_iterator_cls->giveAttr("__name__", boxStrConstant("listiterator")); CLFunction* hasnext = boxRTFunction((void*)listiterHasnextUnboxed, BOOL, 1); addRTFunction(hasnext, (void*)listiterHasnext, BOXED_BOOL); list_iterator_cls->giveAttr("__hasnext__", new BoxedFunction(hasnext)); list_iterator_cls->giveAttr( "__iter__", new BoxedFunction(boxRTFunction((void*)listIterIter, typeFromClass(list_iterator_cls), 1))); list_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)listiterNext, UNKNOWN, 1))); list_iterator_cls->freeze(); }
Box* sysGetFilesystemEncoding() { if (Py_FileSystemDefaultEncoding) return boxStrConstant(Py_FileSystemDefaultEncoding); return None; }
extern "C" Box* boolRepr(BoxedBool* v) { if (v->b) return boxStrConstant("True"); return boxStrConstant("False"); }
void setupUnicode() { unicode_cls->giveAttr("__name__", boxStrConstant("unicode")); unicode_cls->freeze(); }
extern "C" Box* open1(Box* arg) { Box* mode = boxStrConstant("r"); Box *rtn = open2(arg, mode); return rtn; }
Box* sysGetDefaultEncoding() { return boxStrConstant(PyUnicode_GetDefaultEncoding()); }