mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
improve exception performance
This commit is contained in:
parent
4cd84bbada
commit
2cf3bdc097
@ -34,21 +34,41 @@ struct SourceData {
|
||||
|
||||
SourceData(const Str& source, const Str& filename, CompileMode mode);
|
||||
std::pair<const char*,const char*> get_line(int lineno) const;
|
||||
Str snapshot(int lineno, const char* cursor=nullptr, std::string_view name="");
|
||||
Str snapshot(int lineno, const char* cursor, std::string_view name) const;
|
||||
};
|
||||
|
||||
struct ExceptionLine{
|
||||
std::shared_ptr<SourceData> src;
|
||||
int lineno;
|
||||
const char* cursor;
|
||||
std::string name;
|
||||
|
||||
Str snapshot() const { return src->snapshot(lineno, cursor, name); }
|
||||
|
||||
ExceptionLine(std::shared_ptr<SourceData> src, int lineno, const char* cursor, std::string_view name):
|
||||
src(src), lineno(lineno), cursor(cursor), name(name) {}
|
||||
};
|
||||
|
||||
struct Exception {
|
||||
StrName type;
|
||||
Str msg;
|
||||
bool is_re;
|
||||
stack<Str> stacktrace;
|
||||
|
||||
int _ip_on_error;
|
||||
void* _code_on_error;
|
||||
|
||||
Exception(StrName type, Str msg): type(type), msg(msg), is_re(true), _ip_on_error(-1), _code_on_error(nullptr) {}
|
||||
stack<ExceptionLine> stacktrace;
|
||||
|
||||
Exception(StrName type, Str msg):
|
||||
type(type), msg(msg), is_re(true), _ip_on_error(-1), _code_on_error(nullptr) {}
|
||||
bool match_type(StrName t) const { return this->type == t;}
|
||||
void st_push(Str&& snapshot);
|
||||
|
||||
template<typename... Args>
|
||||
void st_push(Args&&... args){
|
||||
if(stacktrace.size() >= 8) return;
|
||||
stacktrace.emplace(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
Str summary() const;
|
||||
};
|
||||
|
||||
|
@ -29,7 +29,7 @@ namespace pkpy{
|
||||
return {_start, i};
|
||||
}
|
||||
|
||||
Str SourceData::snapshot(int lineno, const char* cursor, std::string_view name){
|
||||
Str SourceData::snapshot(int lineno, const char* cursor, std::string_view name) const{
|
||||
std::stringstream ss;
|
||||
ss << " " << "File \"" << filename << "\", line " << lineno;
|
||||
if(!name.empty()) ss << ", in " << name;
|
||||
@ -50,16 +50,14 @@ namespace pkpy{
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void Exception::st_push(Str&& snapshot){
|
||||
if(stacktrace.size() >= 8) return;
|
||||
stacktrace.push(std::move(snapshot));
|
||||
}
|
||||
|
||||
Str Exception::summary() const {
|
||||
stack<Str> st(stacktrace);
|
||||
stack<ExceptionLine> st(stacktrace);
|
||||
std::stringstream ss;
|
||||
if(is_re) ss << "Traceback (most recent call last):\n";
|
||||
while(!st.empty()) { ss << st.top() << '\n'; st.pop(); }
|
||||
while(!st.empty()) {
|
||||
ss << st.top().snapshot() << '\n';
|
||||
st.pop();
|
||||
}
|
||||
if (!msg.empty()) ss << type.sv() << ": " << msg;
|
||||
else ss << type.sv();
|
||||
return ss.str();
|
||||
|
@ -454,8 +454,8 @@ static bool is_unicode_Lo_char(uint32_t c) {
|
||||
}
|
||||
|
||||
void Lexer::throw_err(Str type, Str msg, int lineno, const char* cursor){
|
||||
auto e = Exception(type, msg);
|
||||
e.st_push(src->snapshot(lineno, cursor));
|
||||
Exception e(type, msg);
|
||||
e.st_push(src, lineno, cursor, "");
|
||||
throw e;
|
||||
}
|
||||
|
||||
|
@ -1095,7 +1095,7 @@ void VM::_raise(bool re_raise){
|
||||
int current_line = frame->co->lines[actual_ip]; // current line
|
||||
auto current_f_name = frame->co->name.sv(); // current function name
|
||||
if(frame->_callable == nullptr) current_f_name = ""; // not in a function
|
||||
e.st_push(frame->co->src->snapshot(current_line, nullptr, current_f_name));
|
||||
e.st_push(frame->co->src, current_line, nullptr, current_f_name);
|
||||
|
||||
if(ok) throw HandledException();
|
||||
else throw UnhandledException();
|
||||
|
Loading…
x
Reference in New Issue
Block a user