This commit is contained in:
blueloveTH 2023-05-27 19:59:17 +08:00
parent 9d7204fe3a
commit db283d4ba6
3 changed files with 28 additions and 24 deletions

View File

@ -471,7 +471,7 @@ __NEXT_STEP:;
TOP() = py_iter(TOP());
DISPATCH();
TARGET(FOR_ITER)
_0 = PyIterNext(TOP());
_0 = py_next(TOP());
if(_0 != StopIteration){
PUSH(_0);
}else{
@ -493,35 +493,39 @@ __NEXT_STEP:;
frame->f_globals()._try_perfect_rehash();
DISPATCH();
/*****************************************/
TARGET(UNPACK_SEQUENCE)
TARGET(UNPACK_SEQUENCE){
auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!!
_0 = py_iter(POPX());
for(int i=0; i<byte.arg; i++){
_1 = py_next(_0);
if(_1 == StopIteration) ValueError("not enough values to unpack");
PUSH(_1);
}
if(py_next(_0) != StopIteration) ValueError("too many values to unpack");
} DISPATCH();
TARGET(UNPACK_EX) {
auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!!
_0 = py_iter(POPX());
for(int i=0; i<byte.arg; i++){
_1 = PyIterNext(_0);
_1 = py_next(_0);
if(_1 == StopIteration) ValueError("not enough values to unpack");
PUSH(_1);
}
// handle extra items
if(byte.op == OP_UNPACK_EX){
List extras;
while(true){
_1 = PyIterNext(_0);
_1 = py_next(_0);
if(_1 == StopIteration) break;
extras.push_back(_1);
}
PUSH(VAR(extras));
}else{
if(PyIterNext(_0) != StopIteration) ValueError("too many values to unpack");
}
} DISPATCH();
TARGET(UNPACK_UNLIMITED) {
auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!!
_0 = py_iter(POPX());
_1 = PyIterNext(_0);
_1 = py_next(_0);
while(_1 != StopIteration){
PUSH(_1);
_1 = PyIterNext(_0);
_1 = py_next(_0);
}
} DISPATCH();
/*****************************************/

View File

@ -185,7 +185,7 @@ inline void init_builtins(VM* _vm) {
});
_vm->bind_builtin_func<1>("next", [](VM* vm, ArgsView args) {
return vm->PyIterNext(args[0]);
return vm->py_next(args[0]);
});
_vm->bind_builtin_func<1>("dir", [](VM* vm, ArgsView args) {
@ -463,11 +463,11 @@ inline void init_builtins(VM* _vm) {
const Str& self = _CAST(Str&, args[0]);
FastStrStream ss;
PyObject* it = vm->py_iter(args[1]); // strong ref
PyObject* obj = vm->PyIterNext(it);
PyObject* obj = vm->py_next(it);
while(obj != vm->StopIteration){
if(!ss.empty()) ss << self;
ss << CAST(Str&, obj);
obj = vm->PyIterNext(it);
obj = vm->py_next(it);
}
return VAR(ss.str());
});
@ -560,10 +560,10 @@ inline void init_builtins(VM* _vm) {
auto _lock = vm->heap.gc_scope_lock();
List& self = _CAST(List&, args[0]);
PyObject* it = vm->py_iter(args[1]); // strong ref
PyObject* obj = vm->PyIterNext(it);
PyObject* obj = vm->py_next(it);
while(obj != vm->StopIteration){
self.push_back(obj);
obj = vm->PyIterNext(it);
obj = vm->py_next(it);
}
return vm->None;
});

View File

@ -485,7 +485,7 @@ public:
return index;
}
PyObject* PyIterNext(PyObject* obj){
PyObject* py_next(PyObject* obj){
const PyTypeInfo* ti = _inst_type_info(obj);
if(ti->m__next__) return ti->m__next__(this, obj);
return call_method(obj, __next__);
@ -790,10 +790,10 @@ inline PyObject* VM::py_list(PyObject* it){
auto _lock = heap.gc_scope_lock();
it = py_iter(it);
List list;
PyObject* obj = PyIterNext(it);
PyObject* obj = py_next(it);
while(obj != StopIteration){
list.push_back(obj);
obj = PyIterNext(it);
obj = py_next(it);
}
return VAR(std::move(list));
}