diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 47a08d1e..7940bedc 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -127,17 +127,15 @@ public: PyObject* StopIteration; PyObject* _main; // __main__ module - PyObject* _last_exception; // last exception - PyObject* _curr_class; // current class being defined + // typeid -> Type + std::map _cxx_typeid_map; // this is for repr() recursion detection (no need to mark) std::set _repr_recursion_set; - // cached code objects for FSTRING_EVAL - std::map _cached_codes; - - // typeid -> Type - std::map _cxx_typeid_map; + PyObject* __last_exception; // last exception + PyObject* __curr_class; // current class being defined + std::map __cached_codes; void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr; @@ -396,8 +394,8 @@ public: #if PK_DEBUG_CEVAL_STEP void _log_s_data(const char* title = nullptr); #endif - void _unpack_as_list(ArgsView args, List& list); - void _unpack_as_dict(ArgsView args, Dict& dict); + void __unpack_as_list(ArgsView args, List& list); + void __unpack_as_dict(ArgsView args, Dict& dict); PyObject* vectorcall(int ARGC, int KWARGC=0, bool op_call=false); PyObject* py_negate(PyObject* obj); bool py_bool(PyObject* obj); @@ -405,7 +403,8 @@ public: PyObject* py_list(PyObject*); PyObject* new_module(Str name, Str package=""); Str disassemble(CodeObject_ co); - void init_builtin_types(); + void __init_builtin_types(); + void __post_init_builtin_types(); PyObject* getattr(PyObject* obj, StrName name, bool throw_err=true); void delattr(PyObject* obj, StrName name); PyObject* get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err=true, bool fallback=false); @@ -419,7 +418,6 @@ public: PyObject* bind_func(PyObject*, StrName, NativeFuncC, UserData userdata={}, BindType bt=BindType::DEFAULT); void _error(PyObject*); PyObject* __run_top_frame(); - void post_init(); PyObject* __format_string(Str, PyObject*); PyObject* __py_generator(Frame&& frame, ArgsView buffer); void __op_unpack_sequence(uint16_t arg); diff --git a/src/ceval.cpp b/src/ceval.cpp index 005c02f4..aa445812 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -205,9 +205,9 @@ __NEXT_STEP:; TOP() = getattr(TOP(), StrName(byte.arg)); } DISPATCH(); case OP_LOAD_CLASS_GLOBAL:{ - PK_ASSERT(_curr_class != nullptr); + PK_ASSERT(__curr_class != nullptr); StrName _name(byte.arg); - PyObject* _0 = getattr(_curr_class, _name, false); + PyObject* _0 = getattr(__curr_class, _name, false); if(_0 != nullptr) { PUSH(_0); DISPATCH(); } // load global if attribute not found _0 = frame->f_globals().try_get_likely_found(_name); @@ -391,7 +391,7 @@ __NEXT_STEP:; case OP_BUILD_TUPLE_UNPACK: { auto _lock = heap.gc_scope_lock(); List list; - _unpack_as_list(STACK_VIEW(byte.arg), list); + __unpack_as_list(STACK_VIEW(byte.arg), list); STACK_SHRINK(byte.arg); PyObject* _0 = VAR(Tuple(std::move(list))); PUSH(_0); @@ -399,7 +399,7 @@ __NEXT_STEP:; case OP_BUILD_LIST_UNPACK: { auto _lock = heap.gc_scope_lock(); List list; - _unpack_as_list(STACK_VIEW(byte.arg), list); + __unpack_as_list(STACK_VIEW(byte.arg), list); STACK_SHRINK(byte.arg); PyObject* _0 = VAR(std::move(list)); PUSH(_0); @@ -407,7 +407,7 @@ __NEXT_STEP:; case OP_BUILD_DICT_UNPACK: { auto _lock = heap.gc_scope_lock(); Dict dict(this); - _unpack_as_dict(STACK_VIEW(byte.arg), dict); + __unpack_as_dict(STACK_VIEW(byte.arg), dict); STACK_SHRINK(byte.arg); PyObject* _0 = VAR(std::move(dict)); PUSH(_0); @@ -415,7 +415,7 @@ __NEXT_STEP:; case OP_BUILD_SET_UNPACK: { auto _lock = heap.gc_scope_lock(); List list; - _unpack_as_list(STACK_VIEW(byte.arg), list); + __unpack_as_list(STACK_VIEW(byte.arg), list); STACK_SHRINK(byte.arg); PyObject* _0 = VAR(std::move(list)); _0 = call(builtins->attr(pk_id_set), _0); @@ -645,11 +645,11 @@ __NEXT_STEP:; case OP_FSTRING_EVAL:{ PyObject* _0 = co->consts[byte.arg]; std::string_view string = CAST(Str&, _0).sv(); - auto it = _cached_codes.find(string); + auto it = __cached_codes.find(string); CodeObject_ code; - if(it == _cached_codes.end()){ + if(it == __cached_codes.end()){ code = vm->compile(string, "", EVAL_MODE, true); - _cached_codes[string] = code; + __cached_codes[string] = code; }else{ code = it->second; } @@ -871,39 +871,39 @@ __NEXT_STEP:; PyObject* _0 = POPX(); // super if(_0 == None) _0 = _t(tp_object); check_type(_0, tp_type); - _curr_class = new_type_object(frame->_module, _name, PK_OBJ_GET(Type, _0)); + __curr_class = new_type_object(frame->_module, _name, PK_OBJ_GET(Type, _0)); } DISPATCH(); case OP_END_CLASS: { - PK_ASSERT(_curr_class != nullptr); + PK_ASSERT(__curr_class != nullptr); StrName _name(byte.arg); - frame->_module->attr().set(_name, _curr_class); + frame->_module->attr().set(_name, __curr_class); // call on_end_subclass - PyTypeInfo* ti = &_all_types[PK_OBJ_GET(Type, _curr_class)]; + PyTypeInfo* ti = &_all_types[PK_OBJ_GET(Type, __curr_class)]; if(ti->base != tp_object){ PyTypeInfo* base_ti = &_all_types[ti->base]; if(base_ti->on_end_subclass) base_ti->on_end_subclass(this, ti); } - _curr_class = nullptr; + __curr_class = nullptr; } DISPATCH(); case OP_STORE_CLASS_ATTR:{ - PK_ASSERT(_curr_class != nullptr); + PK_ASSERT(__curr_class != nullptr); StrName _name(byte.arg); PyObject* _0 = POPX(); if(is_type(_0, tp_function)){ - PK_OBJ_GET(Function, _0)._class = _curr_class; + PK_OBJ_GET(Function, _0)._class = __curr_class; } - _curr_class->attr().set(_name, _0); + __curr_class->attr().set(_name, _0); } DISPATCH(); case OP_BEGIN_CLASS_DECORATION:{ - PUSH(_curr_class); + PUSH(__curr_class); } DISPATCH(); case OP_END_CLASS_DECORATION:{ - _curr_class = POPX(); + __curr_class = POPX(); } DISPATCH(); case OP_ADD_CLASS_ANNOTATION: { - PK_ASSERT(_curr_class != nullptr); + PK_ASSERT(__curr_class != nullptr); StrName _name(byte.arg); - Type type = PK_OBJ_GET(Type, _curr_class); + Type type = PK_OBJ_GET(Type, __curr_class); _all_types[type].annotated_fields.push_back(_name); } DISPATCH(); /*****************************************/ @@ -940,7 +940,7 @@ __NEXT_STEP:; } DISPATCH(); case OP_RE_RAISE: __raise(true); DISPATCH(); - case OP_POP_EXCEPTION: _last_exception = POPX(); DISPATCH(); + case OP_POP_EXCEPTION: __last_exception = POPX(); DISPATCH(); /*****************************************/ case OP_FORMAT_STRING: { PyObject* _0 = POPX(); diff --git a/src/modules.cpp b/src/modules.cpp index 196a11ec..b4205ebc 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -200,15 +200,15 @@ void add_module_math(VM* vm){ void add_module_traceback(VM* vm){ PyObject* mod = vm->new_module("traceback"); vm->bind_func<0>(mod, "print_exc", [](VM* vm, ArgsView args) { - if(vm->_last_exception==nullptr) vm->ValueError("no exception"); - Exception& e = _CAST(Exception&, vm->_last_exception); + if(vm->__last_exception==nullptr) vm->ValueError("no exception"); + Exception& e = _CAST(Exception&, vm->__last_exception); vm->stdout_write(e.summary()); return vm->None; }); vm->bind_func<0>(mod, "format_exc", [](VM* vm, ArgsView args) { - if(vm->_last_exception==nullptr) vm->ValueError("no exception"); - Exception& e = _CAST(Exception&, vm->_last_exception); + if(vm->__last_exception==nullptr) vm->ValueError("no exception"); + Exception& e = _CAST(Exception&, vm->__last_exception); return VAR(e.summary()); }); } diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 855f4da3..6aaa01d3 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -1510,7 +1510,7 @@ void init_builtins(VM* _vm) { _vm->register_user_class(_vm->builtins, "_dict_items_iter"); } -void VM::post_init(){ +void VM::__post_init_builtin_types(){ init_builtins(this); bind_method<-1>(tp_module, "__init__", [](VM* vm, ArgsView args) { diff --git a/src/vm.cpp b/src/vm.cpp index ef7a8de4..9d97b476 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -78,9 +78,9 @@ namespace pkpy{ _stdout = [](const char* buf, int size) { std::cout.write(buf, size); }; _stderr = [](const char* buf, int size) { std::cerr.write(buf, size); }; _main = nullptr; - _last_exception = nullptr; + __last_exception = nullptr; _import_handler = [](const char* name_p, int name_size, int* out_size) -> unsigned char*{ return nullptr; }; - init_builtin_types(); + __init_builtin_types(); } PyObject* VM::py_str(PyObject* obj){ @@ -734,7 +734,7 @@ void VM::_log_s_data(const char* title) { } #endif -void VM::init_builtin_types(){ +void VM::__init_builtin_types(){ _all_types.push_back({heap._new(Type(1), Type(0)), -1, nullptr, "object", true}); _all_types.push_back({heap._new(Type(1), Type(1)), 0, nullptr, "type", false}); @@ -802,12 +802,12 @@ void VM::init_builtin_types(){ builtins->attr().set("SyntaxError", _t(tp_syntax_error)); builtins->attr().set("IndentationError", _t(tp_indentation_error)); - post_init(); + __post_init_builtin_types(); this->_main = new_module("__main__"); } // `heap.gc_scope_lock();` needed before calling this function -void VM::_unpack_as_list(ArgsView args, List& list){ +void VM::__unpack_as_list(ArgsView args, List& list){ for(PyObject* obj: args){ if(is_type(obj, tp_star_wrapper)){ const StarWrapper& w = _CAST(StarWrapper&, obj); @@ -827,7 +827,7 @@ void VM::_unpack_as_list(ArgsView args, List& list){ } // `heap.gc_scope_lock();` needed before calling this function -void VM::_unpack_as_dict(ArgsView args, Dict& dict){ +void VM::__unpack_as_dict(ArgsView args, Dict& dict){ for(PyObject* obj: args){ if(is_type(obj, tp_star_wrapper)){ const StarWrapper& w = _CAST(StarWrapper&, obj); @@ -1321,9 +1321,9 @@ void ManagedHeap::mark() { for(PyObject* obj: _no_gc) PK_OBJ_MARK(obj); vm->callstack.apply([](Frame& frame){ frame._gc_mark(); }); for(PyObject* obj: vm->s_data) PK_OBJ_MARK(obj); - for(auto [_, co]: vm->_cached_codes) co->_gc_mark(); - if(vm->_last_exception) PK_OBJ_MARK(vm->_last_exception); - if(vm->_curr_class) PK_OBJ_MARK(vm->_curr_class); + for(auto [_, co]: vm->__cached_codes) co->_gc_mark(); + if(vm->__last_exception) PK_OBJ_MARK(vm->__last_exception); + if(vm->__curr_class) PK_OBJ_MARK(vm->__curr_class); if(vm->_c.error != nullptr) PK_OBJ_MARK(vm->_c.error); if(_gc_marker_ex) _gc_marker_ex(vm); }