mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
some fix
This commit is contained in:
parent
fb2c168a20
commit
236902c629
@ -189,7 +189,7 @@ struct PyVar final{
|
|||||||
// uninitialized
|
// uninitialized
|
||||||
PyVar() = default;
|
PyVar() = default;
|
||||||
// constexpr initialized
|
// constexpr initialized
|
||||||
constexpr PyVar(const const_sso_var&, Type type, i64 value): type(type), is_sso(true), flags(0), _0(0), _1(value) {}
|
constexpr PyVar(const const_sso_var&, Type type, int value): type(type), is_sso(true), flags(0), _0(value), _1(0) {}
|
||||||
// zero initialized
|
// zero initialized
|
||||||
constexpr PyVar(std::nullptr_t): type(0), is_sso(false), flags(0), _0(0), _1(0) {}
|
constexpr PyVar(std::nullptr_t): type(0), is_sso(false), flags(0), _0(0), _1(0) {}
|
||||||
// PyObject* initialized (is_sso = false)
|
// PyObject* initialized (is_sso = false)
|
||||||
@ -213,12 +213,14 @@ struct PyVar final{
|
|||||||
|
|
||||||
explicit operator bool() const { return (bool)type; }
|
explicit operator bool() const { return (bool)type; }
|
||||||
|
|
||||||
|
i64 _qword(int i) const { return ((const i64*)this)[i]; }
|
||||||
|
|
||||||
bool operator==(const PyVar& other) const {
|
bool operator==(const PyVar& other) const {
|
||||||
return memcmp(this, &other, sizeof(PyVar)) == 0;
|
return _qword(0) == other._qword(0) && _qword(1) == other._qword(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const PyVar& other) const {
|
bool operator!=(const PyVar& other) const {
|
||||||
return memcmp(this, &other, sizeof(PyVar)) != 0;
|
return _qword(0) != other._qword(0) || _qword(1) != other._qword(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(std::nullptr_t) const { return !(bool)type; }
|
bool operator==(std::nullptr_t) const { return !(bool)type; }
|
||||||
|
@ -205,7 +205,10 @@ public:
|
|||||||
|
|
||||||
List py_list(PyVar); // x -> list(x)
|
List py_list(PyVar); // x -> list(x)
|
||||||
bool py_callable(PyVar obj); // x -> callable(x)
|
bool py_callable(PyVar obj); // x -> callable(x)
|
||||||
bool py_bool(PyVar obj); // x -> bool(x)
|
bool py_bool(PyVar obj){ // x -> bool(x)
|
||||||
|
if(obj.type == tp_bool) return obj._0;
|
||||||
|
return __py_bool_non_trivial(obj);
|
||||||
|
}
|
||||||
i64 py_hash(PyVar obj); // x -> hash(x)
|
i64 py_hash(PyVar obj); // x -> hash(x)
|
||||||
|
|
||||||
bool py_eq(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs == rhs
|
bool py_eq(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs == rhs
|
||||||
@ -449,6 +452,7 @@ public:
|
|||||||
void __push_varargs(PyVar _0, PyVar _1, PyVar _2, PyVar _3){ PUSH(_0); PUSH(_1); PUSH(_2); PUSH(_3); }
|
void __push_varargs(PyVar _0, PyVar _1, PyVar _2, PyVar _3){ PUSH(_0); PUSH(_1); PUSH(_2); PUSH(_3); }
|
||||||
PyVar __pack_next_retval(unsigned);
|
PyVar __pack_next_retval(unsigned);
|
||||||
PyVar __minmax_reduce(bool (VM::*op)(PyVar, PyVar), PyVar args, PyVar key);
|
PyVar __minmax_reduce(bool (VM::*op)(PyVar, PyVar), PyVar args, PyVar key);
|
||||||
|
bool __py_bool_non_trivial(PyVar);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -504,11 +508,17 @@ PyVar py_var(VM* vm, __T&& value){
|
|||||||
if constexpr((bool)const_type){
|
if constexpr((bool)const_type){
|
||||||
if constexpr(is_sso_v<T>) return PyVar(const_type, value);
|
if constexpr(is_sso_v<T>) return PyVar(const_type, value);
|
||||||
else return vm->heap.gcnew<T>(const_type, std::forward<__T>(value));
|
else return vm->heap.gcnew<T>(const_type, std::forward<__T>(value));
|
||||||
|
}else{
|
||||||
|
Type type = vm->_find_type_in_cxx_typeid_map<T>();
|
||||||
|
if constexpr(is_sso_v<T>) return PyVar(type, value);
|
||||||
|
else return vm->heap.gcnew<T>(type, std::forward<__T>(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Type type = vm->_find_type_in_cxx_typeid_map<T>();
|
}
|
||||||
if constexpr(is_sso_v<T>) return PyVar(type, value);
|
|
||||||
else return vm->heap.gcnew<T>(type, std::forward<__T>(value));
|
// fast path for bool if py_var<> cannot be inlined
|
||||||
|
inline PyVar py_var(VM* vm, bool value){
|
||||||
|
return value ? vm->True : vm->False;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename __T, bool with_check>
|
template<typename __T, bool with_check>
|
||||||
@ -530,18 +540,16 @@ __T _py_cast__internal(VM* vm, PyVar obj) {
|
|||||||
if(obj == vm->True) return true;
|
if(obj == vm->True) return true;
|
||||||
if(obj == vm->False) return false;
|
if(obj == vm->False) return false;
|
||||||
vm->TypeError("expected 'bool', got " + _type_name(vm, vm->_tp(obj)).escape());
|
vm->TypeError("expected 'bool', got " + _type_name(vm, vm->_tp(obj)).escape());
|
||||||
}else{
|
|
||||||
return obj == vm->True;
|
|
||||||
}
|
}
|
||||||
|
return obj == vm->True;
|
||||||
}else if constexpr(is_integral_v<T>){
|
}else if constexpr(is_integral_v<T>){
|
||||||
static_assert(!std::is_reference_v<__T>);
|
static_assert(!std::is_reference_v<__T>);
|
||||||
// int
|
// int
|
||||||
if constexpr(with_check){
|
if constexpr(with_check){
|
||||||
if(is_int(obj)) return (T)obj.as<i64>();
|
if(is_int(obj)) return (T)obj.as<i64>();
|
||||||
vm->TypeError("expected 'int', got " + _type_name(vm, vm->_tp(obj)).escape());
|
vm->TypeError("expected 'int', got " + _type_name(vm, vm->_tp(obj)).escape());
|
||||||
}else{
|
|
||||||
return (T)obj.as<i64>();
|
|
||||||
}
|
}
|
||||||
|
return (T)obj.as<i64>();
|
||||||
}else if constexpr(is_floating_point_v<T>){
|
}else if constexpr(is_floating_point_v<T>){
|
||||||
static_assert(!std::is_reference_v<__T>);
|
static_assert(!std::is_reference_v<__T>);
|
||||||
// float
|
// float
|
||||||
@ -549,6 +557,7 @@ __T _py_cast__internal(VM* vm, PyVar obj) {
|
|||||||
i64 bits;
|
i64 bits;
|
||||||
if(try_cast_int(obj, &bits)) return (T)bits;
|
if(try_cast_int(obj, &bits)) return (T)bits;
|
||||||
vm->TypeError("expected 'int' or 'float', got " + _type_name(vm, vm->_tp(obj)).escape());
|
vm->TypeError("expected 'int' or 'float', got " + _type_name(vm, vm->_tp(obj)).escape());
|
||||||
|
return 0.0f;
|
||||||
}else if constexpr(std::is_enum_v<T>){
|
}else if constexpr(std::is_enum_v<T>){
|
||||||
static_assert(!std::is_reference_v<__T>);
|
static_assert(!std::is_reference_v<__T>);
|
||||||
return (__T)_py_cast__internal<i64, with_check>(vm, obj);
|
return (__T)_py_cast__internal<i64, with_check>(vm, obj);
|
||||||
@ -567,13 +576,14 @@ __T _py_cast__internal(VM* vm, PyVar obj) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return PK_OBJ_GET(T, obj);
|
return PK_OBJ_GET(T, obj);
|
||||||
|
}else{
|
||||||
|
if constexpr(with_check){
|
||||||
|
Type type = vm->_find_type_in_cxx_typeid_map<T>();
|
||||||
|
vm->check_compatible_type(obj, type);
|
||||||
|
}
|
||||||
|
return PK_OBJ_GET(T, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if constexpr(with_check){
|
|
||||||
Type type = vm->_find_type_in_cxx_typeid_map<T>();
|
|
||||||
vm->check_compatible_type(obj, type);
|
|
||||||
}
|
|
||||||
return PK_OBJ_GET(T, obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename __T>
|
template<typename __T>
|
||||||
|
@ -417,9 +417,7 @@ PyVar VM::py_negate(PyVar obj){
|
|||||||
return call_method(obj, __neg__);
|
return call_method(obj, __neg__);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VM::py_bool(PyVar obj){
|
bool VM::__py_bool_non_trivial(PyVar obj){
|
||||||
if(obj == vm->True) return true;
|
|
||||||
if(obj == vm->False) return false;
|
|
||||||
if(obj == None) return false;
|
if(obj == None) return false;
|
||||||
if(is_int(obj)) return _CAST(i64, obj) != 0;
|
if(is_int(obj)) return _CAST(i64, obj) != 0;
|
||||||
if(is_float(obj)) return _CAST(f64, obj) != 0.0;
|
if(is_float(obj)) return _CAST(f64, obj) != 0.0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user