This commit is contained in:
blueloveTH 2023-09-27 06:54:44 +08:00
parent 5a42d35f9d
commit 39706325dd
7 changed files with 9 additions and 54 deletions

View File

@ -37,7 +37,6 @@ struct VoidP{
void* ptr;
VoidP(void* ptr): ptr(ptr){}
VoidP(): ptr(nullptr){}
bool operator==(const VoidP& other) const {
return ptr == other.ptr;

View File

@ -234,7 +234,7 @@ struct Mat3x3{
bool is_affine() const{
float det = _11 * _22 - _12 * _21;
if(!isclose(det, 0)) return false;
if(isclose(det, 0)) return false;
return _31 == 0.0f && _32 == 0.0f && _33 == 1.0f;
}

View File

@ -7,14 +7,13 @@ def memset(ptr: 'void_p', value: int, size: int) -> None: ...
def memcpy(dst: 'void_p', src: 'void_p', size: int) -> None: ...
class void_p:
def __init__(self, addr: int): ...
def __add__(self, i: int) -> 'void_p': ...
def __sub__(self, i: int) -> 'void_p': ...
def __eq__(self, other: 'void_p') -> bool: ...
def __ne__(self, other: 'void_p') -> bool: ...
def hex(self) -> str: ...
@staticmethod
def from_hex(s: str) -> 'void_p': ...
def read_char(self) -> int: ...
def read_uchar(self) -> int: ...

View File

@ -3,15 +3,12 @@
namespace pkpy{
void VoidP::_register(VM* vm, PyObject* mod, PyObject* type){
vm->bind_default_constructor<VoidP>(type);
vm->bind_func<1>(type, "from_hex", [](VM* vm, ArgsView args){
std::string s = CAST(Str&, args[0]).str();
size_t size;
intptr_t ptr = std::stoll(s, &size, 16);
if(size != s.size()) vm->ValueError("invalid literal for void_p(): " + s);
return VAR_T(VoidP, (void*)ptr);
vm->bind_constructor<2>(type, [](VM* vm, ArgsView args){
Type cls = PK_OBJ_GET(Type, args[0]);
i64 addr = CAST(i64, args[1]);
return vm->heap.gcnew<VoidP>(cls, reinterpret_cast<void*>(addr));
});
vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){
VoidP& self = _CAST(VoidP&, args[0]);
return VAR(self.hex());

View File

@ -109,11 +109,3 @@ class B(A):
# assert B.a == 1 ...bug here
assert B.b == 3
assert B.c == 4
import c
class A(c.void_p):
pass
a = A()
assert repr(a).startswith('<void* at')

View File

@ -115,5 +115,5 @@ import c
class A(c.void_p):
pass
a = A()
a = A(0)
assert repr(a).startswith('<void* at')

View File

@ -1,38 +1,6 @@
import c
assert c.void_p.from_hex('0x2568b60').hex() == '0x2568b60'
# ------------------------------------------------
class HexAddress:
def __init__(self, address):
if not address.startswith("0x"): # 确保地址以0x开头
raise ValueError("Address should start with '0x'.")
self.address = address[2:] # 去除0x前缀并保存十六进制字符串
def __str__(self):
return "0x" + self.address
def __add__(self, other):
if isinstance(other, int):
return HexAddress(hex(int(self.address, 16) + other)) # 将字符串地址转为整数进行运算
elif isinstance(other, HexAddress):
return HexAddress(hex(int(self.address, 16) + int(other.address, 16))) # 将字符串地址转为整数进行运算
else:
raise TypeError("Unsupported operand type for +: HexAddress and {}".format(type(other)))
def __sub__(self, other):
if isinstance(other, int):
return HexAddress(hex(int(self.address, 16) - other)) # 将字符串地址转为整数进行运算
elif isinstance(other, HexAddress):
return HexAddress(hex(int(self.address, 16) - int(other.address, 16))) # 将字符串地址转为整数进行运算
else:
raise TypeError("Unsupported operand type for -: HexAddress and {}".format(type(other)))
c_void_1 = c.malloc(8)
assert (c_void_1 + 8).hex() == c.void_p.from_hex(str(HexAddress(c_void_1.hex()) + 8)).hex()
assert (c_void_1 - 8).hex() == c.void_p.from_hex(str(HexAddress(c_void_1.hex()) - 8)).hex()
assert c.NULL == c.void_p(0)
# ------------------------------------------------
# 此处测试并不完全
c_void_1 = c.malloc(8)