mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
...
This commit is contained in:
parent
5a42d35f9d
commit
39706325dd
@ -37,7 +37,6 @@ struct VoidP{
|
|||||||
|
|
||||||
void* ptr;
|
void* ptr;
|
||||||
VoidP(void* ptr): ptr(ptr){}
|
VoidP(void* ptr): ptr(ptr){}
|
||||||
VoidP(): ptr(nullptr){}
|
|
||||||
|
|
||||||
bool operator==(const VoidP& other) const {
|
bool operator==(const VoidP& other) const {
|
||||||
return ptr == other.ptr;
|
return ptr == other.ptr;
|
||||||
|
@ -234,7 +234,7 @@ struct Mat3x3{
|
|||||||
|
|
||||||
bool is_affine() const{
|
bool is_affine() const{
|
||||||
float det = _11 * _22 - _12 * _21;
|
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;
|
return _31 == 0.0f && _32 == 0.0f && _33 == 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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: ...
|
def memcpy(dst: 'void_p', src: 'void_p', size: int) -> None: ...
|
||||||
|
|
||||||
class void_p:
|
class void_p:
|
||||||
|
def __init__(self, addr: int): ...
|
||||||
def __add__(self, i: int) -> 'void_p': ...
|
def __add__(self, i: int) -> 'void_p': ...
|
||||||
def __sub__(self, i: int) -> 'void_p': ...
|
def __sub__(self, i: int) -> 'void_p': ...
|
||||||
def __eq__(self, other: 'void_p') -> bool: ...
|
def __eq__(self, other: 'void_p') -> bool: ...
|
||||||
def __ne__(self, other: 'void_p') -> bool: ...
|
def __ne__(self, other: 'void_p') -> bool: ...
|
||||||
|
|
||||||
def hex(self) -> str: ...
|
def hex(self) -> str: ...
|
||||||
@staticmethod
|
|
||||||
def from_hex(s: str) -> 'void_p': ...
|
|
||||||
|
|
||||||
def read_char(self) -> int: ...
|
def read_char(self) -> int: ...
|
||||||
def read_uchar(self) -> int: ...
|
def read_uchar(self) -> int: ...
|
||||||
|
13
src/cffi.cpp
13
src/cffi.cpp
@ -3,15 +3,12 @@
|
|||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
void VoidP::_register(VM* vm, PyObject* mod, PyObject* type){
|
void VoidP::_register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
vm->bind_default_constructor<VoidP>(type);
|
vm->bind_constructor<2>(type, [](VM* vm, ArgsView args){
|
||||||
|
Type cls = PK_OBJ_GET(Type, args[0]);
|
||||||
vm->bind_func<1>(type, "from_hex", [](VM* vm, ArgsView args){
|
i64 addr = CAST(i64, args[1]);
|
||||||
std::string s = CAST(Str&, args[0]).str();
|
return vm->heap.gcnew<VoidP>(cls, reinterpret_cast<void*>(addr));
|
||||||
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_method<0>(type, "hex", [](VM* vm, ArgsView args){
|
vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){
|
||||||
VoidP& self = _CAST(VoidP&, args[0]);
|
VoidP& self = _CAST(VoidP&, args[0]);
|
||||||
return VAR(self.hex());
|
return VAR(self.hex());
|
||||||
|
@ -109,11 +109,3 @@ class B(A):
|
|||||||
# assert B.a == 1 ...bug here
|
# assert B.a == 1 ...bug here
|
||||||
assert B.b == 3
|
assert B.b == 3
|
||||||
assert B.c == 4
|
assert B.c == 4
|
||||||
|
|
||||||
import c
|
|
||||||
|
|
||||||
class A(c.void_p):
|
|
||||||
pass
|
|
||||||
|
|
||||||
a = A()
|
|
||||||
assert repr(a).startswith('<void* at')
|
|
||||||
|
@ -115,5 +115,5 @@ import c
|
|||||||
class A(c.void_p):
|
class A(c.void_p):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
a = A()
|
a = A(0)
|
||||||
assert repr(a).startswith('<void* at')
|
assert repr(a).startswith('<void* at')
|
||||||
|
@ -1,38 +1,6 @@
|
|||||||
import c
|
import c
|
||||||
|
|
||||||
assert c.void_p.from_hex('0x2568b60').hex() == '0x2568b60'
|
assert c.NULL == c.void_p(0)
|
||||||
|
|
||||||
# ------------------------------------------------
|
|
||||||
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()
|
|
||||||
|
|
||||||
# ------------------------------------------------
|
# ------------------------------------------------
|
||||||
# 此处测试并不完全
|
# 此处测试并不完全
|
||||||
c_void_1 = c.malloc(8)
|
c_void_1 = c.malloc(8)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user