From cb094331e782d2c15b5deae8e027845f44bf7071 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 15 Mar 2023 21:26:20 +0800 Subject: [PATCH] fix a bug --- src/cffi.h | 15 +++++++++++++++ src/vm.h | 56 +++++++++++++++++++++++++++++------------------------- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/cffi.h b/src/cffi.h index ea5ab381..131e2ef0 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -309,6 +309,16 @@ struct Value { this->head = Pointer(type, data); } + Value(Value&& other) noexcept { + data = other.data; + head = other.head; + other.data = nullptr; + } + + Value& operator=(Value&& other) noexcept = delete; + Value& operator=(const Value& other) = delete; + Value(const Value& other) = delete; + static void _register(VM* vm, PyVar mod, PyVar type){ vm->bind_static_method<-1>(type, "__new__", CPP_NOT_IMPLEMENTED()); @@ -323,6 +333,10 @@ struct Value { return self.head._to(vm, name).get(vm); }); } + + ~Value(){ + delete[] data; + } }; @@ -459,6 +473,7 @@ template std::enable_if_t>, PyVar> py_var(VM* vm, T p){ const TypeInfo* type = _type_db.get::baseT>(); + if(type == nullptr) type = _type_db.get(); return VAR_T(Pointer, type, pointer::level, (char*)p); } diff --git a/src/vm.h b/src/vm.h index a97d8135..0b9f4541 100644 --- a/src/vm.h +++ b/src/vm.h @@ -380,33 +380,27 @@ DEF_NATIVE_2(Slice, tp_slice) DEF_NATIVE_2(Exception, tp_exception) DEF_NATIVE_2(StarWrapper, tp_star_wrapper) -template<> i64 py_cast(VM* vm, const PyVar& obj){ - vm->check_type(obj, vm->tp_int); - return obj.bits >> 2; -} -template<> i64 _py_cast(VM* vm, const PyVar& obj){ - return obj.bits >> 2; -} -template<> f64 py_cast(VM* vm, const PyVar& obj){ - vm->check_type(obj, vm->tp_float); - i64 bits = obj.bits; - bits = (bits >> 2) << 2; - return __8B(bits)._float; -} -template<> f64 _py_cast(VM* vm, const PyVar& obj){ - i64 bits = obj.bits; - bits = (bits >> 2) << 2; - return __8B(bits)._float; +#define PY_CAST_INT(T) \ +template<> T py_cast(VM* vm, const PyVar& obj){ \ + vm->check_type(obj, vm->tp_int); \ + return (T)(obj.bits >> 2); \ +} \ +template<> T _py_cast(VM* vm, const PyVar& obj){ \ + return (T)(obj.bits >> 2); \ } -#ifndef PKPY_USE_32_BITS -template<> int py_cast(VM* vm, const PyVar& obj){ - vm->check_type(obj, vm->tp_int); - return obj.bits >> 2; -} -template<> int _py_cast(VM* vm, const PyVar& obj){ - return obj.bits >> 2; -} +PY_CAST_INT(char) +PY_CAST_INT(short) +PY_CAST_INT(int) +PY_CAST_INT(long) +PY_CAST_INT(long long) +PY_CAST_INT(unsigned char) +PY_CAST_INT(unsigned short) +PY_CAST_INT(unsigned int) +PY_CAST_INT(unsigned long) +PY_CAST_INT(unsigned long long) + + template<> float py_cast(VM* vm, const PyVar& obj){ vm->check_type(obj, vm->tp_float); i64 bits = obj.bits; @@ -418,7 +412,17 @@ template<> float _py_cast(VM* vm, const PyVar& obj){ bits = (bits >> 2) << 2; return __8B(bits)._float; } -#endif +template<> double py_cast(VM* vm, const PyVar& obj){ + vm->check_type(obj, vm->tp_float); + i64 bits = obj.bits; + bits = (bits >> 2) << 2; + return __8B(bits)._float; +} +template<> double _py_cast(VM* vm, const PyVar& obj){ + i64 bits = obj.bits; + bits = (bits >> 2) << 2; + return __8B(bits)._float; +} #define PY_VAR_INT(T) \