From f3ab008f6a4bcb57074c9bd837ce4f6751dbccdc Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 26 Feb 2023 09:54:26 +0800 Subject: [PATCH] fix a bug --- src/ceval.h | 4 ++-- src/cffi.h | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/ceval.h b/src/ceval.h index 78016a70..30f141ba 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -136,8 +136,8 @@ PyVar VM::run_frame(Frame* frame){ } continue; case OP_COMPARE_OP: { pkpy::Args args(2); - args[1] = frame->pop(); - args[0] = frame->top(); + args[1] = frame->pop_value(this); + args[0] = frame->top_value(this); frame->top() = fast_call(CMP_SPECIAL_METHODS[byte.arg], std::move(args)); } continue; case OP_IS_OP: { diff --git a/src/cffi.h b/src/cffi.h index bc5c57ad..424414f5 100644 --- a/src/cffi.h +++ b/src/cffi.h @@ -25,7 +25,7 @@ struct CType{ constexpr CType kCTypes[] = { CType("char_", sizeof(char), 0), CType("int_", sizeof(int), 1), CType("float_", sizeof(float), 2), CType("double_", sizeof(double), 3), - CType("bool_", sizeof(bool), 4), CType("void_", 0, 5), + CType("bool_", sizeof(bool), 4), CType("void_", 1, 5), CType("int8_", sizeof(int8_t), 6), CType("int16_", sizeof(int16_t), 7), CType("int32_", sizeof(int32_t), 8), CType("int64_", sizeof(int64_t), 9), CType("uint8_", sizeof(uint8_t), 10), CType("uint16_", sizeof(uint16_t), 11), @@ -61,10 +61,36 @@ struct Pointer{ vm->bind_method<0>(type, "__repr__", [](VM* vm, pkpy::Args& args) { Pointer& self = vm->py_cast(args[0]); StrStream ss; - ss << "<" << self._ctype.name << "* at " << std::hex << self.ptr << ">"; + ss << "<" << self._ctype.name << "* at " << (i64)self.ptr << ">"; return vm->PyStr(ss.str()); }); + vm->bind_method<1>(type, "__add__", [](VM* vm, pkpy::Args& args) { + Pointer& self = vm->py_cast(args[0]); + i64 offset = vm->PyInt_AS_C(args[1]); + int8_t* new_ptr = (int8_t*)self.ptr + offset * self._ctype.size; + return vm->new_object((void*)new_ptr, self._ctype); + }); + + vm->bind_method<1>(type, "__sub__", [](VM* vm, pkpy::Args& args) { + Pointer& self = vm->py_cast(args[0]); + i64 offset = vm->PyInt_AS_C(args[1]); + int8_t* new_ptr = (int8_t*)self.ptr - offset * self._ctype.size; + return vm->new_object((void*)new_ptr, self._ctype); + }); + + vm->bind_method<1>(type, "__eq__", [](VM* vm, pkpy::Args& args) { + Pointer& self = vm->py_cast(args[0]); + Pointer& other = vm->py_cast(args[1]); + return vm->PyBool(self.ptr == other.ptr); + }); + + vm->bind_method<1>(type, "__ne__", [](VM* vm, pkpy::Args& args) { + Pointer& self = vm->py_cast(args[0]); + Pointer& other = vm->py_cast(args[1]); + return vm->PyBool(self.ptr != other.ptr); + }); + // https://docs.python.org/zh-cn/3/library/ctypes.html vm->bind_method<1>(type, "__getitem__", [](VM* vm, pkpy::Args& args) { Pointer& self = vm->py_cast(args[0]); @@ -133,6 +159,7 @@ void add_module_c(VM* vm){ for(int i=0; isetattr(mod, kCTypes[i].name, vm->new_object(kCTypes[i])); } + vm->setattr(mod, "nullptr", vm->new_object(nullptr, ctype_t("void_"))); vm->bind_func<1>(mod, "malloc", [](VM* vm, pkpy::Args& args) { i64 size = vm->PyInt_AS_C(args[0]);