This commit is contained in:
blueloveTH 2023-05-27 15:01:32 +08:00
parent bb75341c49
commit e32a907336
2 changed files with 23 additions and 1 deletions

View File

@ -20,6 +20,10 @@ class void_p:
def __ne__(self, other: 'void_p') -> bool: ... def __ne__(self, other: 'void_p') -> bool: ...
def offset(self, i: int) -> 'void_p': ... def offset(self, i: int) -> 'void_p': ...
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: ...
def read_short(self) -> int: ... def read_short(self) -> int: ...

View File

@ -46,13 +46,31 @@ struct VoidP{
return ptr != other.ptr || base_offset != other.base_offset; return ptr != other.ptr || base_offset != other.base_offset;
} }
Str hex() const{
std::stringstream ss;
ss << std::hex << reinterpret_cast<intptr_t>(ptr);
return "0x" + ss.str();
}
static void _register(VM* vm, PyObject* mod, PyObject* type){ static void _register(VM* vm, PyObject* mod, PyObject* type){
vm->bind_default_constructor<VoidP>(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_method<0>(type, "hex", [](VM* vm, ArgsView args){
VoidP& self = _CAST(VoidP&, args[0]);
return VAR(self.hex());
});
vm->bind__repr__(OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ vm->bind__repr__(OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
VoidP& self = _CAST(VoidP&, obj); VoidP& self = _CAST(VoidP&, obj);
std::stringstream ss; std::stringstream ss;
ss << "<void* at " << self.ptr; ss << "<void* at " << self.hex();
if(self.base_offset != 1) ss << ", base_offset=" << self.base_offset; if(self.base_offset != 1) ss << ", base_offset=" << self.base_offset;
ss << ">"; ss << ">";
return VAR(ss.str()); return VAR(ss.str());