some refactor

This commit is contained in:
blueloveTH 2024-04-24 17:54:55 +08:00
parent 4ea33da1ec
commit 508cbfdb75
9 changed files with 35 additions and 28 deletions

View File

@ -16,7 +16,7 @@ struct NativeProxyFuncC final: NativeProxyFuncCBase {
NativeProxyFuncC(_Fp func) : func(func) {} NativeProxyFuncC(_Fp func) : func(func) {}
PyObject* operator()(VM* vm, ArgsView args) override { PyObject* operator()(VM* vm, ArgsView args) override {
PK_ASSERT(args.size() == N); PK_DEBUG_ASSERT(args.size() == N);
return call<Ret>(vm, args, std::make_index_sequence<N>()); return call<Ret>(vm, args, std::make_index_sequence<N>());
} }
@ -40,7 +40,7 @@ struct NativeProxyMethodC final: NativeProxyFuncCBase {
NativeProxyMethodC(_Fp func) : func(func) {} NativeProxyMethodC(_Fp func) : func(func) {}
PyObject* operator()(VM* vm, ArgsView args) override { PyObject* operator()(VM* vm, ArgsView args) override {
PK_ASSERT(args.size() == N+1); PK_DEBUG_ASSERT(args.size() == N+1);
return call<Ret>(vm, args, std::make_index_sequence<N>()); return call<Ret>(vm, args, std::make_index_sequence<N>());
} }

View File

@ -136,9 +136,7 @@ struct UserData{
T get() const{ T get() const{
static_assert(std::is_trivially_copyable_v<T>); static_assert(std::is_trivially_copyable_v<T>);
static_assert(sizeof(T) <= sizeof(data)); static_assert(sizeof(T) <= sizeof(data));
#if PK_DEBUG_EXTRA_CHECK PK_DEBUG_ASSERT(!empty);
PK_ASSERT(!empty);
#endif
return reinterpret_cast<const T&>(data); return reinterpret_cast<const T&>(data);
} }
}; };

View File

@ -117,6 +117,12 @@ struct Type {
#define PK_ASSERT(x) if(!(x)) PK_FATAL_ERROR(); #define PK_ASSERT(x) if(!(x)) PK_FATAL_ERROR();
#if PK_DEBUG_EXTRA_CHECK
#define PK_DEBUG_ASSERT(x) if(!(x)) PK_FATAL_ERROR();
#else
#define PK_DEBUG_ASSERT(x)
#endif
struct PyObject; struct PyObject;
#define PK_BITS(p) (reinterpret_cast<i64>(p)) #define PK_BITS(p) (reinterpret_cast<i64>(p))
#define PK_SMALL_INT(val) (reinterpret_cast<PyObject*>(val << 2 | 0b10)) #define PK_SMALL_INT(val) (reinterpret_cast<PyObject*>(val << 2 | 0b10))

View File

@ -55,12 +55,7 @@ struct ValueStack {
bool empty() const { return _sp == _begin; } bool empty() const { return _sp == _begin; }
PyObject** begin() { return _begin; } PyObject** begin() { return _begin; }
PyObject** end() { return _sp; } PyObject** end() { return _sp; }
void reset(PyObject** sp) { void reset(PyObject** sp) { _sp = sp; }
#if PK_DEBUG_EXTRA_CHECK
if(sp < _begin || sp > _begin + MAX_SIZE) PK_FATAL_ERROR();
#endif
_sp = sp;
}
void clear() { _sp = _begin; } void clear() { _sp = _begin; }
bool is_overflow() const { return _sp >= _max_end; } bool is_overflow() const { return _sp >= _max_end; }
@ -101,9 +96,7 @@ struct Frame {
int next_bytecode() { int next_bytecode() {
_ip = _next_ip++; _ip = _next_ip++;
#if PK_DEBUG_EXTRA_CHECK PK_DEBUG_ASSERT(_ip >= 0 && _ip < co->codes.size());
if(_ip >= co->codes.size()) PK_FATAL_ERROR();
#endif
return _ip; return _ip;
} }
@ -152,16 +145,17 @@ struct CallStack{
} }
void pop(){ void pop(){
#if PK_DEBUG_EXTRA_CHECK PK_DEBUG_ASSERT(!empty())
if(empty()) PK_FATAL_ERROR();
#endif
LinkedFrame* p = _tail; LinkedFrame* p = _tail;
_tail = p->f_back; _tail = p->f_back;
pool64_dealloc(p); pool64_dealloc(p);
--_size; --_size;
} }
Frame& top() const { return _tail->frame; } Frame& top() const {
PK_DEBUG_ASSERT(!empty())
return _tail->frame;
}
template<typename Func> template<typename Func>
void apply(Func&& f){ void apply(Func&& f){

View File

@ -100,8 +100,16 @@ struct PyObject{
NameDict* _attr; NameDict* _attr;
bool is_attr_valid() const noexcept { return _attr != nullptr; } bool is_attr_valid() const noexcept { return _attr != nullptr; }
NameDict& attr() { return *_attr; }
PyObject* attr(StrName name) const { return (*_attr)[name]; } NameDict& attr() {
PK_DEBUG_ASSERT(is_attr_valid())
return *_attr;
}
PyObject* attr(StrName name) const {
PK_DEBUG_ASSERT(is_attr_valid())
return (*_attr)[name];
}
virtual void _obj_gc_mark() = 0; virtual void _obj_gc_mark() = 0;
@ -132,9 +140,7 @@ inline bool is_float(PyObject* p) noexcept { return !is_tagged(p) && p->type.ind
inline bool is_int(PyObject* p) noexcept { return is_small_int(p) || is_heap_int(p); } inline bool is_int(PyObject* p) noexcept { return is_small_int(p) || is_heap_int(p); }
inline bool is_type(PyObject* obj, Type type) { inline bool is_type(PyObject* obj, Type type) {
#if PK_DEBUG_EXTRA_CHECK PK_DEBUG_ASSERT(obj != nullptr)
if(obj == nullptr) throw std::runtime_error("is_type() called with nullptr");
#endif
return is_small_int(obj) ? type.index == kTpIntIndex : obj->type == type; return is_small_int(obj) ? type.index == kTpIntIndex : obj->type == type;
} }

View File

@ -397,7 +397,7 @@ namespace pkpy{
int for_codei = ctx->emit_(OP_FOR_ITER, BC_NOARG, BC_KEEPLINE); int for_codei = ctx->emit_(OP_FOR_ITER, BC_NOARG, BC_KEEPLINE);
bool ok = vars->emit_store(ctx); bool ok = vars->emit_store(ctx);
// this error occurs in `vars` instead of this line, but...nevermind // this error occurs in `vars` instead of this line, but...nevermind
PK_ASSERT(ok); // TODO: raise a SyntaxError instead if(!ok) throw std::runtime_error("SyntaxError");
ctx->try_merge_for_iter_store(for_codei); ctx->try_merge_for_iter_store(for_codei);
if(cond){ if(cond){
cond->emit_(ctx); cond->emit_(ctx);

View File

@ -518,7 +518,7 @@ void init_builtins(VM* _vm) {
char* p_end; char* p_end;
try{ try{
float_out = std::strtod(s.data, &p_end); float_out = std::strtod(s.data, &p_end);
PK_ASSERT(p_end == s.end()); if(p_end != s.end()) throw 1;
}catch(...){ }catch(...){
vm->ValueError("invalid literal for float(): " + s.escape()); vm->ValueError("invalid literal for float(): " + s.escape());
} }

View File

@ -953,9 +953,7 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
// [type, NULL, args..., kwargs...] // [type, NULL, args..., kwargs...]
PyObject* new_f = find_name_in_mro(PK_OBJ_GET(Type, callable), __new__); PyObject* new_f = find_name_in_mro(PK_OBJ_GET(Type, callable), __new__);
PyObject* obj; PyObject* obj;
#if PK_DEBUG_EXTRA_CHECK PK_DEBUG_ASSERT(new_f != nullptr && !method_call);
PK_ASSERT(new_f != nullptr && !method_call);
#endif
if(new_f == cached_object__new__) { if(new_f == cached_object__new__) {
// fast path for object.__new__ // fast path for object.__new__
obj = vm->heap.gcnew<DummyInstance>(PK_OBJ_GET(Type, callable)); obj = vm->heap.gcnew<DummyInstance>(PK_OBJ_GET(Type, callable));

View File

@ -90,3 +90,8 @@ assert -2e-3j == -0.002j
assert 3.4e-3 == 0.0034 assert 3.4e-3 == 0.0034
assert 3.4e+3 == 3400.0 assert 3.4e+3 == 3400.0
try:
float('-x13')
exit(1)
except ValueError:
pass