mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 20:10:17 +00:00
fix a bug
This commit is contained in:
parent
6ba441a5fe
commit
f3ab008f6a
@ -136,8 +136,8 @@ PyVar VM::run_frame(Frame* frame){
|
|||||||
} continue;
|
} continue;
|
||||||
case OP_COMPARE_OP: {
|
case OP_COMPARE_OP: {
|
||||||
pkpy::Args args(2);
|
pkpy::Args args(2);
|
||||||
args[1] = frame->pop();
|
args[1] = frame->pop_value(this);
|
||||||
args[0] = frame->top();
|
args[0] = frame->top_value(this);
|
||||||
frame->top() = fast_call(CMP_SPECIAL_METHODS[byte.arg], std::move(args));
|
frame->top() = fast_call(CMP_SPECIAL_METHODS[byte.arg], std::move(args));
|
||||||
} continue;
|
} continue;
|
||||||
case OP_IS_OP: {
|
case OP_IS_OP: {
|
||||||
|
31
src/cffi.h
31
src/cffi.h
@ -25,7 +25,7 @@ struct CType{
|
|||||||
constexpr CType kCTypes[] = {
|
constexpr CType kCTypes[] = {
|
||||||
CType("char_", sizeof(char), 0), CType("int_", sizeof(int), 1),
|
CType("char_", sizeof(char), 0), CType("int_", sizeof(int), 1),
|
||||||
CType("float_", sizeof(float), 2), CType("double_", sizeof(double), 3),
|
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("int8_", sizeof(int8_t), 6), CType("int16_", sizeof(int16_t), 7),
|
||||||
CType("int32_", sizeof(int32_t), 8), CType("int64_", sizeof(int64_t), 9),
|
CType("int32_", sizeof(int32_t), 8), CType("int64_", sizeof(int64_t), 9),
|
||||||
CType("uint8_", sizeof(uint8_t), 10), CType("uint16_", sizeof(uint16_t), 11),
|
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) {
|
vm->bind_method<0>(type, "__repr__", [](VM* vm, pkpy::Args& args) {
|
||||||
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
||||||
StrStream ss;
|
StrStream ss;
|
||||||
ss << "<" << self._ctype.name << "* at " << std::hex << self.ptr << ">";
|
ss << "<" << self._ctype.name << "* at " << (i64)self.ptr << ">";
|
||||||
return vm->PyStr(ss.str());
|
return vm->PyStr(ss.str());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
vm->bind_method<1>(type, "__add__", [](VM* vm, pkpy::Args& args) {
|
||||||
|
Pointer& self = vm->py_cast<Pointer>(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<Pointer>((void*)new_ptr, self._ctype);
|
||||||
|
});
|
||||||
|
|
||||||
|
vm->bind_method<1>(type, "__sub__", [](VM* vm, pkpy::Args& args) {
|
||||||
|
Pointer& self = vm->py_cast<Pointer>(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<Pointer>((void*)new_ptr, self._ctype);
|
||||||
|
});
|
||||||
|
|
||||||
|
vm->bind_method<1>(type, "__eq__", [](VM* vm, pkpy::Args& args) {
|
||||||
|
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
||||||
|
Pointer& other = vm->py_cast<Pointer>(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<Pointer>(args[0]);
|
||||||
|
Pointer& other = vm->py_cast<Pointer>(args[1]);
|
||||||
|
return vm->PyBool(self.ptr != other.ptr);
|
||||||
|
});
|
||||||
|
|
||||||
// https://docs.python.org/zh-cn/3/library/ctypes.html
|
// https://docs.python.org/zh-cn/3/library/ctypes.html
|
||||||
vm->bind_method<1>(type, "__getitem__", [](VM* vm, pkpy::Args& args) {
|
vm->bind_method<1>(type, "__getitem__", [](VM* vm, pkpy::Args& args) {
|
||||||
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
Pointer& self = vm->py_cast<Pointer>(args[0]);
|
||||||
@ -133,6 +159,7 @@ void add_module_c(VM* vm){
|
|||||||
for(int i=0; i<kCTypeCount; i++){
|
for(int i=0; i<kCTypeCount; i++){
|
||||||
vm->setattr(mod, kCTypes[i].name, vm->new_object<CType>(kCTypes[i]));
|
vm->setattr(mod, kCTypes[i].name, vm->new_object<CType>(kCTypes[i]));
|
||||||
}
|
}
|
||||||
|
vm->setattr(mod, "nullptr", vm->new_object<Pointer>(nullptr, ctype_t("void_")));
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "malloc", [](VM* vm, pkpy::Args& args) {
|
vm->bind_func<1>(mod, "malloc", [](VM* vm, pkpy::Args& args) {
|
||||||
i64 size = vm->PyInt_AS_C(args[0]);
|
i64 size = vm->PyInt_AS_C(args[0]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user