diff --git a/src/builtins.h b/src/builtins.h index 9168c186..7ad4c555 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -14,6 +14,15 @@ def round(x, ndigits=0): else: 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): return x < 0 ? -x : x diff --git a/src/pocketpy.h b/src/pocketpy.h index e79b27fa..d06d88ba 100644 --- a/src/pocketpy.h +++ b/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()); }); - _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->check_args_size(args, 1); return vm->asRepr(args[0]); diff --git a/src/repl.h b/src/repl.h index 692b56bb..3a6d4510 100644 --- a/src/repl.h +++ b/src/repl.h @@ -42,7 +42,6 @@ __NOT_ENOUGH_LINES: } try{ - // duplicated compile to catch NeedMoreLines vm->compile(line, "", SINGLE_MODE); }catch(NeedMoreLines& ne){ buffer += line; diff --git a/src/vm.h b/src/vm.h index 5314cc30..2921c81c 100644 --- a/src/vm.h +++ b/src/vm.h @@ -365,7 +365,7 @@ public: } 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)); } @@ -682,16 +682,6 @@ public: 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{ return obj->is_type(_tp_int) || obj->is_type(_tp_float); } diff --git a/tests/_class.py b/tests/_class.py index c616474a..d16833fc 100644 --- a/tests/_class.py +++ b/tests/_class.py @@ -64,4 +64,15 @@ assert D.__base__ is C d = D(1, 2, 3, 4, 5) assert d.add() == 15 -assert d.sub() == -13 \ No newline at end of file +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) \ No newline at end of file