| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000 |
- #pragma once
- #include "codeobject.h"
- #include "iter.h"
- #include "error.h"
- #define __DEF_PY_AS_C(type, ctype, ptype) \
- inline ctype& Py##type##_AS_C(const PyVar& obj) { \
- __checkType(obj, ptype); \
- return std::get<ctype>(obj->_native); \
- }
- #define __DEF_PY(type, ctype, ptype) \
- inline PyVar Py##type(ctype value) { \
- return newObject(ptype, value); \
- }
- #define DEF_NATIVE(type, ctype, ptype) \
- __DEF_PY(type, ctype, ptype) \
- __DEF_PY_AS_C(type, ctype, ptype)
- #define __DEF_PY_POOL(name, ctype, ptype, max_size) \
- std::vector<PyObject*> _pool##name; \
- PyVar Py##name(ctype _native) { \
- PyObject* _raw = nullptr; \
- if(_pool##name.size() > 0) { \
- _raw = _pool##name.back(); \
- _raw->_native = std::move(_native); \
- _pool##name.pop_back(); \
- }else{ \
- __checkType(ptype, _tp_type); \
- _raw = new PyObject(std::move(_native));\
- _raw->setType(ptype); \
- } \
- return PyVar(_raw, [this](PyObject* p){ \
- if(_pool##name.size() < max_size){ \
- _pool##name.push_back(p); \
- }else{ \
- delete p; \
- } \
- }); \
- }
- typedef void(*PrintFn)(const VM*, const char*);
- class VM: public PkExportedResource{
- private:
- std::stack< std::unique_ptr<Frame> > callstack;
- PyVarDict _modules; // 3rd modules
- PyVar __py2py_call_signal;
- PyVar runFrame(Frame* frame){
- while(!frame->isCodeEnd()){
- const ByteCode& byte = frame->readCode();
- //printf("%s (%d) stack_size: %d\n", OP_NAMES[byte.op], byte.arg, frame->stackSize());
- switch (byte.op)
- {
- case OP_NO_OP: break; // do nothing
- case OP_LOAD_CONST: frame->push(frame->code->co_consts[byte.arg]); break;
- case OP_LOAD_LAMBDA: {
- PyVar obj = frame->code->co_consts[byte.arg];
- setAttr(obj, __module__, frame->_module);
- frame->push(obj);
- } break;
- case OP_LOAD_NAME_PTR: {
- frame->push(PyPointer(frame->code->co_names[byte.arg]));
- } break;
- case OP_STORE_NAME_PTR: {
- const auto& p = frame->code->co_names[byte.arg];
- p->set(this, frame, frame->popValue(this));
- } break;
- case OP_BUILD_ATTR_PTR: {
- const auto& attr = frame->code->co_names[byte.arg];
- PyVar obj = frame->popValue(this);
- frame->push(PyPointer(std::make_shared<AttrPointer>(obj, attr.get())));
- } break;
- case OP_BUILD_INDEX_PTR: {
- PyVar index = frame->popValue(this);
- PyVar obj = frame->popValue(this);
- frame->push(PyPointer(std::make_shared<IndexPointer>(obj, index)));
- } break;
- case OP_STORE_PTR: {
- PyVar obj = frame->popValue(this);
- const _Pointer& p = PyPointer_AS_C(frame->__pop());
- p->set(this, frame, std::move(obj));
- } break;
- case OP_DELETE_PTR: {
- const _Pointer& p = PyPointer_AS_C(frame->__pop());
- p->del(this, frame);
- } break;
- case OP_BUILD_SMART_TUPLE:
- {
- PyVarList items = frame->__popNReversed(byte.arg);
- bool done = false;
- for(auto& item : items){
- if(!item->isType(_tp_pointer)) {
- done = true;
- PyVarList values(items.size());
- for(int i=0; i<items.size(); i++){
- values[i] = frame->__deref_pointer(this, items[i]);
- }
- frame->push(PyTuple(values));
- break;
- }
- }
- if(done) break;
- std::vector<_Pointer> pointers(items.size());
- for(int i=0; i<items.size(); i++)
- pointers[i] = PyPointer_AS_C(items[i]);
- frame->push(PyPointer(std::make_shared<CompoundPointer>(pointers)));
- } break;
- case OP_BUILD_STRING:
- {
- PyVarList items = frame->popNValuesReversed(this, byte.arg);
- _StrStream ss;
- for(const auto& i : items) ss << PyStr_AS_C(asStr(i));
- frame->push(PyStr(ss.str()));
- } break;
- case OP_LOAD_EVAL_FN: {
- frame->push(builtins->attribs["eval"]);
- } break;
- case OP_LIST_APPEND: {
- PyVar obj = frame->popValue(this);
- PyVar list = frame->__topValueN(this, -2);
- fastCall(list, "append", {list, obj});
- } break;
- case OP_STORE_FUNCTION:
- {
- PyVar obj = frame->popValue(this);
- const _Func& fn = PyFunction_AS_C(obj);
- setAttr(obj, __module__, frame->_module);
- frame->f_globals()[fn->name] = obj;
- } break;
- case OP_BUILD_CLASS:
- {
- const _Str& clsName = frame->code->co_names[byte.arg]->name;
- PyVar clsBase = frame->popValue(this);
- if(clsBase == None) clsBase = _tp_object;
- __checkType(clsBase, _tp_type);
- PyVar cls = newUserClassType(clsName, clsBase);
- while(true){
- PyVar fn = frame->popValue(this);
- if(fn == None) break;
- const _Func& f = PyFunction_AS_C(fn);
- setAttr(fn, __module__, frame->_module);
- setAttr(cls, f->name, fn);
- }
- frame->f_globals()[clsName] = cls;
- } break;
- case OP_RETURN_VALUE: return frame->popValue(this);
- case OP_PRINT_EXPR:
- {
- const PyVar& expr = frame->topValue(this);
- if(expr == None) break;
- *_stdout << PyStr_AS_C(asRepr(expr)) << '\n';
- } break;
- case OP_POP_TOP: frame->popValue(this); break;
- case OP_BINARY_OP:
- {
- pkpy::ArgList args(2);
- args[1] = frame->popValue(this);
- args[0] = frame->popValue(this);
- frame->push(fastCall(args[0], BINARY_SPECIAL_METHODS[byte.arg], std::move(args)));
- } break;
- case OP_BITWISE_OP:
- {
- pkpy::ArgList args(2);
- args[1] = frame->popValue(this);
- args[0] = frame->popValue(this);
- frame->push(fastCall(args[0], BITWISE_SPECIAL_METHODS[byte.arg], std::move(args)));
- } break;
- case OP_COMPARE_OP:
- {
- pkpy::ArgList args(2);
- args[1] = frame->popValue(this);
- args[0] = frame->popValue(this);
- // for __ne__ we use the negation of __eq__
- int op = byte.arg == 3 ? 2 : byte.arg;
- PyVar res = fastCall(args[0], CMP_SPECIAL_METHODS[op], std::move(args));
- if(op != byte.arg) res = PyBool(!PyBool_AS_C(res));
- frame->push(std::move(res));
- } break;
- case OP_IS_OP:
- {
- bool ret_c = frame->popValue(this) == frame->popValue(this);
- if(byte.arg == 1) ret_c = !ret_c;
- frame->push(PyBool(ret_c));
- } break;
- case OP_CONTAINS_OP:
- {
- PyVar rhs = frame->popValue(this);
- PyVar lhs = frame->popValue(this);
- bool ret_c = PyBool_AS_C(call(std::move(rhs), __contains__, {std::move(lhs)}));
- if(byte.arg == 1) ret_c = !ret_c;
- frame->push(PyBool(ret_c));
- } break;
- case OP_UNARY_NEGATIVE:
- {
- PyVar obj = frame->popValue(this);
- frame->push(numNegated(obj));
- } break;
- case OP_UNARY_NOT:
- {
- PyVar obj = frame->popValue(this);
- const PyVar& obj_bool = asBool(obj);
- frame->push(PyBool(!PyBool_AS_C(obj_bool)));
- } break;
- case OP_POP_JUMP_IF_FALSE:
- if(!PyBool_AS_C(asBool(frame->popValue(this)))) frame->jump(byte.arg);
- break;
- case OP_LOAD_NONE: frame->push(None); break;
- case OP_LOAD_TRUE: frame->push(True); break;
- case OP_LOAD_FALSE: frame->push(False); break;
- case OP_LOAD_ELLIPSIS: frame->push(Ellipsis); break;
- case OP_ASSERT:
- {
- PyVar expr = frame->popValue(this);
- _assert(PyBool_AS_C(expr), "assertion failed");
- } break;
- case OP_RAISE_ERROR:
- {
- _Str msg = PyStr_AS_C(asRepr(frame->popValue(this)));
- _Str type = PyStr_AS_C(frame->popValue(this));
- _error(type, msg);
- } break;
- case OP_BUILD_LIST:
- {
- PyVarList items = frame->popNValuesReversed(this, byte.arg);
- frame->push(PyList(items));
- } break;
- case OP_BUILD_MAP:
- {
- PyVarList items = frame->popNValuesReversed(this, byte.arg*2);
- PyVar obj = call(builtins->attribs["dict"], {});
- for(int i=0; i<items.size(); i+=2){
- call(obj, __setitem__, {items[i], items[i+1]});
- }
- frame->push(obj);
- } break;
- case OP_DUP_TOP: frame->push(frame->topValue(this)); break;
- case OP_CALL:
- {
- PyVarList args = frame->popNValuesReversed(this, byte.arg);
- PyVar callable = frame->popValue(this);
- PyVar ret = call(std::move(callable), std::move(args), true);
- if(ret == __py2py_call_signal) return ret;
- frame->push(std::move(ret));
- } break;
- case OP_JUMP_ABSOLUTE: frame->jump(byte.arg); break;
- case OP_SAFE_JUMP_ABSOLUTE: frame->safeJump(byte.arg); break;
- case OP_GOTO: {
- PyVar obj = frame->popValue(this);
- const _Str& label = PyStr_AS_C(obj);
- auto it = frame->code->co_labels.find(label);
- if(it == frame->code->co_labels.end()){
- _error("KeyError", "label '" + label + "' not found");
- }
- frame->safeJump(it->second);
- } break;
- case OP_GET_ITER:
- {
- PyVar obj = frame->popValue(this);
- PyVarOrNull iter_fn = getAttr(obj, __iter__, false);
- if(iter_fn != nullptr){
- PyVar tmp = call(iter_fn, {obj});
- PyIter_AS_C(tmp)->var = std::move(PyPointer_AS_C(frame->__pop()));
- frame->push(std::move(tmp));
- }else{
- typeError("'" + obj->getTypeName() + "' object is not iterable");
- }
- } break;
- case OP_FOR_ITER:
- {
- frame->__reportForIter();
- // __top() must be PyIter, so no need to __deref()
- auto& it = PyIter_AS_C(frame->__top());
- if(it->hasNext()){
- it->var->set(this, frame, it->next());
- }
- else{
- frame->safeJump(byte.arg);
- }
- } break;
- case OP_JUMP_IF_FALSE_OR_POP:
- {
- const PyVar& expr = frame->topValue(this);
- if(asBool(expr)==False) frame->jump(byte.arg);
- else frame->popValue(this);
- } break;
- case OP_JUMP_IF_TRUE_OR_POP:
- {
- const PyVar& expr = frame->topValue(this);
- if(asBool(expr)==True) frame->jump(byte.arg);
- else frame->popValue(this);
- } break;
- case OP_BUILD_SLICE:
- {
- PyVar stop = frame->popValue(this);
- PyVar start = frame->popValue(this);
- _Slice s;
- if(start != None) {__checkType(start, _tp_int); s.start = PyInt_AS_C(start);}
- if(stop != None) {__checkType(stop, _tp_int); s.stop = PyInt_AS_C(stop);}
- frame->push(PySlice(s));
- } break;
- case OP_IMPORT_NAME:
- {
- const _Str& name = frame->code->co_names[byte.arg]->name;
- auto it = _modules.find(name);
- if(it == _modules.end()) _error("ImportError", "module '" + name + "' not found");
- else frame->push(it->second);
- } break;
- default:
- systemError(_Str("opcode ") + OP_NAMES[byte.op] + " is not implemented");
- break;
- }
- }
- if(frame->code->src->mode == EVAL_MODE) {
- if(frame->stackSize() != 1) systemError("stack size is not 1 in EVAL_MODE");
- return frame->popValue(this);
- }
- if(frame->stackSize() != 0) systemError("stack not empty in EXEC_MODE");
- return None;
- }
- public:
- PyVarDict _types;
- PyVar None, True, False, Ellipsis;
- bool use_stdio;
- std::ostream* _stdout;
- std::ostream* _stderr;
-
- PyVar builtins; // builtins module
- PyVar _main; // __main__ module
- int maxRecursionDepth = 1000;
- VM(bool use_stdio){
- this->use_stdio = use_stdio;
- if(use_stdio){
- std::cout.setf(std::ios::unitbuf);
- std::cerr.setf(std::ios::unitbuf);
- this->_stdout = &std::cout;
- this->_stderr = &std::cerr;
- }else{
- this->_stdout = new _StrStream();
- this->_stderr = new _StrStream();
- }
- initializeBuiltinClasses();
- }
- PyVar asStr(const PyVar& obj){
- PyVarOrNull str_fn = getAttr(obj, __str__, false);
- if(str_fn != nullptr) return call(str_fn, {});
- return asRepr(obj);
- }
- Frame* topFrame(){
- if(callstack.size() == 0) UNREACHABLE();
- return callstack.top().get();
- }
- PyVar asRepr(const PyVar& obj){
- if(obj->isType(_tp_type)) return PyStr("<class '" + obj->getName() + "'>");
- return call(obj, __repr__, {});
- }
- PyVar asJson(const PyVar& obj){
- return call(obj, __json__, {});
- }
- const PyVar& asBool(const PyVar& obj){
- if(obj == None) return False;
- if(obj->_type == _tp_bool) return obj;
- if(obj->_type == _tp_int) return PyBool(PyInt_AS_C(obj) != 0);
- if(obj->_type == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0);
- PyVarOrNull len_fn = getAttr(obj, __len__, false);
- if(len_fn != nullptr){
- PyVar ret = call(std::move(len_fn), {});
- return PyBool(PyInt_AS_C(ret) > 0);
- }
- return True;
- }
- PyVar fastCall(const PyVar& obj, const _Str& name, pkpy::ArgList&& args){
- PyObject* cls = obj->_type.get();
- while(cls != None.get()) {
- auto it = cls->attribs.find(name);
- if(it != cls->attribs.end()){
- return call(it->second, args);
- }
- cls = cls->attribs[__base__].get();
- }
- attributeError(obj, name);
- return nullptr;
- }
- PyVar call(const PyVar& _callable, pkpy::ArgList args, bool opCall=false){
- if(_callable->isType(_tp_type)){
- auto it = _callable->attribs.find(__new__);
- PyVar obj;
- if(it != _callable->attribs.end()){
- obj = call(it->second, args);
- }else{
- obj = newObject(_callable, (_Int)-1);
- }
- if(obj->isType(_callable)){
- PyVarOrNull init_fn = getAttr(obj, __init__, false);
- if (init_fn != nullptr) call(init_fn, args);
- }
- return obj;
- }
- const PyVar* callable = &_callable;
- if((*callable)->isType(_tp_bounded_method)){
- auto& bm = PyBoundedMethod_AS_C((*callable));
- // TODO: avoid insertion here, bad performance
- pkpy::ArgList new_args(args.size()+1);
- new_args[0] = bm.obj;
- for(int i=0; i<args.size(); i++) new_args[i+1] = args[i];
- callable = &bm.method;
- args = std::move(new_args);
- }
-
- if((*callable)->isType(_tp_native_function)){
- const auto& f = std::get<_CppFunc>((*callable)->_native);
- return f(this, args);
- } else if((*callable)->isType(_tp_function)){
- const _Func& fn = PyFunction_AS_C((*callable));
- PyVarDict locals;
- int i = 0;
- for(const auto& name : fn->args){
- if(i < args.size()) {
- locals[name] = args[i++];
- }else{
- typeError("missing positional argument '" + name + "'");
- }
- }
- // handle *args
- if(!fn->starredArg.empty()){
- PyVarList vargs;
- while(i < args.size()) vargs.push_back(args[i++]);
- locals[fn->starredArg] = PyTuple(vargs);
- }
- // handle keyword arguments
- for(const _Str& name : fn->kwArgsOrder){
- if(i < args.size()) {
- locals[name] = args[i++];
- }else{
- locals[name] = fn->kwArgs[name];
- }
- }
- if(i < args.size()) typeError("too many arguments");
- auto it_m = (*callable)->attribs.find(__module__);
- PyVar _module = it_m != (*callable)->attribs.end() ? it_m->second : topFrame()->_module;
- if(opCall){
- __pushNewFrame(fn->code, _module, locals);
- return __py2py_call_signal;
- }
- return _exec(fn->code, _module, locals);
- }
- typeError("'" + (*callable)->getTypeName() + "' object is not callable");
- return None;
- }
- inline PyVar call(const PyVar& obj, const _Str& func, const pkpy::ArgList& args){
- return call(getAttr(obj, func), args);
- }
- inline PyVar call(const PyVar& obj, const _Str& func, pkpy::ArgList&& args){
- return call(getAttr(obj, func), args);
- }
- PyVarOrNull exec(const _Code& code, PyVar _module=nullptr){
- if(_module == nullptr) _module = _main;
- try {
- return _exec(code, _module);
- } catch (const std::exception& e) {
- if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
- *_stderr << e.what() << '\n';
- }else{
- auto re = RuntimeError("UnexpectedError", e.what(), _cleanErrorAndGetSnapshots());
- *_stderr << re.what() << '\n';
- }
- return nullptr;
- }
- }
- Frame* __pushNewFrame(const _Code& code, PyVar _module, const PyVarDict& locals){
- if(code == nullptr) UNREACHABLE();
- if(callstack.size() > maxRecursionDepth){
- throw RuntimeError("RecursionError", "maximum recursion depth exceeded", _cleanErrorAndGetSnapshots());
- }
- Frame* frame = new Frame(code.get(), _module, locals);
- callstack.push(std::unique_ptr<Frame>(frame));
- return frame;
- }
- PyVar _exec(const _Code& code, PyVar _module, const PyVarDict& locals={}){
- Frame* frame = __pushNewFrame(code, _module, locals);
- Frame* frameBase = frame;
- PyVar ret = nullptr;
- while(true){
- ret = runFrame(frame);
- if(ret != __py2py_call_signal){
- if(frame == frameBase){ // [ frameBase<- ]
- break;
- }else{
- callstack.pop();
- frame = callstack.top().get();
- frame->push(ret);
- }
- }else{
- frame = callstack.top().get(); // [ frameBase, newFrame<- ]
- }
- }
- callstack.pop();
- return ret;
- }
- PyVar newUserClassType(_Str name, PyVar base){
- PyVar obj = newClassType(name, base);
- setAttr(obj, __name__, PyStr(name));
- _types.erase(name);
- return obj;
- }
- PyVar newClassType(_Str name, PyVar base=nullptr) {
- if(base == nullptr) base = _tp_object;
- PyVar obj = std::make_shared<PyObject>((_Int)0);
- obj->setType(_tp_type);
- setAttr(obj, __base__, base);
- _types[name] = obj;
- return obj;
- }
- PyVar newObject(PyVar type, _Value _native) {
- __checkType(type, _tp_type);
- PyVar obj = std::make_shared<PyObject>(_native);
- obj->setType(type);
- return obj;
- }
- PyVar newModule(_Str name, bool saveToPath=true) {
- PyVar obj = newObject(_tp_module, (_Int)-2);
- setAttr(obj, __name__, PyStr(name));
- if(saveToPath) _modules[name] = obj;
- return obj;
- }
- PyVarOrNull getAttr(const PyVar& obj, const _Str& name, bool throw_err=true) {
- auto it = obj->attribs.find(name);
- if(it != obj->attribs.end()) return it->second;
- PyObject* cls = obj->_type.get();
- while(cls != None.get()) {
- it = cls->attribs.find(name);
- if(it != cls->attribs.end()){
- PyVar valueFromCls = it->second;
- if(valueFromCls->isType(_tp_function) || valueFromCls->isType(_tp_native_function)){
- return PyBoundedMethod({obj, valueFromCls});
- }else{
- return valueFromCls;
- }
- }
- cls = cls->attribs[__base__].get();
- }
- if(throw_err) attributeError(obj, name);
- return nullptr;
- }
- inline void setAttr(PyVar& obj, const _Str& name, const PyVar& value) {
- obj->attribs[name] = value;
- }
- inline void setAttr(PyVar& obj, const _Str& name, PyVar&& value) {
- obj->attribs[name] = std::move(value);
- }
- void bindMethod(_Str typeName, _Str funcName, _CppFunc fn) {
- funcName.intern();
- PyVar type = _types[typeName];
- PyVar func = PyNativeFunction(fn);
- setAttr(type, funcName, func);
- }
- void bindMethodMulti(std::vector<_Str> typeNames, _Str funcName, _CppFunc fn) {
- for(auto& typeName : typeNames){
- bindMethod(typeName, funcName, fn);
- }
- }
- void bindBuiltinFunc(_Str funcName, _CppFunc fn) {
- bindFunc(builtins, funcName, fn);
- }
- void bindFunc(PyVar module, _Str funcName, _CppFunc fn) {
- funcName.intern();
- __checkType(module, _tp_module);
- PyVar func = PyNativeFunction(fn);
- setAttr(module, funcName, func);
- }
- bool isInstance(PyVar obj, PyVar type){
- __checkType(type, _tp_type);
- PyVar t = obj->_type;
- while (t != None){
- if (t == type) return true;
- t = t->attribs[__base__];
- }
- return false;
- }
- inline bool isIntOrFloat(const PyVar& obj){
- return obj->isType(_tp_int) || obj->isType(_tp_float);
- }
- inline bool isIntOrFloat(const PyVar& obj1, const PyVar& obj2){
- return isIntOrFloat(obj1) && isIntOrFloat(obj2);
- }
- _Float numToFloat(const PyVar& obj){
- if (obj->isType(_tp_int)){
- return (_Float)PyInt_AS_C(obj);
- }else if(obj->isType(_tp_float)){
- return PyFloat_AS_C(obj);
- }
- UNREACHABLE();
- }
- PyVar numNegated(const PyVar& obj){
- if (obj->isType(_tp_int)){
- return PyInt(-PyInt_AS_C(obj));
- }else if(obj->isType(_tp_float)){
- return PyFloat(-PyFloat_AS_C(obj));
- }
- typeError("unsupported operand type(s) for -");
- return nullptr;
- }
- int normalizedIndex(int index, int size){
- if(index < 0) index += size;
- if(index < 0 || index >= size){
- indexError("index out of range, " + std::to_string(index) + " not in [0, " + std::to_string(size) + ")");
- }
- return index;
- }
- // for quick access
- PyVar _tp_object, _tp_type, _tp_int, _tp_float, _tp_bool, _tp_str;
- PyVar _tp_list, _tp_tuple;
- PyVar _tp_function, _tp_native_function, _tp_native_iterator, _tp_bounded_method;
- PyVar _tp_slice, _tp_range, _tp_module, _tp_pointer;
- __DEF_PY_POOL(Int, _Int, _tp_int, 256);
- __DEF_PY_AS_C(Int, _Int, _tp_int)
- __DEF_PY_POOL(Float, _Float, _tp_float, 256);
- __DEF_PY_AS_C(Float, _Float, _tp_float)
- __DEF_PY_POOL(Pointer, _Pointer, _tp_pointer, 512)
- __DEF_PY_AS_C(Pointer, _Pointer, _tp_pointer)
- DEF_NATIVE(Str, _Str, _tp_str)
- DEF_NATIVE(List, PyVarList, _tp_list)
- DEF_NATIVE(Tuple, PyVarList, _tp_tuple)
- DEF_NATIVE(Function, _Func, _tp_function)
- DEF_NATIVE(NativeFunction, _CppFunc, _tp_native_function)
- DEF_NATIVE(Iter, std::shared_ptr<_Iterator>, _tp_native_iterator)
- DEF_NATIVE(BoundedMethod, _BoundedMethod, _tp_bounded_method)
- DEF_NATIVE(Range, _Range, _tp_range)
- DEF_NATIVE(Slice, _Slice, _tp_slice)
-
- // there is only one True/False, so no need to copy them!
- inline bool PyBool_AS_C(const PyVar& obj){return obj == True;}
- inline const PyVar& PyBool(bool value){return value ? True : False;}
- void initializeBuiltinClasses(){
- _tp_object = std::make_shared<PyObject>((_Int)0);
- _tp_type = std::make_shared<PyObject>((_Int)0);
- _types["object"] = _tp_object;
- _types["type"] = _tp_type;
- _tp_bool = newClassType("bool");
- _tp_int = newClassType("int");
- _tp_float = newClassType("float");
- _tp_str = newClassType("str");
- _tp_list = newClassType("list");
- _tp_tuple = newClassType("tuple");
- _tp_slice = newClassType("slice");
- _tp_range = newClassType("range");
- _tp_module = newClassType("module");
- _tp_pointer = newClassType("_pointer");
- newClassType("NoneType");
- newClassType("ellipsis");
-
- _tp_function = newClassType("function");
- _tp_native_function = newClassType("_native_function");
- _tp_native_iterator = newClassType("_native_iterator");
- _tp_bounded_method = newClassType("_bounded_method");
- this->None = newObject(_types["NoneType"], (_Int)0);
- this->Ellipsis = newObject(_types["ellipsis"], (_Int)0);
- this->True = newObject(_tp_bool, true);
- this->False = newObject(_tp_bool, false);
- this->builtins = newModule("builtins");
- this->_main = newModule("__main__"_c, false);
- setAttr(_tp_type, __base__, _tp_object);
- _tp_type->setType(_tp_type);
- setAttr(_tp_object, __base__, None);
- _tp_object->setType(_tp_type);
-
- for (auto& [name, type] : _types) {
- setAttr(type, __name__, PyStr(name));
- }
- this->__py2py_call_signal = newObject(_tp_object, (_Int)7);
- std::vector<_Str> publicTypes = {"type", "object", "bool", "int", "float", "str", "list", "tuple", "range"};
- for (auto& name : publicTypes) {
- setAttr(builtins, name, _types[name]);
- }
- }
- _Int hash(const PyVar& obj){
- if (obj->isType(_tp_int)) return PyInt_AS_C(obj);
- if (obj->isType(_tp_bool)) return PyBool_AS_C(obj) ? 1 : 0;
- if (obj->isType(_tp_float)){
- _Float val = PyFloat_AS_C(obj);
- return (_Int)std::hash<_Float>()(val);
- }
- if (obj->isType(_tp_str)) return PyStr_AS_C(obj).hash();
- if (obj->isType(_tp_type)) return (_Int)obj.get();
- if (obj->isType(_tp_tuple)) {
- _Int x = 1000003;
- for (const auto& item : PyTuple_AS_C(obj)) {
- _Int y = hash(item);
- // this is recommended by Github Copilot
- // i am not sure whether it is a good idea
- x = x ^ (y + 0x9e3779b9 + (x << 6) + (x >> 2));
- }
- return x;
- }
- typeError("unhashable type: " + obj->getTypeName());
- return 0;
- }
- /***** Error Reporter *****/
- private:
- void _error(const _Str& name, const _Str& msg){
- throw RuntimeError(name, msg, _cleanErrorAndGetSnapshots());
- }
- std::stack<_Str> _cleanErrorAndGetSnapshots(){
- std::stack<_Str> snapshots;
- while (!callstack.empty()){
- if(snapshots.size() < 8){
- snapshots.push(callstack.top()->errorSnapshot());
- }
- callstack.pop();
- }
- return snapshots;
- }
- public:
- void typeError(const _Str& msg){
- _error("TypeError", msg);
- }
- void systemError(const _Str& msg){
- _error("SystemError", msg);
- }
- void zeroDivisionError(){
- _error("ZeroDivisionError", "division by zero");
- }
- void indexError(const _Str& msg){
- _error("IndexError", msg);
- }
- void valueError(const _Str& msg){
- _error("ValueError", msg);
- }
- void nameError(const _Str& name){
- _error("NameError", "name '" + name + "' is not defined");
- }
- void attributeError(PyVar obj, const _Str& name){
- _error("AttributeError", "type '" + obj->getTypeName() + "' has no attribute '" + name + "'");
- }
- inline void __checkType(const PyVar& obj, const PyVar& type){
- if(!obj->isType(type)) typeError("expected '" + type->getName() + "', but got '" + obj->getTypeName() + "'");
- }
- inline void __checkArgSize(const pkpy::ArgList& args, int size, bool method=false){
- if(args.size() == size) return;
- if(method) typeError(args.size()>size ? "too many arguments" : "too few arguments");
- else typeError("expected " + std::to_string(size) + " arguments, but got " + std::to_string(args.size()));
- }
- void _assert(bool val, const _Str& msg){
- if (!val) _error("AssertionError", msg);
- }
- virtual ~VM() {
- if(!use_stdio){
- delete _stdout;
- delete _stderr;
- }
- }
- };
- /***** Pointers' Impl *****/
- PyVar NamePointer::get(VM* vm, Frame* frame) const{
- auto it = frame->f_locals.find(name);
- if(it != frame->f_locals.end()) return it->second;
- it = frame->f_globals().find(name);
- if(it != frame->f_globals().end()) return it->second;
- it = vm->builtins->attribs.find(name);
- if(it != vm->builtins->attribs.end()) return it->second;
- vm->nameError(name);
- return nullptr;
- }
- void NamePointer::set(VM* vm, Frame* frame, PyVar val) const{
- switch(scope) {
- case NAME_LOCAL: frame->f_locals[name] = std::move(val); break;
- case NAME_GLOBAL:
- {
- if(frame->f_locals.count(name) > 0){
- frame->f_locals[name] = std::move(val);
- }else{
- frame->f_globals()[name] = std::move(val);
- }
- } break;
- default: UNREACHABLE();
- }
- }
- void NamePointer::del(VM* vm, Frame* frame) const{
- switch(scope) {
- case NAME_LOCAL: {
- if(frame->f_locals.count(name) > 0){
- frame->f_locals.erase(name);
- }else{
- vm->nameError(name);
- }
- } break;
- case NAME_GLOBAL:
- {
- if(frame->f_locals.count(name) > 0){
- frame->f_locals.erase(name);
- }else{
- if(frame->f_globals().count(name) > 0){
- frame->f_globals().erase(name);
- }else{
- vm->nameError(name);
- }
- }
- } break;
- default: UNREACHABLE();
- }
- }
- PyVar AttrPointer::get(VM* vm, Frame* frame) const{
- return vm->getAttr(obj, attr->name);
- }
- void AttrPointer::set(VM* vm, Frame* frame, PyVar val) const{
- vm->setAttr(obj, attr->name, val);
- }
- void AttrPointer::del(VM* vm, Frame* frame) const{
- vm->typeError("cannot delete attribute");
- }
- PyVar IndexPointer::get(VM* vm, Frame* frame) const{
- return vm->call(obj, __getitem__, {index});
- }
- void IndexPointer::set(VM* vm, Frame* frame, PyVar val) const{
- vm->call(obj, __setitem__, {index, val});
- }
- void IndexPointer::del(VM* vm, Frame* frame) const{
- vm->call(obj, __delitem__, {index});
- }
- PyVar CompoundPointer::get(VM* vm, Frame* frame) const{
- PyVarList args(pointers.size());
- for (int i = 0; i < pointers.size(); i++) {
- args[i] = pointers[i]->get(vm, frame);
- }
- return vm->PyTuple(args);
- }
- void CompoundPointer::set(VM* vm, Frame* frame, PyVar val) const{
- if(!val->isType(vm->_tp_tuple) && !val->isType(vm->_tp_list)){
- vm->typeError("only tuple or list can be unpacked");
- }
- const PyVarList& args = std::get<PyVarList>(val->_native);
- if(args.size() > pointers.size()) vm->valueError("too many values to unpack");
- if(args.size() < pointers.size()) vm->valueError("not enough values to unpack");
- for (int i = 0; i < pointers.size(); i++) {
- pointers[i]->set(vm, frame, args[i]);
- }
- }
- void CompoundPointer::del(VM* vm, Frame* frame) const{
- for (auto& ptr : pointers) ptr->del(vm, frame);
- }
- /***** Frame's Impl *****/
- inline PyVar Frame::__deref_pointer(VM* vm, PyVar v){
- if(v->isType(vm->_tp_pointer)) return vm->PyPointer_AS_C(v)->get(vm, this);
- return v;
- }
- /***** Iterators' Impl *****/
- PyVar RangeIterator::next(){
- PyVar val = vm->PyInt(current);
- current += r.step;
- return val;
- }
- PyVar StringIterator::next(){
- return vm->PyStr(str.u8_getitem(index++));
- }
- enum ThreadState {
- THREAD_READY,
- THREAD_RUNNING,
- THREAD_SUSPENDED,
- THREAD_FINISHED
- };
- class ThreadedVM : public VM {
- std::thread* _thread = nullptr;
- std::atomic<ThreadState> state = THREAD_READY;
- public:
- ThreadedVM(bool use_stdio) : VM(use_stdio) {}
- _Str _stdin;
- void suspend(){
- if(_thread == nullptr) UNREACHABLE();
- if(state != THREAD_RUNNING) UNREACHABLE();
- state = THREAD_SUSPENDED;
- // 50 fps is enough
- while(state == THREAD_SUSPENDED) std::this_thread::sleep_for(std::chrono::milliseconds(20));
- }
- _Str readStdin(){
- if(_thread == nullptr) UNREACHABLE();
- _Str copy = _stdin;
- _stdin = "";
- return copy;
- }
- /***** For outer use *****/
- ThreadState getState(){
- return state.load();
- }
- void resume(){
- if(_thread == nullptr) UNREACHABLE();
- if(state != THREAD_SUSPENDED) UNREACHABLE();
- state = THREAD_RUNNING;
- }
- void startExec(const _Code& code){
- if(_thread != nullptr) UNREACHABLE();
- if(state != THREAD_READY) UNREACHABLE();
- _thread = new std::thread([this, code](){
- this->state = THREAD_RUNNING;
- this->exec(code);
- this->state = THREAD_FINISHED;
- });
- }
- ~ThreadedVM(){
- if(_thread != nullptr){
- _thread->join();
- delete _thread;
- }
- }
- };
|