Add better error message
This commit is contained in:
107
src/vm/exception.cpp
Normal file
107
src/vm/exception.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "CrossLang.hpp"
|
||||
|
||||
namespace Tesses::CrossLang {
|
||||
TObject TCallable::CallWithFatalError(GCList& ls, std::vector<TObject> args)
|
||||
{
|
||||
try {
|
||||
return Call(ls,args);
|
||||
}
|
||||
catch(VMByteCodeException& ex)
|
||||
{
|
||||
ThrowFatalError(ex);
|
||||
}
|
||||
catch(VMException& ex)
|
||||
{
|
||||
ThrowFatalError(ex);
|
||||
}
|
||||
catch(SyntaxException& ex)
|
||||
{
|
||||
ThrowFatalError(ex);
|
||||
}
|
||||
catch(std::runtime_error& ex)
|
||||
{
|
||||
ThrowFatalError(ex);
|
||||
}
|
||||
catch(std::exception& ex)
|
||||
{
|
||||
ThrowFatalError(ex);
|
||||
}
|
||||
return (int64_t)1;
|
||||
}
|
||||
void ThrowFatalError(std::exception& ex)
|
||||
{
|
||||
using namespace Tesses::Framework::TextStreams;
|
||||
std::exception* ex2 = &ex;
|
||||
auto clexcept = dynamic_cast<VMByteCodeException*>(ex2);
|
||||
auto clrtexcept = dynamic_cast<VMException*>(ex2);
|
||||
auto compiler = dynamic_cast<SyntaxException*>(ex2);
|
||||
Tesses::Framework::TextStreams::ConsoleWriter error(true);
|
||||
|
||||
if(clexcept != nullptr)
|
||||
{
|
||||
error.WriteLine("CrossLang has encountered a fatal exception");
|
||||
error.WriteLine();
|
||||
|
||||
error.WriteLine(ToString(clexcept->GetGCList()->GetGC(),clexcept->exception));
|
||||
error.WriteLine();
|
||||
error.WriteLine("STACKTRACE:");
|
||||
|
||||
for(auto ittr = clexcept->stack_trace.crbegin(); ittr != clexcept->stack_trace.crend(); ittr++)
|
||||
{
|
||||
auto item = *ittr;
|
||||
std::string text = "\t";
|
||||
text += item->callable->closure->name.value_or((std::string)"<Closure>");
|
||||
text += "(";
|
||||
bool first=true;
|
||||
for(auto& arg : item->callable->closure->args)
|
||||
{
|
||||
if(!first) text += ", ";
|
||||
text += arg;
|
||||
first=false;
|
||||
}
|
||||
text += ")";
|
||||
if(item->srcline >= 1)
|
||||
{
|
||||
text += " at ";
|
||||
text += item->srcfile;
|
||||
text += ":";
|
||||
text += std::to_string(item->srcline);
|
||||
}
|
||||
error.WriteLine(text);
|
||||
}
|
||||
|
||||
exit(1);
|
||||
}
|
||||
if(clrtexcept != nullptr)
|
||||
{
|
||||
error.WriteLine("CrossLang has encountered a fatal runtime exception");
|
||||
error.WriteLine();
|
||||
error.WriteLine(clrtexcept->what());
|
||||
exit(1);
|
||||
}
|
||||
if(compiler != nullptr)
|
||||
{
|
||||
error.WriteLine("CrossLang has encountered a compiler error");
|
||||
error.WriteLine();
|
||||
error.WriteLine(compiler->Message());
|
||||
error.WriteLine();
|
||||
auto li = compiler->LineInfo();
|
||||
error.Write("in file: ");
|
||||
error.Write(li.filename);
|
||||
error.Write(":");
|
||||
error.Write((int64_t)li.line);
|
||||
error.Write(":");
|
||||
error.Write((int64_t)li.column);
|
||||
error.Write(":");
|
||||
error.WriteLine((int64_t)li.offset);
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
error.WriteLine("CrossLang has encountered a fatal C++ exception");
|
||||
error.WriteLine();
|
||||
error.Write("what(): ");
|
||||
error.WriteLine(ex2->what());
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -4237,7 +4237,7 @@ namespace Tesses::CrossLang {
|
||||
if(args.size() > 0)
|
||||
{
|
||||
try {
|
||||
throw VMByteCodeException(gc,args[0]);
|
||||
throw VMByteCodeException(gc,args[0],cse.back());
|
||||
} catch(...) {
|
||||
ttask->SetFailed(std::current_exception());
|
||||
}
|
||||
@@ -7012,7 +7012,7 @@ namespace Tesses::CrossLang {
|
||||
{
|
||||
auto env = cse.back()->env;
|
||||
if(!env->GetRootEnvironment()->HandleException(gc,env, _res2))
|
||||
throw VMByteCodeException(gc,_res2);
|
||||
throw VMByteCodeException(gc,_res2,cse.back());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -7167,6 +7167,24 @@ namespace Tesses::CrossLang {
|
||||
auto stk = cse.back();
|
||||
|
||||
stk->Push(gc,Undefined());
|
||||
return false;
|
||||
}
|
||||
bool InterperterThread::LineInfo(GC* gc)
|
||||
{
|
||||
GCList ls(gc);
|
||||
std::vector<CallStackEntry*>& cse=this->call_stack_entries;
|
||||
auto stk = cse.back();
|
||||
auto file = stk->Pop(ls);
|
||||
auto line = stk->Pop(ls);
|
||||
|
||||
//Set the fields in this cse
|
||||
GetObject(file,stk->srcfile);
|
||||
GetObject(line,stk->srcline);
|
||||
|
||||
//TODO: implement lines (this will make the language work until we get it rigged up)
|
||||
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
bool InterperterThread::PushFalse(GC* gc)
|
||||
@@ -7867,6 +7885,9 @@ namespace Tesses::CrossLang {
|
||||
{
|
||||
CallStackEntry* cse = new CallStackEntry();
|
||||
cse->mustReturn=false;
|
||||
cse->srcline = -1;
|
||||
cse->srcfile = "";
|
||||
cse->thread=nullptr;
|
||||
GC* _gc = ls.GetGC();
|
||||
ls.Add(cse);
|
||||
_gc->Watch(cse);
|
||||
@@ -7877,6 +7898,9 @@ namespace Tesses::CrossLang {
|
||||
{
|
||||
CallStackEntry* cse = new CallStackEntry();
|
||||
cse->mustReturn=false;
|
||||
cse->srcline = -1;
|
||||
cse->srcfile = "";
|
||||
cse->thread=nullptr;
|
||||
GC* _gc = ls->GetGC();
|
||||
ls->Add(cse);
|
||||
_gc->Watch(cse);
|
||||
@@ -7886,6 +7910,7 @@ namespace Tesses::CrossLang {
|
||||
{
|
||||
ls.GetGC()->BarrierBegin();
|
||||
CallStackEntry* cse = CallStackEntry::Create(ls);
|
||||
cse->thread = this;
|
||||
cse->callable = closure;
|
||||
cse->env = closure->chunkId == 0 ? closure->env : closure->ownScope ? closure->env->GetSubEnvironment(ls) : closure->env;
|
||||
cse->ip = 0;
|
||||
|
||||
@@ -64,7 +64,7 @@ static opcode opcodes[256]={
|
||||
&InterperterThread::JumpIfContinue,
|
||||
&InterperterThread::JumpIfDefined,
|
||||
&InterperterThread::DeclareConstVariable,
|
||||
&InterperterThread::Illegal,
|
||||
&InterperterThread::LineInfo,
|
||||
&InterperterThread::Illegal,
|
||||
&InterperterThread::Illegal,
|
||||
&InterperterThread::Illegal,
|
||||
|
||||
Reference in New Issue
Block a user