This commit is contained in:
blueloveTH 2024-05-19 17:42:20 +08:00
parent 2d3abde352
commit 79ae2cdc28
3 changed files with 28 additions and 26 deletions

View File

@ -380,6 +380,8 @@ public:
const PyTypeInfo* _tp_info(Type type) { return &_all_types[type]; } const PyTypeInfo* _tp_info(Type type) { return &_all_types[type]; }
PyVar _t(PyVar obj){ return _all_types[_tp(obj)].obj; } PyVar _t(PyVar obj){ return _all_types[_tp(obj)].obj; }
PyVar _t(Type type){ return _all_types[type].obj; } PyVar _t(Type type){ return _all_types[type].obj; }
static bool is_not_implemented(PyVar obj){ return obj.type == tp_not_implemented; }
#endif #endif
#if PK_REGION("User Type Registration") #if PK_REGION("User Type Registration")

View File

@ -27,12 +27,12 @@ namespace pkpy{
if(_2 != nullptr) ret = call_method(self, _2, _1); \ if(_2 != nullptr) ret = call_method(self, _2, _1); \
else ret = NotImplemented; \ else ret = NotImplemented; \
} \ } \
if(ret == NotImplemented){ \ if(is_not_implemented(ret)){ \
PyVar self; \ PyVar self; \
PyVar _2 = get_unbound_method(_1, rfunc, &self, false); \ PyVar _2 = get_unbound_method(_1, rfunc, &self, false); \
if(_2 != nullptr) ret = call_method(self, _2, _0); \ if(_2 != nullptr) ret = call_method(self, _2, _0); \
else BinaryOptError(op, _0, _1); \ else BinaryOptError(op, _0, _1); \
if(ret == NotImplemented) BinaryOptError(op, _0, _1); \ if(is_not_implemented(ret)) BinaryOptError(op, _0, _1); \
} }
@ -454,23 +454,23 @@ __NEXT_STEP:
} DISPATCH() } DISPATCH()
/*****************************************/ /*****************************************/
#define BINARY_OP_SPECIAL(func) \ #define BINARY_OP_SPECIAL(func) \
_ti = _tp_info(_0); \ _ti = _tp_info(_0); \
if(_ti->m##func){ \ if(_ti->m##func){ \
TOP() = _ti->m##func(this, _0, _1); \ TOP() = _ti->m##func(this, _0, _1); \
}else{ \ }else{ \
PyVar self; \ PyVar self; \
PyVar _2 = get_unbound_method(_0, func, &self, false); \ PyVar _2 = get_unbound_method(_0, func, &self, false); \
if(_2 != nullptr) TOP() = call_method(self, _2, _1); \ if(_2 != nullptr) TOP() = call_method(self, _2, _1); \
else TOP() = NotImplemented; \ else TOP() = NotImplemented; \
} }
#define BINARY_OP_RSPECIAL(op, func) \ #define BINARY_OP_RSPECIAL(op, func) \
if(TOP() == NotImplemented){ \ if(is_not_implemented(TOP())){ \
PyVar self; \ PyVar self; \
PyVar _2 = get_unbound_method(_1, func, &self, false); \ PyVar _2 = get_unbound_method(_1, func, &self, false); \
if(_2 != nullptr) TOP() = call_method(self, _2, _0); \ if(_2 != nullptr) TOP() = call_method(self, _2, _0); \
else BinaryOptError(op, _0, _1); \ else BinaryOptError(op, _0, _1); \
if(TOP() == NotImplemented) BinaryOptError(op, _0, _1); \ if(is_not_implemented(TOP())) BinaryOptError(op, _0, _1); \
} }
case OP_BINARY_TRUEDIV:{ case OP_BINARY_TRUEDIV:{
@ -478,14 +478,14 @@ __NEXT_STEP:
PyVar _0 = TOP(); PyVar _0 = TOP();
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__truediv__); BINARY_OP_SPECIAL(__truediv__);
if(TOP() == NotImplemented) BinaryOptError("/", _0, _1); if(is_not_implemented(TOP())) BinaryOptError("/", _0, _1);
} DISPATCH() } DISPATCH()
case OP_BINARY_POW:{ case OP_BINARY_POW:{
PyVar _1 = POPX(); PyVar _1 = POPX();
PyVar _0 = TOP(); PyVar _0 = TOP();
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__pow__); BINARY_OP_SPECIAL(__pow__);
if(TOP() == NotImplemented) BinaryOptError("**", _0, _1); if(is_not_implemented(TOP())) BinaryOptError("**", _0, _1);
} DISPATCH() } DISPATCH()
case OP_BINARY_ADD:{ case OP_BINARY_ADD:{
PyVar _1 = POPX(); PyVar _1 = POPX();
@ -517,7 +517,7 @@ __NEXT_STEP:
PREDICT_INT_DIV_OP(/) PREDICT_INT_DIV_OP(/)
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__floordiv__); BINARY_OP_SPECIAL(__floordiv__);
if(TOP() == NotImplemented) BinaryOptError("//", _0, _1); if(is_not_implemented(TOP())) BinaryOptError("//", _0, _1);
} DISPATCH() } DISPATCH()
case OP_BINARY_MOD:{ case OP_BINARY_MOD:{
PyVar _1 = POPX(); PyVar _1 = POPX();
@ -525,7 +525,7 @@ __NEXT_STEP:
PREDICT_INT_DIV_OP(%) PREDICT_INT_DIV_OP(%)
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__mod__); BINARY_OP_SPECIAL(__mod__);
if(TOP() == NotImplemented) BinaryOptError("%", _0, _1); if(is_not_implemented(TOP())) BinaryOptError("%", _0, _1);
} DISPATCH() } DISPATCH()
case OP_COMPARE_LT:{ case OP_COMPARE_LT:{
PyVar _1 = POPX(); PyVar _1 = POPX();
@ -567,7 +567,7 @@ __NEXT_STEP:
PREDICT_INT_OP(<<) PREDICT_INT_OP(<<)
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__lshift__); BINARY_OP_SPECIAL(__lshift__);
if(TOP() == NotImplemented) BinaryOptError("<<", _0, _1); if(is_not_implemented(TOP())) BinaryOptError("<<", _0, _1);
} DISPATCH() } DISPATCH()
case OP_BITWISE_RSHIFT:{ case OP_BITWISE_RSHIFT:{
PyVar _1 = POPX(); PyVar _1 = POPX();
@ -575,7 +575,7 @@ __NEXT_STEP:
PREDICT_INT_OP(>>) PREDICT_INT_OP(>>)
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__rshift__); BINARY_OP_SPECIAL(__rshift__);
if(TOP() == NotImplemented) BinaryOptError(">>", _0, _1); if(is_not_implemented(TOP())) BinaryOptError(">>", _0, _1);
} DISPATCH() } DISPATCH()
case OP_BITWISE_AND:{ case OP_BITWISE_AND:{
PyVar _1 = POPX(); PyVar _1 = POPX();
@ -583,7 +583,7 @@ __NEXT_STEP:
PREDICT_INT_OP(&) PREDICT_INT_OP(&)
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__and__); BINARY_OP_SPECIAL(__and__);
if(TOP() == NotImplemented) BinaryOptError("&", _0, _1); if(is_not_implemented(TOP())) BinaryOptError("&", _0, _1);
} DISPATCH() } DISPATCH()
case OP_BITWISE_OR:{ case OP_BITWISE_OR:{
PyVar _1 = POPX(); PyVar _1 = POPX();
@ -591,7 +591,7 @@ __NEXT_STEP:
PREDICT_INT_OP(|) PREDICT_INT_OP(|)
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__or__); BINARY_OP_SPECIAL(__or__);
if(TOP() == NotImplemented) BinaryOptError("|", _0, _1); if(is_not_implemented(TOP())) BinaryOptError("|", _0, _1);
} DISPATCH() } DISPATCH()
case OP_BITWISE_XOR:{ case OP_BITWISE_XOR:{
PyVar _1 = POPX(); PyVar _1 = POPX();
@ -599,14 +599,14 @@ __NEXT_STEP:
PREDICT_INT_OP(^) PREDICT_INT_OP(^)
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__xor__); BINARY_OP_SPECIAL(__xor__);
if(TOP() == NotImplemented) BinaryOptError("^", _0, _1); if(is_not_implemented(TOP())) BinaryOptError("^", _0, _1);
} DISPATCH() } DISPATCH()
case OP_BINARY_MATMUL:{ case OP_BINARY_MATMUL:{
PyVar _1 = POPX(); PyVar _1 = POPX();
PyVar _0 = TOP(); PyVar _0 = TOP();
const PyTypeInfo* _ti; const PyTypeInfo* _ti;
BINARY_OP_SPECIAL(__matmul__); BINARY_OP_SPECIAL(__matmul__);
if(TOP() == NotImplemented) BinaryOptError("@", _0, _1); if(is_not_implemented(TOP())) BinaryOptError("@", _0, _1);
} DISPATCH() } DISPATCH()
#undef BINARY_OP_SPECIAL #undef BINARY_OP_SPECIAL

View File

@ -229,18 +229,18 @@ namespace pkpy{
PyVar res; PyVar res;
if(ti->m__eq__){ if(ti->m__eq__){
res = ti->m__eq__(this, lhs, rhs); res = ti->m__eq__(this, lhs, rhs);
if(res != vm->NotImplemented) return res == vm->True; if(!is_not_implemented(res)) return res == vm->True;
} }
res = call_method(lhs, __eq__, rhs); res = call_method(lhs, __eq__, rhs);
if(res != vm->NotImplemented) return res == vm->True; if(!is_not_implemented(res)) return res == vm->True;
ti = _tp_info(rhs); ti = _tp_info(rhs);
if(ti->m__eq__){ if(ti->m__eq__){
res = ti->m__eq__(this, rhs, lhs); res = ti->m__eq__(this, rhs, lhs);
if(res != vm->NotImplemented) return res == vm->True; if(!is_not_implemented(res)) return res == vm->True;
} }
res = call_method(rhs, __eq__, lhs); res = call_method(rhs, __eq__, lhs);
if(res != vm->NotImplemented) return res == vm->True; if(!is_not_implemented(res)) return res == vm->True;
return false; return false;
} }