blueloveTH 2 лет назад
Родитель
Сommit
07d8a520bf
4 измененных файлов с 13 добавлено и 8 удалено
  1. 0 1
      src/ceval.h
  2. 1 1
      src/expr.h
  3. 4 6
      src/frame.h
  4. 8 0
      src/namedict.h

+ 0 - 1
src/ceval.h

@@ -598,7 +598,6 @@ __NEXT_STEP:;
         TOP()->attr().set(_name, _0);
         DISPATCH();
     /*****************************************/
-    // TODO: using "goto" inside with block may cause __exit__ not called
     TARGET(WITH_ENTER)
         call_method(POPX(), __enter__);
         DISPATCH();

+ 1 - 1
src/expr.h

@@ -497,7 +497,7 @@ struct CompExpr: Expr{
         ctx->emit(OP_FOR_ITER, BC_NOARG, BC_KEEPLINE);
         bool ok = vars->emit_store(ctx);
         // this error occurs in `vars` instead of this line, but...nevermind
-        if(!ok) FATAL_ERROR();  // TODO: raise a SyntaxError instead
+        PK_ASSERT(ok);  // TODO: raise a SyntaxError instead
         if(cond){
             cond->emit(ctx);
             int patch = ctx->emit(OP_POP_JUMP_IF_FALSE, BC_NOARG, BC_KEEPLINE);

+ 4 - 6
src/frame.h

@@ -49,12 +49,10 @@ struct FastLocals{
 
     NameDict_ to_namedict(){
         NameDict_ dict = make_sp<NameDict>();
-        // TODO: optimize this, NameDict.items() is expensive
-        for(auto& kv: varnames_inv->items()){
-            PyObject* value = a[kv.second];
-            if(value == PY_NULL) continue;
-            dict->set(kv.first, value);
-        }
+        varnames_inv->apply([&](StrName name, int index){
+            PyObject* value = a[index];
+            if(value != PY_NULL) dict->set(name, value);
+        });
         return dict;
     }
 };

+ 8 - 0
src/namedict.h

@@ -182,6 +182,14 @@ while(!_items[i].first.empty()) {       \
         return v;
     }
 
+    template<typename __Func>
+    void apply(__Func func) const {
+        for(uint16_t i=0; i<_capacity; i++){
+            if(_items[i].first.empty()) continue;
+            func(_items[i].first, _items[i].second);
+        }
+    }
+
     std::vector<StrName> keys() const {
         std::vector<StrName> v;
         for(uint16_t i=0; i<_capacity; i++){