fix a bug

This commit is contained in:
blueloveTH 2023-02-26 02:07:30 +08:00
parent 27ad4a047d
commit 7ebf5f1e88
4 changed files with 11 additions and 6 deletions

View File

@ -158,10 +158,10 @@ PyVar VM::run_frame(Frame* frame){
case OP_UNARY_NOT: { case OP_UNARY_NOT: {
PyVar obj = frame->pop_value(this); PyVar obj = frame->pop_value(this);
const PyVar& obj_bool = asBool(obj); const PyVar& obj_bool = asBool(obj);
frame->push(PyBool(!PyBool_AS_C(obj_bool))); frame->push(PyBool(!_PyBool_AS_C(obj_bool)));
} continue; } continue;
case OP_POP_JUMP_IF_FALSE: 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; continue;
case OP_LOAD_NONE: frame->push(None); continue; case OP_LOAD_NONE: frame->push(None); continue;
case OP_LOAD_TRUE: frame->push(True); continue; case OP_LOAD_TRUE: frame->push(True); continue;

View File

@ -77,7 +77,7 @@ struct Pointer{
case ctype("float64"): return vm->PyFloat(((double*)self.ptr)[index]); case ctype("float64"): return vm->PyFloat(((double*)self.ptr)[index]);
case ctype("bool8"): return vm->PyBool(((bool*)self.ptr)[index]); case ctype("bool8"): return vm->PyBool(((bool*)self.ptr)[index]);
case ctype("void"): vm->TypeError("cannot index void*"); case ctype("void"): vm->TypeError("cannot index void*");
default: vm->TypeError("unsupported type"); default: UNREACHABLE();
} }
return vm->None; 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("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("bool8"): ((bool*)self.ptr)[index] = vm->PyBool_AS_C(args[2]); break;
case ctype("void"): vm->TypeError("cannot index void*"); case ctype("void"): vm->TypeError("cannot index void*");
default: UNREACHABLE();
} }
return vm->None; return vm->None;
}); });

View File

@ -210,7 +210,7 @@ void init_builtins(VM* _vm) {
_vm->bind_static_method<1>("int", "__new__", [](VM* vm, pkpy::Args& args) { _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_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_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)) { if (is_type(args[0], vm->tp_str)) {
const Str& s = vm->PyStr_AS_C(args[0]); const Str& s = vm->PyStr_AS_C(args[0]);
try{ try{

View File

@ -607,7 +607,11 @@ public:
DEF_NATIVE(StarWrapper, pkpy::StarWrapper, tp_star_wrapper) DEF_NATIVE(StarWrapper, pkpy::StarWrapper, tp_star_wrapper)
// there is only one True/False, so no need to copy them! // 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;} inline const PyVar& PyBool(bool value){return value ? True : False;}
void init_builtin_types(){ void init_builtin_types(){
@ -684,7 +688,7 @@ public:
return x; return x;
} }
if (is_type(obj, tp_type)) return obj.bits; 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)){ if (is_float(obj)){
f64 val = PyFloat_AS_C(obj); f64 val = PyFloat_AS_C(obj);
return (i64)std::hash<f64>()(val); return (i64)std::hash<f64>()(val);