mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
...
This commit is contained in:
parent
bb75341c49
commit
e32a907336
@ -20,6 +20,10 @@ class void_p:
|
||||
def __ne__(self, other: 'void_p') -> bool: ...
|
||||
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_uchar(self) -> int: ...
|
||||
def read_short(self) -> int: ...
|
||||
|
20
src/cffi.h
20
src/cffi.h
@ -46,13 +46,31 @@ struct VoidP{
|
||||
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){
|
||||
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){
|
||||
VoidP& self = _CAST(VoidP&, obj);
|
||||
std::stringstream ss;
|
||||
ss << "<void* at " << self.ptr;
|
||||
ss << "<void* at " << self.hex();
|
||||
if(self.base_offset != 1) ss << ", base_offset=" << self.base_offset;
|
||||
ss << ">";
|
||||
return VAR(ss.str());
|
||||
|
Loading…
x
Reference in New Issue
Block a user