mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 12:30:19 +00:00
some rename
This commit is contained in:
parent
518fa3ac07
commit
9331e8eae0
@ -168,13 +168,13 @@ public:
|
||||
VM(bool enable_os=true);
|
||||
|
||||
void set_main_argv(int argc, char** argv);
|
||||
void _breakpoint();
|
||||
void __breakpoint();
|
||||
|
||||
Frame* top_frame(){
|
||||
return &callstack.top();
|
||||
}
|
||||
|
||||
void _pop_frame(){
|
||||
void __pop_frame(){
|
||||
s_data.reset(callstack.top()._sp_base);
|
||||
callstack.pop();
|
||||
|
||||
@ -206,14 +206,14 @@ public:
|
||||
template<typename ...Args>
|
||||
PyObject* _exec(Args&&... args){
|
||||
callstack.emplace(s_data._sp, std::forward<Args>(args)...);
|
||||
return _run_top_frame();
|
||||
return __run_top_frame();
|
||||
}
|
||||
|
||||
void _push_varargs(){}
|
||||
void _push_varargs(PyObject* _0){ PUSH(_0); }
|
||||
void _push_varargs(PyObject* _0, PyObject* _1){ PUSH(_0); PUSH(_1); }
|
||||
void _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2){ PUSH(_0); PUSH(_1); PUSH(_2); }
|
||||
void _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2, PyObject* _3){ PUSH(_0); PUSH(_1); PUSH(_2); PUSH(_3); }
|
||||
void __push_varargs(){}
|
||||
void __push_varargs(PyObject* _0){ PUSH(_0); }
|
||||
void __push_varargs(PyObject* _0, PyObject* _1){ PUSH(_0); PUSH(_1); }
|
||||
void __push_varargs(PyObject* _0, PyObject* _1, PyObject* _2){ PUSH(_0); PUSH(_1); PUSH(_2); }
|
||||
void __push_varargs(PyObject* _0, PyObject* _1, PyObject* _2, PyObject* _3){ PUSH(_0); PUSH(_1); PUSH(_2); PUSH(_3); }
|
||||
|
||||
virtual void stdout_write(const Str& s){
|
||||
_stdout(s.data, s.size);
|
||||
@ -227,7 +227,7 @@ public:
|
||||
PyObject* call(PyObject* callable, Args&&... args){
|
||||
PUSH(callable);
|
||||
PUSH(PY_NULL);
|
||||
_push_varargs(args...);
|
||||
__push_varargs(args...);
|
||||
return vectorcall(sizeof...(args));
|
||||
}
|
||||
|
||||
@ -235,7 +235,7 @@ public:
|
||||
PyObject* call_method(PyObject* self, PyObject* callable, Args&&... args){
|
||||
PUSH(callable);
|
||||
PUSH(self);
|
||||
_push_varargs(args...);
|
||||
__push_varargs(args...);
|
||||
return vectorcall(sizeof...(args));
|
||||
}
|
||||
|
||||
@ -258,7 +258,6 @@ public:
|
||||
void bind__hash__(Type type, i64 (*f)(VM* vm, PyObject*));
|
||||
void bind__len__(Type type, i64 (*f)(VM* vm, PyObject*));
|
||||
|
||||
|
||||
void bind__eq__(Type type, BinaryFuncC f);
|
||||
void bind__lt__(Type type, BinaryFuncC f);
|
||||
void bind__le__(Type type, BinaryFuncC f);
|
||||
@ -310,13 +309,13 @@ public:
|
||||
i64 normalized_index(i64 index, int size);
|
||||
PyObject* py_next(PyObject*);
|
||||
PyObject* _py_next(const PyTypeInfo*, PyObject*);
|
||||
PyObject* _pack_next_retval(unsigned);
|
||||
PyObject* __pack_next_retval(unsigned);
|
||||
bool py_callable(PyObject* obj);
|
||||
|
||||
PyObject* _minmax_reduce(bool (VM::*op)(PyObject*, PyObject*), PyObject* args, PyObject* key);
|
||||
PyObject* __minmax_reduce(bool (VM::*op)(PyObject*, PyObject*), PyObject* args, PyObject* key);
|
||||
|
||||
/***** Error Reporter *****/
|
||||
void _raise(bool re_raise=false);
|
||||
void __raise(bool re_raise=false);
|
||||
|
||||
void _builtin_error(StrName type);
|
||||
void _builtin_error(StrName type, PyObject* arg);
|
||||
@ -411,7 +410,6 @@ public:
|
||||
void delattr(PyObject* obj, StrName name);
|
||||
PyObject* get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err=true, bool fallback=false);
|
||||
void parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step);
|
||||
PyObject* _format_string(Str, PyObject*);
|
||||
void setattr(PyObject* obj, StrName name, PyObject* value);
|
||||
template<int ARGC>
|
||||
PyObject* bind_method(Type, StrName, NativeFuncC);
|
||||
@ -420,11 +418,12 @@ public:
|
||||
template<int ARGC>
|
||||
PyObject* bind_func(PyObject*, StrName, NativeFuncC, UserData userdata={}, BindType bt=BindType::DEFAULT);
|
||||
void _error(PyObject*);
|
||||
PyObject* _run_top_frame();
|
||||
PyObject* __run_top_frame();
|
||||
void post_init();
|
||||
PyObject* _py_generator(Frame&& frame, ArgsView buffer);
|
||||
void _op_unpack_sequence(uint16_t arg);
|
||||
void _prepare_py_call(PyObject**, ArgsView, ArgsView, const FuncDecl_&);
|
||||
PyObject* __format_string(Str, PyObject*);
|
||||
PyObject* __py_generator(Frame&& frame, ArgsView buffer);
|
||||
void __op_unpack_sequence(uint16_t arg);
|
||||
void __prepare_py_call(PyObject**, ArgsView, ArgsView, const FuncDecl_&);
|
||||
// new style binding api
|
||||
PyObject* bind(PyObject*, const char*, const char*, NativeFuncC, UserData userdata={}, BindType bt=BindType::DEFAULT);
|
||||
PyObject* bind(PyObject*, const char*, NativeFuncC, UserData userdata={}, BindType bt=BindType::DEFAULT);
|
||||
|
@ -35,7 +35,7 @@ namespace pkpy{
|
||||
}
|
||||
|
||||
|
||||
void VM::_op_unpack_sequence(uint16_t arg){
|
||||
void VM::__op_unpack_sequence(uint16_t arg){
|
||||
PyObject* _0 = POPX();
|
||||
if(is_type(_0, VM::tp_tuple)){
|
||||
// fast path for tuple
|
||||
@ -80,14 +80,14 @@ bool VM::py_ge(PyObject* _0, PyObject* _1){
|
||||
|
||||
#undef BINARY_F_COMPARE
|
||||
|
||||
PyObject* VM::_run_top_frame(){
|
||||
PyObject* VM::__run_top_frame(){
|
||||
Frame* frame = top_frame();
|
||||
const Frame* base_frame = frame;
|
||||
bool need_raise = false;
|
||||
|
||||
while(true){
|
||||
try{
|
||||
if(need_raise){ need_raise = false; _raise(); }
|
||||
if(need_raise){ need_raise = false; __raise(); }
|
||||
/**********************************************************************/
|
||||
/* NOTE:
|
||||
* Be aware of accidental gc!
|
||||
@ -709,7 +709,7 @@ __NEXT_STEP:;
|
||||
} DISPATCH();
|
||||
case OP_RETURN_VALUE:{
|
||||
PyObject* _0 = byte.arg == BC_NOARG ? POPX() : None;
|
||||
_pop_frame();
|
||||
__pop_frame();
|
||||
if(frame == base_frame){ // [ frameBase<- ]
|
||||
return _0;
|
||||
}else{
|
||||
@ -798,7 +798,7 @@ __NEXT_STEP:;
|
||||
frame->loop_break(&s_data, co);
|
||||
}else if(n == 1){
|
||||
// UNPACK_SEQUENCE
|
||||
_op_unpack_sequence(byte.arg);
|
||||
__op_unpack_sequence(byte.arg);
|
||||
}else{
|
||||
if(n != byte.arg){
|
||||
ValueError(_S("expected ", (int)byte.arg, " values to unpack, got ", (int)n));
|
||||
@ -810,7 +810,7 @@ __NEXT_STEP:;
|
||||
if(_0 != StopIteration){
|
||||
PUSH(_0);
|
||||
// UNPACK_SEQUENCE
|
||||
_op_unpack_sequence(byte.arg);
|
||||
__op_unpack_sequence(byte.arg);
|
||||
}else{
|
||||
frame->loop_break(&s_data, co);
|
||||
}
|
||||
@ -845,7 +845,7 @@ __NEXT_STEP:;
|
||||
} DISPATCH();
|
||||
/*****************************************/
|
||||
case OP_UNPACK_SEQUENCE:{
|
||||
_op_unpack_sequence(byte.arg);
|
||||
__op_unpack_sequence(byte.arg);
|
||||
} DISPATCH();
|
||||
case OP_UNPACK_EX: {
|
||||
auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!!
|
||||
@ -939,13 +939,13 @@ __NEXT_STEP:;
|
||||
_builtin_error("AssertionError");
|
||||
}
|
||||
DISPATCH();
|
||||
case OP_RE_RAISE: _raise(true); DISPATCH();
|
||||
case OP_RE_RAISE: __raise(true); DISPATCH();
|
||||
case OP_POP_EXCEPTION: _last_exception = POPX(); DISPATCH();
|
||||
/*****************************************/
|
||||
case OP_FORMAT_STRING: {
|
||||
PyObject* _0 = POPX();
|
||||
const Str& spec = CAST(Str&, co->consts[byte.arg]);
|
||||
PUSH(_format_string(spec, _0));
|
||||
PUSH(__format_string(spec, _0));
|
||||
} DISPATCH();
|
||||
/*****************************************/
|
||||
case OP_INC_FAST:{
|
||||
@ -981,7 +981,7 @@ __NEXT_STEP:;
|
||||
PyObject* e_obj = POPX();
|
||||
Exception& _e = PK_OBJ_GET(Exception, e_obj);
|
||||
bool is_base_frame_to_be_popped = frame == base_frame;
|
||||
_pop_frame();
|
||||
__pop_frame();
|
||||
if(callstack.empty()) throw _e; // propagate to the top level
|
||||
frame = top_frame();
|
||||
PUSH(e_obj);
|
||||
|
@ -56,7 +56,7 @@ namespace pkpy{
|
||||
|
||||
PyObject* ret;
|
||||
try{
|
||||
ret = vm->_run_top_frame();
|
||||
ret = vm->__run_top_frame();
|
||||
}catch(...){
|
||||
state = 2; // end this generator immediately when an exception is thrown
|
||||
throw;
|
||||
@ -67,7 +67,7 @@ namespace pkpy{
|
||||
frame = std::move(vm->callstack.top());
|
||||
ret = vm->s_data.popx();
|
||||
for(PyObject* obj: frame.stack_view(&vm->s_data)) s_backup.push_back(obj);
|
||||
vm->_pop_frame();
|
||||
vm->__pop_frame();
|
||||
state = 1;
|
||||
if(ret == vm->StopIteration) state = 2;
|
||||
return ret;
|
||||
@ -103,7 +103,7 @@ namespace pkpy{
|
||||
});
|
||||
}
|
||||
|
||||
PyObject* VM::_py_generator(Frame&& frame, ArgsView buffer){
|
||||
PyObject* VM::__py_generator(Frame&& frame, ArgsView buffer){
|
||||
return vm->new_user_object<Generator>(std::move(frame), buffer);
|
||||
}
|
||||
|
||||
|
@ -163,11 +163,11 @@ void init_builtins(VM* _vm) {
|
||||
});
|
||||
|
||||
_vm->bind(_vm->builtins, "max(*args, key=None)", [](VM* vm, ArgsView args){
|
||||
return vm->_minmax_reduce(&VM::py_gt, args[0], args[1]);
|
||||
return vm->__minmax_reduce(&VM::py_gt, args[0], args[1]);
|
||||
});
|
||||
|
||||
_vm->bind(_vm->builtins, "min(*args, key=None)", [](VM* vm, ArgsView args){
|
||||
return vm->_minmax_reduce(&VM::py_lt, args[0], args[1]);
|
||||
return vm->__minmax_reduce(&VM::py_lt, args[0], args[1]);
|
||||
});
|
||||
|
||||
_vm->bind_func<1>(_vm->builtins, "id", [](VM* vm, ArgsView args) {
|
||||
|
34
src/vm.cpp
34
src/vm.cpp
@ -249,7 +249,7 @@ namespace pkpy{
|
||||
PyObject* VM::_py_next(const PyTypeInfo* ti, PyObject* obj){
|
||||
if(ti->m__next__){
|
||||
unsigned n = ti->m__next__(this, obj);
|
||||
return _pack_next_retval(n);
|
||||
return __pack_next_retval(n);
|
||||
}
|
||||
return call_method(obj, __next__);
|
||||
}
|
||||
@ -270,7 +270,7 @@ namespace pkpy{
|
||||
return vm->find_name_in_mro(cls, __call__) != nullptr;
|
||||
}
|
||||
|
||||
PyObject* VM::_minmax_reduce(bool (VM::*op)(PyObject*, PyObject*), PyObject* args, PyObject* key){
|
||||
PyObject* VM::__minmax_reduce(bool (VM::*op)(PyObject*, PyObject*), PyObject* args, PyObject* key){
|
||||
auto _lock = heap.gc_scope_lock();
|
||||
const Tuple& args_tuple = PK_OBJ_GET(Tuple, args); // from *args, it must be a tuple
|
||||
if(key==vm->None && args_tuple.size()==2){
|
||||
@ -499,7 +499,7 @@ i64 VM::py_hash(PyObject* obj){
|
||||
}
|
||||
}
|
||||
|
||||
PyObject* VM::_format_string(Str spec, PyObject* obj){
|
||||
PyObject* VM::__format_string(Str spec, PyObject* obj){
|
||||
if(spec.empty()) return py_str(obj);
|
||||
char type;
|
||||
switch(spec.end()[-1]){
|
||||
@ -844,7 +844,7 @@ void VM::_unpack_as_dict(ArgsView args, Dict& dict){
|
||||
}
|
||||
|
||||
|
||||
void VM::_prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, const FuncDecl_& decl){
|
||||
void VM::__prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, const FuncDecl_& decl){
|
||||
const CodeObject* co = decl->code.get();
|
||||
int co_nlocals = co->varnames.size();
|
||||
int decl_argc = decl->args.size();
|
||||
@ -943,7 +943,7 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
|
||||
switch(fn.decl->type){
|
||||
case FuncType::UNSET: PK_FATAL_ERROR(); break;
|
||||
case FuncType::NORMAL:
|
||||
_prepare_py_call(buffer, args, kwargs, fn.decl);
|
||||
__prepare_py_call(buffer, args, kwargs, fn.decl);
|
||||
// copy buffer back to stack
|
||||
s_data.reset(_base + co_nlocals);
|
||||
for(int j=0; j<co_nlocals; j++) _base[j] = buffer[j];
|
||||
@ -963,9 +963,9 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
|
||||
s_data.reset(p0);
|
||||
return None;
|
||||
case FuncType::GENERATOR:
|
||||
_prepare_py_call(buffer, args, kwargs, fn.decl);
|
||||
__prepare_py_call(buffer, args, kwargs, fn.decl);
|
||||
s_data.reset(p0);
|
||||
return _py_generator(
|
||||
return __py_generator(
|
||||
Frame(nullptr, co, fn._module, callable, nullptr),
|
||||
ArgsView(buffer, buffer + co_nlocals)
|
||||
);
|
||||
@ -974,7 +974,7 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
|
||||
// simple or normal
|
||||
callstack.emplace(p0, co, fn._module, callable, args.begin());
|
||||
if(op_call) return PY_OP_CALL;
|
||||
return _run_top_frame();
|
||||
return __run_top_frame();
|
||||
/*****************_py_call*****************/
|
||||
}
|
||||
|
||||
@ -983,7 +983,7 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
|
||||
PyObject* ret;
|
||||
if(f.decl != nullptr){
|
||||
int co_nlocals = f.decl->code->varnames.size();
|
||||
_prepare_py_call(buffer, args, kwargs, f.decl);
|
||||
__prepare_py_call(buffer, args, kwargs, f.decl);
|
||||
// copy buffer back to stack
|
||||
s_data.reset(_base + co_nlocals);
|
||||
for(int j=0; j<co_nlocals; j++) _base[j] = buffer[j];
|
||||
@ -1294,10 +1294,10 @@ void VM::_error(PyObject* e_obj){
|
||||
throw e;
|
||||
}
|
||||
PUSH(e_obj);
|
||||
_raise();
|
||||
__raise();
|
||||
}
|
||||
|
||||
void VM::_raise(bool re_raise){
|
||||
void VM::__raise(bool re_raise){
|
||||
Frame* frame = top_frame();
|
||||
Exception& e = PK_OBJ_GET(Exception, s_data.top());
|
||||
if(!re_raise){
|
||||
@ -1365,7 +1365,7 @@ void VM::bind__delitem__(Type type, void (*f)(VM*, PyObject*, PyObject*)){
|
||||
}
|
||||
|
||||
|
||||
PyObject* VM::_pack_next_retval(unsigned n){
|
||||
PyObject* VM::__pack_next_retval(unsigned n){
|
||||
if(n == 0) return StopIteration;
|
||||
if(n == 1) return s_data.popx();
|
||||
PyObject* retval = VAR(s_data.view(n).to_tuple());
|
||||
@ -1377,7 +1377,7 @@ void VM::bind__delitem__(Type type, void (*f)(VM*, PyObject*, PyObject*)){
|
||||
_all_types[type].m__next__ = f; \
|
||||
PyObject* nf = bind_method<0>(_t(type), __next__, [](VM* vm, ArgsView args){ \
|
||||
int n = lambda_get_userdata<unsigned(*)(VM*, PyObject*)>(args.begin())(vm, args[0]);\
|
||||
return vm->_pack_next_retval(n); \
|
||||
return vm->__pack_next_retval(n); \
|
||||
}); \
|
||||
PK_OBJ_GET(NativeFunc, nf).set_userdata(f); \
|
||||
}
|
||||
@ -1500,20 +1500,20 @@ void NextBreakpoint::_step(VM* vm){
|
||||
int curr_lineno = vm->top_frame()->curr_lineno();
|
||||
if(should_step_into){
|
||||
if(curr_callstack_size != callstack_size || curr_lineno != lineno){
|
||||
vm->_breakpoint();
|
||||
vm->__breakpoint();
|
||||
}
|
||||
}else{
|
||||
if(curr_callstack_size == callstack_size) {
|
||||
if(curr_lineno != lineno) vm->_breakpoint();
|
||||
if(curr_lineno != lineno) vm->__breakpoint();
|
||||
}else if(curr_callstack_size < callstack_size){
|
||||
// returning
|
||||
vm->_breakpoint();
|
||||
vm->__breakpoint();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void VM::_breakpoint(){
|
||||
void VM::__breakpoint(){
|
||||
#if PK_ENABLE_PROFILER
|
||||
_next_breakpoint = NextBreakpoint();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user