From f53a46941a377d4d3eb505fe6368c384d93f6bd9 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 11 Apr 2024 11:50:15 +0800 Subject: [PATCH] deprecate `*non_tagged*` --- 3rd/cjson/src/cJSONw.cpp | 2 +- 3rd/lua_bridge/src/lua_bridge.cpp | 4 +-- docs/quick-start/interop.md | 3 +- include/pocketpy/obj.h | 12 +++----- include/pocketpy/vm.h | 10 +++---- src/array2d.cpp | 10 +++---- src/ceval.cpp | 8 ++--- src/cffi.cpp | 4 +-- src/collections.cpp | 2 +- src/dataclasses.cpp | 2 +- src/expr.cpp | 2 +- src/linalg.cpp | 6 ++-- src/modules.cpp | 4 +-- src/pocketpy.cpp | 50 +++++++++++++++---------------- src/pocketpy_c.cpp | 6 ++-- src/vm.cpp | 22 +++++++------- 16 files changed, 71 insertions(+), 76 deletions(-) diff --git a/3rd/cjson/src/cJSONw.cpp b/3rd/cjson/src/cJSONw.cpp index a70024fb..e37e47ec 100644 --- a/3rd/cjson/src/cJSONw.cpp +++ b/3rd/cjson/src/cJSONw.cpp @@ -100,7 +100,7 @@ void add_module_cjson(VM* vm){ vm->bind_func<1>(mod, "loads", [](VM* vm, ArgsView args){ std::string_view sv; - if(is_non_tagged_type(args[0], vm->tp_bytes)){ + if(is_type(args[0], vm->tp_bytes)){ sv = PK_OBJ_GET(Bytes, args[0]).sv(); }else{ sv = CAST(Str&, args[0]).sv(); diff --git a/3rd/lua_bridge/src/lua_bridge.cpp b/3rd/lua_bridge/src/lua_bridge.cpp index 0a5bd040..96623b0f 100644 --- a/3rd/lua_bridge/src/lua_bridge.cpp +++ b/3rd/lua_bridge/src/lua_bridge.cpp @@ -274,13 +274,13 @@ void lua_push_from_python(VM* vm, PyObject* val){ } } - if(is_non_tagged_type(val, PyLuaTable::_type(vm))){ + if(is_type(val, PyLuaTable::_type(vm))){ const PyLuaTable& table = _CAST(PyLuaTable&, val); lua_rawgeti(_L, LUA_REGISTRYINDEX, table.r); return; } - if(is_non_tagged_type(val, PyLuaFunction::_type(vm))){ + if(is_type(val, PyLuaFunction::_type(vm))){ const PyLuaFunction& func = _CAST(PyLuaFunction&, val); lua_rawgeti(_L, LUA_REGISTRYINDEX, func.r); return; diff --git a/docs/quick-start/interop.md b/docs/quick-start/interop.md index d406e18c..c4deeba6 100644 --- a/docs/quick-start/interop.md +++ b/docs/quick-start/interop.md @@ -95,7 +95,6 @@ you can use the following functions: + `bool is_int(PyObject* obj)` + `bool is_float(PyObject* obj)` + `bool is_tagged(PyObject* obj)` -+ `bool is_non_tagged_type(PyObject* obj, Type type)` ```cpp PyObject* obj = py_var(vm, 1); @@ -115,4 +114,4 @@ You can also use `check_` prefix functions assert the type of a `PyObject*`, which will throw `TypeError` on failure. + `void check_type(PyObject* obj, Type type)` -+ `void check_non_tagged_type(PyObject* obj, Type type)` + diff --git a/include/pocketpy/obj.h b/include/pocketpy/obj.h index 83ba0730..28925ae1 100644 --- a/include/pocketpy/obj.h +++ b/include/pocketpy/obj.h @@ -149,17 +149,13 @@ inline bool is_type(PyObject* obj, Type type) { #if PK_DEBUG_EXTRA_CHECK if(obj == nullptr) throw std::runtime_error("is_type() called with nullptr"); #endif - switch(type.index){ - case kTpIntIndex: return is_int(obj); - default: return !is_tagged(obj) && obj->type == type; - } + if(type.index == kTpIntIndex) return is_int(obj); + return !is_tagged(obj) && obj->type == type; } +[[deprecated("use is_type() instead")]] inline bool is_non_tagged_type(PyObject* obj, Type type) { -#if PK_DEBUG_EXTRA_CHECK - if(obj == nullptr) throw std::runtime_error("is_non_tagged_type() called with nullptr"); -#endif - return !is_tagged(obj) && obj->type == type; + return is_type(obj, type); } template struct has_gc_marker : std::false_type {}; diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 06ba2e36..6a52335d 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -351,9 +351,9 @@ public: TypeError("expected " + _type_name(vm, type).escape() + ", got " + _type_name(vm, _tp(obj)).escape()); } + [[deprecated("use check_type() instead")]] void check_non_tagged_type(PyObject* obj, Type type){ - if(is_non_tagged_type(obj, type)) return; - TypeError("expected " + _type_name(vm, type).escape() + ", got " + _type_name(vm, _tp(obj)).escape()); + return check_type(obj, type); } void check_compatible_type(PyObject* obj, Type type){ @@ -527,7 +527,7 @@ __T _py_cast__internal(VM* vm, PyObject* obj) { static_assert(!std::is_reference_v<__T>); // str (shortcuts) if(obj == vm->None) return nullptr; - if constexpr(with_check) vm->check_non_tagged_type(obj, vm->tp_str); + if constexpr(with_check) vm->check_type(obj, vm->tp_str); return PK_OBJ_GET(Str, obj).c_str(); }else if constexpr(std::is_same_v){ static_assert(!std::is_reference_v<__T>); @@ -571,7 +571,7 @@ __T _py_cast__internal(VM* vm, PyObject* obj) { // Exception is `subclass_enabled` vm->check_compatible_type(obj, const_type); }else{ - vm->check_non_tagged_type(obj, const_type); + vm->check_type(obj, const_type); } } return PK_OBJ_GET(T, obj); @@ -597,7 +597,7 @@ PyObject* VM::bind_method(Type type, Str name, NativeFuncC fn) { template PyObject* VM::bind_method(PyObject* obj, Str name, NativeFuncC fn) { - check_non_tagged_type(obj, tp_type); + check_type(obj, tp_type); return bind_method(PK_OBJ_GET(Type, obj), name, fn); } diff --git a/src/array2d.cpp b/src/array2d.cpp index 4fd8548d..501d4c82 100644 --- a/src/array2d.cpp +++ b/src/array2d.cpp @@ -103,7 +103,7 @@ struct Array2d{ return self._get(col, row); } - if(is_non_tagged_type(xy[0], VM::tp_slice) && is_non_tagged_type(xy[1], VM::tp_slice)){ + if(is_type(xy[0], VM::tp_slice) && is_type(xy[1], VM::tp_slice)){ HANDLE_SLICE(); PyObject* new_array_obj = vm->heap.gcnew(Array2d::_type(vm)); Array2d& new_array = PK_OBJ_GET(Array2d, new_array_obj); @@ -131,7 +131,7 @@ struct Array2d{ return; } - if(is_non_tagged_type(xy[0], VM::tp_slice) && is_non_tagged_type(xy[1], VM::tp_slice)){ + if(is_type(xy[0], VM::tp_slice) && is_type(xy[1], VM::tp_slice)){ HANDLE_SLICE(); bool is_basic_type = false; @@ -150,7 +150,7 @@ struct Array2d{ return; } - if(!is_non_tagged_type(_2, Array2d::_type(vm))){ + if(!is_type(_2, Array2d::_type(vm))){ vm->TypeError(_S("expected int/float/str/bool/None or an array2d instance")); } @@ -231,7 +231,7 @@ struct Array2d{ vm->bind(type, "copy_(self, other)", [](VM* vm, ArgsView args){ Array2d& self = PK_OBJ_GET(Array2d, args[0]); - if(is_non_tagged_type(args[1], VM::tp_list)){ + if(is_type(args[1], VM::tp_list)){ const List& list = PK_OBJ_GET(List, args[1]); if(list.size() != self.numel){ vm->ValueError("list size must be equal to the number of elements in the array2d"); @@ -255,7 +255,7 @@ struct Array2d{ vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ Array2d& self = PK_OBJ_GET(Array2d, _0); - if(!is_non_tagged_type(_1, Array2d::_type(vm))) return vm->NotImplemented; + if(!is_type(_1, Array2d::_type(vm))) return vm->NotImplemented; Array2d& other = PK_OBJ_GET(Array2d, _1); if(self.n_cols != other.n_cols || self.n_rows != other.n_rows) return vm->False; for(int i = 0; i < self.numel; i++){ diff --git a/src/ceval.cpp b/src/ceval.cpp index 6be22301..3f77a70b 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -807,7 +807,7 @@ __NEXT_STEP:; StrName _name(byte.arg); PyObject* _0 = POPX(); // super if(_0 == None) _0 = _t(tp_object); - check_non_tagged_type(_0, tp_type); + check_type(_0, tp_type); _curr_class = new_type_object(frame->_module, _name, PK_OBJ_GET(Type, _0)); } DISPATCH(); TARGET(END_CLASS) { @@ -826,7 +826,7 @@ __NEXT_STEP:; PK_ASSERT(_curr_class != nullptr); StrName _name(byte.arg); PyObject* _0 = POPX(); - if(is_non_tagged_type(_0, tp_function)){ + if(is_type(_0, tp_function)){ PK_OBJ_GET(Function, _0)._class = _curr_class; } _curr_class->attr().set(_name, _0); @@ -854,13 +854,13 @@ __NEXT_STEP:; /*****************************************/ TARGET(EXCEPTION_MATCH) { PyObject* assumed_type = POPX(); - check_non_tagged_type(assumed_type, tp_type); + check_type(assumed_type, tp_type); PyObject* e_obj = TOP(); bool ok = isinstance(e_obj, PK_OBJ_GET(Type, assumed_type)); PUSH(VAR(ok)); } DISPATCH(); TARGET(RAISE) { - if(is_non_tagged_type(TOP(), tp_type)){ + if(is_type(TOP(), tp_type)){ TOP() = call(TOP()); } if(!isinstance(TOP(), tp_exception)){ diff --git a/src/cffi.cpp b/src/cffi.cpp index d252925d..2919bb21 100644 --- a/src/cffi.cpp +++ b/src/cffi.cpp @@ -96,7 +96,7 @@ namespace pkpy{ vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){ C99Struct& self = _CAST(C99Struct&, lhs); - if(!is_non_tagged_type(rhs, C99Struct::_type(vm))) return vm->NotImplemented; + if(!is_type(rhs, C99Struct::_type(vm))) return vm->NotImplemented; C99Struct& other = _CAST(C99Struct&, rhs); bool ok = self.size == other.size && memcmp(self.p, other.p, self.size) == 0; return VAR(ok); @@ -167,7 +167,7 @@ void add_module_c(VM* vm){ vm->bind(mod, "p_cast(ptr: 'void_p', cls: type[T]) -> T", [](VM* vm, ArgsView args){ VoidP& ptr = CAST(VoidP&, args[0]); - vm->check_non_tagged_type(args[1], vm->tp_type); + vm->check_type(args[1], vm->tp_type); Type cls = PK_OBJ_GET(Type, args[1]); if(!vm->issubclass(cls, VoidP::_type(vm))){ vm->ValueError("expected a subclass of void_p"); diff --git a/src/collections.cpp b/src/collections.cpp index 5a81490f..8376fb8a 100644 --- a/src/collections.cpp +++ b/src/collections.cpp @@ -134,7 +134,7 @@ namespace pkpy vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM *vm, PyObject* _0, PyObject* _1) { const PyDeque &self = _CAST(PyDeque&, _0); - if(!is_non_tagged_type(_0, PyDeque::_type(vm))) return vm->NotImplemented; + if(!is_type(_0, PyDeque::_type(vm))) return vm->NotImplemented; const PyDeque &other = _CAST(PyDeque&, _1); if (self.dequeItems.size() != other.dequeItems.size()) return vm->False; for (int i = 0; i < self.dequeItems.size(); i++){ diff --git a/src/dataclasses.cpp b/src/dataclasses.cpp index f85bbb3f..6491549a 100644 --- a/src/dataclasses.cpp +++ b/src/dataclasses.cpp @@ -81,7 +81,7 @@ void add_module_dataclasses(VM* vm){ PyObject* mod = vm->new_module("dataclasses"); vm->bind_func<1>(mod, "dataclass", [](VM* vm, ArgsView args){ - vm->check_non_tagged_type(args[0], VM::tp_type); + vm->check_type(args[0], VM::tp_type); Type cls = PK_OBJ_GET(Type, args[0]); NameDict& cls_d = args[0]->attr(); diff --git a/src/expr.cpp b/src/expr.cpp index 132cbcac..45ad6bcf 100644 --- a/src/expr.cpp +++ b/src/expr.cpp @@ -129,7 +129,7 @@ namespace pkpy{ } int CodeEmitContext::add_const(PyObject* v){ - if(is_non_tagged_type(v, vm->tp_str)){ + if(is_type(v, vm->tp_str)){ // warning: should use add_const_string() instead return add_const_string(PK_OBJ_GET(Str, v).sv()); }else{ diff --git a/src/linalg.cpp b/src/linalg.cpp index e2ffd4c5..851a1e59 100644 --- a/src/linalg.cpp +++ b/src/linalg.cpp @@ -32,7 +32,7 @@ namespace pkpy{ #define BIND_VEC_MUL_OP(D) \ vm->bind__mul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ \ Vec##D& self = _CAST(Vec##D&, _0); \ - if(is_non_tagged_type(_1, Vec##D::_type(vm))){ \ + if(is_type(_1, Vec##D::_type(vm))){ \ Vec##D& other = _CAST(Vec##D&, _1); \ return VAR(self * other); \ } \ @@ -356,11 +356,11 @@ static Vec2 SmoothDamp(Vec2 current, Vec2 target, Vec2& currentVelocity, float s vm->bind__matmul__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ Mat3x3& self = _CAST(Mat3x3&, _0); - if(is_non_tagged_type(_1, Mat3x3::_type(vm))){ + if(is_type(_1, Mat3x3::_type(vm))){ const Mat3x3& other = _CAST(Mat3x3&, _1); return VAR_T(Mat3x3, self.matmul(other)); } - if(is_non_tagged_type(_1, Vec3::_type(vm))){ + if(is_type(_1, Vec3::_type(vm))){ const Vec3& other = _CAST(Vec3&, _1); return VAR_T(Vec3, self.matmul(other)); } diff --git a/src/modules.cpp b/src/modules.cpp index e7d1cb3f..79594b7c 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -108,7 +108,7 @@ void add_module_json(VM* vm){ PyObject* mod = vm->new_module("json"); vm->bind_func<1>(mod, "loads", [](VM* vm, ArgsView args) { std::string_view sv; - if(is_non_tagged_type(args[0], vm->tp_bytes)){ + if(is_type(args[0], vm->tp_bytes)){ sv = PK_OBJ_GET(Bytes, args[0]).sv(); }else{ sv = CAST(Str&, args[0]).sv(); @@ -270,7 +270,7 @@ struct LineProfilerW{ vm->bind(type, "add_function(self, func)", [](VM* vm, ArgsView args){ LineProfilerW& self = PK_OBJ_GET(LineProfilerW, args[0]); - vm->check_non_tagged_type(args[1], VM::tp_function); + vm->check_type(args[1], VM::tp_function); auto decl = PK_OBJ_GET(Function, args[1]).decl.get(); self.profiler.functions.insert(decl); return vm->None; diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 2750e7d5..26430833 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -15,7 +15,7 @@ PyObject* PyArrayGetItem(VM* vm, PyObject* _0, PyObject* _1){ index = vm->normalized_index(index, self.size()); return self[index]; } - if(is_non_tagged_type(_1, vm->tp_slice)){ + if(is_type(_1, vm->tp_slice)){ const Slice& s = _CAST(Slice&, _1); int start, stop, step; vm->parse_int_slice(s, self.size(), start, stop, step); @@ -90,7 +90,7 @@ void init_builtins(VM* _vm) { }else{ vm->TypeError("super() takes 0 or 2 arguments"); } - vm->check_non_tagged_type(class_arg, vm->tp_type); + vm->check_type(class_arg, vm->tp_type); Type type = PK_OBJ_GET(Type, class_arg); if(!vm->isinstance(self_arg, type)){ StrName _0 = _type_name(vm, vm->_tp(self_arg)); @@ -102,33 +102,33 @@ void init_builtins(VM* _vm) { _vm->bind_func<1>(_vm->builtins, "staticmethod", [](VM* vm, ArgsView args) { PyObject* func = args[0]; - vm->check_non_tagged_type(func, vm->tp_function); + vm->check_type(func, vm->tp_function); return vm->heap.gcnew(vm->tp_staticmethod, args[0]); }); _vm->bind_func<1>(_vm->builtins, "classmethod", [](VM* vm, ArgsView args) { PyObject* func = args[0]; - vm->check_non_tagged_type(func, vm->tp_function); + vm->check_type(func, vm->tp_function); return vm->heap.gcnew(vm->tp_classmethod, args[0]); }); _vm->bind_func<2>(_vm->builtins, "isinstance", [](VM* vm, ArgsView args) { - if(is_non_tagged_type(args[1], vm->tp_tuple)){ + if(is_type(args[1], vm->tp_tuple)){ Tuple& types = _CAST(Tuple&, args[1]); for(PyObject* type : types){ - vm->check_non_tagged_type(type, vm->tp_type); + vm->check_type(type, vm->tp_type); if(vm->isinstance(args[0], PK_OBJ_GET(Type, type))) return vm->True; } return vm->False; } - vm->check_non_tagged_type(args[1], vm->tp_type); + vm->check_type(args[1], vm->tp_type); Type type = PK_OBJ_GET(Type, args[1]); return VAR(vm->isinstance(args[0], type)); }); _vm->bind_func<2>(_vm->builtins, "issubclass", [](VM* vm, ArgsView args) { - vm->check_non_tagged_type(args[0], vm->tp_type); - vm->check_non_tagged_type(args[1], vm->tp_type); + vm->check_type(args[0], vm->tp_type); + vm->check_type(args[1], vm->tp_type); return VAR(vm->issubclass(PK_OBJ_GET(Type, args[0]), PK_OBJ_GET(Type, args[1]))); }); @@ -189,7 +189,7 @@ void init_builtins(VM* _vm) { Frame* frame = vm->top_frame(); return vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals); } - vm->check_non_tagged_type(globals, vm->tp_mappingproxy); + vm->check_type(globals, vm->tp_mappingproxy); PyObject* obj = PK_OBJ_GET(MappingProxy, globals).obj; return vm->_exec(code, obj); }); @@ -202,7 +202,7 @@ void init_builtins(VM* _vm) { vm->_exec(code.get(), frame->_module, frame->_callable, frame->_locals); return vm->None; } - vm->check_non_tagged_type(globals, vm->tp_mappingproxy); + vm->check_type(globals, vm->tp_mappingproxy); PyObject* obj = PK_OBJ_GET(MappingProxy, globals).obj; vm->_exec(code, obj); return vm->None; @@ -324,7 +324,7 @@ void init_builtins(VM* _vm) { }); _vm->cached_object__new__ = _vm->bind_constructor<1>(_vm->_t(VM::tp_object), [](VM* vm, ArgsView args) { - vm->check_non_tagged_type(args[0], vm->tp_type); + vm->check_type(args[0], vm->tp_type); Type t = PK_OBJ_GET(Type, args[0]); return vm->heap.gcnew(t); }); @@ -562,7 +562,7 @@ void init_builtins(VM* _vm) { #define BIND_CMP_STR(name, op) \ _vm->bind##name(VM::tp_str, [](VM* vm, PyObject* lhs, PyObject* rhs) { \ - if(!is_non_tagged_type(rhs, vm->tp_str)) return vm->NotImplemented; \ + if(!is_type(rhs, vm->tp_str)) return vm->NotImplemented; \ return VAR(_CAST(Str&, lhs) op _CAST(Str&, rhs)); \ }); @@ -575,7 +575,7 @@ void init_builtins(VM* _vm) { _vm->bind__getitem__(VM::tp_str, [](VM* vm, PyObject* _0, PyObject* _1) { const Str& self = PK_OBJ_GET(Str, _0); - if(is_non_tagged_type(_1, vm->tp_slice)){ + if(is_type(_1, vm->tp_slice)){ const Slice& s = _CAST(Slice&, _1); int start, stop, step; vm->parse_int_slice(s, self.u8_length(), start, stop, step); @@ -831,7 +831,7 @@ void init_builtins(VM* _vm) { _vm->bind__eq__(VM::tp_list, [](VM* vm, PyObject* _0, PyObject* _1) { List& a = _CAST(List&, _0); - if(!is_non_tagged_type(_1, vm->tp_list)) return vm->NotImplemented; + if(!is_type(_1, vm->tp_list)) return vm->NotImplemented; List& b = _CAST(List&, _1); if(a.size() != b.size()) return vm->False; for(int i=0; ibind__##name##__(_vm->_t, [](VM* vm, PyObject* lhs, PyObject* rhs){ \ - if(!is_non_tagged_type(rhs, vm->_t)) return vm->NotImplemented; \ + if(!is_type(rhs, vm->_t)) return vm->NotImplemented; \ auto& a = _CAST(_T&, lhs); \ auto& b = _CAST(_T&, rhs); \ for(int i=0; ibind__eq__(VM::tp_tuple, [](VM* vm, PyObject* _0, PyObject* _1) { const Tuple& self = _CAST(Tuple&, _0); - if(!is_non_tagged_type(_1, vm->tp_tuple)) return vm->NotImplemented; + if(!is_type(_1, vm->tp_tuple)) return vm->NotImplemented; const Tuple& other = _CAST(Tuple&, _1); if(self.size() != other.size()) return vm->False; for(int i = 0; i < self.size(); i++) { @@ -1067,7 +1067,7 @@ void init_builtins(VM* _vm) { return VAR(_CAST(bool, _0) != CAST(bool, _1)); }); _vm->bind__eq__(VM::tp_bool, [](VM* vm, PyObject* _0, PyObject* _1) { - if(is_non_tagged_type(_1, vm->tp_bool)) return VAR(_0 == _1); + if(is_type(_1, vm->tp_bool)) return VAR(_0 == _1); if(is_int(_1)) return VAR(_CAST(bool, _0) == (bool)CAST(i64, _1)); return vm->NotImplemented; }); @@ -1094,7 +1094,7 @@ void init_builtins(VM* _vm) { _vm->bind__getitem__(VM::tp_bytes, [](VM* vm, PyObject* _0, PyObject* _1) { const Bytes& self = PK_OBJ_GET(Bytes, _0); - if(is_non_tagged_type(_1, vm->tp_slice)){ + if(is_type(_1, vm->tp_slice)){ const Slice& s = _CAST(Slice&, _1); int start, stop, step; vm->parse_int_slice(s, self.size(), start, stop, step); @@ -1147,7 +1147,7 @@ void init_builtins(VM* _vm) { }); _vm->bind__eq__(VM::tp_bytes, [](VM* vm, PyObject* _0, PyObject* _1) { - if(!is_non_tagged_type(_1, vm->tp_bytes)) return vm->NotImplemented; + if(!is_type(_1, vm->tp_bytes)) return vm->NotImplemented; return VAR(_CAST(Bytes&, _0) == _CAST(Bytes&, _1)); }); @@ -1158,7 +1158,7 @@ void init_builtins(VM* _vm) { _vm->bind__eq__(VM::tp_slice, [](VM* vm, PyObject* _0, PyObject* _1){ const Slice& self = _CAST(Slice&, _0); - if(!is_non_tagged_type(_1, vm->tp_slice)) return vm->NotImplemented; + if(!is_type(_1, vm->tp_slice)) return vm->NotImplemented; const Slice& other = _CAST(Slice&, _1); if(vm->py_ne(self.start, other.start)) return vm->False; if(vm->py_ne(self.stop, other.stop)) return vm->False; @@ -1207,7 +1207,7 @@ void init_builtins(VM* _vm) { _vm->bind__eq__(VM::tp_mappingproxy, [](VM* vm, PyObject* _0, PyObject* _1){ const MappingProxy& a = _CAST(MappingProxy&, _0); - if(!is_non_tagged_type(_1, vm->tp_mappingproxy)) return vm->NotImplemented; + if(!is_type(_1, vm->tp_mappingproxy)) return vm->NotImplemented; const MappingProxy& b = _CAST(MappingProxy&, _1); return VAR(a.obj == b.obj); }); @@ -1262,12 +1262,12 @@ void init_builtins(VM* _vm) { if(args.size() == 1+1){ auto _lock = vm->heap.gc_scope_lock(); Dict& self = PK_OBJ_GET(Dict, args[0]); - if(is_non_tagged_type(args[1], vm->tp_dict)){ + if(is_type(args[1], vm->tp_dict)){ Dict& other = CAST(Dict&, args[1]); self.update(other); return vm->None; } - if(is_non_tagged_type(args[1], vm->tp_list)){ + if(is_type(args[1], vm->tp_list)){ List& list = PK_OBJ_GET(List, args[1]); for(PyObject* item : list){ Tuple& t = CAST(Tuple&, item); @@ -1563,7 +1563,7 @@ void VM::post_init(){ }); bind__eq__(tp_bound_method, [](VM* vm, PyObject* lhs, PyObject* rhs){ - if(!is_non_tagged_type(rhs, vm->tp_bound_method)) return vm->NotImplemented; + if(!is_type(rhs, vm->tp_bound_method)) return vm->NotImplemented; const BoundMethod& _0 = PK_OBJ_GET(BoundMethod, lhs); const BoundMethod& _1 = PK_OBJ_GET(BoundMethod, rhs); return VAR(_0.self == _1.self && _0.func == _1.func); diff --git a/src/pocketpy_c.cpp b/src/pocketpy_c.cpp index 22982086..1311cb9a 100644 --- a/src/pocketpy_c.cpp +++ b/src/pocketpy_c.cpp @@ -215,7 +215,7 @@ bool pkpy_is_bool(pkpy_vm* vm_handle, int i){ PK_ASSERT_NO_ERROR() PK_PROTECTED( PyObject* item = stack_item(vm, i); - return is_non_tagged_type(item, vm->tp_bool); + return is_type(item, vm->tp_bool); ) } @@ -243,7 +243,7 @@ bool pkpy_is_string(pkpy_vm* vm_handle, int i){ PK_ASSERT_NO_ERROR() PK_PROTECTED( PyObject* item = stack_item(vm, i); - return is_non_tagged_type(item, vm->tp_str); + return is_type(item, vm->tp_str); ) } @@ -272,7 +272,7 @@ bool pkpy_is_voidp(pkpy_vm* vm_handle, int i){ PK_ASSERT_NO_ERROR() PK_PROTECTED( PyObject* item = stack_item(vm, i); - return is_non_tagged_type(item, VoidP::_type(vm)); + return is_type(item, VoidP::_type(vm)); ) } diff --git a/src/vm.cpp b/src/vm.cpp index 7051cbc2..6eee7b84 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -25,7 +25,7 @@ namespace pkpy{ dict.apply([&](PyObject* k, PyObject* v){ if(!first) ss << ", "; first = false; - if(!is_non_tagged_type(k, vm->tp_str)){ + if(!is_type(k, VM::tp_str)){ vm->TypeError(_S("json keys must be string, got ", _type_name(vm, vm->_tp(k)))); } ss << _CAST(Str&, k).escape(false) << ": "; @@ -108,10 +108,10 @@ namespace pkpy{ } std::pair VM::_cast_array(PyObject* obj){ - if(is_non_tagged_type(obj, VM::tp_list)){ + if(is_type(obj, VM::tp_list)){ List& list = PK_OBJ_GET(List, obj); return {list.data(), list.size()}; - }else if(is_non_tagged_type(obj, VM::tp_tuple)){ + }else if(is_type(obj, VM::tp_tuple)){ Tuple& tuple = PK_OBJ_GET(Tuple, obj); return {tuple.data(), tuple.size()}; } @@ -760,7 +760,7 @@ void VM::init_builtin_types(){ // `heap.gc_scope_lock();` needed before calling this function void VM::_unpack_as_list(ArgsView args, List& list){ for(PyObject* obj: args){ - if(is_non_tagged_type(obj, tp_star_wrapper)){ + if(is_type(obj, tp_star_wrapper)){ const StarWrapper& w = _CAST(StarWrapper&, obj); // maybe this check should be done in the compile time if(w.level != 1) TypeError("expected level 1 star wrapper"); @@ -779,7 +779,7 @@ void VM::_unpack_as_list(ArgsView args, List& list){ // `heap.gc_scope_lock();` needed before calling this function void VM::_unpack_as_dict(ArgsView args, Dict& dict){ for(PyObject* obj: args){ - if(is_non_tagged_type(obj, tp_star_wrapper)){ + if(is_type(obj, tp_star_wrapper)){ const StarWrapper& w = _CAST(StarWrapper&, obj); // maybe this check should be done in the compile time if(w.level != 2) TypeError("expected level 2 star wrapper"); @@ -1010,7 +1010,7 @@ void VM::delattr(PyObject *_0, StrName _name){ PyObject* VM::getattr(PyObject* obj, StrName name, bool throw_err){ Type objtype(0); // handle super() proxy - if(is_non_tagged_type(obj, tp_super)){ + if(is_type(obj, tp_super)){ const Super& super = PK_OBJ_GET(Super, obj); obj = super.first; objtype = super.second; @@ -1020,7 +1020,7 @@ PyObject* VM::getattr(PyObject* obj, StrName name, bool throw_err){ PyObject* cls_var = find_name_in_mro(objtype, name); if(cls_var != nullptr){ // handle descriptor - if(is_non_tagged_type(cls_var, tp_property)){ + if(is_type(cls_var, tp_property)){ const Property& prop = PK_OBJ_GET(Property, cls_var); return call(prop.getter, obj); } @@ -1074,7 +1074,7 @@ PyObject* VM::get_unbound_method(PyObject* obj, StrName name, PyObject** self, b *self = PY_NULL; Type objtype(0); // handle super() proxy - if(is_non_tagged_type(obj, tp_super)){ + if(is_type(obj, tp_super)){ const Super& super = PK_OBJ_GET(Super, obj); obj = super.first; objtype = super.second; @@ -1086,7 +1086,7 @@ PyObject* VM::get_unbound_method(PyObject* obj, StrName name, PyObject** self, b if(fallback){ if(cls_var != nullptr){ // handle descriptor - if(is_non_tagged_type(cls_var, tp_property)){ + if(is_type(cls_var, tp_property)){ const Property& prop = PK_OBJ_GET(Property, cls_var); return call(prop.getter, obj); } @@ -1142,7 +1142,7 @@ PyObject* VM::get_unbound_method(PyObject* obj, StrName name, PyObject** self, b void VM::setattr(PyObject* obj, StrName name, PyObject* value){ Type objtype(0); // handle super() proxy - if(is_non_tagged_type(obj, tp_super)){ + if(is_type(obj, tp_super)){ Super& super = PK_OBJ_GET(Super, obj); obj = super.first; objtype = super.second; @@ -1152,7 +1152,7 @@ void VM::setattr(PyObject* obj, StrName name, PyObject* value){ PyObject* cls_var = find_name_in_mro(objtype, name); if(cls_var != nullptr){ // handle descriptor - if(is_non_tagged_type(cls_var, tp_property)){ + if(is_type(cls_var, tp_property)){ const Property& prop = _CAST(Property&, cls_var); if(prop.setter != vm->None){ call(prop.setter, obj, value);