fix a bug

This commit is contained in:
blueloveTH 2023-02-21 01:08:39 +08:00
parent 2da3c23165
commit 9107e22e7f
5 changed files with 6 additions and 11 deletions

View File

@ -112,8 +112,8 @@ PyVar VM::run_frame(Frame* frame){
case OP_POP_TOP: frame->_pop(); continue; case OP_POP_TOP: frame->_pop(); continue;
case OP_BINARY_OP: { case OP_BINARY_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(BINARY_SPECIAL_METHODS[byte.arg], std::move(args)); frame->top() = fast_call(BINARY_SPECIAL_METHODS[byte.arg], std::move(args));
} continue; } continue;
case OP_BITWISE_OP: { case OP_BITWISE_OP: {

View File

@ -39,13 +39,9 @@
#if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(__i386__) #if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(__i386__)
typedef int32_t i64; typedef int32_t i64;
typedef float f64; typedef float f64;
const i64 kMinSafeInt = -((i64)1 << 30);
const i64 kMaxSafeInt = ((i64)1 << 30) - 1;
#else #else
typedef int64_t i64; typedef int64_t i64;
typedef double f64; typedef double f64;
const i64 kMinSafeInt = -((i64)1 << 62);
const i64 kMaxSafeInt = ((i64)1 << 62) - 1;
#endif #endif
struct Dummy { char _; }; struct Dummy { char _; };

View File

@ -86,7 +86,7 @@ void init_builtins(VM* _vm) {
_vm->bind_builtin_func<1>("hash", [](VM* vm, pkpy::Args& args){ _vm->bind_builtin_func<1>("hash", [](VM* vm, pkpy::Args& args){
i64 value = vm->hash(args[0]); i64 value = vm->hash(args[0]);
if(value < kMinSafeInt || value > kMaxSafeInt) value >>= 2; if(((value << 2) >> 2) != value) value >>= 2;
return vm->PyInt(value); return vm->PyInt(value);
}); });

View File

@ -551,7 +551,7 @@ public:
} }
inline PyVar PyInt(i64 value) { inline PyVar PyInt(i64 value) {
if(value < kMinSafeInt || value > kMaxSafeInt){ if(((value << 2) >> 2) != value){
_error("OverflowError", std::to_string(value) + " is out of range"); _error("OverflowError", std::to_string(value) + " is out of range");
} }
value = (value << 2) | 0b01; value = (value << 2) | 0b01;

View File

@ -120,9 +120,8 @@ assert round(-23.8) == -24
assert 7**21 == 558545864083284007 assert 7**21 == 558545864083284007
assert 7**22 == 3909821048582988049 assert 2**60 == 1152921504606846976
assert 2**61 == 2305843009213693952 assert -2**60 == -1152921504606846976
assert -2**61 == -2305843009213693952
assert eq(2**-2, 0.25) assert eq(2**-2, 0.25)
assert 0**0 == 1 assert 0**0 == 1
assert 0**1 == 0 assert 0**1 == 0