From 7ebf5f1e88ebefd70860787f6f7b8cdf68aefef8 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 26 Feb 2023 02:07:30 +0800 Subject: [PATCH] fix a bug --- src/ceval.h | 4 ++-- src/cffi.h | 3 ++- src/pocketpy.h | 2 +- src/vm.h | 8 ++++++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/ceval.h b/src/ceval.h index 95025910..971674c3 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -158,10 +158,10 @@ PyVar VM::run_frame(Frame* frame){ case OP_UNARY_NOT: { PyVar obj = frame->pop_value(this); const PyVar& obj_bool = asBool(obj); - frame->push(PyBool(!PyBool_AS_C(obj_bool))); + frame->push(PyBool(!_PyBool_AS_C(obj_bool))); } continue; case OP_POP_JUMP_IF_FALSE: - if(!PyBool_AS_C(asBool(frame->pop_value(this)))) frame->jump_abs(byte.arg); + if(!_PyBool_AS_C(asBool(frame->pop_value(this)))) frame->jump_abs(byte.arg); continue; case OP_LOAD_NONE: frame->push(None); continue; case OP_LOAD_TRUE: frame->push(True); continue; diff --git a/src/cffi.h b/src/cffi.h index 3aec4b50..1f1e1e5a 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -77,7 +77,7 @@ struct Pointer{ case ctype("float64"): return vm->PyFloat(((double*)self.ptr)[index]); case ctype("bool8"): return vm->PyBool(((bool*)self.ptr)[index]); case ctype("void"): vm->TypeError("cannot index void*"); - default: vm->TypeError("unsupported type"); + default: UNREACHABLE(); } return vm->None; }); @@ -98,6 +98,7 @@ struct Pointer{ case ctype("float64"): ((double*)self.ptr)[index] = vm->PyFloat_AS_C(args[2]); break; case ctype("bool8"): ((bool*)self.ptr)[index] = vm->PyBool_AS_C(args[2]); break; case ctype("void"): vm->TypeError("cannot index void*"); + default: UNREACHABLE(); } return vm->None; }); diff --git a/src/pocketpy.h b/src/pocketpy.h index 82e7942b..353de01b 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -210,7 +210,7 @@ void init_builtins(VM* _vm) { _vm->bind_static_method<1>("int", "__new__", [](VM* vm, pkpy::Args& args) { if (is_type(args[0], vm->tp_int)) return args[0]; if (is_type(args[0], vm->tp_float)) return vm->PyInt((i64)vm->PyFloat_AS_C(args[0])); - if (is_type(args[0], vm->tp_bool)) return vm->PyInt(vm->PyBool_AS_C(args[0]) ? 1 : 0); + if (is_type(args[0], vm->tp_bool)) return vm->PyInt(vm->_PyBool_AS_C(args[0]) ? 1 : 0); if (is_type(args[0], vm->tp_str)) { const Str& s = vm->PyStr_AS_C(args[0]); try{ diff --git a/src/vm.h b/src/vm.h index 8bac1515..2928193b 100644 --- a/src/vm.h +++ b/src/vm.h @@ -607,7 +607,11 @@ public: DEF_NATIVE(StarWrapper, pkpy::StarWrapper, tp_star_wrapper) // there is only one True/False, so no need to copy them! - inline bool PyBool_AS_C(const PyVar& obj){return obj == True;} + inline bool PyBool_AS_C(const PyVar& obj){ + check_type(obj, tp_bool); + return obj == True; + } + inline bool _PyBool_AS_C(const PyVar& obj){ return obj == True; } inline const PyVar& PyBool(bool value){return value ? True : False;} void init_builtin_types(){ @@ -684,7 +688,7 @@ public: return x; } if (is_type(obj, tp_type)) return obj.bits; - if (is_type(obj, tp_bool)) return PyBool_AS_C(obj) ? 1 : 0; + if (is_type(obj, tp_bool)) return _PyBool_AS_C(obj) ? 1 : 0; if (is_float(obj)){ f64 val = PyFloat_AS_C(obj); return (i64)std::hash()(val);