Răsfoiți Sursa

move isinstance

blueloveTH 3 ani în urmă
părinte
comite
3bfb18e2c3
5 a modificat fișierele cu 22 adăugiri și 18 ștergeri
  1. 9 0
      src/builtins.h
  2. 0 5
      src/pocketpy.h
  3. 0 1
      src/repl.h
  4. 1 11
      src/vm.h
  5. 12 1
      tests/_class.py

+ 9 - 0
src/builtins.h

@@ -14,6 +14,15 @@ def round(x, ndigits=0):
     else:
     else:
         return int(x * 10**ndigits - 0.5) / 10**ndigits
         return int(x * 10**ndigits - 0.5) / 10**ndigits
 
 
+def isinstance(obj, cls):
+    assert type(cls) is type
+    obj_t = type(obj)
+    while obj_t is not None:
+        if obj_t is cls:
+            return True
+        obj_t = obj_t.__base__
+    return False
+
 def abs(x):
 def abs(x):
     return x < 0 ? -x : x
     return x < 0 ? -x : x
 
 

+ 0 - 5
src/pocketpy.h

@@ -70,11 +70,6 @@ void __initializeBuiltinFunctions(VM* _vm) {
         return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->f_locals_copy());
         return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->f_locals_copy());
     });
     });
 
 
-    _vm->bindBuiltinFunc("isinstance", [](VM* vm, const pkpy::ArgList& args) {
-        vm->check_args_size(args, 2);
-        return vm->PyBool(vm->isinstance(args[0], args[1]));
-    });
-
     _vm->bindBuiltinFunc("repr", [](VM* vm, const pkpy::ArgList& args) {
     _vm->bindBuiltinFunc("repr", [](VM* vm, const pkpy::ArgList& args) {
         vm->check_args_size(args, 1);
         vm->check_args_size(args, 1);
         return vm->asRepr(args[0]);
         return vm->asRepr(args[0]);

+ 0 - 1
src/repl.h

@@ -42,7 +42,6 @@ __NOT_ENOUGH_LINES:
         }
         }
 
 
         try{
         try{
-            // duplicated compile to catch NeedMoreLines
             vm->compile(line, "<stdin>", SINGLE_MODE);
             vm->compile(line, "<stdin>", SINGLE_MODE);
         }catch(NeedMoreLines& ne){
         }catch(NeedMoreLines& ne){
             buffer += line;
             buffer += line;

+ 1 - 11
src/vm.h

@@ -365,7 +365,7 @@ public:
         }
         }
         initializeBuiltinClasses();
         initializeBuiltinClasses();
 
 
-        _small_integers.reserve(300);
+        _small_integers.reserve(270);
         for(i64 i=-5; i<=256; i++) _small_integers.push_back(new_object(_tp_int, i));
         for(i64 i=-5; i<=256; i++) _small_integers.push_back(new_object(_tp_int, i));
     }
     }
 
 
@@ -682,16 +682,6 @@ public:
         setattr(module, funcName, func);
         setattr(module, funcName, func);
     }
     }
 
 
-    bool isinstance(PyVar obj, PyVar type){
-        check_type(type, _tp_type);
-        PyObject* t = obj->_type.get();
-        while (t != None.get()){
-            if (t == type.get()) return true;
-            t = t->attribs[__base__].get();
-        }
-        return false;
-    }
-
     inline bool is_int_or_float(const PyVar& obj) const{
     inline bool is_int_or_float(const PyVar& obj) const{
         return obj->is_type(_tp_int) || obj->is_type(_tp_float);
         return obj->is_type(_tp_int) || obj->is_type(_tp_float);
     }
     }

+ 12 - 1
tests/_class.py

@@ -64,4 +64,15 @@ assert D.__base__ is C
 
 
 d = D(1, 2, 3, 4, 5)
 d = D(1, 2, 3, 4, 5)
 assert d.add() == 15
 assert d.add() == 15
-assert d.sub() == -13
+assert d.sub() == -13
+
+assert isinstance(1, int)
+assert isinstance(1, object)
+assert isinstance(C, type)
+assert isinstance(C, object)
+assert isinstance(d, object)
+assert isinstance(d, C)
+assert isinstance(d, B)
+assert isinstance(d, A)
+assert isinstance(object, object)
+assert isinstance(type, object)